Skip to content

Commit 755009c

Browse files
WIP
Starting to get new network update loop registration into place.
1 parent 548280b commit 755009c

2 files changed

Lines changed: 99 additions & 1 deletion

File tree

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
11
using System;
22
using System.Text;
3+
using System.Collections.Generic;
34
using UnityEngine;
45
using UnityEngine.PlayerLoop;
56
using UnityEngine.LowLevel;
67

78
namespace MLAPI
89
{
10+
11+
public struct NetworkLoopUpdateRegistration
12+
{
13+
NetworkUpdateManager.NetworkUpdateLoopCallbackFunction updateCallback;
14+
15+
}
16+
17+
/// <summary>
18+
/// Allows one to create their own network update engine
19+
/// in the event they have specific processing needs etc.
20+
/// </summary>
21+
public interface INetworkUpdateLoopSystem
22+
{
23+
24+
25+
NetworkUpdateManager.NetworkUpdateLoopCallbackFunction RegisterUpdate(NetworkUpdateManager.NetworkUpdateStages stage );
26+
27+
}
28+
929
/// <summary>
1030
/// Allows one to create their own network update engine
1131
/// in the event they have specific processing needs etc.
@@ -113,6 +133,9 @@ public enum NetworkUpdateStages
113133

114134
static INetworkUpdateEngine CurrentNetworkUpdateEngine;
115135

136+
137+
public delegate void NetworkUpdateLoopCallbackFunction();
138+
116139
/// <summary>
117140
/// AssignNetworkUpdateEngine
118141
/// Provides the option of passing in a custom network update engine
@@ -123,6 +146,39 @@ public static void AssignNetworkUpdateEngine(INetworkUpdateEngine updateEngine)
123146
CurrentNetworkUpdateEngine = updateEngine;
124147
}
125148

149+
static void RegisterNetworkLoopSystems(Dictionary<NetworkUpdateStages,PlayerLoopSystem> systems)
150+
{
151+
152+
}
153+
154+
public static void HandleNetworkLoopRegistrations(List<INetworkUpdateLoopSystem> networkLoopSystems)
155+
{
156+
foreach(INetworkUpdateLoopSystem loopSystem in networkLoopSystems)
157+
{
158+
Dictionary<NetworkUpdateStages,PlayerLoopSystem> RegisterPlayerLoopSystems = new Dictionary<NetworkUpdateStages, PlayerLoopSystem>();
159+
160+
foreach(NetworkUpdateStages stage in Enum.GetValues(typeof(NetworkUpdateStages)))
161+
{
162+
NetworkUpdateLoopCallbackFunction updateFunction = loopSystem.RegisterUpdate(stage);
163+
if(updateFunction != null)
164+
{
165+
PlayerLoopSystem.UpdateFunction callback = new PlayerLoopSystem.UpdateFunction(updateFunction);
166+
PlayerLoopSystem stageLoop = new PlayerLoopSystem() { updateDelegate = callback, type = loopSystem.GetType() };
167+
if(stageLoop.updateDelegate != null)
168+
{
169+
RegisterPlayerLoopSystems.Add(stage, stageLoop);
170+
}
171+
}
172+
}
173+
if(RegisterPlayerLoopSystems.Count > 0)
174+
{
175+
RegisterNetworkLoopSystems(RegisterPlayerLoopSystems);
176+
}
177+
}
178+
179+
}
180+
181+
126182
/// <summary>
127183
/// RegisterNetworkUpdateAction
128184
/// Registers an action to a specific update stage

com.unity.multiplayer.mlapi/Runtime/Core/NetworkingManager.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.ComponentModel;
55
using UnityEngine;
6+
using UnityEngine.LowLevel;
67
using System.Linq;
78
using MLAPI.Logging;
89
using UnityEngine.SceneManagement;
@@ -34,7 +35,7 @@ namespace MLAPI
3435
/// The main component of the library
3536
/// </summary>
3637
[AddComponentMenu("MLAPI/NetworkingManager", -100)]
37-
public class NetworkingManager : MonoBehaviour
38+
public class NetworkingManager : MonoBehaviour,INetworkUpdateLoopSystem
3839
{
3940
// RuntimeAccessModifiersILPP will make this `public`
4041
internal static readonly Dictionary<uint, Action<NetworkedBehaviour, BitReader, ulong>> __ntable = new Dictionary<uint, Action<NetworkedBehaviour, BitReader, ulong>>();
@@ -72,6 +73,10 @@ public class NetworkingManager : MonoBehaviour
7273

7374
public RPCQueueManager RpcQueueManager { get; private set; }
7475

76+
77+
78+
79+
7580
/// <summary>
7681
/// A synchronized time, represents the time in seconds since the server application started. Is replicated across all clients
7782
/// </summary>
@@ -697,13 +702,50 @@ private void Shutdown()
697702
}
698703

699704

705+
public List<INetworkUpdateLoopSystem> NetworkLoopUpdateSystems;
706+
707+
public NetworkUpdateManager.NetworkUpdateLoopCallbackFunction RegisterUpdate(NetworkUpdateManager.NetworkUpdateStages stage )
708+
{
709+
NetworkUpdateManager.NetworkUpdateLoopCallbackFunction networkUpdateLoopCallback = null;
710+
switch(stage)
711+
{
712+
case NetworkUpdateManager.NetworkUpdateStages.PREUPDATE:
713+
{
714+
networkUpdateLoopCallback = NetworkPreUpdate;
715+
break;
716+
}
717+
case NetworkUpdateManager.NetworkUpdateStages.FIXEDUPDATE:
718+
{
719+
networkUpdateLoopCallback = NetworkFixedUpdate;
720+
break;
721+
}
722+
case NetworkUpdateManager.NetworkUpdateStages.UPDATE:
723+
{
724+
networkUpdateLoopCallback = NetworkUpdate;
725+
break;
726+
}
727+
case NetworkUpdateManager.NetworkUpdateStages.LATEUPDATE:
728+
{
729+
networkUpdateLoopCallback = NetworkLateUpdate;
730+
break;
731+
}
732+
}
733+
return networkUpdateLoopCallback;
734+
}
735+
736+
700737
private void Awake()
701738
{
739+
//We always add the networking manager as the first entry
740+
NetworkLoopUpdateSystems.Insert(0, this);
741+
702742
RpcQueueManager = new RPCQueueManager(LoopbackEnabled);
703743
//Note: Since frame history is not being used, this is set to 0
704744
//To test frame history, increase the number to (n) where n > 0
705745
RpcQueueManager?.Initialize(0);
706746

747+
NetworkUpdateManager.HandleNetworkLoopRegistrations(NetworkLoopUpdateSystems);
748+
707749
NetworkUpdateManager.RegisterNetworkUpdateAction(NetworkPreUpdate, NetworkUpdateManager.NetworkUpdateStages.PREUPDATE);
708750
NetworkUpdateManager.RegisterNetworkUpdateAction(NetworkFixedUpdate, NetworkUpdateManager.NetworkUpdateStages.FIXEDUPDATE);
709751
NetworkUpdateManager.RegisterNetworkUpdateAction(NetworkUpdate, NetworkUpdateManager.NetworkUpdateStages.UPDATE);

0 commit comments

Comments
 (0)