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

This commit is contained in:
vis2k 2019-01-01 19:23:43 +01:00
parent 7ce386a4f0
commit 750f94eab8
5 changed files with 49 additions and 41 deletions

View File

@ -0,0 +1,43 @@
using Mono.Cecil;
namespace Mirror.Weaver
{
public static class Extensions
{
public static bool IsDerivedFrom(this TypeDefinition td, TypeReference baseClass)
{
if (!td.IsClass)
return false;
// are ANY parent classes of baseClass?
TypeReference parent = td.BaseType;
while (parent != null)
{
string parentName = parent.FullName;
// strip generic parameters
int index = parentName.IndexOf('<');
if (index != -1)
{
parentName = parentName.Substring(0, index);
}
if (parentName == baseClass.FullName)
{
return true;
}
try
{
parent = parent.Resolve().BaseType;
}
catch (AssemblyResolutionException)
{
// this can happen for plugins.
//Console.WriteLine("AssemblyResolutionException: "+ ex.ToString());
break;
}
}
return false;
}
}
}

View File

@ -67,6 +67,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AssemblyInfo.cs" /> <Compile Include="AssemblyInfo.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="Processors\MessageClassProcessor.cs" /> <Compile Include="Processors\MessageClassProcessor.cs" />
<Compile Include="Processors\MonoBehaviourProcessor.cs" /> <Compile Include="Processors\MonoBehaviourProcessor.cs" />
<Compile Include="Processors\CommandProcessor.cs" /> <Compile Include="Processors\CommandProcessor.cs" />

View File

@ -751,7 +751,7 @@ public static bool ProcessMethodsValidateParameters(TypeDefinition td, MethodRef
Weaver.fail = true; Weaver.fail = true;
return false; return false;
} }
if (Weaver.IsDerivedFrom(p.ParameterType.Resolve(), Weaver.ComponentType)) if (p.ParameterType.Resolve().IsDerivedFrom(Weaver.ComponentType))
{ {
if (p.ParameterType.FullName != Weaver.NetworkIdentityType.FullName) if (p.ParameterType.FullName != Weaver.NetworkIdentityType.FullName)
{ {

View File

@ -210,14 +210,14 @@ public static void ProcessSyncVars(TypeDefinition td, List<FieldDefinition> sync
{ {
var resolvedField = fd.FieldType.Resolve(); var resolvedField = fd.FieldType.Resolve();
if (Weaver.IsDerivedFrom(resolvedField, Weaver.NetworkBehaviourType)) if (resolvedField.IsDerivedFrom(Weaver.NetworkBehaviourType))
{ {
Log.Error("SyncVar [" + fd.FullName + "] cannot be derived from NetworkBehaviour."); Log.Error("SyncVar [" + fd.FullName + "] cannot be derived from NetworkBehaviour.");
Weaver.fail = true; Weaver.fail = true;
return; return;
} }
if (Weaver.IsDerivedFrom(resolvedField, Weaver.ScriptableObjectType)) if (resolvedField.IsDerivedFrom(Weaver.ScriptableObjectType))
{ {
Log.Error("SyncVar [" + fd.FullName + "] cannot be derived from ScriptableObject."); Log.Error("SyncVar [" + fd.FullName + "] cannot be derived from ScriptableObject.");
Weaver.fail = true; Weaver.fail = true;

View File

@ -1418,45 +1418,9 @@ static void SetupWriteFunctions()
}; };
} }
public static bool IsDerivedFrom(TypeDefinition td, TypeReference baseClass)
{
if (!td.IsClass)
return false;
// are ANY parent classes NetworkBehaviours
TypeReference parent = td.BaseType;
while (parent != null)
{
var parentName = parent.FullName;
// strip generic parameters
int index = parentName.IndexOf('<');
if (index != -1)
{
parentName = parentName.Substring(0, index);
}
if (parentName == baseClass.FullName)
{
return true;
}
try
{
parent = parent.Resolve().BaseType;
}
catch (AssemblyResolutionException)
{
// this can happen for plugins.
//Console.WriteLine("AssemblyResolutionException: "+ ex.ToString());
break;
}
}
return false;
}
static bool IsNetworkBehaviour(TypeDefinition td) static bool IsNetworkBehaviour(TypeDefinition td)
{ {
return IsDerivedFrom(td, NetworkBehaviourType); return td.IsDerivedFrom(NetworkBehaviourType);
} }
public static bool ImplementsInterface(TypeDefinition td, TypeReference baseInterface) public static bool ImplementsInterface(TypeDefinition td, TypeReference baseInterface)
@ -1507,7 +1471,7 @@ public static bool IsValidTypeToGenerate(TypeDefinition variable)
static void CheckMonoBehaviour(TypeDefinition td) static void CheckMonoBehaviour(TypeDefinition td)
{ {
if (IsDerivedFrom(td, MonoBehaviourType)) if (td.IsDerivedFrom(MonoBehaviourType))
{ {
MonoBehaviourProcessor.Process(td); MonoBehaviourProcessor.Process(td);
} }