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:
James Frowen 2020-05-27 18:09:35 +01:00 committed by GitHub
parent 3d04f97c8d
commit 64533ca4da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 36 deletions

View File

@ -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;
bool isNetworkConnection = p.ParameterType.FullName == Weaver.NetworkConnectionType.FullName;
if (isTargetRpc && isFirstParam)
// TargetRPC is an exception to this rule and can have a NetworkConnection as first parameter
if (isNetworkConnection && !(isTargetRpc && isFirstParam))
{
if (p.ParameterType.FullName != Weaver.NetworkConnectionType.FullName)
{
Weaver.Error($"{md.Name} must have NetworkConnection as the first parameter", md);
return false;
}
Weaver.Error($"{md.Name} has invalid parameter {p}. Cannot pass NeworkConnections", md);
return false;
}
else
{
if (p.ParameterType.FullName == Weaver.NetworkConnectionType.FullName)
{
Weaver.Error($"{md.Name} has invalid parameter {p}. Cannot pass NeworkConnections", md);
return false;
}
}
}
return true;
}

View File

@ -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" />

View File

@ -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))"));
}
}

View File

@ -1,10 +0,0 @@
using Mirror;
namespace WeaverTargetRpcTests.ErrorWhenTargetRpcIsMissingNetworkConnection
{
class ErrorWhenTargetRpcIsMissingNetworkConnection : NetworkBehaviour
{
[TargetRpc]
void TargetRpcMethod() { }
}
}

View File

@ -0,0 +1,10 @@
using Mirror;
namespace WeaverTargetRpcTests.TargetRpcCanHaveOtherParametersWhileSkipingNetworkConnection
{
class TargetRpcCanHaveOtherParametersWhileSkipingNetworkConnection : NetworkBehaviour
{
[TargetRpc]
void TargetRpcMethod(int usefulNumber) { }
}
}

View File

@ -0,0 +1,10 @@
using Mirror;
namespace WeaverTargetRpcTests.TargetRpcCanSkipNetworkConnection
{
class TargetRpcCanSkipNetworkConnection : NetworkBehaviour
{
[TargetRpc]
void TargetRpcMethod() { }
}
}