/// <summary> /// Saves the EncodingFile to disk and optionally updates the BuildConfig /// </summary> /// <param name="directory">Root Directory</param> /// <param name="configContainer"></param> /// <returns></returns> public CASRecord Write(string directory, Configs.ConfigContainer configContainer = null) { if (Partial) { throw new NotSupportedException("Writing is not supported for partial EncodingFiles"); } EBlock[] eblocks = new EBlock[_EncodingMap.Length]; CASRecord record; using (var bt = new BlockTableStreamWriter(_EncodingMap[1], 1)) using (var bw = new BinaryWriter(bt)) { // ESpecStringTable 1 bt.Write(string.Join('\0', ESpecStringTable).GetBytes()); EncodingHeader.ESpecTableSize = (uint)bt.Length; // CKeysPageIndices 2, CKeysPageTable 3 WritePage(bw, eblocks, 2, EncodingHeader.CKeyPageSize << 10, _CKeyEntries); // EKeysPageIndices 4, EKeysPageTable 5 WritePage(bw, eblocks, 4, EncodingHeader.EKeyPageSize << 10, _EKeyEntries); // Header 0 bt.AddBlock(_EncodingMap[0], 0); EncodingHeader.Write(bw); // File ESpec 6 bt.AddBlock(_EncodingMap[6], 6); bt.Write(GetFileESpec(bt.SubStreams).GetBytes()); // finalise record = bt.Finalise(); // save string saveLocation = Helpers.GetCDNPath(record.EKey.ToString(), "data", directory, true); using (var fs = File.Create(saveLocation)) { bt.WriteTo(fs); record.BLTEPath = saveLocation; } } // update the build config with the new values if (configContainer?.BuildConfig != null) { configContainer.BuildConfig.SetValue("encoding-size", record.EBlock.DecompressedSize, 0); configContainer.BuildConfig.SetValue("encoding-size", record.EBlock.CompressedSize, 1); configContainer.BuildConfig.SetValue("encoding", record.CKey, 0); configContainer.BuildConfig.SetValue("encoding", record.EKey, 1); } Checksum = record.CKey; return(record); }
private void WriteFileEntries(BlockTableStreamWriter bt) { var size = _FileEntries.Count * (DownloadSizeHeader.EKeySize + 4); using var ms = new MemoryStream(size); using var bw = new BinaryWriter(ms); // ordered by descending size var fileEntries = _FileEntries.Values.OrderByDescending(x => x.CompressedSize); foreach (var fileEntry in fileEntries) { fileEntry.Write(bw, DownloadSizeHeader); } // batched into 0xFFFF size uncompressed blocks // this is for client performance and isn't mandatory ms.Position = 0; byte[] buffer = ArrayPool <byte> .Shared.Rent(0xFFFF); int read; while ((read = ms.Read(buffer)) != 0) { bt.AddBlock(_EncodingMap[2]); bt.Write(buffer, 0, read); } ArrayPool <byte> .Shared.Return(buffer); }
private void WriteFileEntries(BlockTableStreamWriter bt) { using var ms = new MemoryStream(); using var bw = new BinaryWriter(ms); // ordered by descending size foreach (var fileEntry in _FileEntries.Values.OrderByDescending(x => x.CompressedSize)) { fileEntry.Write(bw, DownloadSizeHeader); } // batched into 0xFFFF size uncompressed blocks // this is for client performance and isn't mandatory ms.Position = 0; byte[] buffer = new byte[0xFFFF]; int read; while ((read = ms.Read(buffer)) != 0) { bt.AddBlock(_EncodingMap[2]); bt.Write(buffer, 0, read); } }