Esempio n. 1
0
        /// <summary>
        /// Updates the TPPK archive in a NARC archive with a new TPPK archive.
        /// </summary>
        /// <param name="inputPaths">The DDS files to add to the new TPPK archive.</param>
        /// <param name="narcPath">The NARC archive.</param>
        public static void UpdateTppk(ICollection <string> inputPaths, string narcPath)
        {
            // Before we start, let's go through all the paths and make sure they are all DDS files
            foreach (var file in inputPaths)
            {
                if (!FileHelper.IsDds(file))
                {
                    throw new InvalidFileTypeException(string.Format(ErrorMessages.NotADdsFile, Path.GetFileName(file)));
                }
            }

            var entryStreams = new List <Stream>();
            var isTppkFound  = false;

            void OnExtract(Stream stream)
            {
                var entryStream = new MemoryStream();

                if (!isTppkFound && FileHelper.IsTppk(stream))
                {
                    // Create a new TPPK archive
                    TppkArchive.Create(inputPaths, entryStream);
                    isTppkFound = true;
                }
                else
                {
                    // Just extract this file to add to the new NARC
                    stream.CopyTo(entryStream);
                }

                entryStream.Position = 0;
                entryStreams.Add(entryStream);
            }

            Extract(narcPath, OnExtract);

            if (!isTppkFound)
            {
                throw new NoTppkArchiveException(string.Format(ErrorMessages.NoTppkArchiveInFile, Path.GetFileName(narcPath)));
            }

            // Create the NARC archive. We'll create a temporary file first, then overwrite the original.
            var tempNarcPath = Path.GetTempFileName();

            Create(entryStreams, tempNarcPath);
            File.Delete(narcPath);
            File.Move(tempNarcPath, narcPath);
        }
Esempio n. 2
0
        /// <summary>
        /// Extracts the TPPK archive from a NARC archive.
        /// </summary>
        /// <param name="inputPath">The NARC archive.</param>
        /// <param name="outputPath">The folder to extract the TPPK archive to.</param>
        public static void ExtractTppk(string inputPath, string outputPath)
        {
            var isTppkFound = false;

            void OnExtract(Stream stream)
            {
                if (!isTppkFound && FileHelper.IsTppk(stream))
                {
                    TppkArchive.Extract(stream, outputPath, $"{Path.GetFileNameWithoutExtension(inputPath)}_");
                    isTppkFound = true;
                }
            }

            Extract(inputPath, OnExtract);

            if (!isTppkFound)
            {
                throw new NoTppkArchiveException(string.Format(ErrorMessages.NoTppkArchiveInFile, Path.GetFileName(inputPath)));
            }
        }