/// <summary>
 ///  Extracts the HG-X image information ONLY from open KIFINT archive stream and does not extract the actual
 ///  images.
 /// </summary>
 /// <param name="entry">The entry to extract from.</param>
 /// <param name="kifintStream">The stream to the open KIFINT archive.</param>
 /// <returns>The extracted <see cref="HgxImage"/> information.</returns>
 ///
 /// <exception cref="ArgumentNullException">
 ///  <paramref name="entry"/> or <paramref name="kifintStream"/> is null.
 /// </exception>
 public static HgxImage ExtractHgx(this KifintEntry entry, KifintStream kifintStream)
 {
     if (entry == null)
     {
         throw new ArgumentNullException(nameof(entry));
     }
     using (var stream = entry.ExtractToStream(kifintStream))
         return(HgxImage.Extract(stream, entry.FileName));
 }
 /// <summary>
 ///  Loads and decompiles the FES screen script entry and returns the script as a string.
 /// </summary>
 /// <param name="entry">The entry to extract from.</param>
 /// <param name="kifintStream">The stream to the open KIFINT archive.</param>
 /// <returns>The decompiled script.</returns>
 ///
 /// <exception cref="ArgumentNullException">
 ///  <paramref name="entry"/> or <paramref name="kifintStream"/> is null.
 /// </exception>
 public static string DecompileScreen(this KifintEntry entry, KifintStream kifintStream)
 {
     if (entry == null)
     {
         throw new ArgumentNullException(nameof(entry));
     }
     using (var stream = entry.ExtractToStream(kifintStream))
         return(FesScreen.Decompile(stream, entry.FileName));
 }
Beispiel #3
0
 /// <summary>
 ///  Loads and decompiles the CST scene script entry and returns the human readable script as a string.
 /// </summary>
 /// <param name="entry">The entry to extract from.</param>
 /// <param name="kifintStream">The stream to the open KIFINT archive.</param>
 /// <returns>The human readable script.</returns>
 ///
 /// <exception cref="ArgumentNullException">
 ///  <paramref name="entry"/> or <paramref name="kifintStream"/> is null.
 /// </exception>
 public static string HumanReadableScene(this KifintEntry entry, KifintStream kifintStream)
 {
     if (entry == null)
     {
         throw new ArgumentNullException(nameof(entry));
     }
     using (var stream = entry.ExtractToStream(kifintStream))
         return(CstScene.HumanReadable(stream, entry.FileName));
 }
