diff --git a/RecodeItLib/Remapper/ReCodeItRemapper.cs b/RecodeItLib/Remapper/ReCodeItRemapper.cs index 257bfab..2116e19 100644 --- a/RecodeItLib/Remapper/ReCodeItRemapper.cs +++ b/RecodeItLib/Remapper/ReCodeItRemapper.cs @@ -55,7 +55,7 @@ public class ReCodeItRemapper IsRunning = true; Stopwatch.Start(); - + var types = Module.GetTypes(); if (!types.Any(t => t.Name.Contains("GClass"))) @@ -110,7 +110,9 @@ public class ReCodeItRemapper // Don't go any further during a validation if (validate) { - DisplayEndBanner(validate: true); + new Statistics(_remaps, Stopwatch, OutPath) + .DisplayStatistics(true); + return; } @@ -223,6 +225,7 @@ public class ReCodeItRemapper } types = GenericTypeFilters.FilterAbstract(types, mapping.SearchParams); + if (types.Count() == 1) return true; if (!types.Any()) { @@ -232,6 +235,7 @@ public class ReCodeItRemapper } types = GenericTypeFilters.FilterSealed(types, mapping.SearchParams); + if (types.Count() == 1) return true; if (!types.Any()) { @@ -241,6 +245,7 @@ public class ReCodeItRemapper } types = GenericTypeFilters.FilterInterface(types, mapping.SearchParams); + if (types.Count() == 1) return true; if (!types.Any()) { @@ -250,6 +255,7 @@ public class ReCodeItRemapper } types = GenericTypeFilters.FilterStruct(types, mapping.SearchParams); + if (types.Count() == 1) return true; if (!types.Any()) { @@ -259,6 +265,7 @@ public class ReCodeItRemapper } types = GenericTypeFilters.FilterEnum(types, mapping.SearchParams); + if (types.Count() == 1) return true; if (!types.Any()) { @@ -268,6 +275,7 @@ public class ReCodeItRemapper } types = GenericTypeFilters.FilterAttributes(types, mapping.SearchParams); + if (types.Count() == 1) return true; if (!types.Any()) { @@ -277,6 +285,7 @@ public class ReCodeItRemapper } types = GenericTypeFilters.FilterDerived(types, mapping.SearchParams); + if (types.Count() == 1) return true; if (!types.Any()) { @@ -607,13 +616,14 @@ public class ReCodeItRemapper throw; } - DisplayEndBanner(hollowedPath); - if (DataProvider.Settings?.Remapper?.MappingPath != string.Empty) { DataProvider.UpdateMapping(DataProvider.Settings!.Remapper!.MappingPath.Replace("mappings.", "mappings-new."), _remaps); } + new Statistics(_remaps, Stopwatch, OutPath, hollowedPath) + .DisplayStatistics(); + Stopwatch.Reset(); Module = null; @@ -637,80 +647,7 @@ public class ReCodeItRemapper } } } - - private void DisplayEndBanner(string hollowedPath = "", bool validate = false) - { - var failures = 0; - var changes = 0; - - Logger.Log("-----------------------------------------------", ConsoleColor.Green); - Logger.Log("-----------------------------------------------", ConsoleColor.Green); - - foreach (var remap in _remaps) - { - if (remap.Succeeded is false) { continue; } - - var original = remap.OriginalTypeName; - var proposed = remap.NewTypeName; - - Logger.Log($"Renamed {original} to {proposed}", ConsoleColor.Green); - - DisplayAlternativeMatches(remap); - } - - foreach (var remap in _remaps) - { - if (remap.Succeeded is false && remap.NoMatchReasons.Contains(ENoMatchReason.AmbiguousWithPreviousMatch)) - { - Logger.Log("----------------------------------------------------------------------", ConsoleColor.Red); - Logger.Log("Ambiguous match with a previous match during matching. Skipping remap.", ConsoleColor.Red); - Logger.Log($"New Type Name: {remap.NewTypeName}", ConsoleColor.Red); - Logger.Log($"{remap.AmbiguousTypeMatch} already assigned to a previous match.", ConsoleColor.Red); - Logger.Log("----------------------------------------------------------------------", ConsoleColor.Red); - } - else if (remap.Succeeded is false) - { - Logger.Log("-----------------------------------------------", ConsoleColor.Red); - Logger.Log($"Renaming {remap.NewTypeName} failed with reason(s)", ConsoleColor.Red); - - foreach (var reason in remap.NoMatchReasons) - { - Logger.Log($"Reason: {reason}", ConsoleColor.Red); - } - - Logger.Log("-----------------------------------------------", ConsoleColor.Red); - failures++; - continue; - } - - changes++; - } - - Logger.Log("-----------------------------------------------", ConsoleColor.Green); - Logger.Log("-----------------------------------------------", ConsoleColor.Green); - Logger.Log($"Result renamed {changes} Types. Failed to rename {failures} Types", ConsoleColor.Green); - - if (!validate) - { - Logger.Log($"Assembly written to `{OutPath}`", ConsoleColor.Green); - Logger.Log($"Hollowed written to `{hollowedPath}`", ConsoleColor.Green); - Logger.Log($"Remap took {Stopwatch.Elapsed.TotalSeconds:F1} seconds", ConsoleColor.Green); - } - } - - private void DisplayAlternativeMatches(RemapModel remap) - { - if (remap.TypeCandidates.Count() > 1) - { - Logger.Log($"Warning! There were {remap.TypeCandidates.Count()} possible matches for {remap.NewTypeName}. Consider adding more search parameters, Only showing the first 5.", ConsoleColor.Yellow); - - foreach (var type in remap.TypeCandidates.Skip(1).Take(5)) - { - Logger.Log($"{type.Name}", ConsoleColor.Yellow); - } - } - } - + /// /// This is used to log that all types for a given remap were filtered out. /// diff --git a/RecodeItLib/Remapper/Statistics.cs b/RecodeItLib/Remapper/Statistics.cs new file mode 100644 index 0000000..8050d3b --- /dev/null +++ b/RecodeItLib/Remapper/Statistics.cs @@ -0,0 +1,96 @@ +using System.Diagnostics; +using ReCodeIt.Enums; +using ReCodeIt.Models; +using ReCodeIt.Utils; + +namespace ReCodeItLib.Remapper; + +public class Statistics( + List remapModels, + Stopwatch stopwatch, + string outPath, + string hollowedPath = "") +{ + public void DisplayStatistics(bool validate = false) + { + DisplayAlternativeMatches(); + DisplayFailuresAndChanges(); + + if (!validate) + { + DisplayWriteAssembly(); + } + } + + private void DisplayAlternativeMatches() + { + foreach (var remap in remapModels) + { + if (remap.Succeeded is false) { continue; } + + if (remap.TypeCandidates.Count > 1) + { + DisplayAlternativeMatches(remap); + } + } + } + + private void DisplayAlternativeMatches(RemapModel remap) + { + Logger.Log($"Warning! There were {remap.TypeCandidates.Count()} possible matches for {remap.NewTypeName}. Consider adding more search parameters, Only showing the first 5.", ConsoleColor.Yellow); + + foreach (var type in remap.TypeCandidates.Skip(1).Take(5)) + { + Logger.Log($"{type.Name}", ConsoleColor.Yellow); + } + } + + private void DisplayFailuresAndChanges() + { + var failures = 0; + var changes = 0; + + foreach (var remap in remapModels) + { + if (remap.Succeeded is false && remap.NoMatchReasons.Contains(ENoMatchReason.AmbiguousWithPreviousMatch)) + { + Logger.Log("----------------------------------------------------------------------", ConsoleColor.Red); + Logger.Log("Ambiguous match with a previous match during matching. Skipping remap.", ConsoleColor.Red); + Logger.Log($"New Type Name: {remap.NewTypeName}", ConsoleColor.Red); + Logger.Log($"{remap.AmbiguousTypeMatch} already assigned to a previous match.", ConsoleColor.Red); + Logger.Log("----------------------------------------------------------------------", ConsoleColor.Red); + } + else if (remap.Succeeded is false) + { + Logger.Log("-----------------------------------------------", ConsoleColor.Red); + Logger.Log($"Renaming {remap.NewTypeName} failed with reason(s)", ConsoleColor.Red); + + foreach (var reason in remap.NoMatchReasons) + { + Logger.Log($"Reason: {reason}", ConsoleColor.Red); + } + + Logger.Log("-----------------------------------------------", ConsoleColor.Red); + failures++; + continue; + } + + changes++; + } + + var renamedColor = changes > 0 ? ConsoleColor.Green : ConsoleColor.Yellow; + + Logger.Log($"Renamed {changes} types", renamedColor); + + var failColor = failures > 0 ? ConsoleColor.Red : ConsoleColor.Green; + + Logger.Log($"Failed to rename {failures} types", failColor); + } + + private void DisplayWriteAssembly() + { + Logger.Log($"Assembly written to `{outPath}`", ConsoleColor.Green); + Logger.Log($"Hollowed written to `{hollowedPath}`", ConsoleColor.Green); + Logger.Log($"Remap took {stopwatch.Elapsed.TotalSeconds:F1} seconds", ConsoleColor.Green); + } +} \ No newline at end of file