Automapper finishing touches

This commit is contained in:
Cj 2024-06-15 21:05:06 -04:00
parent 88373b084e
commit 5a97aca1cf
6 changed files with 99 additions and 30 deletions

View File

@ -685,7 +685,7 @@ partial class ReCodeItForm
NewTypeName.BackColor = SystemColors.ScrollBar; NewTypeName.BackColor = SystemColors.ScrollBar;
NewTypeName.Location = new Point(10, 30); NewTypeName.Location = new Point(10, 30);
NewTypeName.Name = "NewTypeName"; NewTypeName.Name = "NewTypeName";
NewTypeName.PlaceholderText = "New Type Name"; NewTypeName.PlaceholderText = "New TypeRef Name";
NewTypeName.Size = new Size(208, 31); NewTypeName.Size = new Size(208, 31);
NewTypeName.TabIndex = 0; NewTypeName.TabIndex = 0;
// //
@ -726,7 +726,7 @@ partial class ReCodeItForm
NestedTypeCountEnabled.Name = "NestedTypeCountEnabled"; NestedTypeCountEnabled.Name = "NestedTypeCountEnabled";
NestedTypeCountEnabled.Size = new Size(189, 29); NestedTypeCountEnabled.Size = new Size(189, 29);
NestedTypeCountEnabled.TabIndex = 12; NestedTypeCountEnabled.TabIndex = 12;
NestedTypeCountEnabled.Text = "Nested Type Count"; NestedTypeCountEnabled.Text = "Nested TypeRef Count";
NestedTypeCountEnabled.UseVisualStyleBackColor = true; NestedTypeCountEnabled.UseVisualStyleBackColor = true;
// //
// PropertyCountUpDown // PropertyCountUpDown
@ -781,7 +781,7 @@ partial class ReCodeItForm
NestedTypeParentName.BackColor = SystemColors.ScrollBar; NestedTypeParentName.BackColor = SystemColors.ScrollBar;
NestedTypeParentName.Location = new Point(224, 106); NestedTypeParentName.Location = new Point(224, 106);
NestedTypeParentName.Name = "NestedTypeParentName"; NestedTypeParentName.Name = "NestedTypeParentName";
NestedTypeParentName.PlaceholderText = "Nested Type Parent Name"; NestedTypeParentName.PlaceholderText = "Nested TypeRef Parent Name";
NestedTypeParentName.Size = new Size(208, 31); NestedTypeParentName.Size = new Size(208, 31);
NestedTypeParentName.TabIndex = 0; NestedTypeParentName.TabIndex = 0;
// //
@ -817,7 +817,7 @@ partial class ReCodeItForm
OriginalTypeName.BackColor = SystemColors.ScrollBar; OriginalTypeName.BackColor = SystemColors.ScrollBar;
OriginalTypeName.Location = new Point(224, 30); OriginalTypeName.Location = new Point(224, 30);
OriginalTypeName.Name = "OriginalTypeName"; OriginalTypeName.Name = "OriginalTypeName";
OriginalTypeName.PlaceholderText = "Original Type Name"; OriginalTypeName.PlaceholderText = "Original TypeRef Name";
OriginalTypeName.Size = new Size(208, 31); OriginalTypeName.Size = new Size(208, 31);
OriginalTypeName.TabIndex = 1; OriginalTypeName.TabIndex = 1;
// //

View File

@ -122,7 +122,7 @@ internal static class GUIHelpers
if (model.SearchParams.NestedTypeCount > 0) if (model.SearchParams.NestedTypeCount > 0)
{ {
remapTreeItem.Nodes.Add(new TreeNode($"Nested Type Count: {model.SearchParams.NestedTypeCount}")); remapTreeItem.Nodes.Add(new TreeNode($"Nested TypeRef Count: {model.SearchParams.NestedTypeCount}"));
} }
remapTreeItem.Nodes.Add(originalTypeName); remapTreeItem.Nodes.Add(originalTypeName);

View File

