From cf50a51d4f289b3b9387fd4c146da87bf9396020 Mon Sep 17 00:00:00 2001 From: Beck Sebenius Date: Mon, 16 Aug 2021 17:03:31 -0700 Subject: [PATCH 1/5] feat: expose the name of the network behaviour without runtime alloc --- .../Editor/CodeGen/NetworkBehaviourILPP.cs | 20 +++++++++++++++++++ .../Runtime/Core/NetworkBehaviour.cs | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs b/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs index 0b09d7e84a..870dab3d22 100644 --- a/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs +++ b/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs @@ -12,6 +12,7 @@ using MethodAttributes = Mono.Cecil.MethodAttributes; using ParameterAttributes = Mono.Cecil.ParameterAttributes; using ILPPInterface = Unity.CompilationPipeline.Common.ILPostProcessing.ILPostProcessor; +using PropertyAttributes = Mono.Cecil.PropertyAttributes; namespace Unity.Netcode.Editor.CodeGen { @@ -592,6 +593,25 @@ private void ProcessNetworkBehaviour(TypeDefinition typeDefinition, string[] ass instructions.Reverse(); instructions.ForEach(instruction => processor.Body.Instructions.Insert(0, instruction)); } + + // __getBehaviourName processor + { + var baseType = typeDefinition.BaseType.Resolve(); + var baseMethod = baseType.Methods.First(p => p.Name.Equals(nameof(NetworkBehaviour.__getBehaviourName))); + + var newMethod = new MethodDefinition( + nameof(NetworkBehaviour.__getBehaviourName), + (baseMethod.Attributes & ~MethodAttributes.NewSlot) | MethodAttributes.ReuseSlot, + baseMethod.ReturnType); + newMethod.ImplAttributes = baseMethod.ImplAttributes; + newMethod.SemanticsAttributes = baseMethod.SemanticsAttributes; + + var processor = newMethod.Body.GetILProcessor(); + processor.Body.Instructions.Add(processor.Create(OpCodes.Ldstr, typeDefinition.Name)); + processor.Body.Instructions.Add(processor.Create(OpCodes.Ret)); + + typeDefinition.Methods.Add(newMethod); + } } private CustomAttribute CheckAndGetRpcAttribute(MethodDefinition methodDefinition) diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs index 98824f00f5..4e872c8bb5 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs @@ -35,6 +35,10 @@ private static void SetUpdateStage(ref T param) where T : IHasUpdateStage } } +#pragma warning disable IDE1006 // disable naming rule violation check + internal virtual string __getBehaviourName() => nameof(NetworkBehaviour); +#pragma warning restore IDE1006 // restore naming rule violation + #pragma warning disable 414 // disable assigned but its value is never used #pragma warning disable IDE1006 // disable naming rule violation check [NonSerialized] From 036f8369d88ae7f5a71a68105e96a2e97b00e4be Mon Sep 17 00:00:00 2001 From: Beck Sebenius Date: Mon, 16 Aug 2021 17:36:50 -0700 Subject: [PATCH 2/5] Added test to verify that the name is exposed correctly --- .../Tests/Editor/NetworkBehaviourTests.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/com.unity.netcode.gameobjects/Tests/Editor/NetworkBehaviourTests.cs b/com.unity.netcode.gameobjects/Tests/Editor/NetworkBehaviourTests.cs index 60f10b23c4..7d529a8120 100644 --- a/com.unity.netcode.gameobjects/Tests/Editor/NetworkBehaviourTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Editor/NetworkBehaviourTests.cs @@ -46,6 +46,15 @@ public void AccessNetworkObjectTest() Object.DestroyImmediate(gameObject); } + [Test] + public void GetNetworkBehaviourNameTest() + { + var gameObject = new GameObject(nameof(GetNetworkBehaviourNameTest)); + var networkBehaviour = gameObject.AddComponent(); + + Assert.AreEqual(nameof(EmptyNetworkBehaviour), networkBehaviour.__getBehaviourName()); + } + public class EmptyNetworkBehaviour : NetworkBehaviour { From 4fcaf7652aeead3bcdfe73486d913fbe41de497a Mon Sep 17 00:00:00 2001 From: Beck Sebenius Date: Mon, 16 Aug 2021 17:47:46 -0700 Subject: [PATCH 3/5] removed unnecessary using --- .../Editor/CodeGen/NetworkBehaviourILPP.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs b/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs index 870dab3d22..58fbdaa612 100644 --- a/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs +++ b/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs @@ -12,7 +12,6 @@ using MethodAttributes = Mono.Cecil.MethodAttributes; using ParameterAttributes = Mono.Cecil.ParameterAttributes; using ILPPInterface = Unity.CompilationPipeline.Common.ILPostProcessing.ILPostProcessor; -using PropertyAttributes = Mono.Cecil.PropertyAttributes; namespace Unity.Netcode.Editor.CodeGen { From 811e49a5f4cb862b2a534b96e9290eb34a31419c Mon Sep 17 00:00:00 2001 From: Beck Sebenius Date: Tue, 17 Aug 2021 07:30:13 -0700 Subject: [PATCH 4/5] code review feedback --- .../Editor/CodeGen/NetworkBehaviourILPP.cs | 22 ++++++++++--------- .../Runtime/Core/NetworkBehaviour.cs | 7 +++--- .../Tests/Editor/NetworkBehaviourTests.cs | 2 +- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs b/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs index 58fbdaa612..8fc7febf54 100644 --- a/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs +++ b/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs @@ -593,23 +593,25 @@ private void ProcessNetworkBehaviour(TypeDefinition typeDefinition, string[] ass instructions.ForEach(instruction => processor.Body.Instructions.Insert(0, instruction)); } - // __getBehaviourName processor + // override NetworkBehaviour.__getBehaviourName() method to return concrete type { var baseType = typeDefinition.BaseType.Resolve(); - var baseMethod = baseType.Methods.First(p => p.Name.Equals(nameof(NetworkBehaviour.__getBehaviourName))); + var baseGetTypeNameMethod = baseType.Methods.First(p => p.Name.Equals(nameof(NetworkBehaviour.__getTypeName))); - var newMethod = new MethodDefinition( - nameof(NetworkBehaviour.__getBehaviourName), - (baseMethod.Attributes & ~MethodAttributes.NewSlot) | MethodAttributes.ReuseSlot, - baseMethod.ReturnType); - newMethod.ImplAttributes = baseMethod.ImplAttributes; - newMethod.SemanticsAttributes = baseMethod.SemanticsAttributes; + var newGetTypeNameMethod = new MethodDefinition( + nameof(NetworkBehaviour.__getTypeName), + (baseGetTypeNameMethod.Attributes & ~MethodAttributes.NewSlot) | MethodAttributes.ReuseSlot, + baseGetTypeNameMethod.ReturnType) + { + ImplAttributes = baseGetTypeNameMethod.ImplAttributes, + SemanticsAttributes = baseGetTypeNameMethod.SemanticsAttributes + }; - var processor = newMethod.Body.GetILProcessor(); + var processor = newGetTypeNameMethod.Body.GetILProcessor(); processor.Body.Instructions.Add(processor.Create(OpCodes.Ldstr, typeDefinition.Name)); processor.Body.Instructions.Add(processor.Create(OpCodes.Ret)); - typeDefinition.Methods.Add(newMethod); + typeDefinition.Methods.Add(newGetTypeNameMethod); } } diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs index e28e5b5165..d5c3692195 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs @@ -36,8 +36,9 @@ private static void SetUpdateStage(ref T param) where T : IHasUpdateStage } #pragma warning disable IDE1006 // disable naming rule violation check - internal virtual string __getBehaviourName() => nameof(NetworkBehaviour); -#pragma warning restore IDE1006 // restore naming rule violation + // NetworkBehaviourILPP will override this in derived classes to return the name of the concrete type + internal virtual string __getTypeName() => nameof(NetworkBehaviour); +#pragma warning restore IDE1006 // restore naming rule violation check #pragma warning disable 414 // disable assigned but its value is never used #pragma warning disable IDE1006 // disable naming rule violation check @@ -45,7 +46,7 @@ private static void SetUpdateStage(ref T param) where T : IHasUpdateStage // RuntimeAccessModifiersILPP will make this `protected` internal __RpcExecStage __rpc_exec_stage = __RpcExecStage.None; #pragma warning restore 414 // restore assigned but its value is never used -#pragma warning restore IDE1006 // restore naming rule violation +#pragma warning restore IDE1006 // restore naming rule violation check #pragma warning disable IDE1006 // disable naming rule violation check // RuntimeAccessModifiersILPP will make this `protected` diff --git a/com.unity.netcode.gameobjects/Tests/Editor/NetworkBehaviourTests.cs b/com.unity.netcode.gameobjects/Tests/Editor/NetworkBehaviourTests.cs index 7d529a8120..e6d9607702 100644 --- a/com.unity.netcode.gameobjects/Tests/Editor/NetworkBehaviourTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Editor/NetworkBehaviourTests.cs @@ -52,7 +52,7 @@ public void GetNetworkBehaviourNameTest() var gameObject = new GameObject(nameof(GetNetworkBehaviourNameTest)); var networkBehaviour = gameObject.AddComponent(); - Assert.AreEqual(nameof(EmptyNetworkBehaviour), networkBehaviour.__getBehaviourName()); + Assert.AreEqual(nameof(EmptyNetworkBehaviour), networkBehaviour.__getTypeName()); } public class EmptyNetworkBehaviour : NetworkBehaviour From 44ffa4f6e46ef4e94c631756918a50484e4b1f89 Mon Sep 17 00:00:00 2001 From: Beck Sebenius Date: Tue, 17 Aug 2021 08:22:00 -0700 Subject: [PATCH 5/5] fixed bad comment --- .../Editor/CodeGen/NetworkBehaviourILPP.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs b/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs index 8fc7febf54..9c9c45f586 100644 --- a/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs +++ b/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs @@ -593,7 +593,7 @@ private void ProcessNetworkBehaviour(TypeDefinition typeDefinition, string[] ass instructions.ForEach(instruction => processor.Body.Instructions.Insert(0, instruction)); } - // override NetworkBehaviour.__getBehaviourName() method to return concrete type + // override NetworkBehaviour.__getTypeName() method to return concrete type { var baseType = typeDefinition.BaseType.Resolve(); var baseGetTypeNameMethod = baseType.Methods.First(p => p.Name.Equals(nameof(NetworkBehaviour.__getTypeName)));