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

View File

@ -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);
for (int i = 0; i < Channels.Count; i++)
{ {
writer.Write(ProtocolVersion); writer.WriteString(Channels[i].Name);
for (int i = 0; i < Channels.Count; i++) writer.WriteByte((byte)Channels[i].Type);
{ if (EnableEncryption)
writer.Write(Channels[i].Name); writer.WriteBool(Channels[i].Encrypted);
writer.Write((byte)Channels[i].Type);
if (EnableEncryption)
writer.Write(Channels[i].Encrypted);
}
for (int i = 0; i < MessageTypes.Count; i++)
{
writer.Write(MessageTypes[i].Name);
if (AllowPassthroughMessages)
writer.Write(MessageTypes[i].Passthrough);
}
if (EnableSceneSwitching)
{
for (int i = 0; i < RegisteredScenes.Count; i++)
{
writer.Write(RegisteredScenes[i]);
}
}
if(HandleObjectSpawning)
{
for (int i = 0; i < NetworkedPrefabs.Count; i++)
{
writer.Write(NetworkedPrefabs[i].name);
}
}
writer.Write(HandleObjectSpawning);
writer.Write(EnableEncryption);
writer.Write(AllowPassthroughMessages);
writer.Write(EnableSceneSwitching);
writer.Write(SignKeyExchange);
} }
for (int i = 0; i < MessageTypes.Count; i++)
{
writer.WriteString(MessageTypes[i].Name);
if (AllowPassthroughMessages)
writer.WriteBool(MessageTypes[i].Passthrough);
}
if (EnableSceneSwitching)
{
for (int i = 0; i < RegisteredScenes.Count; i++)
{
writer.WriteString(RegisteredScenes[i]);
}
}
if (HandleObjectSpawning)
{
for (int i = 0; i < NetworkedPrefabs.Count; i++)
{
writer.WriteString(NetworkedPrefabs[i].name);
}
}
writer.WriteBool(HandleObjectSpawning);
writer.WriteBool(EnableEncryption);
writer.WriteBool(AllowPassthroughMessages);
writer.WriteBool(EnableSceneSwitching);
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());
} }
} }
} }