From 1643444a9bae09125112f43a142de4fbe919da44 Mon Sep 17 00:00:00 2001 From: vis2k Date: Tue, 1 Jan 2019 19:29:19 +0100 Subject: [PATCH] Weaver ImplementsInterface is now an extension. Easier usage and less magic in Weaver.cs --- Mirror/Weaver/Extensions.cs | 27 ++++++++++++++++++ .../Weaver/Processors/SyncObjectProcessor.cs | 2 +- Mirror/Weaver/Processors/SyncVarProcessor.cs | 2 +- Mirror/Weaver/Weaver.cs | 28 ------------------- 4 files changed, 29 insertions(+), 30 deletions(-) diff --git a/Mirror/Weaver/Extensions.cs b/Mirror/Weaver/Extensions.cs index e4ac48781..49f62b1a7 100644 --- a/Mirror/Weaver/Extensions.cs +++ b/Mirror/Weaver/Extensions.cs @@ -39,5 +39,32 @@ public static bool IsDerivedFrom(this TypeDefinition td, TypeReference baseClass } 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; + } } } \ No newline at end of file diff --git a/Mirror/Weaver/Processors/SyncObjectProcessor.cs b/Mirror/Weaver/Processors/SyncObjectProcessor.cs index dc5e09421..1c562075e 100644 --- a/Mirror/Weaver/Processors/SyncObjectProcessor.cs +++ b/Mirror/Weaver/Processors/SyncObjectProcessor.cs @@ -17,7 +17,7 @@ public static bool ImplementsSyncObject(TypeReference typeRef) return false; } - return Weaver.ImplementsInterface(typeRef.Resolve(), Weaver.SyncObjectType); + return typeRef.Resolve().ImplementsInterface(Weaver.SyncObjectType); } catch { diff --git a/Mirror/Weaver/Processors/SyncVarProcessor.cs b/Mirror/Weaver/Processors/SyncVarProcessor.cs index 6b58f48c1..0fa0ba53a 100644 --- a/Mirror/Weaver/Processors/SyncVarProcessor.cs +++ b/Mirror/Weaver/Processors/SyncVarProcessor.cs @@ -295,7 +295,7 @@ public static void ProcessSyncVars(TypeDefinition td, List sync return; } - if (Weaver.ImplementsInterface(fd.FieldType.Resolve(), Weaver.SyncObjectType)) + if (fd.FieldType.Resolve().ImplementsInterface(Weaver.SyncObjectType)) { if (fd.IsStatic) { diff --git a/Mirror/Weaver/Weaver.cs b/Mirror/Weaver/Weaver.cs index e325cc1ec..33871391d 100644 --- a/Mirror/Weaver/Weaver.cs +++ b/Mirror/Weaver/Weaver.cs @@ -1423,34 +1423,6 @@ static bool IsNetworkBehaviour(TypeDefinition td) 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) { // 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