Search in sources :

Example 1 with LazyLoadEmoji

use of com.glitchcog.fontificator.emoji.LazyLoadEmoji in project ChatGameFontificator by GlitchCog.

the class EmojiParser method parseFrankerFaceZModBadge.

/**
     * Parse the FrankerFaceZ room data for the optional moderator badge
     * 
     * @param manager
     * @param jsonData
     * @throws IOException
     */
public void parseFrankerFaceZModBadge(EmojiManager manager, String jsonData) throws IOException {
    Gson gson = new Gson();
    Type roomType = new TypeToken<Room>() {
    }.getType();
    JsonObject jsonObject = new JsonParser().parse(jsonData).getAsJsonObject();
    Room room = gson.fromJson(jsonObject.get("room"), roomType);
    final boolean customFfzModBadgeExists = room != null && room.getModerator_badge() != null;
    if (customFfzModBadgeExists) {
        LazyLoadEmoji modLle = new LazyLoadEmoji(UserType.MOD.getKey(), UserType.MOD.getKey(), "https:" + room.getModerator_badge(), ConfigEmoji.MOD_BADGE_COLOR, EmojiType.FRANKERFACEZ_BADGE);
        manager.getEmojiByType(EmojiType.FRANKERFACEZ_BADGE).put(UserType.MOD.getKey(), modLle);
        logBox.log("Loaded the custom FrankerFaceZ moderator badge");
    }
}
Also used : LazyLoadEmoji(com.glitchcog.fontificator.emoji.LazyLoadEmoji) EmojiType(com.glitchcog.fontificator.emoji.EmojiType) UserType(com.glitchcog.fontificator.bot.UserType) Type(java.lang.reflect.Type) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) Room(com.glitchcog.fontificator.emoji.loader.frankerfacez.Room) JsonParser(com.google.gson.JsonParser)

Example 2 with LazyLoadEmoji

use of com.glitchcog.fontificator.emoji.LazyLoadEmoji in project ChatGameFontificator by GlitchCog.

the class EmojiParser method parseTwitchEmoteJsonV2.

/**
     * Parse emotes loaded using Twitch's emote API version 2
     * 
     * Twitch V2 API retired on February 14, 2017
     * 
     * @param manager
     * @param jsonData
     * @param jsonMapData
     * @throws IOException
     */
@Deprecated
private void parseTwitchEmoteJsonV2(EmojiManager manager, String jsonData, String jsonMapData) throws IOException {
    TypedEmojiMap emoji = manager.getEmojiByType(EmojiType.TWITCH_V2);
    JsonElement emoteElement = new JsonParser().parse(jsonData).getAsJsonObject().get("emoticons");
    Gson gson = new Gson();
    Type emoteType = new TypeToken<TwitchEmoteV2[]>() {
    }.getType();
    TwitchEmoteV2[] jsonEmoteObjects = gson.fromJson(emoteElement, emoteType);
    for (TwitchEmoteV2 e : jsonEmoteObjects) {
        // For Twitch emotes V2, there are no multi-image emotes, I think, based on the JSON structure
        LazyLoadEmoji lle = new LazyLoadEmoji(e.getRegex(), e.getUrl(), e.getWidth(), e.getHeight(), EmojiType.TWITCH_V2);
        lle.setSubscriber(e.isSubscriber_only());
        lle.setState(e.getState());
        emoji.put(e.getRegex(), lle);
    }
    logBox.log(jsonEmoteObjects.length + " Twitch emote" + (jsonEmoteObjects.length == 1 ? "" : "s") + " loaded");
}
Also used : TypedEmojiMap(com.glitchcog.fontificator.emoji.TypedEmojiMap) LazyLoadEmoji(com.glitchcog.fontificator.emoji.LazyLoadEmoji) EmojiType(com.glitchcog.fontificator.emoji.EmojiType) UserType(com.glitchcog.fontificator.bot.UserType) Type(java.lang.reflect.Type) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) TwitchEmoteV2(com.glitchcog.fontificator.emoji.loader.twitch.TwitchEmoteV2) JsonParser(com.google.gson.JsonParser)

Example 3 with LazyLoadEmoji

use of com.glitchcog.fontificator.emoji.LazyLoadEmoji in project ChatGameFontificator by GlitchCog.

the class EmojiParser method parseBetterTtvEmoteJson.

/**
     * Parse emotes loaded using the BetterTTV emote API
     * 
     * @param emoji
     * @param jsonData
     * @param isGlobal
     *            Whether the BetterTTV emotes to be loaded are the BetterTTV global emotes
     * @throws IOException
     */
