/// <summary> /// Reads the contents of RGSSAD archive and populates ArchivedFiles property. /// </summary> private void ReadRGSSAD() { uint key = Constants.RGASSADv1Key; ArchivedFiles = new List <ArchivedFile>(); BinaryReader.BaseStream.Seek(8, SeekOrigin.Begin); while (true) { int length = DecryptInteger(BinaryReader.ReadInt32(), ref key); var archivedFile = new ArchivedFile { Name = DecryptFilename(BinaryReader.ReadBytes(length), ref key), Size = DecryptInteger(BinaryReader.ReadInt32(), ref key), Offset = BinaryReader.BaseStream.Position, Key = key }; ArchivedFiles.Add(archivedFile); BinaryReader.BaseStream.Seek(archivedFile.Size, SeekOrigin.Current); if (BinaryReader.BaseStream.Position == BinaryReader.BaseStream.Length) { break; } } }
/// <summary> /// Reads the contents of RGSSAD archive and populates ArchivedFiles property. /// </summary> private void ReadRGSSAD() { BinaryReader.BaseStream.Seek(8, SeekOrigin.Begin); uint key = (uint)BinaryReader.ReadInt32(); key *= 9; key += 3; ArchivedFiles = new List <ArchivedFile>(); while (true) { ArchivedFile archivedFile = new ArchivedFile(); archivedFile.Offset = DecryptInteger(BinaryReader.ReadInt32(), key); archivedFile.Size = DecryptInteger(BinaryReader.ReadInt32(), key); archivedFile.Key = (uint)DecryptInteger(BinaryReader.ReadInt32(), key); int length = DecryptInteger(BinaryReader.ReadInt32(), key); if (archivedFile.Offset == 0) { break; } archivedFile.Name = DecryptFilename(BinaryReader.ReadBytes(length), key); ArchivedFiles.Add(archivedFile); } }
/// <summary> /// Extracts single file from the file. /// </summary> /// <param name="archivedFile">Archived file</param> /// <param name="outputDirectoryPath">Output directory path</param> /// <param name="overrideExisting">If set to true, overrides existing files</param> /// <param name="createDirectory">If set to true, creates directory specified in encrypted file name</param> /// <exception cref="System.Exception">Invalid file path. Archive could be corrupted.</exception> public void ExtractFile(ArchivedFile archivedFile, string outputDirectoryPath, bool overrideExisting = false, bool createDirectory = true) { string outputPath; if (createDirectory) { // Create output directory if it does not exist string directoryPath = Path.GetDirectoryName(archivedFile.Name); if (directoryPath == null) { throw new Exception("Invalid file path. Archive could be corrupted."); } if (!Directory.Exists(Path.Combine(outputDirectoryPath, directoryPath))) { Directory.CreateDirectory(Path.Combine(outputDirectoryPath, directoryPath)); } outputPath = Path.Combine(outputDirectoryPath, archivedFile.Name); } else { string fileName = archivedFile.Name.Split('\\').Last(); outputPath = Path.Combine(outputDirectoryPath, fileName); } // Override existing file flag is set to true if (File.Exists(outputPath) && !overrideExisting) { return; } BinaryReader.BaseStream.Seek(archivedFile.Offset, SeekOrigin.Begin); byte[] data = BinaryReader.ReadBytes(archivedFile.Size); BinaryWriter binaryWriter = new BinaryWriter(File.OpenWrite(outputPath)); binaryWriter.Write(DecryptFileData(data, archivedFile.Key)); binaryWriter.Close(); }