@@ -186,7 +186,7 @@ public void Serialize(string path, CompressionType infoPacker = CompressionType.
186186 Serialize ( new AssetWriter ( path ) , infoPacker , dataPacker ) ;
187187 }
188188
189- public void Serialize ( AssetWriter writer , CompressionType infoPacker = CompressionType . None , CompressionType dataPacker = CompressionType . None )
189+ public void Serialize ( AssetWriter writer , CompressionType infoPacker = CompressionType . None , CompressionType dataPacker = CompressionType . None , string ? unityCNKey = null )
190190 {
191191 if ( Header == null || DataInfo == null || CabFiles == null )
192192 {
@@ -226,26 +226,31 @@ public void Serialize(AssetWriter writer, CompressionType infoPacker = Compressi
226226 var blocksSize = cabStream . Length ;
227227 byte [ ] uncompressedData = cabStream . ToArray ( ) ;
228228 using MemoryStream compressedStream = new MemoryStream ( ) ;
229- if ( dataPacker == CompressionType . Lzma )
229+ var defaultChunkSize = dataPacker == CompressionType . Lzma ? UInt32 . MaxValue : Setting . DefaultChunkSize ;
230+ offset = 0 ;
231+ while ( blocksSize > 0 )
230232 {
231- var compressedSize = Compression . CompressStream ( uncompressedData . AsSpan ( ) , compressedStream , dataPacker ) ;
232- blocksInfo . Add ( new StorageBlockInfo ( ( uint ) uncompressedData . Length , ( UInt32 ) compressedSize , ( StorageBlockFlags ) dataPacker ) ) ;
233+ int chunkSize = ( int ) Math . Min ( blocksSize , defaultChunkSize ) ;
234+ var compressedSize = Compression . CompressStream ( uncompressedData . AsSpan ( ( int ) offset , chunkSize ) , compressedStream , dataPacker ) ;
235+ blocksInfo . Add ( new StorageBlockInfo ( ( UInt32 ) chunkSize , ( UInt32 ) compressedSize , ( StorageBlockFlags ) dataPacker ) ) ;
236+ blocksSize -= chunkSize ;
237+ offset += chunkSize ;
233238 }
234- else
239+ compressedStream . Position = 0 ;
240+
241+ UnityCN ? unityCn = null ;
242+ if ( unityCNKey != null )
235243 {
236- offset = 0 ;
237- while ( blocksSize > 0x00020000 )
244+ unityCn = new UnityCN ( unityCNKey ) ;
245+ if ( dataPacker == CompressionType . Lz4 || dataPacker == CompressionType . Lz4HC )
238246 {
239- var compressedSize = Compression . CompressStream ( uncompressedData . AsSpan ( ( int ) offset , 0x00020000 ) , compressedStream , dataPacker ) ;
240- blocksInfo . Add ( new StorageBlockInfo ( 0x00020000 , ( UInt32 ) compressedSize , ( StorageBlockFlags ) dataPacker ) ) ;
241- blocksSize -= 0x00020000 ;
242- offset += 0x00020000 ;
243247 }
244- var finalCompressedSize = Compression . CompressStream ( uncompressedData . AsSpan ( ( int ) offset , ( int ) blocksSize ) , compressedStream , dataPacker ) ;
245- blocksInfo . Add ( new StorageBlockInfo ( ( UInt32 ) blocksSize , ( UInt32 ) finalCompressedSize , ( StorageBlockFlags ) dataPacker ) ) ;
248+ else
249+ {
250+ throw new Exception ( $ "UnityCN Encryption only support Lz4/Lz4HC, but { dataPacker } was set.") ;
251+ }
246252 }
247253
248- compressedStream . Position = 0 ;
249254 var dataInfo = new BlocksAndCabsInfo ( DataInfo . UncompressedDataHash , blocksInfo , directoryInfo ) ;
250255 using MemoryStream dataInfoStream = new MemoryStream ( ) ;
251256 AssetWriter dataInfoWriter = new AssetWriter ( dataInfoStream ) ;
0 commit comments