Skip to content

Commit 4a280c0

Browse files
committed
Added more core features & object / player spawning
1 parent c102935 commit 4a280c0

11 files changed

+762
-401
lines changed

MLAPI/Data/NetworkedClient.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using UnityEngine;
2+
3+
namespace MLAPI
4+
{
5+
public class NetworkedClient
6+
{
7+
public int ClientId;
8+
public GameObject PlayerObject;
9+
}
10+
}

MLAPI/Data/NetworkingConfiguration.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Security.Cryptography;
5+
using UnityEngine.Networking;
6+
7+
namespace MLAPI
8+
{
9+
public class NetworkingConfiguration
10+
{
11+
public ushort ProtocolVersion = 0;
12+
public Dictionary<string, QosType> Channels = new Dictionary<string, QosType>();
13+
public List<string> MessageTypes = new List<string>();
14+
public int MessageBufferSize = 65536;
15+
public int MaxMessagesPerFrame = 150;
16+
public int MaxConnections = 100;
17+
public int Port = 7777;
18+
public string Address;
19+
public int ClientConnectionBufferTimeout = 10;
20+
public bool ConnectionApproval = false;
21+
public Action<byte[], int, Action<int, bool>> ConnectionApprovalCallback;
22+
public byte[] ConnectionData;
23+
public bool HandleObjectSpawning = true;
24+
//TODO
25+
public bool CompressMessages = false;
26+
//Should only be used for dedicated servers and will require the servers RSA keypair being hard coded into clients in order to exchange a AES key
27+
//TODO
28+
public bool EncryptMessages = false;
29+
30+
31+
//Cached config hash
32+
private byte[] ConfigHash = null;
33+
public byte[] GetConfig(bool cache = true)
34+
{
35+
if (ConfigHash != null && cache)
36+
return ConfigHash;
37+
38+
using(MemoryStream writeStream = new MemoryStream())
39+
{
40+
using(BinaryWriter writer = new BinaryWriter(writeStream))
41+
{
42+
writer.Write(ProtocolVersion);
43+
foreach (KeyValuePair<string, QosType> pair in Channels)
44+
{
45+
writer.Write(pair.Key);
46+
writer.Write((int)pair.Value);
47+
}
48+
for (int i = 0; i < MessageTypes.Count; i++)
49+
{
50+
writer.Write(MessageTypes[i]);
51+
}
52+
writer.Write(HandleObjectSpawning);
53+
writer.Write(CompressMessages);
54+
writer.Write(EncryptMessages);
55+
}
56+
using(SHA256Managed sha256 = new SHA256Managed())
57+
{
58+
//Returns a 256 bit / 32 byte long checksum of the config
59+
if (cache)
60+
{
61+
ConfigHash = sha256.ComputeHash(writeStream);
62+
return ConfigHash;
63+
}
64+
return sha256.ComputeHash(writeStream);
65+
}
66+
}
67+
}
68+
69+
public bool CompareConfig(byte[] hash)
70+
{
71+
return hash == GetConfig();
72+
}
73+
}
74+
}

MLAPI/MLAPI.csproj

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,18 @@
3535
<Reference Include="System.Core" />
3636
<Reference Include="System.Xml.Linq" />
3737
<Reference Include="System.Data.DataSetExtensions" />
38-
<Reference Include="Microsoft.CSharp" />
3938
<Reference Include="System.Data" />
40-
<Reference Include="System.Net.Http" />
4139
<Reference Include="System.Xml" />
4240
<Reference Include="UnityEngine">
4341
<HintPath>..\..\..\..\..\..\Program Files\Unity\Editor\Data\Managed\UnityEngine.dll</HintPath>
4442
</Reference>
4543
</ItemGroup>
4644
<ItemGroup>
47-
<Compile Include="NetworkedBehaviour.cs" />
48-
<Compile Include="NetworkedClient.cs" />
49-
<Compile Include="NetworkedObject.cs" />
50-
<Compile Include="NetworkingConfiguration.cs" />
51-
<Compile Include="NetworkingManager.cs" />
45+
<Compile Include="MonoBehaviours\Core\NetworkedBehaviour.cs" />
46+
<Compile Include="Data\NetworkedClient.cs" />
47+
<Compile Include="MonoBehaviours\Core\NetworkedObject.cs" />
48+
<Compile Include="Data\NetworkingConfiguration.cs" />
49+
<Compile Include="MonoBehaviours\Core\NetworkingManager.cs" />
5250
<Compile Include="Properties\AssemblyInfo.cs" />
5351
</ItemGroup>
5452
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using UnityEngine;
5+
6+
namespace MLAPI
7+
{
8+
public abstract class NetworkedBehaviour : MonoBehaviour
9+
{
10+
protected bool isLocalPlayer
11+
{
12+
get
13+
{
14+
return networkedObject.isLocalPlayer;
15+
}
16+
}
17+
protected bool isServer = NetworkingManager.singleton.isServer;
18+
protected NetworkedObject networkedObject
19+
{
20+
get
21+
{
22+
if(_networkedObject == null)
23+
{
24+
_networkedObject = GetComponentInParent<NetworkedObject>();
25+
}
26+
return _networkedObject;
27+
}
28+
}
29+
private NetworkedObject _networkedObject = null;
30+
protected uint netId
31+
{
32+
get
33+
{
34+
return networkedObject.NetworkId;
35+
}
36+
}
37+
38+
//Change data type
39+
private Dictionary<string, int> registeredMessageHandlers = new Dictionary<string, int>();
40+
41+
public int RegisterMessageHandler(string name, Action<int, byte[]> action)
42+
{
43+
int counter = NetworkingManager.singleton.AddIncomingMessageHandler(name, action);
44+
registeredMessageHandlers.Add(name, counter);
45+
return counter;
46+
}
47+
48+
public void DeregisterMessageHandler(string name, int counter)
49+
{
50+
NetworkingManager.singleton.RemoveIncomingMessageHandler(name, counter);
51+
}
52+
53+
private void OnDestroy()
54+
{
55+
foreach (KeyValuePair<string, int> pair in registeredMessageHandlers)
56+
{
57+
DeregisterMessageHandler(pair.Key, pair.Value);
58+
}
59+
}
60+
61+
public void Send(int connectionId, string messageType, string channelName, byte[] data)
62+
{
63+
Send(connectionId, messageType, channelName, data);
64+
}
65+
66+
public void Send(int[] connectonIds, string messageType, string channelName, byte[] data)
67+
{
68+
for (int i = 0; i < connectonIds.Length; i++)
69+
{
70+
Send(connectonIds[i], messageType, channelName, data);
71+
}
72+
}
73+
74+
public void Send(List<int> connectonIds, string messageType, string channelName, byte[] data)
75+
{
76+
for (int i = 0; i < connectonIds.Count; i++)
77+
{
78+
Send(connectonIds[i], messageType, channelName, data);
79+
}
80+
}
81+
}
82+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using UnityEngine;
2+
3+
namespace MLAPI
4+
{
5+
//TODO
6+
//Will be used for objects which will be spawned automatically across clients
7+
public class NetworkedObject : MonoBehaviour
8+
{
9+
public uint NetworkId;
10+
public int OwnerClientId = -1;
11+
public int SpawnablePrefabId;
12+
internal bool IsPlayerObject = false;
13+
public bool isLocalPlayer
14+
{
15+
get
16+
{
17+
return OwnerClientId == NetworkingManager.singleton.MyClientId;
18+
}
19+
}
20+
21+
private void OnDestroy()
22+
{
23+
NetworkingManager.OnDestroyObject(NetworkId);
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)