/// <summary> /// Extends a given array by a given number. /// </summary> /// <param name="array">Array to extend</param> /// <param name="extend">Number to extend by</param> /// <returns>Extended array</returns> private PackFile[] extendPackArray(PackFile[] array, int extend) { int newarraysize = array.Length + extend; PackFile[] newarray = new PackFile[newarraysize]; for (int i = 0; i < newarraysize; i++) { if (newarraysize == 1) { newarray[i] = new PackFile(); break; // Will cause error otherwise, empty base array } else if (i > array.Length - 1) { newarray[i] = new PackFile(); } else { newarray[i] = new PackFile(); newarray[i].FileField = array[i].FileField; newarray[i].FileName = array[i].FileName; newarray[i].FilePath = array[i].FilePath; newarray[i].FilePosition = array[i].FilePosition; newarray[i].FileLength = array[i].FileLength; } } return(newarray); }
/// <summary> /// Once a PackFile's FileName property is filled with a full path, /// populates properties FileName, FilePath, FileField. /// </summary> /// <param name="pfile">a PackFile instance</param> private void translateFileName(PackFile pfile) { /* Decoding the FileField to FileName */ StringBuilder sbFileName = new StringBuilder(56); StringBuilder sbFilePath = new StringBuilder(56); for (int i = 0; i < 56; i++) { try { if ((char)pfile.FileName[i] == '\0') { break; } // We don't support spaces in names, must be end of filename. else if ((char)pfile.FileName[i] == '/') // Found a directory { pfile.PathNodes.Add(sbFileName.ToString()); sbFilePath.Append(sbFileName + "/"); // Add new directory to filepath sbFileName.Clear(); // Clear filename, contents were a directory } else { sbFileName.Append((char)pfile.FileName[i]); // Add char-casted-byte to filename string } } catch (IndexOutOfRangeException) { break; } // Went too far, assume done. } pfile.FileName = sbFileName.ToString(); pfile.FilePath = sbFilePath.ToString(); string fullpath = pfile.FilePath + pfile.FileName; for (int i = 0; i < fullpath.Length; i++) { pfile.FileField[i] = (byte)fullpath[i]; } }
/// <summary> /// Reads PAK file, creates file /// objects and populates fields. /// </summary> private void readPack() { /* Initialization */ int seekPosition = BitConverter.ToInt32(header.IndexOffset, 0); // start position for file indices, will be incremented later byte[] indexData = new byte[64]; // file index data (56 filename, 4 length, 4 offset) double totalSize = 0; /* Creating file objects */ Files = new PackFile[NumFiles]; for (int i = 0; i < NumFiles; i++) { Files[i] = new PackFile(); } /* Populating file objects */ for (int i = 0; i < NumFiles; i++) { /* Read index data from next position */ packStream.Seek(seekPosition, SeekOrigin.Begin); // Go to beginning of next index packStream.Read(indexData, 0, 64); // Get the entire file entry /* Assigning index data */ for (int j = 0; j < 56; j++) { Files[i].FileField[j] = indexData[j]; } Files[i].FilePosition = BitConverter.ToInt32(indexData, 56); Files[i].FileLength = BitConverter.ToInt32(indexData, 60); totalSize += Files[i].FileLength; /* Decoding the FileField to FileName */ StringBuilder sbFileName = new StringBuilder(56); StringBuilder sbFilePath = new StringBuilder(56); for (int k = 0; k < 56; k++) { try { if ((char)Files[i].FileField[k] == '\0') { break; } // We don't support spaces in names, must be end of filename. else if ((char)Files[i].FileField[k] == '/') // Found a directory { Files[i].PathNodes.Add(sbFileName.ToString()); sbFilePath.Append(sbFileName + "/"); // Add new directory to filepath sbFileName.Clear(); // Clear filename, contents were a directory } else { sbFileName.Append((char)Files[i].FileField[k]); // Add char-casted-byte to filename string } } catch (IndexOutOfRangeException) { break; } // Went too far, assume done. } Files[i].FileName = sbFileName.ToString(); Files[i].FilePath = sbFilePath.ToString(); seekPosition += 64; // Increment to next file index } /* Reset packStream */ packStream.Flush(); }