72 lines
2.1 KiB
C#
Raw Normal View History

2024-06-11 23:07:59 -04:00
using AssemblyRemapper.Models;
using AssemblyRemapper.Utils;
using Mono.Cecil;
2024-06-11 19:18:48 -04:00
using Mono.Collections.Generic;
namespace AssemblyRemapper.Reflection;
internal static class RenameService
{
public static void RenameAllFields(
2024-06-12 00:05:59 -04:00
RemapModel remap,
2024-06-11 23:07:59 -04:00
Collection<TypeDefinition> typesToCheck)
2024-06-11 19:18:48 -04:00
{
2024-06-11 23:07:59 -04:00
foreach (var type in typesToCheck)
2024-06-11 19:18:48 -04:00
{
int fieldCount = 0;
foreach (var field in type.Fields)
{
2024-06-11 23:07:59 -04:00
if (field.FieldType.ToString() == remap.NewTypeName)
2024-06-11 19:18:48 -04:00
{
Logger.Log($"Renaming Field: `{field.Name}` on Type `{type}`");
2024-06-11 23:07:59 -04:00
field.Name = GetNewFieldName(remap.NewTypeName, field.IsPrivate, fieldCount);
2024-06-11 19:18:48 -04:00
fieldCount++;
}
}
if (type.HasNestedTypes)
{
foreach (var _ in type.NestedTypes)
{
2024-06-11 23:07:59 -04:00
RenameAllFields(remap, type.NestedTypes);
2024-06-11 19:18:48 -04:00
}
}
}
}
public static void RenameAllProperties(
2024-06-12 00:05:59 -04:00
RemapModel remap,
2024-06-11 23:07:59 -04:00
Collection<TypeDefinition> typesToCheck)
2024-06-11 19:18:48 -04:00
{
2024-06-11 23:07:59 -04:00
foreach (var type in typesToCheck)
2024-06-11 19:18:48 -04:00
{
int propertyCount = 0;
foreach (var property in type.Properties)
{
2024-06-11 23:07:59 -04:00
if (property.PropertyType.ToString() == remap.NewTypeName)
2024-06-11 19:18:48 -04:00
{
Logger.Log($"Renaming Property: `{property.Name}` on Type `{type}`");
2024-06-11 23:07:59 -04:00
property.Name = propertyCount > 0 ? $"{remap.NewTypeName}_{propertyCount}" : remap.NewTypeName;
2024-06-11 19:18:48 -04:00
}
}
if (type.HasNestedTypes)
{
foreach (var _ in type.NestedTypes)
{
2024-06-11 23:07:59 -04:00
RenameAllProperties(remap, type.NestedTypes);
2024-06-11 19:18:48 -04:00
}
}
}
}
private static string GetNewFieldName(string TypeName, bool isPrivate, int fieldCount = 0)
{
var discard = isPrivate ? "_" : "";
string newFieldCount = fieldCount > 0 ? $"_{fieldCount}" : string.Empty;
return $"{discard}{char.ToLower(TypeName[0])}{TypeName[1..]}{newFieldCount}";
}
}