New Method and field models
This commit is contained in:
parent
cefeb9638b
commit
a7a4ee33ce
@ -9,6 +9,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Mono.Cecil" Version="0.11.5" />
|
||||
<PackageReference Include="morelinq" Version="4.2.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
using AssemblyRemapper.Remapper;
|
||||
using AssemblyRemapper.Utils;
|
||||
using AssemblyRemapper.Utils;
|
||||
|
||||
namespace AssemblyRemapper.Commands
|
||||
{
|
||||
@ -25,6 +24,10 @@ namespace AssemblyRemapper.Commands
|
||||
{
|
||||
var remapper = new Remapper.Remapper();
|
||||
|
||||
Logger.ClearLog();
|
||||
Console.Clear();
|
||||
ShowStartText();
|
||||
|
||||
DataProvider.LoadMappingFile();
|
||||
DataProvider.LoadAssemblyDefinition();
|
||||
|
||||
|
@ -1,5 +1,73 @@
|
||||
namespace AssemblyRemapper.Remapper.Search;
|
||||
using AssemblyRemapper.Enums;
|
||||
using AssemblyRemapper.Models;
|
||||
using Mono.Cecil;
|
||||
using MoreLinq;
|
||||
|
||||
namespace AssemblyRemapper.Remapper.Search;
|
||||
|
||||
internal static class Fields
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns a match on any type with the provided fields
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="parms"></param>
|
||||
/// <param name="score"></param>
|
||||
/// <returns></returns>
|
||||
public static EMatchResult GetTypeWithFields(TypeDefinition type, SearchParams parms, ScoringModel score)
|
||||
{
|
||||
if (parms.MatchFields is null || parms.MatchFields.Count == 0) return EMatchResult.Disabled;
|
||||
|
||||
var matches = type.Fields
|
||||
.Where(field => parms.MatchFields.Contains(field.Name))
|
||||
.Count();
|
||||
|
||||
score.Score += matches;
|
||||
|
||||
return matches > 0
|
||||
? EMatchResult.Match
|
||||
: EMatchResult.NoMatch;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a match on any type without the provided fields
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="parms"></param>
|
||||
/// <param name="score"></param>
|
||||
/// <returns></returns>
|
||||
public static EMatchResult GetTypeWithoutFields(TypeDefinition type, SearchParams parms, ScoringModel score)
|
||||
{
|
||||
if (parms.IgnoreFields is null || parms.IgnoreFields.Count == 0) return EMatchResult.Disabled;
|
||||
|
||||
var matches = type.Fields
|
||||
.Where(field => parms.IgnoreFields.Contains(field.Name))
|
||||
.Count();
|
||||
|
||||
score.Score += matches;
|
||||
|
||||
return matches > 0
|
||||
? EMatchResult.Match
|
||||
: EMatchResult.NoMatch;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a match on any type with a matching number of fields
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="parms"></param>
|
||||
/// <param name="score"></param>
|
||||
/// <returns></returns>
|
||||
public static EMatchResult GetTypeByNumberOfFields(TypeDefinition type, SearchParams parms, ScoringModel score)
|
||||
{
|
||||
if (parms.FieldCount is null) return EMatchResult.Disabled;
|
||||
|
||||
var match = type.Fields.Exactly((int)parms.FieldCount);
|
||||
|
||||
if (match) { score.Score++; }
|
||||
|
||||
return match
|
||||
? EMatchResult.Match
|
||||
: EMatchResult.NoMatch;
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
using AssemblyRemapper.Enums;
|
||||
using AssemblyRemapper.Models;
|
||||
using Mono.Cecil;
|
||||
using Mono.Cecil.Rocks;
|
||||
using MoreLinq;
|
||||
|
||||
namespace AssemblyRemapper.Remapper.Search;
|
||||
|
||||
@ -16,23 +16,15 @@ internal static class Methods
|
||||
/// <returns>Match if type contains any supplied methods</returns>
|
||||
public static EMatchResult GetTypeWithMethods(TypeDefinition type, SearchParams parms, ScoringModel score)
|
||||
{
|
||||
var matchCount = 0;
|
||||
if (parms.MatchMethods is null || parms.MatchMethods.Count == 0) return EMatchResult.Disabled;
|
||||
|
||||
// Handle match methods
|
||||
foreach (var method in type.Methods)
|
||||
{
|
||||
foreach (var name in parms.MatchMethods)
|
||||
{
|
||||
// Method name match
|
||||
if (method.Name == name)
|
||||
{
|
||||
matchCount += 1;
|
||||
score.Score++;
|
||||
}
|
||||
}
|
||||
}
|
||||
var matches = type.Methods
|
||||
.Where(method => parms.MatchMethods.Contains(method.Name))
|
||||
.Count();
|
||||
|
||||
return matchCount > 0
|
||||
score.Score += matches;
|
||||
|
||||
return matches > 0
|
||||
? EMatchResult.Match
|
||||
: EMatchResult.NoMatch;
|
||||
}
|
||||
@ -46,54 +38,29 @@ internal static class Methods
|
||||
/// <returns>Match if type has no methods</returns>
|
||||
public static EMatchResult GetTypeWithoutMethods(TypeDefinition type, SearchParams parms, ScoringModel score)
|
||||
{
|
||||
if (parms.IgnoreMethods is null) return EMatchResult.Disabled;
|
||||
if (parms.IgnoreMethods is null || parms.IgnoreMethods.Count == 0) return EMatchResult.Disabled;
|
||||
|
||||
var skippAll = parms.IgnoreMethods.Contains("*");
|
||||
var methodCount = type.Methods.Count - type.GetConstructors().Count();
|
||||
var matches = type.Methods
|
||||
.Where(method => parms.IgnoreMethods.Contains(method.Name))
|
||||
.Count();
|
||||
|
||||
// Subtract method count from constructor count to check for real methods
|
||||
if (methodCount is 0 && skippAll is true)
|
||||
{
|
||||
score.Score++;
|
||||
return EMatchResult.Match;
|
||||
}
|
||||
score.Score += matches;
|
||||
|
||||
return EMatchResult.NoMatch;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all types without the specified method
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="parms"></param>
|
||||
/// <param name="score"></param>
|
||||
/// <returns></returns>
|
||||
public static EMatchResult GetTypeWithNoMethods(TypeDefinition type, SearchParams parms, ScoringModel score)
|
||||
{
|
||||
if (parms.IgnoreMethods is null) { return EMatchResult.Disabled; }
|
||||
|
||||
foreach (var method in type.Methods)
|
||||
{
|
||||
if (parms.IgnoreMethods.Contains(method.Name))
|
||||
{
|
||||
return EMatchResult.NoMatch;
|
||||
}
|
||||
}
|
||||
|
||||
score.Score++;
|
||||
return EMatchResult.Match;
|
||||
return matches > 0
|
||||
? EMatchResult.Match
|
||||
: EMatchResult.NoMatch;
|
||||
}
|
||||
|
||||
public static EMatchResult GetTypeByNumberOfMethods(TypeDefinition type, SearchParams parms, ScoringModel score)
|
||||
{
|
||||
if (parms.MethodCount is null) { return EMatchResult.Disabled; }
|
||||
if (parms.MethodCount is null) return EMatchResult.Disabled;
|
||||
|
||||
if (type.Methods.Count == parms.MethodCount)
|
||||
{
|
||||
score.Score++;
|
||||
return EMatchResult.Match;
|
||||
}
|
||||
var match = type.Methods.Exactly((int)parms.MethodCount);
|
||||
|
||||
return EMatchResult.NoMatch;
|
||||
if (match) { score.Score++; }
|
||||
|
||||
return match
|
||||
? EMatchResult.Match
|
||||
: EMatchResult.NoMatch;
|
||||
}
|
||||
}
|
@ -200,35 +200,12 @@ internal static class TypeDefExtensions
|
||||
/// <returns>Match if any search criteria met</returns>
|
||||
public static EMatchResult MatchMethods(this TypeDefinition type, SearchParams parms, ScoringModel score)
|
||||
{
|
||||
var matches = new List<EMatchResult> { };
|
||||
|
||||
if (parms.MatchMethods.Count > 0 && parms.IgnoreMethods.Contains("*") is true)
|
||||
var matches = new List<EMatchResult>
|
||||
{
|
||||
Logger.Log($"Cannot both ignore all methods and search for a method on {score.ProposedNewName}.", ConsoleColor.Red);
|
||||
return EMatchResult.NoMatch;
|
||||
}
|
||||
else if (parms.MatchMethods.Count > 0)
|
||||
{
|
||||
matches.Add(Methods.GetTypeWithMethods(type, parms, score));
|
||||
}
|
||||
|
||||
if (parms.IgnoreMethods.Count > 0)
|
||||
{
|
||||
Logger.Log("TypeWithoutMethods");
|
||||
matches.Add(Methods.GetTypeWithoutMethods(type, parms, score));
|
||||
}
|
||||
|
||||
if (parms.IgnoreMethods.Contains("*"))
|
||||
{
|
||||
Logger.Log("TypeWithNoMethods");
|
||||
matches.Add(Methods.GetTypeWithNoMethods(type, parms, score));
|
||||
}
|
||||
|
||||
if (parms.MethodCount > 0)
|
||||
{
|
||||
Logger.Log("TypeByNumberOfMethods");
|
||||
matches.Add(Methods.GetTypeByNumberOfMethods(type, parms, score));
|
||||
}
|
||||
Methods.GetTypeWithMethods(type, parms, score),
|
||||
Methods.GetTypeWithoutMethods(type, parms, score),
|
||||
Methods.GetTypeByNumberOfMethods(type, parms, score)
|
||||
};
|
||||
|
||||
// return match if any condition matched
|
||||
return matches.GetMatch();
|
||||
@ -236,42 +213,15 @@ internal static class TypeDefExtensions
|
||||
|
||||
public static EMatchResult MatchFields(this TypeDefinition type, SearchParams parms, ScoringModel score)
|
||||
{
|
||||
if (parms.MatchFields.Count is 0 && parms.IgnoreFields.Count is 0)
|
||||
var matches = new List<EMatchResult>
|
||||
{
|
||||
return EMatchResult.Disabled;
|
||||
}
|
||||
Fields.GetTypeWithFields(type, parms, score),
|
||||
Fields.GetTypeWithoutFields(type, parms, score),
|
||||
Fields.GetTypeByNumberOfFields(type, parms, score)
|
||||
};
|
||||
|
||||
var skippAll = parms.IgnoreFields.Contains("*");
|
||||
|
||||
// Type has fields, we dont want any
|
||||
if (type.HasFields is false && skippAll is true)
|
||||
{
|
||||
score.Score++;
|
||||
return EMatchResult.Match;
|
||||
}
|
||||
|
||||
int matchCount = 0;
|
||||
|
||||
foreach (var field in type.Fields)
|
||||
{
|
||||
if (parms.IgnoreFields.Contains(field.Name))
|
||||
{
|
||||
// Type contains blacklisted field
|
||||
score.FailureReason = EFailureReason.HasFields;
|
||||
return EMatchResult.NoMatch;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var field in type.Fields)
|
||||
{
|
||||
if (parms.MatchFields.Contains(field.Name))
|
||||
{
|
||||
matchCount++;
|
||||
score.Score++;
|
||||
}
|
||||
}
|
||||
|
||||
return matchCount > 0 ? EMatchResult.Match : EMatchResult.NoMatch;
|
||||
// return match if any condition matched
|
||||
return matches.GetMatch();
|
||||
}
|
||||
|
||||
public static EMatchResult MatchProperties(this TypeDefinition type, SearchParams parms, ScoringModel score)
|
||||
|
@ -1,4 +1,5 @@
|
||||
using AssemblyRemapper.Enums;
|
||||
using MoreLinq.Extensions;
|
||||
|
||||
namespace AssemblyRemapper.Utils;
|
||||
|
||||
@ -11,11 +12,14 @@ internal static class EnumExtensions
|
||||
/// <returns>highest EMatchResult</returns>
|
||||
public static EMatchResult GetMatch(this List<EMatchResult> matches)
|
||||
{
|
||||
if (matches.Contains(EMatchResult.Disabled)) { return EMatchResult.Disabled; }
|
||||
// Prioritize returning matches
|
||||
if (matches.Contains(EMatchResult.Match)) { return EMatchResult.Match; }
|
||||
|
||||
// Then NoMatches
|
||||
if (matches.Contains(EMatchResult.NoMatch)) { return EMatchResult.NoMatch; }
|
||||
|
||||
if (matches.Contains(EMatchResult.Match)) { return EMatchResult.Match; }
|
||||
// Then Disabled
|
||||
if (matches.Contains(EMatchResult.Disabled)) { return EMatchResult.Disabled; }
|
||||
|
||||
// default to disabled
|
||||
return EMatchResult.Disabled;
|
||||
|
@ -13,7 +13,16 @@ internal static class Logger
|
||||
|
||||
private static string _logPath = Path.Combine(AppContext.BaseDirectory, "Data", "Log.log");
|
||||
|
||||
public static void Log(string message, ConsoleColor color = ConsoleColor.Gray, bool silent = false)
|
||||
public static void ClearLog()
|
||||
{
|
||||
if (File.Exists(_logPath))
|
||||
{
|
||||
File.Delete(_logPath);
|
||||
File.Create(_logPath).Close();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Log(object message, ConsoleColor color = ConsoleColor.Gray, bool silent = false)
|
||||
{
|
||||
if (silent)
|
||||
{
|
||||
@ -27,7 +36,7 @@ internal static class Logger
|
||||
WriteToDisk(message);
|
||||
}
|
||||
|
||||
public static void LogDebug(string message, ConsoleColor color = ConsoleColor.Gray, bool silent = false)
|
||||
public static void LogDebug(object message, ConsoleColor color = ConsoleColor.Gray, bool silent = false)
|
||||
{
|
||||
if (silent)
|
||||
{
|
||||
@ -44,7 +53,7 @@ internal static class Logger
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteToDisk(string message)
|
||||
private static void WriteToDisk(object message)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
@ -35,9 +35,7 @@
|
||||
"FieldCount": 0, // Match types that have a method count this length
|
||||
"PropertyCount": 0, // Match types that have a method count this length
|
||||
|
||||
// Note:
|
||||
// - You can can filter the ignore list with a wild card '*' to match all types that have none of that search parameter
|
||||
// - These are all lists of strings
|
||||
// List parameters
|
||||
|
||||
"MatchMethods": [ // This is a list of methods we want to match
|
||||
],
|
||||
|
Loading…
x
Reference in New Issue
Block a user