diff --git a/MLAPI/MLAPI.csproj b/MLAPI/MLAPI.csproj
index 9c7949f..9b8a4bc 100644
--- a/MLAPI/MLAPI.csproj
+++ b/MLAPI/MLAPI.csproj
@@ -78,8 +78,8 @@
-
-
+
+
diff --git a/MLAPI/NetworkingManagerComponents/Binary/BinaryDistributor.cs b/MLAPI/NetworkingManagerComponents/Binary/BitReader.cs
similarity index 94%
rename from MLAPI/NetworkingManagerComponents/Binary/BinaryDistributor.cs
rename to MLAPI/NetworkingManagerComponents/Binary/BitReader.cs
index fa4857f..2541fc4 100644
--- a/MLAPI/NetworkingManagerComponents/Binary/BinaryDistributor.cs
+++ b/MLAPI/NetworkingManagerComponents/Binary/BitReader.cs
@@ -1,13 +1,10 @@
-using MLAPI.NetworkingManagerComponents.Binary;
-using System;
-using System.Collections.Generic;
-using System.Linq;
+using System;
using System.Runtime.InteropServices;
using System.Text;
-namespace Tofvesson.Common
+namespace MLAPI.NetworkingManagerComponents.Binary
{
- public class BinaryDistributor
+ public class BitReader
{
private delegate T Getter();
private static readonly float[] holder_f = new float[1];
@@ -17,7 +14,7 @@ namespace Tofvesson.Common
private readonly byte[] readFrom;
private long bitCount = 0;
- public BinaryDistributor(byte[] readFrom) => this.readFrom = readFrom;
+ public BitReader(byte[] readFrom) => this.readFrom = readFrom;
public bool ReadBool()
{
diff --git a/MLAPI/NetworkingManagerComponents/Binary/BinaryCollector.cs b/MLAPI/NetworkingManagerComponents/Binary/BitWriter.cs
similarity index 94%
rename from MLAPI/NetworkingManagerComponents/Binary/BinaryCollector.cs
rename to MLAPI/NetworkingManagerComponents/Binary/BitWriter.cs
index ae47506..400de4a 100644
--- a/MLAPI/NetworkingManagerComponents/Binary/BinaryCollector.cs
+++ b/MLAPI/NetworkingManagerComponents/Binary/BitWriter.cs
@@ -1,15 +1,12 @@
-using MLAPI.NetworkingManagerComponents.Binary;
-using System;
+using System;
using System.Collections.Generic;
-using System.IO;
-using System.Linq;
using System.Reflection;
-using System.Runtime.InteropServices;
using System.Text;
+using UnityEngine;
-namespace Tofvesson.Common
+namespace MLAPI.NetworkingManagerComponents.Binary
{
- public sealed class BinaryCollector : IDisposable
+ public sealed class BitWiter : IDisposable
{
// Collects reusable
private static readonly List expired = new List();
@@ -41,7 +38,7 @@ namespace Tofvesson.Common
dec_hi,
dec_flags;
- static BinaryCollector()
+ static BitWiter()
{
dec_lo = typeof(decimal).GetField("lo", BindingFlags.NonPublic);
dec_mid = typeof(decimal).GetField("mid", BindingFlags.NonPublic);
@@ -56,7 +53,7 @@ namespace Tofvesson.Common
///
/// Allocates a new binary collector.
///
- public BinaryCollector(int bufferSize)
+ public BitWiter(int bufferSize)
{
this.bufferSize = bufferSize;
for (int i = expired.Count - 1; i >= 0; --i)
@@ -106,17 +103,33 @@ namespace Tofvesson.Common
public void WriteLongArray(long[] l) => Push(l);
public void WriteString(string s) => Push(s);
- public byte[] ToArray()
+ public long Finalize(ref byte[] buffer)
{
+ if(buffer == null)
+ {
+ Debug.LogWarning("MLAPI: no buffer provided");
+ return 0;
+ }
long bitCount = 0;
for (int i = 0; i < collectCount; ++i) bitCount += GetBitCount(collect[i]);
- byte[] alloc = new byte[(bitCount / 8) + (bitCount % 8 == 0 ? 0 : 1)];
+ if(buffer.Length < ((bitCount / 8) + (bitCount % 8 == 0 ? 0 : 1)))
+ {
+ Debug.LogWarning("MLAPI: The buffer size is not large enough");
+ return 0;
+ }
long bitOffset = 0;
foreach (var item in collect)
- Serialize(item, alloc, ref bitOffset);
+ Serialize(item, buffer, ref bitOffset);
- return alloc;
+ return (bitCount / 8) + (bitCount % 8 == 0 ? 0 : 1));
+ }
+
+ public long GetFinalizeSize()
+ {
+ long bitCount = 0;
+ for (int i = 0; i < collectCount; ++i) bitCount += GetBitCount(collect[i]);
+ return ((bitCount / 8) + (bitCount % 8 == 0 ? 0 : 1));
}
private static void Serialize(T t, byte[] writeTo, ref long bitOffset)