2024-06-16 01:48:48 -04:00
using Mono.Cecil ;
2024-06-23 17:17:27 -04:00
using Mono.Cecil.Rocks ;
2024-06-16 01:48:48 -04:00
using ReCodeIt.Models ;
using ReCodeIt.Utils ;
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>
2024-06-18 17:35:31 -04:00
public static void RenameAll ( ScoringModel score , bool direct = false )
2024-06-16 01:48:48 -04:00
{
2024-06-23 17:17:27 -04:00
var types = DataProvider . ModuleDefinition . GetAllTypes ( ) ;
2024-06-16 01:48:48 -04:00
// Rename all fields and properties first
2024-06-21 17:01:07 -04:00
if ( DataProvider . Settings . Remapper . MappingSettings . RenameFields )
{
RenameAllFields ( score . Definition . Name , score . ReMap . NewTypeName , types ) ;
}
if ( DataProvider . Settings . Remapper . MappingSettings . RenameProperties )
{
RenameAllProperties ( score . Definition . Name , score . ReMap . NewTypeName , types ) ;
}
2024-06-16 01:48:48 -04:00
2024-06-18 17:35:31 -04:00
if ( ! direct )
{
RenameType ( types , score ) ;
}
2024-06-16 01:48:48 -04:00
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 )
{
2024-06-18 17:35:31 -04:00
var directRename = new ScoringModel
{
Definition = type ,
ReMap = remap
} ;
RenameAll ( directRename , true ) ;
2024-06-16 01:48:48 -04:00
}
/// <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 ,
2024-06-23 17:17:27 -04:00
IEnumerable < TypeDefinition > typesToCheck ,
2024-06-16 01:48:48 -04:00
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 ,
2024-06-23 17:17:27 -04:00
IEnumerable < TypeDefinition > typesToCheck ,
2024-06-16 01:48:48 -04:00
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 ;
}
2024-06-18 17:35:31 -04:00
2024-06-23 17:17:27 -04:00
private static void RenameType ( IEnumerable < TypeDefinition > typesToCheck , ScoringModel score )
2024-06-18 17:35:31 -04:00
{
foreach ( var type in typesToCheck )
{
if ( type . HasNestedTypes )
{
RenameType ( type . NestedTypes , score ) ;
}
2024-06-22 02:28:36 -04:00
if ( score . Definition . Name is null ) { continue ; }
if ( score . ReMap . SearchParams . IsNested is true & &
type . IsNested & & type . Name = = score . Definition . Name )
{
type . Name = score . ProposedNewName ;
}
2024-06-18 17:35:31 -04:00
if ( type . FullName = = score . Definition . Name )
{
type . Name = score . ProposedNewName ;
}
}
}
2024-06-16 01:48:48 -04:00
}