Skip to content

Instantly share code, notes, and snippets.

@andrew-raphael-lukasik
Last active December 27, 2022 03:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrew-raphael-lukasik/6813fe2b8688eb3ea4e333f21d3cc02a to your computer and use it in GitHub Desktop.
Save andrew-raphael-lukasik/6813fe2b8688eb3ea4e333f21d3cc02a to your computer and use it in GitHub Desktop.
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