mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 11:00:32 +00:00
[TargetRPC] optional connection parameter for easier usage while still allowing for previous usage (#592)
* [TargetRPC] optional connection parameter for easier usage while still allowing for previous usage * allow for parameterless targetrpc too * syntax * Add Helpers.ReaderParameters change from HLAPI 2019.2 package just to be sure
This commit is contained in:
parent
c0aaf23742
commit
4193574864
@ -87,7 +87,7 @@ public static string PrettyPrintType(TypeReference type)
|
|||||||
|
|
||||||
public static ReaderParameters ReaderParameters(string assemblyPath, IEnumerable<string> extraPaths, IAssemblyResolver assemblyResolver, string unityEngineDLLPath, string mirrorNetDLLPath)
|
public static ReaderParameters ReaderParameters(string assemblyPath, IEnumerable<string> extraPaths, IAssemblyResolver assemblyResolver, string unityEngineDLLPath, string mirrorNetDLLPath)
|
||||||
{
|
{
|
||||||
ReaderParameters parameters = new ReaderParameters();
|
ReaderParameters parameters = new ReaderParameters() {ReadWrite = true};
|
||||||
if (assemblyResolver == null)
|
if (assemblyResolver == null)
|
||||||
assemblyResolver = new DefaultAssemblyResolver();
|
assemblyResolver = new DefaultAssemblyResolver();
|
||||||
AddSearchDirectoryHelper helper = new AddSearchDirectoryHelper(assemblyResolver);
|
AddSearchDirectoryHelper helper = new AddSearchDirectoryHelper(assemblyResolver);
|
||||||
|
@ -8,6 +8,13 @@ public static class TargetRpcProcessor
|
|||||||
{
|
{
|
||||||
const string k_TargetRpcPrefix = "InvokeTargetRpc";
|
const string k_TargetRpcPrefix = "InvokeTargetRpc";
|
||||||
|
|
||||||
|
// helper functions to check if the method has a NetworkConnection parameter
|
||||||
|
public static bool HasNetworkConnectionParameter(MethodDefinition md)
|
||||||
|
{
|
||||||
|
return md.Parameters.Count > 0 &&
|
||||||
|
md.Parameters[0].ParameterType.FullName == Weaver.NetworkConnectionType.FullName;
|
||||||
|
}
|
||||||
|
|
||||||
public static MethodDefinition ProcessTargetRpcInvoke(TypeDefinition td, MethodDefinition md)
|
public static MethodDefinition ProcessTargetRpcInvoke(TypeDefinition td, MethodDefinition md)
|
||||||
{
|
{
|
||||||
MethodDefinition rpc = new MethodDefinition(RpcProcessor.k_RpcPrefix + md.Name, MethodAttributes.Family |
|
MethodDefinition rpc = new MethodDefinition(RpcProcessor.k_RpcPrefix + md.Name, MethodAttributes.Family |
|
||||||
@ -24,10 +31,16 @@ public static MethodDefinition ProcessTargetRpcInvoke(TypeDefinition td, MethodD
|
|||||||
rpcWorker.Append(rpcWorker.Create(OpCodes.Ldarg_0));
|
rpcWorker.Append(rpcWorker.Create(OpCodes.Ldarg_0));
|
||||||
rpcWorker.Append(rpcWorker.Create(OpCodes.Castclass, td));
|
rpcWorker.Append(rpcWorker.Create(OpCodes.Castclass, td));
|
||||||
|
|
||||||
//ClientScene.readyconnection
|
// NetworkConnection parameter is optional
|
||||||
rpcWorker.Append(rpcWorker.Create(OpCodes.Call, Weaver.ReadyConnectionReference));
|
bool hasNetworkConnection = HasNetworkConnectionParameter(md);
|
||||||
|
if (hasNetworkConnection)
|
||||||
|
{
|
||||||
|
//ClientScene.readyconnection
|
||||||
|
rpcWorker.Append(rpcWorker.Create(OpCodes.Call, Weaver.ReadyConnectionReference));
|
||||||
|
}
|
||||||
|
|
||||||
if (!NetworkBehaviourProcessor.ProcessNetworkReaderParameters(td, md, rpcWorker, true))
|
// process reader parameters and skip first one if first one is NetworkConnection
|
||||||
|
if (!NetworkBehaviourProcessor.ProcessNetworkReaderParameters(td, md, rpcWorker, hasNetworkConnection))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
// invoke actual command function
|
// invoke actual command function
|
||||||
@ -46,6 +59,14 @@ public void CallTargetTest (NetworkConnection conn, int param)
|
|||||||
writer.WritePackedUInt32 ((uint)param);
|
writer.WritePackedUInt32 ((uint)param);
|
||||||
base.SendTargetRPCInternal (conn, typeof(class), "TargetTest", val);
|
base.SendTargetRPCInternal (conn, typeof(class), "TargetTest", val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
or if optional:
|
||||||
|
public void CallTargetTest (int param)
|
||||||
|
{
|
||||||
|
NetworkWriter writer = new NetworkWriter ();
|
||||||
|
writer.WritePackedUInt32 ((uint)param);
|
||||||
|
base.SendTargetRPCInternal (null, typeof(class), "TargetTest", val);
|
||||||
|
}
|
||||||
*/
|
*/
|
||||||
public static MethodDefinition ProcessTargetRpcCall(TypeDefinition td, MethodDefinition md, CustomAttribute ca)
|
public static MethodDefinition ProcessTargetRpcCall(TypeDefinition td, MethodDefinition md, CustomAttribute ca)
|
||||||
{
|
{
|
||||||
@ -53,7 +74,7 @@ public static MethodDefinition ProcessTargetRpcCall(TypeDefinition td, MethodDef
|
|||||||
MethodAttributes.HideBySig,
|
MethodAttributes.HideBySig,
|
||||||
Weaver.voidType);
|
Weaver.voidType);
|
||||||
|
|
||||||
// add paramters
|
// add parameters
|
||||||
foreach (ParameterDefinition pd in md.Parameters)
|
foreach (ParameterDefinition pd in md.Parameters)
|
||||||
{
|
{
|
||||||
rpc.Parameters.Add(new ParameterDefinition(pd.Name, ParameterAttributes.None, pd.ParameterType));
|
rpc.Parameters.Add(new ParameterDefinition(pd.Name, ParameterAttributes.None, pd.ParameterType));
|
||||||
@ -65,8 +86,12 @@ public static MethodDefinition ProcessTargetRpcCall(TypeDefinition td, MethodDef
|
|||||||
|
|
||||||
NetworkBehaviourProcessor.WriteCreateWriter(rpcWorker);
|
NetworkBehaviourProcessor.WriteCreateWriter(rpcWorker);
|
||||||
|
|
||||||
|
// NetworkConnection parameter is optional
|
||||||
|
bool hasNetworkConnection = HasNetworkConnectionParameter(md);
|
||||||
|
|
||||||
// write all the arguments that the user passed to the TargetRpc call
|
// write all the arguments that the user passed to the TargetRpc call
|
||||||
if (!NetworkBehaviourProcessor.WriteArguments(rpcWorker, md, "TargetRPC", true))
|
// (skip first one if first one is NetworkConnection)
|
||||||
|
if (!NetworkBehaviourProcessor.WriteArguments(rpcWorker, md, "TargetRPC", hasNetworkConnection))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
var rpcName = md.Name;
|
var rpcName = md.Name;
|
||||||
@ -78,7 +103,14 @@ public static MethodDefinition ProcessTargetRpcCall(TypeDefinition td, MethodDef
|
|||||||
|
|
||||||
// invoke SendInternal and return
|
// invoke SendInternal and return
|
||||||
rpcWorker.Append(rpcWorker.Create(OpCodes.Ldarg_0)); // this
|
rpcWorker.Append(rpcWorker.Create(OpCodes.Ldarg_0)); // this
|
||||||
rpcWorker.Append(rpcWorker.Create(OpCodes.Ldarg_1)); // connection
|
if (HasNetworkConnectionParameter(md))
|
||||||
|
{
|
||||||
|
rpcWorker.Append(rpcWorker.Create(OpCodes.Ldarg_1)); // connection
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rpcWorker.Append(rpcWorker.Create(OpCodes.Ldnull)); // null
|
||||||
|
}
|
||||||
rpcWorker.Append(rpcWorker.Create(OpCodes.Ldtoken, td));
|
rpcWorker.Append(rpcWorker.Create(OpCodes.Ldtoken, td));
|
||||||
rpcWorker.Append(rpcWorker.Create(OpCodes.Call, Weaver.getTypeFromHandleReference)); // invokerClass
|
rpcWorker.Append(rpcWorker.Create(OpCodes.Call, Weaver.getTypeFromHandleReference)); // invokerClass
|
||||||
rpcWorker.Append(rpcWorker.Create(OpCodes.Ldstr, rpcName));
|
rpcWorker.Append(rpcWorker.Create(OpCodes.Ldstr, rpcName));
|
||||||
@ -113,18 +145,6 @@ public static bool ProcessMethodsValidateTargetRpc(TypeDefinition td, MethodDefi
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (md.Parameters.Count < 1)
|
|
||||||
{
|
|
||||||
Weaver.Error("Target Rpc function [" + td.FullName + ":" + md.Name + "] must have a NetworkConnection as the first parameter");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (md.Parameters[0].ParameterType.FullName != Weaver.NetworkConnectionType.FullName)
|
|
||||||
{
|
|
||||||
Weaver.Error("Target Rpc function [" + td.FullName + ":" + md.Name + "] first parameter must be a NetworkConnection");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// validate
|
// validate
|
||||||
return NetworkBehaviourProcessor.ProcessMethodsValidateParameters(td, md, ca, "Target Rpc");
|
return NetworkBehaviourProcessor.ProcessMethodsValidateParameters(td, md, ca, "Target Rpc");
|
||||||
}
|
}
|
||||||
|
@ -151,6 +151,11 @@ protected void SendTargetRPCInternal(NetworkConnection conn, Type invokeClass, s
|
|||||||
Debug.LogError("TargetRPC Function " + rpcName + " called on client.");
|
Debug.LogError("TargetRPC Function " + rpcName + " called on client.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// connection parameter is optional. assign if null.
|
||||||
|
if (conn == null)
|
||||||
|
{
|
||||||
|
conn = connectionToClient;
|
||||||
|
}
|
||||||
// this was in Weaver before
|
// this was in Weaver before
|
||||||
if (conn is ULocalConnectionToServer)
|
if (conn is ULocalConnectionToServer)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user