public static void GetOffset(this Stream stream, ref POF POF) { if (stream.Format > Main.Format.F) { POF.Offsets.Add(stream.Position + (stream.IsX ? stream.Offset : 0x00)); } }
public static void Write(this Stream stream, ref POF POF, int ID) { POF.Offsets.Sort(); long CurrentPOFOffset = 0; long POFOffset = 0; byte BitShift = (byte)(2 + POF.Type); int Max1 = (0x00FF >> BitShift) << BitShift; int Max2 = (0xFFFF >> BitShift) << BitShift; POF.Length = 5 + ID; for (int i = 0; i < POF.Offsets.Count; i++) { POFOffset = POF.Offsets[i] - CurrentPOFOffset; CurrentPOFOffset = POF.Offsets[i]; if (POFOffset <= Max1) { POF.Length += 1; } else if (POFOffset <= Max2) { POF.Length += 2; } else { POF.Length += 4; } POF.Offsets[i] = POFOffset; } long POFLengthAling = POF.Length.Align(16); POF.Header = new Header { DataSize = (int)POFLengthAling, ID = ID, Format = Main.Format.F2LE, Length = 0x20, SectionSize = (int)POFLengthAling, Signature = 0x30464F50 }; POF.Header.Signature += POF.Type << 24; stream.Write(POF.Header); stream.Write(POF.Length); for (int i = 0; i < POF.Offsets.Count; i++) { POFOffset = POF.Offsets[i]; if (POFOffset <= Max1) { stream.Write(( byte)((1 << 6) | (POFOffset >> BitShift))); } else if (POFOffset <= Max2) { stream.WriteEndian((ushort)((2 << 14) | (POFOffset >> BitShift)), true); } else { stream.WriteEndian(( uint)((3 << 30) | (POFOffset >> BitShift)), true); } } stream.Write(0x00); stream.Align(16, true); stream.WriteEOFC(ID); }
public static POF AddPOF(this PDHead Header) { POF POF = new POF { Offsets = new List <long>(), POFOffsets = new List <long>(), Offset = Header.DataSize + Header.Lenght }; return(POF); }
public static Stream GetOffset(this Stream stream, ref POF POF) { if (POF != null) { if (stream.Format > Main.Format.F) { POF.POFOffsets.Add(stream.Position + (stream.IsX ? stream.Offset : 0x00)); } } return(stream); }
public static void W(this Stream stream, POF pof, bool shiftX = false, int depth = 0) { byte[] data = pof.Write(shiftX); Header header = new Header { Depth = depth, Format = Format.F2LE, Length = 0x20, Signature = shiftX ? 0x31464F50 : 0x30464F50 }; header.DataSize = header.SectionSize = data.Length; stream.W(header); stream.W(data); }
public void Dispose() { if (!disposed) { if (s != null) { s.D(); } s = null; Strings = default; pof = default; header = default; disposed = true; } }
public void Dispose() { if (!disposed) { if (_IO != null) { _IO.Dispose(); } STRs = default; pof = default; header = default; disposed = true; } }
public static void ReadPOF(this Stream stream, ref POF POF) { if (stream.ReadString(3) == "POF") { POF.POFOffsets.Sort(); POF.Type = byte.Parse(stream.ReadString(1)); int IsX = POF.Type + 2; stream.Seek(-4, SeekOrigin.Current); POF.Header = stream.ReadHeader(); stream.Seek(POF.Offset + POF.Header.Lenght, 0); POF.Lenght = stream.ReadInt32(); while (POF.Lenght + POF.Offset + POF.Header.Lenght > stream.Position) { int a = stream.ReadByte(); if (a >> 6 == 0) { break; } else if (a >> 6 == 1) { a = a & 0x3F; } else if (a >> 6 == 2) { a = a & 0x3F; a = (a << 8) | stream.ReadByte(); } else if (a >> 6 == 3) { a = a & 0x3F; a = (a << 8) | stream.ReadByte(); a = (a << 8) | stream.ReadByte(); a = (a << 8) | stream.ReadByte(); } a <<= IsX; POF.LastOffset += a; POF.Offsets.Add(POF.LastOffset); } for (int i = 0; i < POF.Offsets.Count && i < POF.POFOffsets.Count; i++) { if (POF.Offsets[i] != POF.POFOffsets[i]) { System.Console.WriteLine("Not right POF{0} offset table.\n" + " Expected: {1}\n Got: {2}", POF.Type, POF.Offsets[i].ToString("X8"), POF.POFOffsets[i].ToString("X8")); } } } }
public static void W(this Stream stream, POF pof, bool shiftX = false, uint depth = 0) { byte[] data = pof.Write(shiftX); Header header = new Header { Depth = depth, Format = Format.F2, Length = 0x20, Signature = shiftX ? 0x31464F50u : 0x30464F50u, UseSectionSize = true }; header.DataSize = header.SectionSize = (uint)data.Length.A(0x10); stream.W(header); stream.W(data); for (int c = data.Length.A(0x10) - data.Length; c > 0; c--) { stream.W((byte)0); } }
public static void ReadPOF(this Stream stream, ref POF POF) { if (stream.ReadString(3) == "POF") { POF.Type = byte.Parse(stream.ReadString(1)); int IsX = POF.Type + 2; stream.Seek(-4, SeekOrigin.Current); POF.Header = stream.ReadHeader(); stream.Seek(POF.Offset + POF.Header.Length, 0); POF.Length = stream.ReadInt32(); while (POF.Length + POF.Offset + POF.Header.Length > stream.Position) { int a = stream.ReadByte(); if (a >> 6 == 0) { break; } else if (a >> 6 == 1) { a = a & 0x3F; } else if (a >> 6 == 2) { a = a & 0x3F; a = (a << 8) | stream.ReadByte(); } else if (a >> 6 == 3) { a = a & 0x3F; a = (a << 8) | stream.ReadByte(); a = (a << 8) | stream.ReadByte(); a = (a << 8) | stream.ReadByte(); } a <<= IsX; POF.LastOffset += a; } } }
public static long ReadIntX(this Stream IO, ref POF POF, bool IsBE) => IO.IsX ? IO.ReadInt64() : IO.ReadUInt32Endian(IsBE);
public static long ReadUInt32Endian(this Stream IO, ref POF POF, bool IsBE) => IO.GetOffset(ref POF).ReadUInt32Endian(IsBE);
public static long ReadInt64(this Stream IO, ref POF POF) => IO.GetOffset(ref POF).ReadInt64();
public int STRReader(string filepath, string ext) { IO = File.OpenReader(filepath + ext); Header = new Header(); IO.Format = Main.Format.F; Header.Signature = IO.ReadInt32(); if (Header.Signature == 0x41525453) { Header = IO.ReadHeader(true); POF = Header.AddPOF(); IO.Position = Header.Length; long Count = IO.ReadInt32Endian(); Offset = IO.ReadInt32Endian(); if (Offset == 0) { Offset = Count; OffsetX = IO.ReadInt64(); Count = IO.ReadInt64(); IO.Offset = Header.Length; IO.Format = Main.Format.X; IO.LongOffset += Offset; IO.Position = 0; } else { IO.Position = Header.Length + 0x40; } STRs = new String[Count]; for (int i = 0; i < Count; i++) { STRs[i].ID = IO.ReadInt32Endian(); } if (IO.IsX) { IO.Offset = Header.Length; for (int i = 0; i < Count; i++) { STRs[i].Str.Value = IO.ReadStringAtOffset(STRs[i].Str.Offset + OffsetX); } } else { IO.Offset = 0; for (int i = 0; i < Count; i++) { STRs[i].Str.Value = STRs[i].Str.Offset > 0 ? IO.ReadStringAtOffset(STRs[i].Str.Offset) : null; } } IO.Position = POF.Offset; IO.ReadPOF(ref POF); } else { IO.Position -= 4; int Count = 0; for (int a = 0, i = 0; IO.Position > 0 && IO.Position < IO.Length; i++, Count++) { a = IO.ReadInt32(); if (a == 0) { break; } } STRs = new String[Count]; for (int i = 0; i < Count; i++) { IO.LongPosition = STRs[i].Str.Offset; STRs[i].ID = i; STRs[i].Str.Value = IO.NullTerminatedUTF8(); } } IO.Close(); return(1); }
public void STRWriter(string filepath) { if (STRs == null || STRs.Length == 0 || Header.Format > Main.Format.F2BE) { return; } uint Offset = 0; uint CurrentOffset = 0; IO = File.OpenWriter(filepath + (Header.Format > Main.Format.FT ? ".str" : ".bin"), true); IO.Format = Header.Format; POF = new POF(); IO.IsBE = IO.Format == Main.Format.F2BE; long Count = STRs.LongLength; if (IO.Format > Main.Format.FT) { IO.Position = 0x40; IO.WriteX(Count); IO.GetOffset(ref POF); IO.WriteX(0x80); IO.Position = 0x80; for (int i = 0; i < Count; i++) { IO.Write(0x00L); } IO.Align(0x10); } else { for (int i = 0; i < Count; i++) { IO.Write(0x00); } IO.Align(0x20); } KKdList <string> UsedSTR = KKdList <string> .New; KKdList <int> UsedSTRPos = KKdList <int> .New; int[] STRPos = new int[Count]; for (int i = 0; i < Count; i++) { if (!UsedSTR.Contains(STRs[i].Str.Value)) { STRPos[i] = IO.Position; UsedSTRPos.Add(STRPos[i]); UsedSTR.Add(STRs[i].Str.Value); IO.Write(STRs[i].Str.Value + "\0"); } else { for (int i2 = 0; i2 < Count; i2++) { if (UsedSTR[i2] == STRs[i].Str.Value) { STRPos[i] = UsedSTRPos[i2]; break; } } } } if (IO.Format > Main.Format.FT) { IO.Align(0x10); Offset = IO.UIntPosition; IO.Position = 0x80; for (int i = 0; i < Count; i++) { IO.GetOffset(ref POF); IO.WriteEndian(STRPos[i]); IO.WriteEndian(STRs[i].ID); } IO.UIntPosition = Offset; IO.Write(ref POF, 1); CurrentOffset = IO.UIntPosition; IO.WriteEOFC(0); Header.DataSize = (int)(CurrentOffset - 0x40); Header.Signature = 0x41525453; Header.SectionSize = (int)(Offset - 0x40); IO.Position = 0; IO.Write(Header); } else { IO.Position = 0; for (int i = 0; i < Count; i++) { IO.Write(STRPos[i]); } } IO.Close(); }
public int STRReader(string filepath, string ext) { Stream reader = File.OpenReader(filepath + ext); Header = new PDHead(); reader.Format = Main.Format.F; Header.Signature = reader.ReadInt32(); if (Header.Signature == 0x41525453) { Header = reader.ReadHeader(true); POF = Header.AddPOF(); reader.Position = Header.Lenght; long Count = reader.ReadInt32Endian(); Offset = reader.ReadInt32Endian(); if (Offset == 0) { Offset = Count; OffsetX = reader.ReadInt64(); Count = reader.ReadInt64(); reader.Offset = Header.Lenght; reader.Format = Main.Format.X; } reader.LongPosition = reader.IsX ? Offset + reader.Offset : Offset; STRs = new String[Count]; for (int i = 0; i < Count; i++) { STRs[i].StrOffset = reader.GetOffset(ref POF).ReadInt32Endian(); STRs[i].ID = reader.ReadInt32Endian(); if (reader.IsX) { STRs[i].StrOffset += (int)OffsetX; } } for (int i = 0; i < Count; i++) { reader.LongPosition = STRs[i].StrOffset + (reader.IsX ? reader.Offset : 0); STRs[i].Str = reader.NullTerminatedUTF8(); } reader.Position = POF.Offset; reader.ReadPOF(ref POF); } else { reader.Position -= 4; int Count = 0; for (int a = 0, i = 0; reader.Position > 0 && reader.Position < reader.Length; i++, Count++) { a = reader.ReadInt32(); if (a == 0) { break; } } STRs = new String[Count]; for (int i = 0; i < Count; i++) { reader.LongPosition = STRs[i].StrOffset + (reader.IsX ? reader.Offset : 0); STRs[i].ID = i; STRs[i].Str = reader.NullTerminatedUTF8(); } } reader.Close(); return(1); }
public void STRWriter(string filepath) { uint Offset = 0; uint CurrentOffset = 0; Stream writer = File.OpenWriter(filepath + (Header. Format > Main.Format.FT ? ".str" : ".bin"), true); writer.Format = Header.Format; POF = new POF(); writer.IsBE = writer.Format == Main.Format.F2BE; long Count = STRs.LongLength; if (writer.Format > Main.Format.FT) { writer.Position = 0x40; writer.WriteEndian(Count); writer.GetOffset(ref POF).WriteEndian(0x80); writer.Position = 0x80; for (int i = 0; i < Count; i++) { writer.GetOffset(ref POF).Write(0x00); writer.WriteEndian(STRs[i].ID); } writer.Align(16); } else { for (int i = 0; i < Count; i++) { writer.Write(0x00); } writer.Align(32); } List <string> UsedSTR = new List <string>(); List <int> UsedSTRPos = new List <int>(); int[] STRPos = new int[Count]; for (int i1 = 0; i1 < Count; i1++) { if (UsedSTR.Contains(STRs[i1].Str)) { for (int i2 = 0; i2 < Count; i2++) { if (UsedSTR[i2] == STRs[i1].Str) { STRPos[i1] = UsedSTRPos[i2]; break; } } } else { STRPos[i1] = writer.Position; UsedSTRPos.Add(STRPos[i1]); UsedSTR.Add(STRs[i1].Str); writer.Write(STRs[i1].Str); writer.WriteByte(0); } } if (writer.Format > Main.Format.FT) { writer.Align(16); Offset = writer.UIntPosition; writer.Position = 0x80; } else { writer.Position = 0; } for (int i1 = 0; i1 < Count; i1++) { writer.WriteEndian(STRPos[i1]); if (writer.Format > Main.Format.FT) { writer.Position += 4; } } if (writer.Format > Main.Format.FT) { writer.UIntPosition = Offset; writer.Write(ref POF, 1); CurrentOffset = writer.UIntPosition; writer.WriteEOFC(0); Header.Lenght = 0x40; Header.DataSize = (int)(CurrentOffset - Header.Lenght); Header.Signature = 0x41525453; Header.SectionSize = (int)(Offset - Header.Lenght); writer.Position = 0; writer.Write(Header); } writer.Close(); }
public static string ReadStringAtOffset(this Stream IO, ref POF POF, long Offset = 0, long Length = 0) => IO.GetOffset(ref POF).ReadStringAtOffset(Offset, Length);