Bug fixes

This commit is contained in:
Cj 2024-06-13 12:32:21 -04:00
parent 0453f3d4d6
commit ef0d01c4d2
8 changed files with 105 additions and 77 deletions

View File

@ -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<string> MatchMethods { get; set; }
public List<string> IgnoreMethods { get; set; }
public List<string> MatchFields { get; set; }
public List<string> IgnoreFields { get; set; }
public List<string> MatchProperties { get; set; }
public List<string> IgnorePropterties { get; set; }
public List<string> MatchNestedTypes { get; set; }
public List<string> IgnoreNestedTypes { get; set; }
public List<string> IncludeMethods { get; set; }
public List<string> ExcludeMethods { get; set; }
public List<string> IncludeFields { get; set; }
public List<string> ExcludeFields { get; set; }
public List<string> IncludeProperties { get; set; }
public List<string> ExcludeProperties { get; set; }
public List<string> IncludeNestedTypes { get; set; }
public List<string> ExcludeNestedTypes { get; set; }
#endregion LISTS

View File

@ -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++;

View File

@ -16,15 +16,14 @@ internal static class Fields
/// <returns></returns>
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
/// <returns></returns>
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;

View File

@ -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
/// <returns>Match if type contains any supplied methods</returns>
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
/// <returns>Match if type has no methods</returns>
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;
}
/// <summary>
/// Returns a match if the type has the provided number of methods
/// </summary>
/// <param name="type"></param>
/// <param name="parms"></param>
/// <param name="score"></param>
/// <returns></returns>
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++; }

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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<EMatchResult>
{
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();
}
}

View File

@ -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
]
}
}