WebPreferencesManager.java
13.1 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package com.mumfrey.webprefs;
import java.io.File;
import java.net.Proxy;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.mojang.authlib.GameProfile;
import com.mojang.realmsclient.dto.RealmsServer;
import com.mumfrey.liteloader.JoinGameListener;
import com.mumfrey.liteloader.Tickable;
import com.mumfrey.liteloader.core.LiteLoader;
import com.mumfrey.webprefs.exceptions.InvalidServiceException;
import com.mumfrey.webprefs.exceptions.InvalidUUIDException;
import com.mumfrey.webprefs.framework.WebPreferencesProvider;
import com.mumfrey.webprefs.interfaces.IWebPreferences;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ServerData;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.network.INetHandler;
import net.minecraft.network.play.server.SPacketJoinGame;
import net.minecraft.util.Session;
/**
* WebPreferences Service Manager, acts as a central registry for all web
* preference collections.
*
* <p>To access preferences on a service, first request the service using
* {@link #get(String)} or {@link #getDefault()}, you can then request
* preferences objects from the service by calling the various overloads of
* {@link #getPreferences}.</p>
*
* @author Adam Mummery-Smith
*/
public final class WebPreferencesManager
{
/**
* WebPreferences Manager Update Daemon is injected into LiteLoader to
* facilitate passing events to the WebPreferences Manager without having to
* expose public callback methods.
*
* @author Adam Mummery-Smith
*/
static class WebPreferencesUpdateDeamon implements Tickable, JoinGameListener
{
@Override
public String getName()
{
return "Web Preferences Update Daemon";
}
@Override
public String getVersion()
{
return "N/A";
}
@Override
public void init(File configPath)
{
}
@Override
public void upgradeSettings(String version, File configPath, File oldConfigPath)
{
}
@Override
public void onTick(Minecraft minecraft, float partialTicks, boolean inGame, boolean clock)
{
if (clock)
{
for (WebPreferencesManager manager : WebPreferencesManager.managers.values())
{
manager.onTick();
}
}
}
@Override
public void onJoinGame(INetHandler netHandler, SPacketJoinGame joinGamePacket, ServerData serverData, RealmsServer realmsServer)
{
for (WebPreferencesManager manager : WebPreferencesManager.managers.values())
{
manager.onJoinGame();
}
}
}
/**
* Default KV api hostname to connect to
*/
private static final String DEFAULT_HOSTNAME = "kv.liteloader.com";
/**
* Regex for validating UUIDs
*/
private static final Pattern uuidPattern = Pattern.compile("^[a-f0-9]{32}$");
/**
* Mapping of hostnames to managers
*/
static final Map<String, WebPreferencesManager> managers = new LinkedHashMap<String, WebPreferencesManager>();
/**
* Update daemon
*/
private static WebPreferencesUpdateDeamon updateDeamon;
/**
* Session for this instance
*/
private final Session session;
/**
* Preference provider, manages queueing requests and passing responses back
* to clients
*/
private final WebPreferencesProvider provider;
/**
* All preference sets, for iteration purposes
*/
private final List<AbstractWebPreferences> allPreferences = new LinkedList<AbstractWebPreferences>();
/**
* All public preference sets, mapped by UUID
*/
private final Map<String, IWebPreferences> preferencesPublic = new HashMap<String, IWebPreferences>();
/**
* All private preference sets, mapped by UUID
*/
private final Map<String, IWebPreferences> preferencesPrivate = new HashMap<String, IWebPreferences>();
private WebPreferencesManager(Proxy proxy, Session session, String hostName)
{
this.session = session;
this.provider = new WebPreferencesProvider(proxy, session, hostName, 50);
}
void onTick()
{
this.provider.onTick();
for (AbstractWebPreferences prefs : this.allPreferences)
{
try
{
prefs.onTick();
}
catch (Exception ex) {}
}
}
void onJoinGame()
{
for (AbstractWebPreferences prefs : this.allPreferences)
{
try
{
prefs.poll();
}
catch (Exception ex) {}
}
}
/**
* Get a public or private preferences collection for the local player. If
* the game is running in offline mode, a local preference collection is
* returned instead.
*
* @param privatePrefs true to fetch the player's private preferences, false
* to fetch public preferences
* @return player's preference collection, creates if necessary
*/
public IWebPreferences getLocalPreferences(boolean privatePrefs)
{
try
{
return this.getPreferences(this.session.getPlayerID(), privatePrefs);
}
catch (InvalidUUIDException ex)
{
UUID offlineUUID = EntityPlayer.getOfflineUUID(this.session.getUsername());
return this.getOfflinePreferences(offlineUUID, privatePrefs, false, false);
}
}
/**
* Get a public preferences collection for the specified player. If the game
* is running in offline mode, a dummy preference collection supporting no
* operations is returned instead.
*
* @param player Player to fetch preferences for
* @return Preference collection or <tt>null</tt> if the player's profile
* cannot be retrieved
*/
public IWebPreferences getPreferences(EntityPlayer player)
{
try
{
return this.getPreferences(player, false);
}
catch (InvalidUUIDException ex)
{
String playerName = player.getName();
UUID offlineUUID = EntityPlayer.getOfflineUUID(playerName);
return this.getOfflinePreferences(offlineUUID, false, false, !playerName.equals(this.session.getUsername()));
}
}
/**
* Get a public or private preferences collection for the specified player,
* note that accessing a private collection for another player is likely
* to be prohibited by the service.
*
* @param player Player to fetch preferences for
* @param privatePrefs True to fetch the player's private preferences, false
* to fetch the public preferences
* @return Preference collection or <tt>null</tt> if the player's profile
* cannot be retrieved
*/
public IWebPreferences getPreferences(EntityPlayer player, boolean privatePrefs)
{
GameProfile gameProfile = player.getGameProfile();
return gameProfile != null ? this.getPreferences(gameProfile, privatePrefs) : null;
}
/**
* Get a public preferences collection for the specified game profile.
*
* @param gameProfile game profile to fetch preferences for
* @return Preference collection or <tt>null</tt> if the supplied profile is
* null
*/
public IWebPreferences getPreferences(GameProfile gameProfile)
{
return gameProfile != null ? this.getPreferences(gameProfile, false) : null;
}
/**
* Get a public or private preferences collection for the specified game
* profile, note that accessing a private collection for another player is
* likely to be prohibited by the service.
*
* @param gameProfile game profile to fetch preferences for
* @param privatePrefs True to fetch the player's private preferences, false
* to fetch the public preferences
* @return Preference collection or <tt>null</tt> if the supplied profile is
* null
*/
public IWebPreferences getPreferences(GameProfile gameProfile, boolean privatePrefs)
{
return gameProfile != null ? this.getPreferences(gameProfile.getId(), privatePrefs) : null;
}
/**
* Get a public preferences collection for the specified player UUID.
*
* @param uuid UUID to fetch preferences for
* @return Preference collection or <tt>null</tt> if the supplied UUID is
* null
*/
public IWebPreferences getPreferences(UUID uuid)
{
return uuid != null ? this.getPreferences(uuid, false) : null;
}
/**
* Get a public or private preferences collection for the specified player
* UUID, note that accessing a private collection for another player is
* likely to be prohibited by the service.
*
* @param uuid UUID to fetch preferences for
* @param privatePrefs True to fetch the player's private preferences, false
* to fetch the public preferences
* @return Preference collection or <tt>null</tt> if the supplied UUID is
* null
*/
public IWebPreferences getPreferences(UUID uuid, boolean privatePrefs)
{
return uuid != null ? this.getPreferences(uuid.toString(), privatePrefs) : null;
}
public IWebPreferences getPreferences(String uuid, boolean privatePrefs)
{
uuid = this.sanitiseUUID(uuid);
Map<String, IWebPreferences> preferences = privatePrefs ? this.preferencesPrivate : this.preferencesPublic;
IWebPreferences prefs = preferences.get(uuid);
if (prefs == null)
{
WebPreferences newPrefs = new WebPreferences(this.provider, uuid, privatePrefs, !uuid.equals(this.session.getPlayerID()));
this.allPreferences.add(newPrefs);
preferences.put(uuid, newPrefs);
prefs = newPrefs;
}
return prefs;
}
private IWebPreferences getOfflinePreferences(UUID uuid, boolean privatePrefs, boolean readOnly, boolean dummy)
{
Map<String, IWebPreferences> preferences = privatePrefs ? this.preferencesPrivate : this.preferencesPublic;
IWebPreferences prefs = preferences.get(uuid);
if (prefs == null)
{
AbstractWebPreferences newPrefs = dummy
? new DummyOfflineWebPreferences(uuid, privatePrefs, readOnly)
: new OfflineWebPreferences(uuid, privatePrefs, readOnly);
this.allPreferences.add(newPrefs);
preferences.put(uuid.toString(), newPrefs);
prefs = newPrefs;
}
return prefs;
}
private String sanitiseUUID(String uuid)
{
if (uuid == null)
{
throw new InvalidUUIDException("The UUID was null");
}
uuid = uuid.toLowerCase().replace("-", "").trim();
Matcher uuidPatternMatcher = WebPreferencesManager.uuidPattern.matcher(uuid);
if (!uuidPatternMatcher.matches())
{
throw new InvalidUUIDException("The specified string [" + uuid + "] is not a valid UUID");
}
return uuid;
}
/**
* Get the default preferences manager (kv.liteloader.com)
*
* @return default preferences manager
*/
public static WebPreferencesManager getDefault()
{
return WebPreferencesManager.get(WebPreferencesManager.DEFAULT_HOSTNAME);
}
/**
* Get a preferences manager for the specified service hostname
*
* @param hostName service hostname (bare hostname only, no protocol)
* @return preferences manager
* @throws InvalidServiceException if the specified host name is invalid
*/
@SuppressWarnings("unused")
public static WebPreferencesManager get(String hostName) throws InvalidServiceException
{
try
{
new URI(String.format("http://%s/", hostName));
}
catch (URISyntaxException ex)
{
throw new InvalidServiceException("The specified service host was not valid: " + hostName, ex);
}
if (WebPreferencesManager.updateDeamon == null)
{
WebPreferencesManager.updateDeamon = new WebPreferencesUpdateDeamon();
LiteLoader.getInterfaceManager().registerListener(WebPreferencesManager.updateDeamon);
}
WebPreferencesManager manager = WebPreferencesManager.managers.get(hostName);
if (manager == null)
{
Minecraft minecraft = Minecraft.getMinecraft();
Proxy proxy = minecraft.getProxy();
Session session = minecraft.getSession();
manager = new WebPreferencesManager(proxy, session, hostName);
WebPreferencesManager.managers.put(hostName, manager);
}
return manager;
}
}