private void ReadStream(Stream stream) { using (var reader = new ImprovedBinaryReader(stream)) { // header header = reader.ReadStruct <Header>(); // table 0 reader.BaseStream.Position = header.T0 << 2; using (var table0 = new ImprovedBinaryReader(new MemoryStream(Level5.Decompress(reader.BaseStream)))) { while (table0.BaseStream.Position < table0.BaseStream.Length) { t0_list.Add(table0.ReadStruct <T0Entry>()); } } // table 1 reader.BaseStream.Position = header.T1 << 2; using (var table1 = new ImprovedBinaryReader(new MemoryStream(Level5.Decompress(reader.BaseStream)))) { while (table1.BaseStream.Position < table1.BaseStream.Length) { t1_list.Add(table1.ReadStruct <T1Entry>()); } } // table 2 reader.BaseStream.Position = header.T2 << 2; using (var table2 = new ImprovedBinaryReader(new MemoryStream(Level5.Decompress(reader.BaseStream)))) { while (table2.BaseStream.Position < table2.BaseStream.Length) { t2_list.Add(table2.ReadStruct <T2Entry>()); } } // table 3 reader.BaseStream.Position = header.T3 << 2; using (var table3 = new ImprovedBinaryReader(new MemoryStream(Level5.Decompress(reader.BaseStream)))) { while (table3.BaseStream.Position < table3.BaseStream.Length) { t3_list.Add(table3.ReadStruct <T3Entry>()); } } // table 4 reader.BaseStream.Position = header.T4 << 2; using (var table4 = new ImprovedBinaryReader(new MemoryStream(Level5.Decompress(reader.BaseStream)))) { while (table4.BaseStream.Position < table4.BaseStream.Length) { t4_data.Add(table4.ReadByte()); } } } }
private string GetStringSJIS(uint offset) { using (var text = new ImprovedBinaryReader(new MemoryStream(t4_data.ToArray()))) { text.BaseStream.Position = offset; var str = text.ReadCStringSJIS(); return(str); } }
//Huffman 4bit/8bit public static byte[] Decompress(Stream input, int num_bits, long decompressedLength) { using (var br = new ImprovedBinaryReader(input, true)) { var result = new List <byte>(); var tree_size = br.ReadByte(); var tree_root = br.ReadByte(); var tree_buffer = br.ReadBytes(tree_size * 2); for (int i = 0, code = 0, next = 0, pos = tree_root;; i++) { if (i % 32 == 0) { code = br.ReadInt32(); } next += (pos & 0x3F) * 2 + 2; int direction = (code >> (31 - i)) % 2 == 0 ? 2 : 1; var leaf = (pos >> 5 >> direction) % 2 != 0; pos = tree_buffer[next - direction]; if (leaf) { result.Add((byte)pos); pos = tree_root; next = 0; } if (result.Count == decompressedLength * 8 / num_bits) { if (num_bits == 8) { return(result.ToArray()); } else { return(Enumerable.Range(0, (int)decompressedLength) .Select(j => (byte)(result[2 * j + 1] * 16 + result[2 * j])).ToArray()); } } } } }
public List <string> dumpStrings() { var strings = new List <string>(); using (var reader = new ImprovedBinaryReader(new MemoryStream(t4_data.ToArray()))) { while (reader.BaseStream.Position < reader.BaseStream.Length) { using (var text = new ImprovedBinaryReader(reader.BaseStream, true)) { text.BaseStream.Position = reader.BaseStream.Position; var str = text.ReadCStringSJIS(); strings.Add(str); Console.Out.WriteLine(reader.BaseStream.Position); reader.BaseStream.Position = text.BaseStream.Position; } } } return(strings); }
public static byte[] Decompress(Stream instream, long decompressedLength) { using (var br = new ImprovedBinaryReader(instream, true)) { var result = new List <byte>(); while (true) { var flag = br.ReadByte(); result.AddRange(flag >= 128 ? Enumerable.Repeat(br.ReadByte(), flag - 128 + 3) : br.ReadBytes(flag + 1)); if (result.Count == decompressedLength) { return(result.ToArray()); } else if (result.Count > decompressedLength) { throw new InvalidDataException("Went past the end of the stream"); } } } }
public List <string> GetDebugData() { var debug = new List <string>(); var debug_2 = new List <string>(); var debug_3 = new List <string>(); debug.Add("T0"); foreach (var item in t0_list) { debug.Add( $"nameOffset: {item.nameOffset:X8}, nameCRC32: {item.nameCRC32:X8}, T1From: {item.T1From:X4}, T1Count: {item.T1Count:X4}, T2From: {item.T2From:X4}, T2To: {item.T2To:X4}, Unk5: {item.Unk5:X8}, Unk6: {item.Unk6:X8}"); } debug.Add("T1"); foreach (var item in t1_list) { debug.Add( $"nameOffset: {item.nameOffset:X8}, nameCRC32: {item.nameCRC32:X8}, T2Entry: {item.T2EntryId:X8}"); } debug.Add("T2"); var cur = 0; foreach (var item in t2_list) { debug.Add( $"ID: {cur++:X}, T3Off: {item.T3EntryId:X}, ArgCount: {item.T3ArgCount:X}, Unk1: {item.Unk1:X}, FuncId: {item.FuncId:X}, Unk3: {item.Unk3:X}"); } debug.Add("T3"); cur = 0; foreach (var item in t3_list) { debug.Add( $"ID: {cur++:X}, Cmd: {item.Cmd:X}, Val: {item.Value:X}"); } using (var reader = new ImprovedBinaryReader(new MemoryStream(t4_data.ToArray()))) { // print names in table 0 debug.Add("T0 Names"); foreach (var entry in t0_list) { using (var text = new ImprovedBinaryReader(reader.BaseStream, true)) { text.BaseStream.Position = entry.nameOffset; var str = text.ReadCStringSJIS(); debug.Add("Name: " + str); } } // print names in table 1 debug.Add("T1 Names"); foreach (var entry in t1_list) { using (var text = new ImprovedBinaryReader(reader.BaseStream, true)) { text.BaseStream.Position = entry.nameOffset; var str = text.ReadCStringSJIS(); debug.Add("Name: " + str); } } // parse commands in table 2 debug.Add("Commands"); var cnt = 0; foreach (var t2Entry in t2_list) { //if (t2Entry.FuncId != 0x1B59) // continue; debug.Add($"[{cnt++}] New Command: {t2Entry.FuncId:X}"); for (var i = 0; i < t2Entry.T3ArgCount; ++i) { var cmdArgEntry = t3_list[t2Entry.T3EntryId + i]; if (t2Entry.FuncId == 0x14 && i == 0) { string opcode; opcode = XqOpcodes.OpCodes.TryGetValue((uint)cmdArgEntry.Value, out opcode) ? opcode : "N/A"; /* * if (opcode != "Event3DCharaInit" && opcode != "EvenCrtBuildChara" && * opcode != "Event3DCharaPlace_Crt") * break; * else * { * var chara = t3_list[t2Entry.T3EntryId + 1]; * using (var text = new ImprovedBinaryReader(reader.BaseStream, true)) * { * text.BaseStream.Position = chara.Value; * var str = text.ReadCStringSJIS(); * * debug_2.Add($"opcode: {opcode}, string: {str}"); * } * }*/// todo comment was here debug.Add($"ArgCmd: {cmdArgEntry.Cmd:X}, ArgValue: {cmdArgEntry.Value:X} [{opcode}]"); } else if (cmdArgEntry.Cmd == 0x18) { using (var text = new ImprovedBinaryReader(reader.BaseStream, true)) { text.BaseStream.Position = cmdArgEntry.Value; var str = text.ReadCStringSJIS(); debug.Add( $"ArgCmd: {cmdArgEntry.Cmd:X}, StrOffset: {cmdArgEntry.Value:X}, ArgString: {str}"); } } else { debug.Add($"ArgCmd: {cmdArgEntry.Cmd:X}, ArgValue: {cmdArgEntry.Value:X}"); } } } } // print emotes var curStr = ""; foreach (var t2Entry in t2_list) { if (t2Entry.FuncId != 0x1B59 && t2Entry.FuncId != 0x14) { continue; } for (var i = 0; i < t2Entry.T3ArgCount; ++i) { var cmdArgEntry = t3_list[t2Entry.T3EntryId + i]; if (t2Entry.FuncId == 0x14) { if (i == 0) { { string opcode; opcode = XqOpcodes.OpCodes.TryGetValue((uint)cmdArgEntry.Value, out opcode) ? opcode : "N/A"; if (opcode != "EventSetMtnByMtnSet") { break; } } } else if (i == 1) { debug_3.Add($"{GetStringSJIS(cmdArgEntry.Value)}"); } else if (i == 2) { debug_3.Add($"{GetStringSJIS(cmdArgEntry.Value)}"); } } else if (t2Entry.FuncId == 0x1B59) { if (i % 2 == 0) { curStr = GetStringSJIS(cmdArgEntry.Value); } else if (i % 2 == 1) { if (cmdArgEntry.Cmd != 0x18) { continue; } debug_3.Add($"{curStr}"); debug_3.Add($"{GetStringSJIS(cmdArgEntry.Value)}"); } } } } /* * // print bytes in last table * var hex = new StringBuilder(); * foreach (var b in t4_data) hex.AppendFormat("{0:X2} ", b); * debug.Add(hex.ToString()); */ return(debug); }