@@ -219,25 +219,6 @@ public bool DecryptFilenames(byte[] encryptionKey)
219
219
return true ;
220
220
}
221
221
222
- /// <summary>
223
- /// Serializes depot manifest and saves the output to a file.
224
- /// </summary>
225
- /// <param name="filename">Output file name.</param>
226
- /// <returns><c>true</c> if serialization was successful; otherwise, <c>false</c>.</returns>
227
- public bool SaveToFile ( string filename )
228
- {
229
- using var fs = File . Open ( filename , FileMode . Create ) ;
230
- using var bw = new BinaryWriter ( fs ) ;
231
- var data = Serialize ( ) ;
232
- if ( data != null )
233
- {
234
- bw . Write ( data ) ;
235
- return true ;
236
- }
237
-
238
- return false ;
239
- }
240
-
241
222
/// <summary>
242
223
/// Loads binary manifest from a file and deserializes it.
243
224
/// </summary>
@@ -371,12 +352,34 @@ void ParseProtobufManifestMetadata(ContentManifestMetadata metadata)
371
352
EncryptedCRC = metadata . crc_encrypted ;
372
353
}
373
354
374
- byte [ ] ? Serialize ( )
355
+ class ChunkIdComparer : IEqualityComparer < byte [ ] >
356
+ {
357
+ public bool Equals ( byte [ ] ? x , byte [ ] ? y )
358
+ {
359
+ if ( ReferenceEquals ( x , y ) ) return true ;
360
+ if ( x == null || y == null ) return false ;
361
+ return x . SequenceEqual ( y ) ;
362
+ }
363
+
364
+ public int GetHashCode ( byte [ ] obj )
365
+ {
366
+ ArgumentNullException . ThrowIfNull ( obj ) ;
367
+
368
+ // ChunkID is SHA-1, so we can just use the first 4 bytes
369
+ return BitConverter . ToInt32 ( obj , 0 ) ;
370
+ }
371
+ }
372
+
373
+ /// <summary>
374
+ /// Serializes the depot manifest into the provided output stream.
375
+ /// </summary>
376
+ /// <param name="output">The stream to which the serialized depot manifest will be written.</param>
377
+ public void Serialize ( Stream output )
375
378
{
376
379
DebugLog . Assert ( Files != null , nameof ( DepotManifest ) , "Files was null when attempting to serialize manifest." ) ;
377
380
378
381
var payload = new ContentManifestPayload ( ) ;
379
- var uniqueChunks = new List < byte [ ] > ( ) ;
382
+ var uniqueChunks = new HashSet < byte [ ] > ( new ChunkIdComparer ( ) ) ;
380
383
381
384
foreach ( var file in Files )
382
385
{
@@ -409,10 +412,7 @@ void ParseProtobufManifestMetadata(ContentManifestMetadata metadata)
409
412
protochunk . cb_compressed = chunk . CompressedLength ;
410
413
411
414
protofile . chunks . Add ( protochunk ) ;
412
- if ( ! uniqueChunks . Exists ( x => x . SequenceEqual ( chunk . ChunkID ! ) ) )
413
- {
414
- uniqueChunks . Add ( chunk . ChunkID ! ) ;
415
- }
415
+ uniqueChunks . Add ( chunk . ChunkID ! ) ;
416
416
}
417
417
418
418
payload . mappings . Add ( protofile ) ;
@@ -450,8 +450,7 @@ void ParseProtobufManifestMetadata(ContentManifestMetadata metadata)
450
450
}
451
451
}
452
452
453
- using var ms = new MemoryStream ( ) ;
454
- using var bw = new BinaryWriter ( ms ) ;
453
+ using var bw = new BinaryWriter ( output , Encoding . Default , true ) ;
455
454
456
455
// Write Protobuf payload
457
456
using ( var ms_payload = new MemoryStream ( ) )
@@ -477,8 +476,6 @@ void ParseProtobufManifestMetadata(ContentManifestMetadata metadata)
477
476
478
477
// Write EOF marker
479
478
bw . Write ( DepotManifest . PROTOBUF_ENDOFMANIFEST_MAGIC ) ;
480
-
481
- return ms . ToArray ( ) ;
482
479
}
483
480
}
484
481
}
0 commit comments