adding RemoteCallType (#1936)

* adding RemoteCallType

* Update Assets/Mirror/Editor/Weaver/Processors/NetworkBehaviourProcessor.cs
This commit is contained in:
James Frowen 2020-05-30 14:42:34 +01:00 committed by GitHub
parent 7205d3a705
commit b1e47d5b6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 29 deletions

View File

@ -123,7 +123,7 @@ public static MethodDefinition ProcessCommandInvoke(TypeDefinition td, MethodDef
return cmd;
}
public static bool ProcessMethodsValidateCommand(MethodDefinition md, CustomAttribute commandAttr)
public static bool ProcessMethodsValidateCommand(MethodDefinition md)
{
if (!md.Name.StartsWith("Cmd"))
{
@ -139,7 +139,7 @@ public static bool ProcessMethodsValidateCommand(MethodDefinition md, CustomAttr
// validate
return NetworkBehaviourProcessor.ProcessMethodsValidateFunction(md) &&
NetworkBehaviourProcessor.ProcessMethodsValidateParameters(md, commandAttr);
NetworkBehaviourProcessor.ProcessMethodsValidateParameters(md, RemoteCallType.Command);
}
}
}

View File

@ -4,6 +4,13 @@
namespace Mirror.Weaver
{
public enum RemoteCallType
{
Command,
ClientRpc,
TargetRpc,
}
/// <summary>
/// processes SyncVars, Cmds, Rpcs, etc. of NetworkBehaviours
/// </summary>
@ -820,34 +827,46 @@ public static bool ProcessMethodsValidateFunction(MethodReference md)
return true;
}
public static bool ProcessMethodsValidateParameters(MethodReference md, CustomAttribute ca)
public static bool ProcessMethodsValidateParameters(MethodReference method, RemoteCallType callType)
{
bool isTargetRpc = ca.AttributeType.FullName == Weaver.TargetRpcType.FullName;
for (int i = 0; i < md.Parameters.Count; ++i)
for (int i = 0; i < method.Parameters.Count; ++i)
{
ParameterDefinition p = md.Parameters[i];
if (p.IsOut)
{
Weaver.Error($"{md.Name} cannot have out parameters", md);
return false;
}
if (p.IsOptional)
{
Weaver.Error($"{md.Name} cannot have optional parameters", md);
return false;
}
ParameterDefinition param = method.Parameters[i];
bool isFirstParam = i == 0;
bool isNetworkConnection = p.ParameterType.FullName == Weaver.NetworkConnectionType.FullName;
bool valid = ValidateParameter(method, param, callType, i == 0);
// TargetRPC is an exception to this rule and can have a NetworkConnection as first parameter
if (isNetworkConnection && !(isTargetRpc && isFirstParam))
if (!valid)
{
Weaver.Error($"{md.Name} has invalid parameter {p}. Cannot pass NeworkConnections", md);
return false;
}
}
return true;
}
static bool ValidateParameter(MethodReference method, ParameterDefinition param, RemoteCallType callType, bool firstParam)
{
bool isNetworkConnection = param.ParameterType.FullName == Weaver.NetworkConnectionType.FullName;
if (param.IsOut)
{
Weaver.Error($"{method.Name} cannot have out parameters", method);
return false;
}
// TargetRPC is an exception to this rule and can have a NetworkConnection as first parameter
if (isNetworkConnection && !(callType == RemoteCallType.TargetRpc && firstParam))
{
Weaver.Error($"{method.Name} has invalid parameter {param}. Cannot pass NeworkConnections", method);
return false;
}
// sender connection can be optional
if (param.IsOptional)
{
Weaver.Error($"{method.Name} cannot have optional parameters", method);
return false;
}
return true;
}
@ -886,7 +905,7 @@ void ProcessMethods()
void ProcessClientRpc(HashSet<string> names, MethodDefinition md, CustomAttribute clientRpcAttr)
{
if (!RpcProcessor.ProcessMethodsValidateRpc(md, clientRpcAttr))
if (!RpcProcessor.ProcessMethodsValidateRpc(md))
{
return;
}
@ -910,7 +929,7 @@ void ProcessClientRpc(HashSet<string> names, MethodDefinition md, CustomAttribut
void ProcessTargetRpc(HashSet<string> names, MethodDefinition md, CustomAttribute targetRpcAttr)
{
if (!TargetRpcProcessor.ProcessMethodsValidateTargetRpc(md, targetRpcAttr))
if (!TargetRpcProcessor.ProcessMethodsValidateTargetRpc(md))
return;
if (names.Contains(md.Name))
@ -932,7 +951,7 @@ void ProcessTargetRpc(HashSet<string> names, MethodDefinition md, CustomAttribut
void ProcessCommand(HashSet<string> names, MethodDefinition md, CustomAttribute commandAttr)
{
if (!CommandProcessor.ProcessMethodsValidateCommand(md, commandAttr))
if (!CommandProcessor.ProcessMethodsValidateCommand(md))
return;
if (names.Contains(md.Name))

View File

@ -100,7 +100,7 @@ public static MethodDefinition ProcessRpcCall(TypeDefinition td, MethodDefinitio
return rpc;
}
public static bool ProcessMethodsValidateRpc(MethodDefinition md, CustomAttribute clientRpcAttr)
public static bool ProcessMethodsValidateRpc(MethodDefinition md)
{
if (!md.Name.StartsWith("Rpc"))
{
@ -116,7 +116,7 @@ public static bool ProcessMethodsValidateRpc(MethodDefinition md, CustomAttribut
// validate
return NetworkBehaviourProcessor.ProcessMethodsValidateFunction(md) &&
NetworkBehaviourProcessor.ProcessMethodsValidateParameters(md, clientRpcAttr);
NetworkBehaviourProcessor.ProcessMethodsValidateParameters(md, RemoteCallType.ClientRpc);
}
}
}

View File

@ -141,7 +141,7 @@ public static MethodDefinition ProcessTargetRpcCall(TypeDefinition td, MethodDef
return rpc;
}
public static bool ProcessMethodsValidateTargetRpc(MethodDefinition md, CustomAttribute targetRpcAttr)
public static bool ProcessMethodsValidateTargetRpc(MethodDefinition md)
{
if (!md.Name.StartsWith("Target"))
{
@ -161,7 +161,7 @@ public static bool ProcessMethodsValidateTargetRpc(MethodDefinition md, CustomAt
}
// validate
return NetworkBehaviourProcessor.ProcessMethodsValidateParameters(md, targetRpcAttr);
return NetworkBehaviourProcessor.ProcessMethodsValidateParameters(md, RemoteCallType.TargetRpc);
}
}
}