Advertisement
kujikita

IO.cs

Dec 13th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 26.75 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections.Generic;
  6.  
  7. namespace KKt
  8. {
  9.     public class IO
  10.     {
  11.         public unsafe class KKtIO
  12.         {
  13.             protected Stream Stream;
  14.             protected byte CurrentBitReader = 0;
  15.             protected byte CurrentValReader = 0;
  16.             protected byte CurrentBitWriter = 0;
  17.             protected byte CurrentValWriter = 0;
  18.             protected bool HalfByteReader = false;
  19.             protected bool HalfByteWriter = false;
  20.  
  21.             private readonly byte[] buf = new byte[16];
  22.             public Main.Format format = Main.Format.F;
  23.             public bool IsBE = false;
  24.             public bool IsX = false;
  25.             public int I = 0;
  26.  
  27.             public long Length => Stream.Length;
  28.             public long Position => Stream.Position;
  29.             public bool CanRead => Stream.CanRead;
  30.             public bool CanSeek => Stream.CanSeek;
  31.             public bool CanTimeout => Stream.CanTimeout;
  32.             public bool CanWrite => Stream.CanWrite;
  33.  
  34.             protected KKtIO()
  35.             {
  36.                 Stream = Stream.Null;
  37.                 CurrentBitReader = CurrentValReader = CurrentBitWriter = CurrentValWriter = 0;
  38.                 HalfByteReader = HalfByteWriter = false;
  39.                 Stream = Stream.Null;
  40.                 buf = new byte[16];
  41.                 I = 0;
  42.                 IsBE = false;
  43.             }
  44.  
  45.             public KKtIO(Stream output)
  46.             {
  47.                 Stream = Stream.Null;
  48.                 CurrentBitReader = CurrentValReader = CurrentBitWriter = CurrentValWriter = 0;
  49.                 HalfByteReader = HalfByteWriter = false;
  50.                 Stream = output;
  51.                 buf = new byte[16];
  52.                 I = 0;
  53.                 IsBE = false;
  54.             }
  55.  
  56.             public KKtIO(Stream output, bool isBE)
  57.             {
  58.                 Stream = Stream.Null;
  59.                 CurrentBitReader = CurrentValReader = CurrentBitWriter = CurrentValWriter = 0;
  60.                 HalfByteReader = HalfByteWriter = false;
  61.                 Stream = output;
  62.                 buf = new byte[16];
  63.                 I = 0;
  64.                 IsBE = isBE;
  65.             }
  66.  
  67.             public KKtIO(Stream output, int i)
  68.             {
  69.                 Stream = Stream.Null;
  70.                 CurrentBitReader = CurrentValReader = CurrentBitWriter = CurrentValWriter = 0;
  71.                 HalfByteReader = HalfByteWriter = false;
  72.                 Stream = output;
  73.                 buf = new byte[16];
  74.                 I = i;
  75.                 IsBE = false;
  76.             }
  77.  
  78.             public KKtIO(Stream output, int i, bool isBE)
  79.             {
  80.                 Stream = Stream.Null;
  81.                 CurrentBitReader = CurrentValReader = CurrentBitWriter = CurrentValWriter = 0;
  82.                 HalfByteReader = HalfByteWriter = false;
  83.                 Stream = output;
  84.                 buf = new byte[16];
  85.                 I = i;
  86.                 IsBE = isBE;
  87.             }
  88.  
  89.             public void Close()
  90.             { CheckWrited(); Dispose(true); }
  91.  
  92.             protected void Dispose(bool disposing)
  93.             { if (disposing) Stream.Flush(); }
  94.  
  95.             public void Dispose()
  96.             { CheckWrited(); Dispose(true); }
  97.  
  98.             public Stream BaseStream
  99.             {
  100.                 get
  101.                 {
  102.                     Flush();
  103.                     return Stream;
  104.                 }
  105.             }
  106.  
  107.             public void Flush()
  108.             { Stream.Flush(); }
  109.  
  110.             public long Seek(long offset, SeekOrigin origin)
  111.             { return Stream.Seek(offset, origin); }
  112.  
  113.             public void SetLength(long length)
  114.             { Stream.SetLength(length); }
  115.  
  116.             public void Align(long Align)
  117.             {
  118.                 long Position = Stream.Position;
  119.                 long Al = Align - Position % Align;
  120.                 if (Position % Align != 0)
  121.                     Stream.Seek(Stream.Position + Al, 0);
  122.             }
  123.  
  124.             public   bool ReadBoolean()
  125.             { Read(1); return               buf[0] == 1; }
  126.             public   byte    ReadByte()
  127.             { Read(1); return               buf[0]; }
  128.             public  sbyte   ReadSByte()
  129.             { Read(1); return ( sbyte)      buf[0]; }
  130.             public  sbyte    ReadInt8()
  131.             { Read(1); return ( sbyte)      buf[0]; }
  132.             public   byte   ReadUInt8()
  133.             { Read(1); return               buf[0]; }
  134.             public  short   ReadInt16()
  135.             { Read(2); return (short)     ((buf[1] << 8) | buf[0]); }
  136.             public ushort  ReadUInt16()
  137.             { Read(2); return (ushort)    ((buf[1] << 8) | buf[0]); }
  138.             public    int   ReadInt24()
  139.             { Read(2); return            (((buf[2] << 8) | buf[1]) << 8) | buf[0]; }
  140.             public   uint  ReadUInt24()
  141.             { Read(2); return (  uint)  ((((buf[2] << 8) | buf[1]) << 8) | buf[0]); }
  142.             public    int   ReadInt32()
  143.             { Read(4); return          (((((buf[3] << 8) | buf[2]) << 8) | buf[1]) << 8) | buf[0]; }
  144.             public   uint  ReadUInt32()
  145.             { Read(4); return (  uint)((((((buf[3] << 8) | buf[2]) << 8) | buf[1]) << 8) | buf[0]); }
  146.             public   long   ReadInt64()
  147.             {   uint a = ReadUInt32(); uint b = ReadUInt32(); return (( long)b << 32) | a; }
  148.             public  ulong  ReadUInt64()
  149.             {   uint a = ReadUInt32(); uint b = ReadUInt32(); return ((ulong)b << 32) | a; }
  150.             public double    ReadHalf()
  151.             { ushort a = ReadUInt16(); return Main.ToDouble(a); }
  152.             public  float  ReadSingle()
  153.             {   uint a = ReadUInt32(); return *( float*)&a; }
  154.             public double  ReadDouble()
  155.             {  ulong a = ReadUInt64(); return *(double*)&a; }
  156.  
  157.             public short ReadInt16(bool Swap)
  158.             { return Endian(ReadInt16()); }
  159.             public ushort ReadUInt16(bool Swap)
  160.             { return Endian(ReadUInt16()); }
  161.             public int ReadInt24(bool Swap)
  162.             { return Endian(ReadInt24()) >> 8; }
  163.             public uint ReadUInt24(bool Swap)
  164.             { return Endian(ReadUInt24()) >> 8; }
  165.             public int ReadInt32(bool Swap)
  166.             { return Endian(ReadInt32()); }
  167.             public uint ReadUInt32(bool Swap)
  168.             { return Endian(ReadUInt32()); }
  169.             public long ReadInt64(bool Swap)
  170.             { return Endian(ReadInt64()); }
  171.             public ulong ReadUInt64(bool Swap)
  172.             { return Endian(ReadUInt64()); }
  173.             public float ReadSingle(bool Swap)
  174.             { uint a = Endian(ReadUInt32()); return *(float*)&a; }
  175.  
  176.             public  short  ReadInt16(bool Swap, bool IsBE)
  177.             { return Endian( ReadInt16(), IsBE); }
  178.             public ushort ReadUInt16(bool Swap, bool IsBE)
  179.             { return Endian(ReadUInt16(), IsBE); }
  180.             public    int  ReadInt24(bool Swap, bool IsBE)
  181.             { return Endian( ReadInt24(), IsBE) >> 8; }
  182.             public   uint ReadUInt24(bool Swap, bool IsBE)
  183.             { return Endian(ReadUInt24(), IsBE) >> 8; }
  184.             public    int  ReadInt32(bool Swap, bool IsBE)
  185.             { return Endian( ReadInt32(), IsBE); }
  186.             public   uint ReadUInt32(bool Swap, bool IsBE)
  187.             { return Endian(ReadUInt32(), IsBE); }
  188.             public   long  ReadInt64(bool Swap, bool IsBE)
  189.             { return Endian( ReadInt64(), IsBE); }
  190.             public  ulong ReadUInt64(bool Swap, bool IsBE)
  191.             { return Endian(ReadUInt64(), IsBE); }
  192.             public  float ReadSingle(bool Swap, bool IsBE)
  193.             { uint a = Endian(ReadUInt32(), IsBE); return *( float*)&a; }
  194.             public double ReadDouble(bool Swap, bool IsBE)
  195.             { long a = Endian( ReadInt64(), IsBE); return *(double*)&a; }
  196.  
  197.             public void Write(byte[] Val)
  198.             { Stream.Write(Val, 0, Val.Length); }
  199.             public void Write(byte[] Val, int Length)
  200.             { Stream.Write(Val, 0, Length); }
  201.             public void Write(byte[] Val, int Offset, int Length)
  202.             { Stream.Write(Val, Offset, Length); }
  203.             public void WriteVal(byte Length)
  204.             { Stream.Write(buf, 0, Length); }
  205.             public void Write(char[] val)
  206.             { Write(Encoding.UTF8.GetBytes(val)); }
  207.             public void Write(char[] val, bool UTF8)
  208.             { if (UTF8) Write(Encoding.UTF8.GetBytes(val)); else Write(Encoding.ASCII.GetBytes(val)); }
  209.  
  210.             public void Write(  bool val)
  211.             { ToArray(1, val ? 1 : 0); }
  212.             public void Write( sbyte val)
  213.             { ToArray(1, val); }
  214.             public void Write(  byte val)
  215.             { ToArray(1, val); }
  216.             public void Write( short val)
  217.             { ToArray(2, val); }
  218.             public void Write(ushort val)
  219.             { ToArray(2, val); }
  220.             public void Write(   int val)
  221.             { ToArray(4, val); }
  222.             public void Write(  uint val)
  223.             { ToArray(4, val); }
  224.             public void Write(  long val)
  225.             { ToArray(8, val); }
  226.             public void Write( ulong val)
  227.             { ToArray(8, val); }
  228.             public void Write( float val)
  229.             { ToArray(4, *( uint*)&val); }
  230.             public void Write(double val)
  231.             { ToArray(8, *(ulong*)&val); }
  232.  
  233.             public void Write(char val)
  234.             { Write(new char[] { val }); }
  235.             public void Write(char val, bool UTF8)
  236.             { if (UTF8) Write(Encoding.UTF8.GetBytes(val.ToString())); else Write(Encoding.ASCII.GetBytes(val.ToString())); }
  237.             public void Write(string val)
  238.             { Write(Encoding.UTF8.GetBytes(val)); }
  239.             public void Write(string val, bool UTF8)
  240.             { if (UTF8) Write(Encoding.UTF8.GetBytes(val)); else Write(Encoding.ASCII.GetBytes(val)); }
  241.             public void Write(string Data, string val)
  242.             { if (val != null) if (val != "") if (val.Length > 0)
  243.                         Write(Encoding.UTF8.GetBytes(Data + val + "\n")); }
  244.  
  245.             public void Write( short val, bool Swap)
  246.             { Write(Endian(val, IsBE)); }
  247.             public void Write(ushort val, bool Swap)
  248.             { Write(Endian(val, IsBE)); }
  249.             public void Write(   int val, bool Swap)
  250.             { Write(Endian(val, IsBE)); }
  251.             public void Write(  uint val, bool Swap)
  252.             { Write(Endian(val, IsBE)); }
  253.             public void Write(  long val, bool Swap)
  254.             { Write(Endian(val, IsBE)); }
  255.             public void Write( ulong val, bool Swap)
  256.             { Write(Endian(val, IsBE)); }
  257.             public void Write( float val, bool Swap)
  258.             { Write(Endian(*( uint*)&val, IsBE)); }
  259.             public void Write(double val, bool Swap)
  260.             { Write(Endian(*(ulong*)&val, IsBE)); }
  261.  
  262.             public void Write( short val, bool Swap, bool IsBE)
  263.             { Write( (short)Endian(val, 2, IsBE)); }
  264.             public void Write(ushort val, bool Swap, bool IsBE)
  265.             { Write((ushort)Endian(val, 2, IsBE)); }
  266.             public void Write(   int val, bool Swap, bool IsBE)
  267.             { Write((   int)Endian(val, 4, IsBE)); }
  268.             public void Write(  uint val, bool Swap, bool IsBE)
  269.             { Write((  uint)Endian(val, 4, IsBE)); }
  270.             public void Write(  long val, bool Swap, bool IsBE)
  271.             { Write((  long)Endian(val, 8, IsBE)); }
  272.             public void Write( ulong val, bool Swap, bool IsBE)
  273.             { Write(( ulong)Endian(val, 8, IsBE)); }
  274.             public void Write( float val, bool Swap, bool IsBE)
  275.             { Write((  uint)Endian(*( uint*)&val, 4, IsBE)); }
  276.             public void Write(double val, bool Swap, bool IsBE)
  277.             { Write(( ulong)Endian(*(ulong*)&val, 8, IsBE)); }
  278.  
  279.             public  short Endian( short BE) { return ( short)Endian(BE, 2, IsBE); }
  280.             public ushort Endian(ushort BE) { return (ushort)Endian(BE, 2, IsBE); }
  281.             public    int Endian(   int BE) { return (   int)Endian(BE, 4, IsBE); }
  282.             public   uint Endian(  uint BE) { return (  uint)Endian(BE, 4, IsBE); }
  283.             public   long Endian(  long BE) { return (  long)Endian(BE, 8, IsBE); }
  284.             public  ulong Endian( ulong BE) { return ( ulong)Endian(BE, 8, IsBE); }
  285.  
  286.             public  short Endian( short BE, bool IsBE) { return ( short)Endian(BE, 2, IsBE); }
  287.             public ushort Endian(ushort BE, bool IsBE) { return (ushort)Endian(BE, 2, IsBE); }
  288.             public    int Endian(   int BE, bool IsBE) { return (   int)Endian(BE, 4, IsBE); }
  289.             public   uint Endian(  uint BE, bool IsBE) { return (  uint)Endian(BE, 4, IsBE); }
  290.             public   long Endian(  long BE, bool IsBE) { return (  long)Endian(BE, 8, IsBE); }
  291.             public  ulong Endian( ulong BE, bool IsBE) { return ( ulong)Endian(BE, 8, IsBE); }
  292.  
  293.             public long Endian( long BE, byte Length, bool IsBE)
  294.             {
  295.                 if (IsBE)
  296.                 {
  297.                     for (byte i = 0; i < Length; i++) { buf[i] = (byte)BE; BE >>= 8; }
  298.                     BE = 0; for (byte i = 0; i < Length; i++) { BE |= buf[i]; if (i < Length - 1) BE <<= 8; }
  299.                 }
  300.                 return BE;
  301.             }
  302.  
  303.             public ulong Endian(ulong BE, byte Length, bool IsBE)
  304.             {
  305.                 if (IsBE)
  306.                 {
  307.                     for (byte i = 0; i < Length; i++) { buf[i] = (byte)BE; BE >>= 8; }
  308.                     BE = 0; for (byte i = 0; i < Length; i++) { BE |= buf[i]; if (i < Length - 1) BE <<= 8; }
  309.                 }
  310.                 return BE;
  311.             }
  312.  
  313.             public void ToArray(byte Length,  long val)
  314.             {
  315.                 for (byte i = 0; i < Length; i++) { buf[i] = (byte)val; val >>= 8; }
  316.                 WriteVal(Length);
  317.             }
  318.  
  319.             public void ToArray(byte Length, ulong val)
  320.             {
  321.                 for (byte i = 0; i < Length; i++) { buf[i] = (byte)val; val >>= 8; }
  322.                 WriteVal(Length);
  323.             }
  324.  
  325.             public void Read(byte Length)
  326.             { Stream.Read(buf, 0, Length); }
  327.  
  328.             public byte[] Read(int Offset, int Length)
  329.             { byte[] Val = new byte[Length]; Stream.Read(Val, Offset, Length); return Val; }
  330.  
  331.             public string ReadString(long Length)
  332.             {           return Encoding.UTF8.GetString(ReadBytes(Length)); }
  333.  
  334.             public string ReadString(long Length, bool UTF8)
  335.             { if (UTF8) return Encoding. UTF8.GetString(ReadBytes(Length));
  336.                    else return Encoding.ASCII.GetString(ReadBytes(Length)); }
  337.  
  338.             public string ReadStringUTF8 (long Length)
  339.             { return Encoding. UTF8.GetString(ReadBytes(Length)); }
  340.  
  341.             public string ReadStringASCII(long Length)
  342.             { return Encoding.ASCII.GetString(ReadBytes(Length)); }
  343.  
  344.             public byte[] ReadBytes(long Length)
  345.             { byte[] Buf = new byte[Length]; Stream.Read(Buf, 0, (int)Length); return Buf; }
  346.            
  347.             public long ReadBits(byte Bits)
  348.             {
  349.                 long val = 0;
  350.                 for (byte i = 0; i < Bits; i++)
  351.                 {
  352.                     val <<= 1;
  353.                     val |= ReadBit();
  354.                 }
  355.                 return val;
  356.             }
  357.  
  358.             public byte ReadBit()
  359.             {
  360.                 if (CurrentBitReader >= 8)
  361.                 {
  362.                     CurrentValReader = ReadByte();
  363.                     CurrentBitReader = 0;
  364.                 }
  365.                 CurrentBitReader++;
  366.                 return (byte)((CurrentValReader >> (8 - CurrentBitReader)) & 0x1);
  367.             }
  368.  
  369.             public void Write(long val, byte Bits)
  370.             {
  371.                 byte Val = 0;
  372.                 for (byte i = 0; i < Bits; i++)
  373.                 {
  374.                     Val = (byte)((val >> (byte)(Bits - 1 - i)) & 0x1);
  375.                     Write(Val, 0);
  376.                 }
  377.             }
  378.  
  379.             public void Write(byte val, byte Bit)
  380.             {
  381.                 CurrentValWriter |= (byte)(val << (7 - CurrentBitWriter));
  382.                 CurrentBitWriter++;
  383.                 if (CurrentBitWriter > 7)
  384.                 {
  385.                     Write(CurrentValWriter);
  386.                     CurrentValWriter = 0;
  387.                     CurrentBitWriter = 0;
  388.                 }
  389.             }
  390.  
  391.             public void CheckWrited()
  392.             {
  393.                 if (CurrentValWriter != 0)
  394.                 {
  395.                     Write(CurrentValWriter);
  396.                     CurrentValWriter = 0;
  397.                     CurrentBitWriter = 0;
  398.                 }
  399.                 else if (CurrentBitWriter != 0)
  400.                 {
  401.                     CurrentValWriter = 0;
  402.                     CurrentBitWriter = 0;
  403.                 }
  404.             }
  405.  
  406.             public byte ReadHalfByte()
  407.             {
  408.                 Read(1);
  409.                 if (HalfByteReader)
  410.                 {
  411.                     HalfByteReader = false;
  412.                     return (byte)(buf[0] & 0x0F);
  413.                 }
  414.                 else
  415.                 {
  416.                     HalfByteReader = true;
  417.                     Stream.Seek(-1, SeekOrigin.Current);
  418.                     return (byte)((buf[0] >> 4) & 0x0F);
  419.                 }
  420.             }
  421.  
  422.             public long ReadHalfByte(int Count)
  423.             {
  424.                 long val = 0;
  425.                 for (byte i = 0; i < Count; i++)
  426.                 {
  427.                     val <<= 4;
  428.                     val |= ReadHalfByte();
  429.                 }
  430.                 return val;
  431.             }
  432.  
  433.             public void Write(byte val, bool HalfByte)
  434.             {
  435.                 val = (byte)(val & 0x0F);
  436.                 if (HalfByteWriter)
  437.                 {
  438.                     val = (byte)((Stream.ReadByte()) & 0xF0 | val);
  439.                     Stream.Seek(-1, SeekOrigin.Current);
  440.                     HalfByteWriter = false;
  441.                 }
  442.                 else
  443.                 {
  444.                     val <<= 4;
  445.                     HalfByteWriter = true;
  446.                 }
  447.                 Write(val);
  448.                 if (HalfByteWriter)
  449.                     Stream.Seek(-1, SeekOrigin.Current);
  450.             }
  451.  
  452.             public void Write(long val, bool HalfByte, int Count)
  453.             {
  454.                 byte Val = 0;
  455.                 for (byte i = 0; i < Count; i++)
  456.                 {
  457.                     Val = (byte)((val >> (byte)((Count - 1 - i) * 4)) & 0xF);
  458.                     Write(Val, HalfByte);
  459.                 }
  460.             }
  461.            
  462.             public static KKtIO OpenFileReader(string file)
  463.             {
  464.                 KKtIO IO = new KKtIO(new FileStream(file, FileMode.Open        , FileAccess.ReadWrite, FileShare.ReadWrite));
  465.                 return IO;
  466.             }
  467.  
  468.             public static KKtIO OpenFileWriter(string file)
  469.             {
  470.                 KKtIO IO = new KKtIO(new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite));
  471.                 return IO;
  472.             }
  473.  
  474.             public static KKtIO OpenFileWriter(string file, bool SetLength0)
  475.             {
  476.                 KKtIO IO = new KKtIO(new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite));
  477.                 IO.SetLength(0);
  478.                 return IO;
  479.             }
  480.  
  481.             public static KKtIO OpenFileWriter(string file, int SetLength)
  482.             {
  483.                 KKtIO IO = new KKtIO(new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite));
  484.                 IO.SetLength(SetLength);
  485.                 return IO;
  486.             }
  487.  
  488.             public string NullTerminated(byte End)
  489.             {
  490.                 string s = "";
  491.                 while (true)
  492.                 {
  493.                     byte a = ReadByte();
  494.                     if (a == End)
  495.                         break;
  496.                     else
  497.                         s += Convert.ToChar(a);
  498.                 }
  499.                 return s;
  500.             }
  501.  
  502.             public List<byte> NullTerminated(bool Byte)
  503.             {
  504.                 List<byte> s = new List<byte>();
  505.                 while (true)
  506.                 {
  507.                     byte a = ReadByte();
  508.                     if (a == 0x00)
  509.                         break;
  510.                     else
  511.                         s.Add(a);
  512.                 }
  513.                 return s;
  514.             }
  515.  
  516.             public string NullTerminated(bool ReadOffset, ref Main.POF0 POF0)
  517.             {
  518.                 GetOffset(ref POF0);
  519.                 string s = "";
  520.                 long CurrentOffset = Position;
  521.                 if (IsX)
  522.                     Seek(ReadInt64() + 0x20, 0);
  523.                 else
  524.                     Seek(ReadUInt32(true), 0);
  525.                 s = NullTerminated(0x00);
  526.                 Seek(CurrentOffset + (IsX ? 8 : 4), 0);
  527.                 return s;
  528.             }
  529.  
  530.             public byte[] NullTerminated(bool ReadOffset, ref Main.POF0 POF0, bool Byte)
  531.             {
  532.                 GetOffset(ref POF0);
  533.                 long CurrentOffset = Position;
  534.                 if (IsX)
  535.                     Seek(ReadInt64() + 0x20, 0);
  536.                 else
  537.                     Seek(ReadUInt32(true), 0);
  538.                 List<byte> S = NullTerminated(Byte);
  539.                 byte[] s = new byte[S.Count];
  540.                 for (int i = 0; i < S.Count; i++)
  541.                     s[i] = S[i];
  542.                 Seek(CurrentOffset + (IsX ? 8 : 4), 0);
  543.                 return s;
  544.             }
  545.  
  546.             public long GetOffset(ref Main.POF0 POF0)
  547.             {
  548.                 if ((byte)format > 1 && (byte)format < 5)
  549.                 {
  550.                     POF0.POF0Offsets.Add(Position - (IsX ? 0x20 : 0x00));
  551.                     return Position;
  552.                 }
  553.                 return 0;
  554.             }
  555.  
  556.             public Main.Header HeaderReader()
  557.             {
  558.                 if (Position >= 4)
  559.                     Seek(-4, SeekOrigin.Current);
  560.                 else
  561.                     Seek(0, 0);
  562.                 Main.Header Header = new Main.Header();
  563.                 Header.Format = format = Main.Format.F2LE;
  564.                 Header.Signature = ReadInt32();
  565.                 Header.DataSize = ReadInt32();
  566.                 Header.Lenght = ReadInt32();
  567.                 if (ReadUInt32() == 0x18000000)
  568.                 {
  569.                     IsBE = Header.IsBE = true;
  570.                     Header.Format = format = Main.Format.F2BE;
  571.                 }
  572.                 ReadUInt32();
  573.                 Header.SectionSize = ReadInt32();
  574.                 IsBE = Header.IsBE;
  575.                 Seek(Header.Lenght, 0);
  576.                 Header.Signature = ReadInt32(true);
  577.                 return Header;
  578.             }
  579.  
  580.             public void EOFCWriter()
  581.             {
  582.                 Write(Encoding.ASCII.GetBytes("EOFC"));
  583.                 Write(0x00);
  584.                 Write(0x20);
  585.                 Write(0x10000000);
  586.                 Write(0x00);
  587.                 Write(0x00);
  588.                 Write(0x00);
  589.                 Write(0x00);
  590.             }
  591.  
  592.             public bool WAVHeadReader(out Main.WAVHeader Header, out short Bytes, out short Format)
  593.             {
  594.                 Header = new Main.WAVHeader();
  595.                 Bytes = 0;
  596.                 Format = 0;
  597.                 if (ReadString(4) != "RIFF")
  598.                     return false;
  599.                 ReadUInt32();
  600.                 if (ReadString(4) != "WAVE")
  601.                     return false;
  602.                 if (ReadString(4) != "fmt ")
  603.                     return false;
  604.                 int Offset = ReadInt32();
  605.                 Format = ReadInt16();
  606.                 if (Format != 0x01 && Format != 0x03)
  607.                     return false;
  608.                 Header.Channels = ReadInt16();
  609.                 Header.SampleRate = ReadInt32();
  610.                 ReadInt32();
  611.                 ReadInt16();
  612.                 Bytes = (short)(ReadInt16() / 0x08);
  613.                 if ((Bytes < 2 || Bytes > 4) && Bytes != 8)
  614.                     return false;
  615.                 Seek(Offset + 0x14, 0);
  616.                 if (ReadString(4) != "data")
  617.                     return false;
  618.                 Header.Size = ReadInt32();
  619.                 Header.HeaderSize = (int)Position;
  620.                 return true;
  621.             }
  622.  
  623.             public void WAVHeadWriter(Main.WAVHeader Header, short Bytes)
  624.             {
  625.                 Seek(0, 0);
  626.                 Write("RIFF");
  627.                 Write(Header.Size + 0x26);
  628.                 Write("WAVE");
  629.                 Write("fmt ");
  630.                 Write(0x10);
  631.                 Write((short)(Bytes == 2 ? 0x01 : 0x03));
  632.                 Write((short)Header.Channels);
  633.                 Write(Header.SampleRate);
  634.                 Write(Header.SampleRate * Header.Channels * Bytes);
  635.                 Write((short)(Header.Channels * Bytes));
  636.                 Write((short)(0x08 * Bytes));
  637.                 Write("data");
  638.                 Write(Header.Size);
  639.             }
  640.  
  641.             public void POF0Reader(ref Main.POF0 POF0)
  642.             {
  643.                 if (ReadString(3).StartsWith("POF"))
  644.                 {
  645.                     POF0.POF0Offsets = POF0.POF0Offsets.Distinct().OrderBy(x => x).ToList();
  646.                     POF0.Header = HeaderReader();
  647.                     Seek(POF0.Offset + POF0.Header.Lenght, 0);
  648.                     long position = Position;
  649.                     POF0.Lenght = ReadInt32();
  650.                     while (POF0.Lenght + POF0.Offset + POF0.Header.Lenght > position)
  651.                     {
  652.                         int a = ReadByte();
  653.                         if (a >> 6 == 0)
  654.                             break;
  655.                         else if (a >> 6 == 1)
  656.                             a = a & 0x3F;
  657.                         else if (a >> 6 == 2)
  658.                         {
  659.                             a = a & 0x3F;
  660.                             a = (a << 8) | ReadByte();
  661.                         }
  662.                         else if (a >> 6 == 3)
  663.                         {
  664.                             a = a & 0x3F;
  665.                             a = (a << 8) | ReadByte();
  666.                             a = (a << 8) | ReadByte();
  667.                             a = (a << 8) | ReadByte();
  668.                         }
  669.                         a <<= IsX ? 3 : 2;
  670.                         POF0.LastOffset += a;
  671.                         POF0.Offsets.Add(POF0.LastOffset);
  672.                         position = Position;
  673.                     }
  674.  
  675.                     for (int i = 0; i < POF0.Offsets.Count; i++)
  676.                         if (POF0.Offsets[i] != POF0.POF0Offsets[i])
  677.                             Console.WriteLine("Not right POF{0} offset table.\n  Expected: {1}\n" +
  678.                                 "  Got:      {2}", POF0.Type, POF0.Offsets[i], POF0.POF0Offsets[i]);
  679.                 }
  680.             }
  681.         }
  682.     }
  683. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement