예제 #1
0
 public void Write(byte version, byte[] key, CatalogueEntry entry)
 {
     Write(key);
     Write(entry.Offset);
     Write(entry.Size);
     if (version == 0x01)
     {
         Write(entry.Extra);
     }
     Write(entry.Archive);
 }
        public void WriteFileFromCatalogue(CatalogueEntry entry, string directory)
        {
            string compoundName = Path.GetDirectoryName(entry.Parent.Path) + @"\cas_" + entry.Archive.ToString("00") + ".cas";

            using (BinaryReader reader = new BinaryReader(File.Open(compoundName, FileMode.Open)))
            {
                reader.BaseStream.Seek(entry.Offset, SeekOrigin.Begin);

                string finalDirectory = directory + @"\cat\";
                string finalName      = finalDirectory + entry.ResolvedName;
                Directory.CreateDirectory(finalDirectory);

                ChunkHandler.Dechunk(finalName, reader, entry.Size);
            }
        }
        private void BuildCatalogueEntry(Catalogue catalogue, byte version)
        {
            byte[]         hash  = ReadBytes(20);
            CatalogueEntry entry = new CatalogueEntry();

            entry.Offset = ReadInt32();
            entry.Size   = ReadInt32();

            if (version == 0x01)
            {
                entry.Extra = ReadInt32();
            }

            entry.Archive = ReadInt32();

            catalogue.Files[hash] = entry;
            catalogue.FileHashes.Add(hash);
            entry.Parent       = catalogue;
            entry.ResolvedName = BitConverter.ToString(hash).Replace("-", string.Empty).ToLower();
        }
        private void UpdateManifest()
        {
            var            catalogue        = catalogues.Where(cat => cat.Files.ContainsKey(SessionManifestSha1)).First();
            CatalogueEntry manifestCatEntry = catalogue.Files[SessionManifestSha1];

            using (MemoryStream manifestMemoryStream = new MemoryStream())
            {
                using (ManifestBinaryWriter memoryManifestWriter = new ManifestBinaryWriter(manifestMemoryStream))
                {
                    memoryManifestWriter.Write(manifest);
                }

                byte[] manifestByteArray = manifestMemoryStream.ToArray();

                if (manifestByteArray.Length != manifestCatEntry.Size)
                {
                    throw new InvalidDataException("Incorrectly created manifest!");
                }

                byte[] updatedSha1 = CalculateSha1(manifestByteArray);

                using (BinaryWriter manifestWriter = new BinaryWriter(File.Open(ResolveManifestCasFile(), FileMode.Open)))
                {
                    manifestWriter.BaseStream.Seek(manifestCatEntry.Offset, SeekOrigin.Begin);
                    manifestWriter.Write(manifestByteArray);
                }

                using (BinaryWriter layoutWriter = new BinaryWriter(File.Open(BaseDirectory + LayoutTocPath, FileMode.Open)))
                {
                    layoutWriter.BaseStream.Seek(LayoutTocManifestSha1Location, SeekOrigin.Begin);
                    layoutWriter.Write(updatedSha1);
                }

                using (BinaryWriter catWriter = new BinaryWriter(File.Open(catalogue.Path, FileMode.Open)))
                {
                    catWriter.BaseStream.Seek(ManifestCatSha1Location, SeekOrigin.Begin);
                    catWriter.Write(updatedSha1);
                }
            }
        }