Skip to content

Commit 24cebfb

Browse files
feat: expose network behaviour type name internally (#1057)
* feat: expose the name of the network behaviour without runtime alloc * Added test to verify that the name is exposed correctly * removed unnecessary using * code review feedback * fixed bad comment
1 parent fffc11f commit 24cebfb

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,27 @@ private void ProcessNetworkBehaviour(TypeDefinition typeDefinition, string[] ass
592592
instructions.Reverse();
593593
instructions.ForEach(instruction => processor.Body.Instructions.Insert(0, instruction));
594594
}
595+
596+
// override NetworkBehaviour.__getTypeName() method to return concrete type
597+
{
598+
var baseType = typeDefinition.BaseType.Resolve();
599+
var baseGetTypeNameMethod = baseType.Methods.First(p => p.Name.Equals(nameof(NetworkBehaviour.__getTypeName)));
600+
601+
var newGetTypeNameMethod = new MethodDefinition(
602+
nameof(NetworkBehaviour.__getTypeName),
603+
(baseGetTypeNameMethod.Attributes & ~MethodAttributes.NewSlot) | MethodAttributes.ReuseSlot,
604+
baseGetTypeNameMethod.ReturnType)
605+
{
606+
ImplAttributes = baseGetTypeNameMethod.ImplAttributes,
607+
SemanticsAttributes = baseGetTypeNameMethod.SemanticsAttributes
608+
};
609+
610+
var processor = newGetTypeNameMethod.Body.GetILProcessor();
611+
processor.Body.Instructions.Add(processor.Create(OpCodes.Ldstr, typeDefinition.Name));
612+
processor.Body.Instructions.Add(processor.Create(OpCodes.Ret));
613+
614+
typeDefinition.Methods.Add(newGetTypeNameMethod);
615+
}
595616
}
596617

597618
private CustomAttribute CheckAndGetRpcAttribute(MethodDefinition methodDefinition)

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,18 @@ private static void SetUpdateStage<T>(ref T param) where T : IHasUpdateStage
3535
}
3636
}
3737

38+
#pragma warning disable IDE1006 // disable naming rule violation check
39+
// NetworkBehaviourILPP will override this in derived classes to return the name of the concrete type
40+
internal virtual string __getTypeName() => nameof(NetworkBehaviour);
41+
#pragma warning restore IDE1006 // restore naming rule violation check
42+
3843
#pragma warning disable 414 // disable assigned but its value is never used
3944
#pragma warning disable IDE1006 // disable naming rule violation check
4045
[NonSerialized]
4146
// RuntimeAccessModifiersILPP will make this `protected`
4247
internal __RpcExecStage __rpc_exec_stage = __RpcExecStage.None;
4348
#pragma warning restore 414 // restore assigned but its value is never used
44-
#pragma warning restore IDE1006 // restore naming rule violation
49+
#pragma warning restore IDE1006 // restore naming rule violation check
4550

4651
#pragma warning disable IDE1006 // disable naming rule violation check
4752
// RuntimeAccessModifiersILPP will make this `protected`

com.unity.netcode.gameobjects/Tests/Editor/NetworkBehaviourTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ public void AccessNetworkObjectTest()
4646
Object.DestroyImmediate(gameObject);
4747
}
4848

49+
[Test]
50+
public void GetNetworkBehaviourNameTest()
51+
{
52+
var gameObject = new GameObject(nameof(GetNetworkBehaviourNameTest));
53+
var networkBehaviour = gameObject.AddComponent<EmptyNetworkBehaviour>();
54+
55+
Assert.AreEqual(nameof(EmptyNetworkBehaviour), networkBehaviour.__getTypeName());
56+
}
57+
4958
public class EmptyNetworkBehaviour : NetworkBehaviour
5059
{
5160

0 commit comments

Comments
 (0)