MonoBehaviourProcessor.cs
3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Linq;
using Mono.Cecil;
namespace Unity.UNetWeaver
{
class MonoBehaviourProcessor
{
TypeDefinition m_td;
Weaver m_Weaver;
public MonoBehaviourProcessor(TypeDefinition td, Weaver weaver)
{
m_td = td;
m_Weaver = weaver;
}
public void Process()
{
ProcessSyncVars();
ProcessMethods();
}
void ProcessSyncVars()
{
// find syncvars
foreach (FieldDefinition fd in m_td.Fields)
{
foreach (var ca in fd.CustomAttributes)
{
if (ca.AttributeType.FullName == m_Weaver.SyncVarType.FullName)
{
Log.Error("Script " + m_td.FullName + " uses [SyncVar] " + fd.Name + " but is not a NetworkBehaviour.");
m_Weaver.fail = true;
}
}
if (Helpers.InheritsFromSyncList(fd.FieldType, m_Weaver))
{
Log.Error(string.Format("Script {0} defines field {1} with type {2}, but it's not a NetworkBehaviour", m_td.FullName, fd.Name, Helpers.PrettyPrintType(fd.FieldType)));
m_Weaver.fail = true;
}
}
}
void ProcessMethods()
{
// find command and RPC functions
foreach (MethodDefinition md in m_td.Methods)
{
foreach (var ca in md.CustomAttributes)
{
if (ca.AttributeType.FullName == m_Weaver.CommandType.FullName)
{
Log.Error("Script " + m_td.FullName + " uses [Command] " + md.Name + " but is not a NetworkBehaviour.");
m_Weaver.fail = true;
}
if (ca.AttributeType.FullName == m_Weaver.ClientRpcType.FullName)
{
Log.Error("Script " + m_td.FullName + " uses [ClientRpc] " + md.Name + " but is not a NetworkBehaviour.");
m_Weaver.fail = true;
}
if (ca.AttributeType.FullName == m_Weaver.TargetRpcType.FullName)
{
Log.Error("Script " + m_td.FullName + " uses [TargetRpc] " + md.Name + " but is not a NetworkBehaviour.");
m_Weaver.fail = true;
}
var attrName = ca.Constructor.DeclaringType.ToString();
if (attrName == "UnityEngine.Networking.ServerAttribute")
{
Log.Error("Script " + m_td.FullName + " uses the attribute [Server] on the method " + md.Name + " but is not a NetworkBehaviour.");
m_Weaver.fail = true;
}
else if (attrName == "UnityEngine.Networking.ServerCallbackAttribute")
{
Log.Error("Script " + m_td.FullName + " uses the attribute [ServerCallback] on the method " + md.Name + " but is not a NetworkBehaviour.");
m_Weaver.fail = true;
}
else if (attrName == "UnityEngine.Networking.ClientAttribute")
{
Log.Error("Script " + m_td.FullName + " uses the attribute [Client] on the method " + md.Name + " but is not a NetworkBehaviour.");
m_Weaver.fail = true;
}
else if (attrName == "UnityEngine.Networking.ClientCallbackAttribute")
{
Log.Error("Script " + m_td.FullName + " uses the attribute [ClientCallback] on the method " + md.Name + " but is not a NetworkBehaviour.");
m_Weaver.fail = true;
}
}
}
}
};
}