Exemple #1
0
        /// <summary>
        /// Opens the files and reads the contents of the package.
        /// </summary>
        public void Load(AsyncLoadPackageCallback cb)
        {
            manifestContents = TakeOwnership(ManifestReference, LoadManifest(manifestFile, p => cb(p * 0.1f)));
            packageContents  = TakeOwnership(PackageReference, LoadPackage(packageFile, p => cb(p * 0.9f + 0.1f)));

            isPackageLoaded = true;
        }
Exemple #2
0
        private static List <Entry> LoadPackage(string dataFile, AsyncLoadPackageCallback cb)
        {
            using (var fs = File.OpenRead(dataFile)) {
                var contents   = new List <Entry>(1024);
                var readBuffer = new byte[CHUNK_SIZE];

                byte[] compressionBuffer = null;
                bool   isCompressed      = false;

                // Read the header of the package
                int packageHeader = fs.ReadInt32BE();
                int packageLength = readBuffer.Length - 4;
                if ((packageHeader & COMPRESSION_FLAG) != 0)
                {
                    packageHeader    &= ~COMPRESSION_FLAG;
                    isCompressed      = true;
                    compressionBuffer = new byte[COMP_BUFFER_SIZE];
                }
                if (packageHeader != PACKAGE_VERSION_CODE_TRANSISTOR_PYRE &&
                    packageHeader != PACKAGE_VERSION_CODE_HADES)
                {
                    throw new PackageReadException(string.Format(ERR_PACKAGE_VERSION, packageHeader, dataFile));
                }

                LZF decompressor = new LZF();
                // Read all chunks in the file
                ReadStatus readStatus;
                do
                {
                    float from = fs.Position / (float)fs.Length;
                    int   chunkSize;
                    if (isCompressed && fs.ReadByte() != 0)
                    {
                        int num2 = fs.ReadInt32BE();
                        fs.Read(compressionBuffer, 0, num2);
                        chunkSize = decompressor.Decompress(compressionBuffer, num2, readBuffer, readBuffer.Length);
                    }
                    else
                    {
                        chunkSize = fs.Read(readBuffer, 0, packageLength);
                    }
                    float to = fs.Position / (float)fs.Length;

                    packageLength = readBuffer.Length;
                    var chunk = new MemoryStream(readBuffer, 0, chunkSize, false);
                    do
                    {
                        var e = ReadPackageEntry(chunk, out readStatus);
                        if (e != null)
                        {
                            contents.Add(e);
                        }
                        cb(from + (to - from) * (chunk.Position / (float)chunk.Length));
                    } while (readStatus != ReadStatus.EndOfChunk && readStatus != ReadStatus.EndOfFile);
                } while (readStatus == ReadStatus.EndOfChunk);

                return(contents);
            }
        }
Exemple #3
0
        /// <summary>
        /// Opens and loads all contents of the package manifest file.
        /// </summary>
        private static List <Entry> LoadManifest(string manifestFile, AsyncLoadPackageCallback cb)
        {
            // Open the file we're going to read.
            using (var fs = File.OpenRead(manifestFile)) {
                var contents   = new List <Entry>(1024);
                var readBuffer = new byte[CHUNK_SIZE];

                // Read the header of the manifest
                int manifestHeader = fs.ReadInt32BE();
                int manifestLength = readBuffer.Length - 4;
                if ((manifestHeader & LZF_COMPRESSION_FLAG) != 0)
                {
                    throw new PackageReadException(string.Format(ERR_COMPRESSED_MANIFEST, manifestFile));
                }

                int manifestVersion = manifestHeader;
                if (manifestVersion != PACKAGE_VERSION_CODE_TRANSISTOR_PYRE &&
                    manifestVersion != PACKAGE_VERSION_CODE_HADES &&
                    manifestVersion != PACKAGE_VERSION_CODE_HADES_NEW)
                {
                    throw new PackageReadException(string.Format(ERR_PACKAGE_VERSION, manifestHeader, manifestFile));
                }

                // Read all chunks in the file
                ReadStatus readStatus;
                do
                {
                    int chunkSize = fs.Read(readBuffer, 0, manifestLength);
                    manifestLength = readBuffer.Length;
                    var chunk = new MemoryStream(readBuffer, 0, chunkSize, false);

                    // Read all entries in the chunk
                    do
                    {
                        var e = ReadManifestEntry(chunk, out readStatus);
                        if (e != null)
                        {
                            contents.Add(e);
                        }
                        cb(fs.Position / (float)fs.Length);
                    } while (readStatus != ReadStatus.EndOfChunk && readStatus != ReadStatus.EndOfFile);
                } while (readStatus == ReadStatus.EndOfChunk);

                return(contents);
            }
        }