From b410b81bcad6872d2d270f88720ccf294a7b3647 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 31 Aug 2024 13:23:44 +0200 Subject: [PATCH] use MatchesSimpleExpression instead of janky regex --- Contentless/OverrideInfo.cs | 12 +++++------- Contentless/Program.cs | 13 ++++--------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/Contentless/OverrideInfo.cs b/Contentless/OverrideInfo.cs index d29413d..6cf0e2d 100644 --- a/Contentless/OverrideInfo.cs +++ b/Contentless/OverrideInfo.cs @@ -1,15 +1,13 @@ -using System.Text.RegularExpressions; - -namespace Contentless; +namespace Contentless; public class OverrideInfo { - public readonly Regex Regex; + public readonly string Expression; public readonly Override Override; - public OverrideInfo(Regex regex, Override over) { - this.Regex = regex; + public OverrideInfo(string expression, Override over) { + this.Expression = expression; this.Override = over; } -} \ No newline at end of file +} diff --git a/Contentless/Program.cs b/Contentless/Program.cs index 11baf57..1e9d5e8 100644 --- a/Contentless/Program.cs +++ b/Contentless/Program.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; using System.IO; +using System.IO.Enumeration; using System.Linq; using System.Reflection; -using System.Text.RegularExpressions; using Microsoft.Build.Construction; using Microsoft.Xna.Framework.Content.Pipeline; using Newtonsoft.Json; @@ -143,7 +143,6 @@ public static class Program { Console.WriteLine($"Found possible importer types {string.Join(", ", importers)}"); Console.WriteLine($"Found possible processor types {string.Join(", ", processors)}"); - var excluded = config.ExcludedFiles.Select(Program.GlobbyRegex).ToArray(); var overrides = Program.GetOverrides(config.Overrides).ToArray(); foreach (var file in contentFile.Directory.EnumerateFiles("*", SearchOption.AllDirectories)) { // is the file the content or config file? @@ -152,7 +151,7 @@ public static class Program { var relative = Path.GetRelativePath(contentFile.DirectoryName, file.FullName).Replace('\\', '/'); // is the file in an excluded directory? - if (excluded.Any(e => e.IsMatch(relative))) { + if (config.ExcludedFiles.Any(e => FileSystemName.MatchesSimpleExpression(e, relative))) { if (config.LogSkipped) Console.WriteLine($"Skipping excluded file {relative}"); continue; @@ -271,12 +270,12 @@ public static class Program { private static IEnumerable GetOverrides(Dictionary config) { foreach (var entry in config) - yield return new OverrideInfo(Program.GlobbyRegex(entry.Key), entry.Value); + yield return new OverrideInfo(entry.Key, entry.Value); } private static OverrideInfo GetOverrideFor(string file, IEnumerable overrides) { foreach (var over in overrides) { - if (over.Regex.IsMatch(file)) + if (FileSystemName.MatchesSimpleExpression(over.Expression, file)) return over; } return null; @@ -327,8 +326,4 @@ public static class Program { Console.WriteLine($"Adding file {relative} with the Copy build action"); } - private static Regex GlobbyRegex(string s) { - return new Regex(s.Replace(".", "[.]").Replace("*", ".*").Replace("?", ".")); - } - }