Skip to content

Commit f9777a3

Browse files
authored
Merge pull request #1337 from skylayer/master
2 parents 05511f5 + 00fec82 commit f9777a3

File tree

1 file changed

+26
-29
lines changed

1 file changed

+26
-29
lines changed

SteamKit2/SteamKit2/Types/DepotManifest.cs

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -219,25 +219,6 @@ public bool DecryptFilenames(byte[] encryptionKey)
219219
return true;
220220
}
221221

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-
241222
/// <summary>
242223
/// Loads binary manifest from a file and deserializes it.
243224
/// </summary>
@@ -371,12 +352,34 @@ void ParseProtobufManifestMetadata(ContentManifestMetadata metadata)
371352
EncryptedCRC = metadata.crc_encrypted;
372353
}
373354

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 )
375378
{
376379
DebugLog.Assert( Files != null, nameof( DepotManifest ), "Files was null when attempting to serialize manifest." );
377380

378381
var payload = new ContentManifestPayload();
379-
var uniqueChunks = new List<byte[]>();
382+
var uniqueChunks = new HashSet<byte[]>( new ChunkIdComparer() );
380383

381384
foreach ( var file in Files )
382385
{
@@ -409,10 +412,7 @@ void ParseProtobufManifestMetadata(ContentManifestMetadata metadata)
409412
protochunk.cb_compressed = chunk.CompressedLength;
410413

411414
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! );
416416
}
417417

418418
payload.mappings.Add( protofile );
@@ -450,8 +450,7 @@ void ParseProtobufManifestMetadata(ContentManifestMetadata metadata)
450450
}
451451
}
452452

453-
using var ms = new MemoryStream();
454-
using var bw = new BinaryWriter( ms );
453+
using var bw = new BinaryWriter( output, Encoding.Default, true );
455454

456455
// Write Protobuf payload
457456
using ( var ms_payload = new MemoryStream() )
@@ -477,8 +476,6 @@ void ParseProtobufManifestMetadata(ContentManifestMetadata metadata)
477476

478477
// Write EOF marker
479478
bw.Write( DepotManifest.PROTOBUF_ENDOFMANIFEST_MAGIC );
480-
481-
return ms.ToArray();
482479
}
483480
}
484481
}

0 commit comments

Comments
 (0)