summaryrefslogtreecommitdiffstats
path: root/logic/minecraft/MojangVersionFormat.cpp
blob: 779a2b7df2275d027dba698dcf2764656e4f12be (plain)
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
#include "MojangVersionFormat.h"
#include "onesix/OneSixVersionFormat.h"
#include "MinecraftVersion.h"
#include "VersionBuildError.h"
#include "MojangDownloadInfo.h"

#include "Json.h"
using namespace Json;
#include "ParseUtils.h"

static const int CURRENT_MINIMUM_LAUNCHER_VERSION = 14;

static MojangAssetIndexInfo::Ptr assetIndexFromJson (const QJsonObject &obj);
static MojangDownloadInfo::Ptr downloadInfoFromJson (const QJsonObject &obj);
static MojangLibraryDownloadInfo::Ptr libDownloadInfoFromJson (const QJsonObject &libObj);
static QJsonObject assetIndexToJson (MojangAssetIndexInfo::Ptr assetidxinfo);
static QJsonObject libDownloadInfoToJson (MojangLibraryDownloadInfo::Ptr libinfo);
static QJsonObject downloadInfoToJson (MojangDownloadInfo::Ptr info);

namespace Bits
{
static void readString(const QJsonObject &root, const QString &key, QString &variable)
{
	if (root.contains(key))
	{
		variable = requireString(root.value(key));
	}
}

static void readDownloadInfo(MojangDownloadInfo::Ptr out, const QJsonObject &obj)
{
	// optional, not used
	readString(obj, "path", out->path);
	// required!
	out->sha1 = requireString(obj, "sha1");
	out->url = requireString(obj, "url");
	out->size = requireInteger(obj, "size");
}

static void readAssetIndex(MojangAssetIndexInfo::Ptr out, const QJsonObject &obj)
{
	out->totalSize = requireInteger(obj, "totalSize");
	out->id = requireString(obj, "id");
	// out->known = true;
}
}

MojangDownloadInfo::Ptr downloadInfoFromJson(const QJsonObject &obj)
{
	auto out = std::make_shared<MojangDownloadInfo>();
	Bits::readDownloadInfo(out, obj);
	return out;
}

MojangAssetIndexInfo::Ptr assetIndexFromJson(const QJsonObject &obj)
{
	auto out = std::make_shared<MojangAssetIndexInfo>();
	Bits::readDownloadInfo(out, obj);
	Bits::readAssetIndex(out, obj);
	return out;
}

QJsonObject downloadInfoToJson(MojangDownloadInfo::Ptr info)
{
	QJsonObject out;
	if(!info->path.isNull())
	{
		out.insert("path", info->path);
	}
	out.insert("sha1", info->sha1);
	out.insert("size", info->size);
	out.insert("url", info->url);
	return out;
}

MojangLibraryDownloadInfo::Ptr libDownloadInfoFromJson(const QJsonObject &libObj)
{
	auto out = std::make_shared<MojangLibraryDownloadInfo>();
	auto dlObj = requireObject(libObj.value("downloads"));
	if(dlObj.contains("artifact"))
	{
		out->artifact = downloadInfoFromJson(requireObject(dlObj, "artifact"));
	}
	if(dlObj.contains("classifiers"))
	{
		auto classifiersObj = requireObject(dlObj, "classifiers");
		for(auto iter = classifiersObj.begin(); iter != classifiersObj.end(); iter++)
		{
			auto classifier = iter.key();
			auto classifierObj = requireObject(iter.value());
			out->classifiers[classifier] = downloadInfoFromJson(classifierObj);
		}
	}
	return out;
}

QJsonObject libDownloadInfoToJson(MojangLibraryDownloadInfo::Ptr libinfo)
{
	QJsonObject out;
	if(libinfo->artifact)
	{
		out.insert("artifact", downloadInfoToJson(libinfo->artifact));
	}
	if(libinfo->classifiers.size())
	{
		QJsonObject classifiersOut;
		for(auto iter = libinfo->classifiers.begin(); iter != libinfo->classifiers.end(); iter++)
		{
			classifiersOut.insert(iter.key(), downloadInfoToJson(iter.value()));
		}
		out.insert("classifiers", classifiersOut);
	}
	return out;
}

