-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathClientOnlyComponentBackupSystem.cs
More file actions
422 lines (394 loc) · 22.3 KB
/
ClientOnlyComponentBackupSystem.cs
File metadata and controls
422 lines (394 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
using System;
using System.Diagnostics;
using Unity.Burst;
using Unity.Burst.Intrinsics;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.NetCode;
using Unity.NetCode.LowLevel.Unsafe;
namespace Unity.NetCode.Samples
{
/// <summary>
/// System responsible for:
/// - creating the ClientOnlyCollection and the necessary metadata for processing ghosts with client-only components.
/// - add to all ghosts (that present or not the client-only components) a state component, the ClientOnlyBackup.
/// - backup the client-only components data for every full tick and store them inside the ClientOnlyBackup buffer.
/// <para>
/// System used to make a backup of all client-only component state. The system run at the end of the prediction loop,
/// and store inside the <see cref="ClientOnlyBackup"/> history buffer the state of components for each full predicted tick.
/// Partial ticks are not saved.
/// </para>
/// <para>
/// The backup consist of a mem-copy of the components data and, if the some of the component also implements
/// the <see cref="IEnableableComponent"/>, their enable bits.
/// </para>
/// <remarks>
/// The size of the <see cref="ClientOnlyBackup"/> buffer is not fixed and can grow,to accomodate both latency and
/// ghosts update frequency. The size of the buffer is still bounded, due to fact the oldest backup are removed and
/// the slot reused.
/// </remarks>
/// <para>
/// The saved components states are then used to restore the the components data when a new snapshot is received from the server.
/// See <see cref="ClientOnlyComponentRestoreSystem"/> for more information.
/// </para>
/// </summary>
[BurstCompile]
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
[UpdateInGroup(typeof(PredictedSimulationSystemGroup), OrderLast = true)]
[UpdateBefore(typeof(GhostPredictionHistorySystem))]
public partial struct ClientOnlyComponentBackupSystem : ISystem
{
private const int InitialBackupCapacity = 16;
private EntityQuery m_predictedGhostsWithClientOnlyBackup;
private EntityQuery m_predictedGhostsNotProcessed;
private EntityQuery m_predictedPrespawendGhostsNotProcessed;
private EntityQuery m_destroyedGhostsWithClientOnlyBackup;
private EntityStorageInfoLookup m_childEntityLookup;
private BufferTypeHandle<LinkedEntityGroup> m_linkedEntityGroupHandle;
private ComponentTypeHandle<ClientOnlyBackup> m_backupTypeHandle;
private ComponentTypeHandle<GhostType> m_ghostTypeHandle;
private NativeList<ComponentType> m_clientOnlyComponentTypes;
private NativeList<ClientOnlyBackupInfo> m_clientOnlyBackupInfoCollection;
private NativeHashMap<GhostType, ClientOnlyBackupMetadata> m_ghostTypeToPrefabMetadata;
private ClientOnlyTypeHandleList m_clientOnlyTypeHandleList;
[BurstCompile]
public void OnCreate(ref SystemState state)
{
var queryBuilder = new EntityQueryBuilder(Allocator.Temp);
queryBuilder.WithAll<Simulate>();
queryBuilder.WithAll<PredictedGhost>();
queryBuilder.WithAll<ClientOnlyBackup>();
m_predictedGhostsWithClientOnlyBackup = state.GetEntityQuery(queryBuilder);
queryBuilder.Reset();
queryBuilder.WithAll<PredictedGhost>();
queryBuilder.WithAll<GhostType>();
queryBuilder.WithNone<ClientOnlyProcessed>();
queryBuilder.WithNone<PreSpawnedGhostIndex>();
m_predictedGhostsNotProcessed = state.GetEntityQuery(queryBuilder);
queryBuilder.Reset();
queryBuilder.WithAll<PredictedGhost>();
queryBuilder.WithAll<GhostType>();
queryBuilder.WithNone<ClientOnlyProcessed>();
queryBuilder.WithAll<PreSpawnedGhostIndex>();
m_predictedPrespawendGhostsNotProcessed = state.GetEntityQuery(queryBuilder);
queryBuilder.Reset();
queryBuilder.WithAll<ClientOnlyBackup>();
queryBuilder.WithNone<PredictedGhost>();
m_destroyedGhostsWithClientOnlyBackup = state.GetEntityQuery(queryBuilder);
m_clientOnlyBackupInfoCollection = new NativeList<ClientOnlyBackupInfo>(Allocator.Persistent);
m_clientOnlyComponentTypes = new NativeList<ComponentType>(32, Allocator.Persistent);
m_ghostTypeToPrefabMetadata = new NativeHashMap<GhostType, ClientOnlyBackupMetadata>(128, Allocator.Persistent);
m_clientOnlyTypeHandleList = default(ClientOnlyTypeHandleList);
m_backupTypeHandle = state.GetComponentTypeHandle<ClientOnlyBackup>();
m_ghostTypeHandle = state.GetComponentTypeHandle<GhostType>(true);
m_childEntityLookup = state.GetEntityStorageInfoLookup();
m_linkedEntityGroupHandle = state.GetBufferTypeHandle<LinkedEntityGroup>(true);
//Create the singleton.
var types = new NativeArray<ComponentType>(1, Allocator.Temp);
types[0] = ComponentType.ReadWrite<ClientOnlyCollection>();
var singleton = state.EntityManager.CreateEntity(state.EntityManager.CreateArchetype(types));
state.EntityManager.SetComponentData(singleton, new ClientOnlyCollection
{
ProcessedPrefabs = 0,
ClientOnlyComponentTypes = m_clientOnlyComponentTypes,
BackupInfoCollection = m_clientOnlyBackupInfoCollection,
GhostTypeToPrefabMetadata = m_ghostTypeToPrefabMetadata
});
state.RequireForUpdate<GhostCollection>();
state.RequireForUpdate<EnableClientOnlyBackup>();
}
[BurstCompile]
public void OnDestroy(ref SystemState state)
{
m_clientOnlyComponentTypes.Dispose();
m_clientOnlyBackupInfoCollection.Dispose();
m_ghostTypeToPrefabMetadata.Dispose();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
ref var clientOnlyCollection = ref SystemAPI.GetSingletonRW<ClientOnlyCollection>().ValueRW;
if(clientOnlyCollection.ClientOnlyComponentTypes.Length == 0)
return;
var ghostCollection = SystemAPI.GetSingleton<GhostCollection>();
if(!ghostCollection.IsInGame || ghostCollection.NumLoadedPrefabs == 0)
return;
if (clientOnlyCollection.ProcessedPrefabs < ghostCollection.NumLoadedPrefabs)
{
var ghostPrefabs = SystemAPI.GetSingletonBuffer<GhostCollectionPrefab>(true);
for (int i = clientOnlyCollection.ProcessedPrefabs; i < ghostCollection.NumLoadedPrefabs; ++i)
{
clientOnlyCollection.ProcessGhostTypePrefab(ghostPrefabs[i].GhostType, ghostPrefabs[i].GhostPrefab, state.EntityManager);
++clientOnlyCollection.ProcessedPrefabs;
}
}
if(clientOnlyCollection.ProcessedPrefabs == 0)
return;
if (!m_destroyedGhostsWithClientOnlyBackup.IsEmpty)
{
var job = new DisposeBackupJob();
job.Run(m_destroyedGhostsWithClientOnlyBackup);
state.EntityManager.RemoveComponent<ClientOnlyBackup>(m_destroyedGhostsWithClientOnlyBackup);
}
//Add to ghost the necessary state to backup the components and buffers.
if (!m_predictedGhostsNotProcessed.IsEmpty)
{
var entities = m_predictedGhostsNotProcessed.ToEntityArray(Allocator.Temp);
var ghostTypes = m_predictedGhostsNotProcessed.ToComponentDataArray<GhostType>(Allocator.Temp);
//Mark entities as processed
state.EntityManager.AddComponent<ClientOnlyProcessed>(m_predictedGhostsNotProcessed);
for(int ent=0;ent<entities.Length;++ent)
{
var ghostType = ghostTypes[ent];
//If the type does not have any client only component, skip.
if(!clientOnlyCollection.GhostTypeToPrefabMetadata.ContainsKey(ghostType))
continue;
//Adding component one by one like that is very very slow. Ideally I would like to add the component "per chunk"
//but it is not really possible (or is not that easy to achieve)
var clientOnlyMetadata = clientOnlyCollection.GhostTypeToPrefabMetadata[ghostType];
var clientOnlyBackup = new ClientOnlyBackup(slotSize:clientOnlyMetadata.backupSize, capacity:InitialBackupCapacity);
state.EntityManager.AddComponentData(entities[ent], clientOnlyBackup);
}
}
//Pre-spawned ghosts has a special path, because the handling is a little different. In particular we can just assign
//the processed flag to the query (the fastest way) but we need to inspect entities one by one.
//That generate quite a lot of burden in term of structural changes.
if (!m_predictedPrespawendGhostsNotProcessed.IsEmpty)
{
var entities = m_predictedPrespawendGhostsNotProcessed.ToEntityArray(Allocator.Temp);
var ghostTypes = m_predictedPrespawendGhostsNotProcessed.ToComponentDataArray<GhostType>(Allocator.Temp);
state.EntityManager.AddComponent<ClientOnlyProcessed>(m_predictedPrespawendGhostsNotProcessed);
for(int ent=0;ent<entities.Length;++ent)
{
var ghostType = ghostTypes[ent];
if (!clientOnlyCollection.GhostTypeToPrefabMetadata.ContainsKey(ghostType))
continue;
var clientOnlyMetadata = clientOnlyCollection.GhostTypeToPrefabMetadata[ghostType];
var clientOnlyBackup = new ClientOnlyBackup(slotSize:clientOnlyMetadata.backupSize, capacity:InitialBackupCapacity);
state.EntityManager.AddComponentData(entities[ent], clientOnlyBackup);
}
}
//Only backup full server tick. Partial tick will always start from the last full simulated tick so no need to backup
var networkTime = SystemAPI.GetSingleton<NetworkTime>();
if (networkTime.IsPartialTick)
return;
m_childEntityLookup.Update(ref state);
m_linkedEntityGroupHandle.Update(ref state);
m_backupTypeHandle.Update(ref state);
m_ghostTypeHandle.Update(ref state);
m_clientOnlyTypeHandleList.CreateOrUpdateTypeHandleList(ref state, clientOnlyCollection.ClientOnlyComponentTypes.AsArray());
var backupJob = new BackupJob
{
childEntityLookup = m_childEntityLookup,
ghostTypeHandle = m_ghostTypeHandle,
linkedEntityGroupHandle = m_linkedEntityGroupHandle,
componentTypeHandles = m_clientOnlyTypeHandleList,
clientOnlyComponentCollection = clientOnlyCollection.BackupInfoCollection,
prefabMetadata = clientOnlyCollection.GhostTypeToPrefabMetadata,
backupTypeHandle = m_backupTypeHandle,
serverTick = networkTime.ServerTick,
netDebug = SystemAPI.GetSingleton<NetDebug>()
};
state.Dependency = backupJob.ScheduleParallel(m_predictedGhostsWithClientOnlyBackup, state.Dependency);
}
[BurstCompile]
[WithAll(typeof(ClientOnlyBackup))]
[WithNone(typeof(PredictedGhost))]
partial struct DisposeBackupJob : IJobEntity
{
public void Execute(ref ClientOnlyBackup backup)
{
backup.Dispose();
}
}
//Little class that help writing component backup. Abstract some pointer manipulation and add some
//boundary checks (in the editor)
unsafe ref struct ClientOnlyBackupWriter
{
private readonly int* backupEnableBitPtr;
private uint* backupTick;
private byte* backupCompDataPtr;
#if ENABLE_UNITY_COLLECTIONS_CHECKS
private byte* beginCompDataPtr;
private int backupSize;
#endif
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
private void CheckBounds()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
if((backupCompDataPtr - beginCompDataPtr) >= backupSize)
throw new ArgumentOutOfRangeException();
#endif
}
public ClientOnlyBackupWriter(byte* bufPtr, int compDataOffset, int slotSize)
{
backupTick = (uint*)bufPtr;
backupEnableBitPtr = (int*)(bufPtr + sizeof(uint));
backupCompDataPtr = (bufPtr + compDataOffset);
#if ENABLE_UNITY_COLLECTIONS_CHECKS
beginCompDataPtr = backupCompDataPtr;
backupSize = slotSize;
#endif
}
//Skip the component/buffer backup. Reset the data to 0 and advance the backup pointers
public void Skip(in ClientOnlyBackupInfo comp)
{
CheckBounds();
UnsafeUtility.MemClear(backupCompDataPtr, comp.ComponentSize);
backupCompDataPtr += comp.ComponentSize;
}
public void WriteTick(NetworkTick serverTick)
{
*backupTick = serverTick.SerializedData;
}
//Store the enable bit for the component inside the backup. The backup slot data format is
// 4 bytes 4 bytes 4 bytes
// [ tick ][EnableBits 0][EnableBits 1]
// 3130 ... 0 64 ... 32
// the bits are stored left to right
public void BackupEnableBitForComponent(int compIdx, int ent, [ReadOnly] long* bitArrayPtr)
{
int entIdx = 1 << (ent & 0x3f);
long compEnableFroEntity = ((bitArrayPtr[ent >> 6] >> entIdx) & 0x1);
int bitIdx = 1 << (compIdx & 0x1f);
backupEnableBitPtr[compIdx >> 5] &= ~bitIdx;
backupEnableBitPtr[compIdx >> 5] |= (int)(bitIdx * compEnableFroEntity);
}
//Store the component data into the backup buffer.
public void BackupComponent([ReadOnly] byte* compDataPtr, in ClientOnlyBackupInfo comp)
{
CheckBounds();
//TODO: probably better to optimise that for small component data size (like 8/64 bytes)
UnsafeUtility.MemCpy(backupCompDataPtr, compDataPtr, comp.ComponentSize);
backupCompDataPtr += comp.ComponentSize;
}
}
[BurstCompile]
unsafe struct BackupJob : IJobChunk
{
[ReadOnly] public EntityStorageInfoLookup childEntityLookup;
[ReadOnly] public ComponentTypeHandle<GhostType> ghostTypeHandle;
public ComponentTypeHandle<ClientOnlyBackup> backupTypeHandle;
[ReadOnly] public BufferTypeHandle<LinkedEntityGroup> linkedEntityGroupHandle;
[ReadOnly] public ClientOnlyTypeHandleList componentTypeHandles;
[ReadOnly] public NativeList<ClientOnlyBackupInfo> clientOnlyComponentCollection;
[ReadOnly] public NativeHashMap<GhostType, ClientOnlyBackupMetadata> prefabMetadata;
public NetworkTick serverTick;
public NetDebug netDebug;
public void Execute(in ArchetypeChunk chunk, int chunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
{
//Lookup for the ghost type. If the chunk contains predicted prespawed-ghost and the type has not been assigned
//yet, we skip the backup.
var ghostTypes = chunk.GetNativeArray(ref ghostTypeHandle);
var firstGhostType = ghostTypes[0];
//Retrieve the backup metadata. This should have been constructed already by the system before scheduling the jobs
if (!prefabMetadata.TryGetValue(firstGhostType, out var metadata))
{
netDebug.LogError($"Unable to find client-only backup metadata for ghost with ghost type {firstGhostType}");
return;
}
//Prepare thw writers and copy the ghost data in the backup buffer.
//Components and buffers data are backup on per entity basis, with some slightly different code in between root and child entites.
var backupWriters = stackalloc ClientOnlyBackupWriter[chunk.Count];
InitBackupWriters(chunk, metadata, backupWriters);
BackupComponents(chunk, metadata, backupWriters);
}
private void BackupComponents(in ArchetypeChunk chunk, in ClientOnlyBackupMetadata metadata, ClientOnlyBackupWriter *backupWriters)
{
int chunkEntityCount = chunk.Count;
//store the server tick for the current slot
for (int ent = 0; ent < chunkEntityCount; ++ent)
backupWriters[ent].WriteTick(serverTick);
//Backup all the root component for the whole chunk.
var iterEnd = metadata.componentBegin + metadata.numRootComponents;
var compIdx = metadata.componentBegin;
//just a convenient way to add some boundary checks
var typeHandles = new UnsafeList<DynamicComponentTypeHandle>(componentTypeHandles.Ptr, clientOnlyComponentCollection.Length);
for (;compIdx < iterEnd; ++compIdx)
{
var comp = clientOnlyComponentCollection[compIdx];
//Buffers aren't supported
Unity.Assertions.Assert.IsFalse(comp.ComponentType.IsBuffer);
var typeHandle = typeHandles[comp.ComponentIndex];
if (!chunk.Has(ref typeHandle))
{
for (int ent = 0; ent < chunkEntityCount; ++ent)
backupWriters[ent].Skip(comp);
continue;
}
if (comp.ComponentType.IsEnableable)
{
var handle = typeHandle;
var bitArray = chunk.GetEnableableBits(ref handle);
var bitArrayPtr = (long*)UnsafeUtility.AddressOf(ref bitArray);
for (int ent = 0; ent < chunkEntityCount; ++ent)
{
backupWriters[ent].BackupEnableBitForComponent(compIdx, ent, bitArrayPtr);
}
}
var compDataPtr = (byte*)chunk
.GetDynamicComponentDataArrayReinterpret<byte>(ref typeHandle, comp.ComponentSize)
.GetUnsafeReadOnlyPtr();
for (int ent = 0; ent < chunkEntityCount; ++ent)
{
backupWriters[ent].BackupComponent(compDataPtr, comp);
compDataPtr += comp.ComponentSize;
}
}
//backup all child entities components.
if (!chunk.Has(ref linkedEntityGroupHandle))
return;
var entityGroup = chunk.GetBufferAccessor(ref linkedEntityGroupHandle);
for (int childCompIdx = compIdx; childCompIdx < metadata.componentEnd; ++childCompIdx)
{
var comp = clientOnlyComponentCollection[childCompIdx];
var typeHandle = typeHandles[comp.ComponentIndex];
for (int ent = 0; ent < chunkEntityCount; ++ent)
{
if (entityGroup[ent].Length <= comp.EntityIndex)
{
backupWriters[ent].Skip(comp);
continue;
}
var childEnt = entityGroup[ent][comp.EntityIndex].Value;
var childChunk = childEntityLookup[childEnt].Chunk;
var indexInChunk = childEntityLookup[childEnt].IndexInChunk;
if (!childChunk.Has(ref typeHandle))
{
backupWriters[ent].Skip(comp);
continue;
}
if (comp.ComponentType.IsEnableable)
{
var handle = typeHandle;
var bitArray = childChunk.GetEnableableBits(ref handle);
var bitArrayPtr = (long*)UnsafeUtility.AddressOf(ref bitArray);
backupWriters[ent].BackupEnableBitForComponent(childCompIdx, indexInChunk, bitArrayPtr);
}
var compDataPtr = (byte*)childChunk
.GetDynamicComponentDataArrayReinterpret<byte>(ref typeHandle, comp.ComponentSize)
.GetUnsafeReadOnlyPtr();
compDataPtr += comp.ComponentSize * indexInChunk;
backupWriters[ent].BackupComponent(compDataPtr, comp);
}
}
}
private void InitBackupWriters(ArchetypeChunk chunk, in ClientOnlyBackupMetadata metadata, ClientOnlyBackupWriter* backupWriters)
{
var enableBitsIntSize = ClientOnlyBackup.EnableBitByteSize(metadata.componentEnd - metadata.componentBegin);
var compDataStartOffset = GhostComponentSerializer.SnapshotSizeAligned(sizeof(uint) + enableBitsIntSize);
//use the raw pointer for accessing the component data by ref. This is used to acquire and grow the buffer.
var states = (ClientOnlyBackup*)chunk.GetNativeArray(ref backupTypeHandle).GetUnsafePtr();
for (int ent = 0, chunkEntityCount = chunk.Count; ent < chunkEntityCount; ++ent)
{
states[ent].GrowBufferIfFull(metadata.backupSize);
var backupSlotIndex = states[ent].AcquireBackupSlot();
var slotOffset = metadata.backupSize * backupSlotIndex;
byte* backupDataPtr = states[ent].ComponentBackup.Ptr + slotOffset;
backupWriters[ent] = new ClientOnlyBackupWriter(backupDataPtr, compDataStartOffset, metadata.backupSize);
}
}
}
}
}