What even is m_td? Rename it to something that can actually be read. (#709)

This commit is contained in:
rodolphito 2019-04-05 00:59:45 -07:00 committed by vis2k
parent 5f63256cd1
commit 4ee787b0eb

View File

@ -25,29 +25,29 @@ class NetworkBehaviourProcessor
readonly List<MethodDefinition> m_RpcCallFuncs = new List<MethodDefinition>(); readonly List<MethodDefinition> m_RpcCallFuncs = new List<MethodDefinition>();
readonly List<MethodDefinition> m_TargetRpcCallFuncs = new List<MethodDefinition>(); readonly List<MethodDefinition> m_TargetRpcCallFuncs = new List<MethodDefinition>();
readonly TypeDefinition m_td; readonly TypeDefinition netBehaviourSubclass;
public NetworkBehaviourProcessor(TypeDefinition td) public NetworkBehaviourProcessor(TypeDefinition td)
{ {
Weaver.DLog(td, "NetworkBehaviourProcessor"); Weaver.DLog(td, "NetworkBehaviourProcessor");
m_td = td; netBehaviourSubclass = td;
} }
public void Process() public void Process()
{ {
if (m_td.HasGenericParameters) if (netBehaviourSubclass.HasGenericParameters)
{ {
Weaver.Error("NetworkBehaviour " + m_td.Name + " cannot have generic parameters"); Weaver.Error("NetworkBehaviour " + netBehaviourSubclass.Name + " cannot have generic parameters");
return; return;
} }
Weaver.DLog(m_td, "Process Start"); Weaver.DLog(netBehaviourSubclass, "Process Start");
MarkAsProcessed(m_td); MarkAsProcessed(netBehaviourSubclass);
SyncVarProcessor.ProcessSyncVars(m_td, m_SyncVars, m_SyncObjects, m_SyncVarNetIds); SyncVarProcessor.ProcessSyncVars(netBehaviourSubclass, m_SyncVars, m_SyncObjects, m_SyncVarNetIds);
Weaver.ResetRecursionCount(); Weaver.ResetRecursionCount();
ProcessMethods(); ProcessMethods();
SyncEventProcessor.ProcessEvents(m_td, m_Events, m_EventInvocationFuncs); SyncEventProcessor.ProcessEvents(netBehaviourSubclass, m_Events, m_EventInvocationFuncs);
if (Weaver.WeavingFailed) if (Weaver.WeavingFailed)
{ {
return; return;
@ -62,7 +62,7 @@ public void Process()
} }
GenerateDeSerialization(); GenerateDeSerialization();
Weaver.DLog(m_td, "Process Done"); Weaver.DLog(netBehaviourSubclass, "Process Done");
} }
/* /*
@ -166,12 +166,12 @@ void GenerateConstants()
if (m_Cmds.Count == 0 && m_Rpcs.Count == 0 && m_TargetRpcs.Count == 0 && m_Events.Count == 0 && m_SyncObjects.Count == 0) if (m_Cmds.Count == 0 && m_Rpcs.Count == 0 && m_TargetRpcs.Count == 0 && m_Events.Count == 0 && m_SyncObjects.Count == 0)
return; return;
Weaver.DLog(m_td, " GenerateConstants "); Weaver.DLog(netBehaviourSubclass, " GenerateConstants ");
// find static constructor // find static constructor
MethodDefinition cctor = null; MethodDefinition cctor = null;
bool cctorFound = false; bool cctorFound = false;
foreach (MethodDefinition md in m_td.Methods) foreach (MethodDefinition md in netBehaviourSubclass.Methods)
{ {
if (md.Name == ".cctor") if (md.Name == ".cctor")
{ {
@ -191,7 +191,7 @@ void GenerateConstants()
} }
else else
{ {
Weaver.Error("No cctor for " + m_td.Name); Weaver.Error("No cctor for " + netBehaviourSubclass.Name);
return; return;
} }
} }
@ -210,7 +210,7 @@ void GenerateConstants()
// find instance constructor // find instance constructor
MethodDefinition ctor = null; MethodDefinition ctor = null;
foreach (MethodDefinition md in m_td.Methods) foreach (MethodDefinition md in netBehaviourSubclass.Methods)
{ {
if (md.Name == ".ctor") if (md.Name == ".ctor")
{ {
@ -223,7 +223,7 @@ void GenerateConstants()
} }
else else
{ {
Weaver.Error("No ctor for " + m_td.Name); Weaver.Error("No ctor for " + netBehaviourSubclass.Name);
return; return;
} }
@ -233,7 +233,7 @@ void GenerateConstants()
if (ctor == null) if (ctor == null)
{ {
Weaver.Error("No ctor for " + m_td.Name); Weaver.Error("No ctor for " + netBehaviourSubclass.Name);
return; return;
} }
@ -269,14 +269,14 @@ void GenerateConstants()
cctorWorker.Append(cctorWorker.Create(OpCodes.Ret)); cctorWorker.Append(cctorWorker.Create(OpCodes.Ret));
if (!cctorFound) if (!cctorFound)
{ {
m_td.Methods.Add(cctor); netBehaviourSubclass.Methods.Add(cctor);
} }
// finish ctor // finish ctor
ctorWorker.Append(ctorWorker.Create(OpCodes.Ret)); ctorWorker.Append(ctorWorker.Create(OpCodes.Ret));
// in case class had no cctor, it might have BeforeFieldInit, so injected cctor would be called too late // in case class had no cctor, it might have BeforeFieldInit, so injected cctor would be called too late
m_td.Attributes = m_td.Attributes & ~TypeAttributes.BeforeFieldInit; netBehaviourSubclass.Attributes = netBehaviourSubclass.Attributes & ~TypeAttributes.BeforeFieldInit;
} }
/* /*
@ -285,7 +285,7 @@ void GenerateConstants()
*/ */
void GenerateRegisterCommandDelegate(ILProcessor awakeWorker, MethodReference registerMethod, MethodDefinition func, string cmdName) void GenerateRegisterCommandDelegate(ILProcessor awakeWorker, MethodReference registerMethod, MethodDefinition func, string cmdName)
{ {
awakeWorker.Append(awakeWorker.Create(OpCodes.Ldtoken, m_td)); awakeWorker.Append(awakeWorker.Create(OpCodes.Ldtoken, netBehaviourSubclass));
awakeWorker.Append(awakeWorker.Create(OpCodes.Call, Weaver.getTypeFromHandleReference)); awakeWorker.Append(awakeWorker.Create(OpCodes.Call, Weaver.getTypeFromHandleReference));
awakeWorker.Append(awakeWorker.Create(OpCodes.Ldstr, cmdName)); awakeWorker.Append(awakeWorker.Create(OpCodes.Ldstr, cmdName));
awakeWorker.Append(awakeWorker.Create(OpCodes.Ldnull)); awakeWorker.Append(awakeWorker.Create(OpCodes.Ldnull));
@ -297,9 +297,9 @@ void GenerateRegisterCommandDelegate(ILProcessor awakeWorker, MethodReference re
void GenerateSerialization() void GenerateSerialization()
{ {
Weaver.DLog(m_td, " GenerateSerialization"); Weaver.DLog(netBehaviourSubclass, " GenerateSerialization");
foreach (MethodDefinition m in m_td.Methods) foreach (MethodDefinition m in netBehaviourSubclass.Methods)
{ {
if (m.Name == "OnSerialize") if (m.Name == "OnSerialize")
return; return;
@ -325,7 +325,7 @@ void GenerateSerialization()
VariableDefinition dirtyLocal = new VariableDefinition(Weaver.boolType); VariableDefinition dirtyLocal = new VariableDefinition(Weaver.boolType);
serialize.Body.Variables.Add(dirtyLocal); serialize.Body.Variables.Add(dirtyLocal);
MethodReference baseSerialize = Resolvers.ResolveMethodInParents(m_td.BaseType, Weaver.CurrentAssembly, "OnSerialize"); MethodReference baseSerialize = Resolvers.ResolveMethodInParents(netBehaviourSubclass.BaseType, Weaver.CurrentAssembly, "OnSerialize");
if (baseSerialize != null) if (baseSerialize != null)
{ {
serWorker.Append(serWorker.Create(OpCodes.Ldarg_0)); // base serWorker.Append(serWorker.Create(OpCodes.Ldarg_0)); // base
@ -353,7 +353,7 @@ void GenerateSerialization()
} }
else else
{ {
Weaver.Error("GenerateSerialization for " + m_td.Name + " unknown type [" + syncVar.FieldType + "]. Mirror [SyncVar] member variables must be basic types."); Weaver.Error("GenerateSerialization for " + netBehaviourSubclass.Name + " unknown type [" + syncVar.FieldType + "]. Mirror [SyncVar] member variables must be basic types.");
return; return;
} }
} }
@ -377,7 +377,7 @@ void GenerateSerialization()
// generate a writer call for any dirty variable in this class // generate a writer call for any dirty variable in this class
// start at number of syncvars in parent // start at number of syncvars in parent
int dirtyBit = Weaver.GetSyncVarStart(m_td.BaseType.FullName); int dirtyBit = Weaver.GetSyncVarStart(netBehaviourSubclass.BaseType.FullName);
foreach (FieldDefinition syncVar in m_SyncVars) foreach (FieldDefinition syncVar in m_SyncVars)
{ {
Instruction varLabel = serWorker.Create(OpCodes.Nop); Instruction varLabel = serWorker.Create(OpCodes.Nop);
@ -401,7 +401,7 @@ void GenerateSerialization()
} }
else else
{ {
Weaver.Error("GenerateSerialization for " + m_td.Name + " unknown type [" + syncVar.FieldType + "]. Mirror [SyncVar] member variables must be basic types."); Weaver.Error("GenerateSerialization for " + netBehaviourSubclass.Name + " unknown type [" + syncVar.FieldType + "]. Mirror [SyncVar] member variables must be basic types.");
return; return;
} }
@ -415,14 +415,14 @@ void GenerateSerialization()
if (Weaver.GenerateLogErrors) if (Weaver.GenerateLogErrors)
{ {
serWorker.Append(serWorker.Create(OpCodes.Ldstr, "Injected Serialize " + m_td.Name)); serWorker.Append(serWorker.Create(OpCodes.Ldstr, "Injected Serialize " + netBehaviourSubclass.Name));
serWorker.Append(serWorker.Create(OpCodes.Call, Weaver.logErrorReference)); serWorker.Append(serWorker.Create(OpCodes.Call, Weaver.logErrorReference));
} }
// generate: return dirtyLocal // generate: return dirtyLocal
serWorker.Append(serWorker.Create(OpCodes.Ldloc_0)); serWorker.Append(serWorker.Create(OpCodes.Ldloc_0));
serWorker.Append(serWorker.Create(OpCodes.Ret)); serWorker.Append(serWorker.Create(OpCodes.Ret));
m_td.Methods.Add(serialize); netBehaviourSubclass.Methods.Add(serialize);
} }
public static int GetChannelId(CustomAttribute ca) public static int GetChannelId(CustomAttribute ca)
@ -442,7 +442,7 @@ void DeserializeField(FieldDefinition syncVar, ILProcessor serWorker, MethodDefi
{ {
// check for Hook function // check for Hook function
MethodDefinition foundMethod; MethodDefinition foundMethod;
if (!SyncVarProcessor.CheckForHookFunction(m_td, syncVar, out foundMethod)) if (!SyncVarProcessor.CheckForHookFunction(netBehaviourSubclass, syncVar, out foundMethod))
{ {
return; return;
} }
@ -490,7 +490,7 @@ void DeserializeField(FieldDefinition syncVar, ILProcessor serWorker, MethodDefi
MethodReference readFunc = Weaver.GetReadFunc(syncVar.FieldType); MethodReference readFunc = Weaver.GetReadFunc(syncVar.FieldType);
if (readFunc == null) if (readFunc == null)
{ {
Weaver.Error("GenerateDeSerialization for " + m_td.Name + " unknown type [" + syncVar.FieldType + "]. Mirror [SyncVar] member variables must be basic types."); Weaver.Error("GenerateDeSerialization for " + netBehaviourSubclass.Name + " unknown type [" + syncVar.FieldType + "]. Mirror [SyncVar] member variables must be basic types.");
return; return;
} }
VariableDefinition tmpValue = new VariableDefinition(syncVar.FieldType); VariableDefinition tmpValue = new VariableDefinition(syncVar.FieldType);
@ -518,9 +518,9 @@ void DeserializeField(FieldDefinition syncVar, ILProcessor serWorker, MethodDefi
void GenerateDeSerialization() void GenerateDeSerialization()
{ {
Weaver.DLog(m_td, " GenerateDeSerialization"); Weaver.DLog(netBehaviourSubclass, " GenerateDeSerialization");
foreach (MethodDefinition m in m_td.Methods) foreach (MethodDefinition m in netBehaviourSubclass.Methods)
{ {
if (m.Name == "OnDeserialize") if (m.Name == "OnDeserialize")
return; return;
@ -544,7 +544,7 @@ void GenerateDeSerialization()
VariableDefinition dirtyBitsLocal = new VariableDefinition(Weaver.int64Type); VariableDefinition dirtyBitsLocal = new VariableDefinition(Weaver.int64Type);
serialize.Body.Variables.Add(dirtyBitsLocal); serialize.Body.Variables.Add(dirtyBitsLocal);
MethodReference baseDeserialize = Resolvers.ResolveMethodInParents(m_td.BaseType, Weaver.CurrentAssembly, "OnDeserialize"); MethodReference baseDeserialize = Resolvers.ResolveMethodInParents(netBehaviourSubclass.BaseType, Weaver.CurrentAssembly, "OnDeserialize");
if (baseDeserialize != null) if (baseDeserialize != null)
{ {
serWorker.Append(serWorker.Create(OpCodes.Ldarg_0)); // base serWorker.Append(serWorker.Create(OpCodes.Ldarg_0)); // base
@ -576,7 +576,7 @@ void GenerateDeSerialization()
serWorker.Append(serWorker.Create(OpCodes.Stloc_0)); serWorker.Append(serWorker.Create(OpCodes.Stloc_0));
// conditionally read each syncvar // conditionally read each syncvar
int dirtyBit = Weaver.GetSyncVarStart(m_td.BaseType.FullName); // start at number of syncvars in parent int dirtyBit = Weaver.GetSyncVarStart(netBehaviourSubclass.BaseType.FullName); // start at number of syncvars in parent
foreach (FieldDefinition syncVar in m_SyncVars) foreach (FieldDefinition syncVar in m_SyncVars)
{ {
Instruction varLabel = serWorker.Create(OpCodes.Nop); Instruction varLabel = serWorker.Create(OpCodes.Nop);
@ -595,12 +595,12 @@ void GenerateDeSerialization()
if (Weaver.GenerateLogErrors) if (Weaver.GenerateLogErrors)
{ {
serWorker.Append(serWorker.Create(OpCodes.Ldstr, "Injected Deserialize " + m_td.Name)); serWorker.Append(serWorker.Create(OpCodes.Ldstr, "Injected Deserialize " + netBehaviourSubclass.Name));
serWorker.Append(serWorker.Create(OpCodes.Call, Weaver.logErrorReference)); serWorker.Append(serWorker.Create(OpCodes.Call, Weaver.logErrorReference));
} }
serWorker.Append(serWorker.Create(OpCodes.Ret)); serWorker.Append(serWorker.Create(OpCodes.Ret));
m_td.Methods.Add(serialize); netBehaviourSubclass.Methods.Add(serialize);
} }
public static bool ProcessNetworkReaderParameters(TypeDefinition td, MethodDefinition md, ILProcessor worker, bool skipFirst) public static bool ProcessNetworkReaderParameters(TypeDefinition td, MethodDefinition md, ILProcessor worker, bool skipFirst)
@ -719,31 +719,31 @@ void ProcessMethods()
HashSet<string> names = new HashSet<string>(); HashSet<string> names = new HashSet<string>();
// find command and RPC functions // find command and RPC functions
foreach (MethodDefinition md in m_td.Methods) foreach (MethodDefinition md in netBehaviourSubclass.Methods)
{ {
Weaver.ResetRecursionCount(); Weaver.ResetRecursionCount();
foreach (CustomAttribute ca in md.CustomAttributes) foreach (CustomAttribute ca in md.CustomAttributes)
{ {
if (ca.AttributeType.FullName == Weaver.CommandType.FullName) if (ca.AttributeType.FullName == Weaver.CommandType.FullName)
{ {
if (!CommandProcessor.ProcessMethodsValidateCommand(m_td, md, ca)) if (!CommandProcessor.ProcessMethodsValidateCommand(netBehaviourSubclass, md, ca))
return; return;
if (names.Contains(md.Name)) if (names.Contains(md.Name))
{ {
Weaver.Error("Duplicate Command name [" + m_td.FullName + ":" + md.Name + "]"); Weaver.Error("Duplicate Command name [" + netBehaviourSubclass.FullName + ":" + md.Name + "]");
return; return;
} }
names.Add(md.Name); names.Add(md.Name);
m_Cmds.Add(md); m_Cmds.Add(md);
MethodDefinition cmdFunc = CommandProcessor.ProcessCommandInvoke(m_td, md); MethodDefinition cmdFunc = CommandProcessor.ProcessCommandInvoke(netBehaviourSubclass, md);
if (cmdFunc != null) if (cmdFunc != null)
{ {
m_CmdInvocationFuncs.Add(cmdFunc); m_CmdInvocationFuncs.Add(cmdFunc);
} }
MethodDefinition cmdCallFunc = CommandProcessor.ProcessCommandCall(m_td, md, ca); MethodDefinition cmdCallFunc = CommandProcessor.ProcessCommandCall(netBehaviourSubclass, md, ca);
if (cmdCallFunc != null) if (cmdCallFunc != null)
{ {
m_CmdCallFuncs.Add(cmdCallFunc); m_CmdCallFuncs.Add(cmdCallFunc);
@ -754,24 +754,24 @@ void ProcessMethods()
if (ca.AttributeType.FullName == Weaver.TargetRpcType.FullName) if (ca.AttributeType.FullName == Weaver.TargetRpcType.FullName)
{ {
if (!TargetRpcProcessor.ProcessMethodsValidateTargetRpc(m_td, md, ca)) if (!TargetRpcProcessor.ProcessMethodsValidateTargetRpc(netBehaviourSubclass, md, ca))
return; return;
if (names.Contains(md.Name)) if (names.Contains(md.Name))
{ {
Weaver.Error("Duplicate Target Rpc name [" + m_td.FullName + ":" + md.Name + "]"); Weaver.Error("Duplicate Target Rpc name [" + netBehaviourSubclass.FullName + ":" + md.Name + "]");
return; return;
} }
names.Add(md.Name); names.Add(md.Name);
m_TargetRpcs.Add(md); m_TargetRpcs.Add(md);
MethodDefinition rpcFunc = TargetRpcProcessor.ProcessTargetRpcInvoke(m_td, md); MethodDefinition rpcFunc = TargetRpcProcessor.ProcessTargetRpcInvoke(netBehaviourSubclass, md);
if (rpcFunc != null) if (rpcFunc != null)
{ {
m_TargetRpcInvocationFuncs.Add(rpcFunc); m_TargetRpcInvocationFuncs.Add(rpcFunc);
} }
MethodDefinition rpcCallFunc = TargetRpcProcessor.ProcessTargetRpcCall(m_td, md, ca); MethodDefinition rpcCallFunc = TargetRpcProcessor.ProcessTargetRpcCall(netBehaviourSubclass, md, ca);
if (rpcCallFunc != null) if (rpcCallFunc != null)
{ {
m_TargetRpcCallFuncs.Add(rpcCallFunc); m_TargetRpcCallFuncs.Add(rpcCallFunc);
@ -782,24 +782,24 @@ void ProcessMethods()
if (ca.AttributeType.FullName == Weaver.ClientRpcType.FullName) if (ca.AttributeType.FullName == Weaver.ClientRpcType.FullName)
{ {
if (!RpcProcessor.ProcessMethodsValidateRpc(m_td, md, ca)) if (!RpcProcessor.ProcessMethodsValidateRpc(netBehaviourSubclass, md, ca))
return; return;
if (names.Contains(md.Name)) if (names.Contains(md.Name))
{ {
Weaver.Error("Duplicate ClientRpc name [" + m_td.FullName + ":" + md.Name + "]"); Weaver.Error("Duplicate ClientRpc name [" + netBehaviourSubclass.FullName + ":" + md.Name + "]");
return; return;
} }
names.Add(md.Name); names.Add(md.Name);
m_Rpcs.Add(md); m_Rpcs.Add(md);
MethodDefinition rpcFunc = RpcProcessor.ProcessRpcInvoke(m_td, md); MethodDefinition rpcFunc = RpcProcessor.ProcessRpcInvoke(netBehaviourSubclass, md);
if (rpcFunc != null) if (rpcFunc != null)
{ {
m_RpcInvocationFuncs.Add(rpcFunc); m_RpcInvocationFuncs.Add(rpcFunc);
} }
MethodDefinition rpcCallFunc = RpcProcessor.ProcessRpcCall(m_td, md, ca); MethodDefinition rpcCallFunc = RpcProcessor.ProcessRpcCall(netBehaviourSubclass, md, ca);
if (rpcCallFunc != null) if (rpcCallFunc != null)
{ {
m_RpcCallFuncs.Add(rpcCallFunc); m_RpcCallFuncs.Add(rpcCallFunc);
@ -813,29 +813,29 @@ void ProcessMethods()
// cmds // cmds
foreach (MethodDefinition md in m_CmdInvocationFuncs) foreach (MethodDefinition md in m_CmdInvocationFuncs)
{ {
m_td.Methods.Add(md); netBehaviourSubclass.Methods.Add(md);
} }
foreach (MethodDefinition md in m_CmdCallFuncs) foreach (MethodDefinition md in m_CmdCallFuncs)
{ {
m_td.Methods.Add(md); netBehaviourSubclass.Methods.Add(md);
} }
// rpcs // rpcs
foreach (MethodDefinition md in m_RpcInvocationFuncs) foreach (MethodDefinition md in m_RpcInvocationFuncs)
{ {
m_td.Methods.Add(md); netBehaviourSubclass.Methods.Add(md);
} }
foreach (MethodDefinition md in m_TargetRpcInvocationFuncs) foreach (MethodDefinition md in m_TargetRpcInvocationFuncs)
{ {
m_td.Methods.Add(md); netBehaviourSubclass.Methods.Add(md);
} }
foreach (MethodDefinition md in m_RpcCallFuncs) foreach (MethodDefinition md in m_RpcCallFuncs)
{ {
m_td.Methods.Add(md); netBehaviourSubclass.Methods.Add(md);
} }
foreach (MethodDefinition md in m_TargetRpcCallFuncs) foreach (MethodDefinition md in m_TargetRpcCallFuncs)
{ {
m_td.Methods.Add(md); netBehaviourSubclass.Methods.Add(md);
} }
} }
} }