/// <summary> /// Writes the file to an array of bytes, compressing it as specified. /// </summary> public byte[] Write(DCX.Type compression) { BinaryWriterEx bw = new BinaryWriterEx(false); Write(bw, compression); return(bw.FinishBytes()); }
private void Read(BinaryReaderEx br) { br = SFUtil.GetDecompressedBR(br, out DCX.Type compression); Compression = compression; Files = BND3.ReadHeader(this, br); DataBR = br; }
/// <summary> /// Creates a new file. /// </summary> public BinderFile(FileFlags flags, int id, string name, byte[] bytes) { Flags = flags; ID = id; Name = name; Bytes = bytes; CompressionType = DCX.Type.Zlib; }
/// <summary> /// Writes the file to the specified path, compressing it as specified. /// </summary> public void Write(string path, DCX.Type compression) { using (FileStream stream = File.Create(path)) { BinaryWriterEx bw = new BinaryWriterEx(false, stream); Write(bw, compression); bw.Finish(); } }
/// <summary> /// Writes the file to the specified path, compressing it as specified. /// </summary> public void Write(string path, DCX.Type compression) { Directory.CreateDirectory(Path.GetDirectoryName(path)); using (FileStream stream = File.Create(path)) { BinaryWriterEx bw = new BinaryWriterEx(false, stream); Write(bw, compression); bw.Finish(); } }
private BinderFileHeader(FileFlags flags, int id, string name, long compressedSize, long uncompressedSize, long dataOffset) { Flags = flags; ID = id; Name = name; CompressionType = DCX.Type.Zlib; CompressedSize = compressedSize; UncompressedSize = uncompressedSize; DataOffset = dataOffset; }
/// <summary> /// Writes the file to an array of bytes, compressing it as specified. /// </summary> public byte[] Write(DCX.Type compression) { if (!Validate(out Exception ex)) { throw ex; } BinaryWriterEx bw = new BinaryWriterEx(false); Write(bw, compression); return(bw.FinishBytes()); }
private int PackBinder(IBinder binder, string relOutputDir, CancellationToken cancelToken) { int textureCount = 0; foreach (BinderFile file in binder.Files) { if (cancelToken.IsCancellationRequested) { return(textureCount); } if (TPUtil.HasValidExtension(file.Name)) { try { byte[] bytes = file.Bytes; DCX.Type dcxType = DCX.Type.None; if (DCX.Is(bytes)) { bytes = DCX.Decompress(bytes, out dcxType); } if (TPF.IsRead(bytes, out TPF tpf)) { int thisTextureCount = PackTPF(tpf, relOutputDir); if (thisTextureCount > 0) { file.Bytes = tpf.Write(dcxType); } textureCount += thisTextureCount; } else if (BND4.IsRead(bytes, out BND4 bnd)) { int thisTextureCount = PackBinder(bnd, relOutputDir, cancelToken); if (thisTextureCount > 0) { file.Bytes = bnd.Write(dcxType); } textureCount += thisTextureCount; } else { throw new NotSupportedException("Unknown file type."); } } catch (Exception ex) { throw new Exception($"Error in binder file \"{file.Name}\"", ex); } } } return(textureCount); }
/// <summary> /// Decompresses data and returns a new BinaryReaderEx if necessary. /// </summary> public static BinaryReaderEx GetDecompressedBR(BinaryReaderEx br, out DCX.Type compression) { if (DCX.Is(br)) { byte[] bytes = DCX.Decompress(br, out compression); return(new BinaryReaderEx(false, bytes)); } else { compression = DCX.Type.None; return(br); } }
/// <summary> /// Writes file data to a BinaryWriterEx, compressing it afterwards if specified. /// </summary> private void Write(BinaryWriterEx bw, DCX.Type compression) { if (compression == DCX.Type.None) { Write(bw); } else { BinaryWriterEx bwUncompressed = new BinaryWriterEx(false); Write(bwUncompressed); byte[] uncompressed = bwUncompressed.FinishBytes(); DCX.Compress(uncompressed, bw, compression); } }
/// <summary> /// Writes the file to the specified path, compressing it as specified. /// </summary> public void Write(string path, DCX.Type compression) { using (MemoryStream corruptPreventStream = new MemoryStream()) { BinaryWriterEx bw = new BinaryWriterEx(false, corruptPreventStream); Write(bw, compression); corruptPreventStream.Position = 0; using (FileStream actualFileStream = File.Create(path)) { corruptPreventStream.CopyTo(actualFileStream); } bw.Finish(); } }
internal BinderFile ReadFileData(BinaryReaderEx br) { byte[] bytes; DCX.Type compressionType = DCX.Type.Zlib; if (IsCompressed(Flags)) { bytes = br.GetBytes(DataOffset, (int)CompressedSize); bytes = DCX.Decompress(bytes, out compressionType); } else { bytes = br.GetBytes(DataOffset, (int)CompressedSize); } return(new BinderFile(Flags, ID, Name, bytes) { CompressionType = compressionType, }); }
private static bool Compress(string path) { string xmlPath = $"{path}-yabber-dcx.xml"; if (!File.Exists(xmlPath)) { Console.WriteLine($"XML file not found: {xmlPath}"); return(true); } Console.WriteLine($"Compressing file: {Path.GetFileName(path)}..."); XmlDocument xml = new XmlDocument(); xml.Load(xmlPath); DCX.Type compression = (DCX.Type)Enum.Parse(typeof(DCX.Type), xml.SelectSingleNode("dcx/compression").InnerText); string outPath; if (path.EndsWith(".undcx")) { outPath = path.Substring(0, path.Length - 6); } else { outPath = path + ".dcx"; } if (File.Exists(outPath) && !File.Exists(outPath + ".bak")) { File.Move(outPath, outPath + ".bak"); } DCX.Compress(File.ReadAllBytes(path), compression, outPath); return(false); }
internal BinderFileHeader(BinderFile file) : this(file.Flags, file.ID, file.Name, -1, -1, -1) { CompressionType = file.CompressionType; }
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); } }
public BndContentsEntry(string sourceFile, int fileId, string name, Binder.FileFlags flags, DCX.Type compressionType, string paramType) { this.SourceFile = sourceFile; this.FileId = fileId; this.Name = name; this.Flags = flags; this.CompressionType = compressionType; this.ParamType = paramType; }