private void Read(StreamPart streamFile, string name) { GetType(streamFile); int index = 0; streamFile.Stream.Position = streamFile.Position; using (BinaryReader reader = IOTools.OpenReadFile(streamFile.Stream, IsLittleEndian)) do { int Size = reader.ReadInt32(); if (streamFile.Position + streamFile.Size < Size + streamFile.Stream.Position) { throw new Exception("TBL error"); } byte[] tempdata = reader.ReadBytes(Size); FormatEnum fileType = GameFormatHelper.GetFormat(tempdata); string ext = Path.GetExtension(name); string tempName = name.Substring(0, name.Length - ext.Length) + "(" + index++.ToString().PadLeft(2, '0') + ")"; if (fileType == FormatEnum.Unknown) { tempName += ".DAT"; } else { tempName += "." + fileType.ToString(); } SubFiles.Add(GameFormatHelper.OpenFile(tempName, tempdata, fileType == FormatEnum.Unknown ? FormatEnum.DAT : fileType)); reader.BaseStream.Position += IOTools.Alignment(reader.BaseStream.Position - streamFile.Position, 16); } while (streamFile.Stream.Position < streamFile.Position + streamFile.Size); }
private void OpenNew(byte[] data) { using (BinaryReader reader = IOTools.OpenReadFile(new MemoryStream(data), IsLittleEndian)) { int count = reader.ReadInt32(); if (count == 0) { throw new Exception("BIN: count is zero"); } for (int i = 0; i < count; i++) { string Name = Encoding.ASCII.GetString(reader.ReadBytes(0x20)).Trim('\0'); int Size = reader.ReadInt32(); byte[] Data = reader.ReadBytes(Size); GameFile objectFile = GameFormatHelper.OpenFile(Name, Data, GameFormatHelper.GetFormat(Name)); if (objectFile == null) { objectFile = GameFormatHelper.OpenFile(Name, Data, FormatEnum.DAT); } SubFiles.Add(objectFile); } if (reader.BaseStream.Position != reader.BaseStream.Length) { throw new System.Exception("BIN: read error"); } } }
private void OpenOld(byte[] data) { IsLittleEndian = true; if (data.Length < 0x100) { throw new System.Exception("BIN: data length unacceptable"); } using (BinaryReader reader = IOTools.OpenReadFile(new MemoryStream(data), IsLittleEndian, false)) while (reader.BaseStream.Position < reader.BaseStream.Length - 0x100) { string Name = Encoding.ASCII.GetString(reader.ReadBytes(0x100 - 4)).Trim('\0'); int Size = reader.ReadInt32(); byte[] Data = reader.ReadBytes(Size); reader.BaseStream.Position += IOTools.Alignment(reader.BaseStream.Position, 0x40); GameFile objectFile = GameFormatHelper.OpenFile(Name, Data, GameFormatHelper.GetFormat(Name)); if (objectFile == null) { objectFile = GameFormatHelper.OpenFile(Name, Data, FormatEnum.DAT); } SubFiles.Add(objectFile); } }
private void ReadNamed(Dictionary <int, byte[][]> data) { string[] fileNameList = new string[0]; int fileNameIndex = 0; void ReadSingleFile(TypeMap typeMap) { if (data[(int)typeMap].Length > 1) { throw new Exception($"PM1 Read: {typeMap.ToString()}'s count more than 1"); } string name = fileNameList[fileNameIndex++]; var singleFile = GameFormatHelper.OpenFile(name, data[(int)typeMap][0], GameFormatHelper.GetFormat(name)); if (singleFile == null) { singleFile = GameFormatHelper.OpenFile(name, data[(int)typeMap][0], FormatEnum.DAT); } singleFile.Tag = new object[] { (int)typeMap }; SubFiles.Add(singleFile); } if (data.ContainsKey((int)TypeMap.FileList)) { fileNameList = data[(int)TypeMap.FileList].Select(x => Encoding.ASCII.GetString(x).TrimEnd('\0')).ToArray(); } else { foreach (var a in MainFileList) { if (data.ContainsKey(a)) { throw new Exception("PM1 Read: file contains named files"); } } } // Read T3 if (data.ContainsKey((int)TypeMap.T3)) { ReadSingleFile(TypeMap.T3); } // Read RMD if (data.ContainsKey((int)TypeMap.RMDHead)) { if (data.ContainsKey((int)TypeMap.RMD)) { if (data[(int)TypeMap.RMD].Length > 1) { throw new Exception("PM1 Read: RMD's count more than 1"); } using (BinaryReader RMDreader = IOTools.OpenReadFile(data[(int)TypeMap.RMD][0], IsLittleEndian)) { var rmdHeaders = data[(int)TypeMap.RMDHead] .Select(x => { using (BinaryReader BR = IOTools.OpenReadFile(x, IsLittleEndian)) return(BR.ReadInt32Array(8)); }) .ToArray(); for (int i = 0; i < rmdHeaders.Length; i++) { RMDreader.BaseStream.Position = rmdHeaders[i][4] - rmdHeaders[0][4]; var rmd = GameFormatHelper.OpenFile(fileNameList[fileNameIndex++], RMDreader.ReadBytes(rmdHeaders[i][5]), FormatEnum.DAT); rmd.Tag = new object[] { (int)TypeMap.RMD, rmdHeaders[i] }; SubFiles.Add(rmd); } } } else { throw new Exception("PM1 Read: file contain RMD Header, but not RMD"); } } // Read BMD if (data.ContainsKey((int)TypeMap.BMD)) { ReadSingleFile(TypeMap.BMD); } // Read EPL if (data.ContainsKey((int)TypeMap.EPLHead)) { if (data.ContainsKey((int)TypeMap.EPL)) { if (data[(int)TypeMap.EPL].Length > 1) { throw new Exception("PM1 Read: EPL's count more than 1"); } var eplHeaders = data[(int)TypeMap.EPLHead] .Select(x => { using (BinaryReader BR = IOTools.OpenReadFile(x, IsLittleEndian)) return(BR.ReadInt32Array(4)); }) .ToArray(); var eplList = data[(int)TypeMap.EPL][0].Split(eplHeaders.Select(x => x[1] - eplHeaders[0][1]).ToArray()).ToArray(); for (int i = 0; i < eplList.Length; i++) { var epl = GameFormatHelper.OpenFile(fileNameList[fileNameIndex++], eplList[i], FormatEnum.DAT); epl.Tag = new object[] { (int)TypeMap.EPL, eplHeaders[i] }; SubFiles.Add(epl); } } else { throw new Exception("PM1 Read: file contain EPL Header, but not EPL"); } } // Read TMX if (data.ContainsKey((int)TypeMap.TMXHead)) { if (data.ContainsKey((int)TypeMap.TMX)) { if (data[(int)TypeMap.TMX].Length > 1) { throw new Exception("PM1 Read: TMX's count more than 1"); } var tmxHeaders = data[(int)TypeMap.TMXHead] .Select(x => { using (BinaryReader BR = IOTools.OpenReadFile(x, IsLittleEndian)) return(BR.ReadInt32Array(4)); }) .ToArray(); var tmxList = data[(int)TypeMap.TMX][0].Split(tmxHeaders.Select(x => x[1] - tmxHeaders[0][1]).ToArray()).ToArray(); for (int i = 0; i < tmxList.Length; i++) { var name = fileNameList[fileNameIndex++]; var tmx = GameFormatHelper.OpenFile(name, tmxList[i], GameFormatHelper.GetFormat(name)); tmx.Tag = new object[] { (int)TypeMap.TMX, tmxHeaders[i] }; SubFiles.Add(tmx); } } else { throw new Exception("PM1 Read: file contain TMX Header, but not TMX"); } } // Read CTable if (data.ContainsKey((int)TypeMap.CTable)) { ReadSingleFile(TypeMap.CTable); } if (fileNameIndex != fileNameList.Length) { throw new Exception("PM1 Read: not all files are read"); } }