Skip to content

Commit 6465eb2

Browse files
committed
Fixed various issues
1 parent f16a4e7 commit 6465eb2

File tree

3 files changed

+339
-51
lines changed

3 files changed

+339
-51
lines changed

MLAPI/Data/NetworkingConfiguration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ public byte[] GetConfig(bool cache = true)
5959
//Returns a 256 bit / 32 byte long checksum of the config
6060
if (cache)
6161
{
62-
ConfigHash = sha256.ComputeHash(writeStream);
62+
ConfigHash = sha256.ComputeHash(writeStream.ToArray());
6363
return ConfigHash;
6464
}
65-
return sha256.ComputeHash(writeStream);
65+
return sha256.ComputeHash(writeStream.ToArray());
6666
}
6767
}
6868
}

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using UnityEngine;
5+
using System.Linq;
56

67
namespace MLAPI
78
{
@@ -14,7 +15,20 @@ protected bool isLocalPlayer
1415
return networkedObject.isLocalPlayer;
1516
}
1617
}
17-
protected bool isServer = NetworkingManager.singleton.isServer;
18+
protected bool isServer
19+
{
20+
get
21+
{
22+
return NetworkingManager.singleton.isServer;
23+
}
24+
}
25+
protected bool isClient
26+
{
27+
get
28+
{
29+
return NetworkingManager.singleton.isClient;
30+
}
31+
}
1832
protected NetworkedObject networkedObject
1933
{
2034
get
@@ -27,14 +41,22 @@ protected NetworkedObject networkedObject
2741
}
2842
}
2943
private NetworkedObject _networkedObject = null;
30-
protected uint netId
44+
public uint objectNetworkId
3145
{
3246
get
3347
{
3448
return networkedObject.NetworkId;
3549
}
3650
}
3751

52+
public int ownerConnectionId
53+
{
54+
get
55+
{
56+
return networkedObject.OwnerClientId;
57+
}
58+
}
59+
3860
//Change data type
3961
private Dictionary<string, int> registeredMessageHandlers = new Dictionary<string, int>();
4062

@@ -58,25 +80,79 @@ private void OnDestroy()
5880
}
5981
}
6082

61-
public void Send(int connectionId, string messageType, string channelName, byte[] data)
83+
public void SendToServer(string messageType, string channelName, byte[] data)
6284
{
63-
Send(connectionId, messageType, channelName, data);
85+
if(isServer)
86+
{
87+
Debug.LogWarning("MLAPI: Sending messages from server to server is not yet supported");
88+
return;
89+
}
90+
NetworkingManager.singleton.Send(NetworkingManager.singleton.localConnectionId, messageType, channelName, data);
6491
}
6592

66-
public void Send(int[] connectonIds, string messageType, string channelName, byte[] data)
93+
public void SendToLocalClient(string messageType, string channelName, byte[] data)
6794
{
68-
for (int i = 0; i < connectonIds.Length; i++)
95+
if (!isServer)
6996
{
70-
Send(connectonIds[i], messageType, channelName, data);
97+
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
98+
return;
7199
}
100+
NetworkingManager.singleton.Send(ownerConnectionId, messageType, channelName, data);
72101
}
73102

74-
public void Send(List<int> connectonIds, string messageType, string channelName, byte[] data)
103+
public void SendToNonLocalClients(string messageType, string channelName, byte[] data)
75104
{
76-
for (int i = 0; i < connectonIds.Count; i++)
105+
if (!isServer)
77106
{
78-
Send(connectonIds[i], messageType, channelName, data);
107+
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
108+
return;
79109
}
110+
NetworkingManager.singleton.Send(messageType, channelName, data, ownerConnectionId);
111+
}
112+
113+
public void SendToClient(int connectionId, string messageType, string channelName, byte[] data)
114+
{
115+
if (!isServer)
116+
{
117+
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
118+
return;
119+
}
120+
NetworkingManager.singleton.Send(connectionId, messageType, channelName, data);
121+
}
122+
123+
public void SendToClients(int[] connectionIds, string messageType, string channelName, byte[] data)
124+
{
125+
if (!isServer)
126+
{
127+
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
128+
return;
129+
}
130+
NetworkingManager.singleton.Send(connectionIds, messageType, channelName, data);
131+
}
132+
133+
public void SendToClients(List<int> connectionIds, string messageType, string channelName, byte[] data)
134+
{
135+
if (!isServer)
136+
{
137+
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
138+
return;
139+
}
140+
NetworkingManager.singleton.Send(connectionIds, messageType, channelName, data);
141+
}
142+
143+
public void SendToClients(string messageType, string channelName, byte[] data)
144+
{
145+
if (!isServer)
146+
{
147+
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
148+
return;
149+
}
150+
NetworkingManager.singleton.Send(messageType, channelName, data);
151+
}
152+
153+
public NetworkedObject GetNetworkedObject(uint networkId)
154+
{
155+
return NetworkingManager.singleton.SpawnedObjects[networkId];
80156
}
81157
}
82158
}

0 commit comments

Comments
 (0)