Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unity.Entities.Serialization.BinaryReader basic implementation
// src: https://gist.github.com/andrew-raphael-lukasik/6813fe2b8688eb3ea4e333f21d3cc02a
namespace Unity.Entities.Serialization
{
public unsafe struct Reader : BinaryReader
{
System.IO.BinaryReader _reader;
System.IO.FileStream _stream;
public Reader ( string filePath )
{
_stream = System.IO.File.Open( filePath , System.IO.FileMode.Open );
_reader = new System.IO.BinaryReader( _stream );
}
long BinaryReader.Position
{
get => _reader.BaseStream.Position;
set => _reader.BaseStream.Position = value;
}
void BinaryReader.ReadBytes ( void* data , int bytes )
{
byte* ptr = (byte*) data;
for( int i=0 ; i<bytes ; i++ )
*(ptr+i) = _reader.ReadByte();
}
void System.IDisposable.Dispose ()
{
_reader.Dispose();
_stream.Dispose();
}
}
public unsafe struct Writer : BinaryWriter
{
System.IO.BinaryWriter _writer;
System.IO.FileStream _stream;
public Writer ( string filePath )
{
_stream = System.IO.File.Open( filePath , System.IO.FileMode.Create );
_writer = new System.IO.BinaryWriter( _stream );
}
long BinaryWriter.Position
{
get => _writer.BaseStream.Position;
set => _writer.BaseStream.Position = value;
}
void BinaryWriter.WriteBytes ( void* data , int bytes )
{
byte* ptr = (byte*) data;
for( int i=0 ; i<bytes ; i++ )
_writer.Write( *(ptr+i) );
}
void System.IDisposable.Dispose ()
{
_writer.Dispose();
_stream.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment