From 88373b084e98c5a8f68a044caabac22457e97bd6 Mon Sep 17 00:00:00 2001 From: Cj <161484149+CJ-SPT@users.noreply.github.com> Date: Sat, 15 Jun 2024 19:13:52 -0400 Subject: [PATCH] Current auto mapper work --- RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs | 63 +++++++++++++------ .../Remapper/Search/TypeDefExtensions.cs | 7 ++- Templates/Settings.jsonc | 4 +- 3 files changed, 51 insertions(+), 23 deletions(-) diff --git a/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs b/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs index 84f6a44..3975150 100644 --- a/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs +++ b/RecodeItLib/AutoMapper/ReCodeItAutoMapper.cs @@ -9,26 +9,10 @@ public class ReCodeItAutoMapper { private List MappingPairs { get; set; } = []; + private List CompilerGeneratedClasses = []; + private AutoMapperSettings Settings => DataProvider.Settings.AutoMapper; - private static readonly List SystemTypeIgnoreList = new() - { - "Boolean", - "List", - "Dictionary", - "Byte", - "Int16", - "Int36", - "Func", - "Action" - }; - - private List TokensToMatch = new() - { - "Class", - "GClass" - }; - public void InitializeAutoMapping() { Logger.ClearLog(); @@ -36,8 +20,15 @@ public class ReCodeItAutoMapper // Clear any previous pairs MappingPairs = []; + CompilerGeneratedClasses = []; - foreach (var type in DataProvider.ModuleDefinition.Types) + FindCompilerGeneratedObjects(DataProvider.ModuleDefinition.Types); + + Logger.Log($"Found {CompilerGeneratedClasses.Count} Compiler generated objects"); + + var types = DataProvider.ModuleDefinition.Types; + + foreach (var type in types) { MappingPairs.AddRange(FilterFieldNames(type)); MappingPairs.AddRange(FilterPropertyNames(type)); @@ -46,6 +37,28 @@ public class ReCodeItAutoMapper FilterTypeNames(); } + private void FindCompilerGeneratedObjects(Mono.Collections.Generic.Collection types) + { + foreach (var typeDefinition in types) + { + if (typeDefinition.IsClass || typeDefinition.IsInterface || typeDefinition.IsValueType) // Check for class or struct + { + if (typeDefinition.HasCustomAttributes && + typeDefinition.CustomAttributes.Any(attr => attr.AttributeType.FullName == "System.Runtime.CompilerServices.CompilerGeneratedAttribute")) + { + string typeName = typeDefinition.Name; + CompilerGeneratedClasses.Add(typeName); + //Logger.Log($"Compiler Generated object found: {typeName}", ConsoleColor.Yellow); + } + } + + if (typeDefinition.NestedTypes.Count > 0) + { + FindCompilerGeneratedObjects(typeDefinition.NestedTypes); + } + } + } + /// /// Pair field declaring types with their names /// @@ -55,6 +68,12 @@ public class ReCodeItAutoMapper { var fieldsWithTypes = new List(); + if (CompilerGeneratedClasses.Contains(type.Name)) + { + //Logger.Log($"Skipping over compiler generated object: {type.Name}"); + return fieldsWithTypes; + } + // Handle nested types recursively foreach (var nestedType in type.NestedTypes) { @@ -91,6 +110,12 @@ public class ReCodeItAutoMapper { var propertiesWithTypes = new List(); + if (CompilerGeneratedClasses.Contains(type.Name)) + { + //Logger.Log($"Skipping over compiler generated object: {type.Name}"); + return propertiesWithTypes; + } + // Handle nested types recursively foreach (var nestedType in type.NestedTypes) { diff --git a/RecodeItLib/Remapper/Search/TypeDefExtensions.cs b/RecodeItLib/Remapper/Search/TypeDefExtensions.cs index 820895d..3427591 100644 --- a/RecodeItLib/Remapper/Search/TypeDefExtensions.cs +++ b/RecodeItLib/Remapper/Search/TypeDefExtensions.cs @@ -1,8 +1,8 @@ -using ReCodeIt.Enums; +using Mono.Cecil; +using Mono.Cecil.Rocks; +using ReCodeIt.Enums; using ReCodeIt.Models; using ReCodeIt.Utils; -using Mono.Cecil; -using Mono.Cecil.Rocks; namespace ReCodeIt.ReMapper.Search; @@ -177,6 +177,7 @@ internal static class TypeDefExtensions { return EMatchResult.Disabled; } + var attrs = type.CustomAttributes; if (type.HasCustomAttributes == parms.HasAttribute) { diff --git a/Templates/Settings.jsonc b/Templates/Settings.jsonc index ecc4421..21dd678 100644 --- a/Templates/Settings.jsonc +++ b/Templates/Settings.jsonc @@ -37,7 +37,9 @@ ], "TokensToMatch": [ // The auto mapper will look for these tokens in class names and prioritize those "Class", - "GClass" + "GClass", + "GStruct", + "GInterface" ], "PropertyFieldBlackList": [ // Property or fields names to ignore in the automap, these are case sanitized so case does not matter "Columns",