80 lines
2.6 KiB
C#
Raw Normal View History

2024-06-14 19:06:21 -04:00
using ReCodeIt.Enums;
using ReCodeIt.Models;
2024-06-13 04:56:08 -04:00
using Mono.Cecil;
2024-06-13 12:32:21 -04:00
using Mono.Cecil.Rocks;
2024-06-13 04:56:08 -04:00
2024-06-14 19:06:21 -04:00
namespace ReCodeIt.ReMapper.Search;
2024-06-13 04:56:08 -04:00
internal static class Methods
{
/// <summary>
/// returns a match on all types with the specified methods
/// </summary>
/// <param name="type"></param>
/// <param name="parms"></param>
/// <param name="score"></param>
/// <returns>Match if type contains any supplied methods</returns>
2024-06-13 08:18:16 -04:00
public static EMatchResult IncludeMethods(TypeDefinition type, SearchParams parms, ScoringModel score)
2024-06-13 04:56:08 -04:00
{
2024-06-13 12:32:21 -04:00
if (parms.IncludeMethods is null || parms.IncludeMethods.Count == 0) return EMatchResult.Disabled;
2024-06-13 04:56:08 -04:00
2024-06-13 07:45:40 -04:00
var matches = type.Methods
.Where(method => parms.IncludeMethods.Any(include => method.Name.Contains(include)))
2024-06-13 07:45:40 -04:00
.Count();
2024-06-13 04:56:08 -04:00
2024-06-13 07:45:40 -04:00
score.Score += matches;
2024-06-14 16:18:43 -04:00
score.FailureReason = matches > 0 ? EFailureReason.None : EFailureReason.MethodsInclude;
2024-06-13 07:45:40 -04:00
return matches > 0
2024-06-13 04:56:08 -04:00
? EMatchResult.Match
: EMatchResult.NoMatch;
}
/// <summary>
/// Returns a match on all types without methods
/// </summary>
/// <param name="type"></param>
/// <param name="parms"></param>
/// <param name="score"></param>
/// <returns>Match if type has no methods</returns>
2024-06-13 08:18:16 -04:00
public static EMatchResult ExcludeMethods(TypeDefinition type, SearchParams parms, ScoringModel score)
2024-06-13 04:56:08 -04:00
{
2024-06-13 12:32:21 -04:00
if (parms.ExcludeMethods is null || parms.ExcludeMethods.Count == 0) return EMatchResult.Disabled;
2024-06-13 04:56:08 -04:00
2024-06-13 07:45:40 -04:00
var matches = type.Methods
2024-06-13 12:32:21 -04:00
.Where(method => parms.ExcludeMethods.Contains(method.Name))
2024-06-13 07:45:40 -04:00
.Count();
2024-06-13 04:56:08 -04:00
2024-06-13 07:45:40 -04:00
score.Score += matches;
2024-06-13 04:56:08 -04:00
2024-06-14 16:18:43 -04:00
score.FailureReason = matches > 0 ? EFailureReason.None : EFailureReason.MethodsExclude;
2024-06-13 07:45:40 -04:00
return matches > 0
2024-06-13 08:18:16 -04:00
? EMatchResult.NoMatch
: EMatchResult.Match;
2024-06-13 04:56:08 -04:00
}
2024-06-13 12:32:21 -04:00
/// <summary>
/// Returns a match if the type has the provided number of methods
/// </summary>
/// <param name="type"></param>
/// <param name="parms"></param>
/// <param name="score"></param>
/// <returns></returns>
2024-06-13 08:18:16 -04:00
public static EMatchResult MatchMethodCount(TypeDefinition type, SearchParams parms, ScoringModel score)
2024-06-13 04:56:08 -04:00
{
2024-06-13 07:45:40 -04:00
if (parms.MethodCount is null) return EMatchResult.Disabled;
2024-06-13 12:32:21 -04:00
var numMethods = type.Methods.Count - type.GetConstructors().Count();
bool match = numMethods == parms.MethodCount;
2024-06-13 04:56:08 -04:00
2024-06-13 07:45:40 -04:00
if (match) { score.Score++; }
2024-06-13 04:56:08 -04:00
2024-06-14 16:18:43 -04:00
score.FailureReason = match ? EFailureReason.None : EFailureReason.MethodsCount;
2024-06-13 07:45:40 -04:00
return match
? EMatchResult.Match
: EMatchResult.NoMatch;
2024-06-13 04:56:08 -04:00
}
}