summaryrefslogtreecommitdiffstats
path: root/logic/minecraft/onesix/OneSixVersionFormat.cpp
blob: ff26ab924a762203395773b96b7e2428247d1a16 (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
#include "OneSixVersionFormat.h"
#include <minecraft/NullProfileStrategy.h>
#include <Json.h>
#include "minecraft/ParseUtils.h"
#include <minecraft/MinecraftVersion.h>
#include <minecraft/VersionBuildError.h>
#include <minecraft/MojangVersionFormat.h>

using namespace Json;

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

static QString readStringRet(const QJsonObject &root, const QString &key)
{
	if (root.contains(key))
	{
		return requireString(root.value(key));
	}
	return QString();
}

LibraryPtr OneSixVersionFormat::libraryFromJson(const QJsonObject &libObj, const QString &filename)
{
	LibraryPtr out = MojangVersionFormat::libraryFromJson(libObj, filename);
	readString(libObj, "MMC-hint", out->m_hint);
	readString(libObj, "MMC-absulute_url", out->m_absolute_url);
	readString(libObj, "MMC-absoluteUrl", out->m_absolute_url);
	return out;
}

QJsonObject OneSixVersionFormat::libraryToJson(Library *library)
{
	QJsonObject libRoot = MojangVersionFormat::libraryToJson(library);
	if (library->m_absolute_url.size())
		libRoot.insert("MMC-absoluteUrl", library->m_absolute_url);
	if (library->m_hint.size())
		libRoot.insert("MMC-hint", library->m_hint);
	return libRoot;
}

VersionFilePtr OneSixVersionFormat::versionFileFromJson(const QJsonDocument &doc, const QString &filename, const bool requireOrder)
{
	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();

	if (requireOrder)
	{
		if (root.contains("order"))
		{
			out->order = requireInteger(root.value("order"));
		}
		else
		{
			// FIXME: evaluate if we don't want to throw exceptions here instead
			qCritical() << filename << "doesn't contain an order field";
		}
	}

	out->name = root.value("name").toString();
	out->fileId = root.value("fileId").toString();
	out->version = root.value("version").toString();
	out->mcVersion = root.value("mcVersion").toString();
	out->filename = filename;

	readString(root, "id", out->id);

	readString(root, "mainClass", out->mainClass);
	readString(root, "appletClass", out->appletClass);
	readString(root, "minecraftArguments", out->minecraftArguments);
	if(out->minecraftArguments.isEmpty())
	{
		QString processArguments;
		readString(root, "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));
		}
	}

	readString(root, "type", out->type);

	out->m_releaseTime = timeFromS3Time(readStringRet(root, "releaseTime"));
	out->m_updateTime = timeFromS3Time(readStringRet(root, "time"));

	readString(root, "assets", out->assets);

	if (root.contains("+tweakers"))
	{
		for (auto tweakerVal : requireArray(root.value("+tweakers")))
		{
			out->addTweakers.append(requireString(tweakerVal));
		}
	}

	if (root.contains("+traits"))
	{
		for (auto tweakerVal : requireArray(root.value("+traits")))
		{
			out->traits.insert(requireString(tweakerVal));
		}
	}

	if (root.contains("+jarMods"))
	{
		for (auto libVal : requireArray(root.value("+jarMods")))
		{
			QJsonObject libObj = requireObject(libVal);
			// parse the jarmod
			auto lib = OneSixVersionFormat::jarModFromJson(libObj, filename, out->name);
			if(lib->originalName.isEmpty())
			{
				auto fixed = out->name;
				fixed.remove(" (jar mod)");
				lib->originalName = out->name;
			}
			// and add to jar mods
			out->jarMods.append(lib);
		}
	}

	auto readLibs = [&](const char * which)
	{
		for (auto libVal : requireArray(root.value(which)))
		{
			QJsonObject libObj = requireObject(libVal);
			// parse the library
			auto lib = libraryFromJson(libObj, filename);
			out->addLibs.append(lib);
		}
	};
	bool hasPlusLibs = root.contains("+libraries");
	bool hasLibs = root.contains("libraries");
	if (hasPlusLibs && hasLibs)
	{
		out->addProblem(PROBLEM_WARNING, QObject::tr("Version file has both '+libraries' and 'libraries'. This is no longer supported."));
		readLibs("libraries");
		readLibs("+libraries");
	}
	else if (hasLibs)
	{
		readLibs("libraries");
	}
	else if(hasPlusLibs)
	{
		readLibs("+libraries");
	}

	/* removed features that shouldn't be used */
	if (root.contains("tweakers"))
	{
		out->addProblem(PROBLEM_ERROR, QObject::tr("Version file contains unsupported element 'tweakers'"));
	}
	if (root.contains("-libraries"))
	{
		out->addProblem(PROBLEM_ERROR, QObject::tr("Version file contains unsupported element '-libraries'"));
	}
	if (root.contains("-tweakers"))
	{
		out->addProblem(PROBLEM_ERROR, QObject::tr("Version file contains unsupported element '-tweakers'"));
	}
	if (root.contains("-minecraftArguments"))
	{
		out->addProblem(PROBLEM_ERROR, QObject::tr("Version file contains unsupported element '-minecraftArguments'"));
	}
	if (root.contains("+minecraftArguments"))
	{
		out->addProblem(PROBLEM_ERROR, QObject::tr("Version file contains unsupported element '+minecraftArguments'"));
	}
	return out;
}

template <typename T>
struct libraryConversion
{
	static QJsonObject convert(std::shared_ptr<Library> &value)
	{
		return OneSixVersionFormat::libraryToJson(value.get());
	}
};

static QJsonDocument versionFileToJson(VersionFilePtr patch, bool saveOrder)
{
	QJsonObject root;
	if (saveOrder)
	{
		root.insert("order", patch->order);
	}
	writeString(root, "name", patch->name);
	writeString(root, "fileId", patch->fileId);
	writeString(root, "version", patch->version);
	writeString(root, "mcVersion", patch->mcVersion);
	writeString(root, "id", patch->id);
	writeString(root, "mainClass", patch->mainClass);
	writeString(root, "appletClass", patch->appletClass);
	writeString(root, "minecraftArguments", patch->minecraftArguments);
	writeString(root, "type", patch->type);
	writeString(root, "assets", patch->assets);
	if (patch->isMinecraftVersion())
	{
		writeString(root, "releaseTime", timeToS3Time(patch->m_releaseTime));
		writeString(root, "time", timeToS3Time(patch->m_updateTime));
	}
	writeStringList(root, "+tweakers", patch->addTweakers);
	writeStringList(root, "+traits", patch->traits.toList());
	if (!patch->addLibs.isEmpty())
	{
		QJsonArray array;
		for (auto value: patch->addLibs)
		{
			array.append(OneSixVersionFormat::libraryToJson(value.get()));
		}
		root.insert("libraries", array);
	}
	if (!patch->jarMods.isEmpty())
	{
		QJsonArray array;
		for (auto value: patch->jarMods)
		{
			array.append(OneSixVersionFormat::jarModtoJson(value.get()));
		}
		root.insert("+jarMods", array);
	}
	// write the contents to a json document.
	{
		QJsonDocument out;
		out.setObject(root);
		return out;
	}
}

static QJsonDocument minecraftVersionToJson(MinecraftVersionPtr patch, bool saveOrder)
{
	if(patch->getVersionSource() == Local && patch->getVersionFile())
	{
		return OneSixVersionFormat::profilePatchToJson(patch->getVersionFile(), saveOrder);
	}
	else
	{
		throw VersionIncomplete(QObject::tr("Can't write incomplete/builtin Minecraft version %1").arg(patch->name()));
	}
}

QJsonDocument OneSixVersionFormat::profilePatchToJson(const ProfilePatchPtr &patch, bool saveOrder)
{
	auto vfile = std::dynamic_pointer_cast<VersionFile>(patch);
	if(vfile)
	{
		return versionFileToJson(vfile, saveOrder);
	}
	auto mversion = std::dynamic_pointer_cast<MinecraftVersion>(patch);
	if(mversion)
	{
		return minecraftVersionToJson(mversion, saveOrder);
	}
	throw VersionIncomplete(QObject::tr("Unhandled object type while processing %1").arg(patch->getName()));
}

std::shared_ptr<MinecraftProfile> OneSixVersionFormat::profileFromSingleJson(const QJsonObject &obj)
{
	std::shared_ptr<MinecraftProfile> version(new MinecraftProfile(new NullProfileStrategy()));
	try
	{
		version->clear();
		auto file = versionFileFromJson(QJsonDocument(obj), QString(), false);
		file->applyTo(version.get());
		version->appendPatch(file);
	}
	catch(Exception &err)
	{
		return nullptr;
	}
	return version;
}

JarmodPtr OneSixVersionFormat::jarModFromJson(const QJsonObject &libObj, const QString &filename, const QString &originalName)
{
	JarmodPtr out(new Jarmod());
	if (!libObj.contains("name"))
	{
		throw JSONValidationError(filename +
								  "contains a jarmod that doesn't have a 'name' field");
	}
	out->name = libObj.value("name").toString();
	out->originalName = libObj.value("originalName").toString();
	return out;
}

QJsonObject OneSixVersionFormat::jarModtoJson(Jarmod *jarmod)
{
	QJsonObject out;
	writeString(out, "name", jarmod->name);
	if(!jarmod->originalName.isEmpty())
	{
		writeString(out, "originalName", jarmod->originalName);
	}
	return out;
}