From d92015d071e41ced4037a51cfa7f5e49651a4fee Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Fri, 19 Nov 2021 11:03:24 -0800 Subject: [PATCH] Add sanitization for StarForce --- CHANGELIST.md | 1 + MPF.Library/Protection.cs | 26 +++++++++++++++++++++++++- MPF.Test/Library/ProtectionTests.cs | 27 +++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/CHANGELIST.md b/CHANGELIST.md index 834acb8b..f9aa0ac4 100644 --- a/CHANGELIST.md +++ b/CHANGELIST.md @@ -30,6 +30,7 @@ - Ensure parameters box is safer during options save - Fix protection sanitization and add regression tests - Update to DIC 20211101 +- Add protection sanitization for StarForce ### 2.1 (2021-07-22) - Enum, no more diff --git a/MPF.Library/Protection.cs b/MPF.Library/Protection.cs index 936cb61c..0c00b182 100644 --- a/MPF.Library/Protection.cs +++ b/MPF.Library/Protection.cs @@ -261,7 +261,31 @@ namespace MPF.Library // TODO: Figure this one out // StarForce - // TODO: Figure this one out + if (foundProtections.Any(p => p.StartsWith("StarForce"))) + { + if (foundProtections.Any(p => Regex.IsMatch(p, @"StarForce [0-9]+\..+"))) + { + foundProtections = foundProtections.Where(p => p != "StarForce") + .Where(p => p != "StarForce 3-5") + .Where(p => p != "StarForce 5") + .Where(p => p != "StarForce 5 [Protected Module]"); + } + else if (foundProtections.Any(p => p == "StarForce 5 [Protected Module]")) + { + foundProtections = foundProtections.Where(p => p != "StarForce") + .Where(p => p != "StarForce 3-5") + .Where(p => p != "StarForce 5"); + } + else if (foundProtections.Any(p => p == "StarForce 5")) + { + foundProtections = foundProtections.Where(p => p != "StarForce") + .Where(p => p != "StarForce 3-5"); + } + else if (foundProtections.Any(p => p == "StarForce 3-5")) + { + foundProtections = foundProtections.Where(p => p != "StarForce"); + } + } // Sysiphus if (foundProtections.Any(p => p == "Sysiphus") && foundProtections.Any(p => p.StartsWith("Sysiphus") && p.Length > "Sysiphus".Length)) diff --git a/MPF.Test/Library/ProtectionTests.cs b/MPF.Test/Library/ProtectionTests.cs index aa807215..67a5be24 100644 --- a/MPF.Test/Library/ProtectionTests.cs +++ b/MPF.Test/Library/ProtectionTests.cs @@ -215,6 +215,33 @@ namespace MPF.Test.Library Assert.Equal(protections[0], sanitized); } + [Theory] + [InlineData(0)] + [InlineData(1)] + [InlineData(2)] + [InlineData(3)] + public void SanitizeFoundProtectionStarForceTest(int skip) + { + List protections = new List() + { + "StarForce 1.20.000.000", + "StarForce 5 [Protected Module]", + "StarForce 5", + "StarForce 3-5", + "StarForce", + }; + + // Safeguard for the future + if (skip >= protections.Count) + throw new ArgumentException("Invalid skip value", nameof(skip)); + + // The list is in order of preference + protections = protections.Skip(skip).ToList(); + + string sanitized = Protection.SanitizeFoundProtections(protections); + Assert.Equal(protections[0], sanitized); + } + [Fact] public void SanitizeFoundProtectionsSysiphusTest() {