diff --git a/AssemblyRemapper/Models/RemapModel.cs b/AssemblyRemapper/Models/RemapModel.cs index cd7a9ef..286fb0d 100644 --- a/AssemblyRemapper/Models/RemapModel.cs +++ b/AssemblyRemapper/Models/RemapModel.cs @@ -31,12 +31,10 @@ internal class SearchParams public bool? HasGenericParameters { get; set; } = null; public List MatchMethods { get; set; } public List IgnoreMethods { get; set; } - public List MatchFields { get; set; } public List IgnoreFields { get; set; } public List MatchProperties { get; set; } public List IgnorePropterties { get; set; } - public List MatchNestedTypes { get; set; } public List IgnoreNestedTypes { get; set; } diff --git a/AssemblyRemapper/Reflection/Remapper.cs b/AssemblyRemapper/Reflection/Remapper.cs index d3957d7..fccb52c 100644 --- a/AssemblyRemapper/Reflection/Remapper.cs +++ b/AssemblyRemapper/Reflection/Remapper.cs @@ -153,9 +153,8 @@ internal class Remapper if (type.Name != remap.OriginalTypeName) { return; } var oldName = type.Name; - type.Name = remap.NewTypeName; - remap.OriginalTypeName = type.Name; + type.Name = remap.NewTypeName; Logger.Log("-----------------------------------------------", ConsoleColor.Green); Logger.Log($"Renamed {oldName} to {type.Name} directly", ConsoleColor.Green); diff --git a/AssemblyRemapper/Reflection/SearchExtentions.cs b/AssemblyRemapper/Reflection/SearchExtentions.cs index 88bea47..1d50f3b 100644 --- a/AssemblyRemapper/Reflection/SearchExtentions.cs +++ b/AssemblyRemapper/Reflection/SearchExtentions.cs @@ -174,44 +174,38 @@ internal static class SearchExtentions public static EMatchResult MatchMethods(this TypeDefinition type, SearchParams parms, ScoringModel score) { // We're not searching for methods and this type contains methods - if (parms.MatchMethods.Count == 0 && parms.IgnoreMethods.Count == 0) + if (parms.MatchMethods.Count is 0 && parms.IgnoreMethods.Count is 0) { - return type.HasMethods - ? EMatchResult.NoMatch - : EMatchResult.Match; + return EMatchResult.Disabled; } var skippAll = parms.IgnoreMethods.Contains("*"); + var methodCount = type.Methods.Count - type.GetConstructors().Count(); - // The type has methods and we dont want any - if (type.HasMethods is true && skippAll is true) + // Subtract method count from constructor count to check for real methods + if (methodCount > 0 && skippAll is true) { - foreach (var method in type.Methods) - { - if (method.Name == ".ctor") - { - continue; - } + // Type has methods, we dont want any + return EMatchResult.NoMatch; + } + // Handle Ignore methods + foreach (var method in type.Methods) + { + if (parms.IgnoreMethods.Contains(method.Name)) + { + // Contains blacklisted method, no match + score.FailureReason = EFailureReason.HasMethods; score.Score--; return EMatchResult.NoMatch; } - - score.Score++; - return EMatchResult.Match; } var matchCount = 0; + // Handle match methods foreach (var method in type.Methods) { - // Type contains a method we dont want - if (parms.IgnoreMethods.Contains(method.Name)) - { - score.FailureReason = EFailureReason.HasMethods; - return EMatchResult.NoMatch; - } - foreach (var name in parms.MatchMethods) { // Method name match @@ -228,15 +222,17 @@ internal static class SearchExtentions public static EMatchResult MatchFields(this TypeDefinition type, SearchParams parms, ScoringModel score) { - if (parms.MatchFields.Count == 0 && parms.IgnoreFields.Count == 0) + if (parms.MatchFields.Count is 0 && parms.IgnoreFields.Count is 0) { return EMatchResult.Disabled; } - // `*` is the wildcard to ignore all fields that exist on types - if (!type.HasFields && parms.IgnoreFields.Contains("*")) + var skippAll = parms.IgnoreFields.Contains("*"); + + // Type has fields, we dont want any + if (type.HasFields is true && skippAll is true) { - return EMatchResult.Match; + return EMatchResult.NoMatch; } int matchCount = 0; @@ -249,7 +245,10 @@ internal static class SearchExtentions score.FailureReason = EFailureReason.HasFields; return EMatchResult.NoMatch; } + } + foreach (var field in type.Fields) + { if (parms.MatchFields.Contains(field.Name)) { matchCount++; @@ -262,19 +261,18 @@ internal static class SearchExtentions public static EMatchResult MatchProperties(this TypeDefinition type, SearchParams parms, ScoringModel score) { - if (parms.MatchProperties.Count == 0 && parms.IgnorePropterties.Count == 0) + if (parms.MatchProperties.Count is 0 && parms.IgnorePropterties.Count is 0) { return EMatchResult.Disabled; } - // `*` is the wildcard to ignore all fields that exist on types - if (!type.HasProperties && parms.IgnorePropterties.Contains("*")) - { - score.Score++; - return EMatchResult.Match; - } + var skippAll = parms.IgnorePropterties.Contains("*"); - int matchCount = 0; + // Type has fields, we dont want any + if (type.HasProperties is true && skippAll is true) + { + return EMatchResult.NoMatch; + } foreach (var property in type.Properties) { @@ -284,7 +282,12 @@ internal static class SearchExtentions score.FailureReason = EFailureReason.HasProperties; return EMatchResult.NoMatch; } + } + int matchCount = 0; + + foreach (var property in type.Properties) + { if (parms.MatchProperties.Contains(property.Name)) { matchCount++; @@ -302,15 +305,15 @@ internal static class SearchExtentions return EMatchResult.Disabled; } + var skippAll = parms.IgnorePropterties.Contains("*"); + // `*` is the wildcard to ignore all fields that exist on types - if (type.HasNestedTypes && parms.IgnoreNestedTypes.Contains("*")) + if (type.HasNestedTypes is true && skippAll is true) { score.FailureReason = EFailureReason.HasNestedTypes; return EMatchResult.NoMatch; } - int matchCount = 0; - foreach (var nestedType in type.NestedTypes) { if (parms.IgnoreNestedTypes.Contains(nestedType.Name)) @@ -319,7 +322,12 @@ internal static class SearchExtentions score.FailureReason = EFailureReason.HasNestedTypes; return EMatchResult.NoMatch; } + } + int matchCount = 0; + + foreach (var nestedType in type.NestedTypes) + { if (parms.MatchNestedTypes.Contains(nestedType.Name)) { matchCount++; diff --git a/AssemblyRemapper/Utils/DataProvider.cs b/AssemblyRemapper/Utils/DataProvider.cs index d58614d..6c9ee09 100644 --- a/AssemblyRemapper/Utils/DataProvider.cs +++ b/AssemblyRemapper/Utils/DataProvider.cs @@ -53,6 +53,19 @@ internal static class DataProvider ScoringModels = []; Remaps = JsonConvert.DeserializeObject>(jsonText); + + var properties = typeof(SearchParams).GetProperties(); + + foreach (var remap in Remaps) + { + foreach (var property in properties) + { + if (property.PropertyType == typeof(List) && property.GetValue(remap.SearchParams) is null) + { + property.SetValue(remap.SearchParams, new List()); + } + } + } } public static void UpdateMapping() @@ -68,6 +81,23 @@ internal static class DataProvider Formatting = Formatting.Indented }; + var properties = typeof(SearchParams).GetProperties(); + + foreach (var remap in Remaps) + { + foreach (var property in properties) + { + if (property.PropertyType == typeof(List)) + { + var val = property.GetValue(remap.SearchParams); + + if (val is List list && list.Count > 0) { continue; } + + property.SetValue(remap.SearchParams, null); + } + } + } + var jsonText = JsonConvert.SerializeObject(Remaps, settings); File.WriteAllText(AppSettings.MappingPath, jsonText);