From 28ad9befdcac246eb69a434be970abc29a80bc80 Mon Sep 17 00:00:00 2001 From: Petr Mrázek Date: Sun, 2 Mar 2014 19:12:04 +0100 Subject: Remove a lot of error code and error handling madness. --- logic/MMCJson.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 logic/MMCJson.h (limited to 'logic/MMCJson.h') diff --git a/logic/MMCJson.h b/logic/MMCJson.h new file mode 100644 index 00000000..3e7342b5 --- /dev/null +++ b/logic/MMCJson.h @@ -0,0 +1,46 @@ +/** + * Some de-bullshitting for Qt JSON failures. + * + * Simple exception-throwing + */ + +#pragma once +#include +#include +#include +#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"); +} -- cgit From 47bc7e5ee377dacfeb7cf3d13f07cfd24db906fb Mon Sep 17 00:00:00 2001 From: Petr Mrázek Date: Wed, 5 Mar 2014 01:50:05 +0100 Subject: More refactor. --- gui/dialogs/OneSixModEditDialog.cpp | 2 +- logic/MMCJson.cpp | 7 ++ logic/MMCJson.h | 4 + logic/OneSixLibrary.h | 3 + logic/OneSixVersionBuilder.cpp | 123 ++++++++++++----------- logic/OneSixVersionBuilder.h | 3 +- logic/VersionFile.cpp | 189 +++++++++++++++++------------------- logic/VersionFile.h | 16 +-- logic/VersionFinal.cpp | 42 -------- logic/VersionFinal.h | 10 +- 10 files changed, 184 insertions(+), 215 deletions(-) (limited to 'logic/MMCJson.h') diff --git a/gui/dialogs/OneSixModEditDialog.cpp b/gui/dialogs/OneSixModEditDialog.cpp index cc383993..96205d0a 100644 --- a/gui/dialogs/OneSixModEditDialog.cpp +++ b/gui/dialogs/OneSixModEditDialog.cpp @@ -357,7 +357,7 @@ QMap OneSixModEditDialog::getExistingOrder() const QMap order; // default { - for (VersionFinal::VersionFile file : m_version->versionFiles) + for (auto & file : m_version->versionFiles) { if (file.id.startsWith("org.multimc.")) { diff --git a/logic/MMCJson.cpp b/logic/MMCJson.cpp index 14cde0c1..80d36204 100644 --- a/logic/MMCJson.cpp +++ b/logic/MMCJson.cpp @@ -44,6 +44,13 @@ QJsonObject MMCJson::ensureObject(const QJsonValue val, const QString what) return val.toObject(); } +QJsonObject MMCJson::ensureObject(const QJsonDocument val, const QString what) +{ + if (!val.isObject()) + throw JSONValidationError(what + " is not an object"); + return val.object(); +} + QString MMCJson::ensureString(const QJsonValue val, const QString what) { if (!val.isString()) diff --git a/logic/MMCJson.h b/logic/MMCJson.h index 3e7342b5..b0d898fc 100644 --- a/logic/MMCJson.h +++ b/logic/MMCJson.h @@ -7,6 +7,7 @@ #pragma once #include #include +#include #include #include "MMCError.h" @@ -29,6 +30,9 @@ 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 document is converted into an object. throw otherwise. +QJsonObject ensureObject(const QJsonDocument val, const QString what = "value"); + /// make sure the value is converted into an array. throw otherwise. QJsonArray ensureArray(const QJsonValue val, QString what = "value"); diff --git a/logic/OneSixLibrary.h b/logic/OneSixLibrary.h index 371ca6f4..3bd21c51 100644 --- a/logic/OneSixLibrary.h +++ b/logic/OneSixLibrary.h @@ -26,6 +26,9 @@ class Rule; +class OneSixLibrary; +typedef std::shared_ptr OneSixLibraryPtr; + class OneSixLibrary { private: diff --git a/logic/OneSixVersionBuilder.cpp b/logic/OneSixVersionBuilder.cpp index 0c63dc2a..a6bc5ec7 100644 --- a/logic/OneSixVersionBuilder.cpp +++ b/logic/OneSixVersionBuilder.cpp @@ -65,23 +65,23 @@ void OneSixVersionBuilder::buildInternal(const bool onlyVanilla, const QStringLi if(!external.isEmpty()) for (auto fileName : external) { QLOG_INFO() << "Reading" << fileName; - VersionFile file = parseJsonFile(QFileInfo(fileName), false, fileName.endsWith("pack.json")); - file.name = QFileInfo(fileName).fileName(); - file.fileId = "org.multimc.external." + file.name; - file.version = QString(); - file.mcVersion = QString(); - file.applyTo(m_version); + auto file = parseJsonFile(QFileInfo(fileName), false, fileName.endsWith("pack.json")); + file->name = QFileInfo(fileName).fileName(); + file->fileId = "org.multimc.external." + file->name; + file->version = QString(); + file->mcVersion = QString(); + file->applyTo(m_version); } // 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 = parseJsonFile(QFileInfo(root.absoluteFilePath("custom.json")), false); - file.name = "custom.json"; - file.filename = "custom.json"; - file.fileId = "org.multimc.custom.json"; - file.version = QString(); - file.applyTo(m_version); + auto file = parseJsonFile(QFileInfo(root.absoluteFilePath("custom.json")), false); + file->name = "custom.json"; + file->filename = "custom.json"; + file->fileId = "org.multimc.custom.json"; + file->version = QString(); + file->applyTo(m_version); // 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.") } @@ -90,12 +90,12 @@ void OneSixVersionBuilder::buildInternal(const bool onlyVanilla, const QStringLi { // version.json QLOG_INFO() << "Reading version.json"; - VersionFile file = parseJsonFile(QFileInfo(root.absoluteFilePath("version.json")), false); - file.name = "Minecraft"; - file.fileId = "org.multimc.version.json"; - file.version = m_instance->intendedVersionId(); - file.mcVersion = m_instance->intendedVersionId(); - file.applyTo(m_version); + auto file = parseJsonFile(QFileInfo(root.absoluteFilePath("version.json")), false); + file->name = "Minecraft"; + file->fileId = "org.multimc.version.json"; + file->version = m_instance->intendedVersionId(); + file->mcVersion = m_instance->intendedVersionId(); + file->applyTo(m_version); // QObject::tr("Error while applying %1. Please check MultiMC-0.log for more info.").arg(root.absoluteFilePath("version.json"))); if (onlyVanilla) @@ -105,26 +105,26 @@ void OneSixVersionBuilder::buildInternal(const bool onlyVanilla, const QStringLi // load all, put into map for ordering, apply in the right order QMap overrideOrder = readOverrideOrders(m_instance); - QMap> files; + QMap> files; for (auto info : patches.entryInfoList(QStringList() << "*.json", QDir::Files)) { QLOG_INFO() << "Reading" << info.fileName(); - VersionFile file = parseJsonFile(info, true); - if (overrideOrder.contains(file.fileId)) + auto file = parseJsonFile(info, true); + if (overrideOrder.contains(file->fileId)) { - file.order = overrideOrder.value(file.fileId); + file->order = overrideOrder.value(file->fileId); } - if (files.contains(file.order)) + if (files.contains(file->order)) { - throw VersionBuildError(QObject::tr("%1 has the same order as %2").arg(file.fileId, files[file.order].second.fileId)); + throw VersionBuildError(QObject::tr("%1 has the same order as %2").arg(file->fileId, files[file->order].second->fileId)); } - files.insert(file.order, qMakePair(info.fileName(), file)); + files.insert(file->order, qMakePair(info.fileName(), file)); } for (auto order : files.keys()) { QLOG_DEBUG() << "Applying file with order" << order; auto & filePair = files[order]; - filePair.second.applyTo(m_version); + filePair.second->applyTo(m_version); } } while(0); @@ -163,15 +163,15 @@ void OneSixVersionBuilder::readJsonAndApply(const QJsonObject &obj) { m_version->clear(); - VersionFile file = VersionFile::fromJson(QJsonDocument(obj), QString(), false); + auto file = VersionFile::fromJson(QJsonDocument(obj), QString(), false); // QObject::tr("Error while reading. Please check MultiMC-0.log for more info.")); - file.applyTo(m_version); + 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")); } -VersionFile OneSixVersionBuilder::parseJsonFile(const QFileInfo &fileInfo, const bool requireOrder, bool isFTB) +VersionFilePtr OneSixVersionBuilder::parseJsonFile(const QFileInfo &fileInfo, const bool requireOrder, bool isFTB) { QFile file(fileInfo.absoluteFilePath()); if (!file.open(QFile::ReadOnly)) @@ -193,39 +193,50 @@ VersionFile OneSixVersionBuilder::parseJsonFile(const QFileInfo &fileInfo, const QMap OneSixVersionBuilder::readOverrideOrders(OneSixInstance *instance) { QMap out; - if (QDir(instance->instanceRoot()).exists("order.json")) + + // 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)) { - 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 + QLOG_ERROR() << "Couldn't open" << orderFile.fileName() + << " for reading:" << orderFile.errorString(); + QLOG_WARN() << "Ignoring overriden order"; + return out; + } + + // and it's valid JSON + QJsonParseError error; + QJsonDocument doc = QJsonDocument::fromJson(orderFile.readAll(), &error); + if (error.error != QJsonParseError::NoError ) + { + QLOG_ERROR() << "Couldn't parse" << orderFile.fileName() << ":" << error.errorString(); + 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) { - QJsonParseError error; - QJsonDocument doc = QJsonDocument::fromJson(orderFile.readAll(), &error); - if (error.error != QJsonParseError::NoError || !doc.isObject()) + if (it.key().startsWith("org.multimc.")) { - 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()); - } + continue; } + out.insert(it.key(), MMCJson::ensureInteger(it.value())); } } + catch (JSONValidationError err) + { + QLOG_ERROR() << "Couldn't parse" << orderFile.fileName() << ": bad file format"; + QLOG_WARN() << "Ignoring overriden order"; + return out; + } return out; } diff --git a/logic/OneSixVersionBuilder.h b/logic/OneSixVersionBuilder.h index c48e8ec5..8be3d9d3 100644 --- a/logic/OneSixVersionBuilder.h +++ b/logic/OneSixVersionBuilder.h @@ -43,5 +43,6 @@ private: void readJsonAndApply(const QJsonObject &obj); void finalizeVersion(); - VersionFile parseJsonFile(const QFileInfo &fileInfo, const bool requireOrder, bool isFTB = false); + VersionFilePtr parseJsonFile(const QFileInfo &fileInfo, const bool requireOrder, + bool isFTB = false); }; diff --git a/logic/VersionFile.cpp b/logic/VersionFile.cpp index b7695907..40dcb0c3 100644 --- a/logic/VersionFile.cpp +++ b/logic/VersionFile.cpp @@ -13,16 +13,15 @@ using namespace MMCJson; #define CURRENT_MINIMUM_LAUNCHER_VERSION 14 -RawLibrary RawLibrary::fromJson(const QJsonObject &libObj, - const QString &filename) +RawLibraryPtr RawLibrary::fromJson(const QJsonObject &libObj, const QString &filename) { - RawLibrary out; + RawLibraryPtr out(new RawLibrary()); if (!libObj.contains("name")) { throw JSONValidationError(filename + "contains a library that doesn't have a 'name' field"); } - out.name = libObj.value("name").toString(); + out->name = libObj.value("name").toString(); auto readString = [libObj, filename](const QString & key, QString & variable) { @@ -40,22 +39,22 @@ RawLibrary RawLibrary::fromJson(const QJsonObject &libObj, } }; - readString("url", out.url); - readString("MMC-hint", out.hint); - readString("MMC-absulute_url", out.absoluteUrl); - readString("MMC-absoluteUrl", out.absoluteUrl); + readString("url", out->url); + readString("MMC-hint", out->hint); + readString("MMC-absulute_url", out->absoluteUrl); + readString("MMC-absoluteUrl", out->absoluteUrl); if (libObj.contains("extract")) { - out.applyExcludes = true; + out->applyExcludes = true; auto extractObj = ensureObject(libObj.value("extract")); for (auto excludeVal : ensureArray(extractObj.value("exclude"))) { - out.excludes.append(ensureString(excludeVal)); + out->excludes.append(ensureString(excludeVal)); } } if (libObj.contains("natives")) { - out.applyNatives = true; + out->applyNatives = true; QJsonObject nativesObj = ensureObject(libObj.value("natives")); for (auto it = nativesObj.begin(); it != nativesObj.end(); ++it) { @@ -66,22 +65,22 @@ RawLibrary RawLibrary::fromJson(const QJsonObject &libObj, OpSys opSys = OpSys_fromString(it.key()); if (opSys != Os_Other) { - out.natives.append(qMakePair(opSys, it.value().toString())); + out->natives.append(qMakePair(opSys, it.value().toString())); } } } if (libObj.contains("rules")) { - out.applyRules = true; - out.rules = rulesFromJsonV4(libObj); + out->applyRules = true; + out->rules = rulesFromJsonV4(libObj); } return out; } -VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filename, +VersionFilePtr VersionFile::fromJson(const QJsonDocument &doc, const QString &filename, const bool requireOrder, const bool isFTB) { - VersionFile out; + VersionFilePtr out(new VersionFile()); if (doc.isEmpty() || doc.isNull()) { throw JSONValidationError(filename + " is empty or null"); @@ -97,7 +96,7 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen { if (root.contains("order")) { - out.order = ensureInteger(root.value("order")); + out->order = ensureInteger(root.value("order")); } else { @@ -106,11 +105,11 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen } } - out.name = root.value("name").toString(); - out.fileId = root.value("fileId").toString(); - out.version = root.value("version").toString(); - out.mcVersion = root.value("mcVersion").toString(); - out.filename = filename; + out->name = root.value("name").toString(); + out->fileId = root.value("fileId").toString(); + out->version = root.value("version").toString(); + out->mcVersion = root.value("mcVersion").toString(); + out->filename = filename; auto readString = [root, filename](const QString & key, QString & variable) { @@ -123,30 +122,30 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen // FIXME: This should be ignored when applying. if (!isFTB) { - readString("id", out.id); + readString("id", out->id); } - readString("mainClass", out.mainClass); - readString("processArguments", out.processArguments); - readString("minecraftArguments", out.overwriteMinecraftArguments); - readString("+minecraftArguments", out.addMinecraftArguments); - readString("-minecraftArguments", out.removeMinecraftArguments); - readString("type", out.type); - readString("releaseTime", out.releaseTime); - readString("time", out.time); - readString("assets", out.assets); + readString("mainClass", out->mainClass); + readString("processArguments", out->processArguments); + readString("minecraftArguments", out->overwriteMinecraftArguments); + readString("+minecraftArguments", out->addMinecraftArguments); + readString("-minecraftArguments", out->removeMinecraftArguments); + readString("type", out->type); + readString("releaseTime", out->releaseTime); + readString("time", out->time); + readString("assets", out->assets); if (root.contains("minimumLauncherVersion")) { - out.minimumLauncherVersion = ensureInteger(root.value("minimumLauncherVersion")); + out->minimumLauncherVersion = ensureInteger(root.value("minimumLauncherVersion")); } if (root.contains("tweakers")) { - out.shouldOverwriteTweakers = true; + out->shouldOverwriteTweakers = true; for (auto tweakerVal : ensureArray(root.value("tweakers"))) { - out.overwriteTweakers.append(ensureString(tweakerVal)); + out->overwriteTweakers.append(ensureString(tweakerVal)); } } @@ -154,7 +153,7 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen { for (auto tweakerVal : ensureArray(root.value("+tweakers"))) { - out.addTweakers.append(ensureString(tweakerVal)); + out->addTweakers.append(ensureString(tweakerVal)); } } @@ -162,29 +161,29 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen { for (auto tweakerVal : ensureArray(root.value("-tweakers"))) { - out.removeTweakers.append(ensureString(tweakerVal)); + out->removeTweakers.append(ensureString(tweakerVal)); } } if (root.contains("libraries")) { // FIXME: This should be done when applying. - out.shouldOverwriteLibs = !isFTB; + out->shouldOverwriteLibs = !isFTB; for (auto libVal : ensureArray(root.value("libraries"))) { auto libObj = ensureObject(libVal); - RawLibrary lib = RawLibrary::fromJson(libObj, filename); + auto lib = RawLibrary::fromJson(libObj, filename); // FIXME: This should be done when applying. if (isFTB) { - lib.hint = "local"; - lib.insertType = RawLibrary::Prepend; - out.addLibs.prepend(lib); + lib->hint = "local"; + lib->insertType = RawLibrary::Prepend; + out->addLibs.prepend(lib); } else { - out.overwriteLibs.append(lib); + out->overwriteLibs.append(lib); } } } @@ -197,7 +196,7 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen QJsonValue insertVal = ensureExists(libObj.value("insert")); // parse the library - RawLibrary lib = RawLibrary::fromJson(libObj, filename); + auto lib = RawLibrary::fromJson(libObj, filename); // TODO: utility functions for handling this case. templates? QString insertString; @@ -215,24 +214,24 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen filename); } insertString = insertObj.keys().first(); - lib.insertData = insertObj.value(insertString).toString(); + lib->insertData = insertObj.value(insertString).toString(); } } if (insertString == "apply") { - lib.insertType = RawLibrary::Apply; + lib->insertType = RawLibrary::Apply; } else if (insertString == "prepend") { - lib.insertType = RawLibrary::Prepend; + lib->insertType = RawLibrary::Prepend; } else if (insertString == "append") { - lib.insertType = RawLibrary::Prepend; + lib->insertType = RawLibrary::Prepend; } else if (insertString == "replace") { - lib.insertType = RawLibrary::Replace; + lib->insertType = RawLibrary::Replace; } else { @@ -244,11 +243,11 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen const QString dependString = ensureString(libObj.value("MMC-depend")); if (dependString == "hard") { - lib.dependType = RawLibrary::Hard; + lib->dependType = RawLibrary::Hard; } else if (dependString == "soft") { - lib.dependType = RawLibrary::Soft; + lib->dependType = RawLibrary::Soft; } else { @@ -256,7 +255,7 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen " contains an invalid depend type"); } } - out.addLibs.append(lib); + out->addLibs.append(lib); } } if (root.contains("-libraries")) @@ -264,37 +263,36 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen for (auto libVal : ensureArray(root.value("-libraries"))) { auto libObj = ensureObject(libVal); - out.removeLibs.append(ensureString(libObj.value("name"))); + out->removeLibs.append(ensureString(libObj.value("name"))); } } return out; } -std::shared_ptr VersionFile::createLibrary(const RawLibrary &lib) +OneSixLibraryPtr VersionFile::createLibrary(RawLibraryPtr lib) { - std::shared_ptr out(new OneSixLibrary(lib.name)); - if (!lib.url.isEmpty()) + std::shared_ptr out(new OneSixLibrary(lib->name)); + if (!lib->url.isEmpty()) { - out->setBaseUrl(lib.url); + out->setBaseUrl(lib->url); } - out->setHint(lib.hint); - if (!lib.absoluteUrl.isEmpty()) + out->setHint(lib->hint); + if (!lib->absoluteUrl.isEmpty()) { - out->setAbsoluteUrl(lib.absoluteUrl); + out->setAbsoluteUrl(lib->absoluteUrl); } - out->setAbsoluteUrl(lib.absoluteUrl); - out->extract_excludes = lib.excludes; - for (auto native : lib.natives) + out->setAbsoluteUrl(lib->absoluteUrl); + out->extract_excludes = lib->excludes; + for (auto native : lib->natives) { out->addNative(native.first, native.second); } - out->setRules(lib.rules); + out->setRules(lib->rules); out->finalize(); return out; } -int VersionFile::findLibrary(QList> haystack, - const QString &needle) +int VersionFile::findLibrary(QList haystack, const QString &needle) { for (int i = 0; i < haystack.size(); ++i) { @@ -395,48 +393,48 @@ void VersionFile::applyTo(VersionFinal *version) } for (auto lib : addLibs) { - switch (lib.insertType) + switch (lib->insertType) { case RawLibrary::Apply: { - int index = findLibrary(version->libraries, lib.name); + int index = findLibrary(version->libraries, lib->name); if (index >= 0) { auto library = version->libraries[index]; - if (!lib.url.isNull()) + if (!lib->url.isNull()) { - library->setBaseUrl(lib.url); + library->setBaseUrl(lib->url); } - if (!lib.hint.isNull()) + if (!lib->hint.isNull()) { - library->setHint(lib.hint); + library->setHint(lib->hint); } - if (!lib.absoluteUrl.isNull()) + if (!lib->absoluteUrl.isNull()) { - library->setAbsoluteUrl(lib.absoluteUrl); + library->setAbsoluteUrl(lib->absoluteUrl); } - if (lib.applyExcludes) + if (lib->applyExcludes) { - library->extract_excludes = lib.excludes; + library->extract_excludes = lib->excludes; } - if (lib.applyNatives) + if (lib->applyNatives) { library->clearSuffixes(); - for (auto native : lib.natives) + for (auto native : lib->natives) { library->addNative(native.first, native.second); } } - if (lib.applyRules) + if (lib->applyRules) { - library->setRules(lib.rules); + library->setRules(lib->rules); } library->finalize(); } else { - QLOG_WARN() << "Couldn't find" << lib.name << "(skipping)"; + QLOG_WARN() << "Couldn't find" << lib->name << "(skipping)"; } break; } @@ -444,12 +442,12 @@ void VersionFile::applyTo(VersionFinal *version) case RawLibrary::Prepend: { - const int startOfVersion = lib.name.lastIndexOf(':') + 1; + const int startOfVersion = lib->name.lastIndexOf(':') + 1; const int index = findLibrary( - version->libraries, QString(lib.name).replace(startOfVersion, INT_MAX, '*')); + version->libraries, QString(lib->name).replace(startOfVersion, INT_MAX, '*')); if (index < 0) { - if (lib.insertType == RawLibrary::Append) + if (lib->insertType == RawLibrary::Append) { version->libraries.append(createLibrary(lib)); } @@ -461,7 +459,7 @@ void VersionFile::applyTo(VersionFinal *version) else { auto otherLib = version->libraries.at(index); - const Util::Version ourVersion = lib.name.mid(startOfVersion, INT_MAX); + const Util::Version ourVersion = lib->name.mid(startOfVersion, INT_MAX); const Util::Version otherVersion = otherLib->version(); // if the existing version is a hard dependency we can either use it or // fail, but we can't change it @@ -470,12 +468,12 @@ void VersionFile::applyTo(VersionFinal *version) // we need a higher version, or we're hard to and the versions aren't // equal if (ourVersion > otherVersion || - (lib.dependType == RawLibrary::Hard && ourVersion != otherVersion)) + (lib->dependType == RawLibrary::Hard && ourVersion != otherVersion)) { throw VersionBuildError( QString( "Error resolving library dependencies between %1 and %2 in %3.") - .arg(otherLib->rawName(), lib.name, filename)); + .arg(otherLib->rawName(), lib->name, filename)); } else { @@ -498,11 +496,11 @@ void VersionFile::applyTo(VersionFinal *version) { // our version is smaller than the existing version, but we require // it: fail - if (lib.dependType == RawLibrary::Hard) + if (lib->dependType == RawLibrary::Hard) { throw VersionBuildError(QString( "Error resolving library dependencies between %1 and %2 in %3.") - .arg(otherLib->rawName(), lib.name, + .arg(otherLib->rawName(), lib->name, filename)); } } @@ -512,14 +510,14 @@ void VersionFile::applyTo(VersionFinal *version) } case RawLibrary::Replace: { - int index = findLibrary(version->libraries, lib.insertData); + int index = findLibrary(version->libraries, lib->insertData); if (index >= 0) { version->libraries.replace(index, createLibrary(lib)); } else { - QLOG_WARN() << "Couldn't find" << lib.insertData << "(skipping)"; + QLOG_WARN() << "Couldn't find" << lib->insertData << "(skipping)"; } break; } @@ -537,13 +535,4 @@ void VersionFile::applyTo(VersionFinal *version) QLOG_WARN() << "Couldn't find" << lib << "(skipping)"; } } - - VersionFinal::VersionFile versionFile; - versionFile.name = name; - versionFile.id = fileId; - versionFile.version = this->version; - versionFile.mcVersion = mcVersion; - versionFile.filename = filename; - versionFile.order = order; - version->versionFiles.append(versionFile); } diff --git a/logic/VersionFile.h b/logic/VersionFile.h index 0475f927..504fcbff 100644 --- a/logic/VersionFile.h +++ b/logic/VersionFile.h @@ -20,6 +20,8 @@ public: virtual ~VersionBuildError() {}; }; +struct RawLibrary; +typedef std::shared_ptr RawLibraryPtr; struct RawLibrary { QString name; @@ -50,17 +52,19 @@ struct RawLibrary }; DependType dependType = Soft; - static RawLibrary fromJson(const QJsonObject &libObj, const QString &filename); + static RawLibraryPtr fromJson(const QJsonObject &libObj, const QString &filename); }; +struct VersionFile; +typedef std::shared_ptr VersionFilePtr; struct VersionFile { public: /* methods */ - static VersionFile fromJson(const QJsonDocument &doc, const QString &filename, + static VersionFilePtr fromJson(const QJsonDocument &doc, const QString &filename, const bool requireOrder, const bool isFTB = false); - static std::shared_ptr createLibrary(const RawLibrary &lib); - int findLibrary(QList> haystack, const QString &needle); + static OneSixLibraryPtr createLibrary(RawLibraryPtr lib); + int findLibrary(QList haystack, const QString &needle); void applyTo(VersionFinal *version); public: /* data */ @@ -91,7 +95,7 @@ public: /* data */ QStringList removeTweakers; bool shouldOverwriteLibs = false; - QList overwriteLibs; - QList addLibs; + QList overwriteLibs; + QList addLibs; QList removeLibs; }; \ No newline at end of file diff --git a/logic/VersionFinal.cpp b/logic/VersionFinal.cpp index ec450eda..8d668014 100644 --- a/logic/VersionFinal.cpp +++ b/logic/VersionFinal.cpp @@ -52,26 +52,6 @@ void VersionFinal::clear() endResetModel(); } -void VersionFinal::dump() const -{ - qDebug().nospace() << "VersionFinal(" - << "\n\tid=" << id - << "\n\ttime=" << time - << "\n\treleaseTime=" << releaseTime - << "\n\ttype=" << type - << "\n\tassets=" << assets - << "\n\tprocessArguments=" << processArguments - << "\n\tminecraftArguments=" << minecraftArguments - << "\n\tminimumLauncherVersion=" << minimumLauncherVersion - << "\n\tmainClass=" << mainClass - << "\n\tlibraries="; - for (auto lib : libraries) - { - qDebug().nospace() << "\n\t\t" << lib.get(); - } - qDebug().nospace() << "\n)"; -} - bool VersionFinal::canRemove(const int index) const { if (index < versionFiles.size()) @@ -201,25 +181,3 @@ int VersionFinal::columnCount(const QModelIndex &parent) const { return 2; } - -QDebug operator<<(QDebug &dbg, const VersionFinal *version) -{ - version->dump(); - return dbg.maybeSpace(); -} -QDebug operator<<(QDebug &dbg, const OneSixLibrary *library) -{ - dbg.nospace() << "OneSixLibrary(" - << "\n\t\t\trawName=" << library->rawName() - << "\n\t\t\tname=" << library->name() - << "\n\t\t\tversion=" << library->version() - << "\n\t\t\ttype=" << library->type() - << "\n\t\t\tisActive=" << library->isActive() - << "\n\t\t\tisNative=" << library->isNative() - << "\n\t\t\tdownloadUrl=" << library->downloadUrl() - << "\n\t\t\tstoragePath=" << library->storagePath() - << "\n\t\t\tabsolutePath=" << library->absoluteUrl() - << "\n\t\t\thint=" << library->hint(); - dbg.nospace() << "\n\t\t)"; - return dbg.maybeSpace(); -} diff --git a/logic/VersionFinal.h b/logic/VersionFinal.h index e5a38423..b19cd6f9 100644 --- a/logic/VersionFinal.h +++ b/logic/VersionFinal.h @@ -22,6 +22,7 @@ #include #include "OneSixLibrary.h" +#include "VersionFile.h" class OneSixInstance; @@ -118,15 +119,6 @@ public: */ // QList rules; - struct VersionFile - { - QString name; - QString id; - QString version; - QString mcVersion; - QString filename; - int order; - }; QList versionFiles; private: -- cgit From b2c803a378695026f12aabc3729eb2139bee1b2c Mon Sep 17 00:00:00 2001 From: Petr Mrázek Date: Sun, 9 Mar 2014 23:42:25 +0100 Subject: Improve reporting of version file errors.x --- MMCError.h | 8 +-- gui/dialogs/OneSixModEditDialog.cpp | 60 ++++++++++++----- gui/dialogs/OneSixModEditDialog.h | 4 +- logic/MMCJson.h | 4 -- logic/OneSixFTBInstance.cpp | 9 ++- logic/OneSixInstance.cpp | 17 +++-- logic/OneSixInstance.h | 8 ++- logic/OneSixUpdate.cpp | 40 ++++++----- logic/OneSixVersionBuilder.cpp | 129 +++++++++++++++++++----------------- logic/VersionFile.cpp | 11 ++- logic/VersionFile.h | 36 ++++++++-- logic/VersionFinal.cpp | 2 +- 12 files changed, 202 insertions(+), 126 deletions(-) (limited to 'logic/MMCJson.h') diff --git a/MMCError.h b/MMCError.h index 33591e06..7b2bd0c4 100644 --- a/MMCError.h +++ b/MMCError.h @@ -9,7 +9,7 @@ public: MMCError(QString cause) { exceptionCause = cause; - QLOG_ERROR() << errorName() + ": " + cause; + QLOG_ERROR() << "Exception: " + cause; }; virtual ~MMCError(){}; virtual const char *what() const noexcept @@ -20,10 +20,6 @@ public: { return exceptionCause; } - virtual QString errorName() - { - return "MultiMC Error"; - } private: QString exceptionCause; -}; \ No newline at end of file +}; diff --git a/gui/dialogs/OneSixModEditDialog.cpp b/gui/dialogs/OneSixModEditDialog.cpp index c211d3e1..78585a05 100644 --- a/gui/dialogs/OneSixModEditDialog.cpp +++ b/gui/dialogs/OneSixModEditDialog.cpp @@ -42,8 +42,7 @@ #include "logic/LiteLoaderInstaller.h" #include "logic/OneSixVersionBuilder.h" -template -QMap invert(const QMap &in) +template QMap invert(const QMap &in) { QMap out; for (auto it = in.begin(); it != in.end(); ++it) @@ -96,7 +95,8 @@ OneSixModEditDialog::OneSixModEditDialog(OneSixInstance *inst, QWidget *parent) m_resourcepacks->startWatching(); } - connect(m_inst, &OneSixInstance::versionReloaded, this, &OneSixModEditDialog::updateVersionControls); + connect(m_inst, &OneSixInstance::versionReloaded, this, + &OneSixModEditDialog::updateVersionControls); } OneSixModEditDialog::~OneSixModEditDialog() @@ -120,9 +120,30 @@ void OneSixModEditDialog::disableVersionControls() ui->removeLibraryBtn->setEnabled(false); } +bool OneSixModEditDialog::reloadInstanceVersion() +{ + try + { + m_inst->reloadVersion(); + return true; + } + catch (MMCError &e) + { + QMessageBox::critical(this, tr("Error"), e.cause()); + return false; + } + catch (...) + { + QMessageBox::critical( + this, tr("Error"), + tr("Failed to load the version description file for reasons unknown.")); + return false; + } +} + void OneSixModEditDialog::on_reloadLibrariesBtn_clicked() { - m_inst->reloadVersion(); + reloadInstanceVersion(); } void OneSixModEditDialog::on_removeLibraryBtn_clicked() @@ -136,7 +157,7 @@ void OneSixModEditDialog::on_removeLibraryBtn_clicked() } else { - m_inst->reloadVersion(); + reloadInstanceVersion(); } } } @@ -163,18 +184,20 @@ void OneSixModEditDialog::on_forgeBtn_clicked() // FIXME: model::isCustom(); if (QDir(m_inst->instanceRoot()).exists("custom.json")) { - if (QMessageBox::question(this, tr("Revert?"), tr("This action will remove your custom.json. Continue?")) != QMessageBox::Yes) + if (QMessageBox::question(this, tr("Revert?"), + tr("This action will remove your custom.json. Continue?")) != + QMessageBox::Yes) { return; } // FIXME: model::revertToBase(); QDir(m_inst->instanceRoot()).remove("custom.json"); - m_inst->reloadVersion(); + reloadInstanceVersion(); } VersionSelectDialog vselect(MMC->forgelist().get(), tr("Select Forge version"), this); vselect.setFilter(1, m_inst->currentVersionId()); vselect.setEmptyString(tr("No Forge versions are currently available for Minecraft ") + - m_inst->currentVersionId()); + m_inst->currentVersionId()); if (vselect.exec() && vselect.selectedVersion()) { ForgeVersionPtr forgeVersion = @@ -214,28 +237,32 @@ void OneSixModEditDialog::on_forgeBtn_clicked() } } } - m_inst->reloadVersion(); + reloadInstanceVersion(); } void OneSixModEditDialog::on_liteloaderBtn_clicked() { + // FIXME: model... if (QDir(m_inst->instanceRoot()).exists("custom.json")) { - if (QMessageBox::question(this, tr("Revert?"), tr("This action will remove your custom.json. Continue?")) != QMessageBox::Yes) + if (QMessageBox::question(this, tr("Revert?"), + tr("This action will remove your custom.json. Continue?")) != + QMessageBox::Yes) { return; } QDir(m_inst->instanceRoot()).remove("custom.json"); - m_inst->reloadVersion(); + reloadInstanceVersion(); } - VersionSelectDialog vselect(MMC->liteloaderlist().get(), tr("Select LiteLoader version"), this); + VersionSelectDialog vselect(MMC->liteloaderlist().get(), tr("Select LiteLoader version"), + this); vselect.setFilter(1, m_inst->currentVersionId()); vselect.setEmptyString(tr("No LiteLoader versions are currently available for Minecraft ") + - m_inst->currentVersionId()); + m_inst->currentVersionId()); if (vselect.exec() && vselect.selectedVersion()) { LiteLoaderVersionPtr liteloaderVersion = - std::dynamic_pointer_cast(vselect.selectedVersion()); + std::dynamic_pointer_cast(vselect.selectedVersion()); if (!liteloaderVersion) return; LiteLoaderInstaller liteloader(liteloaderVersion); @@ -247,7 +274,7 @@ void OneSixModEditDialog::on_liteloaderBtn_clicked() } else { - m_inst->reloadVersion(); + reloadInstanceVersion(); } } } @@ -369,7 +396,8 @@ void OneSixModEditDialog::loaderCurrent(QModelIndex current, QModelIndex previou ui->frame->updateWithMod(m); } -void OneSixModEditDialog::versionCurrent(const QModelIndex ¤t, const QModelIndex &previous) +void OneSixModEditDialog::versionCurrent(const QModelIndex ¤t, + const QModelIndex &previous) { if (!current.isValid()) { diff --git a/gui/dialogs/OneSixModEditDialog.h b/gui/dialogs/OneSixModEditDialog.h index 1f3f9f67..e106c6fe 100644 --- a/gui/dialogs/OneSixModEditDialog.h +++ b/gui/dialogs/OneSixModEditDialog.h @@ -57,6 +57,8 @@ protected: bool eventFilter(QObject *obj, QEvent *ev); bool loaderListFilter(QKeyEvent *ev); bool resourcePackListFilter(QKeyEvent *ev); + /// FIXME: this shouldn't be necessary! + bool reloadInstanceVersion(); private: Ui::OneSixModEditDialog *ui; @@ -66,8 +68,6 @@ private: EnabledItemFilter *main_model; OneSixInstance *m_inst; - QMap getExistingOrder() const; - public slots: void loaderCurrent(QModelIndex current, QModelIndex previous); diff --git a/logic/MMCJson.h b/logic/MMCJson.h index b0d898fc..f2cc4b31 100644 --- a/logic/MMCJson.h +++ b/logic/MMCJson.h @@ -15,10 +15,6 @@ class JSONValidationError : public MMCError { public: JSONValidationError(QString cause) : MMCError(cause) {}; - virtual QString errorName() - { - return "JSONValidationError"; - }; virtual ~JSONValidationError() {}; }; diff --git a/logic/OneSixFTBInstance.cpp b/logic/OneSixFTBInstance.cpp index cdb3f53e..8f70ed08 100644 --- a/logic/OneSixFTBInstance.cpp +++ b/logic/OneSixFTBInstance.cpp @@ -17,7 +17,14 @@ OneSixFTBInstance::OneSixFTBInstance(const QString &rootDir, SettingsObject *set void OneSixFTBInstance::init() { - reloadVersion(); + try + { + reloadVersion(); + } + catch(MMCError & e) + { + // QLOG_ERROR() << "Caught exception on instance init: " << e.cause(); + } } void OneSixFTBInstance::copy(const QDir &newDir) diff --git a/logic/OneSixInstance.cpp b/logic/OneSixInstance.cpp index f6fe49f1..3c1f6545 100644 --- a/logic/OneSixInstance.cpp +++ b/logic/OneSixInstance.cpp @@ -41,9 +41,17 @@ OneSixInstance::OneSixInstance(const QString &rootDir, SettingsObject *settings, void OneSixInstance::init() { + // FIXME: why is this decided here? what does this even mean? if (QDir(instanceRoot()).exists("version.json")) { - reloadVersion(); + try + { + reloadVersion(); + } + catch(MMCError & e) + { + // QLOG_ERROR() << "Caught exception on instance init: " << e.cause(); + } } else { @@ -317,7 +325,7 @@ QString OneSixInstance::currentVersionId() const return intendedVersionId(); } -bool OneSixInstance::reloadVersion() +void OneSixInstance::reloadVersion() { I_D(OneSixInstance); @@ -327,16 +335,15 @@ bool OneSixInstance::reloadVersion() d->vanillaVersion->reload(true, externalPatches()); setFlags(flags() & ~VersionBrokenFlag); emit versionReloaded(); - return true; } - catch(MMCError error) + catch(MMCError & error) { d->version->clear(); d->vanillaVersion->clear(); setFlags(flags() | VersionBrokenFlag); //TODO: rethrow to show some error message(s)? emit versionReloaded(); - return false; + throw; } } diff --git a/logic/OneSixInstance.h b/logic/OneSixInstance.h index c7ef2ee8..d2bc9b01 100644 --- a/logic/OneSixInstance.h +++ b/logic/OneSixInstance.h @@ -53,8 +53,12 @@ public: virtual QDialog *createModEditDialog(QWidget *parent) override; - /// reload the full version json files. return true on success! - bool reloadVersion(); + /** + * reload the full version json files. return true on success! + * + * throws various exceptions :3 + */ + void reloadVersion(); /// clears all version information in preparation for an update void clearVersion(); /// get the current full version info diff --git a/logic/OneSixUpdate.cpp b/logic/OneSixUpdate.cpp index 750aeabb..65f30cda 100644 --- a/logic/OneSixUpdate.cpp +++ b/logic/OneSixUpdate.cpp @@ -48,7 +48,7 @@ void OneSixUpdate::executeTask() QDir mcDir(m_inst->minecraftRoot()); if (!mcDir.exists() && !mcDir.mkpath(".")) { - emitFailed("Failed to create bin folder."); + emitFailed(tr("Failed to create folder for minecraft binaries.")); return; } @@ -60,7 +60,7 @@ void OneSixUpdate::executeTask() if (targetVersion == nullptr) { // don't do anything if it was invalid - emitFailed("The specified Minecraft version is invalid. Choose a different one."); + emitFailed(tr("The specified Minecraft version is invalid. Choose a different one.")); return; } versionFileStart(); @@ -108,20 +108,19 @@ void OneSixUpdate::versionFileFinished() QSaveFile vfile1(version1); if (!vfile1.open(QIODevice::Truncate | QIODevice::WriteOnly)) { - emitFailed("Can't open " + version1 + " for writing."); + emitFailed(tr("Can't open %1 for writing.").arg(version1)); return; } auto data = std::dynamic_pointer_cast(DlJob)->m_data; qint64 actual = 0; if ((actual = vfile1.write(data)) != data.size()) { - emitFailed("Failed to write into " + version1 + ". Written " + actual + " out of " + - data.size() + '.'); + emitFailed(tr("Failed to write into %1. Written %2 out of %3.").arg(version1).arg(actual).arg(data.size())); return; } if (!vfile1.commit()) { - emitFailed("Can't commit changes to " + version1); + emitFailed(tr("Can't commit changes to %1").arg(version1)); return; } } @@ -136,14 +135,13 @@ void OneSixUpdate::versionFileFinished() { finfo.remove(); } - inst->reloadVersion(); - + // NOTE: Version is reloaded in jarlibStart jarlibStart(); } void OneSixUpdate::versionFileFailed() { - emitFailed("Failed to download the version description. Try again."); + emitFailed(tr("Failed to download the version description. Try again.")); } void OneSixUpdate::assetIndexStart() @@ -180,7 +178,7 @@ void OneSixUpdate::assetIndexFinished() QString asset_fname = "assets/indexes/" + assetName + ".json"; if (!AssetsUtils::loadAssetsIndexJson(asset_fname, &index)) { - emitFailed("Failed to read the assets index!"); + emitFailed(tr("Failed to read the assets index!")); } QList dls; @@ -216,7 +214,7 @@ void OneSixUpdate::assetIndexFinished() void OneSixUpdate::assetIndexFailed() { - emitFailed("Failed to download the assets index!"); + emitFailed(tr("Failed to download the assets index!")); } void OneSixUpdate::assetsFinished() @@ -226,7 +224,7 @@ void OneSixUpdate::assetsFinished() void OneSixUpdate::assetsFailed() { - emitFailed("Failed to download assets!"); + emitFailed(tr("Failed to download assets!")); } void OneSixUpdate::jarlibStart() @@ -234,11 +232,18 @@ void OneSixUpdate::jarlibStart() setStatus(tr("Getting the library files from Mojang...")); QLOG_INFO() << m_inst->name() << ": downloading libraries"; OneSixInstance *inst = (OneSixInstance *)m_inst; - bool successful = inst->reloadVersion(); - if (!successful) + try + { + inst->reloadVersion(); + } + catch(MMCError & e) + { + emitFailed(e.cause()); + return; + } + catch(...) { - emitFailed("Failed to load the version description file. It might be " - "corrupted, missing or simply too new."); + emitFailed(tr("Failed to load the version description file for reasons unknown.")); return; } @@ -326,6 +331,5 @@ void OneSixUpdate::jarlibFailed() { QStringList failed = jarlibDownloadJob->getFailedFiles(); QString failed_all = failed.join("\n"); - emitFailed("Failed to download the following files:\n" + failed_all + - "\n\nPlease try again."); + emitFailed(tr("Failed to download the following files:\n%1\n\nPlease try again.").arg(failed_all)); } diff --git a/logic/OneSixVersionBuilder.cpp b/logic/OneSixVersionBuilder.cpp index 8abeb0d8..8eacbce4 100644 --- a/logic/OneSixVersionBuilder.cpp +++ b/logic/OneSixVersionBuilder.cpp @@ -38,7 +38,8 @@ OneSixVersionBuilder::OneSixVersionBuilder() { } -void OneSixVersionBuilder::build(VersionFinal *version, OneSixInstance *instance, const bool onlyVanilla, const QStringList &external) +void OneSixVersionBuilder::build(VersionFinal *version, OneSixInstance *instance, + const bool onlyVanilla, const QStringList &external) { OneSixVersionBuilder builder; builder.m_version = version; @@ -46,7 +47,8 @@ void OneSixVersionBuilder::build(VersionFinal *version, OneSixInstance *instance builder.buildInternal(onlyVanilla, external); } -void OneSixVersionBuilder::readJsonAndApplyToVersion(VersionFinal *version, const QJsonObject &obj) +void OneSixVersionBuilder::readJsonAndApplyToVersion(VersionFinal *version, + const QJsonObject &obj) { OneSixVersionBuilder builder; builder.m_version = version; @@ -62,17 +64,19 @@ void OneSixVersionBuilder::buildInternal(const bool onlyVanilla, const QStringLi QDir patches(root.absoluteFilePath("patches/")); // if we do external files, do just those. - if(!external.isEmpty()) for (auto fileName : external) - { - 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->version = QString(); - file->mcVersion = QString(); - file->applyTo(m_version); - m_version->versionFiles.append(file); - } + if (!external.isEmpty()) + for (auto fileName : external) + { + 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->version = QString(); + file->mcVersion = QString(); + file->applyTo(m_version); + m_version->versionFiles.append(file); + } // else, if there's custom json, we just do that. else if (QFile::exists(root.absoluteFilePath("custom.json"))) { @@ -84,48 +88,52 @@ void OneSixVersionBuilder::buildInternal(const bool onlyVanilla, const QStringLi file->version = QString(); file->applyTo(m_version); m_version->versionFiles.append(file); - // 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.") } // version.json -> patches/*.json -> user.json - else do - { - // version.json - QLOG_INFO() << "Reading version.json"; - auto file = parseJsonFile(QFileInfo(root.absoluteFilePath("version.json")), false); - file->name = "Minecraft"; - file->fileId = "org.multimc.version.json"; - file->version = m_instance->intendedVersionId(); - file->mcVersion = m_instance->intendedVersionId(); - file->applyTo(m_version); - m_version->versionFiles.append(file); - // QObject::tr("Error while applying %1. Please check MultiMC-0.log for more info.").arg(root.absoluteFilePath("version.json"))); + else + do + { + // version.json + QLOG_INFO() << "Reading version.json"; + auto file = parseJsonFile(QFileInfo(root.absoluteFilePath("version.json")), false); + file->name = "Minecraft"; + file->fileId = "org.multimc.version.json"; + file->version = m_instance->intendedVersionId(); + file->mcVersion = m_instance->intendedVersionId(); + file->applyTo(m_version); + m_version->versionFiles.append(file); + // QObject::tr("Error while applying %1. Please check MultiMC-0.log for more + // info.").arg(root.absoluteFilePath("version.json"))); - if (onlyVanilla) - break; + if (onlyVanilla) + break; - // patches/ - // load all, put into map for ordering, apply in the right order + // patches/ + // load all, put into map for ordering, apply in the right order - QMap> files; - for (auto info : patches.entryInfoList(QStringList() << "*.json", QDir::Files)) - { - QLOG_INFO() << "Reading" << info.fileName(); - auto file = parseJsonFile(info, true); - if (files.contains(file->order)) + QMap> files; + for (auto info : patches.entryInfoList(QStringList() << "*.json", QDir::Files)) { - throw VersionBuildError(QObject::tr("%1 has the same order as %2").arg(file->fileId, files[file->order].second->fileId)); + QLOG_INFO() << "Reading" << info.fileName(); + auto file = parseJsonFile(info, true); + if (files.contains(file->order)) + { + throw VersionBuildError(QObject::tr("%1 has the same order as %2").arg( + file->fileId, files[file->order].second->fileId)); + } + files.insert(file->order, qMakePair(info.fileName(), file)); } - files.insert(file->order, qMakePair(info.fileName(), file)); - } - for (auto order : files.keys()) - { - QLOG_DEBUG() << "Applying file with order" << order; - auto & filePair = files[order]; - filePair.second->applyTo(m_version); - m_version->versionFiles.append(filePair.second); - } - } while(0); + for (auto order : files.keys()) + { + QLOG_DEBUG() << "Applying file with order" << order; + auto &filePair = files[order]; + filePair.second->applyTo(m_version); + m_version->versionFiles.append(filePair.second); + } + } while (0); // some final touches finalizeVersion(); @@ -168,26 +176,30 @@ void OneSixVersionBuilder::readJsonAndApply(const QJsonObject &obj) file->applyTo(m_version); m_version->versionFiles.append(file); // 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")); + // QObject::tr("The version descriptors of this instance are not compatible with the current + // version of MultiMC")); } -VersionFilePtr OneSixVersionBuilder::parseJsonFile(const QFileInfo &fileInfo, const bool requireOrder, bool isFTB) +VersionFilePtr OneSixVersionBuilder::parseJsonFile(const QFileInfo &fileInfo, + const bool requireOrder, bool isFTB) { QFile file(fileInfo.absoluteFilePath()); if (!file.open(QFile::ReadOnly)) { - throw JSONValidationError(QObject::tr("Unable to open %1: %2").arg(file.fileName(), file.errorString())); + throw JSONValidationError(QObject::tr("Unable to open the version file %1: %2.") + .arg(fileInfo.fileName(), file.errorString())); } QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(file.readAll(), &error); if (error.error != QJsonParseError::NoError) { - throw JSONValidationError(QObject::tr("Unable to parse %1: %2 at %3") - .arg(file.fileName(), error.errorString()) - .arg(error.offset)); + throw JSONValidationError(QObject::tr("Unable to process the version file %1: %2 at %3.") + .arg(fileInfo.fileName(), error.errorString()) + .arg(error.offset)); } return VersionFile::fromJson(doc, file.fileName(), requireOrder, isFTB); - // QObject::tr("Error while reading %1. Please check MultiMC-0.log for more info.").arg(file.fileName()); + // QObject::tr("Error while reading %1. Please check MultiMC-0.log for more + // info.").arg(file.fileName()); } QMap OneSixVersionBuilder::readOverrideOrders(OneSixInstance *instance) @@ -203,7 +215,7 @@ QMap OneSixVersionBuilder::readOverrideOrders(OneSixInstance *inst if (!orderFile.open(QFile::ReadOnly)) { QLOG_ERROR() << "Couldn't open" << orderFile.fileName() - << " for reading:" << orderFile.errorString(); + << " for reading:" << orderFile.errorString(); QLOG_WARN() << "Ignoring overriden order"; return out; } @@ -211,9 +223,9 @@ QMap OneSixVersionBuilder::readOverrideOrders(OneSixInstance *inst // and it's valid JSON QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(orderFile.readAll(), &error); - if (error.error != QJsonParseError::NoError ) + if (error.error != QJsonParseError::NoError) { - QLOG_ERROR() << "Couldn't parse" << orderFile.fileName() << ":" << error.errorString(); + QLOG_ERROR() << "Couldn't parse" << orderFile.fileName() << ":" << error.errorString(); QLOG_WARN() << "Ignoring overriden order"; return out; } @@ -231,7 +243,7 @@ QMap OneSixVersionBuilder::readOverrideOrders(OneSixInstance *inst out.insert(it.key(), MMCJson::ensureInteger(it.value())); } } - catch (JSONValidationError err) + catch (JSONValidationError &err) { QLOG_ERROR() << "Couldn't parse" << orderFile.fileName() << ": bad file format"; QLOG_WARN() << "Ignoring overriden order"; @@ -262,4 +274,3 @@ bool OneSixVersionBuilder::writeOverrideOrders(const QMap &order, orderFile.write(QJsonDocument(obj).toJson(QJsonDocument::Indented)); return true; } - diff --git a/logic/VersionFile.cpp b/logic/VersionFile.cpp index 40dcb0c3..831b086e 100644 --- a/logic/VersionFile.cpp +++ b/logic/VersionFile.cpp @@ -311,9 +311,7 @@ void VersionFile::applyTo(VersionFinal *version) { if (minimumLauncherVersion > CURRENT_MINIMUM_LAUNCHER_VERSION) { - throw VersionBuildError( - QString("%1 is for a different launcher version (%2), current supported is %3") - .arg(filename, minimumLauncherVersion, CURRENT_MINIMUM_LAUNCHER_VERSION)); + throw LauncherVersionError(minimumLauncherVersion, CURRENT_MINIMUM_LAUNCHER_VERSION); } } @@ -322,8 +320,7 @@ void VersionFile::applyTo(VersionFinal *version) if (QRegExp(mcVersion, Qt::CaseInsensitive, QRegExp::Wildcard).indexIn(version->id) == -1) { - throw VersionBuildError( - QString("%1 is for a different version of Minecraft").arg(filename)); + throw MinecraftVersionMismatch(fileId, mcVersion, version->id); } } @@ -471,7 +468,7 @@ void VersionFile::applyTo(VersionFinal *version) (lib->dependType == RawLibrary::Hard && ourVersion != otherVersion)) { throw VersionBuildError( - QString( + QObject::tr( "Error resolving library dependencies between %1 and %2 in %3.") .arg(otherLib->rawName(), lib->name, filename)); } @@ -498,7 +495,7 @@ void VersionFile::applyTo(VersionFinal *version) // it: fail if (lib->dependType == RawLibrary::Hard) { - throw VersionBuildError(QString( + throw VersionBuildError(QObject::tr( "Error resolving library dependencies between %1 and %2 in %3.") .arg(otherLib->rawName(), lib->name, filename)); diff --git a/logic/VersionFile.h b/logic/VersionFile.h index 504fcbff..67d22b23 100644 --- a/logic/VersionFile.h +++ b/logic/VersionFile.h @@ -13,13 +13,39 @@ class VersionBuildError : public MMCError { public: VersionBuildError(QString cause) : MMCError(cause) {}; - virtual QString errorName() - { - return "VersionBuildError"; - }; virtual ~VersionBuildError() {}; }; +/** + * the base version file was meant for a newer version of the vanilla launcher than we support + */ +class LauncherVersionError : public VersionBuildError +{ +public: + LauncherVersionError(int actual, int supported) + : VersionBuildError(QObject::tr( + "The base version file of this instance was meant for a newer (%1) " + "version of the vanilla launcher than this version of MultiMC supports (%2).") + .arg(actual) + .arg(supported)) {}; + virtual ~LauncherVersionError() {}; +}; + +/** + * some patch was intended for a different version of minecraft + */ +class MinecraftVersionMismatch : public VersionBuildError +{ +public: + MinecraftVersionMismatch(QString fileId, QString mcVersion, QString parentMcVersion) + : VersionBuildError(QObject::tr("The patch %1 is for a different version of Minecraft " + "(%2) than that of the instance (%3).") + .arg(fileId) + .arg(mcVersion) + .arg(parentMcVersion)) {}; + virtual ~MinecraftVersionMismatch() {}; +}; + struct RawLibrary; typedef std::shared_ptr RawLibraryPtr; struct RawLibrary @@ -61,7 +87,7 @@ struct VersionFile { public: /* methods */ static VersionFilePtr fromJson(const QJsonDocument &doc, const QString &filename, - const bool requireOrder, const bool isFTB = false); + const bool requireOrder, const bool isFTB = false); static OneSixLibraryPtr createLibrary(RawLibraryPtr lib); int findLibrary(QList haystack, const QString &needle); diff --git a/logic/VersionFinal.cpp b/logic/VersionFinal.cpp index 48601d57..a057ecdd 100644 --- a/logic/VersionFinal.cpp +++ b/logic/VersionFinal.cpp @@ -112,7 +112,7 @@ std::shared_ptr VersionFinal::fromJson(const QJsonObject &obj) { OneSixVersionBuilder::readJsonAndApplyToVersion(version.get(), obj); } - catch(MMCError err) + catch(MMCError & err) { return 0; } -- cgit From d11f10ea1ed54336254838ff068258d2d42e0774 Mon Sep 17 00:00:00 2001 From: Jan Dalheimer Date: Mon, 10 Mar 2014 18:55:54 +0100 Subject: Fix a compiling error by adding noexcept --- MMCError.h | 2 +- logic/MMCJson.h | 2 +- logic/VersionFile.h | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) (limited to 'logic/MMCJson.h') diff --git a/MMCError.h b/MMCError.h index 7b2bd0c4..1f72b7a4 100644 --- a/MMCError.h +++ b/MMCError.h @@ -11,7 +11,7 @@ public: exceptionCause = cause; QLOG_ERROR() << "Exception: " + cause; }; - virtual ~MMCError(){}; + virtual ~MMCError() noexcept {} virtual const char *what() const noexcept { return exceptionCause.toLocal8Bit(); diff --git a/logic/MMCJson.h b/logic/MMCJson.h index f2cc4b31..71ded435 100644 --- a/logic/MMCJson.h +++ b/logic/MMCJson.h @@ -15,7 +15,7 @@ class JSONValidationError : public MMCError { public: JSONValidationError(QString cause) : MMCError(cause) {}; - virtual ~JSONValidationError() {}; + virtual ~JSONValidationError() noexcept {} }; namespace MMCJson diff --git a/logic/VersionFile.h b/logic/VersionFile.h index 67d22b23..169a2066 100644 --- a/logic/VersionFile.h +++ b/logic/VersionFile.h @@ -13,7 +13,7 @@ class VersionBuildError : public MMCError { public: VersionBuildError(QString cause) : MMCError(cause) {}; - virtual ~VersionBuildError() {}; + virtual ~VersionBuildError() noexcept {} }; /** @@ -28,7 +28,7 @@ public: "version of the vanilla launcher than this version of MultiMC supports (%2).") .arg(actual) .arg(supported)) {}; - virtual ~LauncherVersionError() {}; + virtual ~LauncherVersionError() noexcept {} }; /** @@ -43,7 +43,7 @@ public: .arg(fileId) .arg(mcVersion) .arg(parentMcVersion)) {}; - virtual ~MinecraftVersionMismatch() {}; + virtual ~MinecraftVersionMismatch() noexcept {} }; struct RawLibrary; @@ -124,4 +124,4 @@ public: /* data */ QList overwriteLibs; QList addLibs; QList removeLibs; -}; \ No newline at end of file +}; -- cgit