From 4c82026a84e474a9db799035552bc1e358901044 Mon Sep 17 00:00:00 2001 From: Cj <161484149+CJ-SPT@users.noreply.github.com> Date: Wed, 1 Jan 2025 21:10:31 -0500 Subject: [PATCH] Nested type support --- RecodeItLib/Models/RemapModel.cs | 2 + RecodeItLib/Remapper/AutoMatcher.cs | 67 ++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/RecodeItLib/Models/RemapModel.cs b/RecodeItLib/Models/RemapModel.cs index 87b5d0b..028b922 100644 --- a/RecodeItLib/Models/RemapModel.cs +++ b/RecodeItLib/Models/RemapModel.cs @@ -35,6 +35,8 @@ public class RemapModel public bool UseForceRename { get; set; } + public bool? AutoGenerated { get; set; } = null; + public SearchParams SearchParams { get; set; } = new(); } diff --git a/RecodeItLib/Remapper/AutoMatcher.cs b/RecodeItLib/Remapper/AutoMatcher.cs index a4d64cc..cc5a0e5 100644 --- a/RecodeItLib/Remapper/AutoMatcher.cs +++ b/RecodeItLib/Remapper/AutoMatcher.cs @@ -72,6 +72,12 @@ public class AutoMatcher(List mappings, string mappingPath) } if (!ContainsTargetProperties(target, candidate, remapModel.SearchParams.Properties)) + { + CandidateTypes!.Remove(candidate); + continue; + } + + if (!ContainsTargetNestedTypes(target, candidate, remapModel.SearchParams.NestedTypes)) { CandidateTypes!.Remove(candidate); } @@ -89,7 +95,10 @@ public class AutoMatcher(List mappings, string mappingPath) new ReMapper().InitializeRemap(tmpList, assemblyPath, validate: true); ProcessEndQuestions(remapModel, assemblyPath); + return; } + + Logger.LogSync("Could not find a match... :(", ConsoleColor.Red); } private bool PassesGeneralChecks(TypeDef target, TypeDef candidate, GenericParams parms) @@ -113,6 +122,12 @@ public class AutoMatcher(List mappings, string mappingPath) parms.IsNested = target.IsNested; parms.IsSealed = target.IsSealed; parms.HasAttribute = target.HasCustomAttributes; + parms.IsDerived = target.BaseType != null && target.BaseType.Name != "Object"; + + if ((bool)parms.IsDerived) + { + parms.MatchBaseClass = target.BaseType?.Name.String; + } return true; } @@ -188,9 +203,6 @@ public class AutoMatcher(List mappings, string mappingPath) return true; } - // Target has no fields but type has fields - if (!target.Fields.Any() && candidate.Fields.Any()) return false; - // Target has fields but type has no fields if (target.Fields.Any() && !candidate.Fields.Any()) return false; @@ -235,9 +247,6 @@ public class AutoMatcher(List mappings, string mappingPath) return true; } - // Target has no props but type has props - if (!target.Properties.Any() && candidate.Properties.Any()) return false; - // Target has props but type has no props if (target.Properties.Any() && !candidate.Properties.Any()) return false; @@ -273,6 +282,48 @@ public class AutoMatcher(List mappings, string mappingPath) return commonProps.Any(); } + private bool ContainsTargetNestedTypes(TypeDef target, TypeDef candidate, NestedTypeParams nt) + { + // Target has no nt's but type has nt's + if (!target.NestedTypes.Any() && candidate.NestedTypes.Any()) + { + nt.NestedTypeCount = 0; + return false; + } + + // Target has nt's but type has no nt's + if (target.NestedTypes.Any() && !candidate.NestedTypes.Any()) return false; + + // Target has a different number of nt's + if (target.NestedTypes.Count != candidate.NestedTypes.Count) return false; + + var commonNts = target.NestedTypes + .Select(s => s.Name) + .Intersect(candidate.NestedTypes.Select(s => s.Name)); + + var includeNts = target.NestedTypes + .Select(s => s.Name.ToString()) + .Except(candidate.NestedTypes.Select(s => s.Name.ToString())); + + var excludeNts = candidate.NestedTypes + .Select(s => s.Name.ToString()) + .Except(target.NestedTypes.Select(s => s.Name.ToString())); + + foreach (var include in includeNts) + { + nt.IncludeNestedTypes.Add(include); + } + + foreach (var exclude in excludeNts) + { + nt.ExcludeNestedTypes.Add(exclude); + } + + nt.NestedTypeCount = target.NestedTypes.Count; + + return commonNts.Any() || !target.IsNested; + } + private void ProcessEndQuestions(RemapModel remapModel, string assemblyPath) { Thread.Sleep(1000); @@ -293,7 +344,9 @@ public class AutoMatcher(List mappings, string mappingPath) Logger.LogSync($"Ambiguous new type names found for {remapModel.NewTypeName}. Please pick a different name.", ConsoleColor.Red); return; } - + + remapModel.AutoGenerated = true; + mappings.Add(remapModel); DataProvider.UpdateMapping(mappingPath, mappings); }