298 lines
8.1 KiB
C#
Raw Normal View History

2024-06-11 19:18:48 -04:00
using AssemblyRemapper.Models;
using Mono.Cecil;
using Mono.Cecil.Rocks;
namespace AssemblyRemapper.Reflection;
internal static class SearchProvider
{
public static int MatchCount { get; private set; }
public static EMatchResult MatchIsAbstract(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
{
if (parms.IsAbstract is null)
{
return EMatchResult.Disabled;
}
// Interfaces cannot be abstract, and abstract cannot be static
if (type.IsInterface || type.GetStaticConstructor() != null)
{
return EMatchResult.NoMatch;
}
2024-06-11 23:07:59 -04:00
if (type.IsAbstract == parms.IsAbstract)
2024-06-11 19:18:48 -04:00
{
score.Score += 1;
return EMatchResult.Match;
}
return EMatchResult.NoMatch;
}
public static EMatchResult MatchIsEnum(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
{
if (parms.IsEnum is null)
{
return EMatchResult.Disabled;
}
if (type.IsEnum == parms.IsEnum)
{
score.Score += 1;
return EMatchResult.Match;
}
return EMatchResult.NoMatch;
}
public static EMatchResult MatchIsNested(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
{
if (parms.IsNested is null)
{
return EMatchResult.Disabled;
}
if (type.IsNested == parms.IsNested)
{
score.Score += 1;
return EMatchResult.Match;
}
return EMatchResult.NoMatch;
}
public static EMatchResult MatchIsSealed(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
{
if (parms.IsSealed is null)
{
return EMatchResult.Disabled;
}
if (type.IsSealed == parms.IsSealed)
{
score.Score += 1;
return EMatchResult.Match;
}
return EMatchResult.NoMatch;
}
public static EMatchResult MatchIsDerived(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
{
if (parms.IsDerived is null)
{
return EMatchResult.Disabled;
}
if (type.BaseType != null && (bool)parms.IsDerived)
{
score.Score += 1;
return EMatchResult.Match;
}
return EMatchResult.NoMatch;
}
public static EMatchResult MatchIsInterface(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
{
if (parms.IsInterface is null)
{
return EMatchResult.Disabled;
}
2024-06-11 23:07:59 -04:00
if (type.IsInterface == parms.IsInterface)
2024-06-11 19:18:48 -04:00
{
score.Score += 1;
return EMatchResult.Match;
}
return EMatchResult.NoMatch;
}
public static EMatchResult MatchIsGeneric(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
{
2024-06-11 23:07:59 -04:00
if (parms.HasGenericParameters is null)
2024-06-11 19:18:48 -04:00
{
return EMatchResult.Disabled;
}
2024-06-11 23:07:59 -04:00
if (type.HasGenericParameters == parms.HasGenericParameters)
2024-06-11 19:18:48 -04:00
{
score.Score += 1;
return EMatchResult.Match;
}
return EMatchResult.NoMatch;
}
public static EMatchResult MatchIsPublic(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
{
if (parms.IsPublic is null)
{
return EMatchResult.Disabled;
}
2024-06-11 23:07:59 -04:00
var boolToCheck = parms.IsPublic == true ? type.IsPublic : type.IsNotPublic;
if (boolToCheck == !parms.IsPublic)
2024-06-11 19:18:48 -04:00
{
score.Score += 1;
return EMatchResult.Match;
}
return EMatchResult.NoMatch;
}
public static EMatchResult MatchHasAttribute(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
{
if (parms.HasAttribute is null)
{
return EMatchResult.Disabled;
}
if (type.HasCustomAttributes == parms.HasAttribute)
{
score.Score += 1;
return EMatchResult.Match;
}
return EMatchResult.NoMatch;
}
public static EMatchResult MatchMethods(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
{
2024-06-11 23:07:59 -04:00
if (parms.MethodNamesToMatch.Count == 0) { return EMatchResult.Disabled; }
2024-06-11 19:18:48 -04:00
2024-06-11 23:07:59 -04:00
if (type.HasMethods)
2024-06-11 19:18:48 -04:00
{
2024-06-11 23:07:59 -04:00
// `*` is the wildcard to ignore all methods that exist on types
if (parms.MethodNamesToIgnore.Contains("*"))
{
return EMatchResult.NoMatch;
}
2024-06-11 19:18:48 -04:00
}
2024-06-11 23:07:59 -04:00
var matchCount = 0;
2024-06-11 19:18:48 -04:00
foreach (var method in type.Methods)
{
if (parms.MethodNamesToIgnore.Contains(method.Name))
{
// Type contains blacklisted method
return EMatchResult.NoMatch;
}
2024-06-11 23:07:59 -04:00
foreach (var name in parms.MethodNamesToMatch)
2024-06-11 19:18:48 -04:00
{
2024-06-11 23:07:59 -04:00
if (method.Name == name)
{
matchCount += 1;
score.Score += 2;
continue;
}
2024-06-11 19:18:48 -04:00
}
}
return matchCount > 0 ? EMatchResult.Match : EMatchResult.NoMatch;
}
public static EMatchResult MatchFields(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
{
2024-06-11 23:07:59 -04:00
if (parms.FieldNamesToMatch.Count == 0) { return EMatchResult.Disabled; }
2024-06-11 19:18:48 -04:00
2024-06-11 23:07:59 -04:00
if (type.HasFields)
2024-06-11 19:18:48 -04:00
{
2024-06-11 23:07:59 -04:00
// `*` is the wildcard to ignore all fields that exist on types
if (parms.FieldNamesToIgnore.Contains("*"))
{
return EMatchResult.NoMatch;
}
2024-06-11 19:18:48 -04:00
}
int matchCount = 0;
foreach (var field in type.Fields)
{
if (parms.FieldNamesToIgnore.Contains(field.Name))
{
// Type contains blacklisted field
return EMatchResult.NoMatch;
}
if (parms.FieldNamesToMatch.Contains(field.Name))
{
matchCount++;
score.Score += 2;
}
}
return matchCount > 0 ? EMatchResult.Match : EMatchResult.NoMatch;
}
public static EMatchResult MatchProperties(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
{
2024-06-11 23:07:59 -04:00
if (parms.PropertyNamesToMatch.Count == 0) { return EMatchResult.Disabled; }
2024-06-11 19:18:48 -04:00
2024-06-11 23:07:59 -04:00
if (type.HasProperties)
2024-06-11 19:18:48 -04:00
{
2024-06-11 23:07:59 -04:00
// `*` is the wildcard to ignore all fields that exist on types
if (parms.PropertyNamesToIgnore.Contains("*"))
{
return EMatchResult.NoMatch;
}
2024-06-11 19:18:48 -04:00
}
int matchCount = 0;
foreach (var property in type.Properties)
{
if (parms.PropertyNamesToIgnore.Contains(property.Name))
{
// Type contains blacklisted property
return EMatchResult.NoMatch;
}
if (parms.PropertyNamesToMatch.Contains(property.Name))
{
matchCount++;
score.Score += 2;
}
}
return matchCount > 0 ? EMatchResult.Match : EMatchResult.NoMatch;
}
public static EMatchResult MatchNestedTypes(this TypeDefinition type, RemapSearchParams parms, ScoringModel score)
{
2024-06-11 23:07:59 -04:00
if (parms.NestedTypesToMatch.Count == 0) { return EMatchResult.Disabled; }
2024-06-11 19:18:48 -04:00
2024-06-11 23:07:59 -04:00
if (type.HasNestedTypes)
2024-06-11 19:18:48 -04:00
{
2024-06-11 23:07:59 -04:00
// `*` is the wildcard to ignore all fields that exist on types
if (parms.NestedTypesToIgnore.Contains("*"))
{
return EMatchResult.NoMatch;
}
2024-06-11 19:18:48 -04:00
}
int matchCount = 0;
foreach (var nestedType in type.NestedTypes)
{
if (parms.NestedTypesToIgnore.Contains(nestedType.Name))
{
// Type contains blacklisted nested type
return EMatchResult.NoMatch;
}
if (parms.NestedTypesToMatch.Contains(nestedType.Name))
{
matchCount++;
score.Score += 2;
}
}
return matchCount > 0 ? EMatchResult.Match : EMatchResult.NoMatch;
}
}