// Decompiled with JetBrains decompiler
// Type: FiestaLauncher.gClasses.BinaryReaderEx
// Assembly: FiestaOnline, Version=2019.5.22.0, Culture=neutral, PublicKeyToken=null
// MVID: 2FD5EB61-BC25-469D-997E-88998E849F6A
// Assembly location: C:\Users\marbo\Documents\Gamigo 2020 Client\FiestaOnline.exe

using System;
using System.IO;
using System.Text;

namespace FiestaLauncher.gClasses
{
  internal class BinaryReaderEx : BinaryReader
  {
    private static byte[] Buffer = new byte[2048];
    private const int BufferLength = 2048;

    public long Length
    {
      get
      {
        return this.BaseStream.Length;
      }
    }

    public long Position
    {
      get
      {
        return this.BaseStream.Position;
      }
    }

    public BinaryReaderEx(Stream imput)
      : base(imput)
    {
    }

    public BinaryReaderEx(Stream imput, Encoding encoding)
      : base(imput, encoding)
    {
    }

    private string _ReadString(uint bytes)
    {
      this.Read(BinaryReaderEx.Buffer, 0, (int) bytes);
      return Encoding.UTF8.GetString(BinaryReaderEx.Buffer, 0, (int) bytes);
    }

    public string _ReadStringEx(uint bytes)
    {
      byte[] numArray = new byte[(int) byte.MaxValue];
      for (uint index = 0; index < bytes; ++index)
      {
        byte num = this.ReadByte();
        if (num <= (byte) 31)
        {
          --this.BaseStream.Position;
          break;
        }
        numArray[(int) index] = num;
      }
      int num1 = 0;
      for (int index = 0; index < numArray.Length; ++index)
      {
        if (numArray[index] == (byte) 0)
          ++num1;
      }
      return Encoding.UTF8.GetString(new byte[numArray.Length - num1]).Trim();
    }

    public byte[] ReadByteArray(uint count)
    {
      byte[] numArray = new byte[(int) count];
      for (uint index = 0; index < count; ++index)
        numArray[(int) index] = this.ReadByte();
      return numArray;
    }

    public int ReadInt(int bytes)
    {
      switch (bytes)
      {
        case 1:
          return (int) this.ReadSByte();
        case 2:
          return (int) this.ReadInt16();
        case 3:
          throw new ArgumentException();
        case 4:
          return this.ReadInt32();
        default:
          throw new ArgumentException();
      }
    }

    public short[] ReadInt16Array(uint count)
    {
      short[] numArray = new short[(int) count];
      for (uint index = 0; index < count; ++index)
        numArray[(int) index] = this.ReadInt16();
      return numArray;
    }

    public int[] ReadInt32Array(uint count)
    {
      int[] numArray = new int[(int) count];
      for (uint index = 0; index < count; ++index)
        numArray[(int) index] = this.ReadInt32();
      return numArray;
    }

    public sbyte[] ReadSByteArray(uint count)
    {
      sbyte[] numArray = new sbyte[(int) count];
      for (uint index = 0; index < count; ++index)
        numArray[(int) index] = this.ReadSByte();
      return numArray;
    }

    public override string ReadString()
    {
      int count = 0;
      for (byte index1 = this.ReadByte(); index1 > (byte) 0; index1 = this.ReadByte())
      {
        int index2 = count++;
        BinaryReaderEx.Buffer[index2] = index1;
        if (count >= 256)
          break;
      }
      string empty = string.Empty;
      string str = Encoding.UTF8.GetString(BinaryReaderEx.Buffer, 0, count);
      if (count == 256)
        str += this.ReadString();
      return str;
    }

    public string ReadString(int bytes)
    {
      return bytes <= 0 ? string.Empty : this.ReadString((uint) bytes);
    }

    public string ReadString(uint bytes)
    {
      return this._ReadString(bytes).TrimEnd(new char[1]);
    }

    public uint ReadUInt(int bytes)
    {
      switch (bytes)
      {
        case 1:
          return (uint) this.ReadByte();
        case 2:
          return (uint) this.ReadUInt16();
        case 3:
          throw new ArgumentException();
        case 4:
          return this.ReadUInt32();
        default:
          throw new ArgumentException();
      }
    }

    public ushort[] ReadUInt16Array(uint count)
    {
      ushort[] numArray = new ushort[(int) count];
      for (uint index = 0; index < count; ++index)
        numArray[(int) index] = this.ReadUInt16();
      return numArray;
    }

    public uint[] ReadUInt32Array(uint count)
    {
      uint[] numArray = new uint[(int) count];
      for (uint index = 0; index < count; ++index)
        numArray[(int) index] = this.ReadUInt32();
      return numArray;
    }

    public long Seek(long offset, SeekOrigin origin)
    {
      return this.BaseStream.Seek(offset, origin);
    }

    public long Skip(long offset)
    {
      return this.Seek(offset, SeekOrigin.Current);
    }
  }
}
