/// <summary> /// Read the Pk2 structure recursively. /// </summary> private void Read(long Position, string RootPath) { BinaryReader reader = new BinaryReader(m_FileStream); reader.BaseStream.Position = Position; List <Pk2Folder> folders = new List <Pk2Folder>(); sPk2EntryBlock entryBlock = (sPk2EntryBlock)BufferToStruct(m_Blowfish.Decode(reader.ReadBytes(Marshal.SizeOf(typeof(sPk2EntryBlock)))), typeof(sPk2EntryBlock)); for (int i = 0; i < 20; i++) { sPk2Entry entry = entryBlock.Entries[i]; //..... switch (entry.Type) { case 0: //Null Entry break; case 1: //Folder if (entry.Name != "." && entry.Name != "..") { Pk2Folder folder = new Pk2Folder(); folder.Name = entry.Name; folder.Position = BitConverter.ToInt64(entry.g_Position, 0); folders.Add(folder); m_Folders[(RootPath + entry.Name).ToUpper()] = folder; m_CurrentFolder.SubFolders.Add(folder); } break; case 2: //File Pk2File file = new Pk2File(); file.Position = entry.Position; file.Name = entry.Name; file.Size = entry.Size; file.ParentFolder = m_CurrentFolder; m_Files[(RootPath + entry.Name).ToUpper()] = file; m_CurrentFolder.Files.Add(file); break; } } if (entryBlock.Entries[19].NextChain != 0) { Read(entryBlock.Entries[19].NextChain, RootPath); } foreach (Pk2Folder folder in folders) { m_CurrentFolder = folder; if (folder.Files == null) { folder.Files = new List <Pk2File>(); } if (folder.SubFolders == null) { folder.SubFolders = new List <Pk2Folder>(); } Read(folder.Position, RootPath + folder.Name + "\\"); } }
/// <summary> /// Reads Pk2 block structure from the position specified and save all data into the Folder /// </summary> private void Read(long Position, Pk2Folder CurrentFolder, string ParentPath) { // Set cursor position in the stream BinaryReader reader = new BinaryReader(m_FileStream); reader.BaseStream.Position = Position; // Keep a list with all folders from this block to add it to subfolders List <Pk2Folder> subfolders = new List <Pk2Folder>(); // Read pk2 block sPk2EntryBlock entryBlock = (sPk2EntryBlock)BufferToStruct(m_Blowfish.Decode(reader.ReadBytes(Marshal.SizeOf(typeof(sPk2EntryBlock)))), typeof(sPk2EntryBlock)); for (int i = 0; i < 20; i++) { // Entry sPk2Entry entry = entryBlock.Entries[i]; // Check entry type switch (entry.Type) { // Null Entry case 0: break; // Folder case 1: // Check if is not a parent/root folder if (entry.Name != "." && entry.Name != "..") { Pk2Folder folder = new Pk2Folder() { Name = entry.Name, Position = BitConverter.ToInt64(entry.g_Position, 0) }; // Add subfolder subfolders.Add(folder); // Save dictionary reference m_Folders[ParentPath + entry.Name.ToUpper(m_EnglishCulture)] = folder; } break; // File case 2: Pk2File file = new Pk2File { Position = entry.Position, Name = entry.Name, Size = entry.Size, ParentFolder = CurrentFolder }; // Add files to the current folder CurrentFolder.Files.Add(file); // Save dictionary reference m_Files[ParentPath + entry.Name.ToUpper(m_EnglishCulture)] = file; break; } } // Read the next pk2 block chain if (entryBlock.Entries[19].NextChain != 0) { Read(entryBlock.Entries[19].NextChain, CurrentFolder, ParentPath); } // Add subfolders to the current folder CurrentFolder.SubFolders.AddRange(subfolders); // Continue reading folder by folder foreach (var f in subfolders) { Read(f.Position, f, ParentPath + f.Name.ToUpper(m_EnglishCulture) + "\\"); } }