/*************************************************************************
* ModernUO *
* Copyright 2019-2026 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: BinaryFileReader.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see . *
*************************************************************************/
using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Runtime.CompilerServices;
using System.Text;
namespace Server;
///
/// Read bits of data from a serialized file in a managed environment.
///
/// Uses the following components collectively:
///
///
///
///
///
public sealed unsafe class BinaryFileReader : IDisposable, IGenericReader
{
private readonly bool _usePrefixes;
private readonly MemoryMappedFile _mmf;
private readonly MemoryMappedViewStream _accessor;
private readonly UnmanagedDataReader _reader;
///
/// Read bits of data from a serialized file in a managed environment.
///
Encoding is UTF8 if left null.
///
/// Uses the following components collectively:
///
///
///
///
///
/// Full file path of the file to be deserialized.
/// Sets if strings should be read with itern.
/// Set an encoding. By default UTF8
public BinaryFileReader(string path, bool usePrefixes = true, Encoding encoding = null)
{
_usePrefixes = usePrefixes;
var fi = new FileInfo(path);
if (fi.Length > 0)
{
_mmf = MemoryMappedFile.CreateFromFile(path, FileMode.Open);
_accessor = _mmf.CreateViewStream();
byte* ptr = null;
_accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr);
_reader = new UnmanagedDataReader(ptr, _accessor.Length, encoding: encoding);
}
else
{
_reader = new UnmanagedDataReader(null, 0, encoding: encoding);
}
}
///
/// How many bits deep into the file is the reader at currently.
///
public long Position => _reader.Position;
public void Dispose()
{
_accessor?.SafeMemoryMappedViewHandle.ReleasePointer();
_accessor?.Dispose();
_mmf?.Dispose();
}
///
/// If usePrefixes is true, return a ReadString(intern) else ReadStringRaw(intern).
///
/// A string value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadString(bool intern = false) => _usePrefixes ? _reader.ReadString(intern) : _reader.ReadStringRaw(intern);
///
/// Returns the next set of bits that make up a string.
///
/// Next string value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadStringRaw(bool intern = false) => _reader.ReadStringRaw(intern);
///
/// Read the next 64 bits to make up a long (int64).
///
/// Next long value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long ReadLong() => _reader.ReadLong();
///
/// Read the next 64 bits to make up an unsigned long (uint64).
///
/// Next ulong value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong ReadULong() => _reader.ReadULong();
///
/// Read the next 32 bits to make up an int (int32).
///
/// Next int value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int ReadInt() => _reader.ReadInt();
///
/// Read the next 32 bits to make up an unsigned int (uint32).
///
/// Next uint value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint ReadUInt() => _reader.ReadUInt();
///
/// Read the next 16 bits to make up a short (int16).
///
/// Next short value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public short ReadShort() => _reader.ReadShort();
///
/// Read the next 16 bits to make up an unsigned short (int16).
///
/// Next ushort value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ushort ReadUShort() => _reader.ReadUShort();
///
/// Read the next 8 bits to make up a float point double.
///
/// Next double value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double ReadDouble() => _reader.ReadDouble();
///
/// Read the next 4 bits to make up a float point.
///
/// Next float value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float ReadFloat() => _reader.ReadFloat();
///
/// Read the next 8 bits to make up a byte.
///
/// Next byte value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte ReadByte() => _reader.ReadByte();
///
/// Read the next 8 bits to make up a signed byte.
///
/// Next sbyte value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public sbyte ReadSByte() => _reader.ReadSByte();
///
/// Read the next 1 bit to make up a boolean.
///
/// Next bool value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool ReadBool() => _reader.ReadBool();
///
/// Read the next 32 bytes to make up a .
///
/// Next uint value cast as a struct.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Serial ReadSerial() => _reader.ReadSerial();
///
/// Reads the next Byte which helps determine how to read the following Type.
///
If the byte returns 1 => and translate into a Type via the
///
If the byte returns 2 =>
///
else return null
///
/// Next Type value
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Type ReadType() => _reader.ReadType();
///
/// Reads the next set of bytes to fill the buffer.
///
/// A reference span that will be filled with the next set of bytes.
/// The length of the buffer.
/// Thrown if the buffer is larger than the remaining data to read in the file.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Read(Span buffer) => _reader.Read(buffer);
///
/// Sets the current position of the stream to a specified value.
///
/// The new position, relative to the parameter.
/// The reference point for the parameter. It can be one of the values of .
/// The new position in the stream, in bytes.
/// Thrown when the or the resulting position is out of the valid range.
/// Thrown if the stream does not support seeking.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long Seek(long offset, SeekOrigin origin) => _reader.Seek(offset, origin);
}