From 443530d79aeaecee98e3905ef929bca72fcdafe0 Mon Sep 17 00:00:00 2001
From: Cj <161484149+CJ-SPT@users.noreply.github.com>
Date: Sun, 16 Jun 2024 00:19:02 -0400
Subject: [PATCH] Successful rename
---
RecodeItGUI/GUI/Main.Designer.cs | 8 +-
RecodeItGUI/Utils/GUIHelpers.cs | 2 +-
RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs | 146 +++++++++++++++++--
RecodeItLib/Remapper/ReCodeItRemapper.cs | 4 +-
RecodeItLib/Remapper/Renamer.cs | 14 +-
5 files changed, 153 insertions(+), 21 deletions(-)
diff --git a/RecodeItGUI/GUI/Main.Designer.cs b/RecodeItGUI/GUI/Main.Designer.cs
index 8bba1db..c8e23d9 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 TypeRef Name";
+ NewTypeName.PlaceholderText = "New OriginalTypeRef 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 TypeRef Count";
+ NestedTypeCountEnabled.Text = "Nested OriginalTypeRef 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 TypeRef Parent Name";
+ NestedTypeParentName.PlaceholderText = "Nested OriginalTypeRef 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 TypeRef Name";
+ OriginalTypeName.PlaceholderText = "Original OriginalTypeRef Name";
OriginalTypeName.Size = new Size(208, 31);
OriginalTypeName.TabIndex = 1;
//
diff --git a/RecodeItGUI/Utils/GUIHelpers.cs b/RecodeItGUI/Utils/GUIHelpers.cs
index 9bb66bc..cf5ab1e 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 TypeRef Count: {model.SearchParams.NestedTypeCount}"));
+ remapTreeItem.Nodes.Add(new TreeNode($"Nested OriginalTypeRef Count: {model.SearchParams.NestedTypeCount}"));
}
remapTreeItem.Nodes.Add(originalTypeName);
diff --git a/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs b/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs
index 5d0cf7e..dba793d 100644
--- a/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs
+++ b/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs
@@ -13,6 +13,9 @@ public class ReCodeItAutoMapper
private static AutoMapperSettings Settings => DataProvider.Settings.AutoMapper;
+ private static bool Error { get; set; } = false;
+ private int FailureCount { get; set; } = 0;
+
///
/// Start the automapping process
///
@@ -25,6 +28,9 @@ public class ReCodeItAutoMapper
MappingPairs = [];
CompilerGeneratedClasses = [];
+ Error = false;
+ FailureCount = 0;
+
FindCompilerGeneratedObjects(DataProvider.ModuleDefinition.Types);
Logger.Log($"Found {CompilerGeneratedClasses.Count} Compiler generated objects");
@@ -39,6 +45,10 @@ public class ReCodeItAutoMapper
FilterTypeNames();
SanitizeProposedNames();
+ StartRenameProcess();
+
+ if (Error) { return; }
+
WriteChanges();
}
@@ -96,13 +106,16 @@ public class ReCodeItAutoMapper
// Skip value types
.Where(f => !f.FieldType.IsValueType)
+ // TODO: Renaming arrays is strange, come back to this later
+ .Where(p => !p.FieldType.IsArray)
+
// We dont want fields in the system type ignore list
.Where(f => !Settings.TypesToIgnore.Contains(f.FieldType.Name.TrimAfterSpecialChar()));
// Include fields from the current type
foreach (var field in fields)
{
- //Logger.Log($"Collecting Field: TypeRef: {field.FieldType.Name.TrimAfterSpecialChar()} Field Name: {field.Name}");
+ //Logger.Log($"Collecting Field: OriginalTypeRef: {field.FieldType.Name.TrimAfterSpecialChar()} Field Name: {field.Name}");
fieldsWithTypes.Add(new MappingPair(
field.FieldType,
@@ -143,13 +156,16 @@ public class ReCodeItAutoMapper
// Skip value types
.Where(p => !p.PropertyType.IsValueType)
+ // TODO: Renaming arrays is strange, come back to this later
+ .Where(p => !p.PropertyType.IsArray)
+
// We dont want fields in the global ignore list
.Where(p => !Settings.TypesToIgnore.Contains(p.PropertyType.Name.TrimAfterSpecialChar()));
// Include fields from the current type
foreach (var property in properties)
{
- //Logger.Log($"Collecting Property: TypeRef: {property.PropertyType.Name.TrimAfterSpecialChar()} Field Name: {property.Name}");
+ //Logger.Log($"Collecting Property: OriginalTypeRef: {property.PropertyType.Name.TrimAfterSpecialChar()} Field Name: {property.Name}");
;
@@ -175,10 +191,10 @@ public class ReCodeItAutoMapper
.Where(pair => pair.Name.Length >= Settings.MinLengthToMatch)
// 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`
+ // pair.OriginalTypeRef.Name is the property OriginalTypeRef name `Class1202` and token
+ // is start identifer we are looking for `GClass`
.Where(pair => Settings.TokensToMatch
- .Any(token => pair.TypeRef.Name.StartsWith(token)))
+ .Any(token => pair.OriginalTypeRef.Name.StartsWith(token)))
// Filter out anything that has the same name as the type, we cant remap those
.Where(pair => !Settings.TokensToMatch
@@ -194,7 +210,7 @@ public class ReCodeItAutoMapper
.Where(pair => !pair.Name.ToCharArray().Contains('<'))
// We only want types once, so make it unique
- .GroupBy(pair => pair.TypeRef.FullName)
+ .GroupBy(pair => pair.OriginalTypeRef.FullName)
.Select(group => group.First())
.GroupBy(pair => pair.Name)
.Select(group => group.First())
@@ -252,7 +268,7 @@ public class ReCodeItAutoMapper
}
Logger.Log($"------------------------------------------------------------------------");
- Logger.Log($"Original Name: {pair.OriginalName} : Sanitized Name: {pair.Name}");
+ Logger.Log($"Original Name: {pair.OriginalFullName} : Sanitized Name: {pair.Name}");
Logger.Log($"Matched From Name: {pair.OriginalPropOrFieldName}");
Logger.Log($"Matched From Collection: {pair.WasCollection}");
Logger.Log($"IsInterface: {pair.IsInterface}");
@@ -263,6 +279,76 @@ public class ReCodeItAutoMapper
Logger.Log($"Automatically remapped {MappingPairs.Count} objects");
}
+ ///
+ /// Start renaming assembly definitions
+ ///
+ private void StartRenameProcess()
+ {
+ foreach (var type in DataProvider.ModuleDefinition.Types.ToArray())
+ {
+ foreach (var pair in MappingPairs.ToArray())
+ {
+ GatherMatchedTypeRefs(pair, type);
+ }
+ }
+
+ foreach (var pair in MappingPairs)
+ {
+ if (pair.NewTypeRef != null)
+ {
+ pair.NewTypeRef.Name = pair.Name;
+ pair.HasBeenRenamed = true;
+
+ Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Green);
+ Logger.Log($"Renamed: {pair.OriginalShortName} to {pair.Name}", ConsoleColor.Green);
+ Logger.Log($"Original Full Name: {pair.OriginalFullName}", ConsoleColor.Green);
+ Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Green);
+ }
+ }
+
+ // Do a final error check
+ foreach (var pair in MappingPairs)
+ {
+ if (!pair.HasBeenRenamed)
+ {
+ Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Red);
+ Logger.Log($"Renaming: {pair.OriginalTypeRef.Name} to {pair.Name} has failed", ConsoleColor.Red);
+ Logger.Log($"Matched From Collection: {pair.WasCollection}", ConsoleColor.Red);
+ Logger.Log($"Trying to match: {pair.IsInterface}", ConsoleColor.Red);
+ Logger.Log($"IsInterface: {pair.IsInterface}", ConsoleColor.Red);
+ Logger.Log($"IsStruct: {pair.IsStruct}", ConsoleColor.Red);
+ Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Red);
+
+ FailureCount++;
+ Error = true;
+ }
+ }
+
+ Logger.Log($"-------------------------------RESULT-----------------------------------", ConsoleColor.Yellow);
+ Logger.Log($"Found {MappingPairs.Count()} automatic remaps", ConsoleColor.Yellow);
+ Logger.Log($"Failed to rename: {FailureCount} mapping pairs", ConsoleColor.Yellow);
+ Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Yellow);
+ }
+
+ ///
+ /// Recursively handle all renaming on nested types on a given type
+ ///
+ ///
+ ///
+ private void GatherMatchedTypeRefs(MappingPair pair, TypeDefinition type)
+ {
+ // Handle nested types recursively
+ foreach (var nestedType in type.NestedTypes.ToArray())
+ {
+ GatherMatchedTypeRefs(pair, nestedType);
+ }
+
+ if (type == pair.OriginalTypeRef || type.Name == pair.OriginalShortName && !pair.HasBeenRenamed)
+ {
+ pair.NewTypeRef = type;
+ }
+ }
+
private void WriteChanges()
{
}
@@ -281,12 +367,54 @@ public class ReCodeItAutoMapper
bool isStruct = false,
bool wasCollection = false)
{
- public TypeReference TypeRef { get; set; } = type;
- public string OriginalName { get; set; } = type.FullName;
+ ///
+ /// The type reference for the field
+ ///
+ public TypeReference OriginalTypeRef { get; set; } = type;
+
+ ///
+ /// The type reference we want to change
+ ///
+ public TypeReference NewTypeRef { get; set; }
+
+ ///
+ /// The Original full name of the field type
+ ///
+ public string OriginalFullName { get; } = type.FullName;
+
+ ///
+ /// Original short name of the type
+ ///
+ public string OriginalShortName { get; } = type.Name;
+
+ ///
+ /// Is this field an interface?
+ ///
public bool IsInterface { get; set; } = isInterface;
+
+ ///
+ /// Is this type a struct?
+ ///
public bool IsStruct { get; set; } = isStruct;
+
+ ///
+ /// Was this field a collection?
+ ///
public bool WasCollection { get; set; } = wasCollection;
+
+ ///
+ /// Has this type been renamed? Use for checking for failures at the end
+ ///
+ public bool HasBeenRenamed { get; set; } = false;
+
+ ///
+ /// This is the name we want to change the assembly class to
+ ///
public string Name { get; set; } = name;
+
+ ///
+ /// Original name of the property or field type
+ ///
public string OriginalPropOrFieldName { get; } = name;
}
}
\ No newline at end of file
diff --git a/RecodeItLib/Remapper/ReCodeItRemapper.cs b/RecodeItLib/Remapper/ReCodeItRemapper.cs
index 2d2e698..52059d8 100644
--- a/RecodeItLib/Remapper/ReCodeItRemapper.cs
+++ b/RecodeItLib/Remapper/ReCodeItRemapper.cs
@@ -79,7 +79,7 @@ public class ReCodeItRemapper
///
/// Find a match result
///
- /// TypeRef to score
+ /// OriginalTypeRef to score
/// Remap to check against
///
/// EMatchResult
@@ -254,7 +254,7 @@ public class ReCodeItRemapper
Reset();
IsRunning = false;
- OnComplete.Invoke(this, EventArgs.Empty);
+ OnComplete?.Invoke(this, EventArgs.Empty);
}
private void Reset()
diff --git a/RecodeItLib/Remapper/Renamer.cs b/RecodeItLib/Remapper/Renamer.cs
index 2ed334e..8056179 100644
--- a/RecodeItLib/Remapper/Renamer.cs
+++ b/RecodeItLib/Remapper/Renamer.cs
@@ -1,7 +1,7 @@
-using ReCodeIt.Models;
-using ReCodeIt.Utils;
-using Mono.Cecil;
+using Mono.Cecil;
using Mono.Collections.Generic;
+using ReCodeIt.Models;
+using ReCodeIt.Utils;
namespace ReCodeIt.ReMapper;
@@ -18,6 +18,10 @@ internal static class Renamer
score.Definition.Name = score.ProposedNewName;
}
+ public static void RenameAll()
+ {
+ }
+
public static void RenameAllDirect(RemapModel remap, TypeDefinition type)
{
var directRename = new ScoringModel();
@@ -42,7 +46,7 @@ internal static class Renamer
if (field.Name == newFieldName) { continue; }
- Logger.Log($"Renaming field: `{field.Name}` on TypeRef `{type.Name}` to {newFieldName}", ConsoleColor.Green);
+ Logger.Log($"Renaming field: `{field.Name}` on OriginalTypeRef `{type.Name}` to {newFieldName}", ConsoleColor.Green);
field.Name = newFieldName;
@@ -74,7 +78,7 @@ internal static class Renamer
{
var newName = propertyCount > 0 ? $"{score.ReMap.NewTypeName}_{propertyCount}" : score.ReMap.NewTypeName;
- Logger.Log($"Renaming Property: `{property.Name}` on TypeRef `{type}` to {newName}", ConsoleColor.Green);
+ Logger.Log($"Renaming Property: `{property.Name}` on OriginalTypeRef `{type}` to {newName}", ConsoleColor.Green);
property.Name = newName;
}
}