-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathNetworkUpdateManager.cs
More file actions
328 lines (288 loc) · 10.8 KB
/
NetworkUpdateManager.cs
File metadata and controls
328 lines (288 loc) · 10.8 KB
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
/// About the Network Update Loop
/// The NetworkUpdateEngine is a temporary solution for the network update loop implementation.
/// This will be revised with a more robust and modular implementation in the near future.
using System;
using System.Text;
using UnityEngine;
using UnityEngine.PlayerLoop;
using UnityEngine.LowLevel;
namespace MLAPI
{
/// <summary>
/// Allows one to create their own network update engine
/// in the event they have specific processing needs etc.
/// </summary>
public interface INetworkUpdateEngine
{
void PreUpdate();
void PreUpdateRegister(Action updateAction);
void FixedUpdate();
void FixedUpdateRegister(Action updateAction);
void Update();
void UpdateRegister(Action updateAction);
void PostUpdate();
void PostUpdateRegister(Action updateAction);
}
/// <summary>
/// InternalNetworkUpdateEngine
/// Basic-Default version of an INetworkUpdateEngine implementation
/// This allows for anyone to come up with custom variations, which could include allowing multiple actions to be registered
/// </summary>
internal class InternalNetworkUpdateEngine : INetworkUpdateEngine
{
public Action PreUpdateAction;
public Action PostPreUpdateAction;
public Action FixedUpdateAction;
public Action UpdateAction;
public Action PostUpdateAction;
/// <summary>
/// Handle receiving of any pending packets
/// </summary>
public void PreUpdate()
{
if (PreUpdateAction != null)
{
PreUpdateAction.Invoke();
}
}
public void PreUpdateRegister(Action updateAction)
{
PreUpdateAction = updateAction;
}
public void FixedUpdate()
{
if (FixedUpdateAction != null)
{
FixedUpdateAction.Invoke();
}
}
public void FixedUpdateRegister(Action updateAction)
{
FixedUpdateAction = updateAction;
}
public void Update()
{
if (UpdateAction != null)
{
UpdateAction.Invoke();
}
}
public void UpdateRegister(Action updateAction)
{
UpdateAction = updateAction;
}
public void PostUpdate()
{
if (PostUpdateAction != null)
{
PostUpdateAction.Invoke();
}
}
public void PostUpdateRegister(Action updateAction)
{
PostUpdateAction = updateAction;
}
}
/// <summary>
/// NetworkUpdateManager
/// External public facing class for the registration of and processing of network updates
/// Extended to allow for customized INetworkUpdateEngine implementations
/// </summary>
public class NetworkUpdateManager
{
public enum NetworkUpdateStages
{
PREUPDATE, //Invoked after EarlyUpdate.UnityWebRequestUpdate
FIXEDUPDATE, //Invoked after FixedUpdate.AudioFixedUpdate (prior to any physics being applied or simulated)
UPDATE, //Invoked after PreUpdate.UpdateVideo (just before the primary Update is invoked)
LATEUPDATE //Invoked after PostLateUpdate.ProcessWebSendMessages (after all updates)
}
static INetworkUpdateEngine CurrentNetworkUpdateEngine;
/// <summary>
/// AssignNetworkUpdateEngine
/// Provides the option of passing in a custom network update engine
/// </summary>
/// <param name="updateEngine">INetworkUpdateEngine interface derived class</param>
public static void AssignNetworkUpdateEngine(INetworkUpdateEngine updateEngine)
{
CurrentNetworkUpdateEngine = updateEngine;
}
/// <summary>
/// RegisterNetworkUpdateAction
/// Registers an action to a specific update stage
/// </summary>
/// <param name="updateAction">action to apply</param>
/// <param name="updateStage">update stage to apply the action to</param>
public static void RegisterNetworkUpdateAction(Action updateAction, NetworkUpdateStages updateStage)
{
if (CurrentNetworkUpdateEngine == null)
{
CurrentNetworkUpdateEngine = new InternalNetworkUpdateEngine();
}
switch (updateStage)
{
case NetworkUpdateStages.PREUPDATE:
{
CurrentNetworkUpdateEngine.PreUpdateRegister(updateAction);
break;
}
case NetworkUpdateStages.FIXEDUPDATE:
{
CurrentNetworkUpdateEngine.FixedUpdateRegister(updateAction);
break;
}
case NetworkUpdateStages.UPDATE:
{
CurrentNetworkUpdateEngine.UpdateRegister(updateAction);
break;
}
case NetworkUpdateStages.LATEUPDATE:
{
CurrentNetworkUpdateEngine.PostUpdateRegister(updateAction);
break;
}
}
}
/// <summary>
/// AppStart
/// Initial definition of the four primary network update stages
/// </summary>
[RuntimeInitializeOnLoadMethod]
private static void AppStart()
{
var def = PlayerLoop.GetCurrentPlayerLoop();
if (CurrentNetworkUpdateEngine == null)
{
CurrentNetworkUpdateEngine = new InternalNetworkUpdateEngine();
}
Type currentNUEType = CurrentNetworkUpdateEngine.GetType();
//NetworkUpdateManager Primary PlayerLoop Update Registrations
var networkPreUpdateLoop = new PlayerLoopSystem()
{
updateDelegate = CurrentNetworkUpdateEngine.PreUpdate,
type = currentNUEType
};
InsertSystem(ref def, networkPreUpdateLoop, typeof(EarlyUpdate.UnityWebRequestUpdate));
var networkFixedUpdateLoop = new PlayerLoopSystem()
{
updateDelegate = CurrentNetworkUpdateEngine.FixedUpdate,
type = currentNUEType
};
InsertSystem(ref def, networkFixedUpdateLoop, typeof(FixedUpdate.AudioFixedUpdate));
var networkUpdateLoop = new PlayerLoopSystem()
{
updateDelegate = CurrentNetworkUpdateEngine.Update,
type = currentNUEType
};
InsertSystem(ref def, networkUpdateLoop, typeof(PreUpdate.UpdateVideo));
var networkPostUpdateLoop = new PlayerLoopSystem()
{
updateDelegate = CurrentNetworkUpdateEngine.PostUpdate,
type = currentNUEType
};
InsertSystem(ref def, networkPostUpdateLoop, typeof(PostLateUpdate.ProcessWebSendMessages));
#if UNITY_EDITOR
PrintPlayerLoop(def);
#endif
PlayerLoop.SetPlayerLoop(def);
}
/// <summary>
/// InsertSystem
/// Recursively search for the given system type and insert the new system immediately afterwards
/// </summary>
/// <param name="system">PlayerLoopSystem to search</param>
/// <param name="toInsert">PlayerLoopSystem to insert</param>
/// <param name="insertAfter">location to insert the PlayerLoopSystem</param>
/// <returns></returns>
private static bool InsertSystem(ref PlayerLoopSystem system, PlayerLoopSystem toInsert, Type insertAfter)
{
if (system.subSystemList == null)
{
return false;
}
for (int i = 0; i < system.subSystemList.Length; ++i)
{
if (system.subSystemList[i].type == insertAfter)
{
InsertSystemAt(ref system, toInsert, i + 1);
return true;
}
}
for (var i = 0; i < system.subSystemList.Length; i++)
{
if (InsertSystem(ref system.subSystemList[i], toInsert, insertAfter))
{
return true;
}
}
return false;
}
/// <summary>
/// InsertSystemAt
/// Copies the subSystemList of the given system and inserts a new system at the given index
/// </summary>
/// <param name="system">PlayerLoopSystem to be inserted into</param>
/// <param name="toInsert">PlayerLoopSystem to insert</param>
/// <param name="pos">position to insert the PlayerLoopSystem</param>
private static void InsertSystemAt(ref PlayerLoopSystem system, PlayerLoopSystem toInsert, int pos)
{
PlayerLoopSystem[] newSubSystems = new PlayerLoopSystem[system.subSystemList.Length + 1];
for (int i = 0, oldSystemIdx = 0; i < newSubSystems.Length; ++i)
{
if (i == pos)
{
newSubSystems[i] = toInsert;
}
else
{
newSubSystems[i] = system.subSystemList[oldSystemIdx++];
}
}
system.subSystemList = newSubSystems;
}
#if UNITY_EDITOR
/// <summary>
/// PrintPlayerLoop
/// Prints all PlayerLoopSystems within the PlayerLoopSystem provided
/// </summary>
/// <param name="pl">PlayerLoopSystem</param>
private static void PrintPlayerLoop(PlayerLoopSystem pl)
{
var sb = new StringBuilder();
RecursivePlayerLoopPrint(pl, sb, 0);
Debug.Log(sb.ToString());
}
/// <summary>
/// RecursivePlayerLoopPrint
/// Recursively build the entire PlayerLoopSystem list
/// </summary>
/// <param name="def">PlayerLoopSystem to be added</param>
/// <param name="sb">StringBuilder to add to</param>
/// <param name="depth">Maximum recursion depth</param>
private static void RecursivePlayerLoopPrint(PlayerLoopSystem def, StringBuilder sb, int depth)
{
if (depth == 0)
{
sb.AppendLine("ROOT NODE");
}
else if (def.type != null)
{
for (int i = 0; i < depth; i++)
{
sb.Append("\t");
}
sb.AppendLine(def.type.Name);
}
if (def.subSystemList != null)
{
depth++;
foreach (var s in def.subSystemList)
{
RecursivePlayerLoopPrint(s, sb, depth);
}
depth--;
}
}
#endif
}
}