using AssemblyRemapper.Enums;
using AssemblyRemapper.Models;
using Mono.Cecil;
using Mono.Cecil.Rocks;
namespace AssemblyRemapper.Remapper.Search;
internal static class Methods
{
///
/// returns a match on all types with the specified methods
///
///
///
///
/// Match if type contains any supplied methods
public static EMatchResult GetTypeWithMethods(TypeDefinition type, SearchParams parms, ScoringModel score)
{
var matchCount = 0;
// 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++;
}
}
}
return matchCount > 0
? EMatchResult.Match
: EMatchResult.NoMatch;
}
///
/// Returns a match on all types without methods
///
///
///
///
/// Match if type has no methods
public static EMatchResult GetTypeWithoutMethods(TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.IgnoreMethods is null) return EMatchResult.Disabled;
var skippAll = parms.IgnoreMethods.Contains("*");
var methodCount = type.Methods.Count - type.GetConstructors().Count();
// Subtract method count from constructor count to check for real methods
if (methodCount is 0 && skippAll is true)
{
score.Score++;
return EMatchResult.Match;
}
return EMatchResult.NoMatch;
}
///
/// Get all types without the specified method
///
///
///
///
///
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;
}
public static EMatchResult GetTypeByNumberOfMethods(TypeDefinition type, SearchParams parms, ScoringModel score)
{
if (parms.MethodCount is null) { return EMatchResult.Disabled; }
if (type.Methods.Count == parms.MethodCount)
{
score.Score++;
return EMatchResult.Match;
}
return EMatchResult.NoMatch;
}
}