This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
ComponentSendSystem.cs
117 lines (99 loc) · 4.1 KB
/
ComponentSendSystem.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Improbable.Gdk.Core.CodegenAdapters;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Jobs.LowLevel.Unsafe;
using UnityEngine.Profiling;
namespace Improbable.Gdk.Core
{
/// <summary>
/// Executes the default replication logic for each SpatialOS component.
/// </summary>
[DisableAutoCreation]
[AlwaysUpdateSystem]
[UpdateInGroup(typeof(SpatialOSSendGroup.InternalSpatialOSSendGroup))]
[UpdateBefore(typeof(SpatialOSSendSystem))]
public class ComponentSendSystem : ComponentSystem
{
private readonly List<ComponentReplicator> componentReplicators = new List<ComponentReplicator>();
private NativeArray<ArchetypeChunk>[] chunkArrayCache;
private NativeArray<JobHandle> gatheringJobs;
protected override void OnCreate()
{
base.OnCreate();
PopulateDefaultComponentReplicators();
chunkArrayCache = new NativeArray<ArchetypeChunk>[componentReplicators.Count];
gatheringJobs = new NativeArray<JobHandle>(componentReplicators.Count, Allocator.Persistent);
}
protected override void OnDestroy()
{
gatheringJobs.Dispose();
base.OnDestroy();
}
public bool TryRegisterCustomReplicationSystem(uint componentId)
{
if (componentReplicators.All(componentReplicator => componentReplicator.ComponentId != componentId))
{
return false;
}
// The default replication system is removed, instead the custom one is responsible for replication.
return componentReplicators.Remove(componentReplicators.First(
componentReplicator => componentReplicator.ComponentId == componentId));
}
protected override void OnUpdate()
{
var componentUpdateSystem = World.GetExistingSystem<ComponentUpdateSystem>();
Profiler.BeginSample("GatherChunks");
for (var i = 0; i < componentReplicators.Count; i++)
{
var replicator = componentReplicators[i];
chunkArrayCache[i] = replicator.Group.CreateArchetypeChunkArray(Allocator.TempJob, out var jobHandle);
gatheringJobs[i] = jobHandle;
}
Profiler.EndSample();
JobHandle.CompleteAll(gatheringJobs);
for (var i = 0; i < componentReplicators.Count; i++)
{
var replicator = componentReplicators[i];
var chunkArray = chunkArrayCache[i];
Profiler.BeginSample("ExecuteReplication");
replicator.Handler.SendUpdates(chunkArray, this, EntityManager, componentUpdateSystem);
Profiler.EndSample();
chunkArray.Dispose();
}
}
internal void AddComponentReplicator(IComponentReplicationHandler componentReplicationHandler)
{
componentReplicators.Add(new ComponentReplicator
{
ComponentId = componentReplicationHandler.ComponentId,
Handler = componentReplicationHandler,
Group = GetEntityQuery(componentReplicationHandler.ComponentUpdateQuery)
});
}
private void PopulateDefaultComponentReplicators()
{
// Find all component specific replicators and create an instance.
var types = ReflectionUtility.GetNonAbstractTypes(typeof(IComponentReplicationHandler));
foreach (var type in types)
{
if (type.GetCustomAttribute<DisableAutoRegisterAttribute>() != null)
{
continue;
}
var handler = (IComponentReplicationHandler) Activator.CreateInstance(type);
AddComponentReplicator(handler);
}
}
private struct ComponentReplicator
{
public uint ComponentId;
public IComponentReplicationHandler Handler;
public EntityQuery Group;
}
}
}