[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:
vis2k 2019-03-14 14:52:32 +01:00 committed by GitHub
parent c0aaf23742
commit 4193574864
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 19 deletions

View File

@ -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)
{
ReaderParameters parameters = new ReaderParameters();
ReaderParameters parameters = new ReaderParameters() {ReadWrite = true};
if (assemblyResolver == null)
assemblyResolver = new DefaultAssemblyResolver();
AddSearchDirectoryHelper helper = new AddSearchDirectoryHelper(assemblyResolver);

View File

@ -8,6 +8,13 @@ public static class TargetRpcProcessor
{
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)
{
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.Castclass, td));
// NetworkConnection parameter is optional
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;
// invoke actual command function
@ -46,6 +59,14 @@ public void CallTargetTest (NetworkConnection conn, int param)
writer.WritePackedUInt32 ((uint)param);
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)
{
@ -53,7 +74,7 @@ public static MethodDefinition ProcessTargetRpcCall(TypeDefinition td, MethodDef
MethodAttributes.HideBySig,
Weaver.voidType);
// add paramters
// add parameters
foreach (ParameterDefinition pd in md.Parameters)
{
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);
// NetworkConnection parameter is optional
bool hasNetworkConnection = HasNetworkConnectionParameter(md);
// 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;
var rpcName = md.Name;
@ -78,7 +103,14 @@ public static MethodDefinition ProcessTargetRpcCall(TypeDefinition td, MethodDef
// invoke SendInternal and return
rpcWorker.Append(rpcWorker.Create(OpCodes.Ldarg_0)); // this
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.Call, Weaver.getTypeFromHandleReference)); // invokerClass
rpcWorker.Append(rpcWorker.Create(OpCodes.Ldstr, rpcName));
@ -113,18 +145,6 @@ public static bool ProcessMethodsValidateTargetRpc(TypeDefinition td, MethodDefi
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
return NetworkBehaviourProcessor.ProcessMethodsValidateParameters(td, md, ca, "Target Rpc");
}

View File

@ -151,6 +151,11 @@ protected void SendTargetRPCInternal(NetworkConnection conn, Type invokeClass, s
Debug.LogError("TargetRPC Function " + rpcName + " called on client.");
return;
}
// connection parameter is optional. assign if null.
if (conn == null)
{
conn = connectionToClient;
}
// this was in Weaver before
if (conn is ULocalConnectionToServer)
{