public void Serialize(SerializingFile con) { if (con.IsLoading) { _header = new DlcHeader(); } _header.Serialize(con); con.Seek((int)_header.EntryOffset, SeekOrigin.Begin); if (con.IsLoading) { DlcFiles = new DlcFileEntry[_header.FileCount]; } for (var i = 0; i < _header.FileCount; i++) { DlcFiles[i].Serialize(con, _header); } if (con.IsLoading) { ReadFileNames(con); } }
public void AddFileQuick(string filein, string path) { //EndianBitConverter.IsLittleEndian = true; var dlcPath = FileName; var fs = new FileStream(dlcPath, FileMode.OpenOrCreate, FileAccess.ReadWrite); var fileIn = File.ReadAllBytes(filein); //Create Entry var tmp = new List <DlcFileEntry>(DlcFiles); var e = new DlcFileEntry { FileName = path, BlockOffsets = new long[0], Hash = ComputeHash(path), BlockSizeIndex = 0xFFFFFFFF, UncompressedSize = (uint)fileIn.Length, UncompressedSizeAdder = 0 }; tmp.Add(e); //e = new DlcFileEntry(); DlcFiles = tmp.ToArray(); // //Find TOC var f = -1; for (var i = 0; i < _header.FileCount; i++) { //e = DlcFiles[i]; if (CompareByteArray(DlcFiles[i].Hash, TocHash)) { f = i; } } var m = DecompressEntry(f, fs); // //Update TOC WriteString(m, path); m.WriteByte(0xD); m.WriteByte(0xA); // //Append new FileTable var count = (int)_header.FileCount + 1; var oldsize = fs.Length; var offset = oldsize; fs.Seek(oldsize, 0); _header.EntryOffset = (uint)offset; for (var i = 0; i < count; i++) { e = DlcFiles[i]; fs.Write(e.Hash, 0, 16); fs.Write(_converter.GetBytes(e.BlockSizeIndex), 0, 4); fs.Write(_converter.GetBytes(e.UncompressedSize), 0, 4); fs.WriteByte(e.UncompressedSizeAdder); fs.Write(_converter.GetBytes(e.DataOffset), 0, 4); fs.WriteByte(e.DataOffsetAdder); } offset += count * 0x1E; _header.BlockTableOffset = (uint)offset; // //Append blocktable for (var i = 0; i < count; i++) { e = DlcFiles[i]; if (e.BlockSizeIndex != 0xFFFFFFFF && i != f) { foreach (var u in e.BlockSizes) { fs.Write(_converter.GetBytes(u), 0, 2); } } } // //Update Filetable with new Blockoffsets fs.Seek(oldsize, 0); uint blocksizeindex = 0; for (var i = 0; i < count; i++) { e = DlcFiles[i]; fs.Write(e.Hash, 0, 16); if (e.BlockSizeIndex == 0xFFFFFFFF) { fs.Write(_converter.GetBytes(e.BlockSizeIndex), 0, 4); } else { fs.Write(_converter.GetBytes(blocksizeindex), 0, 4); e.BlockSizeIndex = blocksizeindex; blocksizeindex += (uint)e.BlockSizes.Length; DlcFiles[i] = e; } fs.Write(_converter.GetBytes(e.UncompressedSize), 0, 4); fs.WriteByte(e.UncompressedSizeAdder); fs.Write(_converter.GetBytes(e.DataOffset), 0, 4); fs.WriteByte(e.DataOffsetAdder); } offset += blocksizeindex * 2; //offset of new data //append new file raw fs.Seek(offset, 0); fs.Write(fileIn, 0, fileIn.Length); // //update new entry fs.Seek(oldsize, 0); blocksizeindex = 0; for (var i = 0; i < count; i++) { e = DlcFiles[i]; fs.Write(e.Hash, 0, 16); if (i < count - 1) { if (e.BlockSizeIndex == 0xFFFFFFFF) { fs.Write(_converter.GetBytes(e.BlockSizeIndex), 0, 4); } else { fs.Write(_converter.GetBytes(blocksizeindex), 0, 4); blocksizeindex += (uint)e.BlockOffsets.Length; } } else { fs.Write(_converter.GetBytes(0xFFFFFFFF), 0, 4); } if (i < count - 1) { fs.Write(_converter.GetBytes(e.UncompressedSize), 0, 4); fs.WriteByte(e.UncompressedSizeAdder); fs.Write(_converter.GetBytes(e.DataOffset), 0, 4); fs.WriteByte(e.DataOffsetAdder); } else //new entry { if (offset <= 0xFFFFFFFF) { fs.Write(_converter.GetBytes(e.UncompressedSize), 0, 4); fs.WriteByte(e.UncompressedSizeAdder); fs.Write(_converter.GetBytes((int)offset), 0, 4); fs.WriteByte(0); e.DataOffset = (uint)offset; DlcFiles[i] = e; } else { fs.Write(_converter.GetBytes(e.UncompressedSize), 0, 4); fs.WriteByte(e.UncompressedSizeAdder); fs.Write(_converter.GetBytes((int)offset), 0, 4); fs.WriteByte((byte)((offset & 0xFF00000000) >> 32)); } } } offset += filein.Length; fs.Seek(offset, 0); // //Append TOC fs.Write(m.ToArray(), 0, (int)m.Length); // //update new entry fs.Seek(oldsize, 0); blocksizeindex = 0; for (var i = 0; i < count; i++) { e = DlcFiles[i]; fs.Write(e.Hash, 0, 16); if (i != f) { if (e.BlockSizeIndex == 0xFFFFFFFF) { fs.Write(_converter.GetBytes(e.BlockSizeIndex), 0, 4); } else { fs.Write(_converter.GetBytes(blocksizeindex), 0, 4); blocksizeindex += (uint)e.BlockSizes.Length; } } else { fs.Write(_converter.GetBytes(0xFFFFFFFF), 0, 4); } if (i != f) { fs.Write(_converter.GetBytes(e.UncompressedSize), 0, 4); fs.WriteByte(e.UncompressedSizeAdder); fs.Write(_converter.GetBytes(e.DataOffset), 0, 4); fs.WriteByte(e.DataOffsetAdder); } else //new entry { if (offset <= 0xFFFFFFFF) { fs.Write(_converter.GetBytes((uint)m.Length), 0, 4); fs.WriteByte(e.UncompressedSizeAdder); fs.Write(_converter.GetBytes((int)offset), 0, 4); fs.WriteByte(0); } else { fs.Write(_converter.GetBytes((uint)m.Length), 0, 4); fs.WriteByte(e.UncompressedSizeAdder); fs.Write(_converter.GetBytes((int)offset), 0, 4); fs.WriteByte((byte)((offset & 0xFF00000000) >> 32)); } } } //Update Header fs.Seek(0xC, 0); fs.Write(_converter.GetBytes(_header.EntryOffset), 0, 4); fs.Write(_converter.GetBytes(count), 0, 4); fs.Write(_converter.GetBytes(_header.BlockTableOffset), 0, 4); // fs.Close(); }