|
1 | 1 | package com.gikk.twirk.types.emote; |
2 | 2 |
|
3 | | -import java.util.LinkedList; |
4 | | -import java.util.List; |
5 | | - |
6 | | -public class EmoteParser { |
7 | | - private static final String EMOTES_IDENTIFIER = "emotes="; |
8 | | - |
9 | | - public static List<Emote> parseEmotes(String content, String tag) { |
10 | | - /* Emotes come in sets formated like this: |
11 | | - * |
12 | | - * emotes=15614:0-6/4685:8-13,15-21 (Message = tmrToad tmrHat tmrHat) |
13 | | - * |
14 | | - * The first number indicates the emotes ID. The emote ID is follower by a : |
15 | | - * The numbers separated by a - indicates the indices in the message that makes up the emote. |
16 | | - * If an emote appears several times in a message, the different index ranges are separated by a , |
17 | | - * |
18 | | - * So we find the begining of the emotes section and the end of the section. |
19 | | - * Then, check that the message actually contains an emotes section and that |
20 | | - * the emote section actually contains data. |
21 | | - */ |
22 | | - List<Emote> emotes = new LinkedList<>(); |
23 | | - |
24 | | - int begin = tag.indexOf( EMOTES_IDENTIFIER ); |
25 | | - int end = tag.indexOf(';', begin ); |
26 | | - if( begin == -1 || begin + EMOTES_IDENTIFIER.length() == end){ |
27 | | - return emotes; |
28 | | - } |
29 | | - |
30 | | - /* This segment is a head ache, but here is a basic idea of what it is doing: |
31 | | - * Iterate through the emotes-section. |
32 | | - * The first part will be the emote ID. it is terminated by a : |
33 | | - * The second part is the emote's start index. It is terminated by a - |
34 | | - * The third part is the emote's end index. It is terminated EITHER by a / or a , or by the segment ending |
35 | | - * If terminated by a , the emote is present more than once in the message. So the next part will be a new begin index |
36 | | - * If terminated by a / there is another emote in the message. The next part is a new emote ID |
37 | | - * If terminated by the segment ending we simply finish the last begin-end pair and we are done |
38 | | - */ |
39 | | - String emotesString = tag.substring( begin + EMOTES_IDENTIFIER.length(), end); |
| 3 | +import com.gikk.twirk.types.TagMap; |
40 | 4 |
|
41 | | - EmoteImpl emote = new EmoteImpl(); |
42 | | - StringBuilder str = new StringBuilder(); |
43 | | - String emoteID = "", beginIndex = ""; |
44 | | - for( char c : emotesString.toCharArray() ){ |
45 | | - switch(c) { |
46 | | - case ':': |
47 | | - emoteID = str.toString(); |
48 | | - str.setLength(0); |
49 | | - break; |
50 | | - case '-' : |
51 | | - beginIndex = str.toString(); |
52 | | - str.setLength(0); |
53 | | - break; |
54 | | - case ',': |
55 | | - addIndices(emote, beginIndex, str.toString() ); |
56 | | - str.setLength(0); |
57 | | - break; |
58 | | - case '/': |
59 | | - finalizeEmote(content, emotes, emote, emoteID, beginIndex, str.toString() ); |
60 | | - emote = new EmoteImpl(); |
61 | | - str.setLength(0); |
62 | | - break; |
63 | | - default: |
64 | | - str.append(c); |
65 | | - } |
66 | | - } |
67 | | - //The emotes segment ended, so we finish the current emote's begin-end pair, then we're done |
68 | | - finalizeEmote(content, emotes, emote, emoteID, beginIndex, str.toString()); |
69 | | - |
70 | | - return emotes; |
71 | | - } |
72 | | - |
73 | | - private static void finalizeEmote(String content, List<Emote> emotes, EmoteImpl emote, String emoteID, String beginIndex, String endIndex) { |
74 | | - int begin = Integer.parseInt( beginIndex ); |
75 | | - int end = Integer.parseInt( endIndex ) + 1; //The end index we receive from Twitch is inclusive, but Java is almost always exclusive |
76 | | - emote.addIndices(begin, end); |
77 | | - |
78 | | - emote.setEmoteID( Integer.parseInt( emoteID ) ); |
79 | | - emote.setPattern( content.substring(begin, end) ); |
80 | | - emotes.add(emote); |
81 | | - } |
| 5 | +import java.util.List; |
82 | 6 |
|
83 | | - private static void addIndices(EmoteImpl emote, String beginIndex, String endIndex){ |
84 | | - int begin = Integer.parseInt( beginIndex ); |
85 | | - int end = Integer.parseInt( endIndex ) + 1; |
86 | | - emote.addIndices(begin, end); |
87 | | - } |
| 7 | +public interface EmoteParser { |
| 8 | + /** |
| 9 | + * @deprecated Use {{@link #parseEmotes(TagMap, String)} instead. This will be removed in a future release} |
| 10 | + */ |
| 11 | + @Deprecated |
| 12 | + static List<Emote> parseEmotes(String content, String tag) { |
| 13 | + TagMap tagMap = TagMap.getDefault(tag); |
| 14 | + return new EmoteParserImpl().parseEmotes(tagMap, content); |
| 15 | + } |
| 16 | + |
| 17 | + static EmoteParser getDefaultImplementation() { |
| 18 | + return new EmoteParserImpl(); |
| 19 | + } |
| 20 | + |
| 21 | + List<Emote> parseEmotes(TagMap tagMap, String content); |
88 | 22 | } |
0 commit comments