@ -11,7 +11,7 @@ public class ReCodeItAutoMapper
private List<string> CompilerGeneratedClasses = []; private List<string> CompilerGeneratedClasses = [];
private AutoMapperSettings Settings => DataProvider.Settings.AutoMapper; private static AutoMapperSettings Settings => DataProvider.Settings.AutoMapper;
public void InitializeAutoMapping() public void InitializeAutoMapping()
{ {
@ -35,6 +35,8 @@ public class ReCodeItAutoMapper
} }
FilterTypeNames(); FilterTypeNames();
SanitizeProposedNames();
WriteChanges();
} }
private void FindCompilerGeneratedObjects(Mono.Collections.Generic.Collection<TypeDefinition> types) private void FindCompilerGeneratedObjects(Mono.Collections.Generic.Collection<TypeDefinition> types)
@ -93,9 +95,13 @@ public class ReCodeItAutoMapper
// Include fields from the current type // Include fields from the current type
foreach (var field in fields) foreach (var field in fields)
{ {
//Logger.Log($"Collecting Field: Type: {field.FieldType.Name.TrimAfterSpecialChar()} Field Name: {field.Name}"); //Logger.Log($"Collecting Field: TypeRef: {field.FieldType.Name.TrimAfterSpecialChar()} Field Name: {field.Name}");
fieldsWithTypes.Add(new MappingPair(field.FieldType, field.Name)); fieldsWithTypes.Add(new MappingPair(
field.FieldType,
field.Name,
field.FieldType.Name.Contains("Interface"),
field.FieldType.Name.Contains("Struct")));
} }
return fieldsWithTypes; return fieldsWithTypes;
@ -135,17 +141,23 @@ public class ReCodeItAutoMapper
// Include fields from the current type // Include fields from the current type
foreach (var property in properties) foreach (var property in properties)
{ {
//Logger.Log($"Collecting Property: Type: {property.PropertyType.Name.TrimAfterSpecialChar()} Field Name: {property.Name}"); //Logger.Log($"Collecting Property: TypeRef: {property.PropertyType.Name.TrimAfterSpecialChar()} Field Name: {property.Name}");
propertiesWithTypes.Add(new MappingPair(property.PropertyType, property.Name)); ;
propertiesWithTypes.Add(new MappingPair(
property.PropertyType,
property.Name,
property.PropertyType.Name.Contains("Interface"),
property.PropertyType.Name.Contains("Struct")));
} }
return propertiesWithTypes; return propertiesWithTypes;
} }
/// <summary> /// <summary>
/// Filters down match pairs to match deobfuscating names 'ClassXXXX' to field or property names /// This giant linq statement handles all of the filtering once the initial gathering of fields
/// that are not of the same value, also applies a length filter. /// and properties is complete
/// </summary> /// </summary>
private void FilterTypeNames() private void FilterTypeNames()
{ {
@ -154,10 +166,11 @@ public class ReCodeItAutoMapper
// Filter based on length, short lengths dont make good class names // Filter based on length, short lengths dont make good class names
.Where(pair => pair.Name.Length >= Settings.MinLengthToMatch) .Where(pair => pair.Name.Length >= Settings.MinLengthToMatch)
// Filter out anything that doesnt start with our specified tokens (Where pair.Type.Name // Filter out anything that doesnt start with our specified tokens (Where
// is the property Type name `Class1202` and token is start identifer we are looking for `GClass` // pair.TypeRef.Name is the property TypeRef name `Class1202` and token is start
// identifer we are looking for `GClass`
.Where(pair => Settings.TokensToMatch .Where(pair => Settings.TokensToMatch
.Any(token => pair.Type.Name.StartsWith(token))) .Any(token => pair.TypeRef.Name.StartsWith(token)))
// Filter out anything that has the same name as the type, we cant remap those // Filter out anything that has the same name as the type, we cant remap those
.Where(pair => !Settings.TokensToMatch .Where(pair => !Settings.TokensToMatch
@ -168,23 +181,78 @@ public class ReCodeItAutoMapper
.Where(pair => !Settings.PropertyFieldBlackList .Where(pair => !Settings.PropertyFieldBlackList
.Any(token => pair.Name.ToLower().StartsWith(token.ToLower()))) .Any(token => pair.Name.ToLower().StartsWith(token.ToLower())))
// Filter out backing fields
/// This is slow, but oh well
.Where(pair => !pair.Name.ToCharArray().Contains('<'))
// We only want types once, so make it unique // We only want types once, so make it unique
.GroupBy(pair => pair.Type.FullName) .GroupBy(pair => pair.TypeRef.FullName)
.Select(group => group.First()) .Select(group => group.First())
.ToList(); .GroupBy(pair => pair.Name)
.Select(group => group.First())
.ToList();
foreach (var pair in mappingPairs) MappingPairs = [.. mappingPairs];
{
Logger.Log($"Type: {pair.Type.FullName} identifier: {pair.Name}");
}
MappingPairs = mappingPairs.ToList();
Logger.Log($"Match Count {mappingPairs.Count()}");
} }
private sealed class MappingPair(TypeReference type, string name) private void SanitizeProposedNames()
{ {
public TypeReference Type { get; set; } = type; foreach (var pair in MappingPairs)
{
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)
{
Logger.Log($"INTERFACE");
pair.Name = string.Concat("I", pair.Name.AsSpan(0));
}
// If its not an interface, its a struct or class
switch (pair.IsStruct)
{
case true:
pair.Name = string.Concat(pair.Name, "Struct");
break;
case false:
pair.Name = string.Concat(pair.Name, "Class");
break;
}
Logger.Log($"------------------------------------------------------------------------");
Logger.Log($"Original Name: {pair.OriginalName} : Sanitized Name: {pair.Name}");
Logger.Log($"Matched From Name: {pair.OriginalPropOrFieldName}");
Logger.Log($"IsInterface: {pair.IsInterface}");
Logger.Log($"IsStruct: {pair.IsStruct}");
Logger.Log($"------------------------------------------------------------------------");
}
}
private void WriteChanges()
{
}
private sealed class MappingPair(TypeReference type, string name, bool isInterface = false, bool isStruct = false)
{
public TypeReference TypeRef { get; set; } = type;
public string OriginalName { get; set; } = type.FullName;
public bool IsInterface { get; set; } = isInterface;
public bool IsStruct { get; set; } = isStruct;
public string Name { get; set; } = name; public string Name { get; set; } = name;
public string OriginalPropOrFieldName { get; } = name;
} }
} }

