Merge pull request #201 from vis2k/weaver_improvements

Weaver improvements
This commit is contained in:
vis2k 2018-12-31 19:37:37 +01:00 committed by GitHub
commit e6d16b68fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -68,6 +68,13 @@ public void Process()
Weaver.DLog(m_td, "Process Done"); Weaver.DLog(m_td, "Process Done");
} }
/*
generates code like:
if (!NetworkClient.active)
Debug.LogError((object) "Command function CmdRespawn called on server.");
which is used in InvokeCmd, InvokeRpc, etc.
*/
public static void WriteClientActiveCheck(ILProcessor worker, string mdName, Instruction label, string errString) public static void WriteClientActiveCheck(ILProcessor worker, string mdName, Instruction label, string errString)
{ {
// client active check // client active check
@ -79,7 +86,11 @@ public static void WriteClientActiveCheck(ILProcessor worker, string mdName, Ins
worker.Append(worker.Create(OpCodes.Ret)); worker.Append(worker.Create(OpCodes.Ret));
worker.Append(label); worker.Append(label);
} }
/*
generates code like:
if (!NetworkServer.active)
Debug.LogError((object) "Command CmdMsgWhisper called on client.");
*/
public static void WriteServerActiveCheck(ILProcessor worker, string mdName, Instruction label, string errString) public static void WriteServerActiveCheck(ILProcessor worker, string mdName, Instruction label, string errString)
{ {
// server active check // server active check
@ -133,6 +144,8 @@ public static bool WriteArguments(ILProcessor worker, MethodDefinition md, strin
return true; return true;
} }
// adds empty UNetVersion(), which seems to be used to check if we already
// processed a class or not
void ProcessVersion() void ProcessVersion()
{ {
foreach (MethodDefinition md in m_td.Methods) foreach (MethodDefinition md in m_td.Methods)
@ -231,32 +244,24 @@ void GenerateConstants()
ILProcessor ctorWorker = ctor.Body.GetILProcessor(); ILProcessor ctorWorker = ctor.Body.GetILProcessor();
ILProcessor cctorWorker = cctor.Body.GetILProcessor(); ILProcessor cctorWorker = cctor.Body.GetILProcessor();
int cmdIndex = 0; for (int i = 0; i < m_Cmds.Count; ++i)
foreach (MethodDefinition md in m_Cmds)
{ {
GenerateCommandDelegate(cctorWorker, Weaver.registerCommandDelegateReference, m_CmdInvocationFuncs[cmdIndex], md.Name); GenerateRegisterCommandDelegate(cctorWorker, Weaver.registerCommandDelegateReference, m_CmdInvocationFuncs[i], m_Cmds[i].Name);
cmdIndex += 1;
} }
int rpcIndex = 0; for (int i = 0; i < m_Rpcs.Count; ++i)
foreach (MethodDefinition md in m_Rpcs)
{ {
GenerateCommandDelegate(cctorWorker, Weaver.registerRpcDelegateReference, m_RpcInvocationFuncs[rpcIndex], md.Name); GenerateRegisterCommandDelegate(cctorWorker, Weaver.registerRpcDelegateReference, m_RpcInvocationFuncs[i], m_Rpcs[i].Name);
rpcIndex += 1;
} }
int targetRpcIndex = 0; for (int i = 0; i < m_TargetRpcs.Count; ++i)
foreach (MethodDefinition md in m_TargetRpcs)
{ {
GenerateCommandDelegate(cctorWorker, Weaver.registerRpcDelegateReference, m_TargetRpcInvocationFuncs[targetRpcIndex], md.Name); GenerateRegisterCommandDelegate(cctorWorker, Weaver.registerRpcDelegateReference, m_TargetRpcInvocationFuncs[i], m_TargetRpcs[i].Name);
targetRpcIndex += 1;
} }
int eventIndex = 0; for (int i = 0; i < m_Events.Count; ++i)
foreach (EventDefinition ed in m_Events)
{ {
GenerateCommandDelegate(cctorWorker, Weaver.registerEventDelegateReference, m_EventInvocationFuncs[eventIndex], ed.Name); GenerateRegisterCommandDelegate(cctorWorker, Weaver.registerEventDelegateReference, m_EventInvocationFuncs[i], m_Events[i].Name);
eventIndex += 1;
} }
foreach (FieldDefinition fd in m_SyncObjects) foreach (FieldDefinition fd in m_SyncObjects)
@ -278,6 +283,7 @@ void GenerateConstants()
m_td.Attributes = m_td.Attributes & ~TypeAttributes.BeforeFieldInit; m_td.Attributes = m_td.Attributes & ~TypeAttributes.BeforeFieldInit;
} }
// generates 'syncListInt = new SyncListInt()' if user didn't do that yet
void GenerateSyncListInstanceInitializer(ILProcessor ctorWorker, FieldDefinition fd) void GenerateSyncListInstanceInitializer(ILProcessor ctorWorker, FieldDefinition fd)
{ {
// check the ctor's instructions for an Stfld op-code for this specific sync list field. // check the ctor's instructions for an Stfld op-code for this specific sync list field.
@ -308,7 +314,7 @@ void GenerateSyncListInstanceInitializer(ILProcessor ctorWorker, FieldDefinition
// This generates code like: // This generates code like:
NetworkBehaviour.RegisterCommandDelegate(base.GetType(), "CmdThrust", new NetworkBehaviour.CmdDelegate(ShipControl.InvokeCmdCmdThrust)); NetworkBehaviour.RegisterCommandDelegate(base.GetType(), "CmdThrust", new NetworkBehaviour.CmdDelegate(ShipControl.InvokeCmdCmdThrust));
*/ */
void GenerateCommandDelegate(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, m_td));
awakeWorker.Append(awakeWorker.Create(OpCodes.Call, Weaver.getTypeFromHandleReference)); awakeWorker.Append(awakeWorker.Create(OpCodes.Call, Weaver.getTypeFromHandleReference));
@ -337,7 +343,7 @@ void GenerateSerialization()
{ {
Weaver.DLog(m_td, " GenerateSerialization"); Weaver.DLog(m_td, " GenerateSerialization");
foreach (var m in m_td.Methods) foreach (MethodDefinition m in m_td.Methods)
{ {
if (m.Name == "OnSerialize") if (m.Name == "OnSerialize")
return; return;