332 lines
9.4 KiB
C#
Raw Normal View History

2024-06-12 00:07:44 -04:00
using AssemblyRemapper.Enums;
using AssemblyRemapper.Models;
2024-06-11 19:18:48 -04:00
using Mono.Cecil;
using Mono.Cecil.Rocks;
namespace AssemblyRemapper.Reflection;
internal static class SearchExtentions
2024-06-11 19:18:48 -04:00
{
2024-06-12 00:05:59 -04:00
public static EMatchResult MatchIsAbstract(this TypeDefinition type, SearchParams parms, ScoringModel score)
2024-06-11 19:18:48 -04:00
{
if (parms.IsAbstract is null)
{
return EMatchResult.Disabled;
}
// Interfaces cannot be abstract, and abstract cannot be static
2024-06-12 14:38:43 -04:00
if (type.IsInterface || type.GetStaticConstructor() is not null)
2024-06-11 19:18:48 -04:00
{
return EMatchResult.NoMatch;
}
2024-06-11 23:07:59 -04:00
if (type.IsAbstract == parms.IsAbstract)
2024-06-11 19:18:48 -04:00
{
2024-06-12 18:59:08 -04:00
score.Score++;
2024-06-11 19:18:48 -04:00
return EMatchResult.Match;
}
2024-06-12 18:59:08 -04:00
score.FailureReason = EFailureReason.IsAbstract;
2024-06-11 19:18:48 -04:00
return EMatchResult.NoMatch;
}
2024-06-12 00:05:59 -04:00
public static EMatchResult MatchIsEnum(this TypeDefinition type, SearchParams parms, ScoringModel score)
2024-06-11 19:18:48 -04:00
{
if (parms.IsEnum is null)
{
return EMatchResult.Disabled;
}
if (type.IsEnum == parms.IsEnum)
{
2024-06-12 18:59:08 -04:00
score.Score++;
2024-06-11 19:18:48 -04:00
return EMatchResult.Match;
}
2024-06-12 18:59:08 -04:00
score.FailureReason = EFailureReason.IsEnum;
2024-06-11 19:18:48 -04:00
return EMatchResult.NoMatch;
}
2024-06-12 00:05:59 -04:00
public static EMatchResult MatchIsNested(this TypeDefinition type, SearchParams parms, ScoringModel score)
2024-06-11 19:18:48 -04:00
{
if (parms.IsNested is null)
{
return EMatchResult.Disabled;
}
if (type.IsNested == parms.IsNested)
{
2024-06-12 18:59:08 -04:00
score.Score++;
2024-06-11 19:18:48 -04:00
return EMatchResult.Match;
}
2024-06-12 18:59:08 -04:00
score.FailureReason = EFailureReason.IsNested;
2024-06-11 19:18:48 -04:00
return EMatchResult.NoMatch;
}
2024-06-12 00:05:59 -04:00
public static EMatchResult MatchIsSealed(this TypeDefinition type, SearchParams parms, ScoringModel score)
2024-06-11 19:18:48 -04:00
{
if (parms.IsSealed is null)
{
return EMatchResult.Disabled;
}
if (type.IsSealed == parms.IsSealed)
{
2024-06-12 18:59:08 -04:00
score.Score++;
2024-06-11 19:18:48 -04:00
return EMatchResult.Match;
}
2024-06-12 18:59:08 -04:00
score.FailureReason = EFailureReason.IsSealed;
2024-06-11 19:18:48 -04:00
return EMatchResult.NoMatch;
}
2024-06-12 00:05:59 -04:00
public static EMatchResult MatchIsDerived(this TypeDefinition type, SearchParams parms, ScoringModel score)
2024-06-11 19:18:48 -04:00
{
if (parms.IsDerived is null)
{
return EMatchResult.Disabled;
}
if (type.BaseType != null && (bool)parms.IsDerived)
{
2024-06-12 18:59:08 -04:00
score.Score++;
2024-06-11 19:18:48 -04:00
return EMatchResult.Match;
}
2024-06-12 18:59:08 -04:00
score.FailureReason = EFailureReason.IsDerived;
2024-06-11 19:18:48 -04:00
return EMatchResult.NoMatch;
}
2024-06-12 00:05:59 -04:00
public static EMatchResult MatchIsInterface(this TypeDefinition type, SearchParams parms, ScoringModel score)
2024-06-11 19:18:48 -04:00
{
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
{
2024-06-12 18:59:08 -04:00
score.Score++;
2024-06-11 19:18:48 -04:00
return EMatchResult.Match;
}
2024-06-12 18:59:08 -04:00
score.FailureReason = EFailureReason.IsInterface;
2024-06-11 19:18:48 -04:00
return EMatchResult.NoMatch;
}
2024-06-12 18:59:08 -04:00
public static EMatchResult MatchHasGenericParameters(this TypeDefinition type, SearchParams parms, ScoringModel score)
2024-06-11 19:18:48 -04:00
{
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
{
2024-06-12 18:59:08 -04:00
score.Score++;
2024-06-11 19:18:48 -04:00
return EMatchResult.Match;
}
2024-06-12 18:59:08 -04:00
score.FailureReason = EFailureReason.HasGenericParameters;
2024-06-11 19:18:48 -04:00
return EMatchResult.NoMatch;
}
2024-06-12 00:05:59 -04:00
public static EMatchResult MatchIsPublic(this TypeDefinition type, SearchParams parms, ScoringModel score)
2024-06-11 19:18:48 -04:00
{
if (parms.IsPublic is null)
{
return EMatchResult.Disabled;
}
if (parms.IsPublic is false && type.IsNotPublic is true)
{
score.Score++;
return EMatchResult.Match;
}
else if (parms.IsPublic is true && type.IsPublic is true)
2024-06-11 19:18:48 -04:00
{
2024-06-12 18:59:08 -04:00
score.Score++;
2024-06-11 19:18:48 -04:00
return EMatchResult.Match;
}
2024-06-12 18:59:08 -04:00
score.FailureReason = EFailureReason.IsPublic;
2024-06-11 19:18:48 -04:00
return EMatchResult.NoMatch;
}
2024-06-12 00:05:59 -04:00
public static EMatchResult MatchHasAttribute(this TypeDefinition type, SearchParams parms, ScoringModel score)
2024-06-11 19:18:48 -04:00
{
if (parms.HasAttribute is null)
{
return EMatchResult.Disabled;
}
if (type.HasCustomAttributes == parms.HasAttribute)
{
2024-06-12 18:59:08 -04:00
score.Score++;
2024-06-11 19:18:48 -04:00
return EMatchResult.Match;
}
2024-06-12 18:59:08 -04:00
score.FailureReason = EFailureReason.HasAttribute;
2024-06-11 19:18:48 -04:00
return EMatchResult.NoMatch;
}
2024-06-12 00:05:59 -04:00
public static EMatchResult MatchMethods(this TypeDefinition type, SearchParams parms, ScoringModel score)
2024-06-11 19:18:48 -04:00
{
2024-06-12 18:59:08 -04:00
// We're not searching for methods and this type contains methods
2024-06-12 20:40:10 -04:00
if (parms.MatchMethods.Count == 0 && parms.IgnoreMethods.Count == 0)
2024-06-12 18:59:08 -04:00
{
return type.HasMethods
? EMatchResult.NoMatch
: EMatchResult.Match;
}
2024-06-11 19:18:48 -04:00
2024-06-12 20:40:10 -04:00
var skippAll = parms.IgnoreMethods.Contains("*");
2024-06-12 18:59:08 -04:00
// The type has methods and we dont want any
if (type.HasMethods is true && skippAll is true)
2024-06-11 19:18:48 -04:00
{
2024-06-12 18:59:08 -04:00
foreach (var method in type.Methods)
2024-06-11 23:07:59 -04:00
{
2024-06-12 18:59:08 -04:00
if (method.Name == ".ctor")
{
continue;
}
score.Score--;
2024-06-11 23:07:59 -04:00
return EMatchResult.NoMatch;
}
2024-06-12 18:59:08 -04:00
score.Score++;
return EMatchResult.Match;
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)
{
2024-06-12 18:59:08 -04:00
// Type contains a method we dont want
2024-06-12 20:40:10 -04:00
if (parms.IgnoreMethods.Contains(method.Name))
2024-06-11 19:18:48 -04:00
{
2024-06-12 18:59:08 -04:00
score.FailureReason = EFailureReason.HasMethods;
2024-06-11 19:18:48 -04:00
return EMatchResult.NoMatch;
}
2024-06-12 20:40:10 -04:00
foreach (var name in parms.MatchMethods)
2024-06-11 19:18:48 -04:00
{
2024-06-12 18:59:08 -04:00
// Method name match
2024-06-11 23:07:59 -04:00
if (method.Name == name)
{
matchCount += 1;
2024-06-12 18:59:08 -04:00
score.Score++;
2024-06-11 23:07:59 -04:00
}
2024-06-11 19:18:48 -04:00
}
}
return matchCount > 0 ? EMatchResult.Match : EMatchResult.NoMatch;
}
2024-06-12 00:05:59 -04:00
public static EMatchResult MatchFields(this TypeDefinition type, SearchParams parms, ScoringModel score)
2024-06-11 19:18:48 -04:00
{
2024-06-12 20:40:10 -04:00
if (parms.MatchFields.Count == 0 && parms.IgnoreFields.Count == 0)
2024-06-12 18:59:08 -04:00
{
return EMatchResult.Disabled;
}
2024-06-11 19:18:48 -04:00
2024-06-12 18:59:08 -04:00
// `*` is the wildcard to ignore all fields that exist on types
2024-06-12 20:40:10 -04:00
if (!type.HasFields && parms.IgnoreFields.Contains("*"))
2024-06-11 19:18:48 -04:00
{
2024-06-12 18:59:08 -04:00
return EMatchResult.Match;
2024-06-11 19:18:48 -04:00
}
int matchCount = 0;
foreach (var field in type.Fields)
{
2024-06-12 20:40:10 -04:00
if (parms.IgnoreFields.Contains(field.Name))
2024-06-11 19:18:48 -04:00
{
// Type contains blacklisted field
2024-06-12 18:59:08 -04:00
score.FailureReason = EFailureReason.HasFields;
2024-06-11 19:18:48 -04:00
return EMatchResult.NoMatch;
}
2024-06-12 20:40:10 -04:00
if (parms.MatchFields.Contains(field.Name))
2024-06-11 19:18:48 -04:00
{
matchCount++;
2024-06-12 18:59:08 -04:00
score.Score++;
2024-06-11 19:18:48 -04:00
}
}
return matchCount > 0 ? EMatchResult.Match : EMatchResult.NoMatch;
}
2024-06-12 00:05:59 -04:00
public static EMatchResult MatchProperties(this TypeDefinition type, SearchParams parms, ScoringModel score)
2024-06-11 19:18:48 -04:00
{
2024-06-12 20:40:10 -04:00
if (parms.MatchProperties.Count == 0 && parms.IgnorePropterties.Count == 0)
2024-06-12 18:59:08 -04:00
{
return EMatchResult.Disabled;
}
2024-06-11 19:18:48 -04:00
2024-06-12 18:59:08 -04:00
// `*` is the wildcard to ignore all fields that exist on types
2024-06-12 20:40:10 -04:00
if (!type.HasProperties && parms.IgnorePropterties.Contains("*"))
2024-06-11 19:18:48 -04:00
{
2024-06-12 18:59:08 -04:00
score.Score++;
return EMatchResult.Match;
2024-06-11 19:18:48 -04:00
}
int matchCount = 0;
foreach (var property in type.Properties)
{
2024-06-12 20:40:10 -04:00
if (parms.IgnorePropterties.Contains(property.Name))
2024-06-11 19:18:48 -04:00
{
// Type contains blacklisted property
2024-06-12 18:59:08 -04:00
score.FailureReason = EFailureReason.HasProperties;
2024-06-11 19:18:48 -04:00
return EMatchResult.NoMatch;
}
2024-06-12 20:40:10 -04:00
if (parms.MatchProperties.Contains(property.Name))
2024-06-11 19:18:48 -04:00
{
matchCount++;
2024-06-12 18:59:08 -04:00
score.Score++;
2024-06-11 19:18:48 -04:00
}
}
return matchCount > 0 ? EMatchResult.Match : EMatchResult.NoMatch;
}
2024-06-12 00:05:59 -04:00
public static EMatchResult MatchNestedTypes(this TypeDefinition type, SearchParams parms, ScoringModel score)
2024-06-11 19:18:48 -04:00
{
2024-06-12 20:40:10 -04:00
if (parms.MatchNestedTypes.Count == 0 && parms.IgnoreNestedTypes.Count == 0)
2024-06-12 18:59:08 -04:00
{
return EMatchResult.Disabled;
}
2024-06-11 19:18:48 -04:00
2024-06-12 18:59:08 -04:00
// `*` is the wildcard to ignore all fields that exist on types
2024-06-12 20:40:10 -04:00
if (type.HasNestedTypes && parms.IgnoreNestedTypes.Contains("*"))
2024-06-11 19:18:48 -04:00
{
2024-06-12 18:59:08 -04:00
score.FailureReason = EFailureReason.HasNestedTypes;
return EMatchResult.NoMatch;
2024-06-11 19:18:48 -04:00
}
int matchCount = 0;
foreach (var nestedType in type.NestedTypes)
{
2024-06-12 20:40:10 -04:00
if (parms.IgnoreNestedTypes.Contains(nestedType.Name))
2024-06-11 19:18:48 -04:00
{
// Type contains blacklisted nested type
2024-06-12 18:59:08 -04:00
score.FailureReason = EFailureReason.HasNestedTypes;
2024-06-11 19:18:48 -04:00
return EMatchResult.NoMatch;
}
2024-06-12 20:40:10 -04:00
if (parms.MatchNestedTypes.Contains(nestedType.Name))
2024-06-11 19:18:48 -04:00
{
matchCount++;
2024-06-12 18:59:08 -04:00
score.Score++;
2024-06-11 19:18:48 -04:00
}
}
return matchCount > 0 ? EMatchResult.Match : EMatchResult.NoMatch;
}
}