From 86e71e5ebff6195183c86d755c7c9219f8a3a927 Mon Sep 17 00:00:00 2001 From: Kitty Draper Date: Mon, 25 Sep 2023 16:20:32 -0500 Subject: [PATCH 1/4] fix: Errors with NetworkVariable and others in inspector Also fixes NetworkVariables with NonSerializedAttribute showing in editor when they should not. --- .../Editor/CodeGen/NetworkBehaviourILPP.cs | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs b/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs index 03999db128..be19acb07f 100644 --- a/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs +++ b/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs @@ -8,6 +8,9 @@ using Mono.Cecil.Rocks; using Unity.CompilationPipeline.Common.Diagnostics; using Unity.CompilationPipeline.Common.ILPostProcessing; +#if UNITY_EDITOR +using UnityEditor; +#endif using UnityEngine; using ILPPInterface = Unity.CompilationPipeline.Common.ILPostProcessing.ILPostProcessor; using MethodAttributes = Mono.Cecil.MethodAttributes; @@ -76,7 +79,7 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly) .ToList() .ForEach(b => ProcessNetworkBehaviour(b, compiledAssembly.Defines)); - CreateNetworkVariableTypeInitializers(assemblyDefinition); + CreateNetworkVariableTypeInitializers(assemblyDefinition, compiledAssembly.Defines); } catch (Exception e) { @@ -135,7 +138,7 @@ private bool IsSpecialCaseType(TypeReference type) return false; } - private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly) + private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly, string[] assemblyDefines) { var typeDefinition = new TypeDefinition("__GEN", "NetworkVariableSerializationHelper", TypeAttributes.NotPublic | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit, assembly.MainModule.TypeSystem.Object); @@ -145,7 +148,15 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly) MethodAttributes.Static, assembly.MainModule.TypeSystem.Void); staticCtorMethodDef.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); - staticCtorMethodDef.CustomAttributes.Add(new CustomAttribute(m_RuntimeInitializeOnLoadAttribute_Ctor)); + bool isEditor = assemblyDefines.Contains("UNITY_EDITOR"); + if (isEditor) + { + staticCtorMethodDef.CustomAttributes.Add(new CustomAttribute(m_InitializeOnLoadAttribute_Ctor)); + } + else + { + staticCtorMethodDef.CustomAttributes.Add(new CustomAttribute(m_RuntimeInitializeOnLoadAttribute_Ctor)); + } typeDefinition.Methods.Add(staticCtorMethodDef); @@ -382,6 +393,7 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly) private MethodReference m_NetworkVariableSerializationTypes_InitializeEqualityChecker_ManagedClassEquals_MethodRef; private MethodReference m_RuntimeInitializeOnLoadAttribute_Ctor; + private MethodReference m_InitializeOnLoadAttribute_Ctor; private MethodReference m_ExceptionCtorMethodReference; private MethodReference m_List_NetworkVariableBase_Add; @@ -496,6 +508,9 @@ private bool ImportReferences(ModuleDefinition moduleDefinition) } } +#if UNITY_EDITOR + m_InitializeOnLoadAttribute_Ctor = moduleDefinition.ImportReference(typeof(InitializeOnLoadMethodAttribute).GetConstructor(new Type[] { })); +#endif m_RuntimeInitializeOnLoadAttribute_Ctor = moduleDefinition.ImportReference(typeof(RuntimeInitializeOnLoadMethodAttribute).GetConstructor(new Type[] { })); TypeDefinition networkManagerTypeDef = null; From 84e50e8aad7774cb2a1304f4e2528617c6af8afe Mon Sep 17 00:00:00 2001 From: Kitty Draper Date: Mon, 25 Sep 2023 16:25:20 -0500 Subject: [PATCH 2/4] changelog --- com.unity.netcode.gameobjects/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index 1b8180ae57..dcb5d51bbc 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -14,6 +14,8 @@ Additional documentation and release notes are available at [Multiplayer Documen ### Fixed +- NetworkVariables of non-integer types will no longer break the inspector (#2714) +- NetworkVariables with NonSerializedAttribute will not appear in the inspector (#2714 - Fixed issue where `UnityTransport` would attempt to establish WebSocket connections even if using UDP/DTLS Relay allocations when the build target was WebGL. This only applied to working in the editor since UDP/DTLS can't work in the browser. (#2695) - Fixed issue where a `NetworkBehaviour` component's `OnNetworkDespawn` was not being invoked on the host-server side for an in-scene placed `NetworkObject` when a scene was unloaded (during a scene transition) and the `NetworkBehaviour` component was positioned/ordered before the `NetworkObject` component. (#2685) - Fixed issue where `SpawnWithObservers` was not being honored when `NetworkConfig.EnableSceneManagement` was disabled. (#2682) From 43f74b40f72a7cdca4cab0f20e19a0c1cde669ae Mon Sep 17 00:00:00 2001 From: Kitty Draper Date: Tue, 26 Sep 2023 15:15:52 -0500 Subject: [PATCH 3/4] trying a different tactic --- .../Editor/CodeGen/NetworkBehaviourILPP.cs | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs b/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs index be19acb07f..ff872c56dd 100644 --- a/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs +++ b/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs @@ -69,7 +69,7 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly) { m_MainModule = mainModule; - if (ImportReferences(mainModule)) + if (ImportReferences(mainModule, compiledAssembly.Defines)) { // process `NetworkBehaviour` types try @@ -207,10 +207,7 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly, equalityMethod = new GenericInstanceMethod(m_NetworkVariableSerializationTypes_InitializeEqualityChecker_UnmanagedValueEqualsArray_MethodRef); } - if (serializeMethod != null) - { - serializeMethod.GenericArguments.Add(wrappedType); - } + serializeMethod?.GenericArguments.Add(wrappedType); equalityMethod.GenericArguments.Add(wrappedType); } #if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT @@ -270,10 +267,7 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly, equalityMethod = new GenericInstanceMethod(m_NetworkVariableSerializationTypes_InitializeEqualityChecker_UnmanagedValueEquals_MethodRef); } - if (serializeMethod != null) - { - serializeMethod.GenericArguments.Add(type); - } + serializeMethod?.GenericArguments.Add(type); equalityMethod.GenericArguments.Add(type); } else @@ -307,10 +301,7 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly, equalityMethod = new GenericInstanceMethod(m_NetworkVariableSerializationTypes_InitializeEqualityChecker_ManagedClassEquals_MethodRef); } - if (serializeMethod != null) - { - serializeMethod.GenericArguments.Add(type); - } + serializeMethod?.GenericArguments.Add(type); equalityMethod.GenericArguments.Add(type); } @@ -496,7 +487,7 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly, // CodeGen cannot reference the collections assembly to do a typeof() on it due to a bug that causes that to crash. private const string k_INativeListBool_FullName = "Unity.Collections.INativeList`1"; - private bool ImportReferences(ModuleDefinition moduleDefinition) + private bool ImportReferences(ModuleDefinition moduleDefinition, string[] assemblyDefines) { TypeDefinition debugTypeDef = null; foreach (var unityTypeDef in m_UnityModule.GetAllTypes()) @@ -508,9 +499,13 @@ private bool ImportReferences(ModuleDefinition moduleDefinition) } } -#if UNITY_EDITOR - m_InitializeOnLoadAttribute_Ctor = moduleDefinition.ImportReference(typeof(InitializeOnLoadMethodAttribute).GetConstructor(new Type[] { })); -#endif + + bool isEditor = assemblyDefines.Contains("UNITY_EDITOR"); + if (isEditor) + { + m_InitializeOnLoadAttribute_Ctor = moduleDefinition.ImportReference(typeof(InitializeOnLoadMethodAttribute).GetConstructor(new Type[] { })); + } + m_RuntimeInitializeOnLoadAttribute_Ctor = moduleDefinition.ImportReference(typeof(RuntimeInitializeOnLoadMethodAttribute).GetConstructor(new Type[] { })); TypeDefinition networkManagerTypeDef = null; From c03127af0a3bfe3ae5807f0c803283f359324aff Mon Sep 17 00:00:00 2001 From: Kitty Draper Date: Fri, 29 Sep 2023 14:12:50 -0500 Subject: [PATCH 4/4] Missed a file in the commits. --- .../Editor/NetworkBehaviourEditor.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/com.unity.netcode.gameobjects/Editor/NetworkBehaviourEditor.cs b/com.unity.netcode.gameobjects/Editor/NetworkBehaviourEditor.cs index 84af9377dd..7d57afb055 100644 --- a/com.unity.netcode.gameobjects/Editor/NetworkBehaviourEditor.cs +++ b/com.unity.netcode.gameobjects/Editor/NetworkBehaviourEditor.cs @@ -37,12 +37,12 @@ private void Init(MonoScript script) for (int i = 0; i < fields.Length; i++) { var ft = fields[i].FieldType; - if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(NetworkVariable<>) && !fields[i].IsDefined(typeof(HideInInspector), true)) + if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(NetworkVariable<>) && !fields[i].IsDefined(typeof(HideInInspector), true) && !fields[i].IsDefined(typeof(NonSerializedAttribute), true)) { m_NetworkVariableNames.Add(ObjectNames.NicifyVariableName(fields[i].Name)); m_NetworkVariableFields.Add(ObjectNames.NicifyVariableName(fields[i].Name), fields[i]); } - if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(NetworkList<>) && !fields[i].IsDefined(typeof(HideInInspector), true)) + if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(NetworkList<>) && !fields[i].IsDefined(typeof(HideInInspector), true) && !fields[i].IsDefined(typeof(NonSerializedAttribute), true)) { m_NetworkVariableNames.Add(ObjectNames.NicifyVariableName(fields[i].Name)); m_NetworkVariableFields.Add(ObjectNames.NicifyVariableName(fields[i].Name), fields[i]);