Skip to content

Commit 8dd3842

Browse files
committed
Added per NetworkedVar channel
1 parent 065ac9f commit 8dd3842

File tree

6 files changed

+81
-18
lines changed

6 files changed

+81
-18
lines changed

MLAPI/Data/NetworkedCollections/NetworkedDictionary.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using MLAPI.MonoBehaviours.Core;
55
using MLAPI.NetworkingManagerComponents.Binary;
6+
using MLAPI.NetworkingManagerComponents.Core;
67

78
namespace MLAPI.Data.NetworkedCollections
89
{
@@ -34,6 +35,11 @@ public void ResetDirty()
3435
dirtyEvents.Clear();
3536
}
3637

38+
public string GetChannel()
39+
{
40+
return Settings.SendChannel;
41+
}
42+
3743
public void SetDeltaFromReader(BitReader reader)
3844
{
3945
ushort deltaCount = reader.ReadUShort();

MLAPI/Data/NetworkedCollections/NetworkedList.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using MLAPI.MonoBehaviours.Core;
44
using MLAPI.NetworkingManagerComponents.Binary;
5+
using MLAPI.NetworkingManagerComponents.Core;
56

67
namespace MLAPI.Data.NetworkedCollections
78
{
@@ -39,6 +40,11 @@ public bool IsDirty()
3940
return dirtyEvents.Count > 0;
4041
}
4142

43+
public string GetChannel()
44+
{
45+
return Settings.SendChannel;
46+
}
47+
4248
public bool CanClientWrite(uint clientId)
4349
{
4450
switch (Settings.WritePermission)

MLAPI/Data/NetworkedVar.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using MLAPI.NetworkingManagerComponents.Binary;
22
using MLAPI.MonoBehaviours.Core;
3+
using MLAPI.NetworkingManagerComponents.Core;
34

45
namespace MLAPI.Data
56
{
@@ -106,10 +107,16 @@ public void WriteFieldToWriter(BitWriter writer)
106107
{
107108
//TODO: Write field
108109
}
110+
111+
public string GetChannel()
112+
{
113+
return Settings.SendChannel;
114+
}
109115
}
110116

111117
public interface INetworkedVar
112118
{
119+
string GetChannel();
113120
void ResetDirty();
114121
bool IsDirty();
115122
bool CanClientWrite(uint clientId);

MLAPI/Data/NetworkedVarMeta.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace MLAPI.Data
1+
using MLAPI.Data.Transports;
2+
3+
namespace MLAPI.Data
24
{
35
public class NetworkedVarSettings
46
{
@@ -8,8 +10,9 @@ public class NetworkedVarSettings
810
public NetworkedVarPermissionsDelegate ReadPermissionCallback = null;
911
public bool SendOnChange = false;
1012
public float SendDelay = 0.1f;
13+
public string SendChannel = "MLAPI_INTERNAL";
1114

12-
internal NetworkedVarSettings()
15+
public NetworkedVarSettings()
1316
{
1417

1518
}
@@ -24,4 +27,4 @@ public enum NetworkedVarPermission
2427
OwnerOnly,
2528
Custom
2629
}
27-
}
30+
}

MLAPI/Data/NonAllocExtensions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace MLAPI.Data
7+
{
8+
class NonAllocExtensions
9+
{
10+
}
11+
}

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,8 @@ private bool SetDirtyness()
928928
#region NetworkedVar
929929

930930
private bool networkedVarInit = false;
931+
private readonly List<HashSet<int>> channelMappedVarIndexes = new List<HashSet<int>>();
932+
private readonly List<string> channelsForVarGroups = new List<string>();
931933
internal readonly List<INetworkedVar> networkedVarFields = new List<INetworkedVar>();
932934
internal void NetworkedVarInit()
933935
{
@@ -956,34 +958,62 @@ internal void NetworkedVarInit()
956958
networkedVarFields.Add(instance);
957959
}
958960
}
961+
962+
//Create index map for channels
963+
Dictionary<string, int> firstLevelIndex = new Dictionary<string, int>();
964+
int secondLevelCounter = 0;
965+
for (int i = 0; i < networkedVarFields.Count; i++)
966+
{
967+
string channel = networkedVarFields[i].GetChannel(); //Cache this here. Some developers are stupid. You don't know what shit they will do in their methods
968+
if (!firstLevelIndex.ContainsKey(channel))
969+
{
970+
firstLevelIndex.Add(channel, secondLevelCounter);
971+
channelsForVarGroups.Add(channel);
972+
secondLevelCounter++;
973+
}
974+
channelMappedVarIndexes[firstLevelIndex[channel]].Add(i);
975+
}
959976
}
960977

