Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions MBINCompiler/Source/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ 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 = "<Glob Pattern>[;<Glob Pattern>...]",
description = "\nFilter all files to exclude any that match the " +
"glob patterns. A glob pattern is a filepath with wildcards." +
"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" +
Expand Down
57 changes: 32 additions & 25 deletions MBINCompiler/Source/Commands/ConvertMode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections.Generic;

using libMBIN;
Expand All @@ -8,6 +10,23 @@ namespace MBINCompiler.Commands {

using static CommandLineOptions;

public static class StringExtensions {
/// <summary>
/// Compares the string against a given pattern.
/// Code credit: https://stackoverflow.com/a/4146349
/// </summary>
/// <param name="str">The string.</param>
/// <param name="pattern">The pattern to match, where "*" means any sequence of characters, and "?" means any single character.</param>
/// <returns><c>true</c> if the string matches the given pattern; otherwise <c>false</c>.</returns>
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<ConvertCommand> {

public override int ExecuteCommand( CommandLineParser options ) {
Expand Down Expand Up @@ -60,12 +79,8 @@ public override int ExecuteCommand( CommandLineParser options ) {

var defaultExclude = @"";

//#if DEBUG
//defaultExclude = "";
//#endif

IncludeFilters = new List<string>((optIncludes?.value ?? defaultInclude).Split(';'));
ExcludeFilters = new List<string>((optExcludes?.value ?? defaultExclude).Split(';'));
IncludeFilters = new List<string>((optIncludes?.value.ToUpper() ?? defaultInclude).Split(';'));
ExcludeFilters = new List<string>((optExcludes?.value.ToUpper() ?? defaultExclude).Split(';'));

// if not auto-detecting then OutputFormat can be excluded
if ( !autoFormat ) ExcludeFilters.Add( $"*.{OutputFormat}" );
Expand Down Expand Up @@ -150,30 +165,22 @@ private static bool GetFileList( List<string> paths, out List<string> fileList )
private static List<string> GetFilteredFiles( string path ) {
var files = new List<string>();

var includeFiles = new List<string>();
var excludeFiles = new List<string>();
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();
Expand Down
Loading