From 26d75b26c035cd89ad9f33f5b2191e981bf8194d Mon Sep 17 00:00:00 2001 From: Kitty Draper <284434+ShadauxCat@users.noreply.github.com> Date: Tue, 2 May 2023 13:36:04 -0500 Subject: [PATCH] fix: Add ILPP error-handling to catch managed INetworkSerializables that don't meet the `new()` constraint so they won't crash the editor [MTT-6211] (#2528) --- com.unity.netcode.gameobjects/CHANGELOG.md | 1 + .../Editor/CodeGen/NetworkBehaviourILPP.cs | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index b38f6a25ab..2980a3d450 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -17,6 +17,7 @@ Additional documentation and release notes are available at [Multiplayer Documen - Fixed issue where a client could throw an exception if abruptly disconnected from a network session with one or more spawned `NetworkObject`(s). (#2510) - Fixed issue where invalid endpoint addresses were not being detected and returning false from NGO UnityTransport. (#2496) - Fixed some errors that could occur if a connection is lost and the loss is detected when attempting to write to the socket. (#2495) +- Making a `NetworkVariable` with an `INetworkSerializable` type that doesn't meet the `new()` constraint will now create a compile-time error instead of an editor crash (#2528) - Fixed the inspector throwing exceptions when attempting to render `NetworkVariable`s of enum types. (#2529) - Fixed an exception and error logging when two different objects are shown and hidden on the same frame (#2524) diff --git a/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs b/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs index 2882fbbccb..baac903d31 100644 --- a/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs +++ b/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs @@ -210,6 +210,21 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly) { if (type.HasInterface(typeof(INetworkSerializable).FullName)) { + var constructors = type.Resolve().GetConstructors(); + var hasEmptyConstructor = false; + foreach (var constructor in constructors) + { + if (constructor.Parameters.Count == 0) + { + hasEmptyConstructor = true; + } + } + + if (!hasEmptyConstructor) + { + m_Diagnostics.AddError($"{type} cannot be used in a network variable - Managed {nameof(INetworkSerializable)} instances must meet the `new()` (default empty constructor) constraint."); + continue; + } serializeMethod = new GenericInstanceMethod(m_NetworkVariableSerializationTypes_InitializeSerializer_ManagedINetworkSerializable_MethodRef); }