2021-05-09 00:10:39 -07:00
|
|
|
/*************************************************************************
|
|
|
|
|
* ModernUO *
|
|
|
|
|
* Copyright 2019-2021 - 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 <http://www.gnu.org/licenses/>. *
|
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2021-08-25 00:27:19 -07:00
|
|
|
using System.Runtime.CompilerServices;
|
2021-12-13 09:31:02 -08:00
|
|
|
using Server.Collections;
|
2021-05-09 00:10:39 -07:00
|
|
|
|
|
|
|
|
namespace Server
|
|
|
|
|
{
|
|
|
|
|
public class BinaryFileReader : IGenericReader, IDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly BinaryReader _reader;
|
|
|
|
|
|
|
|
|
|
public BinaryFileReader(BinaryReader br) => _reader = br;
|
|
|
|
|
|
|
|
|
|
public BinaryFileReader(Stream stream) => _reader = new BinaryReader(stream);
|
|
|
|
|
|
2021-08-25 00:27:19 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2021-05-09 00:10:39 -07:00
|
|
|
public void Close() => _reader.Close();
|
|
|
|
|
|
2021-09-26 01:09:45 -07:00
|
|
|
public DateTime LastSerialized { get; init; }
|
|
|
|
|
|
2021-08-25 00:27:19 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2021-05-09 00:10:39 -07:00
|
|
|
public string ReadString(bool intern = false)
|
|
|
|
|
{
|
|
|
|
|
var str = _reader.ReadString();
|
|
|
|
|
return intern ? Utility.Intern(str) : str;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 00:27:19 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2021-05-09 00:10:39 -07:00
|
|
|
public long ReadLong() => _reader.ReadInt64();
|
|
|
|
|
|
2021-08-25 00:27:19 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2021-05-09 00:10:39 -07:00
|
|
|
public ulong ReadULong() => _reader.ReadUInt64();
|
|
|
|
|
|
2021-08-25 00:27:19 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2021-05-09 00:10:39 -07:00
|
|
|
public int ReadInt() => _reader.ReadInt32();
|
|
|
|
|
|
2021-08-25 00:27:19 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2021-05-09 00:10:39 -07:00
|
|
|
public uint ReadUInt() => _reader.ReadUInt32();
|
|
|
|
|
|
2021-08-25 00:27:19 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2021-05-09 00:10:39 -07:00
|
|
|
public short ReadShort() => _reader.ReadInt16();
|
|
|
|
|
|
2021-08-25 00:27:19 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2021-05-09 00:10:39 -07:00
|
|
|
public ushort ReadUShort() => _reader.ReadUInt16();
|
|
|
|
|
|
2021-08-25 00:27:19 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2021-05-09 00:10:39 -07:00
|
|
|
public double ReadDouble() => _reader.ReadDouble();
|
|
|
|
|
|
2021-08-25 00:27:19 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2021-05-09 00:10:39 -07:00
|
|
|
public float ReadFloat() => _reader.ReadSingle();
|
|
|
|
|
|
2021-08-25 00:27:19 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2021-05-09 00:10:39 -07:00
|
|
|
public byte ReadByte() => _reader.ReadByte();
|
|
|
|
|
|
2021-08-25 00:27:19 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2021-05-09 00:10:39 -07:00
|
|
|
public sbyte ReadSByte() => _reader.ReadSByte();
|
|
|
|
|
|
2021-08-25 00:27:19 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2021-05-09 00:10:39 -07:00
|
|
|
public bool ReadBool() => _reader.ReadBoolean();
|
|
|
|
|
|
2021-08-25 00:27:19 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public Serial ReadSerial() => (Serial)_reader.ReadUInt32();
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2021-05-09 00:10:39 -07:00
|
|
|
public int Read(Span<byte> buffer) => _reader.Read(buffer);
|
|
|
|
|
|
2021-12-13 09:31:02 -08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public BitArray ReadBitArray()
|
|
|
|
|
{
|
|
|
|
|
var length = ((IGenericReader)this).ReadEncodedInt();
|
|
|
|
|
|
|
|
|
|
// BinaryReader doesn't expose a Span slice of the buffer, so we use a custom ctor
|
|
|
|
|
return new BitArray(_reader, length);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 00:27:19 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2021-05-09 00:10:39 -07:00
|
|
|
public long Seek(long offset, SeekOrigin origin) => _reader.BaseStream.Seek(offset, origin);
|
|
|
|
|
|
2021-08-25 00:27:19 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2021-05-09 00:10:39 -07:00
|
|
|
public void Dispose() => Close();
|
|
|
|
|
}
|
|
|
|
|
}
|