Skip to content

Commit 7433a26

Browse files
0xFA11ShadauxCatjeffreyrainyandrews-unitymattwalsh-unity
authored
test: snapshot unit tests (#1472)
* fix: issue #1151 - client RPCs invoked in OnNetworkSpawn being dropped * standards.py --fix * Removed debug log that accidentally got left in. * Revert "fix: issue #1151 - client RPCs invoked in OnNetworkSpawn being dropped" This reverts commit d7d7bd7. # Conflicts: # com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs # testproject/Assets/Tests/Runtime/MessageOrdering.cs # testproject/Assets/Tests/Runtime/Support/SpawnRpcDespawn.cs * New approach to fixing the issue by deferring things that are received before the object is spawned. * Address review feedback. * Update com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs Co-authored-by: M. Fatih MAR <[email protected]> * Fix bad merge. * Another merge error * wip * wip * wip, refactored SnapshotSystem to allow unit-testing, without SDK going snapshot-enabled * bit further, adding NativeList to test assembly, testing that one spawn is received * test: having the sent snapshot immediately applied locally. Currently causes exception * refactor: made SnapshotDataMessage handle systemOwners of different types, NetworkManager or not * improved coverage: spawns and despawns * starting on a 'lost messages' test * Fixed memory deallocation in mock snapshot sendmessage * improved snapshot test coverage * post-merge adjustment * post-merge adjustment * style: applying standards.py * chore: update PR template to include checklist and info about backports (#1315) * chore: update PR template to include checklist and info about backports * Update pull_request_template.md * test: add checking for ServerRPCParams (#1319) * chore: adding dry-run promotion job (#1321) * style: renaming SnapshotRTT to SnapshotRtt (#1252) * revert unnecessary changes Co-authored-by: Jaedyn Draper <[email protected]> Co-authored-by: Jaedyn Draper <[email protected]> Co-authored-by: Jeffrey Rainy <[email protected]> Co-authored-by: Andrew Spiering <[email protected]> Co-authored-by: Matt Walsh <[email protected]>
1 parent 387ebf3 commit 7433a26

File tree

13 files changed

+624
-319
lines changed

13 files changed

+624
-319
lines changed

com.unity.netcode.gameobjects/Runtime/Configuration/NetworkConfig.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,15 @@ public class NetworkConfig
141141
/// <summary>
142142
/// Whether or not to enable Snapshot System for variable updates. Not supported in this version.
143143
/// </summary>
144-
public bool UseSnapshotDelta { get; } = false;
144+
public bool UseSnapshotDelta { get; internal set; } = false;
145145
/// <summary>
146146
/// Whether or not to enable Snapshot System for spawn and despawn commands. Not supported in this version.
147147
/// </summary>
148-
public bool UseSnapshotSpawn { get; } = false;
148+
public bool UseSnapshotSpawn { get; internal set; } = false;
149149
/// <summary>
150150
/// When Snapshot System spawn is enabled: max size of Snapshot Messages. Meant to fit MTU.
151151
/// </summary>
152-
public int SnapshotMaxSpawnUsage { get; } = 1200;
152+
public int SnapshotMaxSpawnUsage { get; } = 1000;
153153

154154
public const int RttAverageSamples = 5; // number of RTT to keep an average of (plus one)
155155
public const int RttWindowSize = 64; // number of slots to use for RTT computations (max number of in-flight packets)

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,6 @@ private void Initialize(bool server)
554554
SnapshotSystem = null;
555555
}
556556

557-
SnapshotSystem = new SnapshotSystem(this);
558-
559557
if (server)
560558
{
561559
NetworkTimeSystem = NetworkTimeSystem.ServerTimeSystem();
@@ -568,6 +566,8 @@ private void Initialize(bool server)
568566
NetworkTickSystem = new NetworkTickSystem(NetworkConfig.TickRate, 0, 0);
569567
NetworkTickSystem.Tick += OnNetworkManagerTick;
570568

569+
SnapshotSystem = new SnapshotSystem(this, NetworkConfig, NetworkTickSystem);
570+
571571
this.RegisterNetworkUpdate(NetworkUpdateStage.PreUpdate);
572572

573573
// This is used to remove entries not needed or invalid

com.unity.netcode.gameobjects/Runtime/Core/SnapshotSystem.cs

Lines changed: 211 additions & 146 deletions
Large diffs are not rendered by default.

com.unity.netcode.gameobjects/Runtime/Messaging/Messages/SnapshotDataMessage.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ public unsafe void Serialize(FastBufferWriter writer)
104104

105105
public static unsafe void Receive(FastBufferReader reader, in NetworkContext context)
106106
{
107-
var networkManager = (NetworkManager)context.SystemOwner;
108107
var message = new SnapshotDataMessage();
109108
if (!reader.TryBeginRead(
110109
FastBufferWriter.GetWriteSize(message.CurrentTick) +
@@ -142,20 +141,32 @@ public static unsafe void Receive(FastBufferReader reader, in NetworkContext con
142141
using (message.Spawns)
143142
using (message.Despawns)
144143
{
145-
message.Handle(context.SenderId, networkManager);
144+
message.Handle(context.SenderId, context.SystemOwner);
146145
}
147146
}
148147

149-
public void Handle(ulong senderId, NetworkManager networkManager)
148+
public void Handle(ulong senderId, object systemOwner)
150149
{
151-
// todo: temporary hack around bug
152-
if (!networkManager.IsServer)
150+
if (systemOwner is NetworkManager)
153151
{
154-
senderId = networkManager.ServerClientId;
155-
}
152+
var networkManager = (NetworkManager)systemOwner;
153+
154+
// todo: temporary hack around bug
155+
if (!networkManager.IsServer)
156+
{
157+
senderId = networkManager.ServerClientId;
158+
}
156159

157-
var snapshotSystem = networkManager.SnapshotSystem;
158-
snapshotSystem.HandleSnapshot(senderId, this);
160+
var snapshotSystem = networkManager.SnapshotSystem;
161+
snapshotSystem.HandleSnapshot(senderId, this);
162+
}
163+
else
164+
{
165+
var ownerData = (Tuple<SnapshotSystem, ulong>)systemOwner;
166+
var snapshotSystem = ownerData.Item1;
167+
snapshotSystem.HandleSnapshot(ownerData.Item2, this);
168+
return;
169+
}
159170
}
160171
}
161172
}

com.unity.netcode.gameobjects/Tests/Editor/SnapshotRttTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class SnapshotRttTests
99
[Test]
1010
public void TestBasicRtt()
1111
{
12-
var snapshot = new SnapshotSystem(default);
12+
var snapshot = new SnapshotSystem(null, new NetworkConfig(), null);
1313
var client1 = snapshot.GetConnectionRtt(0);
1414

1515
client1.NotifySend(0, 0.0);
@@ -40,7 +40,7 @@ public void TestBasicRtt()
4040
[Test]
4141
public void TestEdgeCasesRtt()
4242
{
43-
var snapshot = new SnapshotSystem(NetworkManager.Singleton);
43+
var snapshot = new SnapshotSystem(null, new NetworkConfig(), null);
4444
var client1 = snapshot.GetConnectionRtt(0);
4545
var iterationCount = NetworkConfig.RttWindowSize * 3;
4646
var extraCount = NetworkConfig.RttWindowSize * 2;

0 commit comments

Comments
 (0)