Skip to content

Commit 85187bf

Browse files
committed
Expanded NetworkedVar foundation to mimic the SyncedVar layout
1 parent 3540c9f commit 85187bf

File tree

7 files changed

+146
-55
lines changed

7 files changed

+146
-55
lines changed

MLAPI/Data/NetworkedCollections/NetworkedList.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal enum NetworkedListEventType
2929
private NetworkedBehaviour networkedBehaviour;
3030
public NetworkedVarSettings Settings = new NetworkedVarSettings();
3131

32-
public void OnSynced()
32+
public void ResetDirty()
3333
{
3434
dirtyEvents.Clear();
3535
}
@@ -218,4 +218,4 @@ public T this[int index]
218218
}
219219
}
220220
}
221-
}
221+
}

MLAPI/Data/NetworkedVar.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public T Value
3434
}
3535
}
3636

37-
public void OnSynced()
37+
public void ResetDirty()
3838
{
3939
isDirty = false;
4040
LastSyncedTime = NetworkingManager.singleton.NetworkTime;
@@ -110,7 +110,7 @@ public void WriteFieldToWriter(BitWriter writer)
110110

111111
public interface INetworkedVar
112112
{
113-
void OnSynced();
113+
void ResetDirty();
114114
bool IsDirty();
115115
bool CanClientWrite(uint clientId);
116116
bool CanClientRead(uint clientId);

MLAPI/Data/TypeExtensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
namespace MLAPI.Data
4+
{
5+
internal static class TypeExtensions
6+
{
7+
internal static bool HasInterface(this Type type, Type interfaceType)
8+
{
9+
Type[] interfaces = type.GetInterfaces();
10+
for (int i = 0; i < interfaces.Length; i++)
11+
{
12+
if (interfaces[i] == interfaceType)
13+
return true;
14+
}
15+
return false;
16+
}
17+
}
18+
}

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,8 @@ private bool SetDirtyness()
927927

928928
#region NetworkedVar
929929

930-
bool networkedVarInit = false;
931-
List<INetworkedVar> networkedVarFields = new List<INetworkedVar>();
930+
private bool networkedVarInit = false;
931+
internal readonly List<INetworkedVar> networkedVarFields = new List<INetworkedVar>();
932932
internal void NetworkedVarInit()
933933
{
934934
if (networkedVarInit)
@@ -939,32 +939,58 @@ internal void NetworkedVarInit()
939939
for (int i = 0; i < sortedFields.Length; i++)
940940
{
941941
Type fieldType = sortedFields[i].FieldType;
942-
if (fieldType == typeof(NetworkedVar<>))
942+
if (fieldType.HasInterface(typeof(INetworkedVar)))
943943
{
944-
Type genericTypeDefinition = typeof(NetworkedVar<>);
945-
Type genericType = genericTypeDefinition.MakeGenericType(fieldType.GetGenericArguments());
946-
INetworkedVar instance = (INetworkedVar)Activator.CreateInstance(genericType, true);
947-
948-
sortedFields[i].SetValue(this, instance);
944+
INetworkedVar instance = null;
945+
if (sortedFields[i].GetValue(this) == null)
946+
{
947+
Type genericType = fieldType.MakeGenericType(fieldType.GetGenericArguments());
948+
instance = (INetworkedVar)Activator.CreateInstance(genericType, true);
949+
sortedFields[i].SetValue(this, instance);
950+
}
951+
else
952+
{
953+
instance = (INetworkedVar)sortedFields[i].GetValue(this);
954+
}
949955
instance.SetNetworkedBehaviour(this);
950-
951-
networkedVarFields.Add((INetworkedVar)instance);
956+
networkedVarFields.Add(instance);
952957
}
953958
}
954959
}
955960

956-
internal void NetworkedVarPrepareSend()
961+
internal void NetworkedVarUpdate()
957962
{
958-
//TODO: Loop all the fields, write dirty fields and send them to the transport.
959963
//TODO: Do this efficiently.
960-
//Avoid: One loop for the networkedFields and then one per client per networkedField iteration.
961-
962-
//Flow:
963-
//Check if dirty, send to clients that have read access
964-
//Call OnSynced
964+
965+
for (int i = 0; i < NetworkingManager.singleton.ConnectedClientsList.Count; i++)
966+
{
967+
using (BitWriter writer = BitWriter.Get())
968+
{
969+
uint clientId = NetworkingManager.singleton.ConnectedClientsList[i].ClientId;
970+
for (int j = 0; j < networkedVarFields.Count; j++)
971+
{
972+
bool isDirty = networkedVarFields[j].IsDirty(); //cache this here. You never know what operations users will do in the dirty methods
973+
writer.WriteBool(isDirty);
974+
if (isDirty && (!isServer || networkedVarFields[j].CanClientRead(clientId)))
975+
{
976+
networkedVarFields[j].WriteDeltaToWriter(writer);
977+
}
978+
}
979+
980+
if (isServer)
981+
InternalMessageHandler.Send(clientId, "MLAPI_NETWORKED_VAR_DELTA", "MLAPI_INTERNAL", writer, null);
982+
else
983+
InternalMessageHandler.Send(NetworkingManager.singleton.NetworkConfig.NetworkTransport.ServerNetId, "MLAPI_NETWORKED_VAR_DELTA", "MLAPI_INTERNAL", writer, null);
984+
}
985+
}
986+
987+
for (int i = 0; i < networkedVarFields.Count; i++)
988+
{
989+
networkedVarFields[i].ResetDirty();
990+
}
965991
}
966992

967-
internal void HandleNetworkedVarChangedByRemote(BitReader reader)
993+
internal void HandleNetworkedVarDeltas(BitReader reader)
968994
{
969995
ushort index = reader.ReadUShort();
970996
if (index >= networkedVarFields.Count)
@@ -973,22 +999,20 @@ internal void HandleNetworkedVarChangedByRemote(BitReader reader)
973999
return;
9741000
}
9751001

976-
networkedVarFields[index].SetFieldFromReader(reader);
1002+
networkedVarFields[index].SetDeltaFromReader(reader);
9771003
}
9781004

979-
internal void SendNetworkedVar(INetworkedVar networkedVar, BitWriter varWriter)
1005+
internal void HandleNetworkedVarUpdate(BitReader reader)
9801006
{
981-
using (BitWriter writer = BitWriter.Get())
1007+
ushort index = reader.ReadUShort();
1008+
if (index >= networkedVarFields.Count)
9821009
{
983-
writer.WriteUShort(GetNetworkedVarIndex(networkedVar));
984-
writer.WriteWriter(varWriter);
985-
986-
if (isClient)
987-
InternalMessageHandler.Send(NetworkingManager.singleton.NetworkConfig.NetworkTransport.ServerNetId, "MLAPI_NETWORKED_VAR_UPDATE", "MLAPI_INTERNAL", writer, null, networkId, networkedObject.GetOrderIndex(this));
988-
else
989-
InternalMessageHandler.Send(OwnerClientId, "MLAPI_NETWORKED_VAR_UPDATE", "MLAPI_INTERNAL", writer, null, networkId, networkedObject.GetOrderIndex(this));
1010+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("NetworkedVar index out of range");
1011+
return;
9901012
}
991-
}
1013+
1014+
networkedVarFields[index].SetFieldFromReader(reader);
1015+
}
9921016

