summaryrefslogtreecommitdiffstats
path: root/logic/OneSixVersionBuilder.cpp
blob: 0d4d66a22fa2916afaf97020b2ec7e049a118a83 (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
/* 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 "OneSixVersionBuilder.h"

#include <QList>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QFile>
#include <QFileInfo>
#include <QMessageBox>
#include <QObject>
#include <QDir>
#include <QDebug>

#include "VersionFinal.h"
#include "OneSixInstance.h"
#include "OneSixRule.h"
#include "VersionFile.h"
#include "modutils.h"
#include "logger/QsLog.h"

OneSixVersionBuilder::OneSixVersionBuilder()
{
}

bool OneSixVersionBuilder::build(VersionFinal *version, OneSixInstance *instance,
								 QWidget *widgetParent, const bool onlyVanilla, const QStringList &external)
{
	OneSixVersionBuilder builder;
	builder.m_version = version;
	builder.m_instance = instance;
	builder.m_widgetParent = widgetParent;
	return builder.buildInternal(onlyVanilla, external);
}

bool OneSixVersionBuilder::readJsonAndApplyToVersion(VersionFinal *version, const QJsonObject &obj)
{
	OneSixVersionBuilder builder;
	builder.m_version = version;
	builder.m_instance = 0;
	builder.m_widgetParent = 0;
	return builder.readJsonAndApply(obj);
}

bool OneSixVersionBuilder::buildInternal(const bool onlyVanilla, const QStringList &external)
{
	m_version->clear();

	QDir root(m_instance->instanceRoot());
	QDir patches(root.absoluteFilePath("patches/"));

	// if we do external files, do just those.
	if(!external.isEmpty()) for (auto fileName : external)
	{
		QLOG_INFO() << "Reading" << fileName;
		VersionFile file;
		if (!parseJsonFile(QFileInfo(fileName), false, &file, fileName.endsWith("pack.json") ? IsFTBPackJson : NoFlags))
		{
			return false;
		}
		file.name = QFileInfo(fileName).fileName();
		file.fileId = "org.multimc.external." + file.name;
		file.version = QString();
		file.mcVersion = QString();
		bool isError = false;
		auto errorcode = file.applyTo(m_version);
		if(errorcode != VersionFile::NoApplyError)
			return false;
	}
	// else, if there's custom json, we just do that.
	else if (QFile::exists(root.absoluteFilePath("custom.json")))
	{
		QLOG_INFO() << "Reading custom.json";
		VersionFile file;
		if (!parseJsonFile(QFileInfo(root.absoluteFilePath("custom.json")), false, &file))
		{
			return false;
		}
		file.name = "custom.json";
		file.filename = "custom.json";
		file.fileId = "org.multimc.custom.json";
		file.version = QString();
		auto errorcode = file.applyTo(m_version);
		if(errorcode != VersionFile::NoApplyError)
			return false;
		// QObject::tr("The version descriptors of this instance are not compatible with the current version of MultiMC"));
		// QObject::tr("Error while applying %1. Please check MultiMC-0.log for more info.")
	}
	// version.json -> patches/*.json -> user.json
	else do
	{
		// version.json
		QLOG_INFO() << "Reading version.json";
		VersionFile file;
		if (!parseJsonFile(QFileInfo(root.absoluteFilePath("version.json")), false, &file))
		{
			return false;
		}
		file.name = "version.json";
		file.fileId = "org.multimc.version.json";
		file.version = m_instance->intendedVersionId();
		file.mcVersion = m_instance->intendedVersionId();
		auto error = file.applyTo(m_version);
		if (error != VersionFile::NoApplyError)
		{
			QMessageBox::critical(
						m_widgetParent, QObject::tr("Error"),
						QObject::tr(
							"Error while applying %1. Please check MultiMC-0.log for more info.")
						.arg(root.absoluteFilePath("version.json")));
			return false;
		}

		if (onlyVanilla)
			break;

		// patches/
		// load all, put into map for ordering, apply in the right order
		QMap<QString, int> overrideOrder = readOverrideOrders(m_instance);

		QMap<int, QPair<QString, VersionFile>> files;
		for (auto info : patches.entryInfoList(QStringList() << "*.json", QDir::Files))
		{
			QLOG_INFO() << "Reading" << info.fileName();
			VersionFile file;
			if (!parseJsonFile(info, true, &file))
			{
				return false;
			}
			if (overrideOrder.contains(file.fileId))
			{
				file.order = overrideOrder.value(file.fileId);
			}
			if (files.contains(file.order))
			{
				QLOG_ERROR() << file.fileId << "has the same order as" << files[file.order].second.fileId;
				return false;
			}
			files.insert(file.order, qMakePair(info.fileName(), file));
		}
		for (auto order : files.keys())
		{
			QLOG_DEBUG() << "Applying file with order" << order;
			auto filePair = files[order];
			auto error = filePair.second.applyTo(m_version);
			if (error != VersionFile::NoApplyError)
			{
				QMessageBox::critical(
							m_widgetParent, QObject::tr("Error"),
							QObject::tr("Error while applying %1. Please check MultiMC-0.log "
										"for more info.").arg(filePair.first));
				return false;
			}
		}
	} while(0);

	// some final touches
	{
		if (m_version->assets.isEmpty())
		{
			m_version->assets = "legacy";
		}
		if (m_version->minecraftArguments.isEmpty())
		{
			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}";
			}
		}
	}

	return true;
}

bool OneSixVersionBuilder::readJsonAndApply(const QJsonObject &obj)
{
	m_version->clear();

	bool isError = false;
	VersionFile file = VersionFile::fromJson(QJsonDocument(obj), QString(), false, isError);
	if (isError)
	{
		QMessageBox::critical(
			m_widgetParent, QObject::tr("Error"),
			QObject::tr("Error while reading. Please check MultiMC-0.log for more info."));
		return false;
	}
	VersionFile::ApplyError error = file.applyTo(m_version);
	if (error == VersionFile::OtherError)
	{
		QMessageBox::critical(
			m_widgetParent, QObject::tr("Error"),
			QObject::tr("Error while applying. Please check MultiMC-0.log for more info."));
		return false;
	}
	else if (error == VersionFile::LauncherVersionError)
	{
		QMessageBox::critical(
					m_widgetParent, QObject::tr("Error"),
					QObject::tr("The version descriptors of this instance are not compatible with the current version of MultiMC"));
		return false;
	}

	return true;
}

bool OneSixVersionBuilder::parseJsonFile(const QFileInfo &fileInfo, const bool requireOrder,
								VersionFile *out, const ParseFlags flags)
{
	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;
	}
	bool isError = false;
	*out = VersionFile::fromJson(doc, file.fileName(), requireOrder, isError, flags);
	if (isError)
	{
		QMessageBox::critical(
			m_widgetParent, QObject::tr("Error"),
			QObject::tr("Error while reading %1. Please check MultiMC-0.log for more info.")
				.arg(file.fileName()));
		;
	}
	return true;
}

QMap<QString, int> OneSixVersionBuilder::readOverrideOrders(OneSixInstance *instance)
{
	QMap<QString, int> out;
	if (QDir(instance->instanceRoot()).exists("order.json"))
	{
		QFile orderFile(instance->instanceRoot() + "/order.json");
		if (!orderFile.open(QFile::ReadOnly))
		{
			QLOG_ERROR() << "Couldn't open" << orderFile.fileName()
						 << " for reading:" << orderFile.errorString();
			QLOG_WARN() << "Ignoring overriden order";
		}
		else
		{
			QJsonParseError error;
			QJsonDocument doc = QJsonDocument::fromJson(orderFile.readAll(), &error);
			if (error.error != QJsonParseError::NoError || !doc.isObject())
			{
				QLOG_ERROR() << "Couldn't parse" << orderFile.fileName() << ":"
							 << error.errorString();
				QLOG_WARN() << "Ignoring overriden order";
			}
			else
			{
				QJsonObject obj = doc.object();
				for (auto it = obj.begin(); it != obj.end(); ++it)
				{
					if (it.key().startsWith("org.multimc."))
					{
						continue;
					}
					out.insert(it.key(), it.value().toDouble());
				}
			}
		}
	}
	return out;
}

bool OneSixVersionBuilder::writeOverrideOrders(const QMap<QString, int> &order,
											   OneSixInstance *instance)
{
	QJsonObject obj;
	for (auto it = order.cbegin(); it != order.cend(); ++it)
	{
		if (it.key().startsWith("org.multimc."))
		{
			continue;
		}
		obj.insert(it.key(), it.value());
	}
	QFile orderFile(instance->instanceRoot() + "/order.json");
	if (!orderFile.open(QFile::WriteOnly))
	{
		QLOG_ERROR() << "Couldn't open" << orderFile.fileName()
					 << "for writing:" << orderFile.errorString();
		return false;
	}
	orderFile.write(QJsonDocument(obj).toJson(QJsonDocument::Indented));
	return true;
}