示例#1
0
 /* Add a file to the PAK2 */
 public override PAKReturnType AddFile(string PathToNewFile, int TrimFromPath = 0) //TrimFromPath is available to leave some directory trace in the filename
 {
     try
     {
         EntryPAK2 NewFile = new EntryPAK2();
         if (TrimFromPath == 0)
         {
             NewFile.Filename = Path.GetFileName(PathToNewFile).ToUpper();
         }                                                                                        //Virtual directory support here would be nice too
         else
         {
             NewFile.Filename = PathToNewFile.Substring(TrimFromPath).ToUpper();
         }                                                                            //Easy to fail here, so be careful on function usage!
         NewFile.Content = File.ReadAllBytes(PathToNewFile);
         Pak2Files.Add(NewFile);
         return(PAKReturnType.SUCCESS);
     }
     catch (IOException) { return(PAKReturnType.FAIL_COULD_NOT_ACCESS_FILE); }
     catch (Exception) { return(PAKReturnType.FAIL_UNKNOWN); }
 }
示例#2
0
        /* Load the contents of an existing PAK2 */
        public override PAKReturnType Load()
        {
            if (!File.Exists(FilePathPAK))
            {
                return(PAKReturnType.FAIL_TRIED_TO_LOAD_VIRTUAL_ARCHIVE);
            }

            try
            {
                //Open PAK
                BinaryReader ArchiveFile = new BinaryReader(File.OpenRead(FilePathPAK));

                //Read the header info
                string MagicValidation = "";
                for (int i = 0; i < 4; i++)
                {
                    MagicValidation += ArchiveFile.ReadChar();
                }
                if (MagicValidation != "PAK2")
                {
                    ArchiveFile.Close(); return(PAKReturnType.FAIL_ARCHIVE_IS_NOT_EXCPETED_TYPE);
                }
                OffsetListBegin = ArchiveFile.ReadInt32() + 16;
                NumberOfEntries = ArchiveFile.ReadInt32();
                ArchiveFile.BaseStream.Position += 4; //Skip "4"

                //Read all file names and create entries
                for (int i = 0; i < NumberOfEntries; i++)
                {
                    string ThisFileName = "";
                    for (byte b; (b = ArchiveFile.ReadByte()) != 0x00;)
                    {
                        ThisFileName += (char)b;
                    }

                    EntryPAK2 NewPakFile = new EntryPAK2();
                    NewPakFile.Filename = ThisFileName;
                    Pak2Files.Add(NewPakFile);
                }

                //Read all file offsets
                ArchiveFile.BaseStream.Position = OffsetListBegin;
                List <int> FileOffsets = new List <int>();
                FileOffsets.Add(OffsetListBegin + (NumberOfEntries * 4));
                for (int i = 0; i < NumberOfEntries; i++)
                {
                    FileOffsets.Add(ArchiveFile.ReadInt32());
                    Pak2Files[i].Offset = FileOffsets[i];
                }

                //Read in the files to entries
                ExtraBinaryUtils BinaryUtils = new ExtraBinaryUtils();
                for (int i = 0; i < NumberOfEntries; i++)
                {
                    //Must pass to RemoveLeadingNulls as each file starts with 0-3 null bytes to align files to a 4-byte block reader
                    Pak2Files[i].Content = BinaryUtils.RemoveLeadingNulls(ArchiveFile.ReadBytes(FileOffsets[i + 1] - FileOffsets[i]));
                }

                //Close PAK
                ArchiveFile.Close();
                return(PAKReturnType.SUCCESS);
            }
            catch (IOException) { return(PAKReturnType.FAIL_COULD_NOT_ACCESS_FILE); }
            catch (Exception) { return(PAKReturnType.FAIL_UNKNOWN); }
        }