From b5eb487c6510e9a93bcb488d389a86394267eb94 Mon Sep 17 00:00:00 2001 From: Paul Pacheco Date: Tue, 1 Jan 2019 17:27:49 -0600 Subject: [PATCH] Fix issue #219 (#220) * Fix issue #219 pass the invoker class from the weaver when invoking rpcs so that it can produce the same hash they registered --- Mirror/Runtime/NetworkBehaviour.cs | 16 ++++++++-------- Mirror/Weaver/Processors/CommandProcessor.cs | 4 +++- .../Processors/NetworkBehaviourProcessor.cs | 6 +++--- Mirror/Weaver/Processors/RpcProcessor.cs | 6 ++++-- Mirror/Weaver/Processors/SyncEventProcessor.cs | 6 ++++-- Mirror/Weaver/Processors/TargetRpcProcessor.cs | 6 ++++-- 6 files changed, 26 insertions(+), 18 deletions(-) diff --git a/Mirror/Runtime/NetworkBehaviour.cs b/Mirror/Runtime/NetworkBehaviour.cs index d07ca1681..15458cb6a 100644 --- a/Mirror/Runtime/NetworkBehaviour.cs +++ b/Mirror/Runtime/NetworkBehaviour.cs @@ -77,7 +77,7 @@ protected void InitSyncObject(SyncObject syncObject) // ----------------------------- Commands -------------------------------- [EditorBrowsable(EditorBrowsableState.Never)] - protected void SendCommandInternal(string cmdName, NetworkWriter writer, int channelId) + protected void SendCommandInternal(Type invokeClass, string cmdName, NetworkWriter writer, int channelId) { // local players can always send commands, regardless of authority, other objects must have authority. if (!(isLocalPlayer || hasAuthority)) @@ -96,7 +96,7 @@ protected void SendCommandInternal(string cmdName, NetworkWriter writer, int cha CommandMessage message = new CommandMessage(); message.netId = netId; message.componentIndex = ComponentIndex; - message.cmdHash = (GetType() + ":" + cmdName).GetStableHashCode(); // type+func so Inventory.RpcUse != Equipment.RpcUse + message.cmdHash = (invokeClass + ":" + cmdName).GetStableHashCode(); // type+func so Inventory.RpcUse != Equipment.RpcUse message.payload = writer.ToArray(); ClientScene.readyConnection.Send((short)MsgType.Command, message, channelId); @@ -111,7 +111,7 @@ public virtual bool InvokeCommand(int cmdHash, NetworkReader reader) // ----------------------------- Client RPCs -------------------------------- [EditorBrowsable(EditorBrowsableState.Never)] - protected void SendRPCInternal(string rpcName, NetworkWriter writer, int channelId) + protected void SendRPCInternal(Type invokeClass, string rpcName, NetworkWriter writer, int channelId) { // This cannot use NetworkServer.active, as that is not specific to this object. if (!isServer) @@ -124,14 +124,14 @@ protected void SendRPCInternal(string rpcName, NetworkWriter writer, int channel RpcMessage message = new RpcMessage(); message.netId = netId; message.componentIndex = ComponentIndex; - message.rpcHash = (GetType() + ":" + rpcName).GetStableHashCode(); // type+func so Inventory.RpcUse != Equipment.RpcUse + message.rpcHash = (invokeClass + ":" + rpcName).GetStableHashCode(); // type+func so Inventory.RpcUse != Equipment.RpcUse message.payload = writer.ToArray(); NetworkServer.SendToReady(gameObject, (short)MsgType.Rpc, message, channelId); } [EditorBrowsable(EditorBrowsableState.Never)] - protected void SendTargetRPCInternal(NetworkConnection conn, string rpcName, NetworkWriter writer, int channelId) + protected void SendTargetRPCInternal(NetworkConnection conn, Type invokeClass, string rpcName, NetworkWriter writer, int channelId) { // This cannot use NetworkServer.active, as that is not specific to this object. if (!isServer) @@ -144,7 +144,7 @@ protected void SendTargetRPCInternal(NetworkConnection conn, string rpcName, Net RpcMessage message = new RpcMessage(); message.netId = netId; message.componentIndex = ComponentIndex; - message.rpcHash = (GetType() + ":" + rpcName).GetStableHashCode(); // type+func so Inventory.RpcUse != Equipment.RpcUse + message.rpcHash = (invokeClass + ":" + rpcName).GetStableHashCode(); // type+func so Inventory.RpcUse != Equipment.RpcUse message.payload = writer.ToArray(); conn.Send((short)MsgType.Rpc, message, channelId); @@ -159,7 +159,7 @@ public virtual bool InvokeRPC(int rpcHash, NetworkReader reader) // ----------------------------- Sync Events -------------------------------- [EditorBrowsable(EditorBrowsableState.Never)] - protected void SendEventInternal(string eventName, NetworkWriter writer, int channelId) + protected void SendEventInternal(Type invokeClass, string eventName, NetworkWriter writer, int channelId) { if (!NetworkServer.active) { @@ -171,7 +171,7 @@ protected void SendEventInternal(string eventName, NetworkWriter writer, int cha SyncEventMessage message = new SyncEventMessage(); message.netId = netId; message.componentIndex = ComponentIndex; - message.eventHash = (GetType() + ":" + eventName).GetStableHashCode(); // type+func so Inventory.RpcUse != Equipment.RpcUse + message.eventHash = (invokeClass + ":" + eventName).GetStableHashCode(); // type+func so Inventory.RpcUse != Equipment.RpcUse message.payload = writer.ToArray(); NetworkServer.SendToReady(gameObject, (short)MsgType.SyncEvent, message, channelId); diff --git a/Mirror/Weaver/Processors/CommandProcessor.cs b/Mirror/Weaver/Processors/CommandProcessor.cs index 4bf6c0d1b..d95f9ad59 100644 --- a/Mirror/Weaver/Processors/CommandProcessor.cs +++ b/Mirror/Weaver/Processors/CommandProcessor.cs @@ -32,7 +32,7 @@ public void CallCmdThrust(float thrusting, int spin) base.SendCommandInternal(cmdName, networkWriter, cmdName); } */ - public static MethodDefinition ProcessCommandCall(MethodDefinition md, CustomAttribute ca) + public static MethodDefinition ProcessCommandCall(TypeDefinition td, MethodDefinition md, CustomAttribute ca) { MethodDefinition cmd = new MethodDefinition("Call" + md.Name, MethodAttributes.Public | MethodAttributes.HideBySig, @@ -89,6 +89,8 @@ public static MethodDefinition ProcessCommandCall(MethodDefinition md, CustomAtt // invoke interal send and return cmdWorker.Append(cmdWorker.Create(OpCodes.Ldarg_0)); // load 'base.' to call the SendCommand function with + cmdWorker.Append(cmdWorker.Create(OpCodes.Ldtoken, td)); + cmdWorker.Append(cmdWorker.Create(OpCodes.Call, Weaver.getTypeFromHandleReference)); // invokerClass cmdWorker.Append(cmdWorker.Create(OpCodes.Ldstr, cmdName)); cmdWorker.Append(cmdWorker.Create(OpCodes.Ldloc_0)); // writer cmdWorker.Append(cmdWorker.Create(OpCodes.Ldc_I4, NetworkBehaviourProcessor.GetChannelId(ca))); diff --git a/Mirror/Weaver/Processors/NetworkBehaviourProcessor.cs b/Mirror/Weaver/Processors/NetworkBehaviourProcessor.cs index d26d4041d..21f346ead 100644 --- a/Mirror/Weaver/Processors/NetworkBehaviourProcessor.cs +++ b/Mirror/Weaver/Processors/NetworkBehaviourProcessor.cs @@ -797,7 +797,7 @@ void ProcessMethods() m_CmdInvocationFuncs.Add(cmdFunc); } - MethodDefinition cmdCallFunc = CommandProcessor.ProcessCommandCall(md, ca); + MethodDefinition cmdCallFunc = CommandProcessor.ProcessCommandCall(m_td, md, ca); if (cmdCallFunc != null) { m_CmdCallFuncs.Add(cmdCallFunc); @@ -827,7 +827,7 @@ void ProcessMethods() m_TargetRpcInvocationFuncs.Add(rpcFunc); } - MethodDefinition rpcCallFunc = TargetRpcProcessor.ProcessTargetRpcCall(md, ca); + MethodDefinition rpcCallFunc = TargetRpcProcessor.ProcessTargetRpcCall(m_td, md, ca); if (rpcCallFunc != null) { m_TargetRpcCallFuncs.Add(rpcCallFunc); @@ -857,7 +857,7 @@ void ProcessMethods() m_RpcInvocationFuncs.Add(rpcFunc); } - MethodDefinition rpcCallFunc = RpcProcessor.ProcessRpcCall(md, ca); + MethodDefinition rpcCallFunc = RpcProcessor.ProcessRpcCall(m_td, md, ca); if (rpcCallFunc != null) { m_RpcCallFuncs.Add(rpcCallFunc); diff --git a/Mirror/Weaver/Processors/RpcProcessor.cs b/Mirror/Weaver/Processors/RpcProcessor.cs index b52eaec3f..ddbd3ba35 100644 --- a/Mirror/Weaver/Processors/RpcProcessor.cs +++ b/Mirror/Weaver/Processors/RpcProcessor.cs @@ -43,11 +43,11 @@ public void CallRpcTest (int param) } else { NetworkWriter writer = new NetworkWriter (); writer.WritePackedUInt32((uint)param); - base.SendRPCInternal("RpcTest", writer, 0); + base.SendRPCInternal(typeof(class),"RpcTest", writer, 0); } } */ - public static MethodDefinition ProcessRpcCall(MethodDefinition md, CustomAttribute ca) + public static MethodDefinition ProcessRpcCall(TypeDefinition td, MethodDefinition md, CustomAttribute ca) { MethodDefinition rpc = new MethodDefinition("Call" + md.Name, MethodAttributes.Public | MethodAttributes.HideBySig, @@ -81,6 +81,8 @@ public static MethodDefinition ProcessRpcCall(MethodDefinition md, CustomAttribu // invoke SendInternal and return rpcWorker.Append(rpcWorker.Create(OpCodes.Ldarg_0)); // this + rpcWorker.Append(rpcWorker.Create(OpCodes.Ldtoken, td)); + rpcWorker.Append(rpcWorker.Create(OpCodes.Call, Weaver.getTypeFromHandleReference)); // invokerClass rpcWorker.Append(rpcWorker.Create(OpCodes.Ldstr, rpcName)); rpcWorker.Append(rpcWorker.Create(OpCodes.Ldloc_0)); // writer rpcWorker.Append(rpcWorker.Create(OpCodes.Ldc_I4, NetworkBehaviourProcessor.GetChannelId(ca))); diff --git a/Mirror/Weaver/Processors/SyncEventProcessor.cs b/Mirror/Weaver/Processors/SyncEventProcessor.cs index f38ae773a..667923533 100644 --- a/Mirror/Weaver/Processors/SyncEventProcessor.cs +++ b/Mirror/Weaver/Processors/SyncEventProcessor.cs @@ -64,7 +64,7 @@ public static MethodDefinition ProcessEventInvoke(TypeDefinition td, EventDefini return cmd; } - public static MethodDefinition ProcessEventCall(EventDefinition ed, CustomAttribute ca) + public static MethodDefinition ProcessEventCall(TypeDefinition td, EventDefinition ed, CustomAttribute ca) { MethodReference invoke = Weaver.ResolveMethod(ed.EventType, "Invoke"); MethodDefinition evt = new MethodDefinition("Call" + ed.Name, MethodAttributes.Public | @@ -91,6 +91,8 @@ public static MethodDefinition ProcessEventCall(EventDefinition ed, CustomAttrib // invoke interal send and return evtWorker.Append(evtWorker.Create(OpCodes.Ldarg_0)); // this + evtWorker.Append(evtWorker.Create(OpCodes.Ldtoken, td)); + evtWorker.Append(evtWorker.Create(OpCodes.Call, Weaver.getTypeFromHandleReference)); // invokerClass evtWorker.Append(evtWorker.Create(OpCodes.Ldstr, ed.Name)); evtWorker.Append(evtWorker.Create(OpCodes.Ldloc_0)); // writer evtWorker.Append(evtWorker.Create(OpCodes.Ldc_I4, NetworkBehaviourProcessor.GetChannelId(ca))); @@ -136,7 +138,7 @@ public static void ProcessEvents(TypeDefinition td, List events Weaver.DLog(td, "ProcessEvent " + ed); - MethodDefinition eventCallFunc = SyncEventProcessor.ProcessEventCall(ed, ca); + MethodDefinition eventCallFunc = SyncEventProcessor.ProcessEventCall(td, ed, ca); td.Methods.Add(eventCallFunc); Weaver.lists.replacedEvents.Add(ed); diff --git a/Mirror/Weaver/Processors/TargetRpcProcessor.cs b/Mirror/Weaver/Processors/TargetRpcProcessor.cs index e3366ef2b..9e4c431e7 100644 --- a/Mirror/Weaver/Processors/TargetRpcProcessor.cs +++ b/Mirror/Weaver/Processors/TargetRpcProcessor.cs @@ -49,11 +49,11 @@ public void CallTargetTest (NetworkConnection conn, int param) } else { NetworkWriter writer = new NetworkWriter (); writer.WritePackedUInt32 ((uint)param); - base.SendTargetRPCInternal (conn, "TargetTest", val); + base.SendTargetRPCInternal (conn, typeof(class), "TargetTest", val); } } */ - public static MethodDefinition ProcessTargetRpcCall(MethodDefinition md, CustomAttribute ca) + public static MethodDefinition ProcessTargetRpcCall(TypeDefinition td, MethodDefinition md, CustomAttribute ca) { MethodDefinition rpc = new MethodDefinition("Call" + md.Name, MethodAttributes.Public | MethodAttributes.HideBySig, @@ -100,6 +100,8 @@ public static MethodDefinition ProcessTargetRpcCall(MethodDefinition md, CustomA // invoke SendInternal and return rpcWorker.Append(rpcWorker.Create(OpCodes.Ldarg_0)); // this rpcWorker.Append(rpcWorker.Create(OpCodes.Ldarg_1)); // connection + rpcWorker.Append(rpcWorker.Create(OpCodes.Ldtoken, td)); + rpcWorker.Append(rpcWorker.Create(OpCodes.Call, Weaver.getTypeFromHandleReference)); // invokerClass rpcWorker.Append(rpcWorker.Create(OpCodes.Ldstr, rpcName)); rpcWorker.Append(rpcWorker.Create(OpCodes.Ldloc_0)); // writer rpcWorker.Append(rpcWorker.Create(OpCodes.Ldc_I4, NetworkBehaviourProcessor.GetChannelId(ca)));