private void parseBetterTtvEmoteJson(TypedEmojiMap emoji, String jsonData, boolean isGlobal) throws IOException {
    JsonParser jp = new JsonParser();
    JsonObject root = jp.parse(jsonData).getAsJsonObject();
    if (root.get("emotes").isJsonNull()) {
        logBox.log("Unable to load Better TTV global emotes");
        return;
    }
    Gson gson = new Gson();
    Type emoteType = new TypeToken<BttvEmote[]>() {
    }.getType();
    final String urlTemplate = "https:" + root.get("urlTemplate").getAsString().replace("{{image}}", "2x");
    BttvEmote[] bttvEmotes = gson.fromJson(root.get("emotes").getAsJsonArray(), emoteType);
    int bttvCount = 0;
    for (BttvEmote be : bttvEmotes) {
        LazyLoadEmoji lle = new LazyLoadEmoji(be.getCode(), urlTemplate.replace("{{id}}", be.getId()), isGlobal ? EmojiType.BETTER_TTV_GLOBAL : EmojiType.BETTER_TTV_CHANNEL);
        lle.setAnimatedGif(AnimatedGifUtil.GIF_EXTENSION.equals(be.getImageType()));
        emoji.put(be.getCode(), lle);
        bttvCount++;
    }
    logBox.log(bttvCount + " Better TTV emote" + (bttvCount == 1 ? "" : "s") + " found");
}
Also used : LazyLoadEmoji(com.glitchcog.fontificator.emoji.LazyLoadEmoji) EmojiType(com.glitchcog.fontificator.emoji.EmojiType) UserType(com.glitchcog.fontificator.bot.UserType) Type(java.lang.reflect.Type) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) JsonParser(com.google.gson.JsonParser) BttvEmote(com.glitchcog.fontificator.emoji.loader.betterttv.BttvEmote)

Example 4 with LazyLoadEmoji

use of com.glitchcog.fontificator.emoji.LazyLoadEmoji in project ChatGameFontificator by GlitchCog.

the class EmojiParser method parseFrankerFaceZEmoteJson.

/**
     * Parse emotes loaded using the FrankerFaceZ emote API
     * 
     * @param emoji
     * @param jsonData
     * @param isGlobal
     *            Whether the FFZ emotes to be loaded are the FFZ global emotes
     * @throws IOException
     */
private void parseFrankerFaceZEmoteJson(TypedEmojiMap emoji, String jsonData, boolean isGlobal) throws IOException {
    JsonParser jp = new JsonParser();
    JsonObject root = jp.parse(jsonData).getAsJsonObject();
    if (root.get("error") != null) {
        String errorMessage = "Unable to load FrankerFaceZ emotes";
        if (!root.get("message").isJsonNull()) {
            errorMessage += ": " + root.get("message");
        }
        logBox.log(errorMessage);
        return;
    } else if (root.get("sets").isJsonNull() || (isGlobal && root.get("default_sets").isJsonNull())) {
        logBox.log("Unable to load FrankerFaceZ global emotes");
        return;
    }
    List<String> setsToLoad;
    if (isGlobal) {
        setsToLoad = new ArrayList<String>();
        JsonArray defaultSetsArray = root.get("default_sets").getAsJsonArray();
        for (int i = 0; i < defaultSetsArray.size(); i++) {
            setsToLoad.add(defaultSetsArray.get(i).getAsString());
        }
    } else {
        setsToLoad = null;
    }
    JsonObject sets = root.get("sets").getAsJsonObject();
    Gson gson = new Gson();
    Type emoteType = new TypeToken<FfzEmote[]>() {
    }.getType();
    int frankerCount = 0;
    int eMultiCount = 0;
    List<String> setNames = new ArrayList<String>();
    for (Map.Entry<String, JsonElement> entry : sets.entrySet()) {
        setNames.add(entry.getKey());
        JsonElement emoteElement = entry.getValue().getAsJsonObject().get("emoticons");
        FfzEmote[] jsonEmoteObjects = gson.fromJson(emoteElement, emoteType);
        for (FfzEmote e : jsonEmoteObjects) {
            LazyLoadEmoji lle = null;
            for (String key : e.getUrls().keySet()) {
                lle = new LazyLoadEmoji(e.getName(), "http:" + e.getUrls().get(key), e.getWidth(), e.getHeight(), isGlobal ? EmojiType.FRANKERFACEZ_GLOBAL : EmojiType.FRANKERFACEZ_CHANNEL);
                break;
            }
            if (e.getUrls().size() > 1) {
                eMultiCount++;
            }
            emoji.put(e.getName(), lle);
            frankerCount++;
        }
    }
    String allSets = "";
    for (int n = 0; n < setNames.size(); n++) {
        allSets += (n == 0 ? "" : ", ") + setNames.get(n);
    }
    logBox.log(setNames.size() + " FrankerFaceZ set" + (setNames.size() == 1 ? "" : "s") + " found: {" + allSets + "}");
    logBox.log(frankerCount + " FrankerFaceZ emote" + (frankerCount == 1 ? "" : "s") + " loaded (" + eMultiCount + " multi-image emote" + (eMultiCount == 1 ? "" : "s") + ")");
}
Also used : FfzEmote(com.glitchcog.fontificator.emoji.loader.frankerfacez.FfzEmote) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) JsonArray(com.google.gson.JsonArray) LazyLoadEmoji(com.glitchcog.fontificator.emoji.LazyLoadEmoji) EmojiType(com.glitchcog.fontificator.emoji.EmojiType) UserType(com.glitchcog.fontificator.bot.UserType) Type(java.lang.reflect.Type) JsonElement(com.google.gson.JsonElement) TypedEmojiMap(com.glitchcog.fontificator.emoji.TypedEmojiMap) HashMap(java.util.HashMap) Map(java.util.Map) JsonParser(com.google.gson.JsonParser)

