0
0
mirror of https://github.com/sp-tarkov/assembly-tool.git synced 2025-02-12 16:50:44 -05:00

cleanup renamer

This commit is contained in:
Cj 2025-01-11 13:43:03 -05:00
parent 371bda961c
commit af8afeab78

View File

@ -7,29 +7,24 @@ namespace ReCodeItLib.ReMapper;
internal class Renamer internal class Renamer
{ {
private static List<string> TokensToMatch => DataProvider.Settings!.TypeNamesToMatch; private static List<string> TokensToMatch => DataProvider.Settings!.TypeNamesToMatch;
/// <summary>
/// Only used by the manual remapper, should probably be removed
/// </summary>
/// <param name="module"></param>
/// <param name="remap"></param>
/// <param name="direct"></param>
public void RenameAll(IEnumerable<TypeDef> types, RemapModel remap) public void RenameAll(IEnumerable<TypeDef> types, RemapModel remap)
{ {
if (remap.TypePrimeCandidate is null) return; if (remap.TypePrimeCandidate is null) return;
// Rename all fields and properties first // Rename all fields and properties first
var typesToCheck = types as TypeDef[] ?? types.ToArray();
RenameAllFields( RenameAllFields(
remap.TypePrimeCandidate.Name.String, remap.TypePrimeCandidate.Name.String,
remap.NewTypeName, remap.NewTypeName,
types); typesToCheck);
RenameAllProperties( RenameAllProperties(
remap.TypePrimeCandidate!.Name.String, remap.TypePrimeCandidate!.Name.String,
remap.NewTypeName, remap.NewTypeName,
types); typesToCheck);
FixMethods(types, remap); FixMethods(typesToCheck, remap);
remap.TypePrimeCandidate.Name = remap.NewTypeName; remap.TypePrimeCandidate.Name = remap.NewTypeName;
} }
@ -58,12 +53,6 @@ internal class Renamer
} }
} }
/// <summary>
/// Rename all fields recursively, returns number of fields changed
/// </summary>
/// <param name="oldTypeName"></param>
/// <param name="newTypeName"></param>
/// <param name="typesToCheck"></param>
private static void RenameAllFields( private static void RenameAllFields(
string oldTypeName, string oldTypeName,
@ -74,28 +63,25 @@ internal class Renamer
foreach (var type in typeDefs) foreach (var type in typeDefs)
{ {
var fields = type.Fields var fields = type.Fields
.Where(field => field.Name.IsFieldOrPropNameInList(TokensToMatch!)); .Where(field => field.Name.IsFieldOrPropNameInList(TokensToMatch));
if (!fields.Any()) { continue; } var fieldCount = 0;
int fieldCount = 0;
foreach (var field in fields) foreach (var field in fields)
{ {
if (field.FieldType.TypeName == oldTypeName) if (field.FieldType.TypeName != oldTypeName) continue;
{
var newFieldName = GetNewFieldName(newTypeName, fieldCount); var newFieldName = GetNewFieldName(newTypeName, fieldCount);
// Dont need to do extra work // Dont need to do extra work
if (field.Name == newFieldName) { continue; } if (field.Name == newFieldName) { continue; }
var oldName = field.Name.ToString(); var oldName = field.Name.ToString();
field.Name = newFieldName; field.Name = newFieldName;
UpdateAllTypeFieldMemberRefs(typeDefs, field, oldName); UpdateAllTypeFieldMemberRefs(typeDefs, field, oldName);
fieldCount++; fieldCount++;
}
} }
} }
} }
@ -124,12 +110,6 @@ internal class Renamer
} }
} }
/// <summary>
/// Rename all properties recursively, returns number of fields changed
/// </summary>
/// <param name="oldTypeName"></param>
/// <param name="newTypeName"></param>
/// <param name="typesToCheck"></param>
private static void RenameAllProperties( private static void RenameAllProperties(
string oldTypeName, string oldTypeName,
string newTypeName, string newTypeName,
@ -138,33 +118,30 @@ internal class Renamer
foreach (var type in typesToCheck) foreach (var type in typesToCheck)
{ {
var properties = type.Properties var properties = type.Properties
.Where(prop => prop.Name.IsFieldOrPropNameInList(TokensToMatch!)); .Where(prop => prop.Name.IsFieldOrPropNameInList(TokensToMatch));
if (!properties.Any()) { continue; } var propertyCount = 0;
int propertyCount = 0;
foreach (var property in properties) foreach (var property in properties)
{ {
if (property.PropertySig.RetType.TypeName == oldTypeName) if (property.PropertySig.RetType.TypeName != oldTypeName) continue;
{
var newPropertyName = GetNewPropertyName(newTypeName, propertyCount); var newPropertyName = GetNewPropertyName(newTypeName, propertyCount);
// Dont need to do extra work // Dont need to do extra work
if (property.Name == newPropertyName) { continue; } if (property.Name == newPropertyName) continue;
property.Name = new UTF8String(newPropertyName); property.Name = newPropertyName;
propertyCount++; propertyCount++;
}
} }
} }
} }
private static string GetNewFieldName(string NewName, int fieldCount = 0) private static string GetNewFieldName(string newName, int fieldCount = 0)
{ {
string newFieldCount = fieldCount > 0 ? $"_{fieldCount}" : string.Empty; var newFieldCount = fieldCount > 0 ? $"_{fieldCount}" : string.Empty;
return $"{char.ToLower(NewName[0])}{NewName[1..]}{newFieldCount}"; return $"{char.ToLower(newName[0])}{newName[1..]}{newFieldCount}";
} }
private static string GetNewPropertyName(string newName, int propertyCount = 0) private static string GetNewPropertyName(string newName, int propertyCount = 0)