Skip to content

Commit e409073

Browse files
authored
fix: search for _<state> instead of splitting on _ (AscensionGameDev#2109)
1 parent bfbfea6 commit e409073

File tree

1 file changed

+13
-30
lines changed

1 file changed

+13
-30
lines changed

Intersect.Editor/Content/ContentManager.cs

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -605,48 +605,31 @@ public static IEnumerable<string> GetOverridesFor(TextureType textureType, strin
605605
// Trim .png
606606
var baseName = Path.GetFileNameWithoutExtension(sprite);
607607

608-
// Split the baseName on _ but only until there is a maximum of 3 segments
609-
// So base_attack_custom_3 would be split to "base", "attack", "custom_3"
610-
// [if Split("_") or Split("_", 4) was used it would become "base", "attack", "custom", "3"]
611-
var parts = baseName.Split(OverridesDelimiter, 3);
612-
613-
// If there are less than 3 parts (base.png, base_attack.png would both have less than 3) mark invalid
614-
if (parts.Length < 3)
608+
var searchState = $"_{filterState}";
609+
var indexOfState = baseName.IndexOf(searchState);
610+
if (indexOfState < 1 || baseName.Length <= indexOfState + searchState.Length)
615611
{
616-
// By returning an empty enumerable instead of null, SelectMany() just reduces it
617-
// into an single-dimensional array without us doing an additional != null filter
618-
return Enumerable.Empty<string>();
612+
return Array.Empty<string>();
619613
}
620614

621-
622-
// If if the first part is not equal to our base filter, mark invalid
623-
if (!string.IsNullOrWhiteSpace(filterBase) && !string.Equals(parts[0], filterBase, StringComparison.OrdinalIgnoreCase))
615+
var customPart = baseName[(indexOfState + searchState.Length + 1)..].Trim();
616+
if (string.IsNullOrWhiteSpace(customPart))
624617
{
625-
return Enumerable.Empty<string>();
618+
return Array.Empty<string>();
626619
}
627620

628-
// If the second part is not equal to our state filter, mark invalid
629-
if (!string.Equals(filterState, parts[1], StringComparison.OrdinalIgnoreCase))
621+
if (string.IsNullOrWhiteSpace(filterBase))
630622
{
631-
return Enumerable.Empty<string>();
623+
return new[] { customPart };
632624
}
633625

634-
// If there are less than 3 parts (base.png, base_attack.png would both have less than 3,
635-
// or if the second part is not equal to our state name, return no matches
636-
if (parts.Length < 3 || !string.Equals(filterState, parts[1], StringComparison.OrdinalIgnoreCase))
626+
var basePart = baseName[0..indexOfState].Trim();
627+
if (string.Equals(filterBase.Trim(), basePart, StringComparison.Ordinal))
637628
{
638-
// By returning an empty enumerable instead of null, SelectMany() just reduces it
639-
// into an single-dimensional array without us doing an additional != null filter
640-
return Enumerable.Empty<string>();
629+
return new[] { customPart };
641630
}
642631

643-
// Return the 3rd segment
644-
// We do Skip(2) instead of Last() because Skip(2) returns an IEnumerable<string>
645-
// which will automatically be consumed correctly by SelectMany(), while Last()
646-
// or LastOrDefault() would return a string/string? which then would need to be
647-
// wrapped in a new []{ <last> } to turn it back into an IEnumerable<string>
648-
// to be consumed by SelectMany()
649-
return parts.Skip(2);
632+
return Array.Empty<string>();
650633
})
651634
/* Find only distinct values, don't show duplicates */
652635
.Distinct();

0 commit comments

Comments
 (0)