View File

@ -79,7 +79,7 @@ public class ReCodeItRemapper
/// <summary> /// <summary>
/// Find a match result /// Find a match result
/// </summary> /// </summary>
/// <param name="type">Type to score</param> /// <param name="type">TypeRef to score</param>
/// <param name="remap">Remap to check against</param> /// <param name="remap">Remap to check against</param>
/// <param name="parentTypeName"></param> /// <param name="parentTypeName"></param>
/// <returns>EMatchResult</returns> /// <returns>EMatchResult</returns>

View File

@ -42,7 +42,7 @@ internal static class Renamer
if (field.Name == newFieldName) { continue; } if (field.Name == newFieldName) { continue; }
Logger.Log($"Renaming field: `{field.Name}` on Type `{type.Name}` to {newFieldName}", ConsoleColor.Green); Logger.Log($"Renaming field: `{field.Name}` on TypeRef `{type.Name}` to {newFieldName}", ConsoleColor.Green);
field.Name = newFieldName; field.Name = newFieldName;
@ -74,7 +74,7 @@ internal static class Renamer
{ {
var newName = propertyCount > 0 ? $"{score.ReMap.NewTypeName}_{propertyCount}" : score.ReMap.NewTypeName; var newName = propertyCount > 0 ? $"{score.ReMap.NewTypeName}_{propertyCount}" : score.ReMap.NewTypeName;
Logger.Log($"Renaming Property: `{property.Name}` on Type `{type}` to {newName}", ConsoleColor.Green); Logger.Log($"Renaming Property: `{property.Name}` on TypeRef `{type}` to {newName}", ConsoleColor.Green);
property.Name = newName; property.Name = newName;
} }
} }

View File

@ -49,7 +49,8 @@
"Conditions", "Conditions",
"Counter", "Counter",
"Instance", "Instance",
"Command" "Command",
"_template"
] ]
} }
} }