dont remap existing names, improve error handling

This commit is contained in:
Cj 2024-06-16 19:10:39 -04:00
parent f847ab424d
commit c2728ab0a4
5 changed files with 77 additions and 23 deletions

View File

@ -135,6 +135,12 @@ public partial class ReCodeItForm : Form
{ {
if (ReCodeItRemapper.IsRunning) { return; } 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(); Console.Clear();
Remapper.InitializeRemap(); Remapper.InitializeRemap();
@ -335,6 +341,12 @@ public partial class ReCodeItForm : Form
private void RunAutoRemapButton_Click(object sender, EventArgs e) 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(); AutoMapper.InitializeAutoMapping();
} }

View File

@ -13,7 +13,6 @@ internal static class Program
{ {
DataProvider.LoadAppSettings(); DataProvider.LoadAppSettings();
DataProvider.LoadMappingFile(); DataProvider.LoadMappingFile();
DataProvider.LoadAssemblyDefinition();
// To customize application configuration such as set high DPI settings or default font, see https://aka.ms/applicationconfiguration. // To customize application configuration such as set high DPI settings or default font, see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();

View File

@ -41,7 +41,7 @@ public sealed class MappingPair(
/// <summary> /// <summary>
/// Did this match come from a method? /// Did this match come from a method?
/// </summary> /// </summary>
public EMapPairSource IsMatchFrom { get; set; } = EMapPairSource.None; public AutoMappingResult AutoMappingResult { get; set; } = AutoMappingResult.None;
/// <summary> /// <summary>
/// This is the name we want to change the assembly class to /// 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 string OriginalPropOrFieldName { get; } = name;
} }
public enum EMapPairSource public enum AutoMappingResult
{ {
None, None,
Field, Match_From_Field,
Property, Match_From_Property,
Method Match_From_Method,
Fail_From_Already_Contained_Name,
Fail_From_New_Type_Ref_Null,
} }

View File

@ -13,6 +13,10 @@ public class ReCodeItAutoMapper
private List<string> CompilerGeneratedClasses = []; private List<string> CompilerGeneratedClasses = [];
private List<TypeDefinition> AllTypes { get; set; } = [];
private List<string> AlreadyChangedNames { get; set; } = [];
private static AutoMapperSettings Settings => DataProvider.Settings.AutoMapper; private static AutoMapperSettings Settings => DataProvider.Settings.AutoMapper;
private static bool Error { get; set; } = false; private static bool Error { get; set; } = false;
@ -32,6 +36,8 @@ public class ReCodeItAutoMapper
// Clear any previous pairs // Clear any previous pairs
MappingPairs = []; MappingPairs = [];
CompilerGeneratedClasses = []; CompilerGeneratedClasses = [];
AllTypes = [];
AlreadyChangedNames = [];
DataProvider.LoadAssemblyDefinition(); DataProvider.LoadAssemblyDefinition();
@ -42,10 +48,13 @@ public class ReCodeItAutoMapper
FindCompilerGeneratedObjects(DataProvider.ModuleDefinition.Types); FindCompilerGeneratedObjects(DataProvider.ModuleDefinition.Types);
Logger.Log($"Found {CompilerGeneratedClasses.Count} Compiler generated objects");
var types = DataProvider.ModuleDefinition.Types; 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) foreach (var type in types)
{ {
// We dont want to do anything with compiler generated objects // We dont want to do anything with compiler generated objects
@ -67,11 +76,22 @@ public class ReCodeItAutoMapper
SanitizeProposedNames(); SanitizeProposedNames();
StartRenameProcess(); StartRenameProcess();
if (Error) { return; }
WriteChanges(); WriteChanges();
} }
private void GetAllTypes(Collection<TypeDefinition> types)
{
AllTypes.AddRange(types);
foreach (var type in types)
{
if (type.HasNestedTypes)
{
GetAllTypes(type.NestedTypes);
}
}
}
/// <summary> /// <summary>
/// Finds any compiler generated code so we can ignore it, its mostly LINQ garbage /// Finds any compiler generated code so we can ignore it, its mostly LINQ garbage
/// </summary> /// </summary>
@ -147,7 +167,7 @@ public class ReCodeItAutoMapper
parm.ParameterType.Name.Contains("Struct"), parm.ParameterType.Name.Contains("Struct"),
true); true);
mapPair.IsMatchFrom = EMapPairSource.Method; mapPair.AutoMappingResult = AutoMappingResult.Match_From_Method;
methodsWithTypes.Add(mapPair); methodsWithTypes.Add(mapPair);
} }
@ -205,7 +225,7 @@ public class ReCodeItAutoMapper
field.FieldType.Name.Contains("Struct"), field.FieldType.Name.Contains("Struct"),
field.IsPublic); field.IsPublic);
pair.IsMatchFrom = EMapPairSource.Field; pair.AutoMappingResult = AutoMappingResult.Match_From_Field;
fieldsWithTypes.Add(pair); fieldsWithTypes.Add(pair);
} }
@ -258,7 +278,7 @@ public class ReCodeItAutoMapper
property.PropertyType.Name.Contains("Struct"), property.PropertyType.Name.Contains("Struct"),
true); true);
mapPair.IsMatchFrom = EMapPairSource.Property; mapPair.AutoMappingResult = AutoMappingResult.Match_From_Property;
propertiesWithTypes.Add(mapPair); propertiesWithTypes.Add(mapPair);
} }
@ -314,7 +334,11 @@ public class ReCodeItAutoMapper
MappingPairs = MappingPairs MappingPairs = MappingPairs
.GroupBy(pair => pair.OriginalPropOrFieldName.TrimAfterSpecialChar()) .GroupBy(pair => pair.OriginalPropOrFieldName.TrimAfterSpecialChar())
.Where(group => group.Count() > Settings.RequiredMatches) .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(); FinalGroupAndSelect();
} }
@ -373,7 +397,7 @@ public class ReCodeItAutoMapper
} }
*/ */
// If its not an interface, its a struct or class // If its not an interface, its a struct or class
switch (pair.IsStruct) switch (pair.IsStruct && !pair.IsInterface)
{ {
case true: case true:
pair.Name = string.Concat(pair.Name, "Struct"); pair.Name = string.Concat(pair.Name, "Struct");
@ -389,7 +413,7 @@ public class ReCodeItAutoMapper
Logger.Log($"Matched From Name: {pair.OriginalPropOrFieldName}"); Logger.Log($"Matched From Name: {pair.OriginalPropOrFieldName}");
Logger.Log($"IsInterface: {pair.IsInterface}"); Logger.Log($"IsInterface: {pair.IsInterface}");
Logger.Log($"IsStruct: {pair.IsStruct}"); Logger.Log($"IsStruct: {pair.IsStruct}");
Logger.Log($"Is match from: {pair.IsMatchFrom}"); Logger.Log($"Is match from: {pair.AutoMappingResult}");
Logger.Log($"------------------------------------------------------------------------"); Logger.Log($"------------------------------------------------------------------------");
} }
@ -413,11 +437,11 @@ public class ReCodeItAutoMapper
// Rename Types to matched types // Rename Types to matched types
foreach (var pair in MappingPairs) foreach (var pair in MappingPairs)
{ {
if (pair.NewTypeRef != null) if (pair.NewTypeRef != null && !AlreadyChangedNames.Contains(pair.Name))
{ {
Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Green); Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Green);
Logger.Log($"Renaming: {pair.OriginalTypeDefinition.Name} to {pair.Name}", 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( var fieldCount = RenameHelper.RenameAllFields(
pair.OriginalTypeDefinition.Name, pair.OriginalTypeDefinition.Name,
@ -436,8 +460,24 @@ public class ReCodeItAutoMapper
Logger.Log($"Renamed: {propCount} properties", ConsoleColor.Green); Logger.Log($"Renamed: {propCount} properties", ConsoleColor.Green);
Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Green); Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Green);
AlreadyChangedNames.Add(pair.Name);
pair.NewTypeRef.Name = pair.Name; pair.NewTypeRef.Name = pair.Name;
pair.HasBeenRenamed = true; 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($"------------------------------------------------------------------------", ConsoleColor.Red);
Logger.Log($"Renaming: {pair.OriginalTypeDefinition.Name} to {pair.Name} has failed", 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($"IsInterface: {pair.IsInterface}", ConsoleColor.Red);
Logger.Log($"IsStruct: {pair.IsStruct}", ConsoleColor.Red); Logger.Log($"IsStruct: {pair.IsStruct}", ConsoleColor.Red);
Logger.Log($"Is match from: {pair.IsMatchFrom}", ConsoleColor.Red);
Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Red); Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Red);
FailureCount++; FailureCount++;
@ -483,9 +522,9 @@ public class ReCodeItAutoMapper
{ {
var path = DataProvider.WriteAssemblyDefinition(); var path = DataProvider.WriteAssemblyDefinition();
var fieldCountMatchResult = MappingPairs.Where(x => x.IsMatchFrom == EMapPairSource.Field).Count(); var fieldCountMatchResult = MappingPairs.Where(x => x.AutoMappingResult == AutoMappingResult.Match_From_Property).Count();
var propertyCountMatchResult = MappingPairs.Where(x => x.IsMatchFrom == EMapPairSource.Property).Count(); var propertyCountMatchResult = MappingPairs.Where(x => x.AutoMappingResult == AutoMappingResult.Match_From_Property).Count();
var methodCountMatchResult = MappingPairs.Where(x => x.IsMatchFrom == EMapPairSource.Method).Count(); var methodCountMatchResult = MappingPairs.Where(x => x.AutoMappingResult == AutoMappingResult.Match_From_Method).Count();
Logger.Log($"-------------------------------RESULT-----------------------------------", ConsoleColor.Green); Logger.Log($"-------------------------------RESULT-----------------------------------", ConsoleColor.Green);
Logger.Log($"Complete: Assembly written to `{path}`", ConsoleColor.Green); Logger.Log($"Complete: Assembly written to `{path}`", ConsoleColor.Green);

View File

@ -22,6 +22,8 @@ public class ReCodeItRemapper
/// </summary> /// </summary>
public void InitializeRemap() public void InitializeRemap()
{ {
DataProvider.LoadAssemblyDefinition();
IsRunning = true; IsRunning = true;
DisplayBasicModuleInformation(); DisplayBasicModuleInformation();