Remove a lot of error code and error handling madness.
This commit is contained in:
parent
80d146866c
commit
28ad9befdc
@ -257,6 +257,7 @@ SET(MULTIMC_SOURCES
|
|||||||
MultiMC.h
|
MultiMC.h
|
||||||
MultiMC.cpp
|
MultiMC.cpp
|
||||||
MultiMCVersion.h
|
MultiMCVersion.h
|
||||||
|
MMCError.h
|
||||||
|
|
||||||
# Logging
|
# Logging
|
||||||
logger/QsDebugOutput.cpp
|
logger/QsDebugOutput.cpp
|
||||||
@ -353,6 +354,10 @@ logic/ModList.cpp
|
|||||||
logic/InstanceLauncher.h
|
logic/InstanceLauncher.h
|
||||||
logic/InstanceLauncher.cpp
|
logic/InstanceLauncher.cpp
|
||||||
|
|
||||||
|
# JSON parsing helpers
|
||||||
|
logic/MMCJson.h
|
||||||
|
logic/MMCJson.cpp
|
||||||
|
|
||||||
# network stuffs
|
# network stuffs
|
||||||
logic/net/NetAction.h
|
logic/net/NetAction.h
|
||||||
logic/net/MD5EtagDownload.h
|
logic/net/MD5EtagDownload.h
|
||||||
|
29
MMCError.h
Normal file
29
MMCError.h
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <exception>
|
||||||
|
#include <QString>
|
||||||
|
#include <logger/QsLog.h>
|
||||||
|
|
||||||
|
class MMCError : public std::exception
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
MMCError(QString cause)
|
||||||
|
{
|
||||||
|
exceptionCause = cause;
|
||||||
|
QLOG_ERROR() << errorName() + ": " + cause;
|
||||||
|
};
|
||||||
|
virtual ~MMCError(){};
|
||||||
|
virtual const char *what() const noexcept
|
||||||
|
{
|
||||||
|
return exceptionCause.toLocal8Bit();
|
||||||
|
};
|
||||||
|
virtual QString cause() const
|
||||||
|
{
|
||||||
|
return exceptionCause;
|
||||||
|
}
|
||||||
|
virtual QString errorName()
|
||||||
|
{
|
||||||
|
return "MultiMC Error";
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
QString exceptionCause;
|
||||||
|
};
|
53
logic/MMCJson.cpp
Normal file
53
logic/MMCJson.cpp
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
#include "MMCJson.h"
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
|
bool MMCJson::ensureBoolean(const QJsonValue val, const QString what)
|
||||||
|
{
|
||||||
|
if (!val.isBool())
|
||||||
|
throw JSONValidationError(what + " is not boolean");
|
||||||
|
return val.isBool();
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonValue MMCJson::ensureExists(QJsonValue val, const QString what)
|
||||||
|
{
|
||||||
|
if(val.isNull())
|
||||||
|
throw JSONValidationError(what + " does not exist");
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonArray MMCJson::ensureArray(const QJsonValue val, const QString what)
|
||||||
|
{
|
||||||
|
if (!val.isArray())
|
||||||
|
throw JSONValidationError(what + " is not an array");
|
||||||
|
return val.toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
double MMCJson::ensureDouble(const QJsonValue val, const QString what)
|
||||||
|
{
|
||||||
|
if (!val.isDouble())
|
||||||
|
throw JSONValidationError(what + " is not a number");
|
||||||
|
double ret = val.toDouble();
|
||||||
|
}
|
||||||
|
|
||||||
|
int MMCJson::ensureInteger(const QJsonValue val, const QString what)
|
||||||
|
{
|
||||||
|
double ret = ensureDouble(val, what);
|
||||||
|
if (fmod(ret, 1) != 0)
|
||||||
|
throw JSONValidationError(what + " is not an integer");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonObject MMCJson::ensureObject(const QJsonValue val, const QString what)
|
||||||
|
{
|
||||||
|
if (!val.isObject())
|
||||||
|
throw JSONValidationError(what + " is not an object");
|
||||||
|
return val.toObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
QString MMCJson::ensureString(const QJsonValue val, const QString what)
|
||||||
|
{
|
||||||
|
if (!val.isString())
|
||||||
|
throw JSONValidationError(what + " is not a string");
|
||||||
|
return val.toString();
|
||||||
|
}
|
||||||
|
|
46
logic/MMCJson.h
Normal file
46
logic/MMCJson.h
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
/**
|
||||||
|
* Some de-bullshitting for Qt JSON failures.
|
||||||
|
*
|
||||||
|
* Simple exception-throwing
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include <QJsonValue>
|
||||||
|
#include <QJsonObject>
|
||||||
|
#include <QJsonArray>
|
||||||
|
#include "MMCError.h"
|
||||||
|
|
||||||
|
class JSONValidationError : public MMCError
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
JSONValidationError(QString cause) : MMCError(cause) {};
|
||||||
|
virtual QString errorName()
|
||||||
|
{
|
||||||
|
return "JSONValidationError";
|
||||||
|
};
|
||||||
|
virtual ~JSONValidationError() {};
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace MMCJson
|
||||||
|
{
|
||||||
|
/// make sure the value exists. throw otherwise.
|
||||||
|
QJsonValue ensureExists(QJsonValue val, const QString what = "value");
|
||||||
|
|
||||||
|
/// make sure the value is converted into an object. throw otherwise.
|
||||||
|
QJsonObject ensureObject(const QJsonValue val, const QString what = "value");
|
||||||
|
|
||||||
|
/// make sure the value is converted into an array. throw otherwise.
|
||||||
|
QJsonArray ensureArray(const QJsonValue val, QString what = "value");
|
||||||
|
|
||||||
|
/// make sure the value is converted into a string. throw otherwise.
|
||||||
|
QString ensureString(const QJsonValue val, QString what = "value");
|
||||||
|
|
||||||
|
/// make sure the value is converted into a boolean. throw otherwise.
|
||||||
|
bool ensureBoolean(const QJsonValue val, QString what = "value");
|
||||||
|
|
||||||
|
/// make sure the value is converted into an integer. throw otherwise.
|
||||||
|
int ensureInteger(const QJsonValue val, QString what = "value");
|
||||||
|
|
||||||
|
/// make sure the value is converted into a double precision floating number. throw otherwise.
|
||||||
|
double ensureDouble(const QJsonValue val, QString what = "value");
|
||||||
|
}
|
@ -73,10 +73,7 @@ bool OneSixVersionBuilder::buildInternal(const bool onlyVanilla, const QStringLi
|
|||||||
file.fileId = "org.multimc.external." + file.name;
|
file.fileId = "org.multimc.external." + file.name;
|
||||||
file.version = QString();
|
file.version = QString();
|
||||||
file.mcVersion = QString();
|
file.mcVersion = QString();
|
||||||
bool isError = false;
|
file.applyTo(m_version);
|
||||||
auto errorcode = file.applyTo(m_version);
|
|
||||||
if(errorcode != VersionFile::NoApplyError)
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
// else, if there's custom json, we just do that.
|
// else, if there's custom json, we just do that.
|
||||||
else if (QFile::exists(root.absoluteFilePath("custom.json")))
|
else if (QFile::exists(root.absoluteFilePath("custom.json")))
|
||||||
@ -91,9 +88,7 @@ bool OneSixVersionBuilder::buildInternal(const bool onlyVanilla, const QStringLi
|
|||||||
file.filename = "custom.json";
|
file.filename = "custom.json";
|
||||||
file.fileId = "org.multimc.custom.json";
|
file.fileId = "org.multimc.custom.json";
|
||||||
file.version = QString();
|
file.version = QString();
|
||||||
auto errorcode = file.applyTo(m_version);
|
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("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.")
|
// QObject::tr("Error while applying %1. Please check MultiMC-0.log for more info.")
|
||||||
}
|
}
|
||||||
@ -111,18 +106,8 @@ bool OneSixVersionBuilder::buildInternal(const bool onlyVanilla, const QStringLi
|
|||||||
file.fileId = "org.multimc.version.json";
|
file.fileId = "org.multimc.version.json";
|
||||||
file.version = m_instance->intendedVersionId();
|
file.version = m_instance->intendedVersionId();
|
||||||
file.mcVersion = m_instance->intendedVersionId();
|
file.mcVersion = m_instance->intendedVersionId();
|
||||||
auto error = file.applyTo(m_version);
|
file.applyTo(m_version);
|
||||||
if (error != VersionFile::NoApplyError)
|
// QObject::tr("Error while applying %1. Please check MultiMC-0.log for more info.").arg(root.absoluteFilePath("version.json")));
|
||||||
{
|
|
||||||
/*
|
|
||||||
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)
|
if (onlyVanilla)
|
||||||
break;
|
break;
|
||||||
@ -155,17 +140,8 @@ bool OneSixVersionBuilder::buildInternal(const bool onlyVanilla, const QStringLi
|
|||||||
{
|
{
|
||||||
QLOG_DEBUG() << "Applying file with order" << order;
|
QLOG_DEBUG() << "Applying file with order" << order;
|
||||||
auto filePair = files[order];
|
auto filePair = files[order];
|
||||||
auto error = filePair.second.applyTo(m_version);
|
filePair.second.applyTo(m_version);
|
||||||
if (error != VersionFile::NoApplyError)
|
// QObject::tr("Error while applying %1. Please check MultiMC-0.log for more info.").arg(filePair.first));
|
||||||
{
|
|
||||||
/*
|
|
||||||
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);
|
} while(0);
|
||||||
|
|
||||||
@ -203,37 +179,12 @@ bool OneSixVersionBuilder::readJsonAndApply(const QJsonObject &obj)
|
|||||||
{
|
{
|
||||||
m_version->clear();
|
m_version->clear();
|
||||||
|
|
||||||
bool isError = false;
|
VersionFile file = VersionFile::fromJson(QJsonDocument(obj), QString(), false);
|
||||||
VersionFile file = VersionFile::fromJson(QJsonDocument(obj), QString(), false, isError);
|
// QObject::tr("Error while reading. Please check MultiMC-0.log for more info."));
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
file.applyTo(m_version);
|
||||||
|
// QObject::tr("Error while applying. Please check MultiMC-0.log for more info."));
|
||||||
|
// QObject::tr("The version descriptors of this instance are not compatible with the current version of MultiMC"));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -242,11 +193,7 @@ bool OneSixVersionBuilder::parseJsonFile(const QFileInfo& fileInfo, const bool r
|
|||||||
QFile file(fileInfo.absoluteFilePath());
|
QFile file(fileInfo.absoluteFilePath());
|
||||||
if (!file.open(QFile::ReadOnly))
|
if (!file.open(QFile::ReadOnly))
|
||||||
{
|
{
|
||||||
/*
|
// QObject::tr("Unable to open %1: %2").arg(file.fileName(), file.errorString());
|
||||||
QMessageBox::critical(
|
|
||||||
m_widgetParent, QObject::tr("Error"),
|
|
||||||
QObject::tr("Unable to open %1: %2").arg(file.fileName(), file.errorString()));
|
|
||||||
*/
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
QJsonParseError error;
|
QJsonParseError error;
|
||||||
@ -261,17 +208,8 @@ bool OneSixVersionBuilder::parseJsonFile(const QFileInfo& fileInfo, const bool r
|
|||||||
*/
|
*/
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
bool isError = false;
|
*out = VersionFile::fromJson(doc, file.fileName(), requireOrder, isFTB);
|
||||||
*out = VersionFile::fromJson(doc, file.fileName(), requireOrder, isError, isFTB);
|
// QObject::tr("Error while reading %1. Please check MultiMC-0.log for more info.").arg(file.fileName());
|
||||||
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,19 +7,20 @@
|
|||||||
#include "logic/VersionFile.h"
|
#include "logic/VersionFile.h"
|
||||||
#include "logic/OneSixLibrary.h"
|
#include "logic/OneSixLibrary.h"
|
||||||
#include "logic/VersionFinal.h"
|
#include "logic/VersionFinal.h"
|
||||||
|
#include "MMCJson.h"
|
||||||
|
|
||||||
|
using namespace MMCJson;
|
||||||
|
|
||||||
#define CURRENT_MINIMUM_LAUNCHER_VERSION 14
|
#define CURRENT_MINIMUM_LAUNCHER_VERSION 14
|
||||||
|
|
||||||
VersionFile::Library VersionFile::Library::fromJson(const QJsonObject &libObj,
|
VersionFile::Library VersionFile::Library::fromJson(const QJsonObject &libObj,
|
||||||
const QString &filename, bool &isError)
|
const QString &filename)
|
||||||
{
|
{
|
||||||
isError = true;
|
|
||||||
Library out;
|
Library out;
|
||||||
if (!libObj.contains("name"))
|
if (!libObj.contains("name"))
|
||||||
{
|
{
|
||||||
QLOG_ERROR() << filename << "contains a library that doesn't have a 'name' field";
|
throw JSONValidationError(filename +
|
||||||
return out;
|
"contains a library that doesn't have a 'name' field");
|
||||||
}
|
}
|
||||||
out.name = libObj.value("name").toString();
|
out.name = libObj.value("name").toString();
|
||||||
|
|
||||||
@ -45,44 +46,17 @@ VersionFile::Library VersionFile::Library::fromJson(const QJsonObject &libObj,
|
|||||||
readString("MMC-absoluteUrl", out.absoluteUrl);
|
readString("MMC-absoluteUrl", out.absoluteUrl);
|
||||||
if (libObj.contains("extract"))
|
if (libObj.contains("extract"))
|
||||||
{
|
{
|
||||||
if (!libObj.value("extract").isObject())
|
|
||||||
{
|
|
||||||
QLOG_ERROR() << filename
|
|
||||||
<< "contains a library with an 'extract' field that's not an object";
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
QJsonObject extractObj = libObj.value("extract").toObject();
|
|
||||||
if (!extractObj.contains("exclude") || !extractObj.value("exclude").isArray())
|
|
||||||
{
|
|
||||||
QLOG_ERROR() << filename << "contains a library with an invalid 'extract' field";
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
out.applyExcludes = true;
|
out.applyExcludes = true;
|
||||||
QJsonArray excludeArray = extractObj.value("exclude").toArray();
|
auto extractObj = ensureObject(libObj.value("extract"));
|
||||||
for (auto excludeVal : excludeArray)
|
for (auto excludeVal : ensureArray(extractObj.value("exclude")))
|
||||||
{
|
{
|
||||||
if (!excludeVal.isString())
|
out.excludes.append(ensureString(excludeVal));
|
||||||
{
|
|
||||||
QLOG_WARN() << filename << "contains a library that contains an 'extract' "
|
|
||||||
"field that contains an invalid 'exclude' entry "
|
|
||||||
"(skipping)";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
out.excludes.append(excludeVal.toString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (libObj.contains("natives"))
|
if (libObj.contains("natives"))
|
||||||
{
|
{
|
||||||
if (!libObj.value("natives").isObject())
|
|
||||||
{
|
|
||||||
QLOG_ERROR() << filename
|
|
||||||
<< "contains a library with a 'natives' field that's not an object";
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
out.applyNatives = true;
|
out.applyNatives = true;
|
||||||
QJsonObject nativesObj = libObj.value("natives").toObject();
|
QJsonObject nativesObj = ensureObject(libObj.value("natives"));
|
||||||
for (auto it = nativesObj.begin(); it != nativesObj.end(); ++it)
|
for (auto it = nativesObj.begin(); it != nativesObj.end(); ++it)
|
||||||
{
|
{
|
||||||
if (!it.value().isString())
|
if (!it.value().isString())
|
||||||
@ -101,24 +75,19 @@ VersionFile::Library VersionFile::Library::fromJson(const QJsonObject &libObj,
|
|||||||
out.applyRules = true;
|
out.applyRules = true;
|
||||||
out.rules = rulesFromJsonV4(libObj);
|
out.rules = rulesFromJsonV4(libObj);
|
||||||
}
|
}
|
||||||
isError = false;
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filename,
|
VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filename, const bool requireOrder, const bool isFTB)
|
||||||
const bool requireOrder, bool &isError, const bool isFTB)
|
|
||||||
{
|
{
|
||||||
VersionFile out;
|
VersionFile out;
|
||||||
isError = true;
|
|
||||||
if (doc.isEmpty() || doc.isNull())
|
if (doc.isEmpty() || doc.isNull())
|
||||||
{
|
{
|
||||||
QLOG_ERROR() << filename << "is empty or null";
|
throw JSONValidationError(filename + " is empty or null");
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
if (!doc.isObject())
|
if (!doc.isObject())
|
||||||
{
|
{
|
||||||
QLOG_ERROR() << "The root of" << filename << "is not an object";
|
throw JSONValidationError("The root of " + filename + " is not an object");
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonObject root = doc.object();
|
QJsonObject root = doc.object();
|
||||||
@ -127,18 +96,11 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen
|
|||||||
{
|
{
|
||||||
if (root.contains("order"))
|
if (root.contains("order"))
|
||||||
{
|
{
|
||||||
if (root.value("order").isDouble())
|
out.order = ensureInteger(root.value("order"));
|
||||||
{
|
|
||||||
out.order = root.value("order").toDouble();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
QLOG_ERROR() << "'order' field contains an invalid value in" << filename;
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
// FIXME: evaluate if we don't want to throw exceptions here instead
|
||||||
QLOG_ERROR() << filename << "doesn't contain an order field";
|
QLOG_ERROR() << filename << "doesn't contain an order field";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -153,19 +115,11 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen
|
|||||||
{
|
{
|
||||||
if (root.contains(key))
|
if (root.contains(key))
|
||||||
{
|
{
|
||||||
QJsonValue val = root.value(key);
|
variable = ensureString(root.value(key));
|
||||||
if (!val.isString())
|
|
||||||
{
|
|
||||||
QLOG_WARN() << key << "is not a string in" << filename << "(skipping)";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
variable = val.toString();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// FTB id attribute is completely bogus. We ignore it.
|
// FIXME: This should be ignored when applying.
|
||||||
if (!isFTB)
|
if (!isFTB)
|
||||||
{
|
{
|
||||||
readString("id", out.id);
|
readString("id", out.id);
|
||||||
@ -180,108 +134,47 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen
|
|||||||
readString("releaseTime", out.releaseTime);
|
readString("releaseTime", out.releaseTime);
|
||||||
readString("time", out.time);
|
readString("time", out.time);
|
||||||
readString("assets", out.assets);
|
readString("assets", out.assets);
|
||||||
|
|
||||||
if (root.contains("minimumLauncherVersion"))
|
if (root.contains("minimumLauncherVersion"))
|
||||||
{
|
{
|
||||||
QJsonValue val = root.value("minimumLauncherVersion");
|
out.minimumLauncherVersion = ensureInteger(root.value("minimumLauncherVersion"));
|
||||||
if (!val.isDouble())
|
|
||||||
{
|
|
||||||
QLOG_WARN() << "minimumLauncherVersion is not an int in" << filename
|
|
||||||
<< "(skipping)";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
out.minimumLauncherVersion = val.toDouble();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (root.contains("tweakers"))
|
if (root.contains("tweakers"))
|
||||||
{
|
{
|
||||||
QJsonValue tweakersVal = root.value("tweakers");
|
|
||||||
if (!tweakersVal.isArray())
|
|
||||||
{
|
|
||||||
QLOG_ERROR() << filename << "contains a 'tweakers' field, but it's not an array";
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
out.shouldOverwriteTweakers = true;
|
out.shouldOverwriteTweakers = true;
|
||||||
QJsonArray tweakers = root.value("tweakers").toArray();
|
for (auto tweakerVal : ensureArray(root.value("tweakers")))
|
||||||
for (auto tweakerVal : tweakers)
|
|
||||||
{
|
{
|
||||||
if (!tweakerVal.isString())
|
out.overwriteTweakers.append(ensureString(tweakerVal));
|
||||||
{
|
|
||||||
QLOG_ERROR() << filename
|
|
||||||
<< "contains a 'tweakers' field entry that's not a string";
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
out.overwriteTweakers.append(tweakerVal.toString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (root.contains("+tweakers"))
|
if (root.contains("+tweakers"))
|
||||||
{
|
{
|
||||||
QJsonValue tweakersVal = root.value("+tweakers");
|
for (auto tweakerVal : ensureArray(root.value("+tweakers")))
|
||||||
if (!tweakersVal.isArray())
|
|
||||||
{
|
{
|
||||||
QLOG_ERROR() << filename << "contains a '+tweakers' field, but it's not an array";
|
out.addTweakers.append(ensureString(tweakerVal));
|
||||||
return out;
|
|
||||||
}
|
|
||||||
QJsonArray tweakers = root.value("+tweakers").toArray();
|
|
||||||
for (auto tweakerVal : tweakers)
|
|
||||||
{
|
|
||||||
if (!tweakerVal.isString())
|
|
||||||
{
|
|
||||||
QLOG_ERROR() << filename
|
|
||||||
<< "contains a '+tweakers' field entry that's not a string";
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
out.addTweakers.append(tweakerVal.toString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (root.contains("-tweakers"))
|
if (root.contains("-tweakers"))
|
||||||
{
|
{
|
||||||
QJsonValue tweakersVal = root.value("-tweakers");
|
for (auto tweakerVal : ensureArray(root.value("-tweakers")))
|
||||||
if (!tweakersVal.isArray())
|
|
||||||
{
|
{
|
||||||
QLOG_ERROR() << filename << "contains a '-tweakers' field, but it's not an array";
|
out.removeTweakers.append(ensureString(tweakerVal));
|
||||||
return out;
|
|
||||||
}
|
|
||||||
out.shouldOverwriteTweakers = true;
|
|
||||||
QJsonArray tweakers = root.value("-tweakers").toArray();
|
|
||||||
for (auto tweakerVal : tweakers)
|
|
||||||
{
|
|
||||||
if (!tweakerVal.isString())
|
|
||||||
{
|
|
||||||
QLOG_ERROR() << filename
|
|
||||||
<< "contains a '-tweakers' field entry that's not a string";
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
out.removeTweakers.append(tweakerVal.toString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (root.contains("libraries"))
|
if (root.contains("libraries"))
|
||||||
{
|
{
|
||||||
|
// FIXME: This should be done when applying.
|
||||||
out.shouldOverwriteLibs = !isFTB;
|
out.shouldOverwriteLibs = !isFTB;
|
||||||
QJsonValue librariesVal = root.value("libraries");
|
for (auto libVal : ensureArray(root.value("libraries")))
|
||||||
if (!librariesVal.isArray())
|
|
||||||
{
|
{
|
||||||
QLOG_ERROR() << filename << "contains a 'libraries' field, but its not an array";
|
auto libObj = ensureObject(libVal);
|
||||||
return out;
|
|
||||||
}
|
Library lib = Library::fromJson(libObj, filename);
|
||||||
QJsonArray librariesArray = librariesVal.toArray();
|
// FIXME: This should be done when applying.
|
||||||
for (auto libVal : librariesArray)
|
|
||||||
{
|
|
||||||
if (!libVal.isObject())
|
|
||||||
{
|
|
||||||
QLOG_ERROR() << filename << "contains a library that's not an object";
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
QJsonObject libObj = libVal.toObject();
|
|
||||||
bool error;
|
|
||||||
Library lib = Library::fromJson(libObj, filename, error);
|
|
||||||
if (error)
|
|
||||||
{
|
|
||||||
QLOG_ERROR() << "Error while reading a library entry in" << filename;
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
if (isFTB)
|
if (isFTB)
|
||||||
{
|
{
|
||||||
lib.hint = "local";
|
lib.hint = "local";
|
||||||
@ -294,36 +187,18 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (root.contains("+libraries"))
|
if (root.contains("+libraries"))
|
||||||
{
|
{
|
||||||
QJsonValue librariesVal = root.value("+libraries");
|
for (auto libVal : ensureArray(root.value("+libraries")))
|
||||||
if (!librariesVal.isArray())
|
|
||||||
{
|
{
|
||||||
QLOG_ERROR() << filename << "contains a '+libraries' field, but its not an array";
|
QJsonObject libObj = ensureObject(libVal);
|
||||||
return out;
|
QJsonValue insertVal = ensureExists(libObj.value("insert"));
|
||||||
}
|
|
||||||
QJsonArray librariesArray = librariesVal.toArray();
|
// parse the library
|
||||||
for (auto libVal : librariesArray)
|
Library lib = Library::fromJson(libObj, filename);
|
||||||
{
|
|
||||||
if (!libVal.isObject())
|
// TODO: utility functions for handling this case. templates?
|
||||||
{
|
|
||||||
QLOG_ERROR() << filename << "contains a library that's not an object";
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
QJsonObject libObj = libVal.toObject();
|
|
||||||
bool error;
|
|
||||||
Library lib = Library::fromJson(libObj, filename, error);
|
|
||||||
if (error)
|
|
||||||
{
|
|
||||||
QLOG_ERROR() << "Error while reading a library entry in" << filename;
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
if (!libObj.contains("insert"))
|
|
||||||
{
|
|
||||||
QLOG_ERROR() << "Missing 'insert' field in '+libraries' field in" << filename;
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
QJsonValue insertVal = libObj.value("insert");
|
|
||||||
QString insertString;
|
QString insertString;
|
||||||
{
|
{
|
||||||
if (insertVal.isString())
|
if (insertVal.isString())
|
||||||
@ -335,8 +210,8 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen
|
|||||||
QJsonObject insertObj = insertVal.toObject();
|
QJsonObject insertObj = insertVal.toObject();
|
||||||
if (insertObj.isEmpty())
|
if (insertObj.isEmpty())
|
||||||
{
|
{
|
||||||
QLOG_ERROR() << "One library has an empty insert object in" << filename;
|
throw JSONValidationError("One library has an empty insert object in " +
|
||||||
return out;
|
filename);
|
||||||
}
|
}
|
||||||
insertString = insertObj.keys().first();
|
insertString = insertObj.keys().first();
|
||||||
lib.insertData = insertObj.value(insertString).toString();
|
lib.insertData = insertObj.value(insertString).toString();
|
||||||
@ -360,13 +235,12 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
QLOG_ERROR() << "A '+' library in" << filename
|
throw JSONValidationError("A '+' library in " + filename +
|
||||||
<< "contains an invalid insert type";
|
" contains an invalid insert type");
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
if (libObj.contains("MMC-depend") && libObj.value("MMC-depend").isString())
|
if (libObj.contains("MMC-depend"))
|
||||||
{
|
{
|
||||||
const QString dependString = libObj.value("MMC-depend").toString();
|
const QString dependString = ensureString(libObj.value("MMC-depend"));
|
||||||
if (dependString == "hard")
|
if (dependString == "hard")
|
||||||
{
|
{
|
||||||
lib.dependType = Library::Hard;
|
lib.dependType = Library::Hard;
|
||||||
@ -377,9 +251,8 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
QLOG_ERROR() << "A '+' library in" << filename
|
throw JSONValidationError("A '+' library in " + filename +
|
||||||
<< "contains an invalid depend type";
|
" contains an invalid depend type");
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out.addLibs.append(lib);
|
out.addLibs.append(lib);
|
||||||
@ -387,36 +260,12 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen
|
|||||||
}
|
}
|
||||||
if (root.contains("-libraries"))
|
if (root.contains("-libraries"))
|
||||||
{
|
{
|
||||||
QJsonValue librariesVal = root.value("-libraries");
|
for (auto libVal : ensureArray(root.value("-libraries")))
|
||||||
if (!librariesVal.isArray())
|
|
||||||
{
|
{
|
||||||
QLOG_ERROR() << filename << "contains a '-libraries' field, but its not an array";
|
auto libObj = ensureObject(libVal);
|
||||||
return out;
|
out.removeLibs.append(ensureString(libObj.value("name")));
|
||||||
}
|
|
||||||
QJsonArray librariesArray = librariesVal.toArray();
|
|
||||||
for (auto libVal : librariesArray)
|
|
||||||
{
|
|
||||||
if (!libVal.isObject())
|
|
||||||
{
|
|
||||||
QLOG_ERROR() << filename << "contains a library that's not an object";
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
QJsonObject libObj = libVal.toObject();
|
|
||||||
if (!libObj.contains("name"))
|
|
||||||
{
|
|
||||||
QLOG_ERROR() << filename << "contains a library without a name";
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
if (!libObj.value("name").isString())
|
|
||||||
{
|
|
||||||
QLOG_ERROR() << filename << "contains a library without a valid 'name' field";
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
out.removeLibs.append(libObj.value("name").toString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
isError = false;
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -457,16 +306,15 @@ int VersionFile::findLibrary(QList<std::shared_ptr<OneSixLibrary>> haystack,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
VersionFile::ApplyError VersionFile::applyTo(VersionFinal *version)
|
void VersionFile::applyTo(VersionFinal *version)
|
||||||
{
|
{
|
||||||
if (minimumLauncherVersion != -1)
|
if (minimumLauncherVersion != -1)
|
||||||
{
|
{
|
||||||
if (minimumLauncherVersion > CURRENT_MINIMUM_LAUNCHER_VERSION)
|
if (minimumLauncherVersion > CURRENT_MINIMUM_LAUNCHER_VERSION)
|
||||||
{
|
{
|
||||||
QLOG_ERROR() << filename << "is for a different launcher version ("
|
throw VersionBuildError(
|
||||||
<< minimumLauncherVersion << "), current supported is"
|
QString("%1 is for a different launcher version (%2), current supported is %3")
|
||||||
<< CURRENT_MINIMUM_LAUNCHER_VERSION;
|
.arg(filename, minimumLauncherVersion, CURRENT_MINIMUM_LAUNCHER_VERSION));
|
||||||
return LauncherVersionError;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -475,8 +323,8 @@ VersionFile::ApplyError VersionFile::applyTo(VersionFinal *version)
|
|||||||
if (QRegExp(mcVersion, Qt::CaseInsensitive, QRegExp::Wildcard).indexIn(version->id) ==
|
if (QRegExp(mcVersion, Qt::CaseInsensitive, QRegExp::Wildcard).indexIn(version->id) ==
|
||||||
-1)
|
-1)
|
||||||
{
|
{
|
||||||
QLOG_ERROR() << filename << "is for a different version of Minecraft";
|
throw VersionBuildError(
|
||||||
return OtherError;
|
QString("%1 is for a different version of Minecraft").arg(filename));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -587,7 +435,7 @@ VersionFile::ApplyError VersionFile::applyTo(VersionFinal *version)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
QLOG_WARN() << "Couldn't find" << lib.insertData << "(skipping)";
|
QLOG_WARN() << "Couldn't find" << lib.name << "(skipping)";
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -623,10 +471,10 @@ VersionFile::ApplyError VersionFile::applyTo(VersionFinal *version)
|
|||||||
if (ourVersion > otherVersion ||
|
if (ourVersion > otherVersion ||
|
||||||
(lib.dependType == Library::Hard && ourVersion != otherVersion))
|
(lib.dependType == Library::Hard && ourVersion != otherVersion))
|
||||||
{
|
{
|
||||||
QLOG_ERROR() << "Error resolving library dependencies between"
|
throw VersionBuildError(
|
||||||
<< otherLib->rawName() << "and" << lib.name << "in"
|
QString(
|
||||||
<< filename;
|
"Error resolving library dependencies between %1 and %2 in %3.")
|
||||||
return OtherError;
|
.arg(otherLib->rawName(), lib.name, filename));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -651,10 +499,10 @@ VersionFile::ApplyError VersionFile::applyTo(VersionFinal *version)
|
|||||||
// it: fail
|
// it: fail
|
||||||
if (lib.dependType == Library::Hard)
|
if (lib.dependType == Library::Hard)
|
||||||
{
|
{
|
||||||
QLOG_ERROR() << "Error resolving library dependencies between"
|
throw VersionBuildError(QString(
|
||||||
<< otherLib->rawName() << "and" << lib.name << "in"
|
"Error resolving library dependencies between %1 and %2 in %3.")
|
||||||
<< filename;
|
.arg(otherLib->rawName(), lib.name,
|
||||||
return OtherError;
|
filename));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -697,6 +545,4 @@ VersionFile::ApplyError VersionFile::applyTo(VersionFinal *version)
|
|||||||
versionFile.filename = filename;
|
versionFile.filename = filename;
|
||||||
versionFile.order = order;
|
versionFile.order = order;
|
||||||
version->versionFiles.append(versionFile);
|
version->versionFiles.append(versionFile);
|
||||||
|
|
||||||
return NoApplyError;
|
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,21 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include "logic/OpSys.h"
|
#include "logic/OpSys.h"
|
||||||
#include "logic/OneSixRule.h"
|
#include "logic/OneSixRule.h"
|
||||||
|
#include "MMCError.h"
|
||||||
|
|
||||||
class VersionFinal;
|
class VersionFinal;
|
||||||
|
|
||||||
|
class VersionBuildError : public MMCError
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
VersionBuildError(QString cause) : MMCError(cause) {};
|
||||||
|
virtual QString errorName()
|
||||||
|
{
|
||||||
|
return "VersionBuildError";
|
||||||
|
};
|
||||||
|
virtual ~VersionBuildError() {};
|
||||||
|
};
|
||||||
|
|
||||||
struct VersionFile
|
struct VersionFile
|
||||||
{
|
{
|
||||||
int order;
|
int order;
|
||||||
@ -65,26 +78,17 @@ struct VersionFile
|
|||||||
};
|
};
|
||||||
DependType dependType = Soft;
|
DependType dependType = Soft;
|
||||||
|
|
||||||
static Library fromJson(const QJsonObject &libObj, const QString &filename,
|
static Library fromJson(const QJsonObject &libObj, const QString &filename);
|
||||||
bool &isError);
|
|
||||||
};
|
};
|
||||||
bool shouldOverwriteLibs = false;
|
bool shouldOverwriteLibs = false;
|
||||||
QList<Library> overwriteLibs;
|
QList<Library> overwriteLibs;
|
||||||
QList<Library> addLibs;
|
QList<Library> addLibs;
|
||||||
QList<QString> removeLibs;
|
QList<QString> removeLibs;
|
||||||
|
|
||||||
enum ApplyError
|
|
||||||
{
|
|
||||||
LauncherVersionError,
|
|
||||||
OtherError,
|
|
||||||
NoApplyError
|
|
||||||
};
|
|
||||||
|
|
||||||
static VersionFile fromJson(const QJsonDocument &doc, const QString &filename,
|
static VersionFile fromJson(const QJsonDocument &doc, const QString &filename,
|
||||||
const bool requireOrder, bool &isError,
|
const bool requireOrder, const bool isFTB = false);
|
||||||
const bool isFTB = false);
|
|
||||||
|
|
||||||
static std::shared_ptr<OneSixLibrary> createLibrary(const Library &lib);
|
static std::shared_ptr<OneSixLibrary> createLibrary(const Library &lib);
|
||||||
int findLibrary(QList<std::shared_ptr<OneSixLibrary>> haystack, const QString &needle);
|
int findLibrary(QList<std::shared_ptr<OneSixLibrary>> haystack, const QString &needle);
|
||||||
ApplyError applyTo(VersionFinal *version);
|
void applyTo(VersionFinal *version);
|
||||||
};
|
};
|
Loading…
Reference in New Issue
Block a user