Search in sources :

Example 1 with EmojiCancelException

use of com.glitchcog.fontificator.gui.emoji.exception.EmojiCancelException in project ChatGameFontificator by GlitchCog.

the class EmojiWorker method runLoader.

/**
     * Gives you back the data from a website, used to get JSON data for emoji, or for loading the FFZ donor list
     * 
     * @param emojiType
     * @return
     * @throws Exception
     */
private String runLoader(EmojiType emojiType) throws Exception {
    String data = null;
    if (loader.initLoad()) {
        while (!loader.isLoadComplete()) {
            int percentComplete = loader.loadChunk();
            // Let the thread rest so the main thread can get the publish
            publish(new EmojiWorkerReport("Downloading " + emojiType.getDescription(), percentComplete));
            if (terminateWork) {
                throw new EmojiCancelException();
            } else if (silentlyTerminateWork) {
                throw new EmojiHaltException();
            }
            Thread.sleep(1L);
        }
        data = loader.getLoadedJson();
        loader.reset();
        if (terminateWork) {
            throw new EmojiCancelException();
        } else if (silentlyTerminateWork) {
            throw new EmojiHaltException();
        }
        publish(new EmojiWorkerReport("Parsing " + emojiType.getDescription() + " data...", 0));
    } else {
        logger.debug("EmojiApiLoader run for " + emojiType.getDescription() + " without required call to prepLoad.");
    }
    publish(new EmojiWorkerReport(emojiType.getDescription() + " loading complete", 100));
    loader.reset();
    return data;
}
Also used : EmojiCancelException(com.glitchcog.fontificator.gui.emoji.exception.EmojiCancelException) EmojiHaltException(com.glitchcog.fontificator.gui.emoji.exception.EmojiHaltException)

Example 2 with EmojiCancelException

use of com.glitchcog.fontificator.gui.emoji.exception.EmojiCancelException in project ChatGameFontificator by GlitchCog.

the class EmojiWorker method doInBackground.

@Override
protected Integer doInBackground() throws Exception {
    if (job.getType() == null || job.getOp() == null) {
        return Integer.valueOf(1);
    }
    try {
        terminateWork = false;
        final EmojiOperation opType = job.getOp();
        final EmojiType emojiType = job.getType();
        final String channel = job.getChannel();
        final String oauth = job.getOauth();
        if (EmojiOperation.LOAD == opType) {
            // Some custom loading required to get the set map from Twitch API, if it's Twitch V2/V3 emotes being loaded
            String jsonSetMapData = null;
            if (emojiType.isLoadSetMap()) {
                loader.prepLoad(TWITCH_URL_EMOTE_ID_TO_SET_ID_MAP);
                jsonSetMapData = runLoader(emojiType);
            }
            // The proper load for the emoji
            loader.prepLoad(emojiType, channel, oauth);
            String data = runLoader(emojiType);
            if (data != null) {
                parser.putJsonEmojiIntoManager(manager, emojiType, data, jsonSetMapData);
            }
            // Some custom loading required for custom FFZ moderator badges
            if (emojiType == EmojiType.FRANKERFACEZ_BADGE) {
                loader.prepLoad(FFZ_BASE_NO_EMOTES_URL + channel);
                String ffzRoomJson = runLoader(emojiType);
                parser.parseFrankerFaceZModBadge(manager, ffzRoomJson);
            }
            // Some custom loading required for the Twitch Prime badge
            if (emojiType == EmojiType.TWITCH_BADGE) {
                TypedEmojiMap tbMap = manager.getEmojiByType(EmojiType.TWITCH_BADGE);
                tbMap.put("prime", new LazyLoadEmoji("prime", TWITCH_BADGE_PRIME, EmojiType.TWITCH_BADGE));
            }
            Thread.sleep(1L);
        } else if (EmojiOperation.CACHE == opType) {
            List<String> regexes;
            List<Integer> ids;
            if (emojiType.isTwitchEmote()) {
                // This is for Twitch emote V2 or V3. It typically will be whichever is set as the default in the
                // emoji control panel
                regexes = new ArrayList<String>(TWITCH_EMOTES_BASIC.length + TWITCH_EMOTES_GLOBAL.length);
                Collections.addAll(regexes, TWITCH_EMOTES_BASIC);
                Collections.addAll(regexes, TWITCH_EMOTES_GLOBAL);
                // This is for Twitch V1 emotes, done by ID. This is how Twitch emotes are actually used, so it
                // should always be tripped
                ids = new ArrayList<Integer>(TWITCH_EMOTE_IDS_BASIC.length + TWITCH_EMOTE_IDS_GLOBAL.length);
                Collections.addAll(ids, TWITCH_EMOTE_IDS_BASIC);
                Collections.addAll(ids, TWITCH_EMOTE_IDS_GLOBAL);
            } else if (emojiType.isFrankerFaceZEmote()) {
                Collection<String> ffzChannelRegexes = manager.getEmojiByType(EmojiType.FRANKERFACEZ_CHANNEL).keySet();
                Collection<String> ffzGlobalRegexes = manager.getEmojiByType(EmojiType.FRANKERFACEZ_GLOBAL).keySet();
                regexes = new ArrayList<String>(ffzChannelRegexes.size() + ffzGlobalRegexes.size());
                regexes.addAll(ffzChannelRegexes);
                regexes.addAll(ffzGlobalRegexes);
                // Unused for FFZ
                ids = new ArrayList<Integer>();
            } else if (emojiType.isBetterTtvEmote()) {
                Collection<String> bttvChannelRegexes = manager.getEmojiByType(EmojiType.BETTER_TTV_CHANNEL).keySet();
                Collection<String> bttvGlobalRegexes = manager.getEmojiByType(EmojiType.BETTER_TTV_GLOBAL).keySet();
                regexes = new ArrayList<String>(bttvChannelRegexes.size() + bttvGlobalRegexes.size());
                regexes.addAll(bttvChannelRegexes);
                regexes.addAll(bttvGlobalRegexes);
                // Unused for BTTV
                ids = new ArrayList<Integer>();
            } else {
                // Nothing to do
                regexes = new ArrayList<String>();
                ids = new ArrayList<Integer>();
            }
            publish(new EmojiWorkerReport("Caching " + emojiType.getDescription(), 0));
            Thread.sleep(1L);
            int count = 0;
            List<LazyLoadEmoji> emojiToCache = new ArrayList<LazyLoadEmoji>();
            for (String regex : regexes) {
                LazyLoadEmoji emoji = manager.getEmoji(emojiType, regex, null);
                emojiToCache.add(emoji);
            }
            for (Integer id : ids) {
                LazyLoadEmoji emoji = manager.getEmojiById(id, null, null);
                emojiToCache.add(emoji);
            }
            for (LazyLoadEmoji emoji : emojiToCache) {
                if (emoji != null) {
                    emoji.cacheImage();
                }
                count++;
                // This is safe from divide by zero exceptions, because we won't be here if emojiToCache is empty
                int percentComplete = (int) (100.0f * count / emojiToCache.size());
                publish(new EmojiWorkerReport("Caching " + emojiType.getDescription(), percentComplete));
                if (terminateWork) {
                    throw new EmojiCancelException();
                }
                Thread.sleep(1L);
            }
            publish(new EmojiWorkerReport(emojiType.getDescription() + " caching complete", 100));
            Thread.sleep(1L);
        }
        return Integer.valueOf(0);
    } catch (EmojiHaltException e) {
        // Don't let it get caught by the generic Exception, send it on up instead
        publish(new EmojiWorkerReport(job.toString() + " halted", 100, false, false, true));
        try {
            Thread.sleep(1L);
        } catch (Exception sleepException) {
            logger.debug("Couldn't sleep for halt", sleepException);
        }
        throw e;
    } catch (EmojiCancelException e) {
        // Don't let it get caught by the generic Exception, send it on up instead
        publish(new EmojiWorkerReport(job.toString() + " canceled", 100, false, true, false));
        try {
            Thread.sleep(1L);
        } catch (Exception sleepException) {
            logger.debug("Couldn't sleep for cancel", sleepException);
        }
        throw e;
    } catch (FileNotFoundException e) {
        publish(new EmojiWorkerReport("URL not found trying to " + job.toString(), 100, true, false, true));
        Thread.sleep(1L);
        return Integer.valueOf(2);
    } catch (Exception e) {
        e.printStackTrace();
        final String errorMsg = "Unknown error trying to " + job.getOp().getDescription() + " " + job.getType().getDescription() + (job.getChannel() != null ? " for channel " + job.getChannel() : "");
        publish(new EmojiWorkerReport(errorMsg, 100, true, false, true));
        Thread.sleep(1L);
        return Integer.valueOf(3);
    }
}
Also used : TypedEmojiMap(com.glitchcog.fontificator.emoji.TypedEmojiMap) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) EmojiCancelException(com.glitchcog.fontificator.gui.emoji.exception.EmojiCancelException) EmojiHaltException(com.glitchcog.fontificator.gui.emoji.exception.EmojiHaltException) EmojiCancelException(com.glitchcog.fontificator.gui.emoji.exception.EmojiCancelException) FileNotFoundException(java.io.FileNotFoundException) EmojiHaltException(com.glitchcog.fontificator.gui.emoji.exception.EmojiHaltException) LazyLoadEmoji(com.glitchcog.fontificator.emoji.LazyLoadEmoji) EmojiOperation(com.glitchcog.fontificator.emoji.EmojiOperation) EmojiType(com.glitchcog.fontificator.emoji.EmojiType) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

EmojiCancelException (com.glitchcog.fontificator.gui.emoji.exception.EmojiCancelException)2 EmojiHaltException (com.glitchcog.fontificator.gui.emoji.exception.EmojiHaltException)2 EmojiOperation (com.glitchcog.fontificator.emoji.EmojiOperation)1 EmojiType (com.glitchcog.fontificator.emoji.EmojiType)1 LazyLoadEmoji (com.glitchcog.fontificator.emoji.LazyLoadEmoji)1 TypedEmojiMap (com.glitchcog.fontificator.emoji.TypedEmojiMap)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1