Skip to content

Commit

Permalink
feat: allow for generic NetworkBehaviors (#519)
Browse files Browse the repository at this point in the history
This would allow network behaviours to be used together with generics.
It doesn't allow for the generic to be used in a syncvar or RPC methods. Also removed Test that checks if generic behaviours are blocked.
  • Loading branch information
Hertzole committed Jan 5, 2021
1 parent 0ecfcea commit 2858ff4
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ public bool Process()
}
Weaver.DLog(netBehaviourSubclass, "Found NetworkBehaviour " + netBehaviourSubclass.FullName);

if (netBehaviourSubclass.HasGenericParameters)
{
Weaver.Error($"{netBehaviourSubclass.Name} cannot have generic parameters", netBehaviourSubclass);
// originally Process returned true in every case, except if already processed.
// maybe return false here in the future.
return true;
}
Weaver.DLog(netBehaviourSubclass, "Process Start");
MarkAsProcessed(netBehaviourSubclass);

Expand Down
6 changes: 6 additions & 0 deletions Assets/Mirror/Editor/Weaver/Processors/RpcProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ static bool ValidateParameter(MethodReference method, ParameterDefinition param,
return false;
}

if (param.ParameterType.IsGenericParameter)
{
Weaver.Error($"{method.Name} cannot have generic parameters", method);
return false;
}

if (IsNetworkConnection(param.ParameterType))
{
if (callType == RemoteCallType.ClientRpc && firstParam)
Expand Down
5 changes: 5 additions & 0 deletions Assets/Mirror/Editor/Weaver/Processors/SyncObjectProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public void ProcessSyncObjects(TypeDefinition td)
{
foreach (FieldDefinition fd in td.Fields)
{
if (fd.FieldType.IsGenericParameter) // Just ignore all generic objects.
{
continue;
}

if (fd.FieldType.Resolve().ImplementsInterface<ISyncObject>())
{
if (fd.IsStatic)
Expand Down
6 changes: 6 additions & 0 deletions Assets/Mirror/Editor/Weaver/Processors/SyncVarProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,12 @@ public void ProcessSyncVars(TypeDefinition td)
continue;
}

if (fd.FieldType.IsGenericParameter)
{
Weaver.Error($"{fd.Name} cannot be synced since it's a generic parameter", fd);
continue;
}

if ((fd.Attributes & FieldAttributes.Static) != 0)
{
Weaver.Error($"{fd.Name} cannot be static", fd);
Expand Down
23 changes: 21 additions & 2 deletions Assets/Tests/Editor/Weaver/WeaverNetworkBehaviourTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,20 @@ public void NetworkBehaviourAbstractBaseValid()
[Test]
public void NetworkBehaviourGeneric()
{
HasError("NetworkBehaviourGeneric`1 cannot have generic parameters",
"WeaverNetworkBehaviourTests.NetworkBehaviourGeneric.NetworkBehaviourGeneric`1");
IsSuccess();
}

[Test]
public void NetworkBehaviourGenericInherit()
{
IsSuccess();
}

[Test]
public void NetworkBehaviourCmdGenericArgument()
{
HasError("CmdCantHaveGeneric cannot have generic parameters",
"System.Void WeaverNetworkBehaviourTests.NetworkBehaviourCmdGenericArgument.NetworkBehaviourCmdGenericArgument`1::CmdCantHaveGeneric(T)");
}

[Test]
Expand All @@ -44,6 +56,13 @@ public void NetworkBehaviourCmdVoidReturn()
"System.Int32 WeaverNetworkBehaviourTests.NetworkBehaviourCmdVoidReturn.NetworkBehaviourCmdVoidReturn::CmdCantHaveNonVoidReturn()");
}

[Test]
public void NetworkBehaviourClientRpcGenericArgument()
{
HasError("RpcCantHaveGeneric cannot have generic parameters",
"System.Void WeaverNetworkBehaviourTests.NetworkBehaviourClientRpcGenericArgument.NetworkBehaviourClientRpcGenericArgument`1::RpcCantHaveGeneric(T)");
}

[Test]
public void NetworkBehaviourClientRpcGenericParam()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Mirror;

namespace WeaverNetworkBehaviourTests.NetworkBehaviourClientRpcGenericArgument
{
class NetworkBehaviourClientRpcGenericArgument<T> : NetworkBehaviour
{
[ClientRpc]
public void RpcCantHaveGeneric(T arg) { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Mirror;

namespace WeaverNetworkBehaviourTests.NetworkBehaviourCmdGenericArgument
{
class NetworkBehaviourCmdGenericArgument<T> : NetworkBehaviour
{
[ServerRpc]
public void CmdCantHaveGeneric(T arg) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace WeaverNetworkBehaviourTests.NetworkBehaviourGeneric
{
class NetworkBehaviourGeneric<T> : NetworkBehaviour
{
T genericsNotAllowed;
T genericAllowed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Mirror;

namespace WeaverNetworkBehaviourTests.NetworkBehaviourGenericInherit
{
class NetworkBehaviourGenericInherit<T> : NetworkBehaviour
{
protected T generic;
}

class NetworkBehaviourGenericChild : NetworkBehaviourGenericInherit<NetworkBehaviourGenericChild> { }
}
7 changes: 7 additions & 0 deletions Assets/Tests/Editor/Weaver/WeaverSyncVarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public void SyncVarsStatic()
"System.Int32 WeaverSyncVarTests.SyncVarsStatic.SyncVarsStatic::invalidVar");
}

[Test]
public void SyncVarsGenericField()
{
HasError("invalidVar cannot be synced since it's a generic parameter",
"T WeaverSyncVarTests.SyncVarGenericFields.SyncVarGenericFields`1::invalidVar");
}

[Test]
public void SyncVarsGenericParam()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Mirror;

namespace WeaverSyncVarTests.SyncVarGenericFields
{
class SyncVarGenericFields<T> : NetworkBehaviour
{
[SyncVar]
T invalidVar;
}
}

0 comments on commit 2858ff4

Please sign in to comment.