This commit is contained in:
Cj 2024-06-12 00:05:59 -04:00
parent dfd50b3b75
commit dcae6663b4
8 changed files with 105 additions and 78 deletions

View File

@ -25,8 +25,17 @@ namespace AssemblyRemapper.Commands
{
var remapper = new Remapper();
DataProvider.LoadMappingFile();
DataProvider.LoadAssemblyDefinition();
remapper.InitializeRemap();
}
if (command == "clear")
{
Console.Clear();
ShowStartText();
}
}
private void ShowStartText()
@ -34,7 +43,7 @@ namespace AssemblyRemapper.Commands
Logger.Log($"-----------------------------------------------------------------", ConsoleColor.Green);
Logger.Log($"Cj's Assembly Tool", ConsoleColor.Green);
Logger.Log($"Version 0.1.0", ConsoleColor.Green);
Logger.Log($"Available Commands: `remap` `help`", ConsoleColor.Green);
Logger.Log($"Available Commands: `remap` `clear`", ConsoleColor.Green);
Logger.Log($"-----------------------------------------------------------------", ConsoleColor.Green);
}
}

View File

@ -14,51 +14,5 @@ internal class AppSettings
public string AssemblyPath { get; set; }
public string OutputPath { get; set; }
public HashSet<Remap> Remaps { get; set; } = [];
}
/// <summary>
/// Object to store linq statements in inside of json to search and remap classes
/// </summary>
internal class Remap
{
public string NewTypeName { get; set; } = string.Empty;
public string OldTypeName { get; set; } = string.Empty;
public bool UseForceRename { get; set; }
public RemapSearchParams SearchParams { get; set; } = new();
}
/// <summary>
/// Search filters to find types and remap them
/// </summary>
internal class RemapSearchParams
{
public bool? IsPublic { get; set; } = null;
public bool? IsAbstract { get; set; } = null;
public bool? IsInterface { get; set; } = null;
public bool? IsEnum { get; set; } = null;
public bool? IsNested { get; set; } = null;
public string? ParentName { get; set; } = null;
public bool? IsSealed { get; set; } = null;
public bool? HasAttribute { get; set; } = null;
public bool? IsDerived { get; set; } = null;
public bool? HasGenericParameters { get; set; } = null;
public HashSet<string> MethodNamesToMatch { get; set; } = [];
public HashSet<string> MethodNamesToIgnore { get; set; } = [];
public HashSet<string> FieldNamesToMatch { get; set; } = [];
public HashSet<string> FieldNamesToIgnore { get; set; } = [];
public HashSet<string> PropertyNamesToMatch { get; set; } = [];
public HashSet<string> PropertyNamesToIgnore { get; set; } = [];
public HashSet<string> NestedTypesToMatch { get; set; } = [];
public HashSet<string> NestedTypesToIgnore { get; set; } = [];
public RemapSearchParams()
{
}
public string MappingPath { get; set; }
}

View File

@ -0,0 +1,46 @@
namespace AssemblyRemapper.Models;
/// <summary>
/// Object to store linq statements in inside of json to search and remap classes
/// </summary>
internal class RemapModel
{
public string NewTypeName { get; set; } = string.Empty;
public string OriginalTypeName { get; set; } = string.Empty;
public bool UseForceRename { get; set; }
public SearchParams SearchParams { get; set; } = new();
}
/// <summary>
/// Search filters to find types and remap them
/// </summary>
internal class SearchParams
{
public bool? IsPublic { get; set; } = null;
public bool? IsAbstract { get; set; } = null;
public bool? IsInterface { get; set; } = null;
public bool? IsEnum { get; set; } = null;
public bool? IsNested { get; set; } = null;
public string? ParentName { get; set; } = null;
public bool? IsSealed { get; set; } = null;
public bool? HasAttribute { get; set; } = null;
public bool? IsDerived { get; set; } = null;
public bool? HasGenericParameters { get; set; } = null;
public HashSet<string> MethodNamesToMatch { get; set; } = [];
public HashSet<string> MethodNamesToIgnore { get; set; } = [];
public HashSet<string> FieldNamesToMatch { get; set; } = [];
public HashSet<string> FieldNamesToIgnore { get; set; } = [];
public HashSet<string> PropertyNamesToMatch { get; set; } = [];
public HashSet<string> PropertyNamesToIgnore { get; set; } = [];
public HashSet<string> NestedTypesToMatch { get; set; } = [];
public HashSet<string> NestedTypesToIgnore { get; set; } = [];
public SearchParams()
{
}
}

