mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
Weaver IsDerivedFrom is now an extension. Easier usage and less magic in Weaver.cs
This commit is contained in:
parent
7ce386a4f0
commit
750f94eab8
43
Mirror/Weaver/Extensions.cs
Normal file
43
Mirror/Weaver/Extensions.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -67,6 +67,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AssemblyInfo.cs" />
|
||||
<Compile Include="Extensions.cs" />
|
||||
<Compile Include="Processors\MessageClassProcessor.cs" />
|
||||
<Compile Include="Processors\MonoBehaviourProcessor.cs" />
|
||||
<Compile Include="Processors\CommandProcessor.cs" />
|
||||
|
@ -751,7 +751,7 @@ public static bool ProcessMethodsValidateParameters(TypeDefinition td, MethodRef
|
||||
Weaver.fail = true;
|
||||
return false;
|
||||
}
|
||||
if (Weaver.IsDerivedFrom(p.ParameterType.Resolve(), Weaver.ComponentType))
|
||||
if (p.ParameterType.Resolve().IsDerivedFrom(Weaver.ComponentType))
|
||||
{
|
||||
if (p.ParameterType.FullName != Weaver.NetworkIdentityType.FullName)
|
||||
{
|
||||
|
@ -210,14 +210,14 @@ public static void ProcessSyncVars(TypeDefinition td, List<FieldDefinition> sync
|
||||
{
|
||||
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.");
|
||||
Weaver.fail = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (Weaver.IsDerivedFrom(resolvedField, Weaver.ScriptableObjectType))
|
||||
if (resolvedField.IsDerivedFrom(Weaver.ScriptableObjectType))
|
||||
{
|
||||
Log.Error("SyncVar [" + fd.FullName + "] cannot be derived from ScriptableObject.");
|
||||
Weaver.fail = true;
|
||||
|
@ -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)
|
||||
{
|
||||
return IsDerivedFrom(td, NetworkBehaviourType);
|
||||
return td.IsDerivedFrom(NetworkBehaviourType);
|
||||
}
|
||||
|
||||
public static bool ImplementsInterface(TypeDefinition td, TypeReference baseInterface)
|
||||
@ -1507,7 +1471,7 @@ public static bool IsValidTypeToGenerate(TypeDefinition variable)
|
||||
|
||||
static void CheckMonoBehaviour(TypeDefinition td)
|
||||
{
|
||||
if (IsDerivedFrom(td, MonoBehaviourType))
|
||||
if (td.IsDerivedFrom(MonoBehaviourType))
|
||||
{
|
||||
MonoBehaviourProcessor.Process(td);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user