Beispiel #4
0
 /// <summary>
 ///  Extracts the ZT package from the open KIFINT archive stream and saves the contained files to the output
 ///  directory.
 /// </summary>
 /// <param name="entry">The entry to extract from.</param>
 /// <param name="kifintStream">The stream to the open KIFINT archive.</param>
 /// <param name="outputDir">The output directory to save the package files to.</param>
 /// <returns>The extracted ZT package.</returns>
 ///
 /// <exception cref="ArgumentNullException">
 ///  <paramref name="entry"/>, <paramref name="kifintStream"/>, or <paramref name="outputDir"/> is null.
 /// </exception>
 public static ZtPackage ExtractZtAndFiles(this KifintEntry entry, KifintStream kifintStream, string outputDir)
 {
     if (entry == null)
     {
         throw new ArgumentNullException(nameof(entry));
     }
     using (var stream = entry.ExtractToStream(kifintStream))
         return(ZtPackage.ExtractFiles(stream, entry.FileName, outputDir));
 }
 /// <summary>
 ///  Extracts the HG-X image information from the KIFINT entry's open KIFINT archive stream and saves all
 ///  images to the output <paramref name="outputDir"/>.
 /// </summary>
 /// <param name="entry">The entry to extract from.</param>
 /// <param name="kifintStream">The stream to the open KIFINT archive.</param>
 /// <param name="outputDir">The output directory to save the images to.</param>
 /// <param name="options">The options for manipulating the image during extraction.</param>
 /// <returns>The extracted <see cref="HgxImage"/> information.</returns>
 ///
 /// <exception cref="ArgumentNullException">
 ///  <paramref name="entry"/>, <paramref name="kifintStream"/>, or <paramref name="outputDir"/> is null.
 /// </exception>
 public static HgxImage ExtractHgxAndImages(this KifintEntry entry, KifintStream kifintStream, string outputDir, HgxOptions options)
 {
     if (entry == null)
     {
         throw new ArgumentNullException(nameof(entry));
     }
     using (var stream = entry.ExtractToStream(kifintStream))
         return(HgxImage.ExtractImages(stream, entry.FileName, outputDir, options));
 }
 /// <summary>
 ///  Loads and decompiles the FES screen script entry and outputs it to the specified stream.
 /// </summary>
 /// <param name="entry">The entry to extract from.</param>
 /// <param name="kifintStream">The stream to the open KIFINT archive.</param>
 /// <param name="outStream">The output stream to write the decompiled script to.</param>
 /// <param name="encoding">The output encoding, <see cref="CatUtils.ShiftJIS"/> if null.</param>
 ///
 /// <exception cref="ArgumentNullException">
 ///  <paramref name="entry"/>, <paramref name="kifintStream"/>, or <paramref name="outStream"/> is null.
 /// </exception>
 public static void DecompileScreenToStream(this KifintEntry entry, KifintStream kifintStream,
                                            Stream outStream, Encoding encoding = null)
 {
     if (entry == null)
     {
         throw new ArgumentNullException(nameof(entry));
     }
     using (var stream = entry.ExtractToStream(kifintStream))
         FesScreen.DecompileToStream(stream, entry.FileName, outStream, encoding);
 }
Beispiel #7
0
 /// <summary>
 ///  Loads and decompiles the CST scene script entry and outputs it to the specified human readable stream.
 /// </summary>
 /// <param name="entry">The entry to extract from.</param>
 /// <param name="outStream">The output stream to write the human readable script to.</param>
 /// <param name="kifintStream">The stream to the open KIFINT archive.</param>
 ///
 /// <exception cref="ArgumentNullException">
 ///  <paramref name="entry"/>, <paramref name="kifintStream"/>, or <paramref name="outStream"/> is null.
 /// </exception>
 public static void HumanReadableSceneToStream(this KifintEntry entry, KifintStream kifintStream,
                                               Stream outStream)
 {
     if (entry == null)
     {
         throw new ArgumentNullException(nameof(entry));
     }
     using (var stream = entry.ExtractToStream(kifintStream))
         CstScene.HumanReadableToStream(stream, entry.FileName, outStream);
 }
