diff --git a/Facepunch.Steamworks/Structs/UgcItem.cs b/Facepunch.Steamworks/Structs/UgcItem.cs index 820a581..e0dbb6d 100644 --- a/Facepunch.Steamworks/Structs/UgcItem.cs +++ b/Facepunch.Steamworks/Structs/UgcItem.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; using Steamworks.Data; @@ -254,10 +255,46 @@ public async Task Subscribe () return result?.Result == Result.OK; } - /// - /// Allows the user to unsubscribe from this item - /// - public async Task Unsubscribe () + /// + /// Allows the user to subscribe to this item and wait for it to be downloaded + /// If CancellationToken is default then there is 60 seconds timeout + /// Progress will be set to 0-1 + /// + public async Task SubscribeDownloadAsync( Action progress = null, CancellationToken ct = default, int milisecondsUpdateDelay = 60 ) + { + if ( ct == default ) + ct = new CancellationTokenSource( TimeSpan.FromSeconds( 60 ) ).Token; + + progress?.Invoke( 0 ); + + var subResult = await SteamUGC.Internal.SubscribeItem( _id ); + if ( subResult?.Result != Result.OK ) + return false; + + var downloading = Download( true ); + if ( !downloading ) + return State == ItemState.Installed; + + while ( true ) + { + if ( ct.IsCancellationRequested ) + return false; + + progress?.Invoke( DownloadAmount ); + + if ( !IsDownloading ) + break; + + await Task.Delay( milisecondsUpdateDelay ); + } + + return State.HasFlag( ItemState.Installed ); + } + + /// + /// Allows the user to unsubscribe from this item + /// + public async Task Unsubscribe () { var result = await SteamUGC.Internal.UnsubscribeItem( _id ); return result?.Result == Result.OK;