Example 5 with LazyLoadEmoji

use of com.glitchcog.fontificator.emoji.LazyLoadEmoji in project ChatGameFontificator by GlitchCog.

the class Message method processEmoji.

/**
     * Convert the content of the message into the appropriate emoji. Add those emoji and the remaining characters
     * between them to the specified keyList array.
     * 
     * @param content
     * @param privmsg
     * @param keyList
     *            The array to add the emoji and remaining characters to
     * @param emojiManager
     * @param emojiConfig
     * @param isManualMessage
     */
private static void processEmoji(String content, TwitchPrivmsg privmsg, List<SpriteCharacterKey> keyList, EmojiManager emojiManager, ConfigEmoji emojiConfig, boolean isManualMessage, MessageCasing casing) {
    Map<Integer, EmoteAndIndices> emotes = privmsg.getEmotes();
    String[] words = content.split(SPACE_BOUNDARY_REGEX);
    int codeIndex = 0;
    LazyLoadEmoji emoji = null;
    for (int w = 0; w < words.length; w++) {
        EmoteAndIndices eai = emotes.get(codeIndex);
        if (eai != null && emojiConfig.isTwitchEnabled()) {
            // This catches subscriber emotes and any non-global emotes
            emoji = emojiManager.getEmojiById(eai.getEmoteId(), words[w], emojiConfig);
            if (emoji == null) {
                // The already loaded Twitch V1 emoji map doesn't this emote ID yet, so add it
                try {
                    emoji = emojiManager.putEmojiById(eai.getEmoteId(), words[w], emojiConfig);
                } catch (MalformedURLException e) {
                    logger.error("Unable to load emote for emote ID " + eai.getEmoteId(), e);
                }
            }
        } else // At this point, only 3rd party emoji should be a possibility for this word (with the exception of
        // manual messages)
        {
            // This is the manual message exception
            if (isManualMessage) {
                // As a known bug here, all manual messages will have access to all Twitch emotes, regardless of
                // subscriber status
                emoji = emojiManager.getEmoji(EmojiType.MANUAL_EMOJI_TYPES, words[w], emojiConfig);
            } else // Only check 3rd party emotes
            {
                emoji = emojiManager.getEmoji(EmojiType.THIRD_PARTY_EMOJI_TYPES, words[w], emojiConfig);
            }
        }
        if (emoji == null) {
            keyList.addAll(toSpriteArray(applyCasing(words[w], casing)));
        } else {
            keyList.add(new SpriteCharacterKey(emoji, false));
        }
        // Increment the codeIndex by the current word's code point count
        // Emoji indexes are in code points, but java is counting in fixed 16-bit units
        codeIndex += words[w].codePointCount(0, words[w].length());
    }
}
Also used : LazyLoadEmoji(com.glitchcog.fontificator.emoji.LazyLoadEmoji) MalformedURLException(java.net.MalformedURLException) SpriteCharacterKey(com.glitchcog.fontificator.sprite.SpriteCharacterKey)

Aggregations

LazyLoadEmoji (com.glitchcog.fontificator.emoji.LazyLoadEmoji)10 EmojiType (com.glitchcog.fontificator.emoji.EmojiType)8 UserType (com.glitchcog.fontificator.bot.UserType)7 Gson (com.google.gson.Gson)7 JsonParser (com.google.gson.JsonParser)7 Type (java.lang.reflect.Type)7 TypedEmojiMap (com.glitchcog.fontificator.emoji.TypedEmojiMap)6 JsonElement (com.google.gson.JsonElement)5 JsonObject (com.google.gson.JsonObject)3 ArrayList (java.util.ArrayList)3 SpriteCharacterKey (com.glitchcog.fontificator.sprite.SpriteCharacterKey)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 EmojiOperation (com.glitchcog.fontificator.emoji.EmojiOperation)1 BttvEmote (com.glitchcog.fontificator.emoji.loader.betterttv.BttvEmote)1 Badge (com.glitchcog.fontificator.emoji.loader.frankerfacez.Badge)1 FfzBadgesAndUsers (com.glitchcog.fontificator.emoji.loader.frankerfacez.FfzBadgesAndUsers)1 FfzEmote (com.glitchcog.fontificator.emoji.loader.frankerfacez.FfzEmote)1 Room (com.glitchcog.fontificator.emoji.loader.frankerfacez.Room)1 TwitchBadges (com.glitchcog.fontificator.emoji.loader.twitch.TwitchBadges)1