/// <summary> /// Extracts a PiggTexture object to the provided stream. /// </summary> /// <param name="output">Stream to which to write the texture.</param> /// <param name="type">How the texture is to be extracted.</param> public void Extract(Stream output, TextureExtractType type) { int block_size = 0x1000; byte[] buffer = new byte[block_size]; switch (type) { case TextureExtractType.Original: output.Write(this.Source, 0, this.Source.Length); break; case TextureExtractType.Texture: this.Header.Extract(output); output.Write(this.Source, 0, this.Source.Length); break; case TextureExtractType.Png: this.Image.Save(output, ImageFormat.Png); break; case TextureExtractType.Bmp: this.Image.Save(output, ImageFormat.Bmp); break; case TextureExtractType.Jpeg: this.Image.Save(output, ImageFormat.Jpeg); break; case TextureExtractType.Gif: this.Image.Save(output, ImageFormat.Gif); break; case TextureExtractType.Tiff: this.Image.Save(output, ImageFormat.Tiff); break; default: throw new FormatException("Invalid extraction format."); } }
/// <summary> /// Extract a node and all of its subnodes and children. /// </summary> /// <param name="RootPath">Path to which to extract node.</param> /// <param name="TextureType">How textures are to be extracted.</param> /// <param name="NodeType">Whether to recreate the full root path.</param> /// <param name="OnlyTextures">Indicates whether only textures or to be /// extracted or all files.</param> /// <param name="Callback">Callback function to update the caller that a /// leaf is about to be extracted.</param> public void Extract(string RootPath, TextureExtractType TextureType, NodeExtractType NodeType, bool OnlyTextures, ExtractProgress Callback) { // Create the path to extract the nodes. string dir_path = RootPath; switch (NodeType) { case NodeExtractType.Relative: dir_path += Path.DirectorySeparatorChar + this.Name; break; case NodeExtractType.FullPath: dir_path += Path.DirectorySeparatorChar + this.FullPath; break; default: throw new NotSupportedException("NodeExtractType not supported."); } DirectoryInfo new_dir = Directory.CreateDirectory(dir_path); // Extract all child nodes foreach (PiggNode node in this.Subnodes) { node.Extract(dir_path, TextureType, NodeExtractType.Relative, OnlyTextures, Callback); } // Extract all files foreach (PiggLeaf leaf in this.Leafs) { if (Callback != null) { Callback(leaf); } int last = leaf.PiggReferences.Count - 1; PiggLeafInfo leaf_info = leaf.PiggReferences[last]; if (Path.GetExtension(leaf.Name).ToLower() != ".texture" || TextureType == TextureExtractType.Texture) { // Check to see if we only want textures. if (TextureType == TextureExtractType.Texture || !OnlyTextures) { // Extract the file as-is. FileStream fs = new FileStream(dir_path + Path.DirectorySeparatorChar + leaf.Name, FileMode.Create, FileAccess.Write, FileShare.Write); if (fs != null) { leaf_info.Extract(fs); } fs.Close(); } } else { PiggTexture tex = new PiggTexture(leaf_info); string ext = ""; switch (TextureType) { case TextureExtractType.Bmp: ext += ".bmp"; break; case TextureExtractType.Gif: ext += ".gif"; break; case TextureExtractType.Jpeg: ext += ".jpg"; break; case TextureExtractType.Original: ext += Path.GetExtension(tex.Filename).ToLower(); break; case TextureExtractType.Png: ext += ".png"; break; case TextureExtractType.Tiff: ext += ".tif"; break; default: throw new NotSupportedException(); } FileStream fs = new FileStream(dir_path + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(leaf.Name) + ext, FileMode.Create, FileAccess.Write, FileShare.Write); tex.Extract(fs, TextureType); fs.Close(); } } }