public static AFS_Directory_Entry FromStream(FileStream stream) { var entry = new AFS_Directory_Entry(); entry.filename = BinaryHelper.ReadString(stream, 32); entry.year = BinaryHelper.ReadUInt16(stream); entry.month = BinaryHelper.ReadUInt16(stream); entry.day = BinaryHelper.ReadUInt16(stream); entry.hour = BinaryHelper.ReadUInt16(stream); entry.minute = BinaryHelper.ReadUInt16(stream); entry.second = BinaryHelper.ReadUInt16(stream); entry.fileLength = BinaryHelper.ReadUInt32(stream); return(entry); }
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); }
public void WriteStream(FileStream stream) { BinaryHelper.WriteUInt32(stream, offset); BinaryHelper.WriteUInt32(stream, length); }
public void WriteStream(FileStream stream) { var padding_size = 2048; // header BinaryHelper.WriteString(stream, header, 4); BinaryHelper.WriteUInt32(stream, numFiles); // table of contents for (var i = 0; i < numFiles; ++i) { var toc_entry = tableOfContents[i]; toc_entry.WriteStream(stream); } // padding (table of contents is 512 KB) var tableOfContentsSize = padding_size * 256; BinaryHelper.WriteAFSPadding(stream, stream.Position, tableOfContentsSize); // directory information is at the very end of the table of contents block, past the padding stream.Position -= 8; BinaryHelper.WriteUInt32(stream, filenameDirectoryOffset); BinaryHelper.WriteUInt32(stream, filenameDirectoryLength); // data (will need to update ToC entry's from here) for (var i = 0; i < numFiles; ++i) { var toc_entry = tableOfContents[i]; toc_entry.offset = (uint)stream.Position; var afs_file = files[i]; afs_file.WriteStream(stream); // padding BinaryHelper.WriteAFSPadding(stream, stream.Position, padding_size); } filenameDirectoryOffset = (uint)stream.Position; // directory for (var i = 0; i < numFiles; ++i) { var dir_entry = directory[i]; dir_entry.WriteStream(stream); } filenameDirectoryLength = (uint)(stream.Position - filenameDirectoryOffset); // padding BinaryHelper.WriteAFSPadding(stream, stream.Position, padding_size); // go back and re-write the table of contents, with the updated information stream.Position = 8; // header = 4, numfiles = 4 // table of contents for (var i = 0; i < numFiles; ++i) { var toc_entry = tableOfContents[i]; toc_entry.WriteStream(stream); } BinaryHelper.WriteUInt32(stream, filenameDirectoryOffset); BinaryHelper.WriteUInt32(stream, filenameDirectoryLength); }