mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 11:00:32 +00:00
Fixing optional network connection (#1934)
* adding tests for optional NetworkConnection * fixing validate to allow optional NetworkConnection * fixing error message in test
This commit is contained in:
parent
3d04f97c8d
commit
64533ca4da
@ -797,11 +797,6 @@ public static bool ProcessMethodsValidateFunction(MethodReference md)
|
||||
public static bool ProcessMethodsValidateParameters(MethodReference md, CustomAttribute ca)
|
||||
{
|
||||
bool isTargetRpc = ca.AttributeType.FullName == Weaver.TargetRpcType.FullName;
|
||||
if (isTargetRpc && md.Parameters.Count == 0)
|
||||
{
|
||||
Weaver.Error($"{md.Name} must have NetworkConnection as the first parameter", md);
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < md.Parameters.Count; ++i)
|
||||
{
|
||||
@ -816,27 +811,18 @@ public static bool ProcessMethodsValidateParameters(MethodReference md, CustomAt
|
||||
Weaver.Error($"{md.Name} cannot have optional parameters", md);
|
||||
return false;
|
||||
}
|
||||
// TargetRPC is an exception to this rule and can have a NetworkConnection as first parameter
|
||||
bool isFirstParam = i == 0;
|
||||
|
||||
if (isTargetRpc && isFirstParam)
|
||||
{
|
||||
if (p.ParameterType.FullName != Weaver.NetworkConnectionType.FullName)
|
||||
{
|
||||
Weaver.Error($"{md.Name} must have NetworkConnection as the first parameter", md);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (p.ParameterType.FullName == Weaver.NetworkConnectionType.FullName)
|
||||
bool isFirstParam = i == 0;
|
||||
bool isNetworkConnection = p.ParameterType.FullName == Weaver.NetworkConnectionType.FullName;
|
||||
|
||||
// TargetRPC is an exception to this rule and can have a NetworkConnection as first parameter
|
||||
if (isNetworkConnection && !(isTargetRpc && isFirstParam))
|
||||
{
|
||||
Weaver.Error($"{md.Name} has invalid parameter {p}. Cannot pass NeworkConnections", md);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -214,7 +214,8 @@
|
||||
<Compile Include="WeaverSyncEventTests~\ErrorWhenSyncEventDoesntStartWithEvent.cs" />
|
||||
<Compile Include="WeaverSyncEventTests~\SyncEventValid.cs" />
|
||||
<Compile Include="WeaverTargetRpcTests~\ErrorWhenTargetRpcIsStatic.cs" />
|
||||
<Compile Include="WeaverTargetRpcTests~\ErrorWhenTargetRpcIsMissingNetworkConnection.cs" />
|
||||
<Compile Include="WeaverTargetRpcTests~\TargetRpcCanHaveOtherParametersWhileSkipingNetworkConnection.cs" />
|
||||
<Compile Include="WeaverTargetRpcTests~\TargetRpcCanSkipNetworkConnection.cs" />
|
||||
<Compile Include="WeaverTargetRpcTests~\ErrorWhenNetworkConnectionIsNotTheFirstParameter.cs" />
|
||||
<Compile Include="WeaverTargetRpcTests~\ErrorWhenMethodDoesNotStartWithTarget.cs" />
|
||||
<Compile Include="WeaverTargetRpcTests~\TargetRpcValid.cs" />
|
||||
|
@ -25,16 +25,21 @@ public void ErrorWhenTargetRpcIsStatic()
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ErrorWhenTargetRpcIsMissingNetworkConnection()
|
||||
public void TargetRpcCanSkipNetworkConnection()
|
||||
{
|
||||
Assert.That(weaverErrors, Contains.Item("TargetRpcMethod must have NetworkConnection as the first parameter " +
|
||||
"(at System.Void WeaverTargetRpcTests.ErrorWhenTargetRpcIsMissingNetworkConnection.ErrorWhenTargetRpcIsMissingNetworkConnection::TargetRpcMethod())"));
|
||||
Assert.That(weaverErrors, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TargetRpcCanHaveOtherParametersWhileSkipingNetworkConnection()
|
||||
{
|
||||
Assert.That(weaverErrors, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ErrorWhenNetworkConnectionIsNotTheFirstParameter()
|
||||
{
|
||||
Assert.That(weaverErrors, Contains.Item("TargetRpcMethod must have NetworkConnection as the first parameter " +
|
||||
Assert.That(weaverErrors, Contains.Item($"TargetRpcMethod has invalid parameter nc. Cannot pass NeworkConnections " +
|
||||
"(at System.Void WeaverTargetRpcTests.ErrorWhenNetworkConnectionIsNotTheFirstParameter.ErrorWhenNetworkConnectionIsNotTheFirstParameter::TargetRpcMethod(System.Int32,Mirror.NetworkConnection))"));
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +0,0 @@
|
||||
using Mirror;
|
||||
|
||||
namespace WeaverTargetRpcTests.ErrorWhenTargetRpcIsMissingNetworkConnection
|
||||
{
|
||||
class ErrorWhenTargetRpcIsMissingNetworkConnection : NetworkBehaviour
|
||||
{
|
||||
[TargetRpc]
|
||||
void TargetRpcMethod() { }
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
using Mirror;
|
||||
|
||||
namespace WeaverTargetRpcTests.TargetRpcCanHaveOtherParametersWhileSkipingNetworkConnection
|
||||
{
|
||||
class TargetRpcCanHaveOtherParametersWhileSkipingNetworkConnection : NetworkBehaviour
|
||||
{
|
||||
[TargetRpc]
|
||||
void TargetRpcMethod(int usefulNumber) { }
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
using Mirror;
|
||||
|
||||
namespace WeaverTargetRpcTests.TargetRpcCanSkipNetworkConnection
|
||||
{
|
||||
class TargetRpcCanSkipNetworkConnection : NetworkBehaviour
|
||||
{
|
||||
[TargetRpc]
|
||||
void TargetRpcMethod() { }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user