Skip to content

Commit

Permalink
fix: Add ILPP error-handling to catch managed INetworkSerializables t…
Browse files Browse the repository at this point in the history
…hat don't meet the `new()` constraint so they won't crash the editor [MTT-6211] (#2528)
  • Loading branch information
ShadauxCat authored May 2, 2023
1 parent 1c58326 commit 26d75b2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 26d75b2

Please sign in to comment.