public static void Unpack(this BXF4 bxf, string bhdName, string bdtName, string targetDir) { Directory.CreateDirectory(targetDir); XmlWriterSettings xws = new XmlWriterSettings(); xws.Indent = true; XmlWriter xw = XmlWriter.Create($"{targetDir}\\_yabber-bxf4.xml", xws); xw.WriteStartElement("bxf4"); xw.WriteElementString("bhd_filename", bhdName); xw.WriteElementString("bdt_filename", bdtName); xw.WriteElementString("version", bxf.Version); xw.WriteElementString("format", bxf.Format.ToString()); xw.WriteElementString("bigendian", bxf.BigEndian.ToString()); xw.WriteElementString("bitbigendian", bxf.BitBigEndian.ToString()); xw.WriteElementString("unicode", bxf.Unicode.ToString()); xw.WriteElementString("extended", $"0x{bxf.Extended:X2}"); xw.WriteElementString("unk04", bxf.Unk04.ToString()); xw.WriteElementString("unk05", bxf.Unk05.ToString()); YBinder.WriteBinderFiles(bxf, xw, targetDir); xw.WriteEndElement(); xw.Close(); }
public static void Repack(string sourceDir, string targetDir) { BXF4 bxf = new BXF4(); XmlDocument xml = new XmlDocument(); xml.Load($"{sourceDir}\\_yabber-bxf4.xml"); string bhdFilename = xml.SelectSingleNode("bxf4/bhd_filename").InnerText; string bdtFilename = xml.SelectSingleNode("bxf4/bdt_filename").InnerText; bxf.Version = xml.SelectSingleNode("bxf4/version").InnerText; bxf.Format = (Binder.Format)Enum.Parse(typeof(Binder.Format), xml.SelectSingleNode("bxf4/format").InnerText); bxf.BigEndian = bool.Parse(xml.SelectSingleNode("bxf4/bigendian").InnerText); bxf.BitBigEndian = bool.Parse(xml.SelectSingleNode("bxf4/bitbigendian").InnerText); bxf.Unicode = bool.Parse(xml.SelectSingleNode("bxf4/unicode").InnerText); bxf.Extended = Convert.ToByte(xml.SelectSingleNode("bxf4/extended").InnerText, 16); bxf.Unk04 = bool.Parse(xml.SelectSingleNode("bxf4/unk04").InnerText); bxf.Unk05 = bool.Parse(xml.SelectSingleNode("bxf4/unk05").InnerText); YBinder.ReadBinderFiles(bxf, xml.SelectSingleNode("bxf4/files"), sourceDir); string bhdPath = $"{targetDir}\\{bhdFilename}"; YBUtil.Backup(bhdPath); string bdtPath = $"{targetDir}\\{bdtFilename}"; YBUtil.Backup(bdtPath); bxf.Write(bhdPath, bdtPath); }
public static void AddSpecificTexturesFromBinder(string name, List <string> textures, bool directEntryNameMatch = false) { if (!File.Exists(name)) { return; } IBinder bnd = null; if (BXF4.IsBHD(name)) { bnd = BXF4.Read(name, name.Substring(0, name.Length - 7) + ".tpfbdt"); } else if (BXF4.IsBDT(name)) { bnd = BXF4.Read(name.Substring(0, name.Length - 7) + ".tpfbhd", name); } else if (BXF3.IsBHD(name)) { bnd = BXF3.Read(name, name.Substring(0, name.Length - 7) + ".tpfbdt"); } else if (BXF3.IsBDT(name)) { bnd = BXF3.Read(name.Substring(0, name.Length - 7) + ".tpfbhd", name); } else if (BND4.Is(name)) { bnd = BND4.Read(name); } else if (BND3.Is(name)) { bnd = BND3.Read(name); } foreach (var f in bnd.Files) { if (directEntryNameMatch ? textures.Contains(Utils.GetShortIngameFileName(f.Name).ToLower()) : TPF.Is(f.Bytes)) { var tpf = TPF.Read(f.Bytes); foreach (var tx in tpf.Textures) { var shortTexName = Utils.GetShortIngameFileName(tx.Name).ToLower(); if (textures.Contains(shortTexName)) { AddFetchTPF(tpf, tx.Name.ToLower()); textures.Remove(shortTexName); } } } } }
public static void Repack(string sourceDir, string targetDir) { BXF4 bxf = new BXF4(); XmlDocument xml = new XmlDocument(); xml.Load($"{sourceDir}\\_yabber-bxf4.xml"); string bhdFilename = xml.SelectSingleNode("bxf4/bhd/filename").InnerText; bxf.BHD.Timestamp = xml.SelectSingleNode("bxf4/bhd/timestamp").InnerText; bxf.BHD.Format = (Binder.Format)Convert.ToByte(xml.SelectSingleNode("bxf4/bhd/format").InnerText, 16); bxf.BHD.Unicode = bool.Parse(xml.SelectSingleNode("bxf4/bhd/unicode").InnerText); bxf.BHD.BigEndian = bool.Parse(xml.SelectSingleNode("bxf4/bhd/bigendian").InnerText); bxf.BHD.Flag1 = bool.Parse(xml.SelectSingleNode("bxf4/bhd/flag1").InnerText); bxf.BHD.Flag2 = bool.Parse(xml.SelectSingleNode("bxf4/bhd/flag2").InnerText); bxf.BHD.Extended = Convert.ToByte(xml.SelectSingleNode("bxf4/bhd/extended").InnerText, 16); string bdtFilename = xml.SelectSingleNode("bxf4/bdt/filename").InnerText; bxf.BDT.Timestamp = xml.SelectSingleNode("bxf4/bdt/timestamp").InnerText; bxf.BDT.BigEndian = bool.Parse(xml.SelectSingleNode("bxf4/bdt/bigendian").InnerText); bxf.BDT.Flag1 = bool.Parse(xml.SelectSingleNode("bxf4/bdt/flag1").InnerText); bxf.BDT.Flag2 = bool.Parse(xml.SelectSingleNode("bxf4/bdt/flag2").InnerText); bxf.BDT.Unk1 = Convert.ToInt64(xml.SelectSingleNode("bxf4/bdt/unk1").InnerText, 16); foreach (XmlNode fileNode in xml.SelectNodes("bxf4/files/file")) { int id = int.Parse(fileNode.SelectSingleNode("id").InnerText); string root = fileNode.SelectSingleNode("root").InnerText; string path = fileNode.SelectSingleNode("path").InnerText; byte flags = Convert.ToByte(fileNode.SelectSingleNode("flags").InnerText, 16); byte[] bytes = File.ReadAllBytes($"{sourceDir}\\{path}"); bxf.Files.Add(new BXF4.File(id, root + path, (Binder.FileFlags)flags, bytes)); } string bhdPath = $"{targetDir}\\{bhdFilename}"; if (File.Exists(bhdPath) && !File.Exists(bhdPath + ".bak")) { File.Move(bhdPath, bhdPath + ".bak"); } string bdtPath = $"{targetDir}\\{bdtFilename}"; if (File.Exists(bdtPath) && !File.Exists(bdtPath + ".bak")) { File.Move(bdtPath, bdtPath + ".bak"); } bxf.Write(bhdPath, bdtPath); }
public static void Unpack(this BXF4 bxf, string bhdName, string bdtName, string targetDir) { Directory.CreateDirectory(targetDir); XmlWriterSettings xws = new XmlWriterSettings(); xws.Indent = true; XmlWriter xw = XmlWriter.Create($"{targetDir}\\_yabber-bxf4.xml", xws); xw.WriteStartElement("bxf4"); xw.WriteStartElement("bhd"); xw.WriteElementString("filename", bhdName); xw.WriteElementString("timestamp", bxf.BHD.Timestamp); xw.WriteElementString("format", $"0x{(byte)bxf.BHD.Format:X2}"); xw.WriteElementString("unicode", bxf.BHD.Unicode.ToString()); xw.WriteElementString("bigendian", bxf.BHD.BigEndian.ToString()); xw.WriteElementString("flag1", bxf.BHD.Flag1.ToString()); xw.WriteElementString("flag2", bxf.BHD.Flag2.ToString()); xw.WriteElementString("extended", $"0x{bxf.BHD.Extended:X2}"); xw.WriteEndElement(); xw.WriteStartElement("bdt"); xw.WriteElementString("filename", bdtName); xw.WriteElementString("timestamp", bxf.BDT.Timestamp); xw.WriteElementString("bigendian", bxf.BDT.BigEndian.ToString()); xw.WriteElementString("flag1", bxf.BDT.Flag1.ToString()); xw.WriteElementString("flag2", bxf.BDT.Flag2.ToString()); xw.WriteElementString("unk1", $"0x{bxf.BDT.Unk1:X16}"); xw.WriteEndElement(); xw.WriteStartElement("files"); foreach (BXF4.File file in bxf.Files) { string path = YBUtil.UnrootBNDPath(file.Name, out string root); xw.WriteStartElement("file"); xw.WriteElementString("id", file.ID.ToString()); xw.WriteElementString("root", root); xw.WriteElementString("path", path); xw.WriteElementString("flags", $"0x{(byte)file.Flags:X2}"); xw.WriteEndElement(); path = $"{targetDir}\\{path}"; Directory.CreateDirectory(Path.GetDirectoryName(path)); File.WriteAllBytes(path, file.Bytes); } xw.WriteEndElement(); xw.WriteEndElement(); xw.Close(); }
public static void AddMapTexBXF4(int area, IProgress <double> prog) { var dir = InterrootLoader.GetInterrootPath($"map\\m{area:D2}"); if (!Directory.Exists(dir)) { return; } var mapTpfFileNames = Directory.GetFiles(dir, "*.tpfbhd"); int fileIndex = 0; foreach (var t in mapTpfFileNames) { BXF4 bxf = null; lock (_lock_IO) { bxf = BXF4.Read(t, t.Substring(0, t.Length - 7) + ".tpfbdt"); } for (int i = 0; i < bxf.Files.Count; i++) { if (bxf.Files[i].Name.Contains(".tpf")) { var tpf = SoulsFormats.TPF.Read(bxf.Files[i].Bytes); foreach (var tn in tpf.Textures) { AddFetch(tpf, tn.Name); } tpf = null; } GFX.ModelDrawer.RequestTextureLoad(); // Report each subfile as a tiny part of the bar prog?.Report((1.0 * fileIndex / mapTpfFileNames.Length) + ((1.0 / mapTpfFileNames.Length) * ((i + 1.0) / bxf.Files.Count))); } bxf = null; fileIndex++; prog?.Report((1.0 * fileIndex / mapTpfFileNames.Length)); } GFX.ModelDrawer.RequestTextureLoad(); }
public static void AddMapTexBXF4DS2(string area, IProgress <double> prog) { var dir = InterrootLoader.GetInterrootPath($"model\\map\\"); if (!Directory.Exists(dir)) { return; } BXF4 bxf = null; lock (_lock_IO) { bxf = BXF4.Read(dir + "\\t" + area.Substring(1) + ".tpfbhd", dir + "\\t" + area.Substring(1) + ".tpfbdt"); } for (int i = 0; i < bxf.Files.Count; i++) { if (bxf.Files[i].Name.Contains(".tpf")) { var tpf = TPF.Read(bxf.Files[i].Bytes); foreach (var tn in tpf.Textures) { AddFetch(tpf, tn.Name); } tpf = null; } GFX.ModelDrawer.RequestTextureLoad(); // Report each subfile as a tiny part of the bar prog?.Report((i + 1.0) / bxf.Files.Count); } bxf = null; //fileIndex++; //prog?.Report((1.0 * fileIndex / mapTpfFileNames.Length)); GFX.ModelDrawer.RequestTextureLoad(); }
private int PackVirtualFile(VirtualFile vf, CancellationToken cancelToken) { byte[] bytes; long baseMemory; lock (this) { while (BaseMemoryCommitted > TPUtil.MAX_BASE_MEMORY) { Thread.Sleep(10); } if (cancelToken.IsCancellationRequested) { return(0); } bytes = vf.Load(); baseMemory = bytes.Length; Interlocked.Add(ref BaseMemoryCommitted, baseMemory); } try { string relOutputDir = TPUtil.GetRelativeOutputDir(vf.Path); DCX.Type dcxType = DCX.Type.None; if (DCX.Is(bytes)) { bytes = DCX.Decompress(bytes, out dcxType); } int textureCount; if (TPF.IsRead(bytes, out TPF tpf)) { textureCount = PackTPF(tpf, relOutputDir); if (textureCount > 0) { tpf.Write($@"{OutputDirectory}\{vf.Path}", dcxType); } } else if (BND4.IsRead(bytes, out BND4 bnd)) { textureCount = PackBinder(bnd, relOutputDir, cancelToken); if (textureCount > 0) { bnd.Write($@"{OutputDirectory}\{vf.Path}", dcxType); } } else if (BXF4.IsBDT(bytes)) { string ext = Path.GetExtension(vf.Path).Replace("bdt", "bhd"); string bhdPath = Path.ChangeExtension(vf.Path, ext); VirtualFile vfHeader = VirtualFS.Files[bhdPath]; byte[] bhdBytes = vfHeader.Load(); var bxf = BXF4.Read(bhdBytes, bytes); textureCount = PackBinder(bxf, relOutputDir, cancelToken); if (textureCount > 0) { bxf.Write($@"{OutputDirectory}\{vfHeader.Path}", $@"{OutputDirectory}\{vf.Path}"); } } else { throw new NotSupportedException("Unknown file type."); } return(textureCount); } finally { Interlocked.Add(ref BaseMemoryCommitted, -baseMemory); } }
private int UnpackVirtualFile(VirtualFile vf, CancellationToken cancelToken) { byte[] bytes; long baseMemory; lock (this) { while (BaseMemoryCommitted > TPUtil.MAX_BASE_MEMORY) { Thread.Sleep(10); } if (cancelToken.IsCancellationRequested) { return(0); } bytes = vf.Load(); baseMemory = bytes.Length; Interlocked.Add(ref BaseMemoryCommitted, baseMemory); } try { string relOutputDir = TPUtil.GetRelativeOutputDir(vf.Path); if (DCX.Is(bytes)) { bytes = DCX.Decompress(bytes); } int textureCount; var report = new UnpackReport(); if (TPF.IsRead(bytes, out TPF tpf)) { textureCount = UnpackTPF(tpf, relOutputDir, report); } else if (BND4.IsRead(bytes, out BND4 bnd)) { textureCount = UnpackBinder(bnd, relOutputDir, report, cancelToken); } else if (BXF4.IsBDT(bytes)) { string ext = Path.GetExtension(vf.Path).Replace("bdt", "bhd"); string bhdPath = Path.ChangeExtension(vf.Path, ext); VirtualFile vfHeader = VirtualFS.Files[bhdPath]; byte[] bhdBytes = vfHeader.Load(); var bxf = BXF4.Read(bhdBytes, bytes); textureCount = UnpackBinder(bxf, relOutputDir, report, cancelToken); } else { throw new NotSupportedException("Unknown file type."); } if (report.Files.Count > 0) { File.WriteAllText($@"{Game.Settings.UnpackDirectory.TrimEnd('\\')}\{relOutputDir}\_report.txt", report.Write()); } return(textureCount); } finally { Interlocked.Add(ref BaseMemoryCommitted, -baseMemory); } }
private static bool UnpackFile(string sourceFile) { string sourceDir = Path.GetDirectoryName(sourceFile); string filename = Path.GetFileName(sourceFile); string targetDir = $"{sourceDir}\\{filename.Replace('.', '-')}"; if (File.Exists(targetDir)) { targetDir += "-ybr"; } if (DCX.Is(sourceFile)) { Console.WriteLine($"Decompressing DCX: {filename}..."); byte[] bytes = DCX.Decompress(sourceFile, out DCX.Type compression); if (BND3.Is(bytes)) { Console.WriteLine($"Unpacking BND3: {filename}..."); BND3 bnd = BND3.Read(bytes); bnd.Compression = compression; bnd.Unpack(filename, targetDir); } else if (BND4.Is(bytes)) { Console.WriteLine($"Unpacking BND4: {filename}..."); BND4 bnd = BND4.Read(bytes); bnd.Compression = compression; bnd.Unpack(filename, targetDir); } else if (TPF.Is(bytes)) { Console.WriteLine($"Unpacking TPF: {filename}..."); TPF tpf = TPF.Read(bytes); tpf.Compression = compression; tpf.Unpack(filename, targetDir); } else if (sourceFile.EndsWith(".gparam.dcx")) { Console.WriteLine($"Unpacking GPARAM: {filename}..."); GPARAM gparam = GPARAM.Read(bytes); gparam.Unpack(sourceFile); } else { Console.WriteLine($"File format not recognized: {filename}"); return(true); } } else { if (BND3.Is(sourceFile)) { Console.WriteLine($"Unpacking BND3: {filename}..."); BND3 bnd = BND3.Read(sourceFile); bnd.Unpack(filename, targetDir); } else if (BND4.Is(sourceFile)) { Console.WriteLine($"Unpacking BND4: {filename}..."); BND4 bnd = BND4.Read(sourceFile); bnd.Unpack(filename, targetDir); } else if (BXF3.IsBHD(sourceFile)) { string bdtExtension = Path.GetExtension(filename).Replace("bhd", "bdt"); string bdtFilename = $"{Path.GetFileNameWithoutExtension(filename)}{bdtExtension}"; string bdtPath = $"{sourceDir}\\{bdtFilename}"; if (File.Exists(bdtPath)) { Console.WriteLine($"Unpacking BXF3: {filename}..."); BXF3 bxf = BXF3.Read(sourceFile, bdtPath); bxf.Unpack(filename, bdtFilename, targetDir); } else { Console.WriteLine($"BDT not found for BHD: {filename}"); return(true); } } else if (BXF4.IsBHD(sourceFile)) { string bdtExtension = Path.GetExtension(filename).Replace("bhd", "bdt"); string bdtFilename = $"{Path.GetFileNameWithoutExtension(filename)}{bdtExtension}"; string bdtPath = $"{sourceDir}\\{bdtFilename}"; if (File.Exists(bdtPath)) { Console.WriteLine($"Unpacking BXF4: {filename}..."); BXF4 bxf = BXF4.Read(sourceFile, bdtPath); bxf.Unpack(filename, bdtFilename, targetDir); } else { Console.WriteLine($"BDT not found for BHD: {filename}"); return(true); } } else if (TPF.Is(sourceFile)) { Console.WriteLine($"Unpacking TPF: {filename}..."); TPF tpf = TPF.Read(sourceFile); tpf.Unpack(filename, targetDir); } else if (sourceFile.EndsWith(".fmg")) { Console.WriteLine($"Unpacking FMG: {filename}..."); FMG fmg = FMG.Read(sourceFile); fmg.Unpack(sourceFile); } else if (sourceFile.EndsWith(".fmg.xml")) { Console.WriteLine($"Repacking FMG: {filename}..."); YFMG.Repack(sourceFile); } else if (sourceFile.EndsWith(".gparam")) { Console.WriteLine($"Unpacking GPARAM: {filename}..."); GPARAM gparam = GPARAM.Read(sourceFile); gparam.Unpack(sourceFile); } else if (sourceFile.EndsWith(".gparam.xml") || sourceFile.EndsWith(".gparam.dcx.xml")) { Console.WriteLine($"Repacking GPARAM: {filename}..."); YGPARAM.Repack(sourceFile); } else if (sourceFile.EndsWith(".luagnl")) { Console.WriteLine($"Unpacking LUAGNL: {filename}..."); LUAGNL gnl = LUAGNL.Read(sourceFile); gnl.Unpack(sourceFile); } else if (sourceFile.EndsWith(".luagnl.xml")) { Console.WriteLine($"Repacking LUAGNL: {filename}..."); YLUAGNL.Repack(sourceFile); } else if (LUAINFO.Is(sourceFile)) { Console.WriteLine($"Unpacking LUAINFO: {filename}..."); LUAINFO info = LUAINFO.Read(sourceFile); info.Unpack(sourceFile); } else if (sourceFile.EndsWith(".luainfo.xml")) { Console.WriteLine($"Repacking LUAINFO: {filename}..."); YLUAINFO.Repack(sourceFile); } else { Console.WriteLine($"File format not recognized: {filename}"); return(true); } } return(false); }