From 0453f3d4d6e5708cd58ecd4c876c065e35931aa8 Mon Sep 17 00:00:00 2001
From: Cj <161484149+CJ-SPT@users.noreply.github.com>
Date: Thu, 13 Jun 2024 08:18:16 -0400
Subject: [PATCH] Properties, constructors, misc
---
AssemblyRemapper/Models/AppSettingsModel.cs | 2 +-
AssemblyRemapper/Remapper/Remapper.cs | 10 ++--
.../Remapper/Search/Constructors.cs | 22 +++-----
AssemblyRemapper/Remapper/Search/Fields.cs | 10 ++--
AssemblyRemapper/Remapper/Search/Methods.cs | 10 ++--
.../Remapper/Search/Properties.cs | 49 ++++++++++++++++-
.../Remapper/Search/TypeDefExtensions.cs | 52 +++++--------------
AssemblyRemapper/Utils/ExtentionMethods.cs | 1 -
Templates/Settings.jsonc | 3 +-
9 files changed, 88 insertions(+), 71 deletions(-)
diff --git a/AssemblyRemapper/Models/AppSettingsModel.cs b/AssemblyRemapper/Models/AppSettingsModel.cs
index 68cf454..6138a51 100644
--- a/AssemblyRemapper/Models/AppSettingsModel.cs
+++ b/AssemblyRemapper/Models/AppSettingsModel.cs
@@ -7,7 +7,7 @@ internal class AppSettings
{
public bool Debug { get; set; }
public bool SilentMode { get; set; }
-
+ public int MaxMatchCount { get; set; }
public bool ScoringMode { get; set; }
public bool Publicize { get; set; }
public bool Unseal { get; set; }
diff --git a/AssemblyRemapper/Remapper/Remapper.cs b/AssemblyRemapper/Remapper/Remapper.cs
index 871de3d..1c8370a 100644
--- a/AssemblyRemapper/Remapper/Remapper.cs
+++ b/AssemblyRemapper/Remapper/Remapper.cs
@@ -245,7 +245,11 @@ internal class Remapper
{
if (scores.Count == 0) { return; }
- var highestScore = scores.OrderByDescending(score => score.Score).FirstOrDefault();
+ var filteredScores = scores
+ .OrderByDescending(score => score.Score)
+ .Take(DataProvider.AppSettings.MaxMatchCount);
+
+ var highestScore = filteredScores.FirstOrDefault();
if (highestScore is null) { return; }
@@ -256,9 +260,9 @@ internal class Remapper
if (scores.Count > 1)
{
- Logger.Log($"Warning! There were {scores.Count - 1} possible matches. Considering adding more search parameters", ConsoleColor.Yellow);
+ Logger.Log($"Warning! There were {filteredScores.Count()} possible matches. Considering adding more search parameters", ConsoleColor.Yellow);
- foreach (var score in scores.OrderByDescending(score => score.Score).Skip(1))
+ foreach (var score in filteredScores.Skip(1))
{
Logger.Log($"{score.Definition.Name} - Score [{score.Score}]", ConsoleColor.Yellow);
}
diff --git a/AssemblyRemapper/Remapper/Search/Constructors.cs b/AssemblyRemapper/Remapper/Search/Constructors.cs
index 0d3fd08..7c7e719 100644
--- a/AssemblyRemapper/Remapper/Search/Constructors.cs
+++ b/AssemblyRemapper/Remapper/Search/Constructors.cs
@@ -15,22 +15,14 @@ internal static class Constructors
/// Match if constructor parameters matches
public static EMatchResult GetTypeByParameterCount(TypeDefinition type, SearchParams parms, ScoringModel score)
{
- if (parms.ConstructorParameterCount is null)
- {
- return EMatchResult.Disabled;
- }
+ if (parms.ConstructorParameterCount is null) return EMatchResult.Disabled;
- var constructors = type.GetConstructors();
+ var match = type.GetConstructors()
+ .Where(c => c.Parameters.Count == parms.ConstructorParameterCount)
+ .Any();
- foreach (var constructor in constructors)
- {
- if (constructor.Parameters.Count == parms.ConstructorParameterCount)
- {
- score.Score++;
- return EMatchResult.Match;
- }
- }
-
- return EMatchResult.NoMatch;
+ return match
+ ? EMatchResult.Match
+ : EMatchResult.NoMatch;
}
}
\ No newline at end of file
diff --git a/AssemblyRemapper/Remapper/Search/Fields.cs b/AssemblyRemapper/Remapper/Search/Fields.cs
index 65f9f5c..d15361b 100644
--- a/AssemblyRemapper/Remapper/Search/Fields.cs
+++ b/AssemblyRemapper/Remapper/Search/Fields.cs
@@ -14,7 +14,7 @@ internal static class Fields
///
///
///
- public static EMatchResult GetTypeWithFields(TypeDefinition type, SearchParams parms, ScoringModel score)
+ public static EMatchResult IncludeFields(TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.MatchFields is null || parms.MatchFields.Count == 0) return EMatchResult.Disabled;
@@ -36,7 +36,7 @@ internal static class Fields
///
///
///
- public static EMatchResult GetTypeWithoutFields(TypeDefinition type, SearchParams parms, ScoringModel score)
+ public static EMatchResult ExcludeFields(TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.IgnoreFields is null || parms.IgnoreFields.Count == 0) return EMatchResult.Disabled;
@@ -47,8 +47,8 @@ internal static class Fields
score.Score += matches;
return matches > 0
- ? EMatchResult.Match
- : EMatchResult.NoMatch;
+ ? EMatchResult.NoMatch
+ : EMatchResult.Match;
}
///
@@ -58,7 +58,7 @@ internal static class Fields
///
///
///
- public static EMatchResult GetTypeByNumberOfFields(TypeDefinition type, SearchParams parms, ScoringModel score)
+ public static EMatchResult MatchFieldCount(TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.FieldCount is null) return EMatchResult.Disabled;
diff --git a/AssemblyRemapper/Remapper/Search/Methods.cs b/AssemblyRemapper/Remapper/Search/Methods.cs
index 757939f..e36edaf 100644
--- a/AssemblyRemapper/Remapper/Search/Methods.cs
+++ b/AssemblyRemapper/Remapper/Search/Methods.cs
@@ -14,7 +14,7 @@ internal static class Methods
///
///
/// Match if type contains any supplied methods
- public static EMatchResult GetTypeWithMethods(TypeDefinition type, SearchParams parms, ScoringModel score)
+ public static EMatchResult IncludeMethods(TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.MatchMethods is null || parms.MatchMethods.Count == 0) return EMatchResult.Disabled;
@@ -36,7 +36,7 @@ internal static class Methods
///
///
/// Match if type has no methods
- public static EMatchResult GetTypeWithoutMethods(TypeDefinition type, SearchParams parms, ScoringModel score)
+ public static EMatchResult ExcludeMethods(TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.IgnoreMethods is null || parms.IgnoreMethods.Count == 0) return EMatchResult.Disabled;
@@ -47,11 +47,11 @@ internal static class Methods
score.Score += matches;
return matches > 0
- ? EMatchResult.Match
- : EMatchResult.NoMatch;
+ ? EMatchResult.NoMatch
+ : EMatchResult.Match;
}
- public static EMatchResult GetTypeByNumberOfMethods(TypeDefinition type, SearchParams parms, ScoringModel score)
+ public static EMatchResult MatchMethodCount(TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.MethodCount is null) return EMatchResult.Disabled;
diff --git a/AssemblyRemapper/Remapper/Search/Properties.cs b/AssemblyRemapper/Remapper/Search/Properties.cs
index 7d794ce..fd8b360 100644
--- a/AssemblyRemapper/Remapper/Search/Properties.cs
+++ b/AssemblyRemapper/Remapper/Search/Properties.cs
@@ -1,6 +1,53 @@
-namespace AssemblyRemapper.Remapper.Search
+using AssemblyRemapper.Enums;
+using AssemblyRemapper.Models;
+using Mono.Cecil;
+using MoreLinq;
+
+namespace AssemblyRemapper.Remapper.Search
{
internal class Properties
{
+ public static EMatchResult IncludeProperties(TypeDefinition type, SearchParams parms, ScoringModel score)
+ {
+ if (parms.MatchProperties is null || parms.MatchProperties.Count == 0) return EMatchResult.Disabled;
+
+ var matches = type.Properties
+ .Where(property => parms.MatchProperties.Contains(property.Name))
+ .Count();
+
+ score.Score += matches;
+
+ return matches > 0
+ ? EMatchResult.Match
+ : EMatchResult.NoMatch;
+ }
+
+ public static EMatchResult ExcludeProperties(TypeDefinition type, SearchParams parms, ScoringModel score)
+ {
+ if (parms.IgnorePropterties is null || parms.IgnorePropterties.Count == 0) return EMatchResult.Disabled;
+
+ var matches = type.Properties
+ .Where(property => parms.IgnorePropterties.Contains(property.Name))
+ .Count();
+
+ score.Score += matches;
+
+ return matches > 0
+ ? EMatchResult.NoMatch
+ : EMatchResult.Match;
+ }
+
+ public static EMatchResult MatchPropertyCount(TypeDefinition type, SearchParams parms, ScoringModel score)
+ {
+ if (parms.PropertyCount is null) return EMatchResult.Disabled;
+
+ var match = type.Properties.Exactly((int)parms.PropertyCount);
+
+ if (match) { score.Score++; }
+
+ return match
+ ? EMatchResult.Match
+ : EMatchResult.NoMatch;
+ }
}
}
\ No newline at end of file
diff --git a/AssemblyRemapper/Remapper/Search/TypeDefExtensions.cs b/AssemblyRemapper/Remapper/Search/TypeDefExtensions.cs
index c9995d8..2b68142 100644
--- a/AssemblyRemapper/Remapper/Search/TypeDefExtensions.cs
+++ b/AssemblyRemapper/Remapper/Search/TypeDefExtensions.cs
@@ -202,9 +202,9 @@ internal static class TypeDefExtensions
{
var matches = new List
{
- Methods.GetTypeWithMethods(type, parms, score),
- Methods.GetTypeWithoutMethods(type, parms, score),
- Methods.GetTypeByNumberOfMethods(type, parms, score)
+ Methods.IncludeMethods(type, parms, score),
+ Methods.ExcludeMethods(type, parms, score),
+ Methods.MatchMethodCount(type, parms, score)
};
// return match if any condition matched
@@ -215,9 +215,9 @@ internal static class TypeDefExtensions
{
var matches = new List
{
- Fields.GetTypeWithFields(type, parms, score),
- Fields.GetTypeWithoutFields(type, parms, score),
- Fields.GetTypeByNumberOfFields(type, parms, score)
+ Fields.IncludeFields(type, parms, score),
+ Fields.ExcludeFields(type, parms, score),
+ Fields.MatchFieldCount(type, parms, score)
};
// return match if any condition matched
@@ -226,41 +226,15 @@ internal static class TypeDefExtensions
public static EMatchResult MatchProperties(this TypeDefinition type, SearchParams parms, ScoringModel score)
{
- if (parms.MatchProperties.Count is 0 && parms.IgnorePropterties.Count is 0)
+ var matches = new List
{
- return EMatchResult.Disabled;
- }
+ Properties.IncludeProperties(type, parms, score),
+ Properties.ExcludeProperties(type, parms, score),
+ Properties.MatchPropertyCount(type, parms, score)
+ };
- var skippAll = parms.IgnorePropterties.Contains("*");
-
- // Type has fields, we dont want any
- if (type.HasProperties is false && skippAll is true)
- {
- return EMatchResult.Match;
- }
-
- foreach (var property in type.Properties)
- {
- if (parms.IgnorePropterties.Contains(property.Name))
- {
- // Type contains blacklisted property
- score.FailureReason = EFailureReason.HasProperties;
- return EMatchResult.NoMatch;
- }
- }
-
- int matchCount = 0;
-
- foreach (var property in type.Properties)
- {
- if (parms.MatchProperties.Contains(property.Name))
- {
- matchCount++;
- score.Score++;
- }
- }
-
- return matchCount > 0 ? EMatchResult.Match : EMatchResult.NoMatch;
+ // return match if any condition matched
+ return matches.GetMatch();
}
public static EMatchResult MatchNestedTypes(this TypeDefinition type, SearchParams parms, ScoringModel score)
diff --git a/AssemblyRemapper/Utils/ExtentionMethods.cs b/AssemblyRemapper/Utils/ExtentionMethods.cs
index ca32e06..bd496aa 100644
--- a/AssemblyRemapper/Utils/ExtentionMethods.cs
+++ b/AssemblyRemapper/Utils/ExtentionMethods.cs
@@ -14,7 +14,6 @@ internal static class ExtentionMethods
{
if (outVal.Definition.Name == model.Definition.Name)
{
- Logger.Log("Skipping adding duplicate type match to list", ConsoleColor.Yellow);
return;
}
}
diff --git a/Templates/Settings.jsonc b/Templates/Settings.jsonc
index 5ccdc94..a54742d 100644
--- a/Templates/Settings.jsonc
+++ b/Templates/Settings.jsonc
@@ -1,6 +1,7 @@
{
- "Debug": true, // Enables extra debug logging, slows down the program by alot
+ "Debug": false, // Enables extra debug logging, slows down the program by alot
"SilentMode": true, // The tool will stop and prompt you to continue on every remapping if disabled
+ "MaxMatchCount": 5, // Default max matches the remapper will return
"Publicize": true, // Publicize all types, methods, and properties : NOTE: Not run until after the remap has completed
"Unseal": true, // Unseal all types : NOTE: Not run until after the remap has completed
"AssemblyPath": "./Data/Managed/Assembly-CSharp.dll", // Path to the assembly we want to remap