2024-06-16 01:48:48 -04:00
using Mono.Cecil ;
using Mono.Collections.Generic ;
using ReCodeIt.Models ;
using ReCodeIt.Utils ;
2024-06-16 03:43:00 -04:00
using ReCodeItLib.Utils ;
2024-06-16 01:48:48 -04:00
namespace ReCodeIt.ReMapper ;
internal static class RenameHelper
{
2024-06-16 03:43:00 -04:00
private static List < string > TokensToMatch = > DataProvider . Settings . AutoMapper . TokensToMatch ;
2024-06-16 01:48:48 -04:00
/// <summary>
/// Only used by the manual remapper, should probably be removed
/// </summary>
/// <param name="score"></param>
public static void RenameAll ( ScoringModel score )
{
var types = DataProvider . ModuleDefinition . Types ;
// Rename all fields and properties first
RenameAllFields ( score . Definition . Name , score . ReMap . NewTypeName , types ) ;
RenameAllProperties ( score . Definition . Name , score . ReMap . NewTypeName , types ) ;
score . Definition . Name = score . ProposedNewName ;
Logger . Log ( $"{score.Definition.Name} Renamed." , ConsoleColor . Green ) ;
}
/// <summary>
/// Only used by the manual remapper, should probably be removed
/// </summary>
/// <param name="score"></param>
public static void RenameAllDirect ( RemapModel remap , TypeDefinition type )
{
var directRename = new ScoringModel ( ) ;
directRename . Definition = type ;
directRename . ReMap = remap ;
RenameAll ( directRename ) ;
}
/// <summary>
/// Rename all fields recursively, returns number of fields changed
/// </summary>
/// <param name="oldTypeName"></param>
/// <param name="newTypeName"></param>
/// <param name="typesToCheck"></param>
/// <returns></returns>
public static int RenameAllFields (
string oldTypeName ,
string newTypeName ,
Collection < TypeDefinition > typesToCheck ,
int overAllCount = 0 )
{
foreach ( var type in typesToCheck )
{
2024-06-16 03:43:00 -04:00
var fields = type . Fields
. Where ( field = > field . Name . IsFieldOrPropNameInList ( TokensToMatch ) ) ;
if ( ! fields . Any ( ) ) { continue ; }
2024-06-16 01:48:48 -04:00
int fieldCount = 0 ;
2024-06-16 03:43:00 -04:00
foreach ( var field in fields )
2024-06-16 01:48:48 -04:00
{
if ( field . FieldType . Name = = oldTypeName )
{
var newFieldName = GetNewFieldName ( newTypeName , field . IsPrivate , fieldCount ) ;
2024-06-16 03:43:00 -04:00
// Dont need to do extra work
2024-06-16 01:48:48 -04:00
if ( field . Name = = newFieldName ) { continue ; }
Logger . Log ( $"Renaming original field type name: `{field.FieldType.Name}` with name `{field.Name}` to `{newFieldName}`" , ConsoleColor . Green ) ;
field . Name = newFieldName ;
fieldCount + + ;
overAllCount + + ;
}
}
if ( type . HasNestedTypes )
{
RenameAllFields ( oldTypeName , newTypeName , type . NestedTypes , overAllCount ) ;
}
}
return overAllCount ;
}
/// <summary>
/// Rename all properties recursively, returns number of fields changed
/// </summary>
/// <param name="oldTypeName"></param>
/// <param name="newTypeName"></param>
/// <param name="typesToCheck"></param>
/// <returns></returns>
public static int RenameAllProperties (
string oldTypeName ,
string newTypeName ,
Collection < TypeDefinition > typesToCheck ,
int overAllCount = 0 )
{
foreach ( var type in typesToCheck )
{
2024-06-16 03:43:00 -04:00
var properties = type . Properties
. Where ( prop = > prop . Name . IsFieldOrPropNameInList ( TokensToMatch ) ) ;
2024-06-16 01:48:48 -04:00
2024-06-16 03:43:00 -04:00
if ( ! properties . Any ( ) ) { continue ; }
int propertyCount = 0 ;
foreach ( var property in properties )
2024-06-16 01:48:48 -04:00
{
if ( property . PropertyType . Name = = oldTypeName )
{
2024-06-16 03:43:00 -04:00
var newPropertyName = GetNewPropertyName ( newTypeName , propertyCount ) ;
// Dont need to do extra work
if ( property . Name = = newPropertyName ) { continue ; }
2024-06-16 01:48:48 -04:00
2024-06-16 03:43:00 -04:00
Logger . Log ( $"Renaming original property type name: `{property.PropertyType.Name}` with name `{property.Name}` to `{newPropertyName}`" , ConsoleColor . Green ) ;
property . Name = newPropertyName ;
2024-06-16 01:48:48 -04:00
propertyCount + + ;
overAllCount + + ;
}
}
if ( type . HasNestedTypes )
{
RenameAllProperties ( oldTypeName , newTypeName , type . NestedTypes , overAllCount ) ;
}
}
return overAllCount ;
}
public static string GetNewFieldName ( string NewName , bool isPrivate , int fieldCount = 0 )
{
var discard = isPrivate ? "_" : "" ;
string newFieldCount = fieldCount > 0 ? $"_{fieldCount}" : string . Empty ;
return $"{discard}{char.ToLower(NewName[0])}{NewName[1..]}{newFieldCount}" ;
}
public static string GetNewPropertyName ( string newName , int propertyCount = 0 )
{
return propertyCount > 0 ? $"{newName}_{propertyCount}" : newName ;
}
}