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
|
/* Copyright 2013 MultiMC Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "DerpVersionBuilder.h"
#include <QList>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QFile>
#include <QFileInfo>
#include <QMessageBox>
#include <QObject>
#include <QDir>
#include <QDebug>
#include "DerpVersion.h"
#include "DerpInstance.h"
#include "DerpRule.h"
DerpVersionBuilder::DerpVersionBuilder()
{
}
bool DerpVersionBuilder::build(DerpVersion *version, DerpInstance *instance, QWidget *widgetParent)
{
DerpVersionBuilder builder;
builder.m_version = version;
builder.m_instance = instance;
builder.m_widgetParent = widgetParent;
return builder.build();
}
bool DerpVersionBuilder::build()
{
m_version->clear();
QDir root(m_instance->instanceRoot());
QDir patches(root.absoluteFilePath("patches/"));
// version.json -> patches/*.json -> custom.json
// version.json
{
QJsonObject obj;
if (!read(QFileInfo(root.absoluteFilePath("version.json")), &obj))
{
return false;
}
if (!apply(obj))
{
return false;
}
}
// patches/
{
// load all, put into map for ordering, apply in the right order
QMap<int, QJsonObject> objects;
for (auto info : patches.entryInfoList(QStringList() << "*.json", QDir::Files))
{
QJsonObject obj;
if (!read(info, &obj))
{
return false;
}
if (!obj.contains("order") || !obj.value("order").isDouble())
{
QMessageBox::critical(m_widgetParent, QObject::tr("Error"), QObject::tr("Missing or invalid 'order' in %1").arg(info.absoluteFilePath()));
return false;
}
objects.insert(obj.value("order").toDouble(), obj);
}
for (auto object : objects.values())
{
if (!apply(object))
{
return false;
}
}
}
// custom.json
{
if (QFile::exists(root.absoluteFilePath("custom.json")))
{
QJsonObject obj;
if (!read(QFileInfo(root.absoluteFilePath("custom.json")), &obj))
{
return false;
}
if (!apply(obj))
{
return false;
}
}
}
return true;
}
void applyString(const QJsonObject &obj, const QString &key, QString &out, const bool onlyOverride = true)
{
if (obj.contains(key) && obj.value(key).isString())
{
out = obj.value(key).toString();
}
else if (!onlyOverride)
{
if (obj.contains("+" + key) && obj.value("+" + key).isString())
{
out += obj.value("+" + key).toString();
}
else if (obj.contains("-" + key) && obj.value("-" + key).isString())
{
out.remove(obj.value("-" + key).toString());
}
}
}
void applyString(const QJsonObject &obj, const QString &key, std::shared_ptr<DerpLibrary> lib, void(DerpLibrary::*func)(const QString &val))
{
if (obj.contains(key) && obj.value(key).isString())
{
(lib.get()->*func)(obj.value(key).toString());
}
}
bool DerpVersionBuilder::apply(const QJsonObject &object)
{
applyString(object, "id", m_version->id);
applyString(object, "mainClass", m_version->mainClass);
applyString(object, "processArguments", m_version->processArguments, false);
{
const QString toCompare = m_version->processArguments.toLower();
if (toCompare == "legacy")
{
m_version->minecraftArguments = " ${auth_player_name} ${auth_session}";
}
else if (toCompare == "username_session")
{
m_version->minecraftArguments = "--username ${auth_player_name} --session ${auth_session}";
}
else if (toCompare == "username_session_version")
{
m_version->minecraftArguments = "--username ${auth_player_name} --session ${auth_session} --version ${profile_name}";
}
}
applyString(object, "minecraftArguments", m_version->minecraftArguments, false);
applyString(object, "type", m_version->type);
applyString(object, "releaseTime", m_version->releaseTime);
applyString(object, "time", m_version->time);
applyString(object, "assets", m_version->assets);
{
if (m_version->assets.isEmpty())
{
m_version->assets = "legacy";
}
}
if (object.contains("minimumLauncherVersion"))
{
auto minLauncherVersionVal = object.value("minimumLauncherVersion");
if (minLauncherVersionVal.isDouble())
{
m_version->minimumLauncherVersion = minLauncherVersionVal.toDouble();
}
}
// libraries
if (object.contains("libraries"))
{
m_version->libraries.clear();
auto librariesValue = object.value("libraries");
if (!librariesValue.isArray())
{
QMessageBox::critical(m_widgetParent, QObject::tr("Error"), QObject::tr("One json files contains a libraries field, but it's not an array"));
return false;
}
auto array = librariesValue.toArray();
for (auto libVal : array)
{
if (libVal.isObject())
{
if (!applyLibrary(libVal.toObject(), Override))
{
return false;
}
}
}
}
// +libraries
if (object.contains("+libraries"))
{
auto librariesValue = object.value("+libraries");
if (!librariesValue.isArray())
{
QMessageBox::critical(m_widgetParent, QObject::tr("Error"), QObject::tr("One json files contains a libraries field, but it's not an array"));
return false;
}
for (auto libVal : librariesValue.toArray())
{
if (libVal.isObject())
{
applyLibrary(libVal.toObject(), Add);
}
}
}
// -libraries
if (object.contains("-libraries"))
{
auto librariesValue = object.value("-libraries");
if (!librariesValue.isArray())
{
QMessageBox::critical(m_widgetParent, QObject::tr("Error"), QObject::tr("One json files contains a libraries field, but it's not an array"));
return false;
}
for (auto libVal : librariesValue.toArray())
{
if (libVal.isObject())
{
applyLibrary(libVal.toObject(), Remove);
}
}
}
return true;
}
int findLibrary(QList<std::shared_ptr<DerpLibrary> > haystack, const QString &needle)
{
for (int i = 0; i < haystack.size(); ++i)
{
if (QRegExp(needle, Qt::CaseSensitive, QRegExp::WildcardUnix).indexIn(haystack.at(i)->rawName()) != -1)
{
return i;
}
}
return -1;
}
bool DerpVersionBuilder::applyLibrary(const QJsonObject &lib, const DerpVersionBuilder::Type type)
{
// Library name
auto nameVal = lib.value("name");
if (!nameVal.isString())
{
return false;
}
auto name = nameVal.toString();
if (type == Remove)
{
int index = findLibrary(m_version->libraries, name);
if (index >= 0)
{
m_version->libraries.removeAt(index);
}
return true;
}
if (type == Add && !lib.contains("insert"))
{
return false;
}
std::shared_ptr<DerpLibrary> library;
if (lib.value("insert").toString() == "apply" && type == Add)
{
library = m_version->libraries[findLibrary(m_version->libraries, name)];
}
else
{
library.reset(new DerpLibrary(nameVal.toString()));
}
applyString(lib, "url", library, &DerpLibrary::setBaseUrl);
applyString(lib, "MMC-hint", library, &DerpLibrary::setHint);
applyString(lib, "MMC-absulute_url", library, &DerpLibrary::setAbsoluteUrl);
applyString(lib, "MMC-absoluteUrl", library, &DerpLibrary::setAbsoluteUrl);
auto extractVal = lib.value("extract");
if (extractVal.isObject())
{
QStringList excludes;
auto extractObj = extractVal.toObject();
auto excludesVal = extractObj.value("exclude");
if (excludesVal.isArray())
{
auto excludesList = excludesVal.toArray();
for (auto excludeVal : excludesList)
{
if (excludeVal.isString())
{
excludes.append(excludeVal.toString());
}
}
library->extract_excludes = excludes;
}
}
auto nativesVal = lib.value("natives");
if (nativesVal.isObject())
{
library->setIsNative();
auto nativesObj = nativesVal.toObject();
for (auto it = nativesObj.begin(); it != nativesObj.end(); ++it)
{
auto osType = OpSys_fromString(it.key());
if (osType == Os_Other)
{
continue;
}
if (!it.value().isString())
{
continue;
}
library->addNative(osType, it.value().toString());
}
}
if (lib.contains("rules"))
{
library->setRules(rulesFromJsonV4(lib));
}
library->finalize();
if (type == Override)
{
qDebug() << "appending" << library->rawName();
m_version->libraries.append(library);
}
else if (lib.value("insert").toString() != "apply")
{
if (lib.value("insert").toString() == "append")
{
m_version->libraries.append(library);
}
else if (lib.value("insert").toString() == "prepend")
{
m_version->libraries.prepend(library);
}
else if (lib.value("insert").isObject())
{
QJsonObject insertObj = lib.value("insert").toObject();
if (insertObj.isEmpty())
{
QMessageBox::critical(m_widgetParent, QObject::tr("Error"), QObject::tr("'insert' object empty"));
return false;
}
const QString key = insertObj.keys().first();
const QString value = insertObj.value(key).toString();
const int index = findLibrary(m_version->libraries, value);
if (index >= 0)
{
if (key == "before")
{
m_version->libraries.insert(index, library);
}
else if (key == "after")
{
m_version->libraries.insert(index + 1, library);
}
else
{
QMessageBox::critical(m_widgetParent, QObject::tr("Error"), QObject::tr("Invalid value for 'insert': %1").arg(lib.value("insert").toString()));
return false;
}
}
}
else
{
QMessageBox::critical(m_widgetParent, QObject::tr("Error"), QObject::tr("Invalid value for 'insert': %1").arg(lib.value("insert").toString()));
return false;
}
}
return true;
}
bool DerpVersionBuilder::read(const QFileInfo &fileInfo, QJsonObject *out)
{
QFile file(fileInfo.absoluteFilePath());
if (!file.open(QFile::ReadOnly))
{
QMessageBox::critical(m_widgetParent, QObject::tr("Error"), QObject::tr("Unable to open %1: %2").arg(file.fileName(), file.errorString()));
return false;
}
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(file.readAll(), &error);
if (error.error != QJsonParseError::NoError)
{
QMessageBox::critical(m_widgetParent, QObject::tr("Error"), QObject::tr("Unable to parse %1: %2 at %3").arg(file.fileName(), error.errorString()).arg(error.offset));
return false;
}
*out = doc.object();
return true;
}
|