Skip to content

Commit 0f9e083

Browse files
NETC-8
Cleaning up the RPCQueue branch for PR. (first pass)
1 parent c59bc72 commit 0f9e083

24 files changed

Lines changed: 2192 additions & 254 deletions

com.unity.multiplayer.mlapi/Editor/NetworkingManagerEditor.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
public class NetworkingManagerEditor : Editor
1313
{
1414
// Properties
15+
private SerializedProperty runInLoopbackModeProperty;
1516
private SerializedProperty dontDestroyOnLoadProperty;
1617
private SerializedProperty runInBackgroundProperty;
1718
private SerializedProperty logLevelProperty;
@@ -26,6 +27,7 @@ public class NetworkingManagerEditor : Editor
2627
private SerializedProperty receiveTickrateProperty;
2728
private SerializedProperty maxReceiveEventsPerTickRateProperty;
2829
private SerializedProperty eventTickrateProperty;
30+
private SerializedProperty maxObjectUpdatesPerTickProperty;
2931
private SerializedProperty clientConnectionBufferTimeoutProperty;
3032
private SerializedProperty connectionApprovalProperty;
3133
private SerializedProperty secondsHistoryProperty;
@@ -94,6 +96,7 @@ private void Init()
9496
networkingManager = (NetworkingManager)target;
9597

9698
// Base properties
99+
runInLoopbackModeProperty = serializedObject.FindProperty("LoopbackEnabled");
97100
dontDestroyOnLoadProperty = serializedObject.FindProperty("DontDestroy");
98101
runInBackgroundProperty = serializedObject.FindProperty("RunInBackground");
99102
logLevelProperty = serializedObject.FindProperty("LogLevel");
@@ -134,6 +137,7 @@ private void Init()
134137
private void CheckNullProperties()
135138
{
136139
// Base properties
140+
runInLoopbackModeProperty = serializedObject.FindProperty("LoopbackEnabled");
137141
dontDestroyOnLoadProperty = serializedObject.FindProperty("DontDestroy");
138142
runInBackgroundProperty = serializedObject.FindProperty("RunInBackground");
139143
logLevelProperty = serializedObject.FindProperty("LogLevel");
@@ -247,7 +251,7 @@ public override void OnInspectorGUI()
247251
if (!networkingManager.IsServer && !networkingManager.IsClient)
248252
{
249253
serializedObject.Update();
250-
254+
EditorGUILayout.PropertyField(runInLoopbackModeProperty);
251255
EditorGUILayout.PropertyField(dontDestroyOnLoadProperty);
252256
EditorGUILayout.PropertyField(runInBackgroundProperty);
253257
EditorGUILayout.PropertyField(logLevelProperty);
@@ -305,6 +309,11 @@ public override void OnInspectorGUI()
305309

306310
using (new EditorGUI.DisabledScope(!networkingManager.NetworkConfig.EnableNetworkedVar))
307311
{
312+
if(maxObjectUpdatesPerTickProperty != null)
313+
{
314+
EditorGUILayout.PropertyField(maxObjectUpdatesPerTickProperty);
315+
}
316+
308317
EditorGUILayout.PropertyField(ensureNetworkedVarLengthSafetyProperty);
309318
}
310319

Lines changed: 320 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,320 @@
1+
using System;
2+
using System.Text;
3+
using UnityEngine;
4+
using UnityEngine.PlayerLoop;
5+
using UnityEngine.LowLevel;
6+
7+
namespace MLAPI
8+
{
9+
/// <summary>
10+
/// Allows one to create their own network update engine
11+
/// in the event they have specific processing needs etc.
12+
/// </summary>
13+
public interface INetworkUpdateEngine
14+
{
15+
void PreUpdate();
16+
void PreUpdateRegister(Action updateAction);
17+
18+
void FixedUpdate();
19+
void FixedUpdateRegister(Action updateAction);
20+
21+
void Update();
22+
void UpdateRegister(Action updateAction);
23+
24+
void PostUpdate();
25+
void PostUpdateRegister(Action updateAction);
26+
27+
}
28+
29+
/// <summary>
30+
/// InternalNetworkUpdateEngine
31+
/// Basic-Default version of an INetworkUpdateEngine implementation
32+
/// This allows for anyone to come up with custom variations, which could include allowing multiple actions to be registered
33+
/// </summary>
34+
internal class InternalNetworkUpdateEngine:INetworkUpdateEngine
35+
{
36+
public Action PreUpdateAction;
37+
public Action PostPreUpdateAction;
38+
public Action FixedUpdateAction;
39+
public Action UpdateAction;
40+
public Action PostUpdateAction;
41+
42+
/// <summary>
43+
/// Handle receiving of any pending packets
44+
/// </summary>
45+
public void PreUpdate()
46+
{
47+
if(PreUpdateAction != null)
48+
{
49+
PreUpdateAction.Invoke();
50+
}
51+
}
52+
53+
public void PreUpdateRegister(Action updateAction)
54+
{
55+
PreUpdateAction = updateAction;
56+
}
57+
58+
public void FixedUpdate()
59+
{
60+
if(FixedUpdateAction != null)
61+
{
62+
FixedUpdateAction.Invoke();
63+
}
64+
}
65+
public void FixedUpdateRegister(Action updateAction)
66+
{
67+
FixedUpdateAction = updateAction;
68+
}
69+
70+
public void Update()
71+
{
72+
if(UpdateAction != null)
73+
{
74+
UpdateAction.Invoke();
75+
}
76+
}
77+
public void UpdateRegister(Action updateAction)
78+
{
79+
UpdateAction = updateAction;
80+
}
81+
public void PostUpdate()
82+
{
83+
84+
if(PostUpdateAction != null)
85+
{
86+
PostUpdateAction.Invoke();
87+
}
88+
}
89+
90+
public void PostUpdateRegister(Action updateAction)
91+
{
92+
PostUpdateAction = updateAction;
93+
}
94+
}
95+
96+
97+
/// <summary>
98+
/// NetworkUpdateManager
99+
/// External public facing class for the registration of and processing of network updates
100+
/// Extended to allow for customized INetworkUpdateEngine implementations
101+
/// </summary>
102+
public class NetworkUpdateManager
103+
{
104+
public enum NetworkUpdateStages
105+
{
106+
PREUPDATE, //Invoked after EarlyUpdate.UnityWebRequestUpdate
107+
FIXEDUPDATE, //Invoked after FixedUpdate.AudioFixedUpdate (prior to any physics being applied or simulated)
108+
UPDATE, //Invoked after PreUpdate.UpdateVideo (just before the primary Update is invoked)
109+
LATEUPDATE //Invoked after PostLateUpdate.ProcessWebSendMessages (after all updates)
110+
}
111+
112+
static INetworkUpdateEngine CurrentNetworkUpdateEngine;
113+
114+
/// <summary>
115+
/// AssignNetworkUpdateEngine
116+
/// Provides the option of passing in a custom network update engine
117+
/// </summary>
118+
/// <param name="updateEngine">INetworkUpdateEngine interface derived class</param>
119+
public static void AssignNetworkUpdateEngine(INetworkUpdateEngine updateEngine)
120+
{
121+
CurrentNetworkUpdateEngine = updateEngine;
122+
}
123+
124+
/// <summary>
125+
/// RegisterNetworkUpdateAction
126+
/// Registers an action to a specific update stage
127+
/// </summary>
128+
/// <param name="updateAction">action to apply</param>
129+
/// <param name="updateStage">update stage to apply the action to</param>
130+
public static void RegisterNetworkUpdateAction(Action updateAction, NetworkUpdateStages updateStage)
131+
{
132+
if(CurrentNetworkUpdateEngine == null)
133+
{
134+
CurrentNetworkUpdateEngine = new InternalNetworkUpdateEngine();
135+
}
136+
137+
switch(updateStage)
138+
{
139+
case NetworkUpdateStages.PREUPDATE:
140+
{
141+
CurrentNetworkUpdateEngine.PreUpdateRegister(updateAction);
142+
break;
143+
}
144+
case NetworkUpdateStages.FIXEDUPDATE:
145+
{
146+
CurrentNetworkUpdateEngine.FixedUpdateRegister(updateAction);
147+
break;
148+
}
149+
case NetworkUpdateStages.UPDATE:
150+
{
151+
CurrentNetworkUpdateEngine.UpdateRegister(updateAction);
152+
break;
153+
}
154+
case NetworkUpdateStages.LATEUPDATE:
155+
{
156+
CurrentNetworkUpdateEngine.PostUpdateRegister(updateAction);
157+
break;
158+
}
159+
}
160+
}
161+
162+
/// <summary>
163+
/// AppStart
164+
/// Initial definition of the four primary network update stages
165+
/// </summary>
166+
[RuntimeInitializeOnLoadMethod]
167+
private static void AppStart()
168+
{
169+
var def = PlayerLoop.GetCurrentPlayerLoop();
170+
171+
if(CurrentNetworkUpdateEngine == null)
172+
{
173+
CurrentNetworkUpdateEngine = new InternalNetworkUpdateEngine();
174+
}
175+
176+
Type currentNUEType = CurrentNetworkUpdateEngine.GetType();
177+
178+
//NetworkUpdateManager Primary PlayerLoop Update Registrations
179+
var networkPreUpdateLoop = new PlayerLoopSystem()
180+
{
181+
updateDelegate = CurrentNetworkUpdateEngine.PreUpdate,
182+
type = currentNUEType
183+
};
184+
InsertSystem(ref def, networkPreUpdateLoop, typeof(EarlyUpdate.UnityWebRequestUpdate));
185+
186+
var networkFixedUpdateLoop = new PlayerLoopSystem()
187+
{
188+
updateDelegate = CurrentNetworkUpdateEngine.FixedUpdate,
189+
type = currentNUEType
190+
};
191+
InsertSystem(ref def, networkFixedUpdateLoop, typeof(FixedUpdate.AudioFixedUpdate));
192+
193+
var networkUpdateLoop = new PlayerLoopSystem()
194+
{
195+
updateDelegate = CurrentNetworkUpdateEngine.Update,
196+
type = currentNUEType
197+
};
198+
InsertSystem(ref def, networkUpdateLoop, typeof(PreUpdate.UpdateVideo));
199+
200+
var networkPostUpdateLoop = new PlayerLoopSystem()
201+
{
202+
updateDelegate = CurrentNetworkUpdateEngine.PostUpdate,
203+
type = currentNUEType
204+
};
205+
InsertSystem(ref def, networkPostUpdateLoop, typeof(PostLateUpdate.ProcessWebSendMessages));
206+
207+
#if UNITY_EDITOR
208+
PrintPlayerLoop(def);
209+
#endif
210+
211+
PlayerLoop.SetPlayerLoop(def);
212+
}
213+
214+
/// <summary>
215+
/// InsertSystem
216+
/// Recursively search for the given system type and insert the new system immediately afterwards
217+
/// </summary>
218+
/// <param name="system">PlayerLoopSystem to search</param>
219+
/// <param name="toInsert">PlayerLoopSystem to insert</param>
220+
/// <param name="insertAfter">location to insert the PlayerLoopSystem</param>
221+
/// <returns></returns>
222+
private static bool InsertSystem(ref PlayerLoopSystem system, PlayerLoopSystem toInsert, Type insertAfter)
223+
{
224+
if (system.subSystemList == null)
225+
{
226+
return false;
227+
}
228+
229+
for (int i = 0; i < system.subSystemList.Length; ++i)
230+
{
231+
if (system.subSystemList[i].type == insertAfter)
232+
{
233+
InsertSystemAt(ref system, toInsert, i + 1);
234+
return true;
235+
}
236+
}
237+
238+
for (var i = 0; i < system.subSystemList.Length; i++)
239+
{
240+
if (InsertSystem(ref system.subSystemList[i], toInsert, insertAfter))
241+
{
242+
return true;
243+
}
244+
}
245+
return false;
246+
}
247+
248+
/// <summary>
249+
/// InsertSystemAt
250+
/// Copies the subSystemList of the given system and inserts a new system at the given index
251+
/// </summary>
252+
/// <param name="system">PlayerLoopSystem to be inserted into</param>
253+
/// <param name="toInsert">PlayerLoopSystem to insert</param>
254+
/// <param name="pos">position to insert the PlayerLoopSystem</param>
255+
private static void InsertSystemAt(ref PlayerLoopSystem system, PlayerLoopSystem toInsert, int pos)
256+
{
257+
PlayerLoopSystem[] newSubSystems = new PlayerLoopSystem[system.subSystemList.Length + 1];
258+
for (int i = 0, oldSystemIdx = 0; i < newSubSystems.Length; ++i)
259+
{
260+
if (i == pos)
261+
{
262+
newSubSystems[i] = toInsert;
263+
}
264+
else
265+
{
266+
newSubSystems[i] = system.subSystemList[oldSystemIdx++];
267+
}
268+
}
269+
270+
system.subSystemList = newSubSystems;
271+
}
272+
273+
#if UNITY_EDITOR
274+
/// <summary>
275+
/// PrintPlayerLoop
276+
/// Prints all PlayerLoopSystems within the PlayerLoopSystem provided
277+
/// </summary>
278+
/// <param name="pl">PlayerLoopSystem</param>
279+
private static void PrintPlayerLoop(PlayerLoopSystem pl)
280+
{
281+
var sb = new StringBuilder();
282+
RecursivePlayerLoopPrint(pl, sb, 0);
283+
Debug.Log(sb.ToString());
284+
}
285+
286+
/// <summary>
287+
/// RecursivePlayerLoopPrint
288+
/// Recursively build the entire PlayerLoopSystem list
289+
/// </summary>
290+
/// <param name="def">PlayerLoopSystem to be added</param>
291+
/// <param name="sb">StringBuilder to add to</param>
292+
/// <param name="depth">Maximum recursion depth</param>
293+
private static void RecursivePlayerLoopPrint(PlayerLoopSystem def, StringBuilder sb, int depth)
294+
{
295+
if (depth == 0)
296+
{
297+
sb.AppendLine("ROOT NODE");
298+
}
299+
else if (def.type != null)
300+
{
301+
for (int i = 0; i < depth; i++)
302+
{
303+
sb.Append("\t");
304+
}
305+
sb.AppendLine(def.type.Name);
306+
}
307+
if (def.subSystemList != null)
308+
{
309+
depth++;
310+
foreach (var s in def.subSystemList)
311+
{
312+
RecursivePlayerLoopPrint(s, sb, depth);
313+
}
314+
depth--;
315+
}
316+
}
317+
#endif
318+
319+
}
320+
}

com.unity.multiplayer.mlapi/Runtime/Core/NetworkUpdateManager.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)