Fixed ugc validation/redownload

This commit is contained in:
kamyker 2020-01-20 19:57:36 +01:00
parent 22e963250e
commit d42996142f
3 changed files with 69 additions and 16 deletions

View File

@ -44,6 +44,7 @@ public static void Init( uint appid, bool asyncCallbacks = true )
SteamParties.InstallEvents();
SteamNetworkingSockets.InstallEvents();
SteamInput.InstallEvents();
SteamUGC.InstallEvents();
if ( asyncCallbacks )
{

View File

@ -29,6 +29,16 @@ internal static ISteamUGC Internal
}
}
internal static void InstallEvents()
{
DownloadItemResult_t.Install( x => OnDownloadItemResult?.Invoke( x.Result ) );
}
/// <summary>
/// Posted after Download call
/// </summary>
public static event Action<Result> OnDownloadItemResult;
internal static void Shutdown()
{
_internal = null;

View File

@ -198,7 +198,9 @@ public float DownloadAmount
{
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 @@ public async Task<bool> SubscribeDownloadAsync( Action<float> progress = null, C
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<Result> 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 );