diff --git a/RecodeItGUI/GUI/Main.cs b/RecodeItGUI/GUI/Main.cs index 07ff124..de7c010 100644 --- a/RecodeItGUI/GUI/Main.cs +++ b/RecodeItGUI/GUI/Main.cs @@ -135,6 +135,12 @@ public partial class ReCodeItForm : Form { if (ReCodeItRemapper.IsRunning) { return; } + if (string.IsNullOrEmpty(DataProvider.Settings.AppSettings.AssemblyPath)) + { + MessageBox.Show("Please go to the settings tab and load an assembly and select and output location", "Assembly not loaded"); + return; + } + Console.Clear(); Remapper.InitializeRemap(); @@ -335,6 +341,12 @@ public partial class ReCodeItForm : Form private void RunAutoRemapButton_Click(object sender, EventArgs e) { + if (string.IsNullOrEmpty(DataProvider.Settings.AppSettings.AssemblyPath)) + { + MessageBox.Show("Please go to the settings tab and load an assembly and select and output location", "Assembly not loaded"); + return; + } + AutoMapper.InitializeAutoMapping(); } diff --git a/RecodeItGUI/Program.cs b/RecodeItGUI/Program.cs index 8682433..f16c311 100644 --- a/RecodeItGUI/Program.cs +++ b/RecodeItGUI/Program.cs @@ -13,7 +13,6 @@ internal static class Program { DataProvider.LoadAppSettings(); DataProvider.LoadMappingFile(); - DataProvider.LoadAssemblyDefinition(); // To customize application configuration such as set high DPI settings or default font, see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); diff --git a/RecodeItLib/AutoMapper/MappingPair.cs b/RecodeItLib/AutoMapper/MappingPair.cs index 812b32a..c2e5abf 100644 --- a/RecodeItLib/AutoMapper/MappingPair.cs +++ b/RecodeItLib/AutoMapper/MappingPair.cs @@ -41,7 +41,7 @@ public sealed class MappingPair( /// /// Did this match come from a method? /// - public EMapPairSource IsMatchFrom { get; set; } = EMapPairSource.None; + public AutoMappingResult AutoMappingResult { get; set; } = AutoMappingResult.None; /// /// This is the name we want to change the assembly class to @@ -54,10 +54,12 @@ public sealed class MappingPair( public string OriginalPropOrFieldName { get; } = name; } -public enum EMapPairSource +public enum AutoMappingResult { None, - Field, - Property, - Method + Match_From_Field, + Match_From_Property, + Match_From_Method, + Fail_From_Already_Contained_Name, + Fail_From_New_Type_Ref_Null, } \ No newline at end of file diff --git a/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs b/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs index a1ea2dd..657a2ac 100644 --- a/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs +++ b/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs @@ -13,6 +13,10 @@ public class ReCodeItAutoMapper private List CompilerGeneratedClasses = []; + private List AllTypes { get; set; } = []; + + private List AlreadyChangedNames { get; set; } = []; + private static AutoMapperSettings Settings => DataProvider.Settings.AutoMapper; private static bool Error { get; set; } = false; @@ -32,6 +36,8 @@ public class ReCodeItAutoMapper // Clear any previous pairs MappingPairs = []; CompilerGeneratedClasses = []; + AllTypes = []; + AlreadyChangedNames = []; DataProvider.LoadAssemblyDefinition(); @@ -42,10 +48,13 @@ public class ReCodeItAutoMapper FindCompilerGeneratedObjects(DataProvider.ModuleDefinition.Types); - Logger.Log($"Found {CompilerGeneratedClasses.Count} Compiler generated objects"); - var types = DataProvider.ModuleDefinition.Types; + GetAllTypes(types); + + Logger.Log($"Found {CompilerGeneratedClasses.Count - AllTypes.Count} potential remappable types"); + Logger.Log($"Found {CompilerGeneratedClasses.Count} compiler generated objects"); + foreach (var type in types) { // We dont want to do anything with compiler generated objects @@ -67,11 +76,22 @@ public class ReCodeItAutoMapper SanitizeProposedNames(); StartRenameProcess(); - if (Error) { return; } - WriteChanges(); } + private void GetAllTypes(Collection types) + { + AllTypes.AddRange(types); + + foreach (var type in types) + { + if (type.HasNestedTypes) + { + GetAllTypes(type.NestedTypes); + } + } + } + /// /// Finds any compiler generated code so we can ignore it, its mostly LINQ garbage /// @@ -147,7 +167,7 @@ public class ReCodeItAutoMapper parm.ParameterType.Name.Contains("Struct"), true); - mapPair.IsMatchFrom = EMapPairSource.Method; + mapPair.AutoMappingResult = AutoMappingResult.Match_From_Method; methodsWithTypes.Add(mapPair); } @@ -205,7 +225,7 @@ public class ReCodeItAutoMapper field.FieldType.Name.Contains("Struct"), field.IsPublic); - pair.IsMatchFrom = EMapPairSource.Field; + pair.AutoMappingResult = AutoMappingResult.Match_From_Field; fieldsWithTypes.Add(pair); } @@ -258,7 +278,7 @@ public class ReCodeItAutoMapper property.PropertyType.Name.Contains("Struct"), true); - mapPair.IsMatchFrom = EMapPairSource.Property; + mapPair.AutoMappingResult = AutoMappingResult.Match_From_Property; propertiesWithTypes.Add(mapPair); } @@ -314,7 +334,11 @@ public class ReCodeItAutoMapper MappingPairs = MappingPairs .GroupBy(pair => pair.OriginalPropOrFieldName.TrimAfterSpecialChar()) .Where(group => group.Count() > Settings.RequiredMatches) - .SelectMany(group => group).ToList(); + .SelectMany(group => group) + .ToList() + // We dont want names that already exist to be considered + .Where(pair => AllTypes + .Any(token => !pair.OriginalTypeDefinition.FullName.Contains(token.FullName))).ToList(); FinalGroupAndSelect(); } @@ -373,7 +397,7 @@ public class ReCodeItAutoMapper } */ // If its not an interface, its a struct or class - switch (pair.IsStruct) + switch (pair.IsStruct && !pair.IsInterface) { case true: pair.Name = string.Concat(pair.Name, "Struct"); @@ -389,7 +413,7 @@ public class ReCodeItAutoMapper Logger.Log($"Matched From Name: {pair.OriginalPropOrFieldName}"); Logger.Log($"IsInterface: {pair.IsInterface}"); Logger.Log($"IsStruct: {pair.IsStruct}"); - Logger.Log($"Is match from: {pair.IsMatchFrom}"); + Logger.Log($"Is match from: {pair.AutoMappingResult}"); Logger.Log($"------------------------------------------------------------------------"); } @@ -413,11 +437,11 @@ public class ReCodeItAutoMapper // Rename Types to matched types foreach (var pair in MappingPairs) { - if (pair.NewTypeRef != null) + if (pair.NewTypeRef != null && !AlreadyChangedNames.Contains(pair.Name)) { Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Green); Logger.Log($"Renaming: {pair.OriginalTypeDefinition.Name} to {pair.Name}", ConsoleColor.Green); - Logger.Log($"Is match from method: {pair.IsMatchFrom}", ConsoleColor.Green); + Logger.Log($"Is match from method: {pair.AutoMappingResult}", ConsoleColor.Green); var fieldCount = RenameHelper.RenameAllFields( pair.OriginalTypeDefinition.Name, @@ -436,8 +460,24 @@ public class ReCodeItAutoMapper Logger.Log($"Renamed: {propCount} properties", ConsoleColor.Green); Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Green); + AlreadyChangedNames.Add(pair.Name); pair.NewTypeRef.Name = pair.Name; pair.HasBeenRenamed = true; + continue; + } + + if (pair.HasBeenRenamed) { continue; } + + // Set some error codes + + if (AlreadyChangedNames.Contains(pair.Name)) + { + pair.AutoMappingResult = AutoMappingResult.Fail_From_Already_Contained_Name; + } + + if (pair.NewTypeRef == null) + { + pair.AutoMappingResult = AutoMappingResult.Fail_From_New_Type_Ref_Null; } } @@ -448,10 +488,9 @@ public class ReCodeItAutoMapper { Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Red); Logger.Log($"Renaming: {pair.OriginalTypeDefinition.Name} to {pair.Name} has failed", ConsoleColor.Red); - Logger.Log($"Trying to match: {pair.IsInterface}", ConsoleColor.Red); + Logger.Log($"Result Code: {pair.AutoMappingResult}", ConsoleColor.Red); Logger.Log($"IsInterface: {pair.IsInterface}", ConsoleColor.Red); Logger.Log($"IsStruct: {pair.IsStruct}", ConsoleColor.Red); - Logger.Log($"Is match from: {pair.IsMatchFrom}", ConsoleColor.Red); Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Red); FailureCount++; @@ -483,9 +522,9 @@ public class ReCodeItAutoMapper { var path = DataProvider.WriteAssemblyDefinition(); - var fieldCountMatchResult = MappingPairs.Where(x => x.IsMatchFrom == EMapPairSource.Field).Count(); - var propertyCountMatchResult = MappingPairs.Where(x => x.IsMatchFrom == EMapPairSource.Property).Count(); - var methodCountMatchResult = MappingPairs.Where(x => x.IsMatchFrom == EMapPairSource.Method).Count(); + var fieldCountMatchResult = MappingPairs.Where(x => x.AutoMappingResult == AutoMappingResult.Match_From_Property).Count(); + var propertyCountMatchResult = MappingPairs.Where(x => x.AutoMappingResult == AutoMappingResult.Match_From_Property).Count(); + var methodCountMatchResult = MappingPairs.Where(x => x.AutoMappingResult == AutoMappingResult.Match_From_Method).Count(); Logger.Log($"-------------------------------RESULT-----------------------------------", ConsoleColor.Green); Logger.Log($"Complete: Assembly written to `{path}`", ConsoleColor.Green); diff --git a/RecodeItLib/Remapper/ReCodeItRemapper.cs b/RecodeItLib/Remapper/ReCodeItRemapper.cs index 1c72870..d948d70 100644 --- a/RecodeItLib/Remapper/ReCodeItRemapper.cs +++ b/RecodeItLib/Remapper/ReCodeItRemapper.cs @@ -22,6 +22,8 @@ public class ReCodeItRemapper /// public void InitializeRemap() { + DataProvider.LoadAssemblyDefinition(); + IsRunning = true; DisplayBasicModuleInformation();