QJsonObject assetIndexToJson(MojangAssetIndexInfo::Ptr info)
{
	QJsonObject out;
	if(!info->path.isNull())
	{
		out.insert("path", info->path);
	}
	out.insert("sha1", info->sha1);
	out.insert("size", info->size);
	out.insert("url", info->url);
	out.insert("totalSize", info->totalSize);
	out.insert("id", info->id);
	return out;
}

void MojangVersionFormat::readVersionProperties(const QJsonObject &in, VersionFile *out)
{
	Bits::readString(in, "id", out->id);
	Bits::readString(in, "mainClass", out->mainClass);
	Bits::readString(in, "minecraftArguments", out->minecraftArguments);
	if(out->minecraftArguments.isEmpty())
	{
		QString processArguments;
		Bits::readString(in, "processArguments", processArguments);
		QString toCompare = processArguments.toLower();
		if (toCompare == "legacy")
		{
			out->minecraftArguments = " ${auth_player_name} ${auth_session}";
		}
		else if (toCompare == "username_session")
		{
			out->minecraftArguments = "--username ${auth_player_name} --session ${auth_session}";
		}
		else if (toCompare == "username_session_version")
		{
			out->minecraftArguments = "--username ${auth_player_name} --session ${auth_session} --version ${profile_name}";
		}
		else if (!toCompare.isEmpty())
		{
			out->addProblem(PROBLEM_ERROR, QObject::tr("processArguments is set to unknown value '%1'").arg(processArguments));
		}
	}
	Bits::readString(in, "type", out->type);

	if(in.contains("assetIndex"))
	{
		out->mojangAssetIndex = assetIndexFromJson(requireObject(in, "assetIndex"));
	}
	Bits::readString(in, "assets", out->assets);

	out->m_releaseTime = timeFromS3Time(in.value("releaseTime").toString(""));
	out->m_updateTime = timeFromS3Time(in.value("time").toString(""));

	if (in.contains("minimumLauncherVersion"))
	{
		out->minimumLauncherVersion = requireInteger(in.value("minimumLauncherVersion"));
		if (out->minimumLauncherVersion > CURRENT_MINIMUM_LAUNCHER_VERSION)
		{
			out->addProblem(
				PROBLEM_WARNING,
				QObject::tr("The 'minimumLauncherVersion' value of this version (%1) is higher than supported by MultiMC (%2). It might not work properly!")
					.arg(out->minimumLauncherVersion)
					.arg(CURRENT_MINIMUM_LAUNCHER_VERSION));
		}
	}
	if(in.contains("downloads"))
	{
		auto downloadsObj = requireObject(in, "downloads");
		for(auto iter = downloadsObj.begin(); iter != downloadsObj.end(); iter++)
		{
			auto classifier = iter.key();
			auto classifierObj = requireObject(iter.value());
			out->mojangDownloads[classifier] = downloadInfoFromJson(classifierObj);
		}
	}
}

VersionFilePtr MojangVersionFormat::versionFileFromJson(const QJsonDocument &doc, const QString &filename)
{
	VersionFilePtr out(new VersionFile());
	if (doc.isEmpty() || doc.isNull())
	{
		throw JSONValidationError(filename + " is empty or null");
	}
	if (!doc.isObject())
	{
		throw JSONValidationError(filename + " is not an object");
	}

	QJsonObject root = doc.object();

	readVersionProperties(root, out.get());

	out->name = "Minecraft";
	out->fileId = "net.minecraft";
	out->version = out->id;
	out->filename = filename;


	if (root.contains("libraries"))
	{
		for (auto libVal : requireArray(root.value("libraries")))
		{
			auto libObj = requireObject(libVal);

			auto lib = MojangVersionFormat::libraryFromJson(libObj, filename);
			out->libraries.append(lib);
		}
	}
	return out;
}

