diff --git a/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs b/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs index f3cbdd0..61f25d9 100644 --- a/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs +++ b/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs @@ -355,12 +355,7 @@ public class ReCodeItAutoMapper } } - Logger.Log($"-------------------------------RESULT-----------------------------------", ConsoleColor.Yellow); - Logger.Log($"Found {MappingPairs.Count()} automatic remaps", ConsoleColor.Yellow); - Logger.Log($"Renamed {TotalFieldRenameCount} fields", ConsoleColor.Yellow); - Logger.Log($"Renamed {TotalPropertyRenameCount} properties", ConsoleColor.Yellow); - Logger.Log($"Failed to rename: {FailureCount} mapping pairs", ConsoleColor.Yellow); - Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Yellow); + } /// @@ -384,6 +379,15 @@ public class ReCodeItAutoMapper private void WriteChanges() { + var path = DataProvider.WriteAssemblyDefinition(); + + Logger.Log($"-------------------------------RESULT-----------------------------------", ConsoleColor.Green); + Logger.Log($"Complete: Assembly written to `{path}`", ConsoleColor.Green); + Logger.Log($"Found {MappingPairs.Count()} automatic remaps", ConsoleColor.Green); + Logger.Log($"Renamed {TotalFieldRenameCount} fields", ConsoleColor.Green); + Logger.Log($"Renamed {TotalPropertyRenameCount} properties", ConsoleColor.Green); + Logger.Log($"Failed to rename: {FailureCount} mapping pairs", ConsoleColor.Green); + Logger.Log($"------------------------------------------------------------------------", ConsoleColor.Green); } /// diff --git a/RecodeItLib/Remapper/ReCodeItRemapper.cs b/RecodeItLib/Remapper/ReCodeItRemapper.cs index 4028cd9..1c72870 100644 --- a/RecodeItLib/Remapper/ReCodeItRemapper.cs +++ b/RecodeItLib/Remapper/ReCodeItRemapper.cs @@ -236,20 +236,12 @@ public class ReCodeItRemapper /// private void WriteAssembly() { - var filename = Path.GetFileNameWithoutExtension(DataProvider.Settings.AppSettings.AssemblyPath); - var strippedPath = Path.GetDirectoryName(filename); - - filename = $"{filename}-Remapped.dll"; - - var remappedPath = Path.Combine(strippedPath, filename); - - DataProvider.AssemblyDefinition.Write(remappedPath); - DataProvider.UpdateMapping(); + var path = DataProvider.WriteAssemblyDefinition(true); Logger.Log("-----------------------------------------------", ConsoleColor.Green); - Logger.Log($"Complete: Assembly written to `{remappedPath}`", ConsoleColor.Green); + Logger.Log($"Complete: Assembly written to `{path}`", ConsoleColor.Green); Logger.Log("Original type names updated on mapping file.", ConsoleColor.Green); - Logger.Log($"Remap took {Stopwatch.Elapsed.TotalSeconds:F0} seconds", ConsoleColor.Green); + Logger.Log($"Remap took {Stopwatch.Elapsed.TotalSeconds:F1} seconds", ConsoleColor.Green); Logger.Log("-----------------------------------------------", ConsoleColor.Green); Reset(); diff --git a/RecodeItLib/Remapper/RenameHelper.cs b/RecodeItLib/Remapper/RenameHelper.cs index 275bff0..f739345 100644 --- a/RecodeItLib/Remapper/RenameHelper.cs +++ b/RecodeItLib/Remapper/RenameHelper.cs @@ -2,11 +2,14 @@ using Mono.Collections.Generic; using ReCodeIt.Models; using ReCodeIt.Utils; +using ReCodeItLib.Utils; namespace ReCodeIt.ReMapper; internal static class RenameHelper { + private static List TokensToMatch => DataProvider.Settings.AutoMapper.TokensToMatch; + /// /// Only used by the manual remapper, should probably be removed /// @@ -52,13 +55,19 @@ internal static class RenameHelper { foreach (var type in typesToCheck) { + var fields = type.Fields + .Where(field => field.Name.IsFieldOrPropNameInList(TokensToMatch)); + + if (!fields.Any()) { continue; } + int fieldCount = 0; - foreach (var field in type.Fields) + foreach (var field in fields) { if (field.FieldType.Name == oldTypeName) { var newFieldName = GetNewFieldName(newTypeName, field.IsPrivate, fieldCount); + // Dont need to do extra work if (field.Name == newFieldName) { continue; } Logger.Log($"Renaming original field type name: `{field.FieldType.Name}` with name `{field.Name}` to `{newFieldName}`", ConsoleColor.Green); @@ -94,16 +103,23 @@ internal static class RenameHelper { foreach (var type in typesToCheck) { - int propertyCount = 0; + var properties = type.Properties + .Where(prop => prop.Name.IsFieldOrPropNameInList(TokensToMatch)); - foreach (var property in type.Properties) + if (!properties.Any()) { continue; } + + int propertyCount = 0; + foreach (var property in properties) { if (property.PropertyType.Name == oldTypeName) { - var newName = GetNewPropertyName(newTypeName, propertyCount); + var newPropertyName = GetNewPropertyName(newTypeName, propertyCount); - Logger.Log($"Renaming original property type name: `{property.PropertyType.Name}` with name `{property.Name}` to `{newName}`", ConsoleColor.Green); - property.Name = newName; + // Dont need to do extra work + if (property.Name == newPropertyName) { continue; } + + Logger.Log($"Renaming original property type name: `{property.PropertyType.Name}` with name `{property.Name}` to `{newPropertyName}`", ConsoleColor.Green); + property.Name = newPropertyName; propertyCount++; overAllCount++; } diff --git a/RecodeItLib/Utils/DataProvider.cs b/RecodeItLib/Utils/DataProvider.cs index ff2181a..5541396 100644 --- a/RecodeItLib/Utils/DataProvider.cs +++ b/RecodeItLib/Utils/DataProvider.cs @@ -174,4 +174,23 @@ public static class DataProvider Logger.Log($"Module `{fileName}` not found in assembly {fileName}"); } + + public static string WriteAssemblyDefinition(bool updateMapping = false) + { + var filename = Path.GetFileNameWithoutExtension(Settings.AppSettings.AssemblyPath); + var strippedPath = Path.GetDirectoryName(filename); + + filename = $"{filename}-Remapped.dll"; + + var remappedPath = Path.Combine(strippedPath, filename); + + AssemblyDefinition.Write(remappedPath); + + if (updateMapping) + { + UpdateMapping(); + } + + return remappedPath; + } } \ No newline at end of file diff --git a/RecodeItLib/Utils/StringExtentions.cs b/RecodeItLib/Utils/StringExtentions.cs index dc2cb4b..0fc3159 100644 --- a/RecodeItLib/Utils/StringExtentions.cs +++ b/RecodeItLib/Utils/StringExtentions.cs @@ -38,4 +38,23 @@ internal static class StringExtentions return str; } + + /// + /// Does the property or field name exist in a given list, this applies prefixes and handles + /// capitalization. + /// + /// + /// + /// True if it in the list + public static bool IsFieldOrPropNameInList(this string str, List list) + { + if (str.Trim().ElementAt(0) == '_') + { + str = str.Replace("_", ""); + } + + var result = list.Any(item => str.StartsWith(item, StringComparison.CurrentCultureIgnoreCase)); + + return result; + } } \ No newline at end of file