private void ParseHeader(IFile f) { headerStream.Position = 0; var numDataFiles = 0; var header = Header.Read(headerStream); var fileEntries = new FileEntry[header.num_files]; for (var i = 0; i < fileEntries.Length; i++) { fileEntries[i] = FileEntry.Read(headerStream); } var dirEntries = new DirEntry[header.num_dirs]; for (var i = 0; i < dirEntries.Length; i++) { dirEntries[i] = DirEntry.Read(headerStream); } var stringTable = new List <string>(); // # of strings doesn't always match file count var stringTableEnd = header.string_table_offset + header.string_table_size; while (headerStream.Position < stringTableEnd) { stringTable.Add(headerStream.ReadASCIINullTerminated()); } var fileOffsets = new OffsetEntry[header.num_offsets]; for (var i = 0; i < fileOffsets.Length; i++) { fileOffsets[i] = OffsetEntry.Read(headerStream); if (fileOffsets[i].pk_num > numDataFiles) { numDataFiles = fileOffsets[i].pk_num; } } var dirs_flat = new Common.DefaultDirectory[header.num_dirs]; dirs_flat[0] = root; for (var i = 1; i < dirs_flat.Length; i++) { var parent = dirs_flat[dirEntries[i].parent]; parent.AddDir(dirs_flat[i] = new Common.DefaultDirectory(parent, stringTable[(int)dirEntries[i].string_num])); } contentFiles = new Stream[numDataFiles + 1]; var baseName = f.Name.Replace(".hdr.e.2", ""); for (var i = 0; i <= numDataFiles; i++) { contentFiles[i] = f.Parent.GetFile($"{baseName}.pk{i}").GetStream(); } for (var i = 0; i < fileEntries.Length; i++) { var entry = fileEntries[i]; var name = entry.string_num < stringTable.Count ? stringTable[(int)entry.string_num] : "ERROR_FILENAME"; bool encrypted = false; if (name.EndsWith(".e.2")) { encrypted = true; name = name.Remove(name.Length - 4); } dirs_flat[entry.dir_num].AddFile(new Common.OffsetFile( name, dirs_flat[entry.dir_num], contentFiles[fileOffsets[entry.offset_num].pk_num], fileOffsets[entry.offset_num].pk_offset, entry.size, wrapStream: encrypted ? (s => new PowerChordCryptStream(s)) : (Func <Stream, Stream>)null)); } }
private void ParseHeader(IFile f) { headerStream.Position = 0; var numDataFiles = 0; var header = Header.Read(headerStream); var fileEntries = new FileEntry[header.num_files]; for (var i = 0; i < fileEntries.Length; i++) { fileEntries[i] = FileEntry.Read(headerStream); } var dirEntries = new DirEntry[header.num_dirs]; for (var i = 0; i < dirEntries.Length; i++) { dirEntries[i] = DirEntry.Read(headerStream); } var stringTable = new string[header.num_files]; var stringTableEnd = header.string_table_offset + header.string_table_size; var x = 0; while (headerStream.Position < stringTableEnd) { stringTable[x++] = headerStream.ReadASCIINullTerminated(); } var fileOffsets = new OffsetEntry[header.num_offsets]; for (var i = 0; i < fileOffsets.Length; i++) { fileOffsets[i] = OffsetEntry.Read(headerStream); if (fileOffsets[i].pk_num > numDataFiles) { numDataFiles = fileOffsets[i].pk_num; } } var dirs_flat = new Common.DefaultDirectory[header.num_dirs]; dirs_flat[0] = root; for (var i = 1; i < dirs_flat.Length; i++) { var parent = dirs_flat[dirEntries[i].parent]; parent.AddDir(dirs_flat[i] = new Common.DefaultDirectory(parent, stringTable[dirEntries[i].string_num])); } contentFiles = new Stream[numDataFiles + 1]; var baseName = f.Name.Replace(".hdr.e.2", ""); for (var i = 0; i <= numDataFiles; i++) { contentFiles[i] = f.Parent.GetFile($"{baseName}.pk{i}").GetStream(); } for (var i = 0; i < fileEntries.Length; i++) { var entry = fileEntries[i]; var name = entry.string_num < stringTable.Length ? stringTable[entry.string_num] : "ERROR_FILENAME"; dirs_flat[entry.dir_num].AddFile(new Common.OffsetFile( name, dirs_flat[entry.dir_num], contentFiles[fileOffsets[entry.offset_num].pk_num], fileOffsets[entry.offset_num].pk_offset, entry.size)); } }