Exemple #1
0
        internal static void InstallEvents(bool server)
        {
            Dispatch.Install <DownloadItemResult_t>(x =>
            {
                if (x.AppID == SteamClient.AppId)
                {
                    OnDownloadItemResult?.Invoke(x.Result);

                    Ugc.Item item = new Ugc.Item(x.PublishedFileId);
                    if (item.IsInstalled && (onItemInstalled?.ContainsKey(x.PublishedFileId) ?? false))
                    {
                        onItemInstalled[x.PublishedFileId]?.Invoke();
                        onItemInstalled.Remove(x.PublishedFileId);
                    }
                }
            }, server);
            Dispatch.Install <ItemInstalled_t>(x =>
            {
                if (x.AppID == SteamClient.AppId)
                {
                    GlobalOnItemInstalled?.Invoke(x.PublishedFileId);
                    if (onItemInstalled?.ContainsKey(x.PublishedFileId) ?? false)
                    {
                        onItemInstalled[x.PublishedFileId]?.Invoke();
                        onItemInstalled.Remove(x.PublishedFileId);
                    }
                }
            }, server);
        }
        internal void Init(Steamworks.Ugc.Item item)
        {
            string previewImageUrl;
            bool   flag;
            string description;
            string title;

            this.Item = new Steamworks.Ugc.Item?(item);
            if (this.Name)
            {
                TextMeshProUGUI name            = this.Name;
                ref Nullable    nullablePointer = ref this.Item;
                if (nullablePointer.HasValue)
                {
                    title = nullablePointer.GetValueOrDefault().Title;
                }
                else
                {
                    title = null;
                }
                name.text = title;
            }
        /// <summary>
        /// Will attempt to download this item asyncronously - allowing you to instantly react to its installation
        /// </summary>
        /// <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>
        /// <returns>true if downloaded and installed correctly</returns>
        public static async Task <bool> DownloadAsync(
            PublishedFileId fileId,
            Action <float> progress    = null,
            Action <string> onError    = null,
            int milisecondsUpdateDelay = 60,
            CancellationToken ct       = default,
            bool highPriority          = true)
        {
            progress?.Invoke(0.00f);

            if (ct == default)
            {
                ct = new CancellationTokenSource(TimeSpan.FromSeconds(60)).Token;
            }

            Ugc.Item?itemNullable = await Ugc.Item.GetAsync(fileId);

            progress?.Invoke(0.05f);

            if (!itemNullable.HasValue)
            {
                onError?.Invoke($"Workshop item (id: " + fileId.Value + ") failed to load or doesn't exist.");
                return(false);
            }

            Ugc.Item item = itemNullable.Value;

            bool   itemDownloaded = false;
            Result?downloadError  = null;

            Action <DownloadItemResult> onDownloadResult = r =>
            {
                if (r.AppID == SteamClient.AppId && r.PublishedFileId == fileId)
                {
                    if (r.Result != Result.OK)
                    {
                        downloadError = r.Result;
                    }
                    else
                    {
                        itemDownloaded = true;
                    }
                }
            };

            try
            {
                OnDownloadItemResult += onDownloadResult;

                progress?.Invoke(0.1f);
                if (Download(fileId, highPriority) == false)
                {
                    return(false);
                }

                await Task.Delay(milisecondsUpdateDelay);                   //have to wait here otherwise first DownloadAmount is 1

                while (itemDownloaded == false && downloadError == null)
                {
                    var state = item.State;
                    if (ct.IsCancellationRequested)
                    {
                        break;
                    }

                    //waiting for download to start
                    if (state.HasFlag(ItemState.DownloadPending) &&
                        !state.HasFlag(ItemState.Downloading))
                    {
                        progress?.Invoke(0.2f);
                    }
                    else
                    {
                        progress?.Invoke(0.2f + item.DownloadAmount * 0.8f);
                    }

                    //skip whole OnDownloadItemResult call as it doesn't work for everyone
                    if (!state.HasFlag(ItemState.DownloadPending) &&
                        !state.HasFlag(ItemState.Downloading) &&
                        !state.HasFlag(ItemState.NeedsUpdate))
                    {
                        break;
                    }

                    await Task.Delay(milisecondsUpdateDelay);
                }

                await Task.Delay(milisecondsUpdateDelay);

                if (downloadError != null)
                {
                    onError?.Invoke("Download item result error: " + downloadError.ToString());
                    return(false);
                }

                progress?.Invoke(1.0f);
                return(true);
            }
            finally
            {
                OnDownloadItemResult -= onDownloadResult;
            }
        }