Beispiel #1
0
        /// <summary>
        /// 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
        /// </summary>
        public async Task <bool> SubscribeDownloadAsync(Action <float> 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.HasFlag(ItemState.Installed));
            }

            while (true)
            {
                if (ct.IsCancellationRequested)
                {
                    return(false);
                }

                progress?.Invoke(DownloadAmount);

                if (!IsDownloading)
                {
                    break;
                }

                await Task.Delay(milisecondsUpdateDelay);
            }

            return(State.HasFlag(ItemState.Installed));
        }
        /// <summary>
        /// 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
        /// </summary>
        public async Task <bool> SubscribeDownloadAsync(Action <float> progress = null, CancellationToken ct = default, int milisecondsUpdateDelay = 60)
        {
            if (ct == default)
            {
                ct = new CancellationTokenSource(TimeSpan.FromSeconds(60)).Token;
            }

            progress?.Invoke(0);
            await Task.Delay(milisecondsUpdateDelay);

            //Subscribe
            {
                var subResult = await SteamUGC.Internal.SubscribeItem(_id);

                if (subResult?.Result != Result.OK)
                {
                    return(false);
                }
            }

            progress?.Invoke(0.1f);
            await Task.Delay(milisecondsUpdateDelay);

            //Try to start downloading
            {
                if (Download(true) == false)
                {
                    return(State.HasFlag(ItemState.Installed));
                }

                //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));
        }