diff --git a/AssemblyRemapper/Commands/CommandProcessor.cs b/AssemblyRemapper/Commands/CommandProcessor.cs index bc424c6..5eb5d16 100644 --- a/AssemblyRemapper/Commands/CommandProcessor.cs +++ b/AssemblyRemapper/Commands/CommandProcessor.cs @@ -25,8 +25,17 @@ namespace AssemblyRemapper.Commands { var remapper = new Remapper(); + DataProvider.LoadMappingFile(); + DataProvider.LoadAssemblyDefinition(); + remapper.InitializeRemap(); } + + if (command == "clear") + { + Console.Clear(); + ShowStartText(); + } } private void ShowStartText() @@ -34,7 +43,7 @@ namespace AssemblyRemapper.Commands Logger.Log($"-----------------------------------------------------------------", ConsoleColor.Green); Logger.Log($"Cj's Assembly Tool", ConsoleColor.Green); Logger.Log($"Version 0.1.0", ConsoleColor.Green); - Logger.Log($"Available Commands: `remap` `help`", ConsoleColor.Green); + Logger.Log($"Available Commands: `remap` `clear`", ConsoleColor.Green); Logger.Log($"-----------------------------------------------------------------", ConsoleColor.Green); } } diff --git a/AssemblyRemapper/Models/AppSettingsModel.cs b/AssemblyRemapper/Models/AppSettingsModel.cs index dae6c31..68cf454 100644 --- a/AssemblyRemapper/Models/AppSettingsModel.cs +++ b/AssemblyRemapper/Models/AppSettingsModel.cs @@ -14,51 +14,5 @@ internal class AppSettings public string AssemblyPath { get; set; } public string OutputPath { get; set; } - - public HashSet Remaps { get; set; } = []; -} - -/// -/// Object to store linq statements in inside of json to search and remap classes -/// -internal class Remap -{ - public string NewTypeName { get; set; } = string.Empty; - - public string OldTypeName { get; set; } = string.Empty; - - public bool UseForceRename { get; set; } - - public RemapSearchParams SearchParams { get; set; } = new(); -} - -/// -/// Search filters to find types and remap them -/// -internal class RemapSearchParams -{ - public bool? IsPublic { get; set; } = null; - public bool? IsAbstract { get; set; } = null; - public bool? IsInterface { get; set; } = null; - public bool? IsEnum { get; set; } = null; - public bool? IsNested { get; set; } = null; - public string? ParentName { get; set; } = null; - public bool? IsSealed { get; set; } = null; - public bool? HasAttribute { get; set; } = null; - public bool? IsDerived { get; set; } = null; - public bool? HasGenericParameters { get; set; } = null; - public HashSet MethodNamesToMatch { get; set; } = []; - public HashSet MethodNamesToIgnore { get; set; } = []; - - public HashSet FieldNamesToMatch { get; set; } = []; - public HashSet FieldNamesToIgnore { get; set; } = []; - public HashSet PropertyNamesToMatch { get; set; } = []; - public HashSet PropertyNamesToIgnore { get; set; } = []; - - public HashSet NestedTypesToMatch { get; set; } = []; - public HashSet NestedTypesToIgnore { get; set; } = []; - - public RemapSearchParams() - { - } + public string MappingPath { get; set; } } \ No newline at end of file diff --git a/AssemblyRemapper/Models/RemapModel.cs b/AssemblyRemapper/Models/RemapModel.cs new file mode 100644 index 0000000..6444815 --- /dev/null +++ b/AssemblyRemapper/Models/RemapModel.cs @@ -0,0 +1,46 @@ +namespace AssemblyRemapper.Models; + +/// +/// Object to store linq statements in inside of json to search and remap classes +/// +internal class RemapModel +{ + public string NewTypeName { get; set; } = string.Empty; + + public string OriginalTypeName { get; set; } = string.Empty; + + public bool UseForceRename { get; set; } + + public SearchParams SearchParams { get; set; } = new(); +} + +/// +/// Search filters to find types and remap them +/// +internal class SearchParams +{ + public bool? IsPublic { get; set; } = null; + public bool? IsAbstract { get; set; } = null; + public bool? IsInterface { get; set; } = null; + public bool? IsEnum { get; set; } = null; + public bool? IsNested { get; set; } = null; + public string? ParentName { get; set; } = null; + public bool? IsSealed { get; set; } = null; + public bool? HasAttribute { get; set; } = null; + public bool? IsDerived { get; set; } = null; + public bool? HasGenericParameters { get; set; } = null; + public HashSet MethodNamesToMatch { get; set; } = []; + public HashSet MethodNamesToIgnore { get; set; } = []; + + public HashSet FieldNamesToMatch { get; set; } = []; + public HashSet FieldNamesToIgnore { get; set; } = []; + public HashSet PropertyNamesToMatch { get; set; } = []; + public HashSet PropertyNamesToIgnore { get; set; } = []; + + public HashSet NestedTypesToMatch { get; set; } = []; + public HashSet NestedTypesToIgnore { get; set; } = []; + + public SearchParams() + { + } +} \ No newline at end of file diff --git a/AssemblyRemapper/Models/ScoringModel.cs b/AssemblyRemapper/Models/ScoringModel.cs index 1273c8d..7621bfd 100644 --- a/AssemblyRemapper/Models/ScoringModel.cs +++ b/AssemblyRemapper/Models/ScoringModel.cs @@ -5,11 +5,12 @@ namespace AssemblyRemapper.Models; internal class ScoringModel { + public string ProposedNewName { get; set; } + public Remap Remap { get; set; } public int Score { get; set; } = 0; - public string ProposedNewName { get; set; } - public TypeDefinition Definition { get; set; } + public RemapModel RemapModel { get; internal set; } public ScoringModel() { diff --git a/AssemblyRemapper/Reflection/Remapper.cs b/AssemblyRemapper/Reflection/Remapper.cs index 58b35b2..b6cb230 100644 --- a/AssemblyRemapper/Reflection/Remapper.cs +++ b/AssemblyRemapper/Reflection/Remapper.cs @@ -24,7 +24,7 @@ internal class Remapper private void StartRemap() { - foreach (var remap in DataProvider.AppSettings.Remaps) + foreach (var remap in DataProvider.Remaps) { Logger.Log($"Trying to remap {remap.NewTypeName}...", ConsoleColor.Gray); @@ -43,7 +43,7 @@ internal class Remapper WriteAssembly(); } - private void HandleMapping(Remap mapping) + private void HandleMapping(RemapModel mapping) { foreach (var type in DataProvider.ModuleDefinition.Types) { @@ -101,7 +101,7 @@ internal class Remapper } } - private void ScoreType(TypeDefinition type, Remap remap, string parentTypeName = "") + private void ScoreType(TypeDefinition type, RemapModel remap, string parentTypeName = "") { // Handle Direct Remaps by strict naming first bypasses everything else if (remap.UseForceRename) @@ -117,8 +117,9 @@ internal class Remapper var score = new ScoringModel { - Definition = type, ProposedNewName = remap.NewTypeName, + RemapModel = remap, + Definition = type, }; if (type.MatchIsAbstract(remap.SearchParams, score) == EMatchResult.NoMatch) @@ -126,7 +127,7 @@ internal class Remapper LogDiscard("IsAbstract", type.Name, score.ProposedNewName); return; } - /* + if (type.MatchIsEnum(remap.SearchParams, score) == EMatchResult.NoMatch) { LogDiscard("IsEnum", type.Name, score.ProposedNewName); @@ -145,7 +146,6 @@ internal class Remapper return; } - */ if (type.MatchIsDerived(remap.SearchParams, score) == EMatchResult.NoMatch) { LogDiscard("IsDerived", type.Name, score.ProposedNewName); @@ -202,9 +202,9 @@ internal class Remapper ScoringModelExtensions.AddModelToResult(score); } - private void HandleByDirectName(TypeDefinition type, Remap remap) + private void HandleByDirectName(TypeDefinition type, RemapModel remap) { - if (type.Name != remap.OldTypeName) { return; } + if (type.Name != remap.OriginalTypeName) { return; } var oldName = type.Name; type.Name = remap.NewTypeName; @@ -248,12 +248,13 @@ internal class Remapper : "Next potential"; Logger.Log("-----------------------------------------------", ConsoleColor.Green); - Logger.Log($"Found {scores.Count} results from search", ConsoleColor.Green); - Logger.Log($"{potentialText} match is `{highestScore.Definition.Name}` for `{highestScore.ProposedNewName}`", ConsoleColor.Green); + Logger.Log($"Renaming {highestScore.Definition.Name} to {highestScore.ProposedNewName}", ConsoleColor.Green); + + highestScore.Definition.Name = highestScore.ProposedNewName; foreach (var score in nextHighestScores) { - Logger.Log($"Next potential match is `{score.Definition.Name}` for `{highestScore.ProposedNewName}`", ConsoleColor.Yellow); + Logger.Log($"Alternative match `{score.Definition.Name}` for `{highestScore.ProposedNewName}`", ConsoleColor.Yellow); } if (DataProvider.AppSettings.ScoringMode) diff --git a/AssemblyRemapper/Reflection/RenameService.cs b/AssemblyRemapper/Reflection/RenameService.cs index e44afd8..e7c0df0 100644 --- a/AssemblyRemapper/Reflection/RenameService.cs +++ b/AssemblyRemapper/Reflection/RenameService.cs @@ -8,7 +8,7 @@ namespace AssemblyRemapper.Reflection; internal static class RenameService { public static void RenameAllFields( - Remap remap, + RemapModel remap, Collection typesToCheck) { foreach (var type in typesToCheck) @@ -36,7 +36,7 @@ internal static class RenameService } public static void RenameAllProperties( - Remap remap, + RemapModel remap, Collection typesToCheck) { foreach (var type in typesToCheck) diff --git a/AssemblyRemapper/Reflection/SearchProvider.cs b/AssemblyRemapper/Reflection/SearchProvider.cs index 062e0db..c0d9020 100644 --- a/AssemblyRemapper/Reflection/SearchProvider.cs +++ b/AssemblyRemapper/Reflection/SearchProvider.cs @@ -8,7 +8,7 @@ internal static class SearchProvider { public static int MatchCount { get; private set; } - public static EMatchResult MatchIsAbstract(this TypeDefinition type, RemapSearchParams parms, ScoringModel score) + public static EMatchResult MatchIsAbstract(this TypeDefinition type, SearchParams parms, ScoringModel score) { if (parms.IsAbstract is null) { @@ -30,7 +30,7 @@ internal static class SearchProvider return EMatchResult.NoMatch; } - public static EMatchResult MatchIsEnum(this TypeDefinition type, RemapSearchParams parms, ScoringModel score) + public static EMatchResult MatchIsEnum(this TypeDefinition type, SearchParams parms, ScoringModel score) { if (parms.IsEnum is null) { @@ -46,7 +46,7 @@ internal static class SearchProvider return EMatchResult.NoMatch; } - public static EMatchResult MatchIsNested(this TypeDefinition type, RemapSearchParams parms, ScoringModel score) + public static EMatchResult MatchIsNested(this TypeDefinition type, SearchParams parms, ScoringModel score) { if (parms.IsNested is null) { @@ -62,7 +62,7 @@ internal static class SearchProvider return EMatchResult.NoMatch; } - public static EMatchResult MatchIsSealed(this TypeDefinition type, RemapSearchParams parms, ScoringModel score) + public static EMatchResult MatchIsSealed(this TypeDefinition type, SearchParams parms, ScoringModel score) { if (parms.IsSealed is null) { @@ -78,7 +78,7 @@ internal static class SearchProvider return EMatchResult.NoMatch; } - public static EMatchResult MatchIsDerived(this TypeDefinition type, RemapSearchParams parms, ScoringModel score) + public static EMatchResult MatchIsDerived(this TypeDefinition type, SearchParams parms, ScoringModel score) { if (parms.IsDerived is null) { @@ -94,7 +94,7 @@ internal static class SearchProvider return EMatchResult.NoMatch; } - public static EMatchResult MatchIsInterface(this TypeDefinition type, RemapSearchParams parms, ScoringModel score) + public static EMatchResult MatchIsInterface(this TypeDefinition type, SearchParams parms, ScoringModel score) { if (parms.IsInterface is null) { @@ -110,7 +110,7 @@ internal static class SearchProvider return EMatchResult.NoMatch; } - public static EMatchResult MatchIsGeneric(this TypeDefinition type, RemapSearchParams parms, ScoringModel score) + public static EMatchResult MatchIsGeneric(this TypeDefinition type, SearchParams parms, ScoringModel score) { if (parms.HasGenericParameters is null) { @@ -126,7 +126,7 @@ internal static class SearchProvider return EMatchResult.NoMatch; } - public static EMatchResult MatchIsPublic(this TypeDefinition type, RemapSearchParams parms, ScoringModel score) + public static EMatchResult MatchIsPublic(this TypeDefinition type, SearchParams parms, ScoringModel score) { if (parms.IsPublic is null) { @@ -144,7 +144,7 @@ internal static class SearchProvider return EMatchResult.NoMatch; } - public static EMatchResult MatchHasAttribute(this TypeDefinition type, RemapSearchParams parms, ScoringModel score) + public static EMatchResult MatchHasAttribute(this TypeDefinition type, SearchParams parms, ScoringModel score) { if (parms.HasAttribute is null) { @@ -160,7 +160,7 @@ internal static class SearchProvider return EMatchResult.NoMatch; } - public static EMatchResult MatchMethods(this TypeDefinition type, RemapSearchParams parms, ScoringModel score) + public static EMatchResult MatchMethods(this TypeDefinition type, SearchParams parms, ScoringModel score) { if (parms.MethodNamesToMatch.Count == 0) { return EMatchResult.Disabled; } @@ -197,7 +197,7 @@ internal static class SearchProvider return matchCount > 0 ? EMatchResult.Match : EMatchResult.NoMatch; } - public static EMatchResult MatchFields(this TypeDefinition type, RemapSearchParams parms, ScoringModel score) + public static EMatchResult MatchFields(this TypeDefinition type, SearchParams parms, ScoringModel score) { if (parms.FieldNamesToMatch.Count == 0) { return EMatchResult.Disabled; } @@ -230,7 +230,7 @@ internal static class SearchProvider return matchCount > 0 ? EMatchResult.Match : EMatchResult.NoMatch; } - public static EMatchResult MatchProperties(this TypeDefinition type, RemapSearchParams parms, ScoringModel score) + public static EMatchResult MatchProperties(this TypeDefinition type, SearchParams parms, ScoringModel score) { if (parms.PropertyNamesToMatch.Count == 0) { return EMatchResult.Disabled; } @@ -263,7 +263,7 @@ internal static class SearchProvider return matchCount > 0 ? EMatchResult.Match : EMatchResult.NoMatch; } - public static EMatchResult MatchNestedTypes(this TypeDefinition type, RemapSearchParams parms, ScoringModel score) + public static EMatchResult MatchNestedTypes(this TypeDefinition type, SearchParams parms, ScoringModel score) { if (parms.NestedTypesToMatch.Count == 0) { return EMatchResult.Disabled; } diff --git a/AssemblyRemapper/Utils/DataProvider.cs b/AssemblyRemapper/Utils/DataProvider.cs index df14db6..b56da60 100644 --- a/AssemblyRemapper/Utils/DataProvider.cs +++ b/AssemblyRemapper/Utils/DataProvider.cs @@ -9,9 +9,10 @@ internal static class DataProvider static DataProvider() { LoadAppSettings(); - LoadAssemblyDefinition(); } + public static HashSet Remaps { get; private set; } = []; + public static Dictionary> ScoringModels { get; set; } = []; public static AppSettings AppSettings { get; private set; } @@ -39,7 +40,22 @@ internal static class DataProvider AppSettings = JsonConvert.DeserializeObject(jsonText, settings); } - private static void LoadAssemblyDefinition() + public static void LoadMappingFile() + { + if (!File.Exists(AppSettings.MappingPath)) + { + throw new InvalidOperationException($"path `{AppSettings.MappingPath}` does not exist..."); + } + + var jsonText = File.ReadAllText(AppSettings.MappingPath); + + Remaps = []; + ScoringModels = []; + + Remaps = JsonConvert.DeserializeObject>(jsonText); + } + + public static void LoadAssemblyDefinition() { DefaultAssemblyResolver resolver = new(); resolver.AddSearchDirectory(Path.GetDirectoryName(AppSettings.AssemblyPath)); // Replace with the correct path