View File

@ -5,11 +5,12 @@ namespace AssemblyRemapper.Models;
internal class ScoringModel
{
public string ProposedNewName { get; set; }
public Remap Remap { get; set; }
public int Score { get; set; } = 0;
public string ProposedNewName { get; set; }
public TypeDefinition Definition { get; set; }
public RemapModel RemapModel { get; internal set; }
public ScoringModel()
{

View File

@ -24,7 +24,7 @@ internal class Remapper
private void StartRemap()
{
foreach (var remap in DataProvider.AppSettings.Remaps)
foreach (var remap in DataProvider.Remaps)
{
Logger.Log($"Trying to remap {remap.NewTypeName}...", ConsoleColor.Gray);
@ -43,7 +43,7 @@ internal class Remapper
WriteAssembly();
}
private void HandleMapping(Remap mapping)
private void HandleMapping(RemapModel mapping)
{
foreach (var type in DataProvider.ModuleDefinition.Types)
{
@ -101,7 +101,7 @@ internal class Remapper
}
}
private void ScoreType(TypeDefinition type, Remap remap, string parentTypeName = "")
private void ScoreType(TypeDefinition type, RemapModel remap, string parentTypeName = "")
{
// Handle Direct Remaps by strict naming first bypasses everything else
if (remap.UseForceRename)
@ -117,8 +117,9 @@ internal class Remapper
var score = new ScoringModel
{
Definition = type,
ProposedNewName = remap.NewTypeName,
RemapModel = remap,
Definition = type,
};
if (type.MatchIsAbstract(remap.SearchParams, score) == EMatchResult.NoMatch)
@ -126,7 +127,7 @@ internal class Remapper
LogDiscard("IsAbstract", type.Name, score.ProposedNewName);
return;
}
/*
if (type.MatchIsEnum(remap.SearchParams, score) == EMatchResult.NoMatch)
{
LogDiscard("IsEnum", type.Name, score.ProposedNewName);
@ -145,7 +146,6 @@ internal class Remapper
return;
}
*/
if (type.MatchIsDerived(remap.SearchParams, score) == EMatchResult.NoMatch)
{
LogDiscard("IsDerived", type.Name, score.ProposedNewName);
@ -202,9 +202,9 @@ internal class Remapper
ScoringModelExtensions.AddModelToResult(score);
}
private void HandleByDirectName(TypeDefinition type, Remap remap)
private void HandleByDirectName(TypeDefinition type, RemapModel remap)
{
if (type.Name != remap.OldTypeName) { return; }
if (type.Name != remap.OriginalTypeName) { return; }
var oldName = type.Name;
type.Name = remap.NewTypeName;
@ -248,12 +248,13 @@ internal class Remapper
: "Next potential";
Logger.Log("-----------------------------------------------", ConsoleColor.Green);
Logger.Log($"Found {scores.Count} results from search", ConsoleColor.Green);
Logger.Log($"{potentialText} match is `{highestScore.Definition.Name}` for `{highestScore.ProposedNewName}`", ConsoleColor.Green);
Logger.Log($"Renaming {highestScore.Definition.Name} to {highestScore.ProposedNewName}", ConsoleColor.Green);
highestScore.Definition.Name = highestScore.ProposedNewName;
foreach (var score in nextHighestScores)
{
Logger.Log($"Next potential match is `{score.Definition.Name}` for `{highestScore.ProposedNewName}`", ConsoleColor.Yellow);
Logger.Log($"Alternative match `{score.Definition.Name}` for `{highestScore.ProposedNewName}`", ConsoleColor.Yellow);
}
if (DataProvider.AppSettings.ScoringMode)

View File

@ -8,7 +8,7 @@ namespace AssemblyRemapper.Reflection;
internal static class RenameService
{
public static void RenameAllFields(
Remap remap,
RemapModel remap,
Collection<TypeDefinition> typesToCheck)
{
foreach (var type in typesToCheck)
@ -36,7 +36,7 @@ internal static class RenameService
}
public static void RenameAllProperties(
Remap remap,
RemapModel remap,
Collection<TypeDefinition> typesToCheck)
{
foreach (var type in typesToCheck)

View File

@ -8,7 +8,7 @@ internal static class SearchProvider
{
public static int MatchCount { get; private set; }
public static EMatchResult MatchIsAbstract(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
public static EMatchResult MatchIsAbstract(this TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.IsAbstract is null)
{
@ -30,7 +30,7 @@ internal static class SearchProvider
return EMatchResult.NoMatch;
}
public static EMatchResult MatchIsEnum(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
public static EMatchResult MatchIsEnum(this TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.IsEnum is null)
{
@ -46,7 +46,7 @@ internal static class SearchProvider
return EMatchResult.NoMatch;
}
public static EMatchResult MatchIsNested(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
public static EMatchResult MatchIsNested(this TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.IsNested is null)
{
@ -62,7 +62,7 @@ internal static class SearchProvider
return EMatchResult.NoMatch;
}
public static EMatchResult MatchIsSealed(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
public static EMatchResult MatchIsSealed(this TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.IsSealed is null)
{
@ -78,7 +78,7 @@ internal static class SearchProvider
return EMatchResult.NoMatch;
}
public static EMatchResult MatchIsDerived(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
public static EMatchResult MatchIsDerived(this TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.IsDerived is null)
{
@ -94,7 +94,7 @@ internal static class SearchProvider
return EMatchResult.NoMatch;
}
public static EMatchResult MatchIsInterface(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
public static EMatchResult MatchIsInterface(this TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.IsInterface is null)
{
@ -110,7 +110,7 @@ internal static class SearchProvider
return EMatchResult.NoMatch;
}
public static EMatchResult MatchIsGeneric(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
public static EMatchResult MatchIsGeneric(this TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.HasGenericParameters is null)
{
@ -126,7 +126,7 @@ internal static class SearchProvider
return EMatchResult.NoMatch;
}
public static EMatchResult MatchIsPublic(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
public static EMatchResult MatchIsPublic(this TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.IsPublic is null)
{
@ -144,7 +144,7 @@ internal static class SearchProvider
return EMatchResult.NoMatch;
}
public static EMatchResult MatchHasAttribute(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
public static EMatchResult MatchHasAttribute(this TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.HasAttribute is null)
{
@ -160,7 +160,7 @@ internal static class SearchProvider
return EMatchResult.NoMatch;
}
public static EMatchResult MatchMethods(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
public static EMatchResult MatchMethods(this TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.MethodNamesToMatch.Count == 0) { return EMatchResult.Disabled; }
@ -197,7 +197,7 @@ internal static class SearchProvider
return matchCount > 0 ? EMatchResult.Match : EMatchResult.NoMatch;
}
public static EMatchResult MatchFields(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
public static EMatchResult MatchFields(this TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.FieldNamesToMatch.Count == 0) { return EMatchResult.Disabled; }
@ -230,7 +230,7 @@ internal static class SearchProvider
return matchCount > 0 ? EMatchResult.Match : EMatchResult.NoMatch;
}
public static EMatchResult MatchProperties(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
public static EMatchResult MatchProperties(this TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.PropertyNamesToMatch.Count == 0) { return EMatchResult.Disabled; }
@ -263,7 +263,7 @@ internal static class SearchProvider
return matchCount > 0 ? EMatchResult.Match : EMatchResult.NoMatch;
}
public static EMatchResult MatchNestedTypes(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
public static EMatchResult MatchNestedTypes(this TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.NestedTypesToMatch.Count == 0) { return EMatchResult.Disabled; }

View File

@ -9,9 +9,10 @@ internal static class DataProvider
static DataProvider()
{
LoadAppSettings();
LoadAssemblyDefinition();
}
public static HashSet<RemapModel> Remaps { get; private set; } = [];
public static Dictionary<string, HashSet<ScoringModel>> ScoringModels { get; set; } = [];
public static AppSettings AppSettings { get; private set; }
@ -39,7 +40,22 @@ internal static class DataProvider
AppSettings = JsonConvert.DeserializeObject<AppSettings>(jsonText, settings);
}
private static void LoadAssemblyDefinition()
public static void LoadMappingFile()
{
if (!File.Exists(AppSettings.MappingPath))
{
throw new InvalidOperationException($"path `{AppSettings.MappingPath}` does not exist...");
}
var jsonText = File.ReadAllText(AppSettings.MappingPath);
Remaps = [];
ScoringModels = [];
Remaps = JsonConvert.DeserializeObject<HashSet<RemapModel>>(jsonText);
}
public static void LoadAssemblyDefinition()
{
DefaultAssemblyResolver resolver = new();
resolver.AddSearchDirectory(Path.GetDirectoryName(AppSettings.AssemblyPath)); // Replace with the correct path