-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLaunchConfig.java
More file actions
573 lines (507 loc) · 17.9 KB
/
LaunchConfig.java
File metadata and controls
573 lines (507 loc) · 17.9 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
package org.mcphackers.launchwrapper;
import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern;
import org.mcphackers.launchwrapper.protocol.skin.MojangSkinProvider;
import org.mcphackers.launchwrapper.protocol.skin.SkinOption;
import org.mcphackers.launchwrapper.protocol.skin.SkinType;
import org.mcphackers.launchwrapper.util.OS;
@SuppressWarnings("unused")
public class LaunchConfig {
private static final File DEFAULT_GAME_DIR = getDefaultGameDir();
// Store all arguments into this list, in order of whatever has been passed by a launcher or the user
// NOTE: this is supposed to hold String and LaunchParameter objects primarily
private final List<Object> arguments = new ArrayList<Object>();
private final Map<String, LaunchParameter<?>> parameters = new HashMap<String, LaunchParameter<?>>();
// clang-format off
// Minecraft options
public final LaunchParameterSwitch demo = new LaunchParameterSwitch("demo");
public final LaunchParameterSwitch fullscreen = new LaunchParameterSwitch("fullscreen");
public final LaunchParameterString server = new LaunchParameterString("server");
public final LaunchParameterNumber port = new LaunchParameterNumber("port", 25565);
public final LaunchParameterFile gameDir = new LaunchParameterFile("gameDir", DEFAULT_GAME_DIR).altName("workDir");
public final LaunchParameterFile assetsDir = new LaunchParameterFile("assetsDir");
public final LaunchParameterFile resourcePackDir = new LaunchParameterFile("resourcePackDir");
public final LaunchParameterString username = new LaunchParameterString("username", "Player");
public final LaunchParameterString session = new LaunchParameterString("session", "-").altName("sessionid");
public final LaunchParameterString password = new LaunchParameterString("password");
public final LaunchParameterString uuid = new LaunchParameterString("uuid");
public final LaunchParameterString accessToken = new LaunchParameterString("accessToken");
public final LaunchParameterString version = new LaunchParameterString("version");
public final LaunchParameterNumber width = new LaunchParameterNumber("width", 854);
public final LaunchParameterNumber height = new LaunchParameterNumber("height", 480);
public final LaunchParameterString userProperties = new LaunchParameterString("userProperties");
public final LaunchParameterString profileProperties = new LaunchParameterString("profileProperties");
public final LaunchParameterString assetIndex = new LaunchParameterString("assetIndex");
public final LaunchParameterString userType = new LaunchParameterString("userType");
public final LaunchParameterString versionType = new LaunchParameterString("versionType");
// Applet arguments
private final LaunchParameterSwitch haspaid = new LaunchParameterSwitch("haspaid", true);
public final LaunchParameterString loadmapUser = new LaunchParameterString("loadmap_user");
public final LaunchParameterNumber loadmapId = new LaunchParameterNumber("loadmap_id");
public final LaunchParameterString mppass = new LaunchParameterString("mppass", "");
// Built-in wrapper options
public final LaunchParameterFile debugClassDump = new LaunchParameterFile("debugClassDump", null, true);
public final LaunchParameterString tweakClass = new LaunchParameterString("tweakClass", null, true);
public final LaunchParameterString brand = new LaunchParameterString("brand", "launchwrapper", true);
public final LaunchParameterString realmsServer = new LaunchParameterString("realmsServer", null, true);
public final LaunchParameterNumber realmsPort = new LaunchParameterNumber("realmsPort", 80, true);
public final LaunchParameterSwitch lwjglFrame = new LaunchParameterSwitch("lwjglFrame", true, true);
private final LaunchParameterSwitch awtFrame = new LaunchParameterSwitchReverse("awtFrame", lwjglFrame);
public final LaunchParameterSwitch modernSkinProxy = new LaunchParameterSwitch("useSkinProxy", false, true);
public final LaunchParameterEnum<SkinType> skinProxy = new LaunchParameterEnum<SkinType>("skinProxy", SkinType.DEFAULT, true);
public final LaunchParameterSkinOptions skinOptions = new LaunchParameterSkinOptions("skinOptions");
public final LaunchParameterFile levelsDir = new LaunchParameterFile("levelsDir", null, true);
public final LaunchParameterSwitch useFakeApplet = new LaunchParameterSwitch("useFakeApplet", false, true);
public final LaunchParameterSwitch isom = new LaunchParameterSwitch("isom", false, true);
public final LaunchParameterFileList icon = new LaunchParameterFileList("icon", null, true);
public final LaunchParameterString title = new LaunchParameterString("title", null, true);
public final LaunchParameterSwitch vsync = new LaunchParameterSwitch("vsync", false, true);
public final LaunchParameterSwitch applet = new LaunchParameterSwitch("applet", false);
public final LaunchParameterSwitch oneSixFlag = new LaunchParameterSwitch("oneSixFlag", false, true);
public final LaunchParameterSwitch unlicensedCopy = new LaunchParameterSwitchReverse("unlicensedCopy", haspaid);
public final LaunchParameterSwitch creative = new LaunchParameterSwitch("creative", false, true);
public final LaunchParameterSwitch survival = new LaunchParameterSwitch("survival", false, true);
public final LaunchParameterString serverURL = new LaunchParameterString("serverURL", null, true);
public final LaunchParameterString serverSHA1 = new LaunchParameterString("serverSHA1", null, true);
public final LaunchParameterSwitch disableSkinFix = new LaunchParameterSwitch("disableSkinFix", false, true);
// clang-format on
private static File getDefaultGameDir() {
String game = "minecraft";
String homeDir = System.getProperty("user.home", ".");
File gameDir;
switch (OS.getOs()) {
case LINUX:
case SOLARIS:
String dataHome = System.getProperty("XDG_DATA_HOME");
if (dataHome != null) {
gameDir = new File(dataHome, "." + game + '/');
} else {
gameDir = new File(homeDir, ".local/share/" + game + '/');
}
break;
case WINDOWS:
String appdata = System.getenv("APPDATA");
if (appdata != null) {
gameDir = new File(appdata, "." + game + '/');
} else {
gameDir = new File(homeDir, '.' + game + '/');
}
break;
case OSX:
gameDir = new File(homeDir, "Library/Application Support/" + game);
break;
default:
gameDir = new File(homeDir, game + '/');
}
return gameDir;
}
@SuppressWarnings("unchecked")
@Override
public LaunchConfig clone() {
LaunchConfig newConfig = new LaunchConfig();
for (Object argument : arguments) {
if (argument == null) {
continue;
}
if (argument instanceof LaunchParameter) {
LaunchParameter<Object> param = (LaunchParameter<Object>)newConfig.parameters.get(((LaunchParameter<?>)argument).name);
if (param == null) {
continue;
}
// This should add the new config's parameters to its arguments list
newConfig.arguments.add(param);
} else {
newConfig.arguments.add(argument);
}
}
for (Map.Entry<String, LaunchParameter<?>> entry : parameters.entrySet()) {
LaunchParameter<Object> param = (LaunchParameter<Object>)newConfig.parameters.get(entry.getKey());
if (param == null) {
continue;
}
param.set(entry.getValue().get());
}
return newConfig;
}
public LaunchConfig() {
levelsDir.set(new File(DEFAULT_GAME_DIR, "levels"));
}
public LaunchConfig(String[] args) {
for (int i = 0; i < args.length; i++) {
if (!args[i].startsWith("--")) {
arguments.add(args[i]);
continue;
}
String paramName = args[i].substring(2);
LaunchParameter<?> param = parameters.get(paramName);
if (param == null) {
arguments.add(args[i]);
continue;
}
if (param.isSwitch()) {
((LaunchParameterSwitch)param).setFlag();
arguments.add(param);
continue;
}
if (i + 1 < args.length) {
try {
param.setString(args[i + 1]);
i++;
} catch (IllegalArgumentException ignored) {
}
}
arguments.add(param);
}
for (LaunchParameter<?> param : parameters.values()) {
if (!arguments.contains(param)) {
arguments.add(param);
}
}
if (levelsDir.get() == null) {
levelsDir.set(new File(gameDir.get(), "levels"));
}
if (uuid.get() == null && username.get() != null) {
// Resolve profile UUID if we only provide username
uuid.set(MojangSkinProvider.getUUIDfromName(username.get()));
}
if (session.get() != null && session.get().startsWith("token:")) {
// Parse the session ID for the access token if we don't have it
// 'token:<ACCESS_TOKEN>:<UUID>'
String[] token = session.get().split(":");
if (accessToken.get() == null) {
accessToken.set(token[1]);
}
if (uuid.get() == null) {
uuid.set(token[2]);
}
}
if (uuid.get() == null) {
// If uuid is still null, generate random one (Fixes LAN play when there is no internet connection)
uuid.set(UUID.randomUUID().toString().replace("-", ""));
}
}
public List<LaunchParameter<?>> getParamsAsList() {
List<LaunchParameter<?>> list = new ArrayList<LaunchParameter<?>>();
for (LaunchParameter<?> param : parameters.values()) {
if (!param.wrapperOnly && param.get() != null && !param.get().equals(Boolean.FALSE)) {
list.add(param);
}
}
return list;
}
public Map<String, String> getArgsAsMap() {
// LinkedHashMap as arguments should be returned in a consistent order
Map<String, String> map = new LinkedHashMap<String, String>();
for (Object argument : arguments) {
if (argument == null) {
continue;
}
if (argument instanceof LaunchParameter) {
LaunchParameter<?> param = (LaunchParameter<?>)argument;
if (!param.wrapperOnly && param.get() != null && !param.get().equals(Boolean.FALSE)) {
map.put(param.name, param.getString());
if (param.getAltName() != null && param.getAltName().length() > 0) {
map.put(param.getAltName(), param.getString());
}
}
} else {
map.put(argument.toString(), null);
}
}
return map;
}
public String[] getArgs() {
List<String> list = new ArrayList<String>();
for (Object argument : arguments) {
if (argument == null) {
continue;
}
if (argument instanceof LaunchParameter) {
LaunchParameter<?> param = (LaunchParameter<?>)argument;
if (!param.wrapperOnly && param.get() != null) {
if (param.isSwitch()) {
if (param.get().equals(Boolean.TRUE)) {
list.add("--" + param.name);
if (param.getAltName() != null && param.getAltName().length() > 0) {
list.add("--" + param.getAltName());
}
}
} else {
list.add("--" + param.name);
list.add(param.getString());
if (param.getAltName() != null && param.getAltName().length() > 0) {
list.add("--" + param.getAltName());
list.add(param.getString());
}
}
}
} else {
list.add(argument.toString());
}
}
String[] arr = new String[list.size()];
return list.toArray(arr);
}
public abstract class LaunchParameter<T> {
public final String name;
public final boolean wrapperOnly;
protected String altName;
protected final T defaultValue;
protected T value;
protected LaunchParameter(String name) {
this(name, null, false);
}
protected LaunchParameter(String name, T defaultValue) {
this(name, defaultValue, false);
}
protected LaunchParameter(String name, T defaultValue, boolean wrapper) {
this.defaultValue = defaultValue;
this.value = defaultValue;
this.name = name;
this.wrapperOnly = wrapper;
this.altName = null;
// Parameters names are case sensitive
parameters.put(name, this);
}
public LaunchParameter<T> altName(String altName) {
this.altName = altName;
parameters.put(altName, this);
return this;
}
public String getAltName() {
return this.altName;
}
public boolean isSwitch() {
return false;
}
public abstract String getString();
public abstract void setString(String argument);
public T get() {
return this.value;
}
public void set(T value) {
this.value = value;
}
}
public class LaunchParameterFile extends LaunchParameter<File> {
public LaunchParameterFile(String name) {
super(name);
}
public LaunchParameterFile(String name, File defaultValue) {
super(name, defaultValue);
}
public LaunchParameterFile(String name, File defaultValue, boolean wrapper) {
super(name, defaultValue, wrapper);
}
@Override
public LaunchParameterFile altName(String altName) {
super.altName(altName);
return this;
}
@Override
public String getString() {
if (value == null) {
return null;
}
return this.value.getAbsolutePath();
}
@Override
public void setString(String argument) {
this.value = new File(argument);
}
}
public class LaunchParameterFileList extends LaunchParameter<File[]> {
public LaunchParameterFileList(String name) {
super(name);
}
public LaunchParameterFileList(String name, File[] defaultValue) {
super(name, defaultValue);
}
public LaunchParameterFileList(String name, File[] defaultValue, boolean wrapper) {
super(name, defaultValue, wrapper);
}
@Override
public String getString() {
if (value == null) {
return null;
}
StringBuilder s = new StringBuilder();
for (int i = 0; i < value.length; i++) {
if (i != 0) {
s.append(File.pathSeparator);
}
s.append(value[i].getAbsolutePath());
}
return s.toString();
}
@Override
public void setString(String argument) {
String[] paths = argument.split(Pattern.quote(File.pathSeparator));
File[] newValue = new File[paths.length];
for (int i = 0; i < paths.length; i++) {
newValue[i] = new File(paths[i]);
}
this.value = newValue;
}
}
public class LaunchParameterSwitchReverse extends LaunchParameterSwitch {
LaunchParameterSwitch parent;
public LaunchParameterSwitchReverse(String name, LaunchParameterSwitch parent) {
super(name, !parent.defaultValue, true);
this.parent = parent;
}
@Override
public void set(Boolean b) {
super.set(b);
parent.value = !value;
}
@Override
public Boolean get() {
return (!parent.value);
}
}
public class LaunchParameterSwitch extends LaunchParameter<Boolean> {
public LaunchParameterSwitch(String name) {
super(name, false);
}
public LaunchParameterSwitch(String name, Boolean defaultVal) {
super(name, defaultVal);
}
public LaunchParameterSwitch(String name, Boolean defaultVal, boolean wrapper) {
super(name, defaultVal, wrapper);
}
@Override
public boolean isSwitch() {
return true;
}
@Override
public String getString() {
return Boolean.toString(value);
}
@Override
public void setString(String argument) {
this.value = "true".equalsIgnoreCase(argument);
}
public void setFlag() {
this.set(true);
}
public void toggle() {
this.set(!value);
}
}
public class LaunchParameterString extends LaunchParameter<String> {
public LaunchParameterString(String name) {
super(name);
}
public LaunchParameterString(String name, String defaultValue) {
super(name, defaultValue);
}
public LaunchParameterString(String name, String defaultValue, boolean wrapper) {
super(name, defaultValue, wrapper);
}
@Override
public LaunchParameterString altName(String altName) {
super.altName(altName);
return this;
}
@Override
public String getString() {
return value;
}
@Override
public void setString(String argument) {
this.value = argument;
}
}
public class LaunchParameterNumber extends LaunchParameter<Integer> {
public LaunchParameterNumber(String name) {
super(name);
}
public LaunchParameterNumber(String name, Integer defaultValue) {
super(name, defaultValue);
}
public LaunchParameterNumber(String name, Integer defaultValue, boolean wrapper) {
super(name, defaultValue, wrapper);
}
@Override
public String getString() {
return value.toString();
}
@Override
public void setString(String argument) {
this.value = Integer.parseInt(argument);
}
}
public class LaunchParameterSkinOptions extends LaunchParameter<List<SkinOption>> {
public LaunchParameterSkinOptions(String name) {
super(name, Collections.<SkinOption>emptyList(), true);
}
@Override
public String getString() {
if (value == null) {
return null;
}
StringBuilder s = new StringBuilder();
int i = 0;
for (SkinOption option : value) {
if (i != 0) {
s.append(',');
}
s.append(option.name);
i++;
}
return s.toString();
}
@Override
public void setString(String argument) {
String[] optionStrings = argument.split(Pattern.quote(","));
ArrayList<SkinOption> newValue = new ArrayList<SkinOption>();
for (String optionString : optionStrings) {
SkinOption opt = SkinOption.getEnum(optionString);
if (opt != null) {
newValue.add(opt);
}
}
this.value = newValue;
}
}
public class LaunchParameterEnum<T extends Enum<T>> extends LaunchParameter<T> {
private final Class<T> enumType;
@SuppressWarnings("unchecked")
public LaunchParameterEnum(String name, Enum<T> defaultValue) {
super(name, (T)defaultValue);
enumType = defaultValue.getDeclaringClass();
}
@SuppressWarnings("unchecked")
public LaunchParameterEnum(String name, Enum<T> defaultValue, boolean wrapper) {
super(name, (T)defaultValue, wrapper);
enumType = defaultValue.getDeclaringClass();
}
@Override
public String getString() {
return value.toString();
}
@SuppressWarnings("unchecked")
@Override
public void setString(String argument) {
try {
Method getEnumFromString = enumType.getDeclaredMethod("getEnum", String.class);
if ((getEnumFromString.getModifiers() & Modifier.STATIC) != 0) {
this.value = (T)getEnumFromString.invoke(null, argument);
return;
}
} catch (Exception ignored) {
}
this.value = Enum.valueOf(enumType, argument);
}
}
}