From 5a97aca1cfb26b3f7ff2d807f0c1f596ff0aab0f Mon Sep 17 00:00:00 2001 From: Cj <161484149+CJ-SPT@users.noreply.github.com> Date: Sat, 15 Jun 2024 21:05:06 -0400 Subject: [PATCH] Automapper finishing touches --- RecodeItGUI/GUI/Main.Designer.cs | 8 +- RecodeItGUI/Utils/GUIHelpers.cs | 2 +- RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs | 110 +++++++++++++++---- RecodeItLib/Remapper/ReCodeItRemapper.cs | 2 +- RecodeItLib/Remapper/Renamer.cs | 4 +- Templates/Settings.jsonc | 3 +- 6 files changed, 99 insertions(+), 30 deletions(-) diff --git a/RecodeItGUI/GUI/Main.Designer.cs b/RecodeItGUI/GUI/Main.Designer.cs index 0c6ff8c..8bba1db 100644 --- a/RecodeItGUI/GUI/Main.Designer.cs +++ b/RecodeItGUI/GUI/Main.Designer.cs @@ -685,7 +685,7 @@ partial class ReCodeItForm NewTypeName.BackColor = SystemColors.ScrollBar; NewTypeName.Location = new Point(10, 30); NewTypeName.Name = "NewTypeName"; - NewTypeName.PlaceholderText = "New Type Name"; + NewTypeName.PlaceholderText = "New TypeRef Name"; NewTypeName.Size = new Size(208, 31); NewTypeName.TabIndex = 0; // @@ -726,7 +726,7 @@ partial class ReCodeItForm NestedTypeCountEnabled.Name = "NestedTypeCountEnabled"; NestedTypeCountEnabled.Size = new Size(189, 29); NestedTypeCountEnabled.TabIndex = 12; - NestedTypeCountEnabled.Text = "Nested Type Count"; + NestedTypeCountEnabled.Text = "Nested TypeRef Count"; NestedTypeCountEnabled.UseVisualStyleBackColor = true; // // PropertyCountUpDown @@ -781,7 +781,7 @@ partial class ReCodeItForm NestedTypeParentName.BackColor = SystemColors.ScrollBar; NestedTypeParentName.Location = new Point(224, 106); NestedTypeParentName.Name = "NestedTypeParentName"; - NestedTypeParentName.PlaceholderText = "Nested Type Parent Name"; + NestedTypeParentName.PlaceholderText = "Nested TypeRef Parent Name"; NestedTypeParentName.Size = new Size(208, 31); NestedTypeParentName.TabIndex = 0; // @@ -817,7 +817,7 @@ partial class ReCodeItForm OriginalTypeName.BackColor = SystemColors.ScrollBar; OriginalTypeName.Location = new Point(224, 30); OriginalTypeName.Name = "OriginalTypeName"; - OriginalTypeName.PlaceholderText = "Original Type Name"; + OriginalTypeName.PlaceholderText = "Original TypeRef Name"; OriginalTypeName.Size = new Size(208, 31); OriginalTypeName.TabIndex = 1; // diff --git a/RecodeItGUI/Utils/GUIHelpers.cs b/RecodeItGUI/Utils/GUIHelpers.cs index aec7dfc..9bb66bc 100644 --- a/RecodeItGUI/Utils/GUIHelpers.cs +++ b/RecodeItGUI/Utils/GUIHelpers.cs @@ -122,7 +122,7 @@ internal static class GUIHelpers if (model.SearchParams.NestedTypeCount > 0) { - remapTreeItem.Nodes.Add(new TreeNode($"Nested Type Count: {model.SearchParams.NestedTypeCount}")); + remapTreeItem.Nodes.Add(new TreeNode($"Nested TypeRef Count: {model.SearchParams.NestedTypeCount}")); } remapTreeItem.Nodes.Add(originalTypeName); diff --git a/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs b/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs index 3975150..b3d5344 100644 --- a/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs +++ b/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs @@ -11,7 +11,7 @@ public class ReCodeItAutoMapper private List CompilerGeneratedClasses = []; - private AutoMapperSettings Settings => DataProvider.Settings.AutoMapper; + private static AutoMapperSettings Settings => DataProvider.Settings.AutoMapper; public void InitializeAutoMapping() { @@ -35,6 +35,8 @@ public class ReCodeItAutoMapper } FilterTypeNames(); + SanitizeProposedNames(); + WriteChanges(); } private void FindCompilerGeneratedObjects(Mono.Collections.Generic.Collection types) @@ -93,9 +95,13 @@ public class ReCodeItAutoMapper // Include fields from the current type foreach (var field in fields) { - //Logger.Log($"Collecting Field: Type: {field.FieldType.Name.TrimAfterSpecialChar()} Field Name: {field.Name}"); + //Logger.Log($"Collecting Field: TypeRef: {field.FieldType.Name.TrimAfterSpecialChar()} Field Name: {field.Name}"); - fieldsWithTypes.Add(new MappingPair(field.FieldType, field.Name)); + fieldsWithTypes.Add(new MappingPair( + field.FieldType, + field.Name, + field.FieldType.Name.Contains("Interface"), + field.FieldType.Name.Contains("Struct"))); } return fieldsWithTypes; @@ -135,17 +141,23 @@ public class ReCodeItAutoMapper // Include fields from the current type foreach (var property in properties) { - //Logger.Log($"Collecting Property: Type: {property.PropertyType.Name.TrimAfterSpecialChar()} Field Name: {property.Name}"); + //Logger.Log($"Collecting Property: TypeRef: {property.PropertyType.Name.TrimAfterSpecialChar()} Field Name: {property.Name}"); - propertiesWithTypes.Add(new MappingPair(property.PropertyType, property.Name)); + ; + + propertiesWithTypes.Add(new MappingPair( + property.PropertyType, + property.Name, + property.PropertyType.Name.Contains("Interface"), + property.PropertyType.Name.Contains("Struct"))); } return propertiesWithTypes; } /// - /// Filters down match pairs to match deobfuscating names 'ClassXXXX' to field or property names - /// that are not of the same value, also applies a length filter. + /// This giant linq statement handles all of the filtering once the initial gathering of fields + /// and properties is complete /// private void FilterTypeNames() { @@ -154,10 +166,11 @@ public class ReCodeItAutoMapper // Filter based on length, short lengths dont make good class names .Where(pair => pair.Name.Length >= Settings.MinLengthToMatch) - // Filter out anything that doesnt start with our specified tokens (Where pair.Type.Name - // is the property Type name `Class1202` and token is start identifer we are looking for `GClass` + // Filter out anything that doesnt start with our specified tokens (Where + // pair.TypeRef.Name is the property TypeRef name `Class1202` and token is start + // identifer we are looking for `GClass` .Where(pair => Settings.TokensToMatch - .Any(token => pair.Type.Name.StartsWith(token))) + .Any(token => pair.TypeRef.Name.StartsWith(token))) // Filter out anything that has the same name as the type, we cant remap those .Where(pair => !Settings.TokensToMatch @@ -168,23 +181,78 @@ public class ReCodeItAutoMapper .Where(pair => !Settings.PropertyFieldBlackList .Any(token => pair.Name.ToLower().StartsWith(token.ToLower()))) + // Filter out backing fields + /// This is slow, but oh well + .Where(pair => !pair.Name.ToCharArray().Contains('<')) + // We only want types once, so make it unique - .GroupBy(pair => pair.Type.FullName) + .GroupBy(pair => pair.TypeRef.FullName) .Select(group => group.First()) - .ToList(); + .GroupBy(pair => pair.Name) + .Select(group => group.First()) + .ToList(); - foreach (var pair in mappingPairs) - { - Logger.Log($"Type: {pair.Type.FullName} identifier: {pair.Name}"); - } - - MappingPairs = mappingPairs.ToList(); - Logger.Log($"Match Count {mappingPairs.Count()}"); + MappingPairs = [.. mappingPairs]; } - private sealed class MappingPair(TypeReference type, string name) + private void SanitizeProposedNames() { - public TypeReference Type { get; set; } = type; + foreach (var pair in MappingPairs) + { + char first = pair.Name.ToCharArray().ElementAt(0); + + if (first.Equals('_')) + { + pair.Name = string.Concat("", pair.Name.AsSpan(1)); + } + + // Re-run incase prefix removed + first = pair.Name.ToCharArray().ElementAt(0); + + if (char.IsLower(first)) + { + pair.Name = string.Concat(char.ToUpper(first).ToString(), pair.Name.AsSpan(1)); + } + + if (pair.IsInterface) + { + Logger.Log($"INTERFACE"); + pair.Name = string.Concat("I", pair.Name.AsSpan(0)); + } + + // If its not an interface, its a struct or class + switch (pair.IsStruct) + { + case true: + pair.Name = string.Concat(pair.Name, "Struct"); + break; + + case false: + pair.Name = string.Concat(pair.Name, "Class"); + break; + } + + Logger.Log($"------------------------------------------------------------------------"); + Logger.Log($"Original Name: {pair.OriginalName} : Sanitized Name: {pair.Name}"); + Logger.Log($"Matched From Name: {pair.OriginalPropOrFieldName}"); + Logger.Log($"IsInterface: {pair.IsInterface}"); + Logger.Log($"IsStruct: {pair.IsStruct}"); + Logger.Log($"------------------------------------------------------------------------"); + } + } + + private void WriteChanges() + { + } + + private sealed class MappingPair(TypeReference type, string name, bool isInterface = false, bool isStruct = false) + { + public TypeReference TypeRef { get; set; } = type; + public string OriginalName { get; set; } = type.FullName; + public bool IsInterface { get; set; } = isInterface; + public bool IsStruct { get; set; } = isStruct; public string Name { get; set; } = name; + + public string OriginalPropOrFieldName { get; } = name; } } \ No newline at end of file diff --git a/RecodeItLib/Remapper/ReCodeItRemapper.cs b/RecodeItLib/Remapper/ReCodeItRemapper.cs index aa79489..2d2e698 100644 --- a/RecodeItLib/Remapper/ReCodeItRemapper.cs +++ b/RecodeItLib/Remapper/ReCodeItRemapper.cs @@ -79,7 +79,7 @@ public class ReCodeItRemapper /// /// Find a match result /// - /// Type to score + /// TypeRef to score /// Remap to check against /// /// EMatchResult diff --git a/RecodeItLib/Remapper/Renamer.cs b/RecodeItLib/Remapper/Renamer.cs index 8262a4a..2ed334e 100644 --- a/RecodeItLib/Remapper/Renamer.cs +++ b/RecodeItLib/Remapper/Renamer.cs @@ -42,7 +42,7 @@ internal static class Renamer if (field.Name == newFieldName) { continue; } - Logger.Log($"Renaming field: `{field.Name}` on Type `{type.Name}` to {newFieldName}", ConsoleColor.Green); + Logger.Log($"Renaming field: `{field.Name}` on TypeRef `{type.Name}` to {newFieldName}", ConsoleColor.Green); field.Name = newFieldName; @@ -74,7 +74,7 @@ internal static class Renamer { var newName = propertyCount > 0 ? $"{score.ReMap.NewTypeName}_{propertyCount}" : score.ReMap.NewTypeName; - Logger.Log($"Renaming Property: `{property.Name}` on Type `{type}` to {newName}", ConsoleColor.Green); + Logger.Log($"Renaming Property: `{property.Name}` on TypeRef `{type}` to {newName}", ConsoleColor.Green); property.Name = newName; } } diff --git a/Templates/Settings.jsonc b/Templates/Settings.jsonc index 21dd678..7e613e9 100644 --- a/Templates/Settings.jsonc +++ b/Templates/Settings.jsonc @@ -49,7 +49,8 @@ "Conditions", "Counter", "Instance", - "Command" + "Command", + "_template" ] } }