Esempio n. 1
0
        /// <summary>
        /// Asynchronously loads a NeFS archive from disk.
        /// </summary>
        /// <param name="filepath">The path to the .nefs file to load.</param>
        /// <param name="p">Progress info for reporting progress.</param>
        /// <returns></returns>
        public static async Task <NefsArchive> LoadAsync(String filePath, NefsProgressInfo p)
        {
            // TODO : Move the async stuff to UI project like replace/save??

            var archive = await Task.Run(() => {
                try
                {
                    log.Info("----------------------------");
                    log.Info(String.Format("Opening archive {0}...", filePath));
                    var ret = new NefsArchive(filePath, p);
                    log.Info("Archive opened successfully.");
                    return(ret);
                }
                catch (OperationCanceledException ex)
                {
                    log.Info("Open archive operation cancelled.");
                    return(null);
                }
                catch (IOException ex)
                {
                    log.Error(String.Format("An error occurred while reading the file {0}.", filePath), ex);
                    return(null);
                }
            });

            return(archive);
        }
Esempio n. 2
0
        /// <summary>
        /// Loads a NeFS item with the specified id from the archive.
        /// </summary>
        /// <param name="file">The file stream to load from.</param>
        /// <param name="archive">The NeFS archive this item is in.</param>
        /// <param name="id">The id of the item in the archive to load.</param>
        public NefsItem(FileStream file, NefsArchive archive, UInt32 id)
        {
            /* Validate inputs */
            if (file == null)
            {
                throw new ArgumentNullException("File stream required to load NeFS item.");
            }

            if (archive == null)
            {
                throw new ArgumentNullException("NeFS archive object required to load an item.");
            }

            _archive = archive;
            _id      = id;

            /* Get header entries related to this item */
            _pt1Entry = archive.Header.Part1.GetEntry(id);
            _pt2Entry = archive.Header.Part2.GetEntry(_pt1Entry);
            _pt5Entry = archive.Header.Part5.GetEntry(id);
            try
            {
                _pt6Entry = archive.Header.Part6.GetEntry(id);
            }
            catch (Exception ex)
            {
                try
                {
                    /* Some items share a part 2 entry, so try to find the corresponding part 6 entry */
                    _pt6Entry = archive.Header.Part6.GetEntry(_pt2Entry.Id);
                }
                catch (Exception ex2)
                {
                    // TODO : Handle when an item doesn't have a part 6 entry
                    _pt6Entry = archive.Header.Part6.GetEntry(0);
                }
            }

            /* Determine item type */
            _type = (_pt1Entry.OffsetToData == 0)
                ? NefsItemType.Directory
                : NefsItemType.File;

            /* Get the filename */
            _fileName = archive.Header.Part3.GetFilename(_pt2Entry.FilenameOffset);

            /* Hash the filename */
            _fileNameHash = FilePathHelper.HashStringMD5(_fileName);

            /* Get offsets */
            _dataOffset       = _pt1Entry.OffsetToData;
            _offsetIntoPt2Raw = _pt1Entry.OffsetIntoPt2Raw;
            _offsetIntoPt4Raw = _pt1Entry.OffsetIntoPt4Raw;

            /* Get extracted size */
            _extractedSize = _pt2Entry.ExtractedSize;

            /*
             * Build the file path inside this archive
             * for example: "rootDir/childDir/file.xml".
             */
            _filePathInArchive = _fileName;
            var currentItem = _pt2Entry;

            /* The root directory's id is equal to its parent directory id */
            while (currentItem.Id != currentItem.DirectoryId)
            {
                var pt1Entry = archive.Header.Part1.GetEntry(currentItem.DirectoryId);
                var dir      = archive.Header.Part2.GetEntry(pt1Entry);
                var dirName  = archive.Header.Part3.GetFilename(dir.FilenameOffset);
                _filePathInArchive = Path.Combine(dirName, _filePathInArchive);

                currentItem = dir;
            }

            /* Hash the file path in archive */
            _filePathInArchiveHash = FilePathHelper.HashStringMD5(_filePathInArchive);

            //
            // Get the compressed file chunk offsets
            //
            if (_pt1Entry.OffsetIntoPt4Raw == 0xFFFFFFFF)
            {
                // TODO : Not sure exactly what this value means yet
                // For now, just set compressed size as extracted size with not compressed chunk sizes
                _compressedSize = ExtractedSize;
            }
            else
            {
                var numChunks = (UInt32)Math.Ceiling(ExtractedSize / (double)CHUNK_SIZE);
                if (numChunks > 0)
                {
                    var        firstChunkSizeEntry = _archive.Header.Part4.Offset + _pt1Entry.OffsetIntoPt4;
                    UInt32Type chunkOffset;

                    for (int i = 0; i < numChunks; i++)
                    {
                        chunkOffset = new UInt32Type(i * 4);
                        chunkOffset.Read(file, firstChunkSizeEntry);
                        _chunkSizes.Add(chunkOffset.Value);
                    }

                    _compressedSize = _chunkSizes.Last();
                }
            }
        }