961978
internal void NetworkedVarUpdate()
962979
{
980+
if (!networkedVarInit)
981+
NetworkedVarInit();
963982
//TODO: Do this efficiently.
964983

965984
for (int i = 0; i < NetworkingManager.singleton.ConnectedClientsList.Count; i++)
966985
{
967-
using (BitWriter writer = BitWriter.Get())
986+
//This iterates over every "channel group".
987+
for (int j = 0; j < channelMappedVarIndexes.Count; j++)
968988
{
969-
writer.WriteUInt(networkId);
970-
writer.WriteUShort(networkedObject.GetOrderIndex(this));
971-
972-
uint clientId = NetworkingManager.singleton.ConnectedClientsList[i].ClientId;
973-
for (int j = 0; j < networkedVarFields.Count; j++)
989+
using (BitWriter writer = BitWriter.Get())
974990
{
975-
bool isDirty = networkedVarFields[j].IsDirty(); //cache this here. You never know what operations users will do in the dirty methods
976-
writer.WriteBool(isDirty);
977-
if (isDirty && (!isServer || networkedVarFields[j].CanClientRead(clientId)))
991+
writer.WriteUInt(networkId);
992+
writer.WriteUShort(networkedObject.GetOrderIndex(this));
993+
994+
uint clientId = NetworkingManager.singleton.ConnectedClientsList[i].ClientId;
995+
for (int k = 0; k < networkedVarFields.Count; k++)
978996
{
979-
networkedVarFields[j].WriteDeltaToWriter(writer);
997+
if (!channelMappedVarIndexes[j].Contains(k))
998+
{
999+
//This var does not belong to the currently iterating channel group.
1000+
writer.WriteBool(false);
1001+
continue;
1002+
}
1003+
1004+
bool isDirty = networkedVarFields[k].IsDirty(); //cache this here. You never know what operations users will do in the dirty methods
1005+
writer.WriteBool(isDirty);
1006+
if (isDirty && (!isServer || networkedVarFields[k].CanClientRead(clientId)))
1007+
{
1008+
networkedVarFields[k].WriteDeltaToWriter(writer);
1009+
}
9801010
}
981-
}
9821011

983-
if (isServer)
984-
InternalMessageHandler.Send(clientId, "MLAPI_NETWORKED_VAR_DELTA", "MLAPI_INTERNAL", writer, null);
985-
else
986-
InternalMessageHandler.Send(NetworkingManager.singleton.NetworkConfig.NetworkTransport.ServerNetId, "MLAPI_NETWORKED_VAR_DELTA", "MLAPI_INTERNAL", writer, null);
1012+
if (isServer)
1013+
InternalMessageHandler.Send(clientId, "MLAPI_NETWORKED_VAR_DELTA", channelsForVarGroups[j], writer, null);
1014+
else
1015+
InternalMessageHandler.Send(NetworkingManager.singleton.NetworkConfig.NetworkTransport.ServerNetId, "MLAPI_NETWORKED_VAR_DELTA", channelsForVarGroups[j], writer, null);
1016+
}
9871017
}
9881018
}
9891019

0 commit comments

Comments
 (0)