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 ReadUnnamed(Dictionary <int, byte[][]> data) { int index = 0; foreach (var el in data) { if (!MainFileList.Contains(el.Key)) { foreach (var a in el.Value) { //throw new Exception("PM1: Unknown"); string name = $"Noname({index.ToString().PadLeft(2, '0')}).DAT"; while (SubFiles.Exists(x => x.Name == name)) { index++; name = $"Noname({index.ToString().PadLeft(2, '0')}).DAT"; } var temp = GameFormatHelper.OpenFile(name, a, FormatEnum.DAT); temp.Tag = new object[] { el.Key }; SubFiles.Add(temp); } } } }
public void OpenFile(string path) { if (Tab.CloseAll()) { FileInfo fileInfo = new FileInfo(path); if (fileInfo.Length > 1000000000) { return; } //FileStream = new FileStream(path, FileMode.Open, FileAccess.Read); //var file = PersonaEditorLib.Utilities.PersonaFile.OpenFile(Path.GetFileName(path), // PersonaEditorLib.Utilities.PersonaFile.GetFileType(Path.GetFileName(path)), // new StreamFile(FileStream, FileStream.Length, 0)); var file = GameFormatHelper.OpenFile(Path.GetFileName(path), File.ReadAllBytes(path)); if (file != null) { Tree.SetRoot(file); Static.OpenedFile = Path.GetFullPath(path); } } }
public async void OpFile(string path) { await Task.Run(() => { BackgroundWorker.Control.Dispatcher.Invoke(() => { if (Tab.CloseAll()) { FileInfo fileInfo = new FileInfo(path); if (fileInfo.Length > 1000000000) { return; } //FileStream = new FileStream(path, FileMode.Open, FileAccess.Read); //var file = PersonaEditorLib.Utilities.PersonaFile.OpenFile(Path.GetFileName(path), // PersonaEditorLib.Utilities.PersonaFile.GetFileType(Path.GetFileName(path)), // new StreamFile(FileStream, FileStream.Length, 0)); var file = GameFormatHelper.OpenFile(Path.GetFileName(path), File.ReadAllBytes(path)); if (file != null) { Tree.SetRoot(file); Static.OpenedFile = Path.GetFullPath(path); } } }); }); }
static void ImportAll(GameFile objectFile, string openedFileDir) { foreach (var item in objectFile.GameData.SubFiles) { string newpath = Path.Combine(openedFileDir, item.Name.Replace('/', '+')); FormatEnum fileType = item.GameData.Type; if (File.Exists(newpath)) { var file = GameFormatHelper.OpenFile(objectFile.Name, File.ReadAllBytes(newpath), fileType); if (file != null) { item.GameData = file.GameData; } } } }
private void Open(byte[] data) { using (BinaryReader reader = IOTools.OpenReadFile(new MemoryStream(data), IsLittleEndian)) { List <int[]> Entry = new List <int[]>(); do { Entry.Add(reader.ReadInt32Array(3)); } while (Entry[Entry.Count - 1][1] != 0); for (int i = 0; i < Entry.Count - 1; i++) { FlagList.Add(Entry[i][0]); reader.BaseStream.Position = Entry[i][1]; string name = Path.GetFileNameWithoutExtension(Name) + "(" + i.ToString().PadLeft(3, '0') + ").BMD"; SubFiles.Add(GameFormatHelper.OpenFile(name, reader.ReadBytes(Entry[i][2]), FormatEnum.BMD)); } } }
private void ReadTexture(StreamPart streamFile, int pos, int count) { streamFile.Stream.Position = streamFile.Position + pos; using (BinaryReader reader = new BinaryReader(streamFile.Stream, Encoding.ASCII, true)) for (int i = 0; i < count; i++) { int tag = reader.ReadInt32(); streamFile.Stream.Position += 4; long texPos = reader.ReadUInt32(); int texSize = reader.ReadInt32(); streamFile.Stream.Position += 16; string name = Encoding.ASCII.GetString(reader.ReadBytes(16)).TrimEnd('\0'); long tempPos = streamFile.Stream.Position; streamFile.Stream.Position = texPos; var text = GameFormatHelper.OpenFile(name + ".dds", reader.ReadBytes(texSize), FormatEnum.DDS); text.Tag = tag; SubFiles.Add(text); streamFile.Stream.Position = tempPos; } }
static void Test(string[] args) { string testPath = @"d:\Persona 5\DATA_PS3_JAP\"; var par = new Parameters(new string[][] { new string[] { "/sub" } }); var files = Directory.EnumerateFiles(testPath, "*.*", SearchOption.AllDirectories).ToArray(); int index = 0; foreach (var filePath in files) { Console.Write($"{index++}/{files.Length}\r"); var OpenedFileDir = Path.GetDirectoryName(filePath); if (new FileInfo(filePath).Length > 10000000) { continue; } GameFile file = GameFormatHelper.OpenFile(Path.GetFileName(filePath), File.ReadAllBytes(filePath)); if (file != null) { SubFileAction((a, b, c, d) => { try { if (a.GameData is BMD bmd) { PTP ptp = new PTP(bmd); var newName = a.Name.Replace('/', '+'); string path = Path.Combine(c, Path.GetFileNameWithoutExtension(newName) + ".TXT"); var exp = ptp.ExportTXT(true, Static.OldEncoding()); File.WriteAllLines(path, exp); } } catch { } }, file, "", OpenedFileDir, par); } } }
private void DropItem(object sender, DragEventArgs e) { if (PersonaFile is IGameData pFile) { string[] temp = e.Data.GetData(DataFormats.FileDrop) as string[]; if (temp.Length > 0) { if (MessageBox.Show("Replace " + PersonaFile.Name + "?", "Replace?", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes) { var personaFile = GameFormatHelper.OpenFile(PersonaFile.Name, File.ReadAllBytes(temp[0]), pFile.Type); if (personaFile != null) { _personaFile.GameData = personaFile.GameData; } else { MessageBox.Show("Error. " + Path.GetFileName(temp[0]) + " is not a " + pFile.Type + " type"); } } } } }
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"); } }
static void DoSome(string[] args) { ArgumentsWork argwrk = new ArgumentsWork(args); GameFile file = GameFormatHelper.OpenFile(Path.GetFileName(argwrk.OpenedFile), File.ReadAllBytes(argwrk.OpenedFile)); foreach (var command in argwrk.ArgumentList) { Action <GameFile, string, string, Parameters> action = null; if (command.Command == CommandType.Export) { if (command.Type == CommandSubType.Image) { action = ExportImage; } else if (command.Type == CommandSubType.Table) { action = ExportTable; } else if (command.Type == CommandSubType.All) { ExportAll(file, argwrk.OpenedFileDir); } else if (command.Type == CommandSubType.PTP) { action = ExportPTP; } else if (command.Type == CommandSubType.Text) { action = ExportText; } else { action = ExportByType; } } else if (command.Command == CommandType.Import) { if (command.Type == CommandSubType.Image) { action = ImportImage; } else if (command.Type == CommandSubType.Table) { action = ImportTable; } else if (command.Type == CommandSubType.All) { ImportAll(file, argwrk.OpenedFileDir); } else if (command.Type == CommandSubType.PTP) { action = ImportPTP; } else if (command.Type == CommandSubType.Text) { action = ImportText; } } else if (command.Command == CommandType.Save) { SaveFile(file, command.Value, argwrk.OpenedFileDir, command.Parameters); } if (action != null) { SubFileAction(action, file, command.Value, argwrk.OpenedFileDir, command.Parameters); } } }
private void Open(Stream stream) { stream.Position = 0x10; if (stream.ReadByte() == 0) { IsLittleEndian = false; } else { IsLittleEndian = true; } BinaryReader reader = IOTools.OpenReadFile(stream, IsLittleEndian); stream.Position = 0x4; int fileSize = reader.ReadInt32(); stream.Position = 0x10; int tablecount = reader.ReadInt32(); Unknown = reader.ReadBytes(12); stream.Position = 0x20; Table = reader.ReadInt32ArrayArray(tablecount, 4); Sizes = new int[Table.Length]; for (int i = 0; i < Table.Length; i++) { Sizes[i] = Table[i][1]; } foreach (var element in Table) { if (element[1] * element[2] > 0) { reader.BaseStream.Position = element[3]; string tempN; FormatEnum type = FormatEnum.DAT; if (MAP.ContainsKey(element[0])) { type = MAP[element[0]]; } tempN = "." + type.ToString(); if (fileSize == element[3] + element[1] * element[2]) { endIndex = element[0]; } byte[] data = reader.ReadBytes(element[1] * element[2]); var item = GameFormatHelper.OpenFile(tempN, data, type); if (item == null) { item = GameFormatHelper.OpenFile(tempN, data, FormatEnum.DAT); } item.Tag = element[0]; SubFiles.Add(item); } } if (endIndex == -1) { throw new Exception("BF: endIndex"); } }
private void ContextMenu_Replace() { FormatEnum fileType = PersonaFile.GameData.Type; OpenFileDialog OFD = new OpenFileDialog(); string name = PersonaFile.Name.Replace('/', '+'); OFD.Filter = $"RAW(*{Path.GetExtension(name)})|*{Path.GetExtension(name)}"; OFD.CheckFileExists = true; OFD.CheckPathExists = true; OFD.Multiselect = false; if (PersonaFile.GameData is IImage) { OFD.Filter += $"|PNG (*.png)|*.png"; } if (PersonaFile.GameData is ITable) { OFD.Filter += $"|XML data table (*.xml)|*.xml"; } if (PersonaFile.GameData is BMD) { OFD.Filter += $"|Persona Text Project (*.ptp)|*.ptp"; } if (OFD.ShowDialog() == true) { if (OFD.FilterIndex == 1) { var item = GameFormatHelper.OpenFile(PersonaFile.Name, File.ReadAllBytes(OFD.FileName), fileType); if (item != null) { PersonaFile.GameData = item.GameData; } } else { string ext = Path.GetExtension(OFD.FileName); if (ext.Equals(".png", StringComparison.CurrentCultureIgnoreCase)) { PersonaEditorTools.OpenImageFile(PersonaFile, OFD.FileName); } else if (ext.Equals(".xml", StringComparison.CurrentCultureIgnoreCase)) { PersonaEditorTools.OpenTableFile(PersonaFile, OFD.FileName); } else if (ext.Equals(".ptp", StringComparison.CurrentCultureIgnoreCase)) { var result = ToolBox.Show(ToolBoxType.OpenPTP); if (result == ToolBoxResult.Ok) { PersonaEditorTools.OpenPTPFile(PersonaFile, OFD.FileName, Static.EncodingManager.GetPersonaEncoding(ApplicationSettings.AppSetting.Default.OpenPTP_Font)); } } else { throw new Exception("OpenPersonaFileDialog"); } } Update(_personaFile); if (_isSelected) { ItemAction?.Invoke(this, UserTreeViewItemEventEnum.Selected); } } }