Пример #1
0
        private AFS ExtractFromAFS(string input, string output)
        {
            using (var reader = File.OpenRead(input))
            {
                var archive = AFS.FromStream(reader);

                Log($"Extracted AFS!");
                Log($"header: {archive.header}");
                Log($"numFiles: {archive.numFiles}");

                if (!Directory.Exists(output))
                {
                    Directory.CreateDirectory(output);
                }

                for (var i = 0; i < archive.numFiles; ++i)
                {
                    var toc_entry       = archive.tableOfContents[i];
                    var directory_entry = archive.directory[i];
                    var file            = archive.files[i];

                    var fullFilename = string.Format("{0}/{1}", output, directory_entry.filename);

                    using (var writer = File.OpenWrite(fullFilename))
                    {
                        writer.Write(file.data, 0, file.data.Length);
                    }
                }

                return(archive);
            }
        }
        private void RebuildAFS(AFS afs, string output_afs)
        {
            Log("Saving AFS.");

            using (var writer = File.OpenWrite(output_afs))
            {
                afs.WriteStream(writer);
            }

            Log("Saved AFS.");
        }
        private bool ReinjectInAFS(AFS afs, string input_file)
        {
            var shortFilename = input_file;

            while (shortFilename.IndexOf('/') >= 0)
            {
                var until       = shortFilename.IndexOf('/');
                var start_index = until + 1;
                var delta       = shortFilename.Length - start_index;
                shortFilename = shortFilename.Substring(start_index, delta);
            }

            Log($"reinjecting into {shortFilename}");

            var directory_index = -1;

            for (var i = 0; i < afs.directory.Length; ++i)
            {
                var entry = afs.directory[i];
                if (entry.filename == shortFilename)
                {
                    directory_index = i;
                    break;
                }
            }

            if (directory_index == -1)
            {
                return(false);
            }

            // read in file to the block at directory_index
            using (var reader = File.OpenRead(input_file))
            {
                var afs_file = afs.files[directory_index];
                afs_file.data = new byte[reader.Length];
                reader.Read(afs_file.data, 0, afs_file.data.Length);

                // update lengths
                var toc_entry = afs.tableOfContents[directory_index];
                toc_entry.length = (uint)afs_file.data.Length;

                var directory_entry = afs.directory[directory_index];
                directory_entry.fileLength = (uint)afs_file.data.Length;
            }

            return(true);
        }
Пример #4
0
        public static AFS FromStream(FileStream stream)
        {
            var afs = new AFS();

            afs.header   = BinaryHelper.ReadString(stream, 4);
            afs.numFiles = BinaryHelper.ReadUInt32(stream);

            afs.tableOfContents = new AFS_TOC_Entry[afs.numFiles];

            for (var i = 0; i < afs.numFiles; ++i)
            {
                var toc_entry = AFS_TOC_Entry.FromStream(stream);
                afs.tableOfContents[i] = toc_entry;
            }

            afs.filenameDirectoryOffset = BinaryHelper.ReadUInt32(stream);
            afs.filenameDirectoryLength = BinaryHelper.ReadUInt32(stream);

            afs.files = new AFS_File[afs.numFiles];
            for (var i = 0; i < afs.numFiles; ++i)
            {
                var toc_entry = afs.tableOfContents[i];
                var file      = AFS_File.FromStream(stream, toc_entry.offset, toc_entry.length);
                afs.files[i] = file;
            }

            afs.directory = new AFS_Directory_Entry[afs.numFiles];

            // hmm
            var dataBlockStart             = afs.tableOfContents[0].offset;
            var directoryOffsetStreamStart = dataBlockStart - 8;

            stream.Position = directoryOffsetStreamStart;
            stream.Position = BinaryHelper.ReadUInt32(stream);

            // HMMM
            // stream.Position = afs.filenameDirectoryOffset;

            for (var i = 0; i < afs.numFiles; ++i)
            {
                var toc_entry       = afs.tableOfContents[i];
                var directory_entry = AFS_Directory_Entry.FromStream(stream);
                afs.directory[i] = directory_entry;
            }

            return(afs);
        }