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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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-05-10 05:23:32 +05:30
|
|
|
#include <qresource.h>
|
2014-05-09 00:50:10 +05:30
|
|
|
#include <modutils.h>
|
|
|
|
|
2014-05-10 05:23:32 +05:30
|
|
|
#include "MultiMC.h"
|
2014-05-09 20:46:25 +05:30
|
|
|
#include "logic/minecraft/VersionBuilder.h"
|
2014-05-09 00:50:10 +05:30
|
|
|
#include "logic/minecraft/VersionFinal.h"
|
|
|
|
#include "logic/minecraft/OneSixRule.h"
|
2014-05-10 05:23:32 +05:30
|
|
|
#include "logic/minecraft/VersionPatch.h"
|
2014-05-09 00:50:10 +05:30
|
|
|
#include "logic/minecraft/VersionFile.h"
|
2014-05-10 05:23:32 +05:30
|
|
|
#include "VersionBuildError.h"
|
|
|
|
#include "MinecraftVersionList.h"
|
2014-05-09 00:50:10 +05:30
|
|
|
|
|
|
|
#include "logic/OneSixInstance.h"
|
|
|
|
#include "logic/MMCJson.h"
|
2014-01-22 12:03:32 +05:30
|
|
|
|
2014-01-24 02:01:41 +05:30
|
|
|
#include "logger/QsLog.h"
|
2014-01-22 12:03:32 +05:30
|
|
|
|
2014-05-09 20:46:25 +05:30
|
|
|
VersionBuilder::VersionBuilder()
|
2014-01-27 23:50:07 +05:30
|
|
|
{
|
2014-01-23 02:45:50 +05:30
|
|
|
}
|
|
|
|
|
2014-05-10 05:23:32 +05:30
|
|
|
void VersionBuilder::build(VersionFinal *version, OneSixInstance *instance,
|
|
|
|
const QStringList &external)
|
2014-01-23 02:45:50 +05:30
|
|
|
{
|
2014-05-09 20:46:25 +05:30
|
|
|
VersionBuilder builder;
|
2014-01-27 23:50:07 +05:30
|
|
|
builder.m_version = version;
|
|
|
|
builder.m_instance = instance;
|
2014-05-10 05:23:32 +05:30
|
|
|
builder.external_patches = external;
|
|
|
|
builder.buildInternal();
|
2014-01-23 02:45:50 +05:30
|
|
|
}
|
|
|
|
|
2014-05-10 05:23:32 +05:30
|
|
|
void VersionBuilder::readJsonAndApplyToVersion(VersionFinal *version, const QJsonObject &obj)
|
2014-01-23 02:45:50 +05:30
|
|
|
{
|
2014-05-09 20:46:25 +05:30
|
|
|
VersionBuilder builder;
|
2014-01-27 23:50:07 +05:30
|
|
|
builder.m_version = version;
|
|
|
|
builder.m_instance = 0;
|
2014-03-03 05:53:10 +05:30
|
|
|
builder.readJsonAndApply(obj);
|
2014-01-27 23:50:07 +05:30
|
|
|
}
|
2014-01-22 12:03:32 +05:30
|
|
|
|
2014-05-10 05:23:32 +05:30
|
|
|
void VersionBuilder::buildFromCustomJson()
|
2014-01-27 23:50:07 +05:30
|
|
|
{
|
2014-05-10 05:23:32 +05:30
|
|
|
QLOG_INFO() << "Building version from custom.json within the instance.";
|
|
|
|
QLOG_INFO() << "Reading custom.json";
|
|
|
|
auto file = parseJsonFile(QFileInfo(instance_root.absoluteFilePath("custom.json")), false);
|
|
|
|
file->name = "custom.json";
|
|
|
|
file->filename = "custom.json";
|
|
|
|
file->fileId = "org.multimc.custom.json";
|
|
|
|
file->order = -1;
|
|
|
|
file->version = QString();
|
|
|
|
m_version->VersionPatches.append(file);
|
|
|
|
// 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.")
|
|
|
|
// some final touches
|
|
|
|
m_version->finalize();
|
|
|
|
return;
|
|
|
|
}
|
2014-01-27 23:50:07 +05:30
|
|
|
|
2014-05-10 05:23:32 +05:30
|
|
|
void VersionBuilder::buildFromVersionJson()
|
|
|
|
{
|
|
|
|
QLOG_INFO() << "Building version from version.json and patches within the instance.";
|
|
|
|
QLOG_INFO() << "Reading version.json";
|
|
|
|
auto file = parseJsonFile(QFileInfo(instance_root.absoluteFilePath("version.json")), false);
|
|
|
|
file->name = "Minecraft";
|
|
|
|
file->fileId = "org.multimc.version.json";
|
|
|
|
file->order = -1;
|
|
|
|
file->version = m_instance->intendedVersionId();
|
|
|
|
file->mcVersion = m_instance->intendedVersionId();
|
|
|
|
m_version->VersionPatches.append(file);
|
2014-01-27 23:50:07 +05:30
|
|
|
|
2014-05-10 05:23:32 +05:30
|
|
|
// load all patches, put into map for ordering, apply in the right order
|
|
|
|
readInstancePatches();
|
|
|
|
|
|
|
|
// some final touches
|
|
|
|
m_version->finalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VersionBuilder::readInstancePatches()
|
|
|
|
{
|
|
|
|
QDir patches(instance_root.absoluteFilePath("patches/"));
|
|
|
|
QMap<int, QPair<QString, VersionFilePtr>> files;
|
|
|
|
for (auto info : patches.entryInfoList(QStringList() << "*.json", QDir::Files))
|
2014-03-15 00:21:56 +05:30
|
|
|
{
|
2014-05-10 05:23:32 +05:30
|
|
|
QLOG_INFO() << "Reading" << info.fileName();
|
|
|
|
auto file = parseJsonFile(info, true);
|
|
|
|
if(file->fileId == "net.minecraft")
|
|
|
|
continue;
|
|
|
|
if(file->fileId == "org.lwjgl")
|
|
|
|
continue;
|
|
|
|
if (files.contains(file->order))
|
2014-03-10 04:12:25 +05:30
|
|
|
{
|
2014-05-10 05:23:32 +05:30
|
|
|
throw VersionBuildError(QObject::tr("%1 has the same order as %2")
|
|
|
|
.arg(file->fileId, files[file->order].second->fileId));
|
2014-03-10 04:12:25 +05:30
|
|
|
}
|
2014-05-10 05:23:32 +05:30
|
|
|
files.insert(file->order, qMakePair(info.fileName(), file));
|
2014-03-15 00:21:56 +05:30
|
|
|
}
|
2014-05-10 05:23:32 +05:30
|
|
|
for (auto order : files.keys())
|
|
|
|
{
|
|
|
|
auto &filePair = files[order];
|
|
|
|
m_version->VersionPatches.append(filePair.second);
|
2014-02-21 22:31:06 +05:30
|
|
|
}
|
2014-05-10 05:23:32 +05:30
|
|
|
}
|
2014-01-22 12:03:32 +05:30
|
|
|
|
2014-05-10 05:23:32 +05:30
|
|
|
void VersionBuilder::buildFromExternalPatches()
|
|
|
|
{
|
|
|
|
QLOG_INFO() << "Building version from external files.";
|
|
|
|
int externalOrder = -1;
|
|
|
|
for (auto fileName : external_patches)
|
|
|
|
{
|
|
|
|
QLOG_INFO() << "Reading" << fileName;
|
|
|
|
auto file = parseJsonFile(QFileInfo(fileName), false, fileName.endsWith("pack.json"));
|
|
|
|
file->name = QFileInfo(fileName).fileName();
|
|
|
|
file->fileId = "org.multimc.external." + file->name;
|
|
|
|
file->order = (externalOrder += 1);
|
|
|
|
file->version = QString();
|
|
|
|
file->mcVersion = QString();
|
|
|
|
m_version->VersionPatches.append(file);
|
|
|
|
}
|
2014-01-27 23:50:07 +05:30
|
|
|
// some final touches
|
2014-03-20 03:53:59 +05:30
|
|
|
m_version->finalize();
|
2014-03-03 06:14:07 +05:30
|
|
|
}
|
|
|
|
|
2014-05-10 05:23:32 +05:30
|
|
|
void VersionBuilder::buildFromMultilayer()
|
|
|
|
{
|
|
|
|
QLOG_INFO() << "Building version from multilayered sources.";
|
|
|
|
// just the builtin stuff for now
|
|
|
|
auto minecraftList = MMC->minecraftlist();
|
|
|
|
auto mcversion = minecraftList->findVersion(m_instance->intendedVersionId());
|
|
|
|
auto minecraftPatch = std::dynamic_pointer_cast<VersionPatch>(mcversion);
|
|
|
|
if(!minecraftPatch)
|
|
|
|
{
|
|
|
|
throw VersionIncomplete("net.minecraft");
|
|
|
|
}
|
|
|
|
minecraftPatch->setOrder(-2);
|
|
|
|
m_version->VersionPatches.append(minecraftPatch);
|
2014-03-20 03:53:59 +05:30
|
|
|
|
2014-05-10 05:23:32 +05:30
|
|
|
QResource LWJGL(":/versions/LWJGL/2.9.1.json");
|
|
|
|
auto lwjgl = parseJsonFile(LWJGL.absoluteFilePath(), false, false);
|
|
|
|
auto lwjglPatch = std::dynamic_pointer_cast<VersionPatch>(lwjgl);
|
|
|
|
if(!lwjglPatch)
|
|
|
|
{
|
|
|
|
throw VersionIncomplete("org.lwjgl");
|
|
|
|
}
|
|
|
|
lwjglPatch->setOrder(-1);
|
|
|
|
m_version->VersionPatches.append(lwjglPatch);
|
|
|
|
|
|
|
|
// load all patches, put into map for ordering, apply in the right order
|
|
|
|
readInstancePatches();
|
|
|
|
|
|
|
|
m_version->finalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VersionBuilder::buildInternal()
|
|
|
|
{
|
|
|
|
m_version->VersionPatches.clear();
|
|
|
|
instance_root = QDir(m_instance->instanceRoot());
|
|
|
|
// if we do external files, do just those.
|
|
|
|
if (!external_patches.isEmpty())
|
|
|
|
{
|
|
|
|
buildFromExternalPatches();
|
|
|
|
}
|
|
|
|
// else, if there's custom json, we just do that.
|
|
|
|
else if (QFile::exists(instance_root.absoluteFilePath("custom.json")))
|
|
|
|
{
|
|
|
|
buildFromCustomJson();
|
|
|
|
}
|
|
|
|
// version.json -> patches/*.json
|
|
|
|
else if (QFile::exists(instance_root.absoluteFilePath("version.json")))
|
|
|
|
{
|
|
|
|
buildFromVersionJson();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buildFromMultilayer();
|
|
|
|
}
|
|
|
|
}
|
2014-01-27 23:50:07 +05:30
|
|
|
|
2014-05-09 20:46:25 +05:30
|
|
|
void VersionBuilder::readJsonAndApply(const QJsonObject &obj)
|
2014-01-27 23:50:07 +05:30
|
|
|
{
|
|
|
|
m_version->clear();
|
|
|
|
|
2014-03-05 06:20:05 +05:30
|
|
|
auto file = VersionFile::fromJson(QJsonDocument(obj), QString(), false);
|
2014-03-02 23:42:04 +05:30
|
|
|
// QObject::tr("Error while reading. Please check MultiMC-0.log for more info."));
|
2014-01-27 23:50:07 +05:30
|
|
|
|
2014-03-05 06:20:05 +05:30
|
|
|
file->applyTo(m_version);
|
2014-05-10 05:23:32 +05:30
|
|
|
m_version->VersionPatches.append(file);
|
2014-03-02 23:42:04 +05:30
|
|
|
// QObject::tr("Error while applying. Please check MultiMC-0.log for more info."));
|
2014-03-10 04:12:25 +05:30
|
|
|
// QObject::tr("The version descriptors of this instance are not compatible with the current
|
|
|
|
// version of MultiMC"));
|
2014-01-22 12:03:32 +05:30
|
|
|
}
|
|
|
|
|
2014-05-10 05:23:32 +05:30
|
|
|
VersionFilePtr VersionBuilder::parseJsonFile(const QFileInfo &fileInfo, const bool requireOrder,
|
|
|
|
bool isFTB)
|
2014-01-22 12:03:32 +05:30
|
|
|
{
|
|
|
|
QFile file(fileInfo.absoluteFilePath());
|
|
|
|
if (!file.open(QFile::ReadOnly))
|
|
|
|
{
|
2014-03-10 04:12:25 +05:30
|
|
|
throw JSONValidationError(QObject::tr("Unable to open the version file %1: %2.")
|
|
|
|
.arg(fileInfo.fileName(), file.errorString()));
|
2014-01-22 12:03:32 +05:30
|
|
|
}
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(file.readAll(), &error);
|
|
|
|
if (error.error != QJsonParseError::NoError)
|
|
|
|
{
|
2014-05-10 05:23:32 +05:30
|
|
|
throw JSONValidationError(
|
|
|
|
QObject::tr("Unable to process the version file %1: %2 at %3.")
|
|
|
|
.arg(fileInfo.fileName(), error.errorString())
|
|
|
|
.arg(error.offset));
|
2014-01-22 12:03:32 +05:30
|
|
|
}
|
2014-03-03 05:53:10 +05:30
|
|
|
return VersionFile::fromJson(doc, file.fileName(), requireOrder, isFTB);
|
2014-03-10 04:12:25 +05:30
|
|
|
// 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
|
|
|
}
|
2014-02-08 21:52:26 +05:30
|
|
|
|
2014-05-09 20:46:25 +05:30
|
|
|
QMap<QString, int> VersionBuilder::readOverrideOrders(OneSixInstance *instance)
|
2014-02-08 21:52:26 +05:30
|
|
|
{
|
|
|
|
QMap<QString, int> out;
|
2014-03-05 06:20:05 +05:30
|
|
|
|
|
|
|
// make sure the order file exists
|
|
|
|
if (!QDir(instance->instanceRoot()).exists("order.json"))
|
|
|
|
return out;
|
|
|
|
|
|
|
|
// and it can be opened
|
|
|
|
QFile orderFile(instance->instanceRoot() + "/order.json");
|
|
|
|
if (!orderFile.open(QFile::ReadOnly))
|
2014-02-08 21:52:26 +05:30
|
|
|
{
|
2014-03-05 06:20:05 +05:30
|
|
|
QLOG_ERROR() << "Couldn't open" << orderFile.fileName()
|
2014-03-10 04:12:25 +05:30
|
|
|
<< " for reading:" << orderFile.errorString();
|
2014-03-05 06:20:05 +05:30
|
|
|
QLOG_WARN() << "Ignoring overriden order";
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// and it's valid JSON
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(orderFile.readAll(), &error);
|
2014-03-10 04:12:25 +05:30
|
|
|
if (error.error != QJsonParseError::NoError)
|
2014-03-05 06:20:05 +05:30
|
|
|
{
|
2014-03-10 04:12:25 +05:30
|
|
|
QLOG_ERROR() << "Couldn't parse" << orderFile.fileName() << ":" << error.errorString();
|
2014-03-05 06:20:05 +05:30
|
|
|
QLOG_WARN() << "Ignoring overriden order";
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// and then read it and process it if all above is true.
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto obj = MMCJson::ensureObject(doc);
|
|
|
|
for (auto it = obj.begin(); it != obj.end(); ++it)
|
2014-02-08 21:52:26 +05:30
|
|
|
{
|
2014-03-05 06:20:05 +05:30
|
|
|
if (it.key().startsWith("org.multimc."))
|
2014-02-08 21:52:26 +05:30
|
|
|
{
|
2014-03-05 06:20:05 +05:30
|
|
|
continue;
|
2014-02-08 21:52:26 +05:30
|
|
|
}
|
2014-03-05 06:20:05 +05:30
|
|
|
out.insert(it.key(), MMCJson::ensureInteger(it.value()));
|
2014-02-08 21:52:26 +05:30
|
|
|
}
|
|
|
|
}
|
2014-03-10 04:12:25 +05:30
|
|
|
catch (JSONValidationError &err)
|
2014-03-05 06:20:05 +05:30
|
|
|
{
|
|
|
|
QLOG_ERROR() << "Couldn't parse" << orderFile.fileName() << ": bad file format";
|
|
|
|
QLOG_WARN() << "Ignoring overriden order";
|
|
|
|
return out;
|
|
|
|
}
|
2014-02-08 21:52:26 +05:30
|
|
|
return out;
|
|
|
|
}
|
2014-03-02 03:36:47 +05:30
|
|
|
|
2014-05-09 20:46:25 +05:30
|
|
|
bool VersionBuilder::writeOverrideOrders(const QMap<QString, int> &order,
|
2014-05-10 05:23:32 +05:30
|
|
|
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;
|
|
|
|
}
|