Skip to content

Commit 554010d

Browse files
committed
Switched NetworkConfig GetConfig to BitWriter
1 parent d2d1b39 commit 554010d

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

MLAPI/Data/NetworkConfig.cs

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using MLAPI.MonoBehaviours.Core;
2+
using MLAPI.NetworkingManagerComponents.Binary;
23
using System;
34
using System.Collections.Generic;
45
using System.IO;
@@ -155,53 +156,51 @@ public byte[] GetConfig(bool cache = true)
155156
if (ConfigHash != null && cache)
156157
return ConfigHash;
157158

158-
using (MemoryStream writeStream = new MemoryStream())
159+
using (BitWriter writer = new BitWriter())
159160
{
160-
using (BinaryWriter writer = new BinaryWriter(writeStream))
161+
writer.WriteUShort(ProtocolVersion);
162+
for (int i = 0; i < Channels.Count; i++)
161163
{
162-
writer.Write(ProtocolVersion);
163-
for (int i = 0; i < Channels.Count; i++)
164-
{
165-
writer.Write(Channels[i].Name);
166-
writer.Write((byte)Channels[i].Type);
167-
if (EnableEncryption)
168-
writer.Write(Channels[i].Encrypted);
169-
}
170-
for (int i = 0; i < MessageTypes.Count; i++)
171-
{
172-
writer.Write(MessageTypes[i].Name);
173-
if (AllowPassthroughMessages)
174-
writer.Write(MessageTypes[i].Passthrough);
175-
}
176-
if (EnableSceneSwitching)
164+
writer.WriteString(Channels[i].Name);
165+
writer.WriteByte((byte)Channels[i].Type);
166+
if (EnableEncryption)
167+
writer.WriteBool(Channels[i].Encrypted);
168+
}
169+
for (int i = 0; i < MessageTypes.Count; i++)
170+
{
171+
writer.WriteString(MessageTypes[i].Name);
172+
if (AllowPassthroughMessages)
173+
writer.WriteBool(MessageTypes[i].Passthrough);
174+
}
175+
if (EnableSceneSwitching)
176+
{
177+
for (int i = 0; i < RegisteredScenes.Count; i++)
177178
{
178-
for (int i = 0; i < RegisteredScenes.Count; i++)
179-
{
180-
writer.Write(RegisteredScenes[i]);
181-
}
179+
writer.WriteString(RegisteredScenes[i]);
182180
}
183-
if(HandleObjectSpawning)
181+
}
182+
if (HandleObjectSpawning)
183+
{
184+
for (int i = 0; i < NetworkedPrefabs.Count; i++)
184185
{
185-
for (int i = 0; i < NetworkedPrefabs.Count; i++)
186-
{
187-
writer.Write(NetworkedPrefabs[i].name);
188-
}
186+
writer.WriteString(NetworkedPrefabs[i].name);
189187
}
190-
writer.Write(HandleObjectSpawning);
191-
writer.Write(EnableEncryption);
192-
writer.Write(AllowPassthroughMessages);
193-
writer.Write(EnableSceneSwitching);
194-
writer.Write(SignKeyExchange);
195188
}
189+
writer.WriteBool(HandleObjectSpawning);
190+
writer.WriteBool(EnableEncryption);
191+
writer.WriteBool(AllowPassthroughMessages);
192+
writer.WriteBool(EnableSceneSwitching);
193+
writer.WriteBool(SignKeyExchange);
194+
196195
using (SHA256Managed sha256 = new SHA256Managed())
197196
{
198197
//Returns a 256 bit / 32 byte long checksum of the config
199198
if (cache)
200199
{
201-
ConfigHash = sha256.ComputeHash(writeStream.ToArray());
200+
ConfigHash = sha256.ComputeHash(writer.Finalize());
202201
return ConfigHash;
203202
}
204-
return sha256.ComputeHash(writeStream.ToArray());
203+
return sha256.ComputeHash(writer.Finalize());
205204
}
206205
}
207206
}

0 commit comments

Comments
 (0)