Simplify finding custom attributes

This commit is contained in:
Paul Pacheco 2020-03-26 09:52:39 -05:00
parent d530d7a1e4
commit 7a6b854179
4 changed files with 88 additions and 88 deletions

View File

@ -138,7 +138,7 @@ public static MethodReference MakeHostInstanceGeneric(this MethodReference self,
return Weaver.CurrentAssembly.MainModule.ImportReference(reference);
}
public static CustomAttribute GetCustomAttribute(this MethodDefinition method, string attributeName)
public static CustomAttribute GetCustomAttribute(this ICustomAttributeProvider method, string attributeName)
{
foreach (CustomAttribute ca in method.CustomAttributes)
{
@ -148,5 +148,15 @@ public static CustomAttribute GetCustomAttribute(this MethodDefinition method, s
return null;
}
public static bool HasCustomAttribute(this ICustomAttributeProvider attributeProvider, TypeReference attribute)
{
foreach (CustomAttribute ca in attributeProvider.CustomAttributes)
{
if (ca.AttributeType.FullName == attribute.FullName)
return true;
}
return false;
}
}
}

View File

@ -16,13 +16,8 @@ static void ProcessSyncVars(TypeDefinition td)
// find syncvars
foreach (FieldDefinition fd in td.Fields)
{
foreach (CustomAttribute ca in fd.CustomAttributes)
{
if (ca.AttributeType.FullName == Weaver.SyncVarType.FullName)
{
if (fd.HasCustomAttribute(Weaver.SyncVarType))
Weaver.Error($"[SyncVar] {fd} must be inside a NetworkBehaviour. {td} is not a NetworkBehaviour");
}
}
if (SyncObjectInitializer.ImplementsSyncObject(fd.FieldType))
{

View File

@ -112,9 +112,9 @@ public static void ProcessEvents(TypeDefinition td, List<EventDefinition> events
// find events
foreach (EventDefinition ed in td.Events)
{
foreach (CustomAttribute ca in ed.CustomAttributes)
{
if (ca.AttributeType.FullName == Weaver.SyncEventType.FullName)
CustomAttribute ca = ed.GetCustomAttribute(Weaver.SyncEventType.FullName);
if (ca != null)
{
if (!ed.Name.StartsWith("Event"))
{
@ -153,4 +153,3 @@ public static void ProcessEvents(TypeDefinition td, List<EventDefinition> events
}
}
}
}

View File

@ -14,9 +14,9 @@ public static class SyncVarProcessor
public static bool CheckForHookFunction(TypeDefinition td, FieldDefinition syncVar, out MethodDefinition foundMethod)
{
foundMethod = null;
foreach (CustomAttribute ca in syncVar.CustomAttributes)
{
if (ca.AttributeType.FullName == Weaver.SyncVarType.FullName)
CustomAttribute ca = syncVar.GetCustomAttribute(Weaver.SyncVarType.FullName);
if (ca != null)
{
foreach (CustomAttributeNamedArgument customField in ca.Fields)
{
@ -48,7 +48,6 @@ public static bool CheckForHookFunction(TypeDefinition td, FieldDefinition syncV
}
}
}
}
return true;
}
@ -301,9 +300,7 @@ public static void ProcessSyncVars(TypeDefinition td, List<FieldDefinition> sync
// find syncvars
foreach (FieldDefinition fd in td.Fields)
{
foreach (CustomAttribute ca in fd.CustomAttributes)
{
if (ca.AttributeType.FullName == Weaver.SyncVarType.FullName)
if (fd.HasCustomAttribute(Weaver.SyncVarType))
{
TypeDefinition resolvedField = fd.FieldType.Resolve();
@ -322,9 +319,9 @@ public static void ProcessSyncVars(TypeDefinition td, List<FieldDefinition> sync
if (SyncObjectInitializer.ImplementsSyncObject(fd.FieldType))
{
Log.Warning($"{fd} has [SyncVar] attribute. SyncLists should not be marked with SyncVar");
break;
}
else
{
syncVars.Add(fd);
ProcessSyncVar(td, fd, syncVarNetIds, 1L << dirtyBitCounter);
@ -336,7 +333,6 @@ public static void ProcessSyncVars(TypeDefinition td, List<FieldDefinition> sync
Weaver.Error($"{td} has too many SyncVars. Consider refactoring your class into multiple components");
return;
}
break;
}
}