Switched NetworkConfig GetConfig to BitWriter

This commit is contained in:
Albin Corén 2018-04-17 21:41:24 +02:00
parent d2d1b392f5
commit 554010dbbc

@ -1,4 +1,5 @@
using MLAPI.MonoBehaviours.Core; using MLAPI.MonoBehaviours.Core;
using MLAPI.NetworkingManagerComponents.Binary;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -155,53 +156,51 @@ namespace MLAPI.Data
if (ConfigHash != null && cache) if (ConfigHash != null && cache)
return ConfigHash; return ConfigHash;
using (MemoryStream writeStream = new MemoryStream()) using (BitWriter writer = new BitWriter())
{ {
using (BinaryWriter writer = new BinaryWriter(writeStream)) writer.WriteUShort(ProtocolVersion);
{
writer.Write(ProtocolVersion);
for (int i = 0; i < Channels.Count; i++) for (int i = 0; i < Channels.Count; i++)
{ {
writer.Write(Channels[i].Name); writer.WriteString(Channels[i].Name);
writer.Write((byte)Channels[i].Type); writer.WriteByte((byte)Channels[i].Type);
if (EnableEncryption) if (EnableEncryption)
writer.Write(Channels[i].Encrypted); writer.WriteBool(Channels[i].Encrypted);
} }
for (int i = 0; i < MessageTypes.Count; i++) for (int i = 0; i < MessageTypes.Count; i++)
{ {
writer.Write(MessageTypes[i].Name); writer.WriteString(MessageTypes[i].Name);
if (AllowPassthroughMessages) if (AllowPassthroughMessages)
writer.Write(MessageTypes[i].Passthrough); writer.WriteBool(MessageTypes[i].Passthrough);
} }
if (EnableSceneSwitching) if (EnableSceneSwitching)
{ {
for (int i = 0; i < RegisteredScenes.Count; i++) for (int i = 0; i < RegisteredScenes.Count; i++)
{ {
writer.Write(RegisteredScenes[i]); writer.WriteString(RegisteredScenes[i]);
} }
} }
if (HandleObjectSpawning) if (HandleObjectSpawning)
{ {
for (int i = 0; i < NetworkedPrefabs.Count; i++) for (int i = 0; i < NetworkedPrefabs.Count; i++)
{ {
writer.Write(NetworkedPrefabs[i].name); writer.WriteString(NetworkedPrefabs[i].name);
} }
} }
writer.Write(HandleObjectSpawning); writer.WriteBool(HandleObjectSpawning);
writer.Write(EnableEncryption); writer.WriteBool(EnableEncryption);
writer.Write(AllowPassthroughMessages); writer.WriteBool(AllowPassthroughMessages);
writer.Write(EnableSceneSwitching); writer.WriteBool(EnableSceneSwitching);
writer.Write(SignKeyExchange); writer.WriteBool(SignKeyExchange);
}
using (SHA256Managed sha256 = new SHA256Managed()) using (SHA256Managed sha256 = new SHA256Managed())
{ {
//Returns a 256 bit / 32 byte long checksum of the config //Returns a 256 bit / 32 byte long checksum of the config
if (cache) if (cache)
{ {
ConfigHash = sha256.ComputeHash(writeStream.ToArray()); ConfigHash = sha256.ComputeHash(writer.Finalize());
return ConfigHash; return ConfigHash;
} }
return sha256.ComputeHash(writeStream.ToArray()); return sha256.ComputeHash(writer.Finalize());
} }
} }
} }