9931017
internal ushort GetNetworkedVarIndex(INetworkedVar networkedVar)
9941018
{

MLAPI/MonoBehaviours/Core/NetworkedObject.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,33 @@ internal static void NetworkedVarPrepareSend()
285285
{
286286
for (int i = 0; i < NetworkedBehaviours.Count; i++)
287287
{
288-
NetworkedBehaviours[i].NetworkedVarPrepareSend();
288+
NetworkedBehaviours[i].NetworkedVarUpdate();
289+
}
290+
}
291+
292+
internal void WriteNetworkedVarData(BitWriter writer)
293+
{
294+
for (int i = 0; i < childNetworkedBehaviours.Count; i++)
295+
{
296+
childNetworkedBehaviours[i].NetworkedVarInit();
297+
if (childNetworkedBehaviours[i].networkedVarFields.Count == 0)
298+
continue;
299+
writer.WriteUShort(GetOrderIndex(childNetworkedBehaviours[i])); //Write the behaviourId
300+
for (int j = 0; j < childNetworkedBehaviours[i].networkedVarFields.Count; j++)
301+
childNetworkedBehaviours[i].networkedVarFields[j].WriteFieldToWriter(writer);
302+
}
303+
}
304+
305+
internal void SetNetworkedVarData(BitReader reader)
306+
{
307+
for (int i = 0; i < childNetworkedBehaviours.Count; i++)
308+
{
309+
childNetworkedBehaviours[i].NetworkedVarInit();
310+
if (childNetworkedBehaviours[i].networkedVarFields.Count == 0)
311+
continue;
312+
NetworkedBehaviour behaviour = GetBehaviourAtOrderIndex(reader.ReadUShort());
313+
for (int j = 0; j < childNetworkedBehaviours[i].networkedVarFields.Count; j++)
314+
childNetworkedBehaviours[i].networkedVarFields[j].SetFieldFromReader(reader);
289315
}
290316
}
291317

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ private object Init(bool server)
320320
MessageManager.messageTypes.Add("MLAPI_RPC", 13);
321321
MessageManager.messageTypes.Add("MLAPI_TARGET", 14);
322322
MessageManager.messageTypes.Add("MLAPI_SET_VISIBILITY", 15);
323-
MessageManager.messageTypes.Add("MLAPI_NETWORKED_VAR_UPDATE", 16);
323+
MessageManager.messageTypes.Add("MLAPI_NETWORKED_VAR_DELTA", 16);
324+
MessageManager.messageTypes.Add("MLAPI_NETWORKED_VAR_UPDATE", 17);
324325

325326
List<MessageType> messageTypes = new List<MessageType>(NetworkConfig.MessageTypes)
326327
{
@@ -994,7 +995,10 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId, uint
994995
break;
995996
case 16:
996997
// Handled on both client and server
997-
InternalMessageHandler.HandleNetworkedVarChangedByRemote(clientId, messageReader, channelId);
998+
InternalMessageHandler.HandleNetworkedVarDelta(clientId, messageReader, channelId);
999+
break;
1000+
case 17:
1001+
InternalMessageHandler.HandleNetworkedVarUpdate(clientId, messageReader, channelId);
9981002
break;
9991003
}
10001004
#endregion

MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Receive.cs

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -246,25 +246,6 @@ internal static void HandleSyncVarUpdate(uint clientId, BitReader reader, int ch
246246
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate();
247247
}
248248

249-
internal static void HandleNetworkedVarChangedByRemote(uint clientId, BitReader reader, int channelId)
250-
{
251-
uint netId = reader.ReadUInt();
252-
ushort orderIndex = reader.ReadUShort();
253-
254-
if (!SpawnManager.spawnedObjects.ContainsKey(netId))
255-
{
256-
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("NetworkedVar message recieved for a non existant object with id: " + netId);
257-
return;
258-
}
259-
else if (SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex) == null)
260-
{
261-
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("NetworkedVar message recieved for a non existant behaviour");
262-
return;
263-
}
264-
265-
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).HandleNetworkedVarChangedByRemote(reader);
266-
}
267-
268249
internal static void HandleAddObjects(uint clientId, BitReader reader, int channelId)
269250
{
270251
if (netManager.NetworkConfig.HandleObjectSpawning)
@@ -380,5 +361,43 @@ internal static void HandleSetVisibility(uint clientId, BitReader reader, int ch
380361
SpawnManager.spawnedObjects[networkId].SetFormattedSyncedVarData(reader);
381362
SpawnManager.spawnedObjects[networkId].SetLocalVisibility(visibility);
382363
}
364+
365+
internal static void HandleNetworkedVarDelta(uint clientId, BitReader reader, int channelId)
366+
{
367+
uint netId = reader.ReadUInt();
368+
ushort orderIndex = reader.ReadUShort();
369+
370+
if (!SpawnManager.spawnedObjects.ContainsKey(netId))
371+
{
372+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("NetworkedVar message recieved for a non existant object with id: " + netId);
373+
return;
374+
}
375+
else if (SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex) == null)
376+
{
377+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("NetworkedVar message recieved for a non existant behaviour");
378+
return;
379+
}
380+
381+
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).HandleNetworkedVarDeltas(reader);
382+
}
383+
384+
internal static void HandleNetworkedVarUpdate(uint clientId, BitReader reader, int channelId)
385+
{
386+
uint netId = reader.ReadUInt();
387+
ushort orderIndex = reader.ReadUShort();
388+
389+
if (!SpawnManager.spawnedObjects.ContainsKey(netId))
390+
{
391+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("NetworkedVar message recieved for a non existant object with id: " + netId);
392+
return;
393+
}
394+
else if (SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex) == null)
395+
{
396+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("NetworkedVar message recieved for a non existant behaviour");
397+
return;
398+
}
399+
400+
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).HandleNetworkedVarUpdate(reader);
401+
}
383402
}
384403
}

0 commit comments

Comments
 (0)