-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManifestParser.java
333 lines (303 loc) · 11.9 KB
/
ManifestParser.java
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
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import com.google.gson.*;
import org.apache.commons.text.StringSubstitutor;
public class ManifestParser
{
final static String VERSION_MANIFEST_URL = "https://launchermeta.mojang.com/mc/game/version_manifest.json";
/**
*
* @param url The URL to the JSON file
* @return The JsonObject for the URL specified
*/
public static JsonObject GetJsonObjectFromURL(String url)
{
StringBuilder json = new StringBuilder();
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(url).openStream()));
String line;
while((line = reader.readLine()) != null) { json.append(line); }
} catch (IOException e) { e.printStackTrace(); }
return new Gson().fromJson(json.toString(), JsonObject.class);
}
/**
*
* @param path The path to the JSON file
* @return The JsonObject for the file specified
*/
public static JsonObject GetJsonObjectFromFile(String path)
{
try
{
BufferedReader reader = new BufferedReader(new FileReader(path));
return JsonParser.parseReader(reader).getAsJsonObject();
} catch (FileNotFoundException e)
{
System.out.println("File not found: " + path);
e.printStackTrace();
}
return null;
}
/**
*
* @param obj The JsonObject to save
* @param path The path to the saved file
*/
public static void SaveJsonObject(JsonObject obj, String path)
{
Gson g = new GsonBuilder().setPrettyPrinting().create();
String json = g.toJson(obj);
try
{
File destinationFile = new File(path);
if(!destinationFile.getParentFile().exists())
{
destinationFile.getParentFile().mkdirs();
}
FileWriter fw = new FileWriter(path);
fw.write(json);
fw.close();
} catch (IOException e) { e.printStackTrace(); }
}
/**
*
* @return The main version manifest
*/
public static JsonObject GetVersionManifest() { return GetJsonObjectFromURL(VERSION_MANIFEST_URL); }
/**
*
* @return URLs of downloadable
*/
public static String[] GetDownloadableVersions()
{
List<String> versions = new ArrayList<>();
for(JsonElement vers : GetVersionManifest().getAsJsonArray("versions"))
{
versions.add(vers.getAsJsonObject().get("id").getAsString());
}
return versions.toArray(new String[0]);
}
/**
*
* @param version The minecraft version
* @return The version's manifest
*/
public static JsonObject GetManifestFromVersion(String version)
{
JsonObject mainManifest = GetVersionManifest();
for(JsonElement vm : mainManifest.getAsJsonArray("versions"))
{
JsonObject versionManifest = vm.getAsJsonObject();
try
{
if(Objects.equals(versionManifest.get("id").getAsString(), version))
{
return GetJsonObjectFromURL(versionManifest.get("url").getAsString());
}
} catch (NullPointerException e) { e.printStackTrace(); }
}
return null;
}
/**
*
* @param manifest The version's manifest
* @return The version's asset index
*/
public static JsonObject GetAssetIndexFromVersion(JsonObject manifest)
{
return GetJsonObjectFromURL(manifest.getAsJsonObject("assetIndex").get("url").getAsString());
}
public static JsonObject GetLocalAssetIndexFromVersion(JsonObject manifest)
{
return ManifestParser.GetJsonObjectFromFile(Launcher.ASSETS_INDEXES_DIR + ManifestParser.GetAssetIndexVersionFromVersion(manifest) + ".json");
}
/**
*
* @param assetManifest The version's asset manifest
* @return [isMapToResources, isVirtual]
*/
public static boolean[] GetSpecialAssetsFlags(JsonObject assetManifest)
{
boolean mapToResources = false;
boolean virtual = false;
try
{
mapToResources = assetManifest.get("map_to_resources").getAsBoolean();
} catch (NullPointerException e) { }
try
{
virtual = assetManifest.get("virtual").getAsBoolean();
} catch (NullPointerException e) { }
return new boolean[] {mapToResources, virtual};
}
public static String GetAssetsDirFromAssetIndex(JsonObject assetManifest, String destinationPath)
{
boolean mapToResources = GetSpecialAssetsFlags(assetManifest)[0], virtual = GetSpecialAssetsFlags(assetManifest)[1];
destinationPath += Launcher.ASSETS_DIR;
if(mapToResources)
{
destinationPath = Launcher.DEFAULT_MC_PATH + Launcher.RESOURCES_DIR;
} else if(virtual)
{
destinationPath += "virtual" + File.separator + "legacy" + File.separator;
} else { destinationPath += "objects" + File.separator; }
return destinationPath;
}
public static String GetAssetRelativePathFromAssetPair(Pair<String, String> asset, boolean isMapToResources, boolean isVirtual)
{
return isMapToResources || isVirtual ? asset.getKey() : asset.getValue().substring(0, 2) + File.separator + asset.getValue();
}
/**
*
* @param manifest The version's manifest
* @return Runtime path and version
*/
public static Pair<String, Integer> GetJavaRuntimeFromVersion(JsonObject manifest)
{
try
{
JsonObject j = manifest.getAsJsonObject("javaVersion");
return new Pair<>(j.get("component").getAsString(), j.get("majorVersion").getAsInt());
} catch (Exception e)
{
System.out.println("Couldn't parse Java version from manifest, assuming Java 8. Stack trace:");
e.printStackTrace();
return new Pair<>("jre-legacy", 8);
}
}
public static File GetRuntimePathFromVersion(JsonObject manifest)
{
String executable = System.getProperty("os.name").toLowerCase().contains("windows") ? "java.exe" : "java";
return new File(Launcher.RUNTIME_PATH + GetJavaRuntimeFromVersion(manifest).getKey() + File.separator + "bin" + File.separator + executable);
}
/**
*
* @param manifest The version manifest
* @return The assets version
*/
public static String GetAssetIndexVersionFromVersion(JsonObject manifest)
{
return manifest.getAsJsonObject("assetIndex").get("id").getAsString();
}
public static String GetGameVersionFromVersion(JsonObject manifest)
{
return manifest.get("id").getAsString();
}
public static String GetClientJarURLFromVersion(JsonObject manifest)
{
return manifest.getAsJsonObject("downloads").getAsJsonObject("client").get("url").getAsString();
}
public static String GetServerJarURLFromVersion(JsonObject manifest)
{
try
{
return manifest.getAsJsonObject("downloads").getAsJsonObject("server").get("url").getAsString();
} catch (NullPointerException e) { return null; }
}
/**
*
* @param manifest The version's manifest
* @param platform The OS platform ("windows", "linux", "osx")
* @return Pairs of library path and URL
*/
public static Pair<String, String>[] GetLibsFromVersion(JsonObject manifest, String platform)
{
JsonArray libsArray = manifest.getAsJsonArray("libraries");
List<String> libs = new ArrayList<>(); // we need to save the names of the libs we download, since
// in the manifest multiple versions are provided
List<String> urls = new ArrayList<>();
List<Pair<String, String>> libraries = new ArrayList<>();
for(int i = 0; i < libsArray.size(); i++)
{
JsonObject library = libsArray.get(i).getAsJsonObject();
String libPath = null;
String name = null;
String url = null;
try
{
url = name = libPath = null;
name = library.get("name").getAsString().split(":")[1];
JsonObject downloads = library.getAsJsonObject("downloads");
List<JsonObject> artifacts = new ArrayList<>();
JsonObject artifact = null;
if((artifact = downloads.getAsJsonObject("artifact")) != null) // Check if lib is universal
{
//artifact = downloads.getAsJsonObject("artifact");
artifacts.add(artifact);
//url = artifact.get("url").getAsString();
//libPath = artifact.get("path").getAsString();
}
if((artifact = downloads.getAsJsonObject("classifiers")) != null) // if it isn't, check if it is a native
{
//artifact = downloads.getAsJsonObject("classifiers").getAsJsonObject("natives-" + platform);
JsonObject a = artifact.getAsJsonObject("natives-" + platform);
if(a == null)
{
artifacts.add(artifact.getAsJsonObject("natives-" + platform + "-" +
(System.getProperty("os.arch").contains("86") ? "32" : "64")));
} else { artifacts.add(a); }
//url = nativeLib.get("url").getAsString();
//libPath = nativeLib.get("path").getAsString();
}
for(JsonObject a : artifacts)
{
url = a.get("url").getAsString();
String filePath = a.get("path").getAsString();
int lastDirIdx = filePath.lastIndexOf("/");
libPath = filePath.substring(0, lastDirIdx + 1);
if(url != null && libPath != null)
{
//if(!libs.contains(name))
//{
//fileName = url.split("/")[url.split("/").length - 1];
libs.add(name);
urls.add(url);
libraries.add(new Pair<>(libPath, url));
//}
}
}
} catch (NullPointerException e) { }
}
//return urls.toArray(new String[0]);
return libraries.toArray(new Pair[0]);
}
/**
*
* @param manifest The version's manifest
* @return Pairs of fileName and hash
*/
public static Pair<String, String>[] GetAssetsFromAssetIndex(JsonObject manifest)
{
List<Pair<String, String>> assets = new ArrayList<>();
JsonObject objs = manifest.getAsJsonObject("objects");
for(Map.Entry<String, JsonElement> entry : objs.entrySet())
{
String dest = entry.getKey();
String hash = entry.getValue().getAsJsonObject().get("hash").getAsString();
assets.add(new Pair<>(dest, hash));
}
return assets.toArray(new Pair[0]);
}
public static String GetLaunchParams(JsonObject manifest, Map<String, String> values)
{
String params = "";
if(manifest.get("minecraftArguments") != null)
{
params = manifest.get("minecraftArguments").getAsString();
} else
{
JsonArray p = manifest.getAsJsonObject("arguments").getAsJsonArray("game");
for(JsonElement element : p)
{
if(element.isJsonPrimitive()) { params += element.getAsString() + " "; }
}
}
StringSubstitutor substitutor = new StringSubstitutor(values);
return substitutor.replace(params);
}
}