Skip to content

Commit 3c8b053

Browse files
authored
Merge pull request #115 from jaglitegrann/feat/custom-bitwriter-types
custom types for BitWriter and BitReader
2 parents 79038d8 + 928a30a commit 3c8b053

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

MLAPI/MLAPI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
<Compile Include="NetworkingManagerComponents\Binary\BitReaderDeprecated.cs" />
116116
<Compile Include="NetworkingManagerComponents\Binary\BinaryHelpers.cs" />
117117
<Compile Include="NetworkingManagerComponents\Binary\ByteBool.cs" />
118+
<Compile Include="NetworkingManagerComponents\Binary\IBitWritable.cs" />
118119
<Compile Include="NetworkingManagerComponents\Binary\ResourcePool.cs" />
119120
<Compile Include="NetworkingManagerComponents\Binary\UIntFloat.cs" />
120121
<Compile Include="NetworkingManagerComponents\Core\LogHelper.cs" />

MLAPI/NetworkingManagerComponents/Binary/BitReader.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,15 @@ public object ReadObjectPacked(Type type)
127127
return ReadRotation(3);
128128
if (type == typeof(char))
129129
return ReadCharPacked();
130+
if (type == typeof(IBitWritable))
131+
{
132+
object instance = Activator.CreateInstance(type);
133+
((IBitWritable)instance).Read(this.source);
134+
return instance;
135+
}
130136
if (type.IsEnum)
131137
return ReadInt32Packed();
138+
132139
throw new ArgumentException("BitReader cannot read type " + type.Name);
133140
}
134141

MLAPI/NetworkingManagerComponents/Binary/BitWriter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ public void WriteObjectPacked(object value)
145145
WriteCharPacked((char)value);
146146
return;
147147
}
148+
else if(value is IBitWritable)
149+
{
150+
((IBitWritable)value).Write(this.sink);
151+
return;
152+
}
148153
else if (value.GetType().IsEnum)
149154
{
150155
WriteInt32Packed((int)value);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace MLAPI.Serialization
8+
{
9+
public interface IBitWritable
10+
{
11+
void Write(Stream stream);
12+
void Read(Stream stream);
13+
}
14+
}

0 commit comments

Comments
 (0)