diff --git a/AssemblyRemapper/AssemblyRemapper.csproj b/AssemblyRemapper/AssemblyRemapper.csproj index 011673a..36791b1 100644 --- a/AssemblyRemapper/AssemblyRemapper.csproj +++ b/AssemblyRemapper/AssemblyRemapper.csproj @@ -9,6 +9,7 @@ + diff --git a/AssemblyRemapper/Commands/CommandProcessor.cs b/AssemblyRemapper/Commands/CommandProcessor.cs index 6915370..0ed70de 100644 --- a/AssemblyRemapper/Commands/CommandProcessor.cs +++ b/AssemblyRemapper/Commands/CommandProcessor.cs @@ -1,5 +1,4 @@ -using AssemblyRemapper.Remapper; -using AssemblyRemapper.Utils; +using AssemblyRemapper.Utils; namespace AssemblyRemapper.Commands { @@ -25,6 +24,10 @@ namespace AssemblyRemapper.Commands { var remapper = new Remapper.Remapper(); + Logger.ClearLog(); + Console.Clear(); + ShowStartText(); + DataProvider.LoadMappingFile(); DataProvider.LoadAssemblyDefinition(); diff --git a/AssemblyRemapper/Remapper/Search/Fields.cs b/AssemblyRemapper/Remapper/Search/Fields.cs index c13f4a4..65f9f5c 100644 --- a/AssemblyRemapper/Remapper/Search/Fields.cs +++ b/AssemblyRemapper/Remapper/Search/Fields.cs @@ -1,5 +1,73 @@ -namespace AssemblyRemapper.Remapper.Search; +using AssemblyRemapper.Enums; +using AssemblyRemapper.Models; +using Mono.Cecil; +using MoreLinq; + +namespace AssemblyRemapper.Remapper.Search; internal static class Fields { + /// + /// Returns a match on any type with the provided fields + /// + /// + /// + /// + /// + public static EMatchResult GetTypeWithFields(TypeDefinition type, SearchParams parms, ScoringModel score) + { + if (parms.MatchFields is null || parms.MatchFields.Count == 0) return EMatchResult.Disabled; + + var matches = type.Fields + .Where(field => parms.MatchFields.Contains(field.Name)) + .Count(); + + score.Score += matches; + + return matches > 0 + ? EMatchResult.Match + : EMatchResult.NoMatch; + } + + /// + /// Returns a match on any type without the provided fields + /// + /// + /// + /// + /// + public static EMatchResult GetTypeWithoutFields(TypeDefinition type, SearchParams parms, ScoringModel score) + { + if (parms.IgnoreFields is null || parms.IgnoreFields.Count == 0) return EMatchResult.Disabled; + + var matches = type.Fields + .Where(field => parms.IgnoreFields.Contains(field.Name)) + .Count(); + + score.Score += matches; + + return matches > 0 + ? EMatchResult.Match + : EMatchResult.NoMatch; + } + + /// + /// Returns a match on any type with a matching number of fields + /// + /// + /// + /// + /// + public static EMatchResult GetTypeByNumberOfFields(TypeDefinition type, SearchParams parms, ScoringModel score) + { + if (parms.FieldCount is null) return EMatchResult.Disabled; + + var match = type.Fields.Exactly((int)parms.FieldCount); + + if (match) { score.Score++; } + + return match + ? EMatchResult.Match + : EMatchResult.NoMatch; + } } \ No newline at end of file diff --git a/AssemblyRemapper/Remapper/Search/Methods.cs b/AssemblyRemapper/Remapper/Search/Methods.cs index 30af909..757939f 100644 --- a/AssemblyRemapper/Remapper/Search/Methods.cs +++ b/AssemblyRemapper/Remapper/Search/Methods.cs @@ -1,7 +1,7 @@ using AssemblyRemapper.Enums; using AssemblyRemapper.Models; using Mono.Cecil; -using Mono.Cecil.Rocks; +using MoreLinq; namespace AssemblyRemapper.Remapper.Search; @@ -16,23 +16,15 @@ internal static class Methods /// Match if type contains any supplied methods public static EMatchResult GetTypeWithMethods(TypeDefinition type, SearchParams parms, ScoringModel score) { - var matchCount = 0; + if (parms.MatchMethods is null || parms.MatchMethods.Count == 0) return EMatchResult.Disabled; - // Handle match methods - foreach (var method in type.Methods) - { - foreach (var name in parms.MatchMethods) - { - // Method name match - if (method.Name == name) - { - matchCount += 1; - score.Score++; - } - } - } + var matches = type.Methods + .Where(method => parms.MatchMethods.Contains(method.Name)) + .Count(); - return matchCount > 0 + score.Score += matches; + + return matches > 0 ? EMatchResult.Match : EMatchResult.NoMatch; } @@ -46,54 +38,29 @@ internal static class Methods /// Match if type has no methods public static EMatchResult GetTypeWithoutMethods(TypeDefinition type, SearchParams parms, ScoringModel score) { - if (parms.IgnoreMethods is null) return EMatchResult.Disabled; + if (parms.IgnoreMethods is null || parms.IgnoreMethods.Count == 0) return EMatchResult.Disabled; - var skippAll = parms.IgnoreMethods.Contains("*"); - var methodCount = type.Methods.Count - type.GetConstructors().Count(); + var matches = type.Methods + .Where(method => parms.IgnoreMethods.Contains(method.Name)) + .Count(); - // Subtract method count from constructor count to check for real methods - if (methodCount is 0 && skippAll is true) - { - score.Score++; - return EMatchResult.Match; - } + score.Score += matches; - return EMatchResult.NoMatch; - } - - /// - /// Get all types without the specified method - /// - /// - /// - /// - /// - public static EMatchResult GetTypeWithNoMethods(TypeDefinition type, SearchParams parms, ScoringModel score) - { - if (parms.IgnoreMethods is null) { return EMatchResult.Disabled; } - - foreach (var method in type.Methods) - { - if (parms.IgnoreMethods.Contains(method.Name)) - { - return EMatchResult.NoMatch; - } - } - - score.Score++; - return EMatchResult.Match; + return matches > 0 + ? EMatchResult.Match + : EMatchResult.NoMatch; } public static EMatchResult GetTypeByNumberOfMethods(TypeDefinition type, SearchParams parms, ScoringModel score) { - if (parms.MethodCount is null) { return EMatchResult.Disabled; } + if (parms.MethodCount is null) return EMatchResult.Disabled; - if (type.Methods.Count == parms.MethodCount) - { - score.Score++; - return EMatchResult.Match; - } + var match = type.Methods.Exactly((int)parms.MethodCount); - return EMatchResult.NoMatch; + if (match) { score.Score++; } + + return match + ? EMatchResult.Match + : EMatchResult.NoMatch; } } \ No newline at end of file diff --git a/AssemblyRemapper/Remapper/Search/TypeDefExtensions.cs b/AssemblyRemapper/Remapper/Search/TypeDefExtensions.cs index 36de9c2..c9995d8 100644 --- a/AssemblyRemapper/Remapper/Search/TypeDefExtensions.cs +++ b/AssemblyRemapper/Remapper/Search/TypeDefExtensions.cs @@ -200,35 +200,12 @@ internal static class TypeDefExtensions /// Match if any search criteria met public static EMatchResult MatchMethods(this TypeDefinition type, SearchParams parms, ScoringModel score) { - var matches = new List { }; - - if (parms.MatchMethods.Count > 0 && parms.IgnoreMethods.Contains("*") is true) + var matches = new List { - Logger.Log($"Cannot both ignore all methods and search for a method on {score.ProposedNewName}.", ConsoleColor.Red); - return EMatchResult.NoMatch; - } - else if (parms.MatchMethods.Count > 0) - { - matches.Add(Methods.GetTypeWithMethods(type, parms, score)); - } - - if (parms.IgnoreMethods.Count > 0) - { - Logger.Log("TypeWithoutMethods"); - matches.Add(Methods.GetTypeWithoutMethods(type, parms, score)); - } - - if (parms.IgnoreMethods.Contains("*")) - { - Logger.Log("TypeWithNoMethods"); - matches.Add(Methods.GetTypeWithNoMethods(type, parms, score)); - } - - if (parms.MethodCount > 0) - { - Logger.Log("TypeByNumberOfMethods"); - matches.Add(Methods.GetTypeByNumberOfMethods(type, parms, score)); - } + Methods.GetTypeWithMethods(type, parms, score), + Methods.GetTypeWithoutMethods(type, parms, score), + Methods.GetTypeByNumberOfMethods(type, parms, score) + }; // return match if any condition matched return matches.GetMatch(); @@ -236,42 +213,15 @@ internal static class TypeDefExtensions public static EMatchResult MatchFields(this TypeDefinition type, SearchParams parms, ScoringModel score) { - if (parms.MatchFields.Count is 0 && parms.IgnoreFields.Count is 0) + var matches = new List { - return EMatchResult.Disabled; - } + Fields.GetTypeWithFields(type, parms, score), + Fields.GetTypeWithoutFields(type, parms, score), + Fields.GetTypeByNumberOfFields(type, parms, score) + }; - var skippAll = parms.IgnoreFields.Contains("*"); - - // Type has fields, we dont want any - if (type.HasFields is false && skippAll is true) - { - score.Score++; - return EMatchResult.Match; - } - - int matchCount = 0; - - foreach (var field in type.Fields) - { - if (parms.IgnoreFields.Contains(field.Name)) - { - // Type contains blacklisted field - score.FailureReason = EFailureReason.HasFields; - return EMatchResult.NoMatch; - } - } - - foreach (var field in type.Fields) - { - if (parms.MatchFields.Contains(field.Name)) - { - matchCount++; - score.Score++; - } - } - - return matchCount > 0 ? EMatchResult.Match : EMatchResult.NoMatch; + // return match if any condition matched + return matches.GetMatch(); } public static EMatchResult MatchProperties(this TypeDefinition type, SearchParams parms, ScoringModel score) diff --git a/AssemblyRemapper/Utils/EnumHelpers.cs b/AssemblyRemapper/Utils/EnumHelpers.cs index c7b4d5e..5854f71 100644 --- a/AssemblyRemapper/Utils/EnumHelpers.cs +++ b/AssemblyRemapper/Utils/EnumHelpers.cs @@ -1,4 +1,5 @@ using AssemblyRemapper.Enums; +using MoreLinq.Extensions; namespace AssemblyRemapper.Utils; @@ -11,11 +12,14 @@ internal static class EnumExtensions /// highest EMatchResult public static EMatchResult GetMatch(this List matches) { - if (matches.Contains(EMatchResult.Disabled)) { return EMatchResult.Disabled; } + // Prioritize returning matches + if (matches.Contains(EMatchResult.Match)) { return EMatchResult.Match; } + // Then NoMatches if (matches.Contains(EMatchResult.NoMatch)) { return EMatchResult.NoMatch; } - if (matches.Contains(EMatchResult.Match)) { return EMatchResult.Match; } + // Then Disabled + if (matches.Contains(EMatchResult.Disabled)) { return EMatchResult.Disabled; } // default to disabled return EMatchResult.Disabled; diff --git a/AssemblyRemapper/Utils/Logger.cs b/AssemblyRemapper/Utils/Logger.cs index d5cf279..db305d0 100644 --- a/AssemblyRemapper/Utils/Logger.cs +++ b/AssemblyRemapper/Utils/Logger.cs @@ -13,7 +13,16 @@ internal static class Logger private static string _logPath = Path.Combine(AppContext.BaseDirectory, "Data", "Log.log"); - public static void Log(string message, ConsoleColor color = ConsoleColor.Gray, bool silent = false) + public static void ClearLog() + { + if (File.Exists(_logPath)) + { + File.Delete(_logPath); + File.Create(_logPath).Close(); + } + } + + public static void Log(object message, ConsoleColor color = ConsoleColor.Gray, bool silent = false) { if (silent) { @@ -27,7 +36,7 @@ internal static class Logger WriteToDisk(message); } - public static void LogDebug(string message, ConsoleColor color = ConsoleColor.Gray, bool silent = false) + public static void LogDebug(object message, ConsoleColor color = ConsoleColor.Gray, bool silent = false) { if (silent) { @@ -44,7 +53,7 @@ internal static class Logger } } - private static void WriteToDisk(string message) + private static void WriteToDisk(object message) { try { diff --git a/Templates/MappingTemplate.jsonc b/Templates/MappingTemplate.jsonc index 575bd71..1f78a4a 100644 --- a/Templates/MappingTemplate.jsonc +++ b/Templates/MappingTemplate.jsonc @@ -35,9 +35,7 @@ "FieldCount": 0, // Match types that have a method count this length "PropertyCount": 0, // Match types that have a method count this length - // Note: - // - You can can filter the ignore list with a wild card '*' to match all types that have none of that search parameter - // - These are all lists of strings + // List parameters "MatchMethods": [ // This is a list of methods we want to match ],