Skip to content

Commit

Permalink
Serialize to stream
Browse files Browse the repository at this point in the history
  • Loading branch information
skylayer committed Apr 23, 2024
1 parent 0f34aea commit cab28e6
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions SteamKit2/SteamKit2/Types/DepotManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,16 @@ public bool DecryptFilenames(byte[] encryptionKey)
/// <returns><c>true</c> if serialization was successful; otherwise, <c>false</c>.</returns>
public bool SaveToFile( string filename )
{
using var fs = File.Open( filename, FileMode.Create );
using var bw = new BinaryWriter( fs );
var data = Serialize();
if ( data != null )
try
{
bw.Write( data );
return true;
using var fs = File.Open( filename, FileMode.Create );
Serialize( fs ); // Directly pass the FileStream to the Serialize method
return true; // If serialization completes without throwing an exception, return true
}
catch ( Exception )
{
return false; // Return false if an error occurs
}

return false;
}

/// <summary>
Expand Down Expand Up @@ -390,10 +390,10 @@ public int GetHashCode( byte[] obj )
}

/// <summary>
/// Serializes the depot manifest into a byte array.
/// Serializes the depot manifest into the provided output stream.
/// </summary>
/// <returns>A byte array containing the serialized depot manifest. Returns <c>null</c> if serialization fails.</returns>
public byte[]? Serialize()
/// <param name="output">The stream to which the serialized depot manifest will be written.</param>
public void Serialize( Stream output )
{
DebugLog.Assert( Files != null, nameof( DepotManifest ), "Files was null when attempting to serialize manifest." );

Expand Down Expand Up @@ -469,8 +469,7 @@ public int GetHashCode( byte[] obj )
}
}

using var ms = new MemoryStream();
using var bw = new BinaryWriter( ms );
using var bw = new BinaryWriter( output, Encoding.Default, true );

// Write Protobuf payload
using ( var ms_payload = new MemoryStream() )
Expand All @@ -496,8 +495,6 @@ public int GetHashCode( byte[] obj )

// Write EOF marker
bw.Write( DepotManifest.PROTOBUF_ENDOFMANIFEST_MAGIC );

return ms.ToArray();
}
}
}

0 comments on commit cab28e6

Please sign in to comment.