From 9ab953b0055b96c47925c77ee9d2d9775acdf553 Mon Sep 17 00:00:00 2001 From: Cj <161484149+CJ-SPT@users.noreply.github.com> Date: Wed, 12 Jun 2024 20:40:10 -0400 Subject: [PATCH] Update Template --- AssemblyRemapper/Models/RemapModel.cs | 16 +++--- AssemblyRemapper/Reflection/Remapper.cs | 4 ++ .../Reflection/SearchExtentions.cs | 32 ++++++------ AssemblyRemapper/Utils/DataProvider.cs | 18 +++++++ Templates/MappingTemplate.jsonc | 52 +++++++++---------- 5 files changed, 70 insertions(+), 52 deletions(-) diff --git a/AssemblyRemapper/Models/RemapModel.cs b/AssemblyRemapper/Models/RemapModel.cs index c41bc57..cd7a9ef 100644 --- a/AssemblyRemapper/Models/RemapModel.cs +++ b/AssemblyRemapper/Models/RemapModel.cs @@ -29,16 +29,16 @@ internal class SearchParams public bool? HasAttribute { get; set; } = null; public bool? IsDerived { get; set; } = null; public bool? HasGenericParameters { get; set; } = null; - public List MethodNamesToMatch { get; set; } = []; - public List MethodNamesToIgnore { get; set; } = []; + public List MatchMethods { get; set; } + public List IgnoreMethods { get; set; } - public List FieldNamesToMatch { get; set; } = []; - public List FieldNamesToIgnore { get; set; } = []; - public List PropertyNamesToMatch { get; set; } = []; - public List PropertyNamesToIgnore { get; set; } = []; + public List MatchFields { get; set; } + public List IgnoreFields { get; set; } + public List MatchProperties { get; set; } + public List IgnorePropterties { get; set; } - public List NestedTypesToMatch { get; set; } = []; - public List NestedTypesToIgnore { get; set; } = []; + public List MatchNestedTypes { get; set; } + public List IgnoreNestedTypes { get; set; } public SearchParams() { diff --git a/AssemblyRemapper/Reflection/Remapper.cs b/AssemblyRemapper/Reflection/Remapper.cs index 5e34113..d3957d7 100644 --- a/AssemblyRemapper/Reflection/Remapper.cs +++ b/AssemblyRemapper/Reflection/Remapper.cs @@ -204,6 +204,8 @@ internal class Remapper } } + highestScore.RemapModel.OriginalTypeName = highestScore.Definition.Name; + // Rename type and all associated type members Renamer.RenameAll(highestScore); @@ -220,9 +222,11 @@ internal class Remapper var remappedPath = Path.Combine(strippedPath, filename); DataProvider.AssemblyDefinition.Write(remappedPath); + DataProvider.UpdateMapping(); Logger.Log("-----------------------------------------------", ConsoleColor.Green); Logger.Log($"Complete: Assembly written to `{remappedPath}`", ConsoleColor.Green); + Logger.Log("Original type names updated on mapping file.", ConsoleColor.Green); Logger.Log("-----------------------------------------------", ConsoleColor.Green); } } \ No newline at end of file diff --git a/AssemblyRemapper/Reflection/SearchExtentions.cs b/AssemblyRemapper/Reflection/SearchExtentions.cs index 457b7e1..88bea47 100644 --- a/AssemblyRemapper/Reflection/SearchExtentions.cs +++ b/AssemblyRemapper/Reflection/SearchExtentions.cs @@ -174,14 +174,14 @@ 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.MethodNamesToMatch.Count == 0 && parms.MethodNamesToIgnore.Count == 0) + if (parms.MatchMethods.Count == 0 && parms.IgnoreMethods.Count == 0) { return type.HasMethods ? EMatchResult.NoMatch : EMatchResult.Match; } - var skippAll = parms.MethodNamesToIgnore.Contains("*"); + var skippAll = parms.IgnoreMethods.Contains("*"); // The type has methods and we dont want any if (type.HasMethods is true && skippAll is true) @@ -206,13 +206,13 @@ internal static class SearchExtentions foreach (var method in type.Methods) { // Type contains a method we dont want - if (parms.MethodNamesToIgnore.Contains(method.Name)) + if (parms.IgnoreMethods.Contains(method.Name)) { score.FailureReason = EFailureReason.HasMethods; return EMatchResult.NoMatch; } - foreach (var name in parms.MethodNamesToMatch) + foreach (var name in parms.MatchMethods) { // Method name match if (method.Name == name) @@ -228,13 +228,13 @@ internal static class SearchExtentions public static EMatchResult MatchFields(this TypeDefinition type, SearchParams parms, ScoringModel score) { - if (parms.FieldNamesToMatch.Count == 0 && parms.FieldNamesToIgnore.Count == 0) + if (parms.MatchFields.Count == 0 && parms.IgnoreFields.Count == 0) { return EMatchResult.Disabled; } // `*` is the wildcard to ignore all fields that exist on types - if (!type.HasFields && parms.FieldNamesToIgnore.Contains("*")) + if (!type.HasFields && parms.IgnoreFields.Contains("*")) { return EMatchResult.Match; } @@ -243,14 +243,14 @@ internal static class SearchExtentions foreach (var field in type.Fields) { - if (parms.FieldNamesToIgnore.Contains(field.Name)) + if (parms.IgnoreFields.Contains(field.Name)) { // Type contains blacklisted field score.FailureReason = EFailureReason.HasFields; return EMatchResult.NoMatch; } - if (parms.FieldNamesToMatch.Contains(field.Name)) + if (parms.MatchFields.Contains(field.Name)) { matchCount++; score.Score++; @@ -262,13 +262,13 @@ internal static class SearchExtentions public static EMatchResult MatchProperties(this TypeDefinition type, SearchParams parms, ScoringModel score) { - if (parms.PropertyNamesToMatch.Count == 0 && parms.PropertyNamesToIgnore.Count == 0) + if (parms.MatchProperties.Count == 0 && parms.IgnorePropterties.Count == 0) { return EMatchResult.Disabled; } // `*` is the wildcard to ignore all fields that exist on types - if (!type.HasProperties && parms.PropertyNamesToIgnore.Contains("*")) + if (!type.HasProperties && parms.IgnorePropterties.Contains("*")) { score.Score++; return EMatchResult.Match; @@ -278,14 +278,14 @@ internal static class SearchExtentions foreach (var property in type.Properties) { - if (parms.PropertyNamesToIgnore.Contains(property.Name)) + if (parms.IgnorePropterties.Contains(property.Name)) { // Type contains blacklisted property score.FailureReason = EFailureReason.HasProperties; return EMatchResult.NoMatch; } - if (parms.PropertyNamesToMatch.Contains(property.Name)) + if (parms.MatchProperties.Contains(property.Name)) { matchCount++; score.Score++; @@ -297,13 +297,13 @@ internal static class SearchExtentions public static EMatchResult MatchNestedTypes(this TypeDefinition type, SearchParams parms, ScoringModel score) { - if (parms.NestedTypesToMatch.Count == 0 && parms.NestedTypesToIgnore.Count == 0) + if (parms.MatchNestedTypes.Count == 0 && parms.IgnoreNestedTypes.Count == 0) { return EMatchResult.Disabled; } // `*` is the wildcard to ignore all fields that exist on types - if (type.HasNestedTypes && parms.NestedTypesToIgnore.Contains("*")) + if (type.HasNestedTypes && parms.IgnoreNestedTypes.Contains("*")) { score.FailureReason = EFailureReason.HasNestedTypes; return EMatchResult.NoMatch; @@ -313,14 +313,14 @@ internal static class SearchExtentions foreach (var nestedType in type.NestedTypes) { - if (parms.NestedTypesToIgnore.Contains(nestedType.Name)) + if (parms.IgnoreNestedTypes.Contains(nestedType.Name)) { // Type contains blacklisted nested type score.FailureReason = EFailureReason.HasNestedTypes; return EMatchResult.NoMatch; } - if (parms.NestedTypesToMatch.Contains(nestedType.Name)) + if (parms.MatchNestedTypes.Contains(nestedType.Name)) { matchCount++; score.Score++; diff --git a/AssemblyRemapper/Utils/DataProvider.cs b/AssemblyRemapper/Utils/DataProvider.cs index b56da60..d58614d 100644 --- a/AssemblyRemapper/Utils/DataProvider.cs +++ b/AssemblyRemapper/Utils/DataProvider.cs @@ -55,6 +55,24 @@ internal static class DataProvider Remaps = JsonConvert.DeserializeObject>(jsonText); } + public static void UpdateMapping() + { + if (!File.Exists(AppSettings.MappingPath)) + { + throw new InvalidOperationException($"path `{AppSettings.MappingPath}` does not exist..."); + } + + JsonSerializerSettings settings = new JsonSerializerSettings + { + NullValueHandling = NullValueHandling.Ignore, + Formatting = Formatting.Indented + }; + + var jsonText = JsonConvert.SerializeObject(Remaps, settings); + + File.WriteAllText(AppSettings.MappingPath, jsonText); + } + public static void LoadAssemblyDefinition() { DefaultAssemblyResolver resolver = new(); diff --git a/Templates/MappingTemplate.jsonc b/Templates/MappingTemplate.jsonc index 39bed31..47c23d3 100644 --- a/Templates/MappingTemplate.jsonc +++ b/Templates/MappingTemplate.jsonc @@ -5,41 +5,37 @@ "NewTypeName": "TEMPLATE", // This is the name we want to change it to "OriginalTypeName": "", // This is the name of the object in the assembly, Can be used to store what type you're changing as its not read unless "UseDirectRename" is true, it is also written to after remapping "UseForceRename": false, // If this is true, directly remap using the name in the assembly from the above property - "SearchParams": { - "IsPublic": true, // Is the Type public? - "IsAbstract": false, // Is the Type Abstract? - "IsInterface": false, // Is the Type an Interface? - "IsEnum": false, // Is the Type an Enum? - "IsNested": false, // Is the Type Nested? - "ParentName": "", // The Name of the parent type if it is nested, can be left empty - "IsSealed": false, // Is the Type Sealed? - "HasAttribute": false, // Does the Type have an attribute? - "IsDerived": true, // Does the Type inherit from another Type? - "HasGenericParameters": false, // Does the type have generic parameters? - "MethodNamesToMatch": [ // This is a list of methods we want to match - "Foo" + "SearchParams": { // null means disabled + "IsPublic": null, // Is the Type public? (bool) + "IsAbstract": null, // Is the Type Abstract? (bool) + "IsInterface": null, // Is the Type an Interface? (bool) + "IsEnum": null, // Is the Type an Enum? (bool) + "IsNested": null, // Is the Type Nested? (bool) + "ParentName": "", // The Name of the parent type if it is nested, can be left empty (string) + "IsSealed": null, // Is the Type Sealed? (bool) + "HasAttribute": null, // Does the Type have an attribute? (bool) + "IsDerived": null, // Does the Type inherit from another Type? (bool) + "HasGenericParameters": null, // Does the type have generic parameters? (bool) + + // 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 + + "MatchMethods": [ // This is a list of methods we want to match ], - "MethodNamesToIgnore": [ // This is a list of methods we want to ignore - "Foo", - "*" // Ignore all Types that have methods, this wildcard can be used on any ignore list + "IgnoreMethods": [ // This is a list of methods we want to ignore ], - "FieldNamesToMatch": [ // This is a list of fields we want to match - "_fooBar" + "MatchFields": [ // This is a list of fields we want to match ], - "FieldNamesToIgnore": [ // This is a list of fields we want to ignore - "Foo" + "IgnoreFields": [ // This is a list of fields we want to ignore ], - "PropertyNamesToMatch": [ // This is a list of properties we want to match - "FooBar" + "MatchProperties": [ // This is a list of properties we want to match ], - "PropertyNamesToIgnore": [ // This is a list of properties we want to ignore - "Foo" + "IgnorePropterties": [ // This is a list of properties we want to ignore ], - "NestedTypesToMatch": [ // This is a list of nested types we want to match - "FooBarFoo" + "MatchNestedTypes": [ // This is a list of nested types we want to match ], - "NestedTypesToIgnore": [ // This is a list of nested types we want to match ignore - "FooBarFoo" + "IgnoreNestedTypes": [ // This is a list of nested types we want to match ignore ] } }