/// <summary>
        /// This function *blocks* the calling thread, and caches all the images required to display the message, then registers them with the provided font.
        /// </summary>
        /// <param name="msg">The chat message to get images from</param>
        /// <param name="font">The font to register these images to</param>
        public async Task <bool> PrepareImages(IESCChatMessage msg, EnhancedFontInfo font)
        {
            var tasks = new List <Task <EnhancedImageInfo> >();
            var pendingEmoteDownloads = new HashSet <string>();

            foreach (var emote in msg.Emotes)
            {
                if (string.IsNullOrEmpty(emote.Id) || pendingEmoteDownloads.Contains(emote.Id))
                {
                    continue;
                }
                if (!font.CharacterLookupTable.ContainsKey(emote.Id))
                {
                    pendingEmoteDownloads.Add(emote.Id);
                    var tcs = new TaskCompletionSource <EnhancedImageInfo>();
                    SharedCoroutineStarter.instance.StartCoroutine(this._chatImageProvider.TryCacheSingleImage(emote.Id, emote.Url, emote.Animated ? ChatImageProvider.ESCAnimationType.GIF : ChatImageProvider.ESCAnimationType.MAYBE_GIF, (info) =>
                    {
                        if (info == null || !font.TryRegisterImageInfo(info, out var character))
                        {
                            Logger.Warn($"Failed to register emote \"{emote.Id}\" in font {font.Font.name}.");
                        }
                        tcs.SetResult(info);
                    }, forcedHeight: 110));
                    tasks.Add(tcs.Task);
                }
            }

            if (msg.Sender is TwitchUser twitchUser)
            {
                foreach (var badge in twitchUser.Badges)
                {
                    if (string.IsNullOrEmpty(badge.Id) || pendingEmoteDownloads.Contains(badge.Id))
                    {
                        continue;
                    }
                    if (!font.CharacterLookupTable.ContainsKey(badge.Id))
                    {
                        pendingEmoteDownloads.Add(badge.Id);
                        var tcs = new TaskCompletionSource <EnhancedImageInfo>();
                        SharedCoroutineStarter.instance.StartCoroutine(this._chatImageProvider.TryCacheSingleImage(badge.Id, badge.Uri, ChatImageProvider.ESCAnimationType.NONE, (info) =>
                        {
                            if (info != null)
                            {
                                if (!font.TryRegisterImageInfo(info, out var character))
                                {
                                    Logger.Warn($"Failed to register badge \"{badge.Id}\" in font {font.Font.name}.");
                                }
                            }
                            tcs.SetResult(info);
                        }, forcedHeight: 100));
                        tasks.Add(tcs.Task);
                    }
                }
            }
            // Wait on all the resources to be ready
            var result = await Task.WhenAll(tasks);

            return(result.All(x => x != null));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This function *blocks* the calling thread, and caches all the images required to display the message, then registers them with the provided font.
        /// </summary>
        /// <param name="msg">The chat message to get images from</param>
        /// <param name="font">The font to register these images to</param>
        public static bool PrepareImages(IChatMessage msg, EnhancedFontInfo font)
        {
            List <Task <EnhancedImageInfo> > tasks = new List <Task <EnhancedImageInfo> >();
            HashSet <string> pendingEmoteDownloads = new HashSet <string>();

            foreach (var emote in msg.Emotes)
            {
                if (pendingEmoteDownloads.Contains(emote.Id))
                {
                    continue;
                }
                if (!font.CharacterLookupTable.ContainsKey(emote.Id))
                {
                    pendingEmoteDownloads.Add(emote.Id);
                    TaskCompletionSource <EnhancedImageInfo> tcs = new TaskCompletionSource <EnhancedImageInfo>();
                    switch (emote.Type)
                    {
                    case EmoteType.SingleImage:
                        SharedCoroutineStarter.instance.StartCoroutine(ChatImageProvider.instance.TryCacheSingleImage(emote.Id, emote.Uri, emote.IsAnimated, (info) => {
                            if (info != null)
                            {
                                if (!font.TryRegisterImageInfo(info, out var character))
                                {
                                    Logger.log.Warn($"Failed to register emote \"{emote.Id}\" in font {font.Font.name}.");
                                }
                            }
                            tcs.SetResult(info);
                        }, forcedHeight: 110));
                        break;

                    case EmoteType.SpriteSheet:
                        ChatImageProvider.instance.TryCacheSpriteSheetImage(emote.Id, emote.Uri, emote.UVs, (info) =>
                        {
                            if (info != null)
                            {
                                if (!font.TryRegisterImageInfo(info, out var character))
                                {
                                    Logger.log.Warn($"Failed to register emote \"{emote.Id}\" in font {font.Font.name}.");
                                }
                            }
                            tcs.SetResult(info);
                        }, forcedHeight: 110);
                        break;

                    default:
                        tcs.SetResult(null);
                        break;
                    }
                    tasks.Add(tcs.Task);
                }
            }

            foreach (var badge in msg.Sender.Badges)
            {
                if (pendingEmoteDownloads.Contains(badge.Id))
                {
                    continue;
                }

                if (!font.CharacterLookupTable.ContainsKey(badge.Id))
                {
                    pendingEmoteDownloads.Add(badge.Id);
                    TaskCompletionSource <EnhancedImageInfo> tcs = new TaskCompletionSource <EnhancedImageInfo>();
                    SharedCoroutineStarter.instance.StartCoroutine(ChatImageProvider.instance.TryCacheSingleImage(badge.Id, badge.Uri, false, (info) => {
                        if (info != null)
                        {
                            if (!font.TryRegisterImageInfo(info, out var character))
                            {
                                Logger.log.Warn($"Failed to register badge \"{badge.Id}\" in font {font.Font.name}.");
                            }
                        }
                        tcs.SetResult(info);
                    }, forcedHeight: 100));
                    tasks.Add(tcs.Task);
                }
            }

            // Wait on all the resources to be ready
            return(Task.WaitAll(tasks.ToArray(), 15000));
        }