示例#1
0
        public override byte[] GetBinaryFile(string fileName, bool cache = false)
        {
            if (ArchiveMap != null)
            {
                lock (BinaryFileLock)
                {
                    FI fi = ArchiveMap.FindString(ref fileName, out int size);
                    if (!string.IsNullOrWhiteSpace(fileName))
                    {
                        if (LocalTryGetValue(fileName, out BufferWithAge value))
                        {
                            Memory.Log.WriteLine($"{nameof(ArchiveZzz)}::{nameof(GetBinaryFile)}::{nameof(CacheTryGetValue)} read from cache {fileName}");
                            return(value);
                        }
                        else
                        {
                            Stream s;
                            if ((s = OpenStream()) != null)
                            {
                                byte[] buffer = ArchiveMap.GetBinaryFile(fi, s, fileName, size);
                                if (buffer != null && cache && LocalTryAdd(fileName, buffer))
                                {
                                    Memory.Log.WriteLine($"{nameof(ArchiveZzz)}::{nameof(GetBinaryFile)}::{nameof(LocalTryAdd)} caching {fileName}");
                                }

                                return(buffer);
                            }
                        }
                    }
                }
            }
            Memory.Log.WriteLine($"{nameof(ArchiveZzz)}::{nameof(GetBinaryFile)} FAILED extracting {fileName}");
            return(null);
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="fi">remove me?</param>
        /// <param name="size">remove me?</param>
        /// <returns></returns>
        // ReSharper disable once RedundantAssignment
        public override StreamWithRangeValues GetStreamWithRangeValues(string filename)
        {
            if (ArchiveMap == null)
            {
                return(null);
            }
            FI fi = ArchiveMap.FindString(ref filename, out int size);

            return(!string.IsNullOrWhiteSpace(filename) && fi != null
                ? new StreamWithRangeValues(OpenStream(), fi.Offset, size, fi.CompressionType, fi.UncompressedSize)
                : null);
        }
示例#3
0
        /// <summary>
        /// GetCompressedData reads data from file directly. This isn't used right now I tried to add checks but they aren't tested yet.
        /// </summary>
        /// <param name="fi"></param>
        /// <param name="size"></param>
        /// <param name="skipData"></param>
        /// <returns></returns>
        private byte[] GetCompressedData(FI fi, out int size, bool skipData = false)
        {
            byte[]     temp = null;
            FileStream fs;

            using (var br = new BinaryReader(fs = new FileStream(Archive.FS, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
            {
                br.BaseStream.Seek(fi.Offset, SeekOrigin.Begin);
                var compressedSize = 0;
                var max            = (int)(fs.Length - fs.Position);
                switch (fi.CompressionType)
                {
                case CompressionType.None:
                case CompressionType.LZSS_UnknownSize:
                    compressedSize = fi.UncompressedSize;
                    break;

                case CompressionType.LZSS:
                case CompressionType.LZSS_LZSS:
                    if (fs.Length < 5)
                    {
                        throw new InvalidDataException();
                    }
                    compressedSize = br.ReadInt32();
                    if (compressedSize > (max -= sizeof(int)))
                    {
                        throw new InvalidDataException();
                    }
                    break;

                case CompressionType.LZ4:
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                // CompressionType = LZS then Compressed Size is defined. Else the Uncompressed size should work for
                size = compressedSize > 0 ? compressedSize : fi.UncompressedSize > max ? max : fi.UncompressedSize;
                if (size > max)
                {
                    throw new InvalidDataException(
                              $"{nameof(ArchiveWorker)}::{nameof(GetCompressedData)} Expected size ({size}) > ({max})");
                }
                if (!skipData)
                {
                    temp = br.ReadBytes(size);
                }
            }
            return(temp);
        }
示例#4
0
        private StreamWithRangeValues GetStreamWithRangeValues(string fileName, FI inputFi, int size)
        {
            void msg() =>
            Memory.Log.WriteLine($"{nameof(ArchiveWorker)}::{nameof(GetStreamWithRangeValues)} stream: {fileName}");

            if (inputFi != null)
            {
            }

            if (Archive == null)
            {
                return(null);
            }
            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new FileNotFoundException("NO FILENAME");
            }
            if (IsDir)
            {
                FindFile(ref fileName);
                msg();
                var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                if (inputFi == null)
                {
                    return(new StreamWithRangeValues(fs, 0, fs.Length));
                }
                return(new StreamWithRangeValues(fs, inputFi.Offset, size, inputFi.CompressionType, inputFi.UncompressedSize));
            }

            Debug.Assert(inputFi == null);
            var parentFs = Archive.FS;

            if (ArchiveMap != null && ArchiveMap.Count > 1)
            {
                var fi = ArchiveMap.FindString(ref fileName, out size);
                msg();
                if (FsArchive == null)
                {
                    return(null);
                }
                if (fi == null)
                {
                    return(FsArchive.GetStreamWithRangeValues(fileName));
                }
                var parentFi = FsArchive.ArchiveMap?.FindString(ref parentFs, out var _);
                return(parentFi == null || parentFi.CompressionType == 0 || (FsArchive is ArchiveZzz)
                    ? new StreamWithRangeValues(FsArchive.GetStreamWithRangeValues(parentFs), fi.Offset, size,
                                                fi.CompressionType, fi.UncompressedSize)
                    : new StreamWithRangeValues(new MemoryStream(FsArchive.GetBinaryFile(parentFs, true), false),
                                                fi.Offset, size, fi.CompressionType, fi.UncompressedSize));
            }

            var loc = -1;

            if (File.Exists(Archive.FL))
            {
                using (var fs = new FileStream(Archive.FL, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    loc = FindFile(ref fileName, fs); //File.OpenRead(archive.FL));
            }
            msg();
            // read file list

            if (loc == -1)
            {
                Debug.WriteLine($"ArchiveWorker: NO SUCH FILE! :: {Archive.FL}");
                //throw new Exception("ArchiveWorker: No such file!");
            }
            else
            {
                var fi = GetFi(loc);
                GetCompressedData(fi, out size, true);
                return(new StreamWithRangeValues(new FileStream(parentFs, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), fi.Offset, size, fi.CompressionType, fi.UncompressedSize));
            }

            Debug.WriteLine($"ArchiveWorker: NO SUCH FILE! :: Searched {Archive} and could not find {fileName}");
            return(null);
        }
示例#5
0
        private byte[] GetBinaryFile(string fileName, int loc, bool cache)
        {
            fileName = FileList.FirstOrDefault(x => x.IndexOf(fileName, StringComparison.OrdinalIgnoreCase) >= 0);
            if (LocalTryGetValue(fileName, out BufferWithAge b))
            {
                Memory.Log.WriteLine($"{nameof(ArchiveWorker)}::{nameof(GetBinaryFile)} :: read from cache: {fileName}");
                return(b);
            }
            if (IsDir)
            {
                if (FileList == null || FileList.Length == 0)
                {
                    ProduceFileLists();
                }
                if (!string.IsNullOrWhiteSpace(fileName))
                {
                    using (BinaryReader br = new BinaryReader(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
                    {
                        Memory.Log.WriteLine($"{nameof(ArchiveWorker)}::{nameof(GetBinaryFile)} :: reading: {fileName}");
                        byte[] buffer = br.ReadBytes(checked ((int)br.BaseStream.Length));
                        if (cache && LocalTryAdd(fileName, buffer))
                        {
                            Memory.Log.WriteLine($"{nameof(ArchiveWorker)}::{nameof(GetBinaryFile)} :: cached: {fileName}");
                        }
                        return(buffer);
                    }
                }
            }
            //read index data

            FI fi = GetFi(loc);

            //read binary data.
            byte[] temp = GetCompressedData(fi, out int _);

            Memory.Log.WriteLine($"{nameof(ArchiveWorker)}::{nameof(GetBinaryFile)} :: extracting: {fileName}");

            if (temp != null)
            {
                switch (fi.CompressionType)
                {
                case CompressionType.None:
                    break;

                case CompressionType.LZSS:
                    LZSS.DecompressAllNew(temp, fi.UncompressedSize);
                    break;

                case CompressionType.LZ4:
                    temp = ArchiveMap.Lz4Uncompress(temp, fi.UncompressedSize);
                    break;

                case CompressionType.LZSS_UnknownSize:
                    LZSS.DecompressAllNew(temp, 0);
                    break;

                case CompressionType.LZSS_LZSS:
                    LZSS.DecompressAllNew(LZSS.DecompressAllNew(temp, fi.UncompressedSize), 0);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            if (temp != null && cache && LocalTryAdd(fileName, temp))
            {
                Memory.Log.WriteLine($"{nameof(ArchiveWorker)}::{nameof(GetBinaryFile)} :: cached: {fileName}");
            }
            return(temp);
        }