Beispiel #8
0
 /// <summary>
 ///  Loads and decompiles the CST scene script entry and outputs it to the specified file.
 /// </summary>
 /// <param name="entry">The entry to extract from.</param>
 /// <param name="kifintStream">The stream to the open KIFINT archive.</param>
 /// <param name="outFile">The output file to write the decompiled script to.</param>
 /// <param name="encoding">The output encoding, <see cref="CatUtils.ShiftJIS"/> if null.</param>
 ///
 /// <exception cref="ArgumentNullException">
 ///  <paramref name="entry"/>, <paramref name="kifintStream"/>, or <paramref name="outFile"/> is null.
 /// </exception>
 public static void DecompileSceneToFile(this KifintEntry entry, KifintStream kifintStream, string outFile,
                                         Encoding encoding = null)
 {
     if (entry == null)
     {
         throw new ArgumentNullException(nameof(entry));
     }
     using (var stream = entry.ExtractToStream(kifintStream))
         CstScene.DecompileToFile(stream, entry.FileName, outFile, encoding);
 }
        /// <summary>
        ///  Extracts the KIFINT entry and saves it to <paramref name="directory"/>/<see cref="FileName"/>.
        /// </summary>
        /// <param name="kifintStream">The stream to the open KIFINT archive.</param>
        /// <param name="directory">The directory to save the decrypted entry to.</param>
        /// <returns>The file path to the KIFINT entry.</returns>
        ///
        /// <exception cref="ArgumentNullException">
        ///  <paramref name="kifintStream"/> or <paramref name="directory"/> is null.
        /// </exception>
        public string ExtractToDirectory(KifintStream kifintStream, string directory)
        {
            if (directory == null)
            {
                throw new ArgumentNullException(nameof(directory));
            }
            string filePath = Path.Combine(directory, FileName);

            ExtractToFile(kifintStream, filePath);
            return(filePath);
        }
 /// <summary>
 ///  Extracts the KIFINT entry to a fixed stream of <paramref name="kifintStream"/>.
 /// </summary>
 /// <param name="kifintStream">The stream to the open KIFINT archive.</param>
 /// <param name="leaveOpen">
 ///  True if the KIFINT archive stream should be left open even after closing the returned stream.
 /// </param>
 /// <returns>
 ///  A fixed stream containing the data of the decrypted entry. This stream must always be disposed of, because
 ///  it's not guaranteed to be a fixed stream of <paramref name="kifintStream"/>. This is the case when the
 ///  length is small enough for extracting bytes to be more efficient.
 /// </returns>
 ///
 /// <exception cref="ArgumentNullException">
 ///  <paramref name="kifintStream"/> is null.
 /// </exception>
 public Stream ExtractToStream(KifintStream kifintStream, bool leaveOpen)
 {
     if (CatDebug.StreamExtract)
     {
         return(KifintArchive.ExtractToStream(kifintStream, this, leaveOpen));
     }
     else
     {
         return(new MemoryStream(ExtractToBytes(kifintStream)));
     }
 }
 /// <summary>
 ///  Extracts the KIFINT entry to a fixed stream of <paramref name="kifintStream"/>.
 /// </summary>
 /// <param name="kifintStream">The stream to the open KIFINT archive.</param>
 /// <returns>
 ///  A fixed stream containing the data of the decrypted entry. This stream must always be disposed of, because
 ///  it's not guaranteed to be a fixed stream of <paramref name="kifintStream"/>. This is the case when the
 ///  length is small enough for extracting bytes to be more efficient.
 /// </returns>
 ///
 /// <exception cref="ArgumentNullException">
 ///  <paramref name="kifintStream"/> is null.
 /// </exception>
 public Stream ExtractToStream(KifintStream kifintStream)
 {
     if (CatDebug.StreamExtract)             // Leave stream open by default, it may be used again.
     {
         return(ExtractToStream(kifintStream, true));
     }
     else
     {
         return(new MemoryStream(ExtractToBytes(kifintStream)));
     }
 }
 /// <summary>
 ///  Extracts the KIFINT entry and saves it to <paramref name="filePath"/>.
 /// </summary>
 /// <param name="kifintStream">The stream to the open KIFINT archive.</param>
 /// <param name="filePath">The file path to save the decrypted entry to.</param>
 ///
 /// <exception cref="ArgumentNullException">
 ///  <paramref name="kifintStream"/> or <paramref name="filePath"/> is null.
 /// </exception>
 public void ExtractToFile(KifintStream kifintStream, string filePath)
 {
     if (CatDebug.StreamExtract)
     {
         using (var output = File.Create(filePath))
             using (var input = ExtractToStream(kifintStream, true))
                 input.CopyTo(output);
     }
     else
     {
         File.WriteAllBytes(filePath, ExtractToBytes());
     }
     File.WriteAllBytes(filePath, ExtractToBytes(kifintStream));
 }
        /// <summary>
        ///  Extracts the KIFINT entry to a fixed stream of <paramref name="kifintStream"/>.
        /// </summary>
        /// <param name="kifintStream">The stream to the open KIFINT archive.</param>
        /// <param name="entry">The KIFINT entry used to locate the file.</param>
        /// <param name="leaveOpen">
        ///  True if the KIFINT archive stream should be left open even after closing the returned stream.
        /// </param>
        /// <returns>
        ///  A fixed stream containing the data of the decrypted entry. This stream must always be disposed of, because
        ///  it's not guaranteed to be a fixed stream of <paramref name="kifintStream"/>. This is the case when the
        ///  length is small enough for extracting bytes to be more efficient.
        /// </returns>
        ///
        /// <exception cref="ArgumentNullException">
        ///  <paramref name="kifintStream"/> or <paramref name="entry"/> is null.
        /// </exception>
        internal static Stream ExtractToStream(KifintStream kifintStream, KifintEntry entry, bool leaveOpen)
        {
            if (kifintStream == null)
            {
                throw new ArgumentNullException(nameof(kifintStream));
            }
            if (entry == null)
            {
                throw new ArgumentNullException(nameof(entry));
            }
            var kifint = entry.Kifint;

            // Smaller streams will be faster as reading a big chunk of data all at once.
            if (kifint.IsEncrypted && entry.Length < 131072 * 2)               // Some arbitrary power of 2 cutoff length
            {
                try {
                    return(new MemoryStream(ExtractToBytes(kifintStream, entry)));
                } finally {
                    // We need to make sure the the stream is closed by us if we're not
                    // relying on the fixed streams to close the KIFINT archive stream.
                    if (!leaveOpen)
                    {
                        kifintStream.Close();
                    }
                }
            }

            kifintStream.Open(kifint);
            kifintStream.Position = entry.Offset;

            if (kifint.IsEncrypted)
            {
                return(new BlowfishInputStream(kifint.Blowfish, kifintStream, entry.Length, leaveOpen));
            }
            return(new FixedStream(kifintStream, entry.Length, leaveOpen));
        }
        /// <summary>
        ///  Extracts the KIFINT entry file from the the entry's KIFINT archive.
        /// </summary>
        /// <param name="kifintStream">The stream to the open KIFINT archive.</param>
        /// <param name="entry">The KIFINT entry used to locate the file.</param>
        /// <returns>A byte array containing the extracted KIFINT entry's file data.</returns>
        ///
        /// <exception cref="ArgumentNullException">
        ///  <paramref name="kifintStream"/> or <paramref name="entry"/> is null.
        /// </exception>
        internal static byte[] ExtractToBytes(KifintStream kifintStream, KifintEntry entry)
        {
            if (kifintStream == null)
            {
                throw new ArgumentNullException(nameof(kifintStream));
            }
            if (entry == null)
            {
                throw new ArgumentNullException(nameof(entry));
            }
            var kifint = entry.Kifint;

            kifintStream.Open(kifint);
            BinaryReader reader = new BinaryReader(kifintStream);

            kifintStream.Position = entry.Offset;
            byte[] buffer = reader.ReadBytes(entry.Length);

            if (kifint.IsEncrypted)
            {
                kifint.Blowfish.Decrypt(buffer, entry.Length & ~7);
            }
            return(buffer);
        }
 /// <summary>
 ///  Extracts the KIFINT entry to a <see cref="byte"/>[].
 /// </summary>
 /// <param name="kifintStream">The stream to the open KIFINT archive.</param>
 /// <returns>A byte array containing the data of the decrypted entry.</returns>
 ///
 /// <exception cref="ArgumentNullException">
 ///  <paramref name="kifintStream"/> is null.
 /// </exception>
 public byte[] ExtractToBytes(KifintStream kifintStream)
 {
     return(KifintArchive.ExtractToBytes(kifintStream, this));
 }
 /// <summary>
 ///  Extracts the KIFINT entry to a <see cref="byte"/>[].
 /// </summary>
 /// <returns>A byte array containing the data of the decrypted entry.</returns>
 public byte[] ExtractToBytes()
 {
     using (KifintStream kifintStream = new KifintStream())
         return(KifintArchive.ExtractToBytes(kifintStream, this));
 }