AssemblyTool/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs

532 lines
19 KiB
C#
Raw Permalink Normal View History

using dnlib.DotNet;
2024-06-15 16:45:34 -04:00
using ReCodeIt.Models;
2024-06-16 01:48:48 -04:00
using ReCodeIt.ReMapper;
2024-06-15 16:21:12 -04:00
using ReCodeIt.Utils;
2024-06-18 17:35:31 -04:00
namespace ReCodeIt.AutoMapper;
2024-06-15 16:21:12 -04:00
public class ReCodeItAutoMapper
{
private ModuleDefMD Module { get; set; }
2024-06-15 16:21:12 -04:00
private List<MappingPair> MappingPairs { get; set; } = [];
2024-06-15 19:13:52 -04:00
private List<string> CompilerGeneratedClasses = [];
2024-06-15 16:21:12 -04:00
private List<TypeDef> AllTypes { get; set; } = [];
private List<string> AlreadyChangedNames { get; set; } = [];
2024-06-15 21:05:06 -04:00
private static AutoMapperSettings Settings => DataProvider.Settings.AutoMapper;
2024-06-15 16:21:12 -04:00
2024-06-16 00:19:02 -04:00
private static bool Error { get; set; } = false;
private int FailureCount { get; set; } = 0;
2024-06-16 01:48:48 -04:00
private int TotalFieldRenameCount { get; set; } = 0;
private int TotalPropertyRenameCount { get; set; } = 0;
2024-06-15 22:12:34 -04:00
/// <summary>
/// Start the automapping process
/// </summary>
2024-06-15 16:21:12 -04:00
public void InitializeAutoMapping()
{
Logger.ClearLog();
Logger.Log($"Starting Auto Mapping...");
// Clear any previous pairs
MappingPairs = [];
2024-06-15 19:13:52 -04:00
CompilerGeneratedClasses = [];
AllTypes = [];
AlreadyChangedNames = [];
2024-06-15 19:13:52 -04:00
Module = DataProvider.LoadModule(Settings.AssemblyPath);
2024-06-16 13:56:55 -04:00
2024-06-16 00:19:02 -04:00
Error = false;
FailureCount = 0;
2024-06-16 01:48:48 -04:00
TotalFieldRenameCount = 0;
TotalPropertyRenameCount = 0;
2024-06-16 00:19:02 -04:00
var types = Module.GetTypes();
2024-06-26 20:44:22 -04:00
AllTypes.AddRange(types);
2024-06-15 16:21:12 -04:00
FindCompilerGeneratedObjects(types);
2024-06-15 19:13:52 -04:00
2024-06-26 20:44:22 -04:00
Logger.Log($"Found {AllTypes.Count - CompilerGeneratedClasses.Count} potential remappable types");
Logger.Log($"Found {CompilerGeneratedClasses.Count} compiler generated objects");
2024-06-15 19:13:52 -04:00
foreach (var type in types)
2024-06-15 16:21:12 -04:00
{
2024-06-16 16:21:42 -04:00
// We dont want to do anything with compiler generated objects
if (CompilerGeneratedClasses.Contains(type.Name))
{
continue;
}
2024-06-15 16:21:12 -04:00
MappingPairs.AddRange(FilterFieldNames(type));
MappingPairs.AddRange(FilterPropertyNames(type));
2024-06-16 16:21:42 -04:00
if (Settings.SearchMethods)
{
MappingPairs.AddRange(GatherFromMethods(type));
}
}
PrimaryTypeNameFilter();
2024-06-15 21:05:06 -04:00
SanitizeProposedNames();
2024-06-16 00:19:02 -04:00
StartRenameProcess();
2024-06-15 21:05:06 -04:00
WriteChanges();
2024-06-15 16:21:12 -04:00
}
2024-06-15 22:12:34 -04:00
/// <summary>
/// Finds any compiler generated code so we can ignore it, its mostly LINQ garbage
/// </summary>
/// <param name="types"></param>
private void FindCompilerGeneratedObjects(IEnumerable<TypeDef> types)
2024-06-15 19:13:52 -04:00
{
foreach (var typeDefinition in types)
{
if (typeDefinition.IsClass || typeDefinition.IsInterface || typeDefinition.IsValueType) // Check for class or struct
{
if (typeDefinition.HasCustomAttributes &&
typeDefinition.CustomAttributes.Any(attr => attr.AttributeType.FullName == "System.Runtime.CompilerServices.CompilerGeneratedAttribute"))
{
string typeName = typeDefinition.Name;
CompilerGeneratedClasses.Add(typeName);
//Logger.Log($"Compiler Generated object found: {typeName}", ConsoleColor.Yellow);
}
}
if (typeDefinition.NestedTypes.Count > 0)
{
FindCompilerGeneratedObjects(typeDefinition.NestedTypes);
}
}
}
2024-06-16 16:21:42 -04:00
#region METHODS
private List<MappingPair> GatherFromMethods(TypeDef type)
2024-06-16 16:21:42 -04:00
{
var methodsWithTypes = new List<MappingPair>();
// Handle nested types recursively
foreach (var nestedType in type.NestedTypes)
{
methodsWithTypes.AddRange(GatherFromMethods(nestedType));
}
var methods = type.Methods
// We only want methods with parameters
.Where(m => m.HasParams())
2024-06-16 16:21:42 -04:00
// Only want parameter names of a certain length
.Where(m => m.Parameters.Any(p => p.Name.Length > Settings.MinLengthToMatch));
// Now go over over all filterd methods manually, because fuck this with linq
foreach (var method in methods)
{
// Now over all parameters in the method
foreach (var parm in method.Parameters)
{
// We dont want blacklisted items
if (Settings.MethodParamaterBlackList.Contains(parm.Name)
|| Settings.TypesToIgnore.Contains(parm.Name))
2024-06-16 16:21:42 -04:00
{
continue;
}
//Logger.Log($"Method Data Found");
//Logger.Log($"Parameter count: {method.Parameters.Count}");
//Logger.Log($"Paremeter Names: {string.Join(", ", parmNames)}");
//Logger.Log($"Paremeter Types: {string.Join(", ", parmTypes)}\n");
2024-06-26 20:44:22 -04:00
if (parm.Type.TryGetTypeDef() is null) continue;
2024-06-16 16:21:42 -04:00
var mapPair = new MappingPair(
parm.Type.TryGetTypeDef(),
2024-06-16 16:21:42 -04:00
parm.Name,
parm.Type.TryGetTypeDef().IsInterface,
parm.Type.TryGetTypeDef().Name.Contains("Struct"),
2024-06-16 16:21:42 -04:00
true);
mapPair.AutoMappingResult = AutoMappingResult.Match_From_Method;
2024-06-16 16:21:42 -04:00
methodsWithTypes.Add(mapPair);
}
}
return methodsWithTypes;
}
#endregion METHODS
#region FIELDS_PROPERTIES
2024-06-15 16:21:12 -04:00
/// <summary>
/// Pair field declaring types with their names
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
private List<MappingPair> FilterFieldNames(TypeDef type)
2024-06-15 16:21:12 -04:00
{
var fieldsWithTypes = new List<MappingPair>();
// Handle nested types recursively
foreach (var nestedType in type.NestedTypes)
{
fieldsWithTypes.AddRange(FilterFieldNames(nestedType));
}
var fields = type.Fields
// we dont want names shorter than 4
.Where(f => f.Name.Length > 3)
2024-06-15 16:21:12 -04:00
// Skip value types
.Where(f => !f.FieldType.IsValueType)
2024-06-16 00:19:02 -04:00
// TODO: Renaming arrays is strange, come back to this later
.Where(p => !p.FieldType.IsArray)
2024-06-15 16:21:12 -04:00
// We dont want fields in the system type ignore list
.Where(f => !Settings.TypesToIgnore.Contains(f.Name.TrimAfterSpecialChar()));
2024-06-15 16:21:12 -04:00
// Include fields from the current type
foreach (var field in fields)
{
2024-06-16 00:19:02 -04:00
//Logger.Log($"Collecting Field: OriginalTypeRef: {field.FieldType.Name.TrimAfterSpecialChar()} Field Name: {field.Name}");
2024-06-15 16:21:12 -04:00
var typeDef = field.FieldType.TryGetTypeDef();
2024-06-16 01:48:48 -04:00
// Dont rename things we cant resolve
if (typeDef is null) { continue; }
2024-06-16 16:21:42 -04:00
var pair = new MappingPair(
2024-06-16 01:48:48 -04:00
typeDef,
2024-06-15 21:05:06 -04:00
field.Name,
typeDef.Name.Contains("Interface"),
typeDef.Name.Contains("Struct"),
2024-06-16 16:21:42 -04:00
field.IsPublic);
pair.AutoMappingResult = AutoMappingResult.Match_From_Field;
2024-06-16 16:21:42 -04:00
fieldsWithTypes.Add(pair);
2024-06-15 16:21:12 -04:00
}
return fieldsWithTypes;
}
/// <summary>
/// Pair field declaring types with their names
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
private IEnumerable<MappingPair> FilterPropertyNames(TypeDef type)
2024-06-15 16:21:12 -04:00
{
var propertiesWithTypes = new List<MappingPair>();
// Handle nested types recursively
foreach (var nestedType in type.NestedTypes)
{
propertiesWithTypes.AddRange(FilterPropertyNames(nestedType));
}
var properties = type.Properties
// we dont want names shorter than 4
.Where(p => p.Name.Length > 3)
2024-06-15 16:21:12 -04:00
// Skip value types
.Where(p => !p.PropertySig.RetType.GetIsValueType())
2024-06-15 16:21:12 -04:00
2024-06-16 00:19:02 -04:00
// TODO: Renaming arrays is strange, come back to this later
.Where(p => !p.PropertySig.RetType.IsArray)
2024-06-16 00:19:02 -04:00
2024-06-15 16:21:12 -04:00
// We dont want fields in the global ignore list
.Where(p => !Settings.TypesToIgnore.Contains(p.Name.TrimAfterSpecialChar()));
2024-06-15 16:21:12 -04:00
// Include fields from the current type
foreach (var property in properties)
{
2024-06-16 00:19:02 -04:00
//Logger.Log($"Collecting Property: OriginalTypeRef: {property.PropertyType.Name.TrimAfterSpecialChar()} Field Name: {property.Name}");
2024-06-15 16:21:12 -04:00
var typeDef = property.PropertySig.RetType.TryGetTypeDef();
2024-06-16 01:48:48 -04:00
// Dont rename things we cant resolve
if (typeDef is null) { continue; }
2024-06-15 21:05:06 -04:00
2024-06-16 16:21:42 -04:00
var mapPair = new MappingPair(
2024-06-16 01:48:48 -04:00
typeDef,
2024-06-15 21:05:06 -04:00
property.Name,
typeDef.Name.Contains("Interface"),
typeDef.Name.Contains("Struct"),
2024-06-16 16:21:42 -04:00
true);
mapPair.AutoMappingResult = AutoMappingResult.Match_From_Property;
2024-06-16 16:21:42 -04:00
propertiesWithTypes.Add(mapPair);
2024-06-15 16:21:12 -04:00
}
return propertiesWithTypes;
}
2024-06-16 16:21:42 -04:00
#endregion FIELDS_PROPERTIES
#region FILTER
2024-06-15 16:45:34 -04:00
/// <summary>
2024-06-15 21:05:06 -04:00
/// This giant linq statement handles all of the filtering once the initial gathering of fields
/// and properties is complete
2024-06-15 16:45:34 -04:00
/// </summary>
private void PrimaryTypeNameFilter()
2024-06-15 16:21:12 -04:00
{
// Filter types to the ones we're looking for
var mappingPairs = MappingPairs
2024-06-15 16:45:34 -04:00
// Filter based on length, short lengths dont make good class names
.Where(pair => pair.Name.Length >= Settings.MinLengthToMatch)
2024-06-15 16:21:12 -04:00
2024-06-15 21:05:06 -04:00
// Filter out anything that doesnt start with our specified tokens (Where
2024-06-16 00:19:02 -04:00
// pair.OriginalTypeRef.Name is the property OriginalTypeRef name `Class1202` and token
// is start identifer we are looking for `GClass`
2024-06-15 17:36:40 -04:00
.Where(pair => Settings.TokensToMatch
2024-06-16 01:48:48 -04:00
.Any(token => pair.OriginalTypeDefinition.Name.StartsWith(token)))
2024-06-15 17:36:40 -04:00
// Filter out anything that has the same name as the type, we cant remap those
.Where(pair => !Settings.TokensToMatch
.Any(token => pair.Name.ToLower().StartsWith(token.ToLower())))
// Filter based on direct name blacklist (Where pair.Name is the property name and token
// is blacklisted item `Columns`
.Where(pair => !Settings.PropertyFieldBlackList
.Any(token => pair.Name.ToLower().StartsWith(token.ToLower())))
2024-06-15 21:05:06 -04:00
// Filter out backing fields
/// This is slow, but oh well
.Where(pair => !pair.Name.ToCharArray().Contains('<')).ToList();
MappingPairs = mappingPairs;
SecondaryTypeNameFilter();
}
/// <summary>
/// This is where we filter down based on more specific parameters
/// </summary>
/// <param name="mappingPairs"></param>
private void SecondaryTypeNameFilter()
{
// Filter property/field names by required number of matches
MappingPairs = MappingPairs
.GroupBy(pair => pair.OriginalPropOrFieldName.TrimAfterSpecialChar())
.Where(group => group.Count() > Settings.RequiredMatches)
.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();
2024-06-15 21:05:06 -04:00
FinalGroupAndSelect();
}
2024-06-16 16:21:42 -04:00
/// <summary>
/// This is where we make sure everything is original
/// </summary>
private void FinalGroupAndSelect()
{
MappingPairs = MappingPairs
2024-06-15 17:36:40 -04:00
// We only want types once, so make it unique
2024-06-16 01:48:48 -04:00
.GroupBy(pair => pair.OriginalTypeDefinition.FullName)
2024-06-15 17:36:40 -04:00
.Select(group => group.First())
2024-06-15 21:05:06 -04:00
.GroupBy(pair => pair.Name)
.Select(group => group.First()).ToList();
2024-06-15 21:05:06 -04:00
}
2024-06-15 16:21:12 -04:00
2024-06-16 16:21:42 -04:00
#endregion FILTER
#region OUTPUT
2024-06-15 22:12:34 -04:00
/// <summary>
/// Sanitizes and prepares mapping pairs for remapping once filtering is complete.
/// </summary>
2024-06-15 21:05:06 -04:00
private void SanitizeProposedNames()
{
foreach (var pair in MappingPairs)
2024-06-15 16:21:12 -04:00
{
2024-06-15 21:05:06 -04:00
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)
{
pair.Name = string.Concat("I", pair.Name.AsSpan(0));
2024-06-16 19:20:42 -04:00
pair.Name = pair.Name.Replace("Class", "");
2024-06-15 21:05:06 -04:00
}
2024-06-16 01:48:48 -04:00
/*
2024-06-15 22:12:34 -04:00
// Try and remove any trailing 's' that exist
if (pair.WasCollection)
{
if (pair.Name.ToLower().EndsWith('s'))
{
pair.Name = pair.Name.Substring(0, pair.Name.Length - 1);
}
}
2024-06-16 01:48:48 -04:00
*/
2024-06-15 21:05:06 -04:00
2024-06-16 19:20:42 -04:00
if (pair.IsInterface)
{
// Replace class if it exists
pair.Name = pair.Name.Replace("Class", "");
}
else if (pair.IsStruct)
{
pair.Name = string.Concat(pair.Name, "Struct");
}
else
{
pair.Name = string.Concat(pair.Name, "Class");
2024-06-15 21:05:06 -04:00
}
Logger.Log($"------------------------------------------------------------------------");
2024-06-16 01:48:48 -04:00
Logger.Log($"Original Name: {pair.OriginalTypeDefinition.FullName} : Sanitized Name: {pair.Name}");
2024-06-15 21:05:06 -04:00
Logger.Log($"Matched From Name: {pair.OriginalPropOrFieldName}");
Logger.Log($"IsInterface: {pair.IsInterface}");
Logger.Log($"IsStruct: {pair.IsStruct}");
Logger.Log($"Is match from: {pair.AutoMappingResult}");
2024-06-15 21:05:06 -04:00
Logger.Log($"------------------------------------------------------------------------");
2024-06-15 16:21:12 -04:00
}
2024-06-15 21:37:55 -04:00
Logger.Log($"Automatically remapped {MappingPairs.Count()} objects");
2024-06-15 21:05:06 -04:00
}
2024-06-15 16:21:12 -04:00
2024-06-16 00:19:02 -04:00
/// <summary>
/// Start renaming assembly definitions
/// </summary>
private void StartRenameProcess()
{
2024-06-16 01:48:48 -04:00
// Gather up any matches we have
foreach (var type in Module.GetTypes().ToArray())
2024-06-16 00:19:02 -04:00
{
foreach (var pair in MappingPairs.ToArray())
{
GatherMatchedTypeRefs(pair, type);
}
}
2024-06-16 01:48:48 -04:00
// Rename Types to matched types
2024-06-16 00:19:02 -04:00
foreach (var pair in MappingPairs)
{
if (pair.NewTypeRef != null && !AlreadyChangedNames.Contains(pair.Name))
2024-06-16 00:19:02 -04:00
{
Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Green);
2024-06-16 01:48:48 -04:00
Logger.Log($"Renaming: {pair.OriginalTypeDefinition.Name} to {pair.Name}", ConsoleColor.Green);
Logger.Log($"Is match from method: {pair.AutoMappingResult}", ConsoleColor.Green);
2024-06-16 01:48:48 -04:00
var fieldCount = RenameHelper.RenameAllFields(
pair.OriginalTypeDefinition.Name,
pair.Name,
Module.GetTypes());
2024-06-16 01:48:48 -04:00
var propCount = RenameHelper.RenameAllProperties(
pair.OriginalTypeDefinition.Name,
pair.Name,
Module.GetTypes());
2024-06-16 01:48:48 -04:00
Logger.Log($"Renamed: {fieldCount} fields", ConsoleColor.Green);
Logger.Log($"Renamed: {propCount} properties", ConsoleColor.Green);
2024-06-16 00:19:02 -04:00
Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Green);
2024-06-16 01:48:48 -04:00
AlreadyChangedNames.Add(pair.Name);
2024-06-16 01:48:48 -04:00
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;
2024-06-16 00:19:02 -04:00
}
}
// Do a final error check
foreach (var pair in MappingPairs)
{
if (!pair.HasBeenRenamed)
{
Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Red);
2024-06-16 01:48:48 -04:00
Logger.Log($"Renaming: {pair.OriginalTypeDefinition.Name} to {pair.Name} has failed", ConsoleColor.Red);
Logger.Log($"Result Code: {pair.AutoMappingResult}", ConsoleColor.Red);
2024-06-16 00:19:02 -04:00
Logger.Log($"IsInterface: {pair.IsInterface}", ConsoleColor.Red);
Logger.Log($"IsStruct: {pair.IsStruct}", ConsoleColor.Red);
Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Red);
FailureCount++;
Error = true;
}
}
}
/// <summary>
/// Recursively handle all renaming on nested types on a given type
/// </summary>
/// <param name="pair"></param>
/// <param name="type"></param>
private void GatherMatchedTypeRefs(MappingPair pair, TypeDef type)
2024-06-16 00:19:02 -04:00
{
// Handle nested types recursively
foreach (var nestedType in type.NestedTypes.ToArray())
{
GatherMatchedTypeRefs(pair, nestedType);
}
2024-06-16 01:48:48 -04:00
if (type == pair.OriginalTypeDefinition)
2024-06-16 00:19:02 -04:00
{
pair.NewTypeRef = type;
}
}
2024-06-15 21:05:06 -04:00
private void WriteChanges()
{
2024-06-26 20:50:53 -04:00
var path = Path.Combine(Settings.OutputPath, Module.Assembly.Name + "-auto_mapped.dll");
2024-06-16 03:43:00 -04:00
2024-06-26 20:50:53 -04:00
Module.Write(path);
var fieldCountMatchResult = MappingPairs
.Count(x => x.AutoMappingResult == AutoMappingResult.Match_From_Field);
var propertyCountMatchResult = MappingPairs
.Count(x => x.AutoMappingResult == AutoMappingResult.Match_From_Property);
var methodCountMatchResult = MappingPairs
.Count(x => x.AutoMappingResult == AutoMappingResult.Match_From_Method);
2024-06-16 16:21:42 -04:00
2024-06-16 03:43:00 -04:00
Logger.Log($"-------------------------------RESULT-----------------------------------", ConsoleColor.Green);
2024-06-26 20:50:53 -04:00
Logger.Log($"Complete: Assembly written to `{path}`", ConsoleColor.Green);
2024-06-16 03:43:00 -04:00
Logger.Log($"Found {MappingPairs.Count()} automatic remaps", ConsoleColor.Green);
2024-06-16 16:21:42 -04:00
Logger.Log($"Found {fieldCountMatchResult} automatic remaps from fields", ConsoleColor.Green);
Logger.Log($"Found {propertyCountMatchResult} automatic remaps from properties", ConsoleColor.Green);
Logger.Log($"Found {methodCountMatchResult} automatic remaps from methods", ConsoleColor.Green);
2024-06-16 03:43:00 -04:00
Logger.Log($"Renamed {TotalFieldRenameCount} fields", ConsoleColor.Green);
Logger.Log($"Renamed {TotalPropertyRenameCount} properties", ConsoleColor.Green);
2024-06-16 16:21:42 -04:00
Logger.Log($"Failed to rename: {FailureCount} mapping pairs", (FailureCount == 0 ? ConsoleColor.Green : ConsoleColor.Red));
2024-06-16 03:43:00 -04:00
Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Green);
2024-06-15 16:21:12 -04:00
}
2024-06-16 16:21:42 -04:00
#endregion OUTPUT
2024-06-15 16:21:12 -04:00
}