2014-01-22 12:03:32 +05:30
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2014-01-24 22:42:02 +05:30
|
|
|
#include "OneSixVersionBuilder.h"
|
2014-01-22 12:03:32 +05:30
|
|
|
|
|
|
|
#include <QList>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QDir>
|
2014-01-22 19:50:48 +05:30
|
|
|
#include <QDebug>
|
2014-01-22 12:03:32 +05:30
|
|
|
|
2014-03-02 03:36:47 +05:30
|
|
|
#include "VersionFinal.h"
|
2014-01-24 22:42:02 +05:30
|
|
|
#include "OneSixInstance.h"
|
|
|
|
#include "OneSixRule.h"
|
2014-03-02 03:36:47 +05:30
|
|
|
#include "VersionFile.h"
|
2014-02-02 18:35:07 +05:30
|
|
|
#include "modutils.h"
|
2014-01-24 02:01:41 +05:30
|
|
|
#include "logger/QsLog.h"
|
2014-01-22 12:03:32 +05:30
|
|
|
|
2014-01-27 23:50:07 +05:30
|
|
|
OneSixVersionBuilder::OneSixVersionBuilder()
|
|
|
|
{
|
2014-01-23 02:45:50 +05:30
|
|
|
}
|
|
|
|
|
2014-03-02 03:36:47 +05:30
|
|
|
bool OneSixVersionBuilder::build(VersionFinal *version, OneSixInstance *instance,
|
2014-02-21 22:31:06 +05:30
|
|
|
QWidget *widgetParent, const bool onlyVanilla, const QStringList &external)
|
2014-01-23 02:45:50 +05:30
|
|
|
{
|
2014-01-27 23:50:07 +05:30
|
|
|
OneSixVersionBuilder builder;
|
|
|
|
builder.m_version = version;
|
|
|
|
builder.m_instance = instance;
|
|
|
|
builder.m_widgetParent = widgetParent;
|
2014-03-02 03:36:47 +05:30
|
|
|
return builder.buildInternal(onlyVanilla, external);
|
2014-01-23 02:45:50 +05:30
|
|
|
}
|
|
|
|
|
2014-03-02 03:36:47 +05:30
|
|
|
bool OneSixVersionBuilder::readJsonAndApplyToVersion(VersionFinal *version, const QJsonObject &obj)
|
2014-01-23 02:45:50 +05:30
|
|
|
{
|
2014-01-27 23:50:07 +05:30
|
|
|
OneSixVersionBuilder builder;
|
|
|
|
builder.m_version = version;
|
|
|
|
builder.m_instance = 0;
|
|
|
|
builder.m_widgetParent = 0;
|
2014-03-02 03:36:47 +05:30
|
|
|
return builder.readJsonAndApply(obj);
|
2014-01-27 23:50:07 +05:30
|
|
|
}
|
2014-01-22 12:03:32 +05:30
|
|
|
|
2014-03-02 03:36:47 +05:30
|
|
|
bool OneSixVersionBuilder::buildInternal(const bool onlyVanilla, const QStringList &external)
|
2014-01-27 23:50:07 +05:30
|
|
|
{
|
|
|
|
m_version->clear();
|
|
|
|
|
|
|
|
QDir root(m_instance->instanceRoot());
|
|
|
|
QDir patches(root.absoluteFilePath("patches/"));
|
|
|
|
|
2014-02-24 07:05:01 +05:30
|
|
|
// if we do external files, do just those.
|
|
|
|
if(!external.isEmpty()) for (auto fileName : external)
|
2014-01-23 02:45:50 +05:30
|
|
|
{
|
2014-02-24 07:05:01 +05:30
|
|
|
QLOG_INFO() << "Reading" << fileName;
|
|
|
|
VersionFile file;
|
2014-03-02 06:21:40 +05:30
|
|
|
if (!parseJsonFile(QFileInfo(fileName), false, &file, fileName.endsWith("pack.json")))
|
2014-01-24 02:01:41 +05:30
|
|
|
{
|
2014-02-24 07:05:01 +05:30
|
|
|
return false;
|
2014-01-23 02:45:50 +05:30
|
|
|
}
|
2014-02-24 07:05:01 +05:30
|
|
|
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")))
|
2014-01-23 02:45:50 +05:30
|
|
|
{
|
2014-01-28 12:09:43 +05:30
|
|
|
QLOG_INFO() << "Reading custom.json";
|
2014-01-27 23:50:07 +05:30
|
|
|
VersionFile file;
|
2014-03-02 03:36:47 +05:30
|
|
|
if (!parseJsonFile(QFileInfo(root.absoluteFilePath("custom.json")), false, &file))
|
2014-01-23 02:45:50 +05:30
|
|
|
{
|
2014-01-27 23:50:07 +05:30
|
|
|
return false;
|
2014-02-21 22:31:06 +05:30
|
|
|
}
|
2014-02-02 00:12:47 +05:30
|
|
|
file.name = "custom.json";
|
|
|
|
file.filename = "custom.json";
|
|
|
|
file.fileId = "org.multimc.custom.json";
|
|
|
|
file.version = QString();
|
2014-02-24 07:05:01 +05:30
|
|
|
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.")
|
2014-02-21 22:31:06 +05:30
|
|
|
}
|
2014-02-24 07:05:01 +05:30
|
|
|
// version.json -> patches/*.json -> user.json
|
|
|
|
else do
|
2014-02-21 22:31:06 +05:30
|
|
|
{
|
2014-02-24 07:05:01 +05:30
|
|
|
// version.json
|
|
|
|
QLOG_INFO() << "Reading version.json";
|
|
|
|
VersionFile file;
|
2014-03-02 03:36:47 +05:30
|
|
|
if (!parseJsonFile(QFileInfo(root.absoluteFilePath("version.json")), false, &file))
|
2014-01-23 02:45:50 +05:30
|
|
|
{
|
2014-01-27 23:50:07 +05:30
|
|
|
return false;
|
2014-01-23 02:45:50 +05:30
|
|
|
}
|
2014-02-24 07:05:01 +05:30
|
|
|
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)
|
2014-02-21 22:31:06 +05:30
|
|
|
{
|
2014-02-17 22:16:43 +05:30
|
|
|
QMessageBox::critical(
|
|
|
|
m_widgetParent, QObject::tr("Error"),
|
2014-02-24 07:05:01 +05:30
|
|
|
QObject::tr(
|
|
|
|
"Error while applying %1. Please check MultiMC-0.log for more info.")
|
|
|
|
.arg(root.absoluteFilePath("version.json")));
|
2014-02-17 22:16:43 +05:30
|
|
|
return false;
|
|
|
|
}
|
2014-01-23 02:45:50 +05:30
|
|
|
|
2014-02-24 07:05:01 +05:30
|
|
|
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))
|
2014-01-24 02:01:41 +05:30
|
|
|
{
|
2014-02-24 07:05:01 +05:30
|
|
|
QLOG_INFO() << "Reading" << info.fileName();
|
2014-02-21 22:31:06 +05:30
|
|
|
VersionFile file;
|
2014-03-02 03:36:47 +05:30
|
|
|
if (!parseJsonFile(info, true, &file))
|
2014-02-21 22:31:06 +05:30
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-24 07:05:01 +05:30
|
|
|
if (overrideOrder.contains(file.fileId))
|
2014-01-22 12:03:32 +05:30
|
|
|
{
|
2014-02-24 07:05:01 +05:30
|
|
|
file.order = overrideOrder.value(file.fileId);
|
2014-01-22 12:03:32 +05:30
|
|
|
}
|
2014-02-24 07:05:01 +05:30
|
|
|
if (files.contains(file.order))
|
2014-02-17 22:16:43 +05:30
|
|
|
{
|
2014-02-24 07:05:01 +05:30
|
|
|
QLOG_ERROR() << file.fileId << "has the same order as" << files[file.order].second.fileId;
|
2014-02-17 22:16:43 +05:30
|
|
|
return false;
|
|
|
|
}
|
2014-02-24 07:05:01 +05:30
|
|
|
files.insert(file.order, qMakePair(info.fileName(), file));
|
2014-01-23 02:45:50 +05:30
|
|
|
}
|
2014-02-24 07:05:01 +05:30
|
|
|
for (auto order : files.keys())
|
2014-01-23 02:45:50 +05:30
|
|
|
{
|
2014-02-24 07:05:01 +05:30
|
|
|
QLOG_DEBUG() << "Applying file with order" << order;
|
|
|
|
auto filePair = files[order];
|
|
|
|
auto error = filePair.second.applyTo(m_version);
|
|
|
|
if (error != VersionFile::NoApplyError)
|
2014-02-21 22:31:06 +05:30
|
|
|
{
|
|
|
|
QMessageBox::critical(
|
|
|
|
m_widgetParent, QObject::tr("Error"),
|
2014-02-02 18:47:44 +05:30
|
|
|
QObject::tr("Error while applying %1. Please check MultiMC-0.log "
|
|
|
|
"for more info.").arg(filePair.first));
|
2014-02-21 22:31:06 +05:30
|
|
|
return false;
|
|
|
|
}
|
2014-02-02 18:47:44 +05:30
|
|
|
}
|
2014-02-24 07:05:01 +05:30
|
|
|
} while(0);
|
2014-01-22 12:03:32 +05:30
|
|
|
|
2014-01-27 23:50:07 +05:30
|
|
|
// some final touches
|
2014-01-23 02:45:50 +05:30
|
|
|
{
|
2014-01-27 23:50:07 +05:30
|
|
|
if (m_version->assets.isEmpty())
|
2014-01-23 02:45:50 +05:30
|
|
|
{
|
2014-01-27 23:50:07 +05:30
|
|
|
m_version->assets = "legacy";
|
2014-01-23 02:45:50 +05:30
|
|
|
}
|
2014-01-27 23:50:07 +05:30
|
|
|
if (m_version->minecraftArguments.isEmpty())
|
2014-01-23 02:45:50 +05:30
|
|
|
{
|
2014-01-27 23:50:07 +05:30
|
|
|
QString toCompare = m_version->processArguments.toLower();
|
|
|
|
if (toCompare == "legacy")
|
2014-01-23 02:45:50 +05:30
|
|
|
{
|
2014-01-27 23:50:07 +05:30
|
|
|
m_version->minecraftArguments = " ${auth_player_name} ${auth_session}";
|
2014-01-23 02:45:50 +05:30
|
|
|
}
|
2014-01-27 23:50:07 +05:30
|
|
|
else if (toCompare == "username_session")
|
2014-01-23 02:45:50 +05:30
|
|
|
{
|
2014-01-27 23:50:07 +05:30
|
|
|
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}";
|
2014-01-23 02:45:50 +05:30
|
|
|
}
|
|
|
|
}
|
2014-01-22 12:03:32 +05:30
|
|
|
}
|
2014-01-27 23:50:07 +05:30
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-02 03:36:47 +05:30
|
|
|
bool OneSixVersionBuilder::readJsonAndApply(const QJsonObject &obj)
|
2014-01-27 23:50:07 +05:30
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2014-02-17 22:16:43 +05:30
|
|
|
VersionFile::ApplyError error = file.applyTo(m_version);
|
|
|
|
if (error == VersionFile::OtherError)
|
2014-01-27 23:50:07 +05:30
|
|
|
{
|
|
|
|
QMessageBox::critical(
|
|
|
|
m_widgetParent, QObject::tr("Error"),
|
|
|
|
QObject::tr("Error while applying. Please check MultiMC-0.log for more info."));
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-17 22:16:43 +05:30
|
|
|
else if (error == VersionFile::LauncherVersionError)
|
|
|
|
{
|
|
|
|
QMessageBox::critical(
|
|
|
|
m_widgetParent, QObject::tr("Error"),
|
2014-02-18 01:01:50 +05:30
|
|
|
QObject::tr("The version descriptors of this instance are not compatible with the current version of MultiMC"));
|
2014-02-17 22:16:43 +05:30
|
|
|
return false;
|
|
|
|
}
|
2014-01-27 23:50:07 +05:30
|
|
|
|
2014-01-22 12:03:32 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-02 06:21:40 +05:30
|
|
|
bool OneSixVersionBuilder::parseJsonFile(const QFileInfo& fileInfo, const bool requireOrder, VersionFile* out, bool isFTB)
|
2014-01-22 12:03:32 +05:30
|
|
|
{
|
|
|
|
QFile file(fileInfo.absoluteFilePath());
|
|
|
|
if (!file.open(QFile::ReadOnly))
|
|
|
|
{
|
2014-01-27 23:50:07 +05:30
|
|
|
QMessageBox::critical(
|
|
|
|
m_widgetParent, QObject::tr("Error"),
|
|
|
|
QObject::tr("Unable to open %1: %2").arg(file.fileName(), file.errorString()));
|
2014-01-22 12:03:32 +05:30
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(file.readAll(), &error);
|
|
|
|
if (error.error != QJsonParseError::NoError)
|
|
|
|
{
|
2014-01-27 23:50:07 +05:30
|
|
|
QMessageBox::critical(m_widgetParent, QObject::tr("Error"),
|
|
|
|
QObject::tr("Unable to parse %1: %2 at %3")
|
|
|
|
.arg(file.fileName(), error.errorString())
|
|
|
|
.arg(error.offset));
|
2014-01-22 12:03:32 +05:30
|
|
|
return false;
|
|
|
|
}
|
2014-01-27 23:50:07 +05:30
|
|
|
bool isError = false;
|
2014-03-02 06:21:40 +05:30
|
|
|
*out = VersionFile::fromJson(doc, file.fileName(), requireOrder, isError, isFTB);
|
2014-01-27 23:50:07 +05:30
|
|
|
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()));
|
|
|
|
}
|
2014-01-22 12:03:32 +05:30
|
|
|
return true;
|
|
|
|
}
|
2014-02-08 21:52:26 +05:30
|
|
|
|
|
|
|
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))
|
|
|
|
{
|
2014-02-17 21:49:58 +05:30
|
|
|
QLOG_ERROR() << "Couldn't open" << orderFile.fileName()
|
|
|
|
<< " for reading:" << orderFile.errorString();
|
2014-02-08 21:52:26 +05:30
|
|
|
QLOG_WARN() << "Ignoring overriden order";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(orderFile.readAll(), &error);
|
|
|
|
if (error.error != QJsonParseError::NoError || !doc.isObject())
|
|
|
|
{
|
2014-02-17 21:49:58 +05:30
|
|
|
QLOG_ERROR() << "Couldn't parse" << orderFile.fileName() << ":"
|
|
|
|
<< error.errorString();
|
2014-02-08 21:52:26 +05:30
|
|
|
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;
|
|
|
|
}
|
2014-03-02 03:36:47 +05:30
|
|
|
|
2014-02-17 21:49:58 +05:30
|
|
|
bool OneSixVersionBuilder::writeOverrideOrders(const QMap<QString, int> &order,
|
|
|
|
OneSixInstance *instance)
|
2014-02-08 21:52:26 +05:30
|
|
|
{
|
|
|
|
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))
|
|
|
|
{
|
2014-02-17 21:49:58 +05:30
|
|
|
QLOG_ERROR() << "Couldn't open" << orderFile.fileName()
|
|
|
|
<< "for writing:" << orderFile.errorString();
|
2014-02-08 21:52:26 +05:30
|
|
|
return false;
|
|
|
|
}
|
|
|
|
orderFile.write(QJsonDocument(obj).toJson(QJsonDocument::Indented));
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-24 07:05:01 +05:30
|
|
|
|