static void DoExtractFile(string vpkDirFileName, VpkNode node) { if (node.ArchiveIndex == VpkNode.DirectoryArchiveIndex) { Console.WriteLine("Found entry: {0}", node.FilePath); } else { Console.WriteLine("Found entry: {0} in VPK {1}", node.FilePath, node.ArchiveIndex); } ExtractFile(vpkDirFileName, node); }
public static Stream GetInputStream(string vpkDirFileName, VpkNode node) { if (node.EntryLength == 0 && node.PreloadBytes > 0) { return new MemoryStream(node.PreloadData); } else if (node.PreloadBytes == 0) { var prefix = new string(Enumerable.Repeat('0', 3 - node.ArchiveIndex.ToString().Length).ToArray()); var dataPakFilename = vpkDirFileName.Replace("_dir.vpk", "_" + prefix + node.ArchiveIndex + ".vpk"); var fsin = new FileStream(dataPakFilename, FileMode.Open); fsin.Seek(node.EntryOffset, SeekOrigin.Begin); return fsin; } else { throw new NotSupportedException("Unable to get entry data: Both EntryLength and PreloadBytes specified."); } }
static VpkNode[] LoadNodeChildren(BinaryReader reader, VpkNode parent) { var nodes = new List<VpkNode>(); VpkNode newNode = null; while (newNode == null || !string.IsNullOrEmpty(newNode.Name)) { newNode = new VpkNode(parent); newNode.Load(reader); if (!string.IsNullOrEmpty(newNode.Name)) { nodes.Add(newNode); newNode.Children = LoadNodeFileChildren(reader, newNode); } } return nodes.ToArray(); }
static List<VpkNode> LoadRootNodes(BinaryReader reader) { var nodes = new List<VpkNode>(); VpkNode newNode = null; while (newNode == null || !string.IsNullOrEmpty(newNode.Name)) { newNode = new VpkNode(); newNode.Load(reader); if (!string.IsNullOrEmpty(newNode.Name)) { nodes.Add(newNode); newNode.Children = LoadNodeChildren(reader, newNode); } } return nodes; }
static VpkNode[] LoadNodeFileChildren(BinaryReader reader, VpkNode parent) { var nodes = new List <VpkNode>(); VpkNode newNode = null; while (newNode == null || !string.IsNullOrEmpty(newNode.Name)) { newNode = new VpkNode(parent); newNode.LoadFileInfo(reader); if (!string.IsNullOrEmpty(newNode.Name)) { nodes.Add(newNode); } } return(nodes.ToArray()); }
static List <VpkNode> LoadRootNodes(BinaryReader reader) { var nodes = new List <VpkNode>(); VpkNode newNode = null; while (newNode == null || !string.IsNullOrEmpty(newNode.Name)) { newNode = new VpkNode(); newNode.Load(reader); if (!string.IsNullOrEmpty(newNode.Name)) { nodes.Add(newNode); newNode.Children = LoadNodeChildren(reader, newNode); } } return(nodes); }
public static Stream GetInputStream(string vpkDirFileName, VpkNode node) { if (node.EntryLength == 0 && node.PreloadBytes > 0) { return(new MemoryStream(node.PreloadData)); } else if (node.PreloadBytes == 0) { var prefix = new string(Enumerable.Repeat('0', 3 - node.ArchiveIndex.ToString().Length).ToArray()); var dataPakFilename = vpkDirFileName.Replace("_dir.vpk", "_" + prefix + node.ArchiveIndex + ".vpk"); var fsin = new FileStream(dataPakFilename, FileMode.Open); fsin.Seek(node.EntryOffset, SeekOrigin.Begin); return(fsin); } else { throw new NotSupportedException("Unable to get entry data: Both EntryLength and PreloadBytes specified."); } }
static void ExtractFile(string vpkDirFileName, VpkNode node) { using (var inputStream = GetInputStream(vpkDirFileName, node)) { var pathPieces = node.FilePath.Split('/'); var directory = pathPieces.Take(pathPieces.Count() - 1); var fileName = pathPieces.Last(); EnsureDirectoryExists(Path.Combine(directory.ToArray())); using (var fsout = File.OpenWrite(Path.Combine(pathPieces))) { var buffer = new byte[1024]; int amtToRead = (int)node.EntryLength; int read; while ((read = inputStream.Read(buffer, 0, buffer.Length)) > 0 && amtToRead > 0) { fsout.Write(buffer, 0, Math.Min(amtToRead, read)); amtToRead -= read; } } } }
public VpkNode(VpkNode parent) { this.parent = parent; }