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;
|
|
|
|
|
|
2024-06-12 20:18:10 -04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 20:16:12 -04:00
|
|
|
|
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
|
|
|
|
|
if (parms.MethodNamesToMatch.Count == 0 && parms.MethodNamesToIgnore.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return type.HasMethods
|
|
|
|
|
? EMatchResult.NoMatch
|
|
|
|
|
: EMatchResult.Match;
|
|
|
|
|
}
|
2024-06-11 19:18:48 -04:00
|
|
|
|
|
2024-06-12 18:59:08 -04:00
|
|
|
|
var skippAll = parms.MethodNamesToIgnore.Contains("*");
|
|
|
|
|
|
|
|
|
|
// 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-11 19:18:48 -04:00
|
|
|
|
if (parms.MethodNamesToIgnore.Contains(method.Name))
|
|
|
|
|
{
|
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-11 23:07:59 -04:00
|
|
|
|
foreach (var name in parms.MethodNamesToMatch)
|
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 18:59:08 -04:00
|
|
|
|
if (parms.FieldNamesToMatch.Count == 0 && parms.FieldNamesToIgnore.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
if (!type.HasFields && parms.FieldNamesToIgnore.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)
|
|
|
|
|
{
|
|
|
|
|
if (parms.FieldNamesToIgnore.Contains(field.Name))
|
|
|
|
|
{
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (parms.FieldNamesToMatch.Contains(field.Name))
|
|
|
|
|
{
|
|
|
|
|
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 18:59:08 -04:00
|
|
|
|
if (parms.PropertyNamesToMatch.Count == 0 && parms.PropertyNamesToIgnore.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
if (!type.HasProperties && parms.PropertyNamesToIgnore.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)
|
|
|
|
|
{
|
|
|
|
|
if (parms.PropertyNamesToIgnore.Contains(property.Name))
|
|
|
|
|
{
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (parms.PropertyNamesToMatch.Contains(property.Name))
|
|
|
|
|
{
|
|
|
|
|
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 18:59:08 -04:00
|
|
|
|
if (parms.NestedTypesToMatch.Count == 0 && parms.NestedTypesToIgnore.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
if (type.HasNestedTypes && parms.NestedTypesToIgnore.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)
|
|
|
|
|
{
|
|
|
|
|
if (parms.NestedTypesToIgnore.Contains(nestedType.Name))
|
|
|
|
|
{
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (parms.NestedTypesToMatch.Contains(nestedType.Name))
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|