From ef0d01c4d2869f9d948dd8b08003a957d1b72f90 Mon Sep 17 00:00:00 2001 From: Cj <161484149+CJ-SPT@users.noreply.github.com> Date: Thu, 13 Jun 2024 12:32:21 -0400 Subject: [PATCH] Bug fixes --- AssemblyRemapper/Models/RemapModel.cs | 17 ++++--- AssemblyRemapper/Remapper/Remapper.cs | 1 - AssemblyRemapper/Remapper/Search/Fields.cs | 13 +++-- AssemblyRemapper/Remapper/Search/Methods.cs | 20 +++++--- .../Remapper/Search/NestedTypes.cs | 49 ++++++++++++++++++- .../Remapper/Search/Properties.cs | 8 +-- .../Remapper/Search/TypeDefExtensions.cs | 48 +++++------------- Templates/MappingTemplate.jsonc | 26 +++++----- 8 files changed, 105 insertions(+), 77 deletions(-) diff --git a/AssemblyRemapper/Models/RemapModel.cs b/AssemblyRemapper/Models/RemapModel.cs index 970d9de..bec158a 100644 --- a/AssemblyRemapper/Models/RemapModel.cs +++ b/AssemblyRemapper/Models/RemapModel.cs @@ -56,19 +56,20 @@ internal class SearchParams public int? MethodCount { get; set; } = null; public int? FieldCount { get; set; } = null; public int? PropertyCount { get; set; } = null; + public int? NestedTypeCount { get; set; } = null; #endregion INT_PARAMS #region LISTS - public List MatchMethods { get; set; } - public List IgnoreMethods { get; set; } - public List MatchFields { get; set; } - public List IgnoreFields { get; set; } - public List MatchProperties { get; set; } - public List IgnorePropterties { get; set; } - public List MatchNestedTypes { get; set; } - public List IgnoreNestedTypes { get; set; } + public List IncludeMethods { get; set; } + public List ExcludeMethods { get; set; } + public List IncludeFields { get; set; } + public List ExcludeFields { get; set; } + public List IncludeProperties { get; set; } + public List ExcludeProperties { get; set; } + public List IncludeNestedTypes { get; set; } + public List ExcludeNestedTypes { get; set; } #endregion LISTS diff --git a/AssemblyRemapper/Remapper/Remapper.cs b/AssemblyRemapper/Remapper/Remapper.cs index 1c8370a..e93174a 100644 --- a/AssemblyRemapper/Remapper/Remapper.cs +++ b/AssemblyRemapper/Remapper/Remapper.cs @@ -226,7 +226,6 @@ internal class Remapper Logger.Log($"Renaming {remap.NewTypeName} failed with reason {remap.FailureReason}", ConsoleColor.Red); Logger.Log("-----------------------------------------------", ConsoleColor.Red); failures++; - return; } changes++; diff --git a/AssemblyRemapper/Remapper/Search/Fields.cs b/AssemblyRemapper/Remapper/Search/Fields.cs index d15361b..75284fa 100644 --- a/AssemblyRemapper/Remapper/Search/Fields.cs +++ b/AssemblyRemapper/Remapper/Search/Fields.cs @@ -16,15 +16,14 @@ internal static class Fields /// public static EMatchResult IncludeFields(TypeDefinition type, SearchParams parms, ScoringModel score) { - if (parms.MatchFields is null || parms.MatchFields.Count == 0) return EMatchResult.Disabled; + if (parms.IncludeFields is null || parms.IncludeFields.Count == 0) return EMatchResult.Disabled; var matches = type.Fields - .Where(field => parms.MatchFields.Contains(field.Name)) - .Count(); + .Where(field => parms.IncludeFields.Contains(field.Name)); - score.Score += matches; + score.Score += matches.Count(); - return matches > 0 + return matches.Any() ? EMatchResult.Match : EMatchResult.NoMatch; } @@ -38,10 +37,10 @@ internal static class Fields /// public static EMatchResult ExcludeFields(TypeDefinition type, SearchParams parms, ScoringModel score) { - if (parms.IgnoreFields is null || parms.IgnoreFields.Count == 0) return EMatchResult.Disabled; + if (parms.ExcludeFields is null || parms.ExcludeFields.Count == 0) return EMatchResult.Disabled; var matches = type.Fields - .Where(field => parms.IgnoreFields.Contains(field.Name)) + .Where(field => parms.ExcludeFields.Contains(field.Name)) .Count(); score.Score += matches; diff --git a/AssemblyRemapper/Remapper/Search/Methods.cs b/AssemblyRemapper/Remapper/Search/Methods.cs index e36edaf..56295a7 100644 --- a/AssemblyRemapper/Remapper/Search/Methods.cs +++ b/AssemblyRemapper/Remapper/Search/Methods.cs @@ -1,7 +1,7 @@ using AssemblyRemapper.Enums; using AssemblyRemapper.Models; using Mono.Cecil; -using MoreLinq; +using Mono.Cecil.Rocks; namespace AssemblyRemapper.Remapper.Search; @@ -16,10 +16,10 @@ internal static class Methods /// Match if type contains any supplied methods public static EMatchResult IncludeMethods(TypeDefinition type, SearchParams parms, ScoringModel score) { - if (parms.MatchMethods is null || parms.MatchMethods.Count == 0) return EMatchResult.Disabled; + if (parms.IncludeMethods is null || parms.IncludeMethods.Count == 0) return EMatchResult.Disabled; var matches = type.Methods - .Where(method => parms.MatchMethods.Contains(method.Name)) + .Where(method => parms.IncludeMethods.Contains(method.Name)) .Count(); score.Score += matches; @@ -38,10 +38,10 @@ internal static class Methods /// Match if type has no methods public static EMatchResult ExcludeMethods(TypeDefinition type, SearchParams parms, ScoringModel score) { - if (parms.IgnoreMethods is null || parms.IgnoreMethods.Count == 0) return EMatchResult.Disabled; + if (parms.ExcludeMethods is null || parms.ExcludeMethods.Count == 0) return EMatchResult.Disabled; var matches = type.Methods - .Where(method => parms.IgnoreMethods.Contains(method.Name)) + .Where(method => parms.ExcludeMethods.Contains(method.Name)) .Count(); score.Score += matches; @@ -51,11 +51,19 @@ internal static class Methods : EMatchResult.Match; } + /// + /// Returns a match if the type has the provided number of methods + /// + /// + /// + /// + /// public static EMatchResult MatchMethodCount(TypeDefinition type, SearchParams parms, ScoringModel score) { if (parms.MethodCount is null) return EMatchResult.Disabled; - var match = type.Methods.Exactly((int)parms.MethodCount); + var numMethods = type.Methods.Count - type.GetConstructors().Count(); + bool match = numMethods == parms.MethodCount; if (match) { score.Score++; } diff --git a/AssemblyRemapper/Remapper/Search/NestedTypes.cs b/AssemblyRemapper/Remapper/Search/NestedTypes.cs index 18d090d..f00ec34 100644 --- a/AssemblyRemapper/Remapper/Search/NestedTypes.cs +++ b/AssemblyRemapper/Remapper/Search/NestedTypes.cs @@ -1,5 +1,52 @@ -namespace AssemblyRemapper.Remapper.Search; +using AssemblyRemapper.Enums; +using AssemblyRemapper.Models; +using Mono.Cecil; +using MoreLinq; + +namespace AssemblyRemapper.Remapper.Search; internal class NestedTypes { + public static EMatchResult IncludeNestedTypes(TypeDefinition type, SearchParams parms, ScoringModel score) + { + if (parms.IncludeNestedTypes is null || parms.IncludeNestedTypes.Count == 0) return EMatchResult.Disabled; + + var matches = type.NestedTypes + .Where(nt => parms.IncludeNestedTypes.Contains(nt.Name)) + .Count(); + + score.Score += matches; + + return matches > 0 + ? EMatchResult.Match + : EMatchResult.NoMatch; + } + + public static EMatchResult ExcludeNestedTypes(TypeDefinition type, SearchParams parms, ScoringModel score) + { + if (parms.ExcludeNestedTypes is null || parms.ExcludeNestedTypes.Count == 0) return EMatchResult.Disabled; + + var matches = type.NestedTypes + .Where(nt => parms.ExcludeNestedTypes.Contains(nt.Name)) + .Count(); + + score.Score += matches; + + return matches > 0 + ? EMatchResult.NoMatch + : EMatchResult.Match; + } + + public static EMatchResult MatchNestedTypeCount(TypeDefinition type, SearchParams parms, ScoringModel score) + { + if (parms.NestedTypeCount is null) return EMatchResult.Disabled; + + var match = type.NestedTypes.Exactly((int)parms.NestedTypeCount); + + if (match) { score.Score++; } + + return match + ? EMatchResult.Match + : EMatchResult.NoMatch; + } } \ No newline at end of file diff --git a/AssemblyRemapper/Remapper/Search/Properties.cs b/AssemblyRemapper/Remapper/Search/Properties.cs index fd8b360..2bf1092 100644 --- a/AssemblyRemapper/Remapper/Search/Properties.cs +++ b/AssemblyRemapper/Remapper/Search/Properties.cs @@ -9,10 +9,10 @@ namespace AssemblyRemapper.Remapper.Search { public static EMatchResult IncludeProperties(TypeDefinition type, SearchParams parms, ScoringModel score) { - if (parms.MatchProperties is null || parms.MatchProperties.Count == 0) return EMatchResult.Disabled; + if (parms.IncludeProperties is null || parms.IncludeProperties.Count == 0) return EMatchResult.Disabled; var matches = type.Properties - .Where(property => parms.MatchProperties.Contains(property.Name)) + .Where(property => parms.IncludeProperties.Contains(property.Name)) .Count(); score.Score += matches; @@ -24,10 +24,10 @@ namespace AssemblyRemapper.Remapper.Search public static EMatchResult ExcludeProperties(TypeDefinition type, SearchParams parms, ScoringModel score) { - if (parms.IgnorePropterties is null || parms.IgnorePropterties.Count == 0) return EMatchResult.Disabled; + if (parms.ExcludeProperties is null || parms.ExcludeProperties.Count == 0) return EMatchResult.Disabled; var matches = type.Properties - .Where(property => parms.IgnorePropterties.Contains(property.Name)) + .Where(property => parms.ExcludeProperties.Contains(property.Name)) .Count(); score.Score += matches; diff --git a/AssemblyRemapper/Remapper/Search/TypeDefExtensions.cs b/AssemblyRemapper/Remapper/Search/TypeDefExtensions.cs index 2b68142..a2e2d7d 100644 --- a/AssemblyRemapper/Remapper/Search/TypeDefExtensions.cs +++ b/AssemblyRemapper/Remapper/Search/TypeDefExtensions.cs @@ -145,17 +145,18 @@ internal static class TypeDefExtensions public static EMatchResult MatchIsPublic(this TypeDefinition type, SearchParams parms, ScoringModel score) { - if (parms.IsPublic is null) + if (parms.IsPublic == null) { return EMatchResult.Disabled; } - if (parms.IsPublic is false && type.IsNotPublic is true) + if (parms.IsPublic == false && type.IsNotPublic) { score.Score++; + return EMatchResult.Match; } - else if (parms.IsPublic is true && type.IsPublic is true) + else if ((bool)parms.IsPublic && type.IsPublic) { score.Score++; return EMatchResult.Match; @@ -239,41 +240,14 @@ internal static class TypeDefExtensions public static EMatchResult MatchNestedTypes(this TypeDefinition type, SearchParams parms, ScoringModel score) { - if (parms.MatchNestedTypes.Count is 0 && parms.IgnoreNestedTypes.Count is 0) + var matches = new List { - return EMatchResult.Disabled; - } + NestedTypes.IncludeNestedTypes(type, parms, score), + NestedTypes.ExcludeNestedTypes(type, parms, score), + NestedTypes.MatchNestedTypeCount(type, parms, score) + }; - var skippAll = parms.IgnorePropterties.Contains("*"); - - // `*` is the wildcard to ignore all fields that exist on types - if (type.HasNestedTypes is false && skippAll is true) - { - score.FailureReason = EFailureReason.HasNestedTypes; - return EMatchResult.Match; - } - - foreach (var nestedType in type.NestedTypes) - { - if (parms.IgnoreNestedTypes.Contains(nestedType.Name)) - { - // Type contains blacklisted nested type - score.FailureReason = EFailureReason.HasNestedTypes; - return EMatchResult.NoMatch; - } - } - - int matchCount = 0; - - foreach (var nestedType in type.NestedTypes) - { - if (parms.MatchNestedTypes.Contains(nestedType.Name)) - { - matchCount++; - score.Score++; - } - } - - return matchCount > 0 ? EMatchResult.Match : EMatchResult.NoMatch; + // return match if any condition matched + return matches.GetMatch(); } } \ No newline at end of file diff --git a/Templates/MappingTemplate.jsonc b/Templates/MappingTemplate.jsonc index 1f78a4a..2a23431 100644 --- a/Templates/MappingTemplate.jsonc +++ b/Templates/MappingTemplate.jsonc @@ -30,28 +30,28 @@ // Integer parameters - "ConstructorParameterCount": 0, // Match types that have a constructor parameter of this length - "MethodCount": 0, // Match types that have a method count this length - "FieldCount": 0, // Match types that have a method count this length - "PropertyCount": 0, // Match types that have a method count this length - + "ConstructorParameterCount": 0, // Match types that have a constructor parameter count of this length + "MethodCount": 0, // Match types that have this many methods + "FieldCount": 0, // Match types that have this many fields + "PropertyCount": 0, // Match types that have this many properties + "NestedTypeCount": 0, // Match types that have this many nested types // List parameters - "MatchMethods": [ // This is a list of methods we want to match + "IncludeMethods": [ // This is a list of methods we want to match ], - "IgnoreMethods": [ // This is a list of methods we want to ignore + "ExcludeMethods": [ // This is a list of methods we want to ignore ], - "MatchFields": [ // This is a list of fields we want to match + "IncludeFields": [ // This is a list of fields we want to match ], - "IgnoreFields": [ // This is a list of fields we want to ignore + "ExcludeFields": [ // This is a list of fields we want to ignore ], - "MatchProperties": [ // This is a list of properties we want to match + "IncludeProperties": [ // This is a list of properties we want to match ], - "IgnorePropterties": [ // This is a list of properties we want to ignore + "ExcludeProperties": [ // This is a list of properties we want to ignore ], - "MatchNestedTypes": [ // This is a list of nested types we want to match + "IncludeNestedTypes": [ // This is a list of nested types we want to match ], - "IgnoreNestedTypes": [ // This is a list of nested types we want to match ignore + "ExcludeNestedTypes": [ // This is a list of nested types we want to match ignore ] } }