This commit is contained in:
Cj 2024-06-12 18:59:08 -04:00
parent 7c27db9694
commit 56d0d3233d
7 changed files with 165 additions and 96 deletions

View File

@ -0,0 +1,20 @@
namespace AssemblyRemapper.Enums;
internal enum EFailureReason
{
None,
IsAbstract,
IsEnum,
IsNested,
IsSealed,
IsDerived,
IsInterface,
IsPublic,
HasGenericParameters,
HasAttribute,
IsAttribute,
HasMethods,
HasFields,
HasProperties,
HasNestedTypes,
}

View File

@ -29,16 +29,16 @@ internal class SearchParams
public bool? HasAttribute { get; set; } = null; public bool? HasAttribute { get; set; } = null;
public bool? IsDerived { get; set; } = null; public bool? IsDerived { get; set; } = null;
public bool? HasGenericParameters { get; set; } = null; public bool? HasGenericParameters { get; set; } = null;
public HashSet<string> MethodNamesToMatch { get; set; } = []; public List<string> MethodNamesToMatch { get; set; } = [];
public HashSet<string> MethodNamesToIgnore { get; set; } = []; public List<string> MethodNamesToIgnore { get; set; } = [];
public HashSet<string> FieldNamesToMatch { get; set; } = []; public List<string> FieldNamesToMatch { get; set; } = [];
public HashSet<string> FieldNamesToIgnore { get; set; } = []; public List<string> FieldNamesToIgnore { get; set; } = [];
public HashSet<string> PropertyNamesToMatch { get; set; } = []; public List<string> PropertyNamesToMatch { get; set; } = [];
public HashSet<string> PropertyNamesToIgnore { get; set; } = []; public List<string> PropertyNamesToIgnore { get; set; } = [];
public HashSet<string> NestedTypesToMatch { get; set; } = []; public List<string> NestedTypesToMatch { get; set; } = [];
public HashSet<string> NestedTypesToIgnore { get; set; } = []; public List<string> NestedTypesToIgnore { get; set; } = [];
public SearchParams() public SearchParams()
{ {

View File

@ -1,4 +1,5 @@
using AssemblyRemapper.Utils; using AssemblyRemapper.Enums;
using AssemblyRemapper.Utils;
using Mono.Cecil; using Mono.Cecil;
namespace AssemblyRemapper.Models; namespace AssemblyRemapper.Models;
@ -10,6 +11,8 @@ internal class ScoringModel
public TypeDefinition Definition { get; set; } public TypeDefinition Definition { get; set; }
public RemapModel RemapModel { get; internal set; } public RemapModel RemapModel { get; internal set; }
public EFailureReason FailureReason { get; set; } = EFailureReason.None;
public ScoringModel() public ScoringModel()
{ {
} }
@ -64,9 +67,9 @@ internal static class ScoringModelExtensions
if (value == null) continue; if (value == null) continue;
if (value is HashSet<string> hashset) if (value is List<string> list)
{ {
maxScore += hashset.Count * 2; maxScore += list.Count;
} }
else else
{ {

View File

@ -13,7 +13,7 @@ internal class Remapper
foreach (var remap in DataProvider.Remaps) foreach (var remap in DataProvider.Remaps)
{ {
Logger.Log($"Trying to remap {remap.NewTypeName}...", ConsoleColor.Gray); Logger.Log($"Finding best match for {remap.NewTypeName}...", ConsoleColor.Gray);
HandleMapping(remap); HandleMapping(remap);
} }
@ -44,7 +44,12 @@ internal class Remapper
{ {
foreach (var type in DataProvider.ModuleDefinition.Types) foreach (var type in DataProvider.ModuleDefinition.Types)
{ {
ScoreType(type, mapping); var result = ScoreType(type, mapping);
if (result is not EFailureReason.None)
{
//Logger.LogDebug($"Remap [{type.Name} : {mapping.NewTypeName}] failed with reason {result}", silent: true);
}
} }
} }
@ -98,13 +103,13 @@ internal class Remapper
} }
} }
private void ScoreType(TypeDefinition type, RemapModel remap, string parentTypeName = "") private EFailureReason ScoreType(TypeDefinition type, RemapModel remap, string parentTypeName = "")
{ {
// Handle Direct Remaps by strict naming first bypasses everything else // Handle Direct Remaps by strict naming first bypasses everything else
if (remap.UseForceRename) if (remap.UseForceRename)
{ {
HandleByDirectName(type, remap); HandleByDirectName(type, remap);
return; return EFailureReason.None;
} }
foreach (var nestedType in type.NestedTypes) foreach (var nestedType in type.NestedTypes)
@ -124,82 +129,72 @@ internal class Remapper
if (type.MatchIsAbstract(remap.SearchParams, score) == EMatchResult.NoMatch) if (type.MatchIsAbstract(remap.SearchParams, score) == EMatchResult.NoMatch)
{ {
LogDiscard("IsAbstract", type.Name, score.ProposedNewName); return EFailureReason.IsAbstract;
return;
} }
if (type.MatchIsEnum(remap.SearchParams, score) == EMatchResult.NoMatch) if (type.MatchIsEnum(remap.SearchParams, score) == EMatchResult.NoMatch)
{ {
LogDiscard("IsEnum", type.Name, score.ProposedNewName); return EFailureReason.IsEnum;
return;
} }
if (type.MatchIsNested(remap.SearchParams, score) == EMatchResult.NoMatch) if (type.MatchIsNested(remap.SearchParams, score) == EMatchResult.NoMatch)
{ {
LogDiscard("IsNested", type.Name, score.ProposedNewName); return EFailureReason.IsNested;
return;
} }
if (type.MatchIsSealed(remap.SearchParams, score) == EMatchResult.NoMatch) if (type.MatchIsSealed(remap.SearchParams, score) == EMatchResult.NoMatch)
{ {
LogDiscard("IsSealed", type.Name, score.ProposedNewName); return EFailureReason.IsSealed;
return;
} }
if (type.MatchIsDerived(remap.SearchParams, score) == EMatchResult.NoMatch) if (type.MatchIsDerived(remap.SearchParams, score) == EMatchResult.NoMatch)
{ {
LogDiscard("IsDerived", type.Name, score.ProposedNewName); return EFailureReason.IsDerived;
return;
} }
if (type.MatchIsInterface(remap.SearchParams, score) == EMatchResult.NoMatch) if (type.MatchIsInterface(remap.SearchParams, score) == EMatchResult.NoMatch)
{ {
LogDiscard("IsInterface", type.Name, score.ProposedNewName); return EFailureReason.IsInterface;
return;
} }
if (type.MatchIsGeneric(remap.SearchParams, score) == EMatchResult.NoMatch) if (type.MatchHasGenericParameters(remap.SearchParams, score) == EMatchResult.NoMatch)
{ {
return; return EFailureReason.HasGenericParameters;
} }
if (type.MatchIsPublic(remap.SearchParams, score) == EMatchResult.NoMatch) if (type.MatchIsPublic(remap.SearchParams, score) == EMatchResult.NoMatch)
{ {
LogDiscard("IsPublic", type.Name, score.ProposedNewName); return EFailureReason.IsPublic;
return;
} }
if (type.MatchHasAttribute(remap.SearchParams, score) == EMatchResult.NoMatch) if (type.MatchHasAttribute(remap.SearchParams, score) == EMatchResult.NoMatch)
{ {
LogDiscard("HasAttribute", type.Name, score.ProposedNewName); return EFailureReason.HasAttribute;
return;
} }
if (type.MatchMethods(remap.SearchParams, score) == EMatchResult.NoMatch) if (type.MatchMethods(remap.SearchParams, score) == EMatchResult.NoMatch)
{ {
LogDiscard("Methods", type.Name, score.ProposedNewName); return EFailureReason.HasMethods;
return;
} }
if (type.MatchFields(remap.SearchParams, score) == EMatchResult.NoMatch) if (type.MatchFields(remap.SearchParams, score) == EMatchResult.NoMatch)
{ {
LogDiscard("Fields", type.Name, score.ProposedNewName); return EFailureReason.HasFields;
return;
} }
if (type.MatchProperties(remap.SearchParams, score) == EMatchResult.NoMatch) if (type.MatchProperties(remap.SearchParams, score) == EMatchResult.NoMatch)
{ {
LogDiscard("Properties", type.Name, score.ProposedNewName); return EFailureReason.HasProperties;
return;
} }
if (type.MatchNestedTypes(remap.SearchParams, score) == EMatchResult.NoMatch) if (type.MatchNestedTypes(remap.SearchParams, score) == EMatchResult.NoMatch)
{ {
LogDiscard("NestedTypes", type.Name, score.ProposedNewName); return EFailureReason.HasNestedTypes;
return;
} }
ScoringModelExtensions.AddModelToResult(score); ScoringModelExtensions.AddModelToResult(score);
return EFailureReason.None;
} }
private void HandleByDirectName(TypeDefinition type, RemapModel remap) private void HandleByDirectName(TypeDefinition type, RemapModel remap)
@ -217,14 +212,6 @@ internal class Remapper
Logger.Log("-----------------------------------------------", ConsoleColor.Green); Logger.Log("-----------------------------------------------", ConsoleColor.Green);
} }
private void LogDiscard(string action, string type, string search)
{
if (DataProvider.AppSettings.Debug)
{
Logger.Log($"[{action}] Discarding type [{type}] for search [{search}]", ConsoleColor.Red);
}
}
private void ChooseBestMatches() private void ChooseBestMatches()
{ {
foreach (var score in DataProvider.ScoringModels) foreach (var score in DataProvider.ScoringModels)

View File

@ -22,10 +22,11 @@ internal static class SearchProvider
if (type.IsAbstract == parms.IsAbstract) if (type.IsAbstract == parms.IsAbstract)
{ {
score.Score += 1; score.Score++;
return EMatchResult.Match; return EMatchResult.Match;
} }
score.FailureReason = EFailureReason.IsAbstract;
return EMatchResult.NoMatch; return EMatchResult.NoMatch;
} }
@ -38,10 +39,11 @@ internal static class SearchProvider
if (type.IsEnum == parms.IsEnum) if (type.IsEnum == parms.IsEnum)
{ {
score.Score += 1; score.Score++;
return EMatchResult.Match; return EMatchResult.Match;
} }
score.FailureReason = EFailureReason.IsEnum;
return EMatchResult.NoMatch; return EMatchResult.NoMatch;
} }
@ -54,10 +56,11 @@ internal static class SearchProvider
if (type.IsNested == parms.IsNested) if (type.IsNested == parms.IsNested)
{ {
score.Score += 1; score.Score++;
return EMatchResult.Match; return EMatchResult.Match;
} }
score.FailureReason = EFailureReason.IsNested;
return EMatchResult.NoMatch; return EMatchResult.NoMatch;
} }
@ -70,10 +73,11 @@ internal static class SearchProvider
if (type.IsSealed == parms.IsSealed) if (type.IsSealed == parms.IsSealed)
{ {
score.Score += 1; score.Score++;
return EMatchResult.Match; return EMatchResult.Match;
} }
score.FailureReason = EFailureReason.IsSealed;
return EMatchResult.NoMatch; return EMatchResult.NoMatch;
} }
@ -86,10 +90,11 @@ internal static class SearchProvider
if (type.BaseType != null && (bool)parms.IsDerived) if (type.BaseType != null && (bool)parms.IsDerived)
{ {
score.Score += 1; score.Score++;
return EMatchResult.Match; return EMatchResult.Match;
} }
score.FailureReason = EFailureReason.IsDerived;
return EMatchResult.NoMatch; return EMatchResult.NoMatch;
} }
@ -102,14 +107,15 @@ internal static class SearchProvider
if (type.IsInterface == parms.IsInterface) if (type.IsInterface == parms.IsInterface)
{ {
score.Score += 1; score.Score++;
return EMatchResult.Match; return EMatchResult.Match;
} }
score.FailureReason = EFailureReason.IsInterface;
return EMatchResult.NoMatch; return EMatchResult.NoMatch;
} }
public static EMatchResult MatchIsGeneric(this TypeDefinition type, SearchParams parms, ScoringModel score) public static EMatchResult MatchHasGenericParameters(this TypeDefinition type, SearchParams parms, ScoringModel score)
{ {
if (parms.HasGenericParameters is null) if (parms.HasGenericParameters is null)
{ {
@ -118,10 +124,11 @@ internal static class SearchProvider
if (type.HasGenericParameters == parms.HasGenericParameters) if (type.HasGenericParameters == parms.HasGenericParameters)
{ {
score.Score += 1; score.Score++;
return EMatchResult.Match; return EMatchResult.Match;
} }
score.FailureReason = EFailureReason.HasGenericParameters;
return EMatchResult.NoMatch; return EMatchResult.NoMatch;
} }
@ -136,10 +143,11 @@ internal static class SearchProvider
if (boolToCheck == !parms.IsPublic) if (boolToCheck == !parms.IsPublic)
{ {
score.Score += 1; score.Score++;
return EMatchResult.Match; return EMatchResult.Match;
} }
score.FailureReason = EFailureReason.IsPublic;
return EMatchResult.NoMatch; return EMatchResult.NoMatch;
} }
@ -152,43 +160,62 @@ internal static class SearchProvider
if (type.HasCustomAttributes == parms.HasAttribute) if (type.HasCustomAttributes == parms.HasAttribute)
{ {
score.Score += 1; score.Score++;
return EMatchResult.Match; return EMatchResult.Match;
} }
score.FailureReason = EFailureReason.HasAttribute;
return EMatchResult.NoMatch; return EMatchResult.NoMatch;
} }
public static EMatchResult MatchMethods(this TypeDefinition type, SearchParams parms, ScoringModel score) public static EMatchResult MatchMethods(this TypeDefinition type, SearchParams parms, ScoringModel score)
{ {
if (parms.MethodNamesToMatch.Count == 0) { return EMatchResult.Disabled; } // We're not searching for methods and this type contains methods
if (parms.MethodNamesToMatch.Count == 0 && parms.MethodNamesToIgnore.Count == 0)
{
return type.HasMethods
? EMatchResult.NoMatch
: EMatchResult.Match;
}
if (type.HasMethods) var skippAll = parms.MethodNamesToIgnore.Contains("*");
// The type has methods and we dont want any
if (type.HasMethods is true && skippAll is true)
{ {
// `*` is the wildcard to ignore all methods that exist on types foreach (var method in type.Methods)
if (parms.MethodNamesToIgnore.Contains("*"))
{ {
if (method.Name == ".ctor")
{
continue;
}
score.Score--;
return EMatchResult.NoMatch; return EMatchResult.NoMatch;
} }
score.Score++;
return EMatchResult.Match;
} }
var matchCount = 0; var matchCount = 0;
foreach (var method in type.Methods) foreach (var method in type.Methods)
{ {
// Type contains a method we dont want
if (parms.MethodNamesToIgnore.Contains(method.Name)) if (parms.MethodNamesToIgnore.Contains(method.Name))
{ {
// Type contains blacklisted method score.FailureReason = EFailureReason.HasMethods;
return EMatchResult.NoMatch; return EMatchResult.NoMatch;
} }
foreach (var name in parms.MethodNamesToMatch) foreach (var name in parms.MethodNamesToMatch)
{ {
// Method name match
if (method.Name == name) if (method.Name == name)
{ {
matchCount += 1; matchCount += 1;
score.Score += 2; score.Score++;
continue;
} }
} }
} }
@ -198,15 +225,15 @@ internal static class SearchProvider
public static EMatchResult MatchFields(this TypeDefinition type, SearchParams parms, ScoringModel score) public static EMatchResult MatchFields(this TypeDefinition type, SearchParams parms, ScoringModel score)
{ {
if (parms.FieldNamesToMatch.Count == 0) { return EMatchResult.Disabled; } if (parms.FieldNamesToMatch.Count == 0 && parms.FieldNamesToIgnore.Count == 0)
if (type.HasFields)
{ {
// `*` is the wildcard to ignore all fields that exist on types return EMatchResult.Disabled;
if (parms.FieldNamesToIgnore.Contains("*"))
{
return EMatchResult.NoMatch;
} }
// `*` is the wildcard to ignore all fields that exist on types
if (!type.HasFields && parms.FieldNamesToIgnore.Contains("*"))
{
return EMatchResult.Match;
} }
int matchCount = 0; int matchCount = 0;
@ -216,13 +243,14 @@ internal static class SearchProvider
if (parms.FieldNamesToIgnore.Contains(field.Name)) if (parms.FieldNamesToIgnore.Contains(field.Name))
{ {
// Type contains blacklisted field // Type contains blacklisted field
score.FailureReason = EFailureReason.HasFields;
return EMatchResult.NoMatch; return EMatchResult.NoMatch;
} }
if (parms.FieldNamesToMatch.Contains(field.Name)) if (parms.FieldNamesToMatch.Contains(field.Name))
{ {
matchCount++; matchCount++;
score.Score += 2; score.Score++;
} }
} }
@ -231,15 +259,16 @@ internal static class SearchProvider
public static EMatchResult MatchProperties(this TypeDefinition type, SearchParams parms, ScoringModel score) public static EMatchResult MatchProperties(this TypeDefinition type, SearchParams parms, ScoringModel score)
{ {
if (parms.PropertyNamesToMatch.Count == 0) { return EMatchResult.Disabled; } if (parms.PropertyNamesToMatch.Count == 0 && parms.PropertyNamesToIgnore.Count == 0)
if (type.HasProperties)
{ {
// `*` is the wildcard to ignore all fields that exist on types return EMatchResult.Disabled;
if (parms.PropertyNamesToIgnore.Contains("*"))
{
return EMatchResult.NoMatch;
} }
// `*` is the wildcard to ignore all fields that exist on types
if (!type.HasProperties && parms.PropertyNamesToIgnore.Contains("*"))
{
score.Score++;
return EMatchResult.Match;
} }
int matchCount = 0; int matchCount = 0;
@ -249,13 +278,14 @@ internal static class SearchProvider
if (parms.PropertyNamesToIgnore.Contains(property.Name)) if (parms.PropertyNamesToIgnore.Contains(property.Name))
{ {
// Type contains blacklisted property // Type contains blacklisted property
score.FailureReason = EFailureReason.HasProperties;
return EMatchResult.NoMatch; return EMatchResult.NoMatch;
} }
if (parms.PropertyNamesToMatch.Contains(property.Name)) if (parms.PropertyNamesToMatch.Contains(property.Name))
{ {
matchCount++; matchCount++;
score.Score += 2; score.Score++;
} }
} }
@ -264,15 +294,16 @@ internal static class SearchProvider
public static EMatchResult MatchNestedTypes(this TypeDefinition type, SearchParams parms, ScoringModel score) public static EMatchResult MatchNestedTypes(this TypeDefinition type, SearchParams parms, ScoringModel score)
{ {
if (parms.NestedTypesToMatch.Count == 0) { return EMatchResult.Disabled; } if (parms.NestedTypesToMatch.Count == 0 && parms.NestedTypesToIgnore.Count == 0)
if (type.HasNestedTypes)
{ {
// `*` is the wildcard to ignore all fields that exist on types return EMatchResult.Disabled;
if (parms.NestedTypesToIgnore.Contains("*"))
{
return EMatchResult.NoMatch;
} }
// `*` is the wildcard to ignore all fields that exist on types
if (type.HasNestedTypes && parms.NestedTypesToIgnore.Contains("*"))
{
score.FailureReason = EFailureReason.HasNestedTypes;
return EMatchResult.NoMatch;
} }
int matchCount = 0; int matchCount = 0;
@ -282,13 +313,14 @@ internal static class SearchProvider
if (parms.NestedTypesToIgnore.Contains(nestedType.Name)) if (parms.NestedTypesToIgnore.Contains(nestedType.Name))
{ {
// Type contains blacklisted nested type // Type contains blacklisted nested type
score.FailureReason = EFailureReason.HasNestedTypes;
return EMatchResult.NoMatch; return EMatchResult.NoMatch;
} }
if (parms.NestedTypesToMatch.Contains(nestedType.Name)) if (parms.NestedTypesToMatch.Contains(nestedType.Name))
{ {
matchCount++; matchCount++;
score.Score += 2; score.Score++;
} }
} }

View File

@ -13,12 +13,39 @@ internal static class Logger
private static string _logPath = Path.Combine(AppContext.BaseDirectory, "Data", "Log.log"); private static string _logPath = Path.Combine(AppContext.BaseDirectory, "Data", "Log.log");
public static void Log(string message, ConsoleColor color = ConsoleColor.Gray) public static void Log(string message, ConsoleColor color = ConsoleColor.Gray, bool silent = false)
{
if (silent)
{
WriteToDisk(message);
return;
}
Console.ForegroundColor = color;
Console.WriteLine(message);
Console.ResetColor();
WriteToDisk(message);
}
public static void LogDebug(string message, ConsoleColor color = ConsoleColor.Gray, bool silent = false)
{
if (silent)
{
WriteToDisk(message);
return;
}
if (DataProvider.AppSettings.Debug)
{ {
Console.ForegroundColor = color; Console.ForegroundColor = color;
Console.WriteLine(message); Console.WriteLine(message);
Console.ResetColor(); Console.ResetColor();
WriteToDisk(message);
}
}
private static void WriteToDisk(string message)
{
try try
{ {
using (StreamWriter sw = File.AppendText(_logPath)) using (StreamWriter sw = File.AppendText(_logPath))

View File

@ -1,5 +1,5 @@
{ {
"Debug": false, // Enables extra debug logging, slows down the program by alot "Debug": true, // 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 "SilentMode": true, // The tool will stop and prompt you to continue on every remapping if disabled
"Publicize": true, // Publicize all types, methods, and properties : NOTE: Not run until after the remap has completed "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 "Unseal": true, // Unseal all types : NOTE: Not run until after the remap has completed