This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
MockConnectionHandler.cs
162 lines (128 loc) · 4.76 KB
/
MockConnectionHandler.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
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Improbable.Gdk.Core.NetworkStats;
using Improbable.Worker.CInterop;
namespace Improbable.Gdk.Core
{
public class MockConnectionHandlerBuilder : IConnectionHandlerBuilder
{
public MockConnectionHandler ConnectionHandler;
public MockConnectionHandlerBuilder()
{
ConnectionHandler = new MockConnectionHandler();
}
public Task<IConnectionHandler> CreateAsync(CancellationToken? token = null)
{
return Task.FromResult((IConnectionHandler) ConnectionHandler);
}
public string WorkerType { get; }
}
public class MockConnectionHandler : IConnectionHandler
{
private uint updateId;
private ViewDiff currentDiff => diffs[currentDiffIndex];
private readonly ViewDiff[] diffs = new[]
{
new ViewDiff(),
new ViewDiff()
};
private int currentDiffIndex = 0;
public void CreateEntity(long entityId, EntityTemplate template)
{
var handler = new EntityTemplateDynamicHandler(template, entityId, currentDiff);
Dynamic.ForEachComponent(handler);
}
public void ChangeAuthority(long entityId, uint componentId, Authority newAuthority)
{
currentDiff.SetAuthority(entityId, componentId, newAuthority);
}
public void UpdateComponent<T>(long entityId, uint componentId, T update) where T : ISpatialComponentUpdate
{
currentDiff.AddComponentUpdate(update, entityId, componentId, updateId++);
}
public void AddEvent<T>(long entityId, uint componentId, T ev) where T : IEvent
{
currentDiff.AddEvent(ev, entityId, componentId, updateId++);
}
public void RemoveEntity(long entityId)
{
currentDiff.RemoveEntity(entityId);
}
public void RemoveComponent(long entityId, uint componentId)
{
currentDiff.RemoveComponent(entityId, componentId);
}
public void UpdateComponentAndAddEvents<TUpdate, TEvent>(long entityId, uint componentId, TUpdate update,
params TEvent[] events)
where TUpdate : ISpatialComponentUpdate
where TEvent : IEvent
{
var thisUpdateId = updateId++;
currentDiff.AddComponentUpdate(update, entityId, componentId, thisUpdateId);
foreach (var ev in events)
{
currentDiff.AddEvent(ev, entityId, componentId, thisUpdateId);
}
}
// TODO: Commands
#region IConnectionHandler implementation
public string GetWorkerId()
{
return "TestWorker";
}
public List<string> GetWorkerAttributes()
{
return new List<string> { "attribute_the_first", "attribute_the_second" };
}
public void GetMessagesReceived(ref ViewDiff viewDiff)
{
var diffToReturn = currentDiff;
currentDiffIndex = (currentDiffIndex + 1) % diffs.Length;
var nextDiff = currentDiff;
nextDiff.Clear();
viewDiff = diffToReturn;
}
public MessagesToSend GetMessagesToSendContainer()
{
return new MessagesToSend();
}
public void PushMessagesToSend(MessagesToSend messages, NetFrameStats netFrameStats)
{
throw new System.NotImplementedException();
}
public bool IsConnected()
{
return true;
}
#endregion
private class EntityTemplateDynamicHandler : Dynamic.IHandler
{
private EntityTemplate template;
private ViewDiff viewDiff;
private long entityId;
public EntityTemplateDynamicHandler(EntityTemplate template, long entityId, ViewDiff viewDiff)
{
this.template = template;
this.viewDiff = viewDiff;
this.entityId = entityId;
viewDiff.AddEntity(this.entityId);
}
public void Accept<TUpdate, TSnapshot>(uint componentId, Dynamic.VTable<TUpdate, TSnapshot> vtable)
where TUpdate : struct, ISpatialComponentUpdate
where TSnapshot : struct, ISpatialComponentSnapshot
{
var maybeSnapshot = template.GetComponent<TSnapshot>();
if (!maybeSnapshot.HasValue)
{
return;
}
var snapshot = maybeSnapshot.Value;
viewDiff.AddComponent(vtable.ConvertSnapshotToUpdate(snapshot), entityId, componentId);
}
}
public void Dispose()
{
}
}
}