Weaver ImplementsInterface is now an extension. Easier usage and less magic in Weaver.cs

This commit is contained in:
vis2k 2019-01-01 19:29:19 +01:00
parent 750f94eab8
commit 1643444a9b
4 changed files with 29 additions and 30 deletions

View File

@ -39,5 +39,32 @@ public static bool IsDerivedFrom(this TypeDefinition td, TypeReference baseClass
} }
return false; return false;
} }
public static bool ImplementsInterface(this TypeDefinition td, TypeReference baseInterface)
{
TypeDefinition typedef = td;
while (typedef != null)
{
foreach (InterfaceImplementation iface in typedef.Interfaces)
{
if (iface.InterfaceType.FullName == baseInterface.FullName)
return true;
}
try
{
TypeReference parent = typedef.BaseType;
typedef = parent == null ? null : parent.Resolve();
}
catch (AssemblyResolutionException)
{
// this can happen for pluins.
//Console.WriteLine("AssemblyResolutionException: "+ ex.ToString());
break;
}
}
return false;
}
} }
} }

View File

@ -17,7 +17,7 @@ public static bool ImplementsSyncObject(TypeReference typeRef)
return false; return false;
} }
return Weaver.ImplementsInterface(typeRef.Resolve(), Weaver.SyncObjectType); return typeRef.Resolve().ImplementsInterface(Weaver.SyncObjectType);
} }
catch catch
{ {

View File

@ -295,7 +295,7 @@ public static void ProcessSyncVars(TypeDefinition td, List<FieldDefinition> sync
return; return;
} }
if (Weaver.ImplementsInterface(fd.FieldType.Resolve(), Weaver.SyncObjectType)) if (fd.FieldType.Resolve().ImplementsInterface(Weaver.SyncObjectType))
{ {
if (fd.IsStatic) if (fd.IsStatic)
{ {

View File

@ -1423,34 +1423,6 @@ static bool IsNetworkBehaviour(TypeDefinition td)
return td.IsDerivedFrom(NetworkBehaviourType); return td.IsDerivedFrom(NetworkBehaviourType);
} }
public static bool ImplementsInterface(TypeDefinition td, TypeReference baseInterface)
{
TypeDefinition typedef = td;
while ( typedef != null)
{
foreach (InterfaceImplementation iface in typedef.Interfaces)
{
if (iface.InterfaceType.FullName == baseInterface.FullName)
return true;
}
try
{
TypeReference parent = typedef.BaseType;
typedef = parent == null ? null : parent.Resolve();
}
catch (AssemblyResolutionException)
{
// this can happen for pluins.
//Console.WriteLine("AssemblyResolutionException: "+ ex.ToString());
break;
}
}
return false;
}
public static bool IsValidTypeToGenerate(TypeDefinition variable) public static bool IsValidTypeToGenerate(TypeDefinition variable)
{ {
// a valid type is a simple class or struct. so we generate only code for types we dont know, and if they are not inside // a valid type is a simple class or struct. so we generate only code for types we dont know, and if they are not inside