|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Text; |
| 4 | +namespace DeathEndFormats.Formats.GDAT |
| 5 | +{ |
| 6 | + public class GDAT : IDisposable |
| 7 | + { |
| 8 | + public static GDAT Load(Stream data) |
| 9 | + { |
| 10 | + GDAT GDatData = new GDAT(); |
| 11 | + GDatData.Data = data; |
| 12 | + BinaryReader reader = new BinaryReader(data, Encoding.UTF8, true); |
| 13 | + |
| 14 | + using (reader) |
| 15 | + { |
| 16 | + string MAGIC = Encoding.UTF8.GetString(reader.ReadBytes(GDAT.MAGIC.Length)); |
| 17 | + bool correctFormat = GDAT.MAGIC == MAGIC; |
| 18 | + if (!correctFormat) |
| 19 | + { |
| 20 | + throw new InvalidDataException(); |
| 21 | + } |
| 22 | + |
| 23 | + GDatData.FileCount = reader.ReadUInt32(); |
| 24 | + Console.WriteLine("There are " + GDatData.FileCount + " files in the GDAT !"); |
| 25 | + GDatData.Files = new GDATFile[GDatData.FileCount]; |
| 26 | + for (int i = 0; i < GDatData.FileCount; i++) |
| 27 | + { |
| 28 | + GDatData.Files[i] = new GDATFile(reader.ReadUInt32(), reader.ReadUInt32()); |
| 29 | + var file = GDatData.Files[i]; |
| 30 | + Console.WriteLine("File found at the address : 0x" + file.Offset.ToString("X") + " => Length : " + ((float)file.Length / 1024 / 1024).ToString("n4") + " Mb"); |
| 31 | + } |
| 32 | + } |
| 33 | + return GDatData; |
| 34 | + } |
| 35 | + public static string ReverseString(string myStr) |
| 36 | + { |
| 37 | + char[] myArr = myStr.ToCharArray(); |
| 38 | + Array.Reverse(myArr); |
| 39 | + return new string(myArr); |
| 40 | + } |
| 41 | + public void Export(int id, string path) |
| 42 | + { |
| 43 | + GDATFile data = Files[id]; |
| 44 | + Data.Seek(data.Offset, SeekOrigin.Begin); |
| 45 | + using (BinaryReader reader = new BinaryReader(Data, Encoding.UTF8, true)) |
| 46 | + { |
| 47 | + byte[] bytes = reader.ReadBytes(4); |
| 48 | + string fileFormat = Encoding.UTF8.GetString(bytes); |
| 49 | + reader.BaseStream.Seek(data.Offset, SeekOrigin.Begin); |
| 50 | + using (FileStream fs = File.Create(path + "." + ReverseString(fileFormat).Replace('\0', '.'))) |
| 51 | + { |
| 52 | + using (BinaryWriter writer = new BinaryWriter(fs)) |
| 53 | + { |
| 54 | + reader.BaseStream.Seek(data.Offset, SeekOrigin.Begin); |
| 55 | + writer.BaseStream.Seek(0, SeekOrigin.Begin); |
| 56 | + while (reader.BaseStream.Position < data.Offset + 0x80) |
| 57 | + { |
| 58 | + // writer.Write(reader.ReadByte()); |
| 59 | + reader.ReadByte(); |
| 60 | + } |
| 61 | + // Array.Reverse(bytes); |
| 62 | + // writer.Write(bytes); |
| 63 | + ZLIB.Load(Data, writer.BaseStream); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public void Dispose() |
| 70 | + { |
| 71 | + Data.Dispose(); |
| 72 | + } |
| 73 | + |
| 74 | + public const string MAGIC = "GDAT"; |
| 75 | + public uint FileCount; |
| 76 | + public GDATFile[] Files; |
| 77 | + public Stream Data; |
| 78 | + } |
| 79 | +} |
0 commit comments