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 (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();
}

View File

@ -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();

View File

@ -41,7 +41,7 @@ public sealed class MappingPair(
/// <summary>
/// Did this match come from a method?
/// </summary>
public EMapPairSource IsMatchFrom { get; set; } = EMapPairSource.None;
public AutoMappingResult AutoMappingResult { get; set; } = AutoMappingResult.None;
/// <summary>
/// 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,
}

View File

@ -13,6 +13,10 @@ public class ReCodeItAutoMapper
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 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<TypeDefinition> types)
{
AllTypes.AddRange(types);
foreach (var type in types)
{
if (type.HasNestedTypes)
{
GetAllTypes(type.NestedTypes);
}
}
}
/// <summary>
/// Finds any compiler generated code so we can ignore it, its mostly LINQ garbage
/// </summary>
@ -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);

View File

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