diff --git a/Facepunch.Steamworks/SteamClient.cs b/Facepunch.Steamworks/SteamClient.cs index c628488..e7fd15c 100644 --- a/Facepunch.Steamworks/SteamClient.cs +++ b/Facepunch.Steamworks/SteamClient.cs @@ -44,6 +44,7 @@ namespace Steamworks SteamParties.InstallEvents(); SteamNetworkingSockets.InstallEvents(); SteamInput.InstallEvents(); + SteamUGC.InstallEvents(); if ( asyncCallbacks ) { diff --git a/Facepunch.Steamworks/SteamUgc.cs b/Facepunch.Steamworks/SteamUgc.cs index a329954..b230ed4 100644 --- a/Facepunch.Steamworks/SteamUgc.cs +++ b/Facepunch.Steamworks/SteamUgc.cs @@ -29,6 +29,16 @@ namespace Steamworks } } + internal static void InstallEvents() + { + DownloadItemResult_t.Install( x => OnDownloadItemResult?.Invoke( x.Result ) ); + } + + /// + /// Posted after Download call + /// + public static event Action OnDownloadItemResult; + internal static void Shutdown() { _internal = null; diff --git a/Facepunch.Steamworks/Structs/UgcItem.cs b/Facepunch.Steamworks/Structs/UgcItem.cs index 9238fd7..fe5dd05 100644 --- a/Facepunch.Steamworks/Structs/UgcItem.cs +++ b/Facepunch.Steamworks/Structs/UgcItem.cs @@ -198,7 +198,9 @@ namespace Steamworks.Ugc { get { - if ( !NeedsUpdate ) return 1; + //changed from NeedsUpdate as it's false when validating and redownloading ugc + //possibly similar properties should also be changed + if ( !IsDownloading ) return 1; ulong downloaded = 0; ulong total = 0; @@ -266,26 +268,66 @@ namespace Steamworks.Ugc ct = new CancellationTokenSource( TimeSpan.FromSeconds( 60 ) ).Token; progress?.Invoke( 0 ); + await Task.Delay( milisecondsUpdateDelay ); - var subResult = await SteamUGC.Internal.SubscribeItem( _id ); - if ( subResult?.Result != Result.OK ) - return false; - - var downloading = Download( true ); - if ( !downloading ) - return State.HasFlag( ItemState.Installed ); - - while ( true ) + //Subscribe { - if ( ct.IsCancellationRequested ) - break; + var subResult = await SteamUGC.Internal.SubscribeItem( _id ); + if ( subResult?.Result != Result.OK ) + return false; + } - progress?.Invoke( DownloadAmount ); + progress?.Invoke( 0.1f ); + await Task.Delay( milisecondsUpdateDelay ); - if ( !IsDownloading && State.HasFlag( ItemState.Installed ) ) - break; + //Try to start downloading + { + if ( Download( true ) == false ) + return State.HasFlag( ItemState.Installed ); - await Task.Delay( milisecondsUpdateDelay ); + //Steam docs about Download: + //If the return value is true then register and wait + //for the Callback DownloadItemResult_t before calling + //GetItemInstallInfo or accessing the workshop item on disk. + + //Wait for DownloadItemResult_t + { + var downloadStarted = false; + Action onDownloadStarted = null; + onDownloadStarted = r => + { + SteamUGC.OnDownloadItemResult -= onDownloadStarted; + downloadStarted = true; + }; + SteamUGC.OnDownloadItemResult += onDownloadStarted; + + while ( downloadStarted == false ) + { + if ( ct.IsCancellationRequested ) + break; + + await Task.Delay( milisecondsUpdateDelay ); + } + } + } + + progress?.Invoke( 0.2f ); + await Task.Delay( milisecondsUpdateDelay ); + + //Wait for downloading completion + { + while ( true ) + { + if ( ct.IsCancellationRequested ) + break; + + progress?.Invoke( 0.2f + DownloadAmount * 0.8f ); + + if ( !IsDownloading && State.HasFlag( ItemState.Installed ) ) + break; + + await Task.Delay( milisecondsUpdateDelay ); + } } return State.HasFlag( ItemState.Installed );