Skip to content

Commit 5beac0f

Browse files
committed
Reworked NetworkConfig to be serializable
1 parent ace1cee commit 5beac0f

File tree

4 files changed

+155
-66
lines changed

4 files changed

+155
-66
lines changed

MLAPI/Data/Channel.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using UnityEngine.Networking;
6+
7+
namespace MLAPI.Data
8+
{
9+
[Serializable]
10+
public class Channel
11+
{
12+
public string Name;
13+
public QosType Type;
14+
}
15+
}

MLAPI/Data/NetworkingConfiguration.cs renamed to MLAPI/Data/NetworkConfig.cs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Security.Cryptography;
6+
using System.Text;
7+
using UnityEngine;
58
using UnityEngine.Networking;
69

710
namespace MLAPI.Data
811
{
912
/// <summary>
1013
/// The configuration object used to start server, client and hosts
1114
/// </summary>
12-
public class NetworkingConfiguration
15+
[Serializable]
16+
public class NetworkConfig
1317
{
1418
/// <summary>
1519
/// The protocol version. Different versions doesn't talk to each other.
@@ -18,7 +22,7 @@ public class NetworkingConfiguration
1822
/// <summary>
1923
/// Channels used by the NetworkedTransport
2024
/// </summary>
21-
public SortedDictionary<string, QosType> Channels = new SortedDictionary<string, QosType>();
25+
public List<Channel> Channels = new List<Channel>();
2226
/// <summary>
2327
/// Registered MessageTypes
2428
/// </summary>
@@ -27,14 +31,12 @@ public class NetworkingConfiguration
2731
/// List of MessageTypes that can be passed through by Server. MessageTypes in this list should thus not be trusted to as great of an extent as normal messages.
2832
/// </summary>
2933
public List<string> PassthroughMessageTypes = new List<string>();
30-
/// <summary>
31-
/// Internal collection of Passthrough MessageTypes
32-
/// </summary>
33-
internal HashSet<ushort> RegisteredPassthroughMessageTypes = new HashSet<ushort>();
34+
internal HashSet<ushort> PassthroughMessageHashSet = new HashSet<ushort>();
3435
/// <summary>
3536
/// Set of channels that will have all message contents encrypted when used
3637
/// </summary>
37-
public HashSet<int> EncryptedChannels = new HashSet<int>();
38+
public List<string> EncryptedChannels = new List<string>();
39+
internal HashSet<string> EncryptedChannelsHashSet = new HashSet<string>();
3840
/// <summary>
3941
/// A list of SceneNames that can be used during networked games.
4042
/// </summary>
@@ -80,10 +82,6 @@ public class NetworkingConfiguration
8082
/// </summary>
8183
public bool ConnectionApproval = false;
8284
/// <summary>
83-
/// The callback to invoke when a connection has to be decided if it should get approved
84-
/// </summary>
85-
public Action<byte[], int, Action<int, bool>> ConnectionApprovalCallback = null;
86-
/// <summary>
8785
/// The data to send during connection which can be used to decide on if a client should get accepted
8886
/// </summary>
8987
public byte[] ConnectionData = new byte[0];
@@ -106,10 +104,12 @@ public class NetworkingConfiguration
106104
/// <summary>
107105
/// Private RSA XML key to use for signing key exchange
108106
/// </summary>
107+
[TextArea]
109108
public string RSAPrivateKey = "<RSAKeyValue><Modulus>vBEvOQki/EftWOgwh4G8/nFRvcDJLylc8P7Dhz5m/hpkkNtAMzizNKYUrGbs7sYWlEuMYBOWrzkIDGOMoOsYc9uCi+8EcmNoHDlIhK5yNfZUexYBF551VbvZ625LSBR7kmBxkyo4IPuA09fYCHeUFm3prt4h6aTD0Hjc7ZsJHUU=</Modulus><Exponent>EQ==</Exponent><P>ydgcrq5qLJOdDQibD3m9+o3/dkKoFeCC110dnMgdpEteCruyBdL0zjGKKvjjgy3XTSSp43EN591NiXaBp0JtDw==</P><Q>7obHrUnUCsSHUsIJ7+JOrupcGrQ0XaYcQ+Uwb2v7d2YUzwZ46U4gI9snfD2J0tc3DGEh3v3G0Q8q7bxEe3H4aw==</Q><DP>L34k3c6vkgSdbHp+1nb/hj+HZx6+I0PijQbZyolwYuSOmR0a1DGjA1bzVWe9D86NAxevgM9OkOjG8yrxVIgZqQ==</DP><DQ>OB+2gyBuIKa2bdNNodrlVlVC2RtXnZB/HwjAGjeGdnJfP8VJoE6eJo3rLEq3BG7fxq1xYaUfuLhGVg4uOyngGQ==</DQ><InverseQ>o97PimYu58qH5eFmySRCIsyhBr/tK2GM17Zd9QQPJZRSorrhIJn1m6gwQ/G5aJLIM/3Yl04CoyqmQGsPXMzW2w==</InverseQ><D>CxAR1i22w4vCquB7U0Pd8Nl9R2Wxez6rHTwpnoszPB+rkAzlqKj7e5FMgpykhoQfciKPyWqQZKkAeTMIRbN56JinvpAt5POId/28HDd5xjGymHE81k3RzoHqzQXFIOF1TSYKUWzjPPF/TU4nn7auD4i6lOODATsMqtLr5DRBN/0=</D></RSAKeyValue>"; //CHANGE THESE FOR PRODUCTION!
110109
/// <summary>
111110
/// Public RSA XML key to use for signing key exchange
112111
/// </summary>
112+
[TextArea]
113113
public string RSAPublicKey = "<RSAKeyValue><Modulus>vBEvOQki/EftWOgwh4G8/nFRvcDJLylc8P7Dhz5m/hpkkNtAMzizNKYUrGbs7sYWlEuMYBOWrzkIDGOMoOsYc9uCi+8EcmNoHDlIhK5yNfZUexYBF551VbvZ625LSBR7kmBxkyo4IPuA09fYCHeUFm3prt4h6aTD0Hjc7ZsJHUU=</Modulus><Exponent>EQ==</Exponent></RSAKeyValue>"; //CHANGE THESE FOR PRODUCTION!
114114
/// <summary>
115115
/// Wheter or not to allow any type of passthrough messages
@@ -119,10 +119,12 @@ public class NetworkingConfiguration
119119
/// Wheter or not to enable scene switching
120120
/// </summary>
121121
public bool EnableSceneSwitching = false;
122+
/// <summary>
123+
/// The RSA Keysize to use
124+
/// </summary>
125+
public int RSAKeySize = 2048;
122126

123-
//Cached config hash
124127
private byte[] ConfigHash = null;
125-
126128
/// <summary>
127129
/// Gets a SHA256 hash of parts of the NetworkingConfiguration instance
128130
/// </summary>
@@ -133,41 +135,48 @@ public byte[] GetConfig(bool cache = true)
133135
if (ConfigHash != null && cache)
134136
return ConfigHash;
135137

136-
using(MemoryStream writeStream = new MemoryStream())
138+
using (MemoryStream writeStream = new MemoryStream())
137139
{
138-
using(BinaryWriter writer = new BinaryWriter(writeStream))
140+
using (BinaryWriter writer = new BinaryWriter(writeStream))
139141
{
140142
writer.Write(ProtocolVersion);
141-
foreach (KeyValuePair<string, QosType> pair in Channels)
143+
for (int i = 0; i < Channels.Count; i++)
142144
{
143-
writer.Write(pair.Key);
144-
writer.Write((int)pair.Value);
145+
writer.Write(Channels[i].Name);
146+
writer.Write((byte)Channels[i].Type);
145147
}
146148
for (int i = 0; i < MessageTypes.Count; i++)
147149
{
148150
writer.Write(MessageTypes[i]);
149151
}
150-
if(AllowPassthroughMessages)
152+
if (AllowPassthroughMessages)
151153
{
152154
for (int i = 0; i < PassthroughMessageTypes.Count; i++)
153155
{
154156
writer.Write(PassthroughMessageTypes[i]);
155157
}
156158
}
157-
if(EnableSceneSwitching)
159+
if (EnableSceneSwitching)
158160
{
159161
for (int i = 0; i < RegisteredScenes.Count; i++)
160162
{
161163
writer.Write(RegisteredScenes[i]);
162164
}
163165
}
166+
if(EnableEncryption)
167+
{
168+
for (int i = 0; i < EncryptedChannels.Count; i++)
169+
{
170+
writer.Write(EncryptedChannels[i]);
171+
}
172+
}
164173
writer.Write(HandleObjectSpawning);
165174
writer.Write(EnableEncryption);
166175
writer.Write(AllowPassthroughMessages);
167176
writer.Write(EnableSceneSwitching);
168177
writer.Write(SignKeyExchange);
169178
}
170-
using(SHA256Managed sha256 = new SHA256Managed())
179+
using (SHA256Managed sha256 = new SHA256Managed())
171180
{
172181
//Returns a 256 bit / 32 byte long checksum of the config
173182
if (cache)

MLAPI/MLAPI.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@
6767
</ItemGroup>
6868
<ItemGroup>
6969
<Compile Include="Attributes\BinaryIgnore.cs" />
70+
<Compile Include="Data\Channel.cs" />
7071
<Compile Include="Data\FieldType.cs" />
7172
<Compile Include="Attributes\SyncedVar.cs" />
73+
<Compile Include="Data\NetworkConfig.cs" />
7274
<Compile Include="Data\NetworkPool.cs" />
7375
<Compile Include="Data\TrackedPointData.cs" />
7476
<Compile Include="MonoBehaviours\Prototyping\NetworkedAnimator.cs" />
@@ -81,7 +83,6 @@
8183
<Compile Include="MonoBehaviours\Core\NetworkedBehaviour.cs" />
8284
<Compile Include="Data\NetworkedClient.cs" />
8385
<Compile Include="MonoBehaviours\Core\NetworkedObject.cs" />
84-
<Compile Include="Data\NetworkingConfiguration.cs" />
8586
<Compile Include="MonoBehaviours\Core\NetworkingManager.cs" />
8687
<Compile Include="MonoBehaviours\Core\TrackedObject.cs" />
8788
<Compile Include="MonoBehaviours\Prototyping\NetworkedTransform.cs" />

0 commit comments

Comments
 (0)