diff --git a/MBINCompiler/Source/CommandLineOptions.cs b/MBINCompiler/Source/CommandLineOptions.cs index 3ab871d54..cb6fb2093 100644 --- a/MBINCompiler/Source/CommandLineOptions.cs +++ b/MBINCompiler/Source/CommandLineOptions.cs @@ -142,7 +142,7 @@ internal set { "The * and ? wildcard characters can be used.\n" + "Multiple glob patterns are separated by a semicolon.\n" + "The default is --include=\"*.MBIN;*.MBIN.PC;*.MXML\" (all).\n" + - "The --include filter is applied before --exclude." }, + "Any pattern in the --include filter will not be applied if it matches --exclude also." }, new Option { longName = "exclude", param = "[;...]", description = "\nFilter all files to exclude any that match the " + @@ -150,7 +150,7 @@ internal set { "The * and ? wildcard characters can be used.\n" + "Multiple glob patterns are separated by a semicolon.\n" + "The default is --exclude=\"\" (nothing).\n" + - "The --exclude filter is applied after --include." }, + "The --exclude filter overrides any value in --include." }, new Option { shortName = 'V', longName = "format-version", param = "[0|1|2]", isHidden = true, description = "\nOutput using the specified MBIN format version.\n" + diff --git a/MBINCompiler/Source/Commands/ConvertMode.cs b/MBINCompiler/Source/Commands/ConvertMode.cs index 2d01d8937..594230bc2 100644 --- a/MBINCompiler/Source/Commands/ConvertMode.cs +++ b/MBINCompiler/Source/Commands/ConvertMode.cs @@ -1,5 +1,7 @@ using System; using System.IO; +using System.Linq; +using System.Text.RegularExpressions; using System.Collections.Generic; using libMBIN; @@ -8,6 +10,23 @@ namespace MBINCompiler.Commands { using static CommandLineOptions; + public static class StringExtensions { + /// + /// Compares the string against a given pattern. + /// Code credit: https://stackoverflow.com/a/4146349 + /// + /// The string. + /// The pattern to match, where "*" means any sequence of characters, and "?" means any single character. + /// true if the string matches the given pattern; otherwise false. + public static bool Like(this string str, string pattern) + { + return new Regex( + "^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$", + RegexOptions.IgnoreCase | RegexOptions.Singleline + ).IsMatch(str); + } + } + internal class ConvertCommand : Command { public override int ExecuteCommand( CommandLineParser options ) { @@ -60,12 +79,8 @@ public override int ExecuteCommand( CommandLineParser options ) { var defaultExclude = @""; - //#if DEBUG - //defaultExclude = ""; - //#endif - - IncludeFilters = new List((optIncludes?.value ?? defaultInclude).Split(';')); - ExcludeFilters = new List((optExcludes?.value ?? defaultExclude).Split(';')); + IncludeFilters = new List((optIncludes?.value.ToUpper() ?? defaultInclude).Split(';')); + ExcludeFilters = new List((optExcludes?.value.ToUpper() ?? defaultExclude).Split(';')); // if not auto-detecting then OutputFormat can be excluded if ( !autoFormat ) ExcludeFilters.Add( $"*.{OutputFormat}" ); @@ -150,30 +165,22 @@ private static bool GetFileList( List paths, out List fileList ) private static List GetFilteredFiles( string path ) { var files = new List(); - var includeFiles = new List(); - var excludeFiles = new List(); - foreach ( var filter in IncludeFilters ) { - includeFiles.AddRange( GetDirectoryFiles( path, filter ) ); - } - foreach ( var filter in ExcludeFilters ) { - excludeFiles.AddRange( GetDirectoryFiles( path, filter ) ); - } - - // add the filtered files to fileList - foreach ( var file in includeFiles ) { - if ( !excludeFiles.Contains( file ) ) files.Add( file ); + var allFiles = Directory.EnumerateFiles(path, "", SearchOption.AllDirectories); + foreach (string fname in allFiles) { + string upperFname = fname.ToUpper(); + if (ExcludeFilters.Count > 0) { + if (ExcludeFilters.Any(ext => upperFname.Like(ext))) { + continue; + } + } + if (IncludeFilters.Any(ext => upperFname.Like(ext))) { + files.Add(fname); + } } return files; } - private static string[] GetDirectoryFiles( string path, string filter ) { - try { - return Directory.GetFiles( path, filter, SearchOption.AllDirectories ); - } catch ( DirectoryNotFoundException ) { } - return new string[] { }; - } - private static string DetectFormat( string file, ref bool foundMBIN, ref bool foundMXML ) { if ( Path.HasExtension( file ) ) { var ext = Path.GetExtension( file ).ToUpper();