diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2022-11-10 21:50:01 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2022-11-10 21:50:01 -0500 |
commit | 303b3924ae3ef905d77b9d7ef0f9efc70e58c2b8 (patch) | |
tree | 6e2e7c5850151c2e1cd63796dfb89e4314e34852 | |
parent | b99dbf53bda9dc1178a3b6e8cbafea609f3ee6dc (diff) | |
download | SMAPI-303b3924ae3ef905d77b9d7ef0f9efc70e58c2b8.tar.gz SMAPI-303b3924ae3ef905d77b9d7ef0f9efc70e58c2b8.tar.bz2 SMAPI-303b3924ae3ef905d77b9d7ef0f9efc70e58c2b8.zip |
fix case where prefix ends with a path separator
-rw-r--r-- | src/SMAPI.Tests/Core/AssetNameTests.cs | 8 | ||||
-rw-r--r-- | src/SMAPI/Framework/Content/AssetName.cs | 23 |
2 files changed, 19 insertions, 12 deletions
diff --git a/src/SMAPI.Tests/Core/AssetNameTests.cs b/src/SMAPI.Tests/Core/AssetNameTests.cs index fbc94e95..fdaa2c01 100644 --- a/src/SMAPI.Tests/Core/AssetNameTests.cs +++ b/src/SMAPI.Tests/Core/AssetNameTests.cs @@ -151,6 +151,12 @@ namespace SMAPI.Tests.Core // with locale codes [TestCase("Data/Achievements.fr-FR", "Data/Achievements", ExpectedResult = true)] + + // prefix ends with path separator + [TestCase("Data/Events/Boop", "Data/Events/", ExpectedResult = true)] + [TestCase("Data/Events/Boop", "Data/Events\\", ExpectedResult = true)] + [TestCase("Data/Events", "Data/Events/", ExpectedResult = false)] + [TestCase("Data/Events", "Data/Events\\", ExpectedResult = false)] public bool StartsWith_SimpleCases(string mainAssetName, string prefix) { // arrange @@ -247,7 +253,7 @@ namespace SMAPI.Tests.Core [TestCase("Mods/SomeMod/SomeSubdirectory", "Mods/Some", false, ExpectedResult = false)] [TestCase("Mods/Jasper/Data", "Mods/Jas/Image", true, ExpectedResult = false)] [TestCase("Mods/Jasper/Data", "Mods/Jas/Image", true, ExpectedResult = false)] - public bool StartsWith_SubfolderWithPartial(string mainAssetName, string otherAssetName, bool allowSubfolder) + public bool StartsWith_PartialMatchInPathSegment(string mainAssetName, string otherAssetName, bool allowSubfolder) { // arrange mainAssetName = PathUtilities.NormalizeAssetName(mainAssetName); diff --git a/src/SMAPI/Framework/Content/AssetName.cs b/src/SMAPI/Framework/Content/AssetName.cs index 7b87c0c5..99968299 100644 --- a/src/SMAPI/Framework/Content/AssetName.cs +++ b/src/SMAPI/Framework/Content/AssetName.cs @@ -138,37 +138,38 @@ namespace StardewModdingAPI.Framework.Content return false; // get initial values - ReadOnlySpan<char> trimmed = prefix.AsSpan().Trim(); - if (trimmed.Length == 0) + ReadOnlySpan<char> trimmedPrefix = prefix.AsSpan().Trim(); + if (trimmedPrefix.Length == 0) return true; ReadOnlySpan<char> pathSeparators = new(ToolkitPathUtilities.PossiblePathSeparators); // just to simplify calling other span APIs // asset keys can't have a leading slash, but AssetPathYielder will trim them - if (pathSeparators.Contains(trimmed[0])) + if (pathSeparators.Contains(trimmedPrefix[0])) return false; // compare segments AssetNamePartEnumerator curParts = new(this.Name); - AssetNamePartEnumerator prefixParts = new(trimmed); + AssetNamePartEnumerator prefixParts = new(trimmedPrefix); while (true) { bool curHasMore = curParts.MoveNext(); bool prefixHasMore = prefixParts.MoveNext(); - // reached end of prefix or asset name + // reached end for one side if (prefixHasMore != curHasMore) { // mismatch: prefix is longer if (prefixHasMore) return false; - // possible match: all prefix segments matched. - return allowSubfolder || (pathSeparators.Contains(trimmed[^1]) ? curParts.Remainder.Length == 0 : curParts.Current.Length == 0); + // match if subfolder paths are fine (e.g. prefix 'Data/Events' with target 'Data/Events/Beach') + return allowSubfolder; } - // match: previous segments matched exactly and both reached the end + // previous segments matched exactly and both reached the end + // match if prefix doesn't end with '/' (which should only match subfolders) if (!prefixHasMore) - return true; + return !pathSeparators.Contains(trimmedPrefix[^1]); // compare segment if (curParts.Current.Length == prefixParts.Current.Length) @@ -188,7 +189,7 @@ namespace StardewModdingAPI.Framework.Content return false; // mismatch: something like "Maps/" would need an exact match - if (pathSeparators.Contains(trimmed[^1])) + if (pathSeparators.Contains(trimmedPrefix[^1])) return false; // mismatch: partial word match not allowed, and the first or last letter of the suffix isn't a word separator @@ -196,7 +197,7 @@ namespace StardewModdingAPI.Framework.Content return false; // possible match - return allowSubfolder || (pathSeparators.Contains(trimmed[^1]) ? curParts.Remainder.IndexOfAny(ToolkitPathUtilities.PossiblePathSeparators) < 0 : curParts.Remainder.Length == 0); + return allowSubfolder || (pathSeparators.Contains(trimmedPrefix[^1]) ? curParts.Remainder.IndexOfAny(ToolkitPathUtilities.PossiblePathSeparators) < 0 : curParts.Remainder.Length == 0); } } } |