Build 0.20.8.0
This commit is contained in:
@@ -69,77 +69,71 @@ namespace Steamworks
|
||||
/// <param name="fileId">The ID of the file you want to download</param>
|
||||
/// <param name="progress">An optional callback</param>
|
||||
/// <param name="ct">Allows you to send a message to cancel the download anywhere during the process</param>
|
||||
/// <param name="milisecondsUpdateDelay">How often to call the progress function</param>
|
||||
/// <param name="millisecondsUpdateDelay">How often to call the progress function</param>
|
||||
/// <returns>true if downloaded and installed correctly</returns>
|
||||
public static async Task<bool> DownloadAsync( PublishedFileId fileId, Action<float> progress = null, int milisecondsUpdateDelay = 60, CancellationToken ct = default )
|
||||
public static async Task<bool> DownloadAsync(
|
||||
PublishedFileId fileId,
|
||||
Action<float> progress = null,
|
||||
int millisecondsUpdateDelay = 60,
|
||||
CancellationToken? ct = null)
|
||||
{
|
||||
var item = new Steamworks.Ugc.Item( fileId );
|
||||
|
||||
if ( ct == default )
|
||||
ct = new CancellationTokenSource( TimeSpan.FromSeconds( 60 ) ).Token;
|
||||
var cancellationToken = ct ?? new CancellationTokenSource(TimeSpan.FromSeconds(60)).Token;
|
||||
|
||||
async Task waitOrCancel()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
await Task.Delay(millisecondsUpdateDelay);
|
||||
}
|
||||
|
||||
progress?.Invoke( 0.0f );
|
||||
|
||||
if ( Download( fileId, highPriority: true ) == false )
|
||||
return item.IsInstalled;
|
||||
Result downloadStartResult = Result.None;
|
||||
|
||||
// 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
|
||||
void onDownloadFinished(Result r, ulong id)
|
||||
{
|
||||
Action<Result, ulong> onDownloadStarted = null;
|
||||
if (id != item.Id) { return; }
|
||||
downloadStartResult = r;
|
||||
}
|
||||
OnDownloadItemResult += onDownloadFinished;
|
||||
|
||||
try
|
||||
if (!Download(fileId, highPriority: true)) { return item.IsInstalled; }
|
||||
|
||||
await Task.Delay(500);
|
||||
|
||||
try
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var downloadStarted = false;
|
||||
|
||||
onDownloadStarted = (r, id) => downloadStarted = true;
|
||||
OnDownloadItemResult += onDownloadStarted;
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
int iters = 0;
|
||||
while ( downloadStarted == false )
|
||||
progress?.Invoke(item.DownloadAmount);
|
||||
|
||||
if (downloadStartResult != Result.None)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
|
||||
iters++;
|
||||
if (iters >= 1000 / milisecondsUpdateDelay)
|
||||
{
|
||||
if (!item.IsDownloading && !item.IsInstalled)
|
||||
{
|
||||
//force download to start if it's not started
|
||||
if ( Download( fileId, highPriority: true ) == false )
|
||||
return item.IsInstalled;
|
||||
}
|
||||
iters = 0;
|
||||
}
|
||||
await Task.Delay( milisecondsUpdateDelay );
|
||||
if (downloadStartResult != Result.OK) { return false; }
|
||||
break;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
OnDownloadItemResult -= onDownloadStarted;
|
||||
|
||||
if (!item.IsDownloadPending && !item.IsDownloading)
|
||||
{
|
||||
if (item.IsInstalled)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (!Download(fileId, highPriority: true))
|
||||
{
|
||||
return item.IsInstalled;
|
||||
}
|
||||
}
|
||||
|
||||
await Task.Delay( millisecondsUpdateDelay );
|
||||
}
|
||||
}
|
||||
|
||||
progress?.Invoke( 0.2f );
|
||||
await Task.Delay( milisecondsUpdateDelay );
|
||||
|
||||
//Wait for downloading completion
|
||||
finally
|
||||
{
|
||||
while ( true )
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
|
||||
progress?.Invoke( 0.2f + item.DownloadAmount * 0.8f );
|
||||
|
||||
if ( !item.IsDownloading && item.IsInstalled )
|
||||
break;
|
||||
|
||||
await Task.Delay( milisecondsUpdateDelay );
|
||||
}
|
||||
OnDownloadItemResult -= onDownloadFinished;
|
||||
}
|
||||
|
||||
progress?.Invoke( 1.0f );
|
||||
|
||||
@@ -224,7 +224,7 @@ namespace Steamworks.Ugc
|
||||
}
|
||||
}
|
||||
|
||||
private ItemState State => (ItemState) SteamUGC.Internal.GetItemState( Id );
|
||||
private ItemState State => (ItemState)(SteamUGC.Internal?.GetItemState( Id ) ?? 0);
|
||||
|
||||
public static async Task<Item?> GetAsync( PublishedFileId id, int maxageseconds = 60 * 30 )
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user