This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
ViewDiff.cs
363 lines (295 loc) · 13.5 KB
/
ViewDiff.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
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
using System;
using System.Collections.Generic;
using Improbable.Gdk.Core.Commands;
using Improbable.Worker.CInterop;
namespace Improbable.Gdk.Core
{
public class ViewDiff
{
public string DisconnectMessage;
public bool Disconnected { get; private set; }
public bool InCriticalSection { get; private set; }
private static List<Type> componentStorageTypes;
private static List<Type> commandStorageTypes;
private readonly HashSet<EntityId> entitiesAdded = new HashSet<EntityId>();
private readonly HashSet<EntityId> entitiesRemoved = new HashSet<EntityId>();
private readonly List<LogMessageReceived> logsReceived = new List<LogMessageReceived>();
private Metrics metricsReceived;
private readonly List<(string, string)> workerFlagsChanged = new List<(string, string)>();
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, Dictionary<uint, IComponentCommandDiffStorage>> componentIdToCommandIdToStorage =
new Dictionary<uint, Dictionary<uint, IComponentCommandDiffStorage>>();
private readonly Dictionary<Type, ICommandDiffStorage> typeToCommandStorage =
new Dictionary<Type, ICommandDiffStorage>();
private readonly List<ICommandDiffStorage> commandStorageList = new List<ICommandDiffStorage>();
private readonly WorldCommandsReceivedStorage worldCommandsReceivedStorage = new WorldCommandsReceivedStorage();
public ViewDiff()
{
if (componentStorageTypes == null)
{
componentStorageTypes = ReflectionUtility.GetNonAbstractTypes(typeof(IComponentDiffStorage));
}
if (commandStorageTypes == null)
{
commandStorageTypes = ReflectionUtility.GetNonAbstractTypes(typeof(IComponentCommandDiffStorage));
}
foreach (var type in componentStorageTypes)
{
var instance = (IComponentDiffStorage) Activator.CreateInstance(type);
componentStorageList.Add(instance);
componentIdToComponentStorage.Add(instance.GetComponentId(), instance);
typeToComponentStorage.Add(instance.GetUpdateType(), instance);
foreach (var eventType in instance.GetEventTypes())
{
typeToComponentStorage.Add(eventType, instance);
}
}
foreach (var type in commandStorageTypes)
{
var instance = (IComponentCommandDiffStorage) Activator.CreateInstance(type);
commandStorageList.Add(instance);
if (!componentIdToCommandIdToStorage.TryGetValue(instance.GetComponentId(),
out var commandIdToStorage))
{
commandIdToStorage = new Dictionary<uint, IComponentCommandDiffStorage>();
componentIdToCommandIdToStorage.Add(instance.GetComponentId(), commandIdToStorage);
}
commandIdToStorage.Add(instance.GetCommandId(), instance);
typeToCommandStorage.Add(instance.GetRequestType(), instance);
typeToCommandStorage.Add(instance.GetResponseType(), instance);
}
commandStorageList.Add(worldCommandsReceivedStorage);
typeToCommandStorage.Add(typeof(WorldCommands.CreateEntity.ReceivedResponse), worldCommandsReceivedStorage);
typeToCommandStorage.Add(typeof(WorldCommands.DeleteEntity.ReceivedResponse), worldCommandsReceivedStorage);
typeToCommandStorage.Add(typeof(WorldCommands.ReserveEntityIds.ReceivedResponse), worldCommandsReceivedStorage);
typeToCommandStorage.Add(typeof(WorldCommands.EntityQuery.ReceivedResponse), worldCommandsReceivedStorage);
}
public void Clear()
{
foreach (var storage in componentStorageList)
{
storage.Clear();
}
foreach (var storage in commandStorageList)
{
storage.Clear();
}
entitiesAdded.Clear();
entitiesRemoved.Clear();
logsReceived.Clear();
workerFlagsChanged.Clear();
metricsReceived = null;
InCriticalSection = false;
Disconnected = false;
DisconnectMessage = null;
}
public void AddEntity(long entityId)
{
if (!entitiesRemoved.Remove(new EntityId(entityId)))
{
entitiesAdded.Add(new EntityId(entityId));
}
}
public void RemoveEntity(long entityId)
{
if (!entitiesAdded.Remove(new EntityId(entityId)))
{
entitiesRemoved.Add(new EntityId(entityId));
}
}
public void AddComponent<T>(T component, long entityId, uint componentId) where T : ISpatialComponentUpdate
{
if (!componentIdToComponentStorage.TryGetValue(componentId, out var storage))
{
throw new ArgumentException(
$"Can not add component with ID {componentId} on entity with ID {entityId}. " +
$"Unknown component ID");
}
((IDiffComponentAddedStorage<T>) storage).AddEntityComponent(entityId, component);
}
public void RemoveComponent(long entityId, uint componentId)
{
if (!componentIdToComponentStorage.TryGetValue(componentId, out var storage))
{
throw new ArgumentException(
$"Can not set remove component with ID {componentId} from entity with ID {entityId}. " +
$"Unknown component ID");
}
storage.RemoveEntityComponent(entityId);
}
public void SetAuthority(long entityId, uint componentId, Authority authority)
{
if (!componentIdToComponentStorage.TryGetValue(componentId, out var authorityStorage))
{
throw new ArgumentException(
$"Can not set authority over component with ID {componentId} for entity with ID {entityId}. " +
$"Unknown component ID");
}
((IDiffAuthorityStorage) authorityStorage).AddAuthorityChange(
new AuthorityChangeReceived(authority, new EntityId(entityId)));
// Remove received command requests if authority has been lost
if (authority == Authority.NotAuthoritative)
{
if (componentIdToCommandIdToStorage.TryGetValue(componentId, out var commandIdToStorage))
{
foreach (var storage in commandIdToStorage)
{
storage.Value.RemoveRequests(entityId);
}
}
}
}
public void AddComponentUpdate<T>(T update, long entityId, uint componentId, uint updateId)
where T : ISpatialComponentUpdate
{
if (!componentIdToComponentStorage.TryGetValue(componentId, out var storage))
{
throw new ArgumentException(
$"Can not update component with ID {componentId} on entity with ID {entityId}. " +
$"Unknown component ID");
}
((IDiffUpdateStorage<T>) storage).AddUpdate(new ComponentUpdateReceived<T>(update, new EntityId(entityId),
updateId));
}
public void AddEvent<T>(T ev, long entityId, uint componentId, uint updateId) where T : IEvent
{
if (!componentIdToComponentStorage.TryGetValue(componentId, out var storage))
{
throw new ArgumentException(
$"Can not add event from component with ID {componentId} on entity with ID {entityId}. " +
$"Unknown component ID");
}
((IDiffEventStorage<T>) storage).AddEvent(new ComponentEventReceived<T>(ev, new EntityId(entityId),
updateId));
}
public void AddCommandRequest<T>(T request, uint componentId, uint commandId) where T : struct, IReceivedCommandRequest
{
if (!componentIdToCommandIdToStorage.TryGetValue(componentId, out var commandIdToStorage))
{
throw new ArgumentException($"Can not find component diff storage. Unknown component ID {componentId}");
}
if (!commandIdToStorage.TryGetValue(commandId, out var storage))
{
throw new ArgumentException($"Can not find component diff storage. Unknown command ID {commandId}");
}
((IDiffCommandRequestStorage<T>) storage).AddRequest(request);
}
public void AddCommandResponse<T>(T response, uint componentId, uint commandId)
where T : struct, IReceivedCommandResponse
{
if (!componentIdToCommandIdToStorage.TryGetValue(componentId, out var commandIdToStorage))
{
throw new ArgumentException($"Can not find component diff storage. Unknown component ID {componentId}");
}
if (!commandIdToStorage.TryGetValue(commandId, out var storage))
{
throw new ArgumentException($"Can not find component diff storage. Unknown command ID {commandId}");
}
((IDiffCommandResponseStorage<T>) storage).AddResponse(response);
}
public void AddCreateEntityResponse(WorldCommands.CreateEntity.ReceivedResponse response)
{
worldCommandsReceivedStorage.AddResponse(response);
}
public void AddDeleteEntityResponse(WorldCommands.DeleteEntity.ReceivedResponse response)
{
worldCommandsReceivedStorage.AddResponse(response);
}
public void AddReserveEntityIdsResponse(WorldCommands.ReserveEntityIds.ReceivedResponse response)
{
worldCommandsReceivedStorage.AddResponse(response);
}
public void AddEntityQueryResponse(WorldCommands.EntityQuery.ReceivedResponse response)
{
worldCommandsReceivedStorage.AddResponse(response);
}
public void AddLogMessage(string message, LogLevel level)
{
logsReceived.Add(new LogMessageReceived(message, level));
}
public void AddMetrics(Metrics metrics)
{
if (metricsReceived == null)
{
metricsReceived = metrics;
}
else
{
metricsReceived.Merge(metrics);
}
}
public void SetWorkerFlag(string flag, string value)
{
workerFlagsChanged.Add((flag, value));
}
public void Disconnect(string message)
{
Disconnected = true;
DisconnectMessage = message;
}
public void SetCriticalSection(bool inCriticalSection)
{
InCriticalSection = inCriticalSection;
}
internal Metrics GetMetrics()
{
return metricsReceived;
}
internal List<LogMessageReceived> GetLogsMessages()
{
return logsReceived;
}
internal List<(string, string)> GetWorkerFlagChanges()
{
return workerFlagsChanged;
}
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 component type {type.FullName}");
}
return storage;
}
internal IComponentCommandDiffStorage GetComponentCommandDiffStorage(uint componentId, uint commandId)
{
if (!componentIdToCommandIdToStorage.TryGetValue(componentId, out var commandIdToStorage))
{
throw new ArgumentException($"Can not find command diff storage. Unknown component ID {componentId}");
}
if (!commandIdToStorage.TryGetValue(commandId, out var storage))
{
throw new ArgumentException($"Can not find command diff storage. Unknown command ID {commandId}");
}
return storage;
}
internal ICommandDiffStorage GetCommandDiffStorage(Type type)
{
if (!typeToCommandStorage.TryGetValue(type, out var storage))
{
throw new ArgumentException($"Can not find command diff storage. Unknown command type {type.FullName}");
}
return storage;
}
internal HashSet<EntityId> GetEntitiesAdded()
{
return entitiesAdded;
}
internal HashSet<EntityId> GetEntitiesRemoved()
{
return entitiesRemoved;
}
}
}