StartupEnvironment.java
7.49 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
/*
* This file is part of LiteLoader.
* Copyright (C) 2012-16 Adam Mummery-Smith
* All Rights Reserved.
*/
package com.mumfrey.liteloader.launch;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import joptsimple.ArgumentAcceptingOptionSpec;
import joptsimple.NonOptionArgumentSpec;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import net.minecraft.launchwrapper.Launch;
/**
* Container for startup environment state which also parses the command line
* options.
*
* @author Adam Mummery-Smith
*/
public abstract class StartupEnvironment implements GameEnvironment
{
private List<String> singularLaunchArgs = new ArrayList<String>();
private Map<String, String> launchArgs;
private ArgumentAcceptingOptionSpec<String> modsDirOption;
private ArgumentAcceptingOptionSpec<String> modsOption;
private ArgumentAcceptingOptionSpec<String> modsRepoOption;
private ArgumentAcceptingOptionSpec<String> apisOption;
private NonOptionArgumentSpec<String> unparsedOptions;
private OptionSet parsedOptions;
private final File gameDirectory;
private final File assetsDirectory;
private final String profile;
public StartupEnvironment(List<String> args, File gameDirectory, File assetsDirectory, String profile)
{
this.gameDirectory = gameDirectory;
this.assetsDirectory = assetsDirectory;
this.profile = profile;
this.initArgs(args);
}
public abstract void registerCoreAPIs(List<String> apisToLoad);
public abstract int getEnvironmentTypeId();
/**
* @param args
*/
@SuppressWarnings("unchecked")
public void initArgs(List<String> args)
{
// Get the launchArgs map from the blackboard, or create it if it's not there
this.launchArgs = (Map<String, String>)Launch.blackboard.get("launchArgs");
if (this.launchArgs == null)
{
this.launchArgs = new HashMap<String, String>();
Launch.blackboard.put("launchArgs", this.launchArgs);
}
// Parse liteloader options using joptsimple
this.parseOptions(args.toArray(new String[args.size()]));
// Parse out the arguments ourself because joptsimple doesn't really provide a good way to
// add arguments to the unparsed argument list after parsing
this.parseArgs(this.parsedOptions.valuesOf(this.unparsedOptions));
// Put required arguments to the blackboard if they don't already exist there
this.provideRequiredArgs();
}
private void parseOptions(String[] args)
{
OptionParser optionParser = new OptionParser();
optionParser.allowsUnrecognizedOptions();
this.modsOption = optionParser.accepts("mods", "Comma-separated list of mods to load")
.withRequiredArg().ofType(String.class).withValuesSeparatedBy(',');
this.modsRepoOption = optionParser.accepts("modRepo", "Path to a JSON file which defines a mod repository")
.withRequiredArg().ofType(String.class);
this.apisOption = optionParser.accepts("api", "Additional API classes to load")
.withRequiredArg().ofType(String.class);
this.modsDirOption = optionParser.accepts("modsDir", "Path to 'mods' folder to use instead of default")
.withRequiredArg().ofType(String.class);
this.unparsedOptions = optionParser.nonOptions();
this.parsedOptions = optionParser.parse(args);
}
private void parseArgs(List<String> args)
{
String classifier = null;
for (String arg : args)
{
if (arg.startsWith("-"))
{
if (classifier != null)
{
this.addClassifiedArg(classifier, "");
classifier = null;
}
else if (arg.contains("="))
{
this.addClassifiedArg(arg.substring(0, arg.indexOf('=')), arg.substring(arg.indexOf('=') + 1));
}
else
{
classifier = arg;
}
}
else
{
if (classifier != null)
{
this.addClassifiedArg(classifier, arg);
classifier = null;
}
else
{
this.singularLaunchArgs.add(arg);
}
}
}
if (classifier != null) this.singularLaunchArgs.add(classifier);
}
public void addClassifiedArg(String classifiedArg, String arg)
{
this.launchArgs.put(classifiedArg, arg);
}
public void provideRequiredArgs()
{
if (this.launchArgs.get("--version") == null)
{
this.addClassifiedArg("--version", LiteLoaderTweaker.VERSION);
}
if (this.launchArgs.get("--gameDir") == null && this.gameDirectory != null)
{
this.addClassifiedArg("--gameDir", this.gameDirectory.getAbsolutePath());
}
if (this.launchArgs.get("--assetsDir") == null && this.assetsDirectory != null)
{
this.addClassifiedArg("--assetsDir", this.assetsDirectory.getAbsolutePath());
}
}
public String[] getLaunchArguments()
{
List<String> args = new ArrayList<String>();
for (String singularArg : this.singularLaunchArgs)
{
args.add(singularArg);
}
for (Entry<String, String> launchArg : this.launchArgs.entrySet())
{
args.add(launchArg.getKey().trim());
args.add(launchArg.getValue().trim());
}
this.singularLaunchArgs.clear();
this.launchArgs.clear();
return args.toArray(new String[args.size()]);
}
/**
* Get the mod filter list
*/
public List<String> getModFilterList()
{
return (this.parsedOptions.has(this.modsOption)) ? this.modsOption.values(this.parsedOptions) : null;
}
/**
* Get API classes to load
*/
public List<String> getAPIsToLoad()
{
List<String> apisToLoad = new ArrayList<String>();
this.registerCoreAPIs(apisToLoad);
if (this.parsedOptions.has(this.apisOption))
{
apisToLoad.addAll(this.apisOption.values(this.parsedOptions));
}
return apisToLoad;
}
public File getOptionalDirectory(File baseDirectory, ArgumentAcceptingOptionSpec<String> option, String defaultDir)
{
if (this.parsedOptions.has(option))
{
String path = option.value(this.parsedOptions);
File dir = new File(path);
if (dir.isAbsolute())
{
return dir;
}
return new File(baseDirectory, path);
}
return new File(baseDirectory, defaultDir);
}
@Override
public File getGameDirectory()
{
return this.gameDirectory;
}
@Override
public File getAssetsDirectory()
{
return this.assetsDirectory;
}
@Override
public String getProfile()
{
return this.profile;
}
@Override
public File getModsFolder()
{
return this.getOptionalDirectory(this.gameDirectory, this.modsDirOption, "mods");
}
/**
* Get the mods repo json path
*/
@Override
public String getModsRepoFile()
{
return (this.parsedOptions.has(this.modsRepoOption)) ? this.modsRepoOption.value(this.parsedOptions) : null;
}
}