Example #1
0
        private void LoadRobots(Library.ILibraryInternal library)
        {
            string json = null;

            if (Library.FolderExists(TWITCH_CACHE_PATH))
            {
                var qualifiedFilename = string.Format("{0}{1}.json", TWITCH_CACHE_PATH, "robots");
                if (Library.FileExists(qualifiedFilename))
                {
                    json = File.ReadAllText(Library.GetFilename(qualifiedFilename));
                }
            }

            if (json == null)
            {
                throw new Exception("Can't find robots!");
            }

            var robots = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, string> >(json);

            if (robots == null)
            {
                throw new Exception("Failed to create robot dictionary");
            }

            foreach (var pair in robots)
            {
                try {
                    AddEmote(pair.Key, pair.Value, library);
                } catch (Exception) {
                    FP.Log("Failed to load robot", pair.Key);
                }
            }
        }
Example #2
0
        private void LoadChannelBttvSet(Library.ILibraryInternal library, string channel)
        {
            string json = null;

            var qualifiedFilename = string.Format("{0}{1}_{2}.json", TWITCH_CACHE_PATH, "bttv", channel);

            try
            {
                json = client.DownloadString(string.Format("https://api.betterttv.net/2/channels/{0}", channel));
                if (!String.IsNullOrEmpty(json))
                {
                    File.WriteAllText(qualifiedFilename, json);
                }
                else
                {
                    json = null;
                }
            }
            catch
            {
                json = null;
            }

            if (json == null)
            {
                if (Library.FileExists(qualifiedFilename))
                {
                    json = File.ReadAllText(Library.GetFilename(qualifiedFilename));
                }
                else
                {
                    return;
                }
            }


            var api = Newtonsoft.Json.JsonConvert.DeserializeObject <BaseBttvAPI>(json);

            foreach (var emote in api.emotes)
            {
                try
                {
                    if (File.Exists(string.Format("{0}{1}", TWITCH_CACHE_PATH, Utility.EncodeStringForFileName(emote.code))))
                    {
                        AddEmote(emote.code, emote.code, library);
                    }
                    else
                    {
                        AddBttvEmote(emote.code, emote.id, library);
                    }
                    LoadedSpecialEmotes.Add(emote.code);
                }
                catch (Exception e)
                {
                    FP.Log(emote.code, e.Message);
                }
            }
        }
Example #3
0
        private void LoadGlobalSet(Library.ILibraryInternal library)
        {
            string json = null;
            var    qualifiedFilename = string.Format("{0}{1}.json", TWITCH_CACHE_PATH, "global");

            try
            {
                json = client.DownloadString(string.Format("https://twitchemotes.com/api_cache/v2/{0}.json", "global"));
                if (!String.IsNullOrEmpty(json))
                {
                    File.WriteAllText(qualifiedFilename, json);
                }
                else
                {
                    json = null;
                }
            }
            catch
            {
                json = null;
            }

            if (json == null)
            {
                if (Library.FileExists(qualifiedFilename))
                {
                    json = File.ReadAllText(Library.GetFilename(qualifiedFilename));
                }
                else
                {
                    return;
                }
            }

            var api = Newtonsoft.Json.JsonConvert.DeserializeObject <GlobalEmoteAPI>(json);

            foreach (var pair in api.emotes)
            {
                try
                {
                    if (File.Exists(string.Format("{0}{1}", TWITCH_CACHE_PATH, pair.Key)))
                    {
                        AddEmote(pair.Key, pair.Key, library);
                    }
                    else
                    {
                        AddTwitchEmote(pair.Key, pair.Value.image_id, library);
                    }
                }
                catch (Exception e)
                {
                    FP.Log(pair.Key, e.Message);
                }
            }
        }
Example #4
0
        public override void Load(string filename, Library.ILibraryInternal library)
        {
            var emote = FromFilename(filename);

            if (File.Exists(string.Format("{0}{1}", TWITCH_CACHE_PATH, emote.code)))
            {
                AddEmote(emote.code, emote.code, library);
            }
            else
            {
                AddTwitchEmote(emote.code, emote.image_id, library);
            }
        }
Example #5
0
 public override void Precache(Library.ILibraryInternal library)
 {
     if (!Library.FolderExists(TWITCH_CACHE_PATH))
     {
         Directory.CreateDirectory(TWITCH_CACHE_PATH);
     }
     LoadRobots(library);
     LoadGlobalSet(library);
     LoadSubscriberSet(library);
     LoadBttvSet(library);
     if (currentChannel != null)
     {
         LoadChannelBttvSet(library, currentChannel);
     }
 }
Example #6
0
        private void AddAvatar(string userName, Library.ILibraryInternal library)
        {
            if (loadedEmotes.Contains(userName))
            {
                return;
            }

            byte[] data = null;

            var userJson = client.DownloadString(string.Format("https://api.twitch.tv/kraken/channels/{0}", userName));
            var userData = Newtonsoft.Json.JsonConvert.DeserializeObject <UserData>(userJson);

            if (userData.logo == null)
            {
                return;
            }

            data = client.DownloadData(userData.logo.Replace("300x300", "50x50"));

            MemoryStream stream = new MemoryStream();

            using (MemoryStream bMapStream = new MemoryStream(data))
            {
                using (Bitmap bMap = new Bitmap(bMapStream))
                {
                    using (MagickImage mImage = new MagickImage(data))
                    {
                        switch (bMap.PixelFormat)
                        {
                        case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
                            mImage.Format = MagickFormat.Png8;
                            break;

                        default:
                            mImage.Format = MagickFormat.Png32;
                            break;
                        }

                        mImage.Write(stream);
                        stream.Position = 0;
                    }
                }
            }

            library.AddTexture(string.Format("{0}{1}", prefix, userName), new MemoryStream(data, false));
            loadedEmotes.Add(userName);
        }
Example #7
0
        private void AddEmote(string emoteName, string path, Library.ILibraryInternal library)
        {
            if (loadedEmotes.Contains(emoteName))
            {
                return;
            }

            string encodedEmoteName = Utility.EncodeStringForFileName(path);

            byte[] data = null;

            if (Library.FolderExists(TWITCH_CACHE_PATH))
            {
                var qualifiedFilename = string.Format("{0}{1}", TWITCH_CACHE_PATH, encodedEmoteName);
                if (Library.FileExists(qualifiedFilename))
                {
                    data = File.ReadAllBytes(Library.GetFilename(qualifiedFilename));
                }
            }

            library.AddTexture(string.Format("twitch//{0}", emoteName), new MemoryStream(data, false));
            loadedEmotes.Add(emoteName);
        }
Example #8
0
        private void LoadSubscriberSet(Library.ILibraryInternal library)
        {
            string json = null;
            var    qualifiedFilename = string.Format("{0}{1}.json", TWITCH_CACHE_PATH, "subscriber");

            try
            {
                json = client.DownloadString(string.Format("https://twitchemotes.com/api_cache/v2/{0}.json", "subscriber"));
                if (!String.IsNullOrEmpty(json))
                {
                    File.WriteAllText(qualifiedFilename, json);
                }
                else
                {
                    json = null;
                }
            }
            catch
            {
                json = null;
            }

            if (json == null)
            {
                if (Library.FileExists(qualifiedFilename))
                {
                    json = File.ReadAllText(Library.GetFilename(qualifiedFilename));
                }
                else
                {
                    return;
                }
            }

            subscriberEmotes = Newtonsoft.Json.JsonConvert.DeserializeObject <SubscriberEmoteAPI>(json);
        }
Example #9
0
        public override void Load(string filename, Library.ILibraryInternal library)
        {
            var match = Regex.Match(filename, prefix + @"([\w\W]+)");

            AddAvatar(match.Groups[1].Value, library);
        }
Example #10
0
 public override void Precache(Library.ILibraryInternal library)
 {
 }
Example #11
0
        private void AddEmote(string emoteName, Library.ILibraryInternal library, string url)
        {
            if (loadedEmotes.Contains(emoteName))
            {
                return;
            }

            string encodedEmoteName = Utility.EncodeStringForFileName(emoteName);

            byte[] data = null;

            if (Library.FolderExists(TWITCH_CACHE_PATH))
            {
                var qualifiedFilename = string.Format("{0}{1}", TWITCH_CACHE_PATH, encodedEmoteName);
                if (Library.FileExists(qualifiedFilename))
                {
                    data = File.ReadAllBytes(Library.GetFilename(qualifiedFilename));
                }
            }
            MemoryStream imageStream = new MemoryStream();

            if (data == null)
            {
                data = client.DownloadData(url);
                var folder = Library.GetFolderName(TWITCH_CACHE_PATH);

                MagickReadSettings settings = new MagickReadSettings();
                settings.ColorSpace = ColorSpace.sRGB;
                settings.SetDefine(MagickFormat.Png, "format", "png8");

                MagickImageInfo mImageInfo = new MagickImageInfo(data);

                if (mImageInfo.Format != MagickFormat.Gif)
                {
                    using (MagickImage mImage = new MagickImage(data, settings))
                    {
                        mImage.Write(imageStream);
                        imageStream.Position = 0;
                    }
                }
                else
                {
                    using (MagickImageCollection mImages = new MagickImageCollection(data, settings))
                    {
                        //Background is PepePls
                        //SourPls is None
                        var width  = mImages.Max(x => x.Page.Width);
                        var height = mImages.Max(y => y.Page.Height);
                        FramePacker.FrameDisposal frameDisposal;

                        switch (mImages[0].GifDisposeMethod)
                        {
                        case GifDisposeMethod.None:
                            frameDisposal = FramePacker.FrameDisposal.Composite;
                            break;

                        case GifDisposeMethod.Undefined:
                        case GifDisposeMethod.Background:
                        case GifDisposeMethod.Previous:
                        default:
                            frameDisposal = FramePacker.FrameDisposal.Replace;
                            break;
                        }

                        var packedImage = FramePacker.PackListOfImagesToMemStream(
                            mImages.Select(imageFrame => new FramePacker.Frame
                        {
                            Image  = imageFrame.ToBitmap(ImageFormat.Png),
                            X      = imageFrame.Page.X,
                            Y      = imageFrame.Page.Y,
                            Width  = imageFrame.BaseWidth,
                            Height = imageFrame.BaseHeight
                        }).ToArray(),
                            imageStream,
                            width,
                            height,
                            frameDisposal
                            );

                        packedImage.FPS = 1 / ((float)mImages.Select(x => x.AnimationDelay).Average() / 100.0f);

                        JsonWriter.Save(packedImage, string.Format("{0}{1}", TWITCH_CACHE_PATH, encodedEmoteName), true);
                        //mImages[0].Write(imageStream);
                        imageStream.Position = 0;
                    }
                }

                File.WriteAllBytes(string.Format("{0}/{1}", folder, encodedEmoteName), data = imageStream.ToArray());
            }

            library.AddTexture(string.Format("twitch//{0}", emoteName), imageStream);
            loadedEmotes.Add(emoteName);
        }
Example #12
0
 public void AddTwitchEmote(string emoteName, string image_id, Library.ILibraryInternal library)
 {
     AddEmote(emoteName, library, string.Format("https://static-cdn.jtvnw.net/emoticons/v1/{0}/1.0", image_id));
 }
Example #13
0
 private void AddBttvEmote(string emoteName, string image_id, Library.ILibraryInternal library)
 {
     AddEmote(emoteName, library, string.Format("https://cdn.betterttv.net/emote/{0}/1x", image_id));
 }