-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStreamotes.java
More file actions
200 lines (173 loc) · 5.66 KB
/
Copy pathStreamotes.java
File metadata and controls
200 lines (173 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package xeed.mc.streamotes;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
import xeed.mc.streamotes.addon.TwitchEmotesAPI;
import xeed.mc.streamotes.addon.pack.*;
import xeed.mc.streamotes.api.EmoteLoaderException;
import xeed.mc.streamotes.emoticon.EmoticonRegistry;
import javax.imageio.ImageIO;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.regex.Pattern;
public class Streamotes {
public static final Pattern EMOTE_PATTERN = Pattern.compile("[^\\s:]{2,}|:?[^\\s:]+:?", Pattern.UNICODE_CHARACTER_CLASS);
public static Streamotes INSTANCE;
private final AtomicInteger LOAD_COUNTER = new AtomicInteger(0);
private ModConfigModel ovConfig = null;
public static void log(String text) {
StreamotesCommon.logi(text);
}
public static void loge(String text, Throwable t) {
StreamotesCommon.loge(text, t);
msg(text);
}
public static void msg(String text) {
var mc = Minecraft.getInstance();
var mode = StreamotesCommon.getOwnConfig().errorReporting;
if (mode == ReportOption.Toast) {
var title = Component.literal("Streamotes");
var msg = Component.literal(text);
Compat.sendToastMessage(title, msg);
}
else if (mode == ReportOption.Chat) {
Compat.sendClientMessage(Component.literal("Streamotes: " + text));
}
}
public ModConfigModel getConfig() {
return ovConfig != null ? ovConfig : StreamotesCommon.getOwnConfig();
}
public void onInitializeClient() {
INSTANCE = this;
ImageIO.scanForPlugins();
}
public void onClientStarted(Minecraft client) {
TwitchEmotesAPI.initialize(client.gameDirectory);
}
public void onPlayerJoin() {
reloadEmoticons();
}
public void onPlayerDisconnect() {
ovConfig = null;
}
public void onReceiveJsonPacket(String json) {
var cfg = StreamotesCommon.configFromJson(json);
if (cfg == null) {
log("Received invalid config JSON: " + json);
msg("Received invalid emote config! Contact server admin.");
}
else {
ovConfig = cfg;
msg("Received emote config, starting loading");
if (cfg.forceClearCache) {
msg("Force cache clear requested, clearing cache");
TwitchEmotesAPI.clearFileCache();
TwitchEmotesAPI.clearJsonCache();
cfg.forceClearCache = false;
}
if (StreamotesCommon.getOwnConfig().versionCode != cfg.versionCode) {
loge("Server reported different mod version: " + cfg.versionName, null);
}
reloadEmoticons();
}
}
private void startLoadingDaemon(String name, int loadId, Runnable action) {
EmoticonRegistry.startLoading();
var thread = new Thread(() -> {
try {
action.run();
}
finally {
if (EmoticonRegistry.endLoading() && LOAD_COUNTER.get() == loadId) {
var emotes = EmoticonRegistry.getEmoteNames();
msg("Finished loading, " + emotes.size() + " emotes from " + getConfig().emoteChannels.size() + " channels");
}
}
}, name);
thread.setDaemon(true);
thread.start();
}
public static void sleepSweetPrince(int millis) {
try {
Thread.sleep(millis);
}
catch (InterruptedException ignored) {
}
}
public static EmoteLoaderException tryFewTimes(Runnable func, int maxTries) {
int nr = 0;
while (true) {
try {
func.run();
return null;
}
catch (EmoteLoaderException t) {
if (--maxTries <= 0) return t;
sleepSweetPrince((++nr) * 50);
}
}
}
private void reloadEmoticons() {
final int loadId = LOAD_COUNTER.incrementAndGet();
var thread = new Thread(() -> {
while (EmoticonRegistry.isLoading()) {
if (LOAD_COUNTER.get() != loadId) return;
sleepSweetPrince(10);
}
if (LOAD_COUNTER.get() != loadId) return;
EmoticonRegistry.reloadEmoticons();
Compat.clearLayerCache();
Minecraft.getInstance().execute(EmoticonRegistry::runDisposal);
final var cfg = getConfig();
final var channelList = new ArrayList<>(cfg.emoteChannels);
if (cfg.x7tvEmotes || cfg.x7tvChannelEmotes) {
processPacks("7TV", loadId, channelList,
cfg.x7tvEmotes ? X7tvPack::loadMetadata : null,
cfg.x7tvChannelEmotes ? X7tvChannelPack::loadMetadata : null);
}
if (cfg.ffzEmotes || cfg.ffzChannelEmotes) {
processPacks("FFZ", loadId, channelList,
cfg.ffzEmotes ? FFZPack::loadMetadata : null,
cfg.ffzChannelEmotes ? FFZChannelPack::loadMetadata : null);
}
if (cfg.bttvEmotes || cfg.bttvChannelEmotes) {
processPacks("BTTV", loadId, channelList,
cfg.bttvEmotes ? BTTVPack::loadMetadata : null,
cfg.bttvChannelEmotes ? BTTVChannelPack::loadMetadata : null);
}
if (cfg.twitchGlobalEmotes || cfg.twitchSubscriberEmotes) {
processPacks("Twitch", loadId, channelList,
cfg.twitchGlobalEmotes ? TwitchGlobalPack::loadMetadata : null,
cfg.twitchSubscriberEmotes ? TwitchSubscriberPack::loadMetadata : null);
}
}, "Emote Load Manager");
thread.setDaemon(true);
thread.start();
}
public void processPacks(String sourceName, int loadId, ArrayList<String> channelList, Runnable globLoader, Consumer<String> subLoader) {
startLoadingDaemon(sourceName + " Emote Loader", loadId, () -> {
if (globLoader != null) {
try {
if (LOAD_COUNTER.get() != loadId) return;
var ex = tryFewTimes(globLoader, 10);
if (ex != null) throw ex;
}
catch (EmoteLoaderException e) {
loge("Failed to load " + sourceName + " global emotes", e);
}
}
if (subLoader != null) {
for (String channel : channelList) {
try {
if (LOAD_COUNTER.get() != loadId) return;
var ex = tryFewTimes(() -> subLoader.accept(channel), 10);
if (ex != null) throw ex;
}
catch (EmoteLoaderException e) {
loge("Failed to load " + sourceName + " " + channel + " emotes", e);
}
}
}
});
}
}