This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
MessagesToSend.cs
196 lines (155 loc) · 7.54 KB
/
MessagesToSend.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using Improbable.Gdk.Core.Commands;
using Improbable.Worker.CInterop;
namespace Improbable.Gdk.Core
{
public class MessagesToSend
{
private static class MessagesToSendMetadata
{
internal static IEnumerable<(uint componentId, Type diffStorageType)> ComponentTypes => ComponentDatabase.Metaclasses
.Select(pair => (pair.Key, pair.Value.DiffStorage));
internal static IEnumerable<(uint componentId, IEnumerable<Type> storageTypes)> CommandSendStorageTypes => ComponentDatabase.Metaclasses
.Select(componentCommands => (componentCommands.Key, componentCommands.Value.Commands.Select(m => m.SendStorage)));
}
private readonly MessageList<SerializedMessagesToSend.UpdateToSend> serializedComponentUpdates;
private readonly Dictionary<uint, IComponentDiffStorage> componentIdToComponentStorage =
new Dictionary<uint, IComponentDiffStorage>();
private readonly Dictionary<Type, IComponentDiffStorage> typeToComponentStorage =
new Dictionary<Type, IComponentDiffStorage>();
private readonly List<IComponentDiffStorage> componentStorageList = new List<IComponentDiffStorage>();
private readonly Dictionary<(uint componentId, uint commandId), IComponentCommandSendStorage> componentCommandToStorage =
new Dictionary<(uint componentId, uint commandId), IComponentCommandSendStorage>();
private readonly Dictionary<Type, ICommandSendStorage> typeToCommandStorage =
new Dictionary<Type, ICommandSendStorage>();
private readonly List<ICommandSendStorage> commandStorageList = new List<ICommandSendStorage>();
private readonly WorldCommandsToSendStorage worldCommandStorage = new WorldCommandsToSendStorage();
private readonly MessageList<LogMessageToSend> logsToSend = new MessageList<LogMessageToSend>();
private readonly List<Metrics> metricsToSend = new List<Metrics>();
public MessagesToSend()
{
serializedComponentUpdates = new MessageList<SerializedMessagesToSend.UpdateToSend>();
foreach (var (componentId, diffStorageType) in MessagesToSendMetadata.ComponentTypes)
{
var instance = (IComponentDiffStorage) Activator.CreateInstance(diffStorageType);
componentStorageList.Add(instance);
componentIdToComponentStorage.Add(componentId, instance);
typeToComponentStorage.Add(instance.GetUpdateType(), instance);
foreach (var eventType in instance.GetEventTypes())
{
typeToComponentStorage.Add(eventType, instance);
}
}
foreach (var (componentId, storageTypes) in MessagesToSendMetadata.CommandSendStorageTypes)
{
foreach (var sendStorageType in storageTypes)
{
var instance = (IComponentCommandSendStorage) Activator.CreateInstance(sendStorageType);
commandStorageList.Add(instance);
componentCommandToStorage.Add((componentId, instance.CommandId), instance);
typeToCommandStorage.Add(instance.RequestType, instance);
typeToCommandStorage.Add(instance.ResponseType, instance);
}
}
commandStorageList.Add(worldCommandStorage);
typeToCommandStorage.Add(typeof(WorldCommands.CreateEntity.Request), worldCommandStorage);
typeToCommandStorage.Add(typeof(WorldCommands.DeleteEntity.Request), worldCommandStorage);
typeToCommandStorage.Add(typeof(WorldCommands.ReserveEntityIds.Request), worldCommandStorage);
typeToCommandStorage.Add(typeof(WorldCommands.EntityQuery.Request), worldCommandStorage);
}
public void Clear()
{
foreach (var storage in componentStorageList)
{
storage.Clear();
}
foreach (var storage in commandStorageList)
{
storage.Clear();
}
logsToSend.Clear();
metricsToSend.Clear();
serializedComponentUpdates.Clear();
}
public void AddSerializedComponentUpdate(in SerializedMessagesToSend.UpdateToSend serializedComponentUpdate)
{
serializedComponentUpdates.Add(serializedComponentUpdate);
}
public void AddEvent<T>(T ev, long entityId) where T : IEvent
{
var storage = GetComponentDiffStorage(typeof(T));
// Update ID isn't needed so we set it to 0
((IDiffEventStorage<T>) storage).AddEvent(new ComponentEventReceived<T>(ev, new EntityId(entityId),
0));
}
public void AddCommandRequest<T>(T request, Unity.Entities.Entity sendingEntity, CommandRequestId requestId) where T : ICommandRequest
{
var storage = (ICommandRequestSendStorage<T>) GetCommandSendStorage(typeof(T));
storage.AddRequest(request, sendingEntity, requestId);
}
public void AddCommandResponse<T>(T response)
where T : ICommandResponse
{
var storage = (ICommandResponseSendStorage<T>) GetCommandSendStorage(typeof(T));
storage.AddResponse(response);
}
public void AddLogMessage(in LogMessageToSend log)
{
logsToSend.Add(in log);
}
public void AddMetrics(Metrics metrics)
{
metricsToSend.Add(metrics);
}
internal MessageList<SerializedMessagesToSend.UpdateToSend> GetSerializedComponentUpdates()
{
return serializedComponentUpdates;
}
internal MessageList<LogMessageToSend> GetLogMessages()
{
return logsToSend;
}
internal List<Metrics> GetMetrics()
{
return metricsToSend;
}
internal IComponentDiffStorage GetComponentDiffStorage(uint componentId)
{
if (!componentIdToComponentStorage.TryGetValue(componentId, out var storage))
{
throw new ArgumentException($"Can not find component diff storage. Unknown component ID {componentId}");
}
return storage;
}
internal IComponentDiffStorage GetComponentDiffStorage(Type type)
{
if (!typeToComponentStorage.TryGetValue(type, out var storage))
{
throw new ArgumentException($"Can not find component diff storage. Unknown type {type.FullName}");
}
return storage;
}
internal ICommandSendStorage GetCommandSendStorage(Type type)
{
if (!typeToCommandStorage.TryGetValue(type, out var storage))
{
throw new ArgumentException($"Can not find command send storage. Unknown command type {type.FullName}");
}
return storage;
}
internal IComponentCommandSendStorage GetCommandSendStorage(uint componentId, uint commandId)
{
if (!componentCommandToStorage.TryGetValue((componentId, commandId), out var commandSendStorage))
{
throw new ArgumentException($"Can not find command send storage. Could not find Command ID {commandId} for Component ID {componentId}");
}
return commandSendStorage;
}
internal WorldCommandsToSendStorage GetWorldCommandStorage()
{
return worldCommandStorage;
}
}
}