Skip to content

Commit 97df049

Browse files
authored
chore: backport #1917 and #1918 into release/1.0.0 (second attempt) (#1930)
* fix: NetworkList serialization sends the list values before the cached updates, to prevent duplicate application (#1917) * fix: shutting down properly, when failing to StartHost or StartClient. This allows the next attempt to not fail automatically. (#1918)
1 parent 437d82a commit 97df049

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkList.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Unity.Netcode
1111
public class NetworkList<T> : NetworkVariableSerialization<T> where T : unmanaged, IEquatable<T>
1212
{
1313
private NativeList<T> m_List = new NativeList<T>(64, Allocator.Persistent);
14+
private NativeList<T> m_ListAtLastReset = new NativeList<T>(64, Allocator.Persistent);
1415
private NativeList<NetworkListEvent<T>> m_DirtyEvents = new NativeList<NetworkListEvent<T>>(64, Allocator.Persistent);
1516

1617
/// <summary>
@@ -41,7 +42,11 @@ public NetworkList(IEnumerable<T> values = default,
4142
public override void ResetDirty()
4243
{
4344
base.ResetDirty();
44-
m_DirtyEvents.Clear();
45+
if (m_DirtyEvents.Length > 0)
46+
{
47+
m_DirtyEvents.Clear();
48+
m_ListAtLastReset.CopyFrom(m_List);
49+
}
4550
}
4651

4752
/// <inheritdoc />
@@ -109,10 +114,10 @@ public override void WriteDelta(FastBufferWriter writer)
109114
/// <inheritdoc />
110115
public override void WriteField(FastBufferWriter writer)
111116
{
112-
writer.WriteValueSafe((ushort)m_List.Length);
113-
for (int i = 0; i < m_List.Length; i++)
117+
writer.WriteValueSafe((ushort)m_ListAtLastReset.Length);
118+
for (int i = 0; i < m_ListAtLastReset.Length; i++)
114119
{
115-
Write(writer, m_List[i]);
120+
Write(writer, m_ListAtLastReset[i]);
116121
}
117122
}
118123

@@ -454,6 +459,7 @@ public int LastModifiedTick
454459
public override void Dispose()
455460
{
456461
m_List.Dispose();
462+
m_ListAtLastReset.Dispose();
457463
m_DirtyEvents.Dispose();
458464
}
459465
}

com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,12 @@ public override bool StartClient()
10471047
return false;
10481048
}
10491049

1050-
return ClientBindAndConnect();
1050+
var succeeded = ClientBindAndConnect();
1051+
if (!succeeded)
1052+
{
1053+
Shutdown();
1054+
}
1055+
return succeeded;
10511056
}
10521057

10531058
public override bool StartServer()
@@ -1057,12 +1062,23 @@ public override bool StartServer()
10571062
return false;
10581063
}
10591064

1065+
bool succeeded;
10601066
switch (m_ProtocolType)
10611067
{
10621068
case ProtocolType.UnityTransport:
1063-
return ServerBindAndListen(ConnectionData.ListenEndPoint);
1069+
succeeded = ServerBindAndListen(ConnectionData.ListenEndPoint);
1070+
if (!succeeded)
1071+
{
1072+
Shutdown();
1073+
}
1074+
return succeeded;
10641075
case ProtocolType.RelayUnityTransport:
1065-
return StartRelayServer();
1076+
succeeded = StartRelayServer();
1077+
if (!succeeded)
1078+
{
1079+
Shutdown();
1080+
}
1081+
return succeeded;
10661082
default:
10671083
return false;
10681084
}

0 commit comments

Comments
 (0)