From cab28e6051fcb5e077a3f3627c3bfdd7897fff4a Mon Sep 17 00:00:00 2001 From: Chen Junsheng Date: Wed, 24 Apr 2024 02:48:09 +0800 Subject: [PATCH] Serialize to stream --- SteamKit2/SteamKit2/Types/DepotManifest.cs | 27 ++++++++++------------ 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/SteamKit2/SteamKit2/Types/DepotManifest.cs b/SteamKit2/SteamKit2/Types/DepotManifest.cs index cee52f14b..3499c2752 100644 --- a/SteamKit2/SteamKit2/Types/DepotManifest.cs +++ b/SteamKit2/SteamKit2/Types/DepotManifest.cs @@ -226,16 +226,16 @@ public bool DecryptFilenames(byte[] encryptionKey) /// true if serialization was successful; otherwise, false. 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; } /// @@ -390,10 +390,10 @@ public int GetHashCode( byte[] obj ) } /// - /// Serializes the depot manifest into a byte array. + /// Serializes the depot manifest into the provided output stream. /// - /// A byte array containing the serialized depot manifest. Returns null if serialization fails. - public byte[]? Serialize() + /// The stream to which the serialized depot manifest will be written. + public void Serialize( Stream output ) { DebugLog.Assert( Files != null, nameof( DepotManifest ), "Files was null when attempting to serialize manifest." ); @@ -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() ) @@ -496,8 +495,6 @@ public int GetHashCode( byte[] obj ) // Write EOF marker bw.Write( DepotManifest.PROTOBUF_ENDOFMANIFEST_MAGIC ); - - return ms.ToArray(); } } }