void MojangVersionFormat::writeVersionProperties(const VersionFile* in, QJsonObject& out)
{
	writeString(out, "id", in->id);
	writeString(out, "mainClass", in->mainClass);
	writeString(out, "minecraftArguments", in->minecraftArguments);
	writeString(out, "type", in->type);
	writeString(out, "assets", in->assets);
	if(!in->m_releaseTime.isNull())
	{
		writeString(out, "releaseTime", timeToS3Time(in->m_releaseTime));
	}
	if(!in->m_updateTime.isNull())
	{
		writeString(out, "time", timeToS3Time(in->m_updateTime));
	}
	if(in->minimumLauncherVersion != -1)
	{
		out.insert("minimumLauncherVersion", in->minimumLauncherVersion);
	}
	if(in->mojangAssetIndex && in->mojangAssetIndex->known)
	{
		out.insert("assetIndex", assetIndexToJson(in->mojangAssetIndex));
	}
	if(in->mojangDownloads.size())
	{
		QJsonObject downloadsOut;
		for(auto iter = in->mojangDownloads.begin(); iter != in->mojangDownloads.end(); iter++)
		{
			downloadsOut.insert(iter.key(), downloadInfoToJson(iter.value()));
		}
		out.insert("downloads", downloadsOut);
	}
}

QJsonDocument MojangVersionFormat::versionFileToJson(const VersionFilePtr &patch)
{
	QJsonObject root;
	writeVersionProperties(patch.get(), root);
	if (!patch->libraries.isEmpty())
	{
		QJsonArray array;
		for (auto value: patch->libraries)
		{
			array.append(MojangVersionFormat::libraryToJson(value.get()));
		}
		root.insert("libraries", array);
	}

	// write the contents to a json document.
	{
		QJsonDocument out;
		out.setObject(root);
		return out;
	}
}

LibraryPtr MojangVersionFormat::libraryFromJson(const QJsonObject &libObj, const QString &filename)
{
	LibraryPtr out(new Library());
	if (!libObj.contains("name"))
	{
		throw JSONValidationError(filename + "contains a library that doesn't have a 'name' field");
	}
	out->m_name = libObj.value("name").toString();

	Bits::readString(libObj, "url", out->m_base_url);
	if (libObj.contains("extract"))
	{
		out->applyExcludes = true;
		auto extractObj = requireObject(libObj.value("extract"));
		for (auto excludeVal : requireArray(extractObj.value("exclude")))
		{
			out->extract_excludes.append(requireString(excludeVal));
		}
	}
	if (libObj.contains("natives"))
	{
		QJsonObject nativesObj = requireObject(libObj.value("natives"));
		for (auto it = nativesObj.begin(); it != nativesObj.end(); ++it)
		{
			if (!it.value().isString())
			{
				qWarning() << filename << "contains an invalid native (skipping)";
			}
			OpSys opSys = OpSys_fromString(it.key());
			if (opSys != Os_Other)
			{
				out->m_native_classifiers[opSys] = it.value().toString();
			}
		}
	}
	if (libObj.contains("rules"))
	{
		out->applyRules = true;
		out->m_rules = rulesFromJsonV4(libObj);
	}
	if (libObj.contains("downloads"))
	{
		out->m_mojang_downloads = libDownloadInfoFromJson(libObj);
	}
	return out;
}

QJsonObject MojangVersionFormat::libraryToJson(Library *library)
{
	QJsonObject libRoot;
	libRoot.insert("name", (QString)library->m_name);
	if (library->m_base_url != "http://" + URLConstants::AWS_DOWNLOAD_LIBRARIES &&
		library->m_base_url != "https://" + URLConstants::AWS_DOWNLOAD_LIBRARIES &&
		library->m_base_url != "https://" + URLConstants::LIBRARY_BASE && !library->m_base_url.isEmpty())
	{
		libRoot.insert("url", library->m_base_url);
	}
	if (library->isNative())
	{
		QJsonObject nativeList;
		auto iter = library->m_native_classifiers.begin();
		while (iter != library->m_native_classifiers.end())
		{
			nativeList.insert(OpSys_toString(iter.key()), iter.value());
			iter++;
		}
		libRoot.insert("natives", nativeList);
		if (library->extract_excludes.size())
		{
			QJsonArray excludes;
			QJsonObject extract;
			for (auto exclude : library->extract_excludes)
			{
				excludes.append(exclude);
			}
			extract.insert("exclude", excludes);
			libRoot.insert("extract", extract);
		}
	}
	if (library->m_rules.size())
	{
		QJsonArray allRules;
		for (auto &rule : library->m_rules)
		{
			QJsonObject ruleObj = rule->toJson();
			allRules.append(ruleObj);
		}
		libRoot.insert("rules", allRules);
	}
	if(library->m_mojang_downloads)
	{
		auto downloadsObj = libDownloadInfoToJson(library->m_mojang_downloads);
		libRoot.insert("downloads", downloadsObj);
	}
	return libRoot;
}