Ejemplo n.º 1
0
        //async
        public static void GetEmote(string url, EmoteCallback callback)
        {
            if (AppSettings.CacheEmotes)
            {
                Task.Run((() =>
                {
                    _emotes_cache ecache;
                    ecache.emote = null;
                    Mutex m;

                    using (m = new Mutex(false, url)) {
                        try {
                            m.WaitOne();
                        } catch (AbandonedMutexException e) {
                            //no problem keep going
                        }
                        if (CachedEmotes.TryGetValue(url, out ecache))
                        {
                            ecache.usedLastTime = true;
                            if (ecache.emote == null && File.Exists(ecache.emotePath))
                            {
                                try {
                                    MemoryStream mem = new MemoryStream();
                                    using (FileStream stream = new FileStream(ecache.emotePath, FileMode.Open, FileAccess.Read)) {
                                        stream.CopyTo(mem);
                                        ecache.emote = ChatterinoImage.FromStream(mem);
                                    }
                                } catch (Exception e) {
                                    GuiEngine.Current.log("emote faild to load " + ecache.emotePath + " " + e.ToString());
                                    ecache.emote = null;
                                }
                            }
                            if (ecache.emote == null)
                            {
                                //assume file doesnt exist. remove from the cache.
                                CachedEmotes.TryRemove(url, out ecache);
                            }
                            else
                            {
                                CachedEmotes[url] = ecache;
                            }
                        }
                        m.ReleaseMutex();
                        callback(ecache.emote);
                    }
                }));
            }
            else
            {
                callback(null);
            }
        }
Ejemplo n.º 2
0
        private void getEmote()
        {
            try
            {
                ChatterinoImage img;
                if (LoadAction != null)
                {
                    img = LoadAction();
                }
                else
                {
                    try
                    {
                        var request = WebRequest.Create(Url);
                        if (AppSettings.IgnoreSystemProxy)
                        {
                            request.Proxy = null;
                        }
                        using (var response = request.GetResponse()) {
                            using (var stream = response.GetResponseStream())
                            {
                                MemoryStream mem = new MemoryStream();
                                stream.CopyTo(mem);
                                img = GuiEngine.Current.ReadImageFromStream(mem);
                            }
                            response.Close();
                        }

                        GuiEngine.Current.FreezeImage(img);
                    }
                    catch (Exception e)
                    {
                        GuiEngine.Current.log("emote faild to load " + Name + " " + Url + " " + e.ToString());
                        img = null;
                    }
                }
                if (img != null)
                {
                    GuiEngine.Current.HandleAnimatedTwitchEmote(this, img);
                    EmoteCache.AddEmote(Url, img);
                    image = img;
                    GuiEngine.Current.TriggerEmoteLoaded();
                    ImageLoaded?.Invoke(null, null);
                }
                loading = false;
            } catch (Exception e) {
                GuiEngine.Current.log("Error loading emote " + Name + " " + Url + " " + e.ToString());
            }
        }
Ejemplo n.º 3
0
 //async
 public static void AddEmote(string url, ChatterinoImage emote)
 {
     if (AppSettings.CacheEmotes)
     {
         Task.Run((() =>
         {
             _emotes_cache ecache;
             Mutex m;
             using (m = new Mutex(false, url)) {
                 try {
                     m.WaitOne();
                 } catch (AbandonedMutexException e) {
                     //no problem keep going
                 }
                 if (!CachedEmotes.ContainsKey(url))
                 {
                     ecache.usedLastTime = true;
                     ecache.isAnimated = false;
                     ecache.emotePath = Path.Combine(Util.GetUserDataPath(), "Cache", "Emotes", HttpUtility.UrlEncode(url));
                     ecache.emote = emote;
                     try {
                         //save emote to a file
                         if (File.Exists(ecache.emotePath))
                         {
                             File.Delete(ecache.emotePath);
                         }
                         lock (emote) {
                             bool animated = emote.IsAnimated;
                             if (!animated)
                             {
                                 emote.Save(ecache.emotePath);
                             }
                             else
                             {
                                 ecache.isAnimated = true;
                             }
                         }
                         CachedEmotes.TryAdd(url, ecache);
                     } catch (Exception e) {
                         GuiEngine.Current.log("emote faild to save " + ecache.emotePath + " " + e.ToString());
                     }
                 }
                 m.ReleaseMutex();
             }
         }));
     }
 }
Ejemplo n.º 4
0
 public LazyLoadedImage(ChatterinoImage image)
 {
     this.image = image;
     loading    = false;
 }