Skip to content

Commit 26d75b2

Browse files
authored
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)
1 parent 1c58326 commit 26d75b2

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1717
- Fixed issue where a client could throw an exception if abruptly disconnected from a network session with one or more spawned `NetworkObject`(s). (#2510)
1818
- Fixed issue where invalid endpoint addresses were not being detected and returning false from NGO UnityTransport. (#2496)
1919
- Fixed some errors that could occur if a connection is lost and the loss is detected when attempting to write to the socket. (#2495)
20+
- 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)
2021
- Fixed the inspector throwing exceptions when attempting to render `NetworkVariable`s of enum types. (#2529)
2122
- Fixed an exception and error logging when two different objects are shown and hidden on the same frame (#2524)
2223

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

+15
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,21 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly)
210210
{
211211
if (type.HasInterface(typeof(INetworkSerializable).FullName))
212212
{
213+
var constructors = type.Resolve().GetConstructors();
214+
var hasEmptyConstructor = false;
215+
foreach (var constructor in constructors)
216+
{
217+
if (constructor.Parameters.Count == 0)
218+
{
219+
hasEmptyConstructor = true;
220+
}
221+
}
222+
223+
if (!hasEmptyConstructor)
224+
{
225+
m_Diagnostics.AddError($"{type} cannot be used in a network variable - Managed {nameof(INetworkSerializable)} instances must meet the `new()` (default empty constructor) constraint.");
226+
continue;
227+
}
213228
serializeMethod = new GenericInstanceMethod(m_NetworkVariableSerializationTypes_InitializeSerializer_ManagedINetworkSerializable_MethodRef);
214229
}
215230

0 commit comments

Comments
 (0)