This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
AcknowledgeAuthorityLossSystem.cs
90 lines (75 loc) · 3.06 KB
/
AcknowledgeAuthorityLossSystem.cs
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
using System;
using System.Collections.Generic;
using Improbable.Gdk.Core;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using UnityEngine.Profiling;
namespace Improbable.Gdk.ReactiveComponents
{
/// <summary>
/// Checks for entities that have acknowledged authority loss during soft-handover and forwards this to SpatialOS.
/// </summary>
[DisableAutoCreation]
[AlwaysUpdateSystem]
[UpdateInGroup(typeof(SpatialOSSendGroup.InternalSpatialOSSendGroup))]
public class AcknowledgeAuthorityLossSystem : ComponentSystem
{
private readonly List<ComponentAuthorityLossDetails> authorityLossDetails =
new List<ComponentAuthorityLossDetails>();
private NativeArray<ArchetypeChunk>[] chunkArrayCache;
private NativeArray<JobHandle> gatheringJobs;
private ComponentUpdateSystem updateSystem;
protected override void OnCreate()
{
base.OnCreate();
updateSystem = World.GetExistingSystem<ComponentUpdateSystem>();
GenerateComponentGroups();
chunkArrayCache = new NativeArray<ArchetypeChunk>[authorityLossDetails.Count];
gatheringJobs = new NativeArray<JobHandle>(authorityLossDetails.Count, Allocator.Persistent);
}
protected override void OnDestroy()
{
gatheringJobs.Dispose();
base.OnDestroy();
}
private void GenerateComponentGroups()
{
foreach (var type in ReflectionUtility.GetNonAbstractTypes(typeof(AbstractAcknowledgeAuthorityLossHandler)))
{
var authorityLossHandler = (AbstractAcknowledgeAuthorityLossHandler) Activator.CreateInstance(type);
authorityLossDetails.Add(new ComponentAuthorityLossDetails
{
Handler = authorityLossHandler,
Group = GetEntityQuery(authorityLossHandler.Query)
});
}
}
protected override void OnUpdate()
{
Profiler.BeginSample("GatherChunks");
for (var i = 0; i < authorityLossDetails.Count; i++)
{
var replicator = authorityLossDetails[i];
chunkArrayCache[i] = replicator.Group.CreateArchetypeChunkArray(Allocator.TempJob, out var jobHandle);
gatheringJobs[i] = jobHandle;
}
Profiler.EndSample();
JobHandle.CompleteAll(gatheringJobs);
for (var i = 0; i < authorityLossDetails.Count; i++)
{
var details = authorityLossDetails[i];
var chunkArray = chunkArrayCache[i];
Profiler.BeginSample("AcknowledgingAuthorityLoss");
details.Handler.AcknowledgeAuthorityLoss(chunkArray, this, updateSystem);
Profiler.EndSample();
chunkArray.Dispose();
}
}
private struct ComponentAuthorityLossDetails
{
public AbstractAcknowledgeAuthorityLossHandler Handler;
public EntityQuery Group;
}
}
}