From 95ac11530618a9b2b3f7f20be32984d190c309f1 Mon Sep 17 00:00:00 2001 From: Cj <161484149+CJ-SPT@users.noreply.github.com> Date: Fri, 28 Jun 2024 17:13:47 -0400 Subject: [PATCH] Add sanity checks for existing remaps, and ambiguous matches during matching --- ReCodeItCLI/Commands/ReMap.cs | 2 +- RecodeItLib/Enums/ENoMatchReason.cs | 1 + RecodeItLib/Remapper/ReCodeItRemapper.cs | 47 +++++++++++++++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/ReCodeItCLI/Commands/ReMap.cs b/ReCodeItCLI/Commands/ReMap.cs index 24252ea..6f6c711 100644 --- a/ReCodeItCLI/Commands/ReMap.cs +++ b/ReCodeItCLI/Commands/ReMap.cs @@ -35,7 +35,7 @@ public class ReMap : ICommand remapperSettings.Publicize = Publicize; var remaps = DataProvider.LoadMappingFile(MappingJsonPath); - + _remapper.InitializeRemap(remaps, AssemblyPath, Path.GetDirectoryName(AssemblyPath)); // Wait for log termination diff --git a/RecodeItLib/Enums/ENoMatchReason.cs b/RecodeItLib/Enums/ENoMatchReason.cs index 7cf7e84..361e560 100644 --- a/RecodeItLib/Enums/ENoMatchReason.cs +++ b/RecodeItLib/Enums/ENoMatchReason.cs @@ -2,6 +2,7 @@ public enum ENoMatchReason { + AmbiguousMatch, IsEnum, IsNested, IsSealed, diff --git a/RecodeItLib/Remapper/ReCodeItRemapper.cs b/RecodeItLib/Remapper/ReCodeItRemapper.cs index 6b4c466..4209913 100644 --- a/RecodeItLib/Remapper/ReCodeItRemapper.cs +++ b/RecodeItLib/Remapper/ReCodeItRemapper.cs @@ -6,6 +6,7 @@ using ReCodeIt.ReMapper.Search; using ReCodeIt.Utils; using ReCodeItLib.Remapper.Search; using System.Diagnostics; +using ReCodeIt.Enums; namespace ReCodeIt.ReMapper; @@ -41,6 +42,8 @@ public class ReCodeItRemapper private List _remaps = []; + private List _alreadyGivenNames = []; + /// /// Start the remapping process /// @@ -60,11 +63,13 @@ public class ReCodeItRemapper OutPath = outPath; + if (!Validate(_remaps)) return; + IsRunning = true; Stopwatch.Start(); var types = Module.GetTypes(); - + var tasks = new List(remapModels.Count); foreach (var remap in remapModels) { @@ -116,6 +121,29 @@ public class ReCodeItRemapper } } + private bool Validate(List remaps) + { + var duplicateGroups = remaps + .GroupBy(m => m.NewTypeName) + .Where(g => g.Count() > 1) + .ToList(); + + if (duplicateGroups.Count() > 1) + { + Logger.Log($"There were {duplicateGroups.Count()} duplicated sets of remaps.", ConsoleColor.Yellow); + + foreach (var duplicate in duplicateGroups) + { + var duplicateNewTypeName = duplicate.Key; + Logger.Log($"Ambiguous NewTypeName: {duplicateNewTypeName} found. Cancelling Remap.", ConsoleColor.Red); + return false; + } + } + + + return true; + } + /// /// First we filter our type collection based on simple search parameters (true/false/null) /// where null is a third disabled state. Then we score the types based on the search parameters @@ -189,9 +217,24 @@ public class ReCodeItRemapper var winner = remap.TypeCandidates.FirstOrDefault(); remap.TypePrimeCandidate = winner; remap.OriginalTypeName = winner.Name.String; - + if (winner is null) { return; } + if (_alreadyGivenNames.Contains(winner.FullName)) + { + Logger.Log("----------------------------------------------------------------------", ConsoleColor.Red); + Logger.Log("Ambiguous match with a previous match during matching. Skipping remap.", ConsoleColor.Red); + Logger.Log($"Ambiguous match: {winner.FullName}"); + Logger.Log("----------------------------------------------------------------------", ConsoleColor.Red); + + remap.NoMatchReasons.Add(ENoMatchReason.AmbiguousMatch); + remap.Succeeded = false; + + return; + } + + _alreadyGivenNames.Add(winner.FullName); + remap.Succeeded = true; remap.OriginalTypeName = winner.Name.String;