From 7c24bcc83476dcbdd7f7acbe14ecef4398962689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sat, 1 Mar 2014 23:06:47 +0100 Subject: Reorganize the version-related code. --- logic/VersionFile.cpp | 702 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 702 insertions(+) create mode 100644 logic/VersionFile.cpp (limited to 'logic/VersionFile.cpp') diff --git a/logic/VersionFile.cpp b/logic/VersionFile.cpp new file mode 100644 index 00000000..76cf9279 --- /dev/null +++ b/logic/VersionFile.cpp @@ -0,0 +1,702 @@ +#include +#include + +#include + +#include "logger/QsLog.h" +#include "logic/VersionFile.h" +#include "logic/OneSixLibrary.h" +#include "logic/VersionFinal.h" + + +#define CURRENT_MINIMUM_LAUNCHER_VERSION 14 + +VersionFile::Library VersionFile::Library::fromJson(const QJsonObject &libObj, + const QString &filename, bool &isError) +{ + isError = true; + Library out; + if (!libObj.contains("name")) + { + QLOG_ERROR() << filename << "contains a library that doesn't have a 'name' field"; + return out; + } + out.name = libObj.value("name").toString(); + + auto readString = [libObj, filename](const QString & key, QString & variable) + { + if (libObj.contains(key)) + { + QJsonValue val = libObj.value(key); + if (!val.isString()) + { + QLOG_WARN() << key << "is not a string in" << filename << "(skipping)"; + } + else + { + variable = val.toString(); + } + } + }; + + readString("url", out.url); + readString("MMC-hint", out.hint); + readString("MMC-absulute_url", out.absoluteUrl); + readString("MMC-absoluteUrl", out.absoluteUrl); + 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; + QJsonArray excludeArray = extractObj.value("exclude").toArray(); + for (auto excludeVal : excludeArray) + { + if (!excludeVal.isString()) + { + 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.value("natives").isObject()) + { + QLOG_ERROR() << filename + << "contains a library with a 'natives' field that's not an object"; + return out; + } + out.applyNatives = true; + QJsonObject nativesObj = libObj.value("natives").toObject(); + for (auto it = nativesObj.begin(); it != nativesObj.end(); ++it) + { + if (!it.value().isString()) + { + QLOG_WARN() << filename << "contains an invalid native (skipping)"; + } + OpSys opSys = OpSys_fromString(it.key()); + if (opSys != Os_Other) + { + out.natives.append(qMakePair(opSys, it.value().toString())); + } + } + } + if (libObj.contains("rules")) + { + out.applyRules = true; + out.rules = rulesFromJsonV4(libObj); + } + isError = false; + return out; +} + +VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filename, + const bool requireOrder, bool &isError, const bool isFTB) +{ + VersionFile out; + isError = true; + if (doc.isEmpty() || doc.isNull()) + { + QLOG_ERROR() << filename << "is empty or null"; + return out; + } + if (!doc.isObject()) + { + QLOG_ERROR() << "The root of" << filename << "is not an object"; + return out; + } + + QJsonObject root = doc.object(); + + if (requireOrder) + { + if (root.contains("order")) + { + if (root.value("order").isDouble()) + { + out.order = root.value("order").toDouble(); + } + else + { + QLOG_ERROR() << "'order' field contains an invalid value in" << filename; + return out; + } + } + else + { + QLOG_ERROR() << filename << "doesn't contain an order field"; + } + } + + 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) + { + if (root.contains(key)) + { + QJsonValue val = 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. + if (!isFTB) + { + 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); + if (root.contains("minimumLauncherVersion")) + { + QJsonValue val = 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")) + { + 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; + 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.overwriteTweakers.append(tweakerVal.toString()); + } + } + 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; + } + 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")) + { + 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; + 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")) + { + out.shouldOverwriteLibs = !isFTB; + QJsonValue librariesVal = root.value("libraries"); + if (!librariesVal.isArray()) + { + QLOG_ERROR() << filename << "contains a 'libraries' field, but its not an array"; + return out; + } + 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(); + 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) + { + lib.hint = "local"; + lib.insertType = Library::Prepend; + out.addLibs.prepend(lib); + } + else + { + out.overwriteLibs.append(lib); + } + } + } + if (root.contains("+libraries")) + { + QJsonValue librariesVal = root.value("+libraries"); + if (!librariesVal.isArray()) + { + QLOG_ERROR() << filename << "contains a '+libraries' field, but its not an array"; + return out; + } + 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(); + 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; + { + if (insertVal.isString()) + { + insertString = insertVal.toString(); + } + else if (insertVal.isObject()) + { + QJsonObject insertObj = insertVal.toObject(); + if (insertObj.isEmpty()) + { + QLOG_ERROR() << "One library has an empty insert object in" << filename; + return out; + } + insertString = insertObj.keys().first(); + lib.insertData = insertObj.value(insertString).toString(); + } + } + if (insertString == "apply") + { + lib.insertType = Library::Apply; + } + else if (insertString == "prepend") + { + lib.insertType = Library::Prepend; + } + else if (insertString == "append") + { + lib.insertType = Library::Prepend; + } + else if (insertString == "replace") + { + lib.insertType = Library::Replace; + } + else + { + QLOG_ERROR() << "A '+' library in" << filename + << "contains an invalid insert type"; + return out; + } + if (libObj.contains("MMC-depend") && libObj.value("MMC-depend").isString()) + { + const QString dependString = libObj.value("MMC-depend").toString(); + if (dependString == "hard") + { + lib.dependType = Library::Hard; + } + else if (dependString == "soft") + { + lib.dependType = Library::Soft; + } + else + { + QLOG_ERROR() << "A '+' library in" << filename + << "contains an invalid depend type"; + return out; + } + } + out.addLibs.append(lib); + } + } + if (root.contains("-libraries")) + { + QJsonValue librariesVal = root.value("-libraries"); + if (!librariesVal.isArray()) + { + QLOG_ERROR() << filename << "contains a '-libraries' field, but its not an array"; + return out; + } + 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; +} + +std::shared_ptr VersionFile::createLibrary(const VersionFile::Library &lib) +{ + std::shared_ptr out(new OneSixLibrary(lib.name)); + if (!lib.url.isEmpty()) + { + out->setBaseUrl(lib.url); + } + out->setHint(lib.hint); + if (!lib.absoluteUrl.isEmpty()) + { + out->setAbsoluteUrl(lib.absoluteUrl); + } + 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->finalize(); + return out; +} + +int VersionFile::findLibrary(QList> haystack, + const QString &needle) +{ + for (int i = 0; i < haystack.size(); ++i) + { + if (QRegExp(needle, Qt::CaseSensitive, QRegExp::WildcardUnix) + .indexIn(haystack.at(i)->rawName()) != -1) + { + return i; + } + } + return -1; +} + +VersionFile::ApplyError VersionFile::applyTo(VersionFinal *version) +{ + if (minimumLauncherVersion != -1) + { + if (minimumLauncherVersion > CURRENT_MINIMUM_LAUNCHER_VERSION) + { + QLOG_ERROR() << filename << "is for a different launcher version (" + << minimumLauncherVersion << "), current supported is" + << CURRENT_MINIMUM_LAUNCHER_VERSION; + return LauncherVersionError; + } + } + + if (!version->id.isNull() && !mcVersion.isNull()) + { + if (QRegExp(mcVersion, Qt::CaseInsensitive, QRegExp::Wildcard).indexIn(version->id) == + -1) + { + QLOG_ERROR() << filename << "is for a different version of Minecraft"; + return OtherError; + } + } + + if (!id.isNull()) + { + version->id = id; + } + if (!mainClass.isNull()) + { + version->mainClass = mainClass; + } + if (!processArguments.isNull()) + { + version->processArguments = processArguments; + } + if (!type.isNull()) + { + version->type = type; + } + if (!releaseTime.isNull()) + { + version->releaseTime = releaseTime; + } + if (!time.isNull()) + { + version->time = time; + } + if (!assets.isNull()) + { + version->assets = assets; + } + if (minimumLauncherVersion >= 0) + { + version->minimumLauncherVersion = minimumLauncherVersion; + } + if (!overwriteMinecraftArguments.isNull()) + { + version->minecraftArguments = overwriteMinecraftArguments; + } + if (!addMinecraftArguments.isNull()) + { + version->minecraftArguments += addMinecraftArguments; + } + if (!removeMinecraftArguments.isNull()) + { + version->minecraftArguments.remove(removeMinecraftArguments); + } + if (shouldOverwriteTweakers) + { + version->tweakers = overwriteTweakers; + } + for (auto tweaker : addTweakers) + { + version->tweakers += tweaker; + } + for (auto tweaker : removeTweakers) + { + version->tweakers.removeAll(tweaker); + } + if (shouldOverwriteLibs) + { + version->libraries.clear(); + for (auto lib : overwriteLibs) + { + version->libraries.append(createLibrary(lib)); + } + } + for (auto lib : addLibs) + { + switch (lib.insertType) + { + case Library::Apply: + { + + int index = findLibrary(version->libraries, lib.name); + if (index >= 0) + { + auto library = version->libraries[index]; + if (!lib.url.isNull()) + { + library->setBaseUrl(lib.url); + } + if (!lib.hint.isNull()) + { + library->setHint(lib.hint); + } + if (!lib.absoluteUrl.isNull()) + { + library->setAbsoluteUrl(lib.absoluteUrl); + } + if (lib.applyExcludes) + { + library->extract_excludes = lib.excludes; + } + if (lib.applyNatives) + { + library->clearSuffixes(); + for (auto native : lib.natives) + { + library->addNative(native.first, native.second); + } + } + if (lib.applyRules) + { + library->setRules(lib.rules); + } + library->finalize(); + } + else + { + QLOG_WARN() << "Couldn't find" << lib.insertData << "(skipping)"; + } + break; + } + case Library::Append: + case Library::Prepend: + { + + const int startOfVersion = lib.name.lastIndexOf(':') + 1; + const int index = findLibrary( + version->libraries, QString(lib.name).replace(startOfVersion, INT_MAX, '*')); + if (index < 0) + { + if (lib.insertType == Library::Append) + { + version->libraries.append(createLibrary(lib)); + } + else + { + version->libraries.prepend(createLibrary(lib)); + } + } + else + { + auto otherLib = version->libraries.at(index); + 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 + if (otherLib->dependType == OneSixLibrary::Hard) + { + // we need a higher version, or we're hard to and the versions aren't + // equal + if (ourVersion > otherVersion || + (lib.dependType == Library::Hard && ourVersion != otherVersion)) + { + QLOG_ERROR() << "Error resolving library dependencies between" + << otherLib->rawName() << "and" << lib.name << "in" + << filename; + return OtherError; + } + else + { + // the library is already existing, so we don't have to do anything + } + } + else if (otherLib->dependType == OneSixLibrary::Soft) + { + // if we are higher it means we should update + if (ourVersion > otherVersion) + { + auto library = createLibrary(lib); + if (Util::Version(otherLib->minVersion) < ourVersion) + { + library->minVersion = ourVersion.toString(); + } + version->libraries.replace(index, library); + } + else + { + // our version is smaller than the existing version, but we require + // it: fail + if (lib.dependType == Library::Hard) + { + QLOG_ERROR() << "Error resolving library dependencies between" + << otherLib->rawName() << "and" << lib.name << "in" + << filename; + return OtherError; + } + } + } + } + break; + } + case Library::Replace: + { + 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)"; + } + break; + } + } + } + for (auto lib : removeLibs) + { + int index = findLibrary(version->libraries, lib); + if (index >= 0) + { + version->libraries.removeAt(index); + } + else + { + 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); + + return NoApplyError; +} -- cgit v1.2.3 From 28ad9befdcac246eb69a434be970abc29a80bc80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 2 Mar 2014 19:12:04 +0100 Subject: Remove a lot of error code and error handling madness. --- logic/VersionFile.cpp | 288 ++++++++++++-------------------------------------- 1 file changed, 67 insertions(+), 221 deletions(-) (limited to 'logic/VersionFile.cpp') diff --git a/logic/VersionFile.cpp b/logic/VersionFile.cpp index 76cf9279..4423e733 100644 --- a/logic/VersionFile.cpp +++ b/logic/VersionFile.cpp @@ -7,19 +7,20 @@ #include "logic/VersionFile.h" #include "logic/OneSixLibrary.h" #include "logic/VersionFinal.h" +#include "MMCJson.h" +using namespace MMCJson; #define CURRENT_MINIMUM_LAUNCHER_VERSION 14 VersionFile::Library VersionFile::Library::fromJson(const QJsonObject &libObj, - const QString &filename, bool &isError) + const QString &filename) { - isError = true; Library out; if (!libObj.contains("name")) { - QLOG_ERROR() << filename << "contains a library that doesn't have a 'name' field"; - return out; + throw JSONValidationError(filename + + "contains a library that doesn't have a 'name' field"); } out.name = libObj.value("name").toString(); @@ -45,44 +46,17 @@ VersionFile::Library VersionFile::Library::fromJson(const QJsonObject &libObj, readString("MMC-absoluteUrl", out.absoluteUrl); 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; - QJsonArray excludeArray = extractObj.value("exclude").toArray(); - for (auto excludeVal : excludeArray) + auto extractObj = ensureObject(libObj.value("extract")); + for (auto excludeVal : ensureArray(extractObj.value("exclude"))) { - if (!excludeVal.isString()) - { - QLOG_WARN() << filename << "contains a library that contains an 'extract' " - "field that contains an invalid 'exclude' entry " - "(skipping)"; - } - else - { - out.excludes.append(excludeVal.toString()); - } + out.excludes.append(ensureString(excludeVal)); } } 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; - QJsonObject nativesObj = libObj.value("natives").toObject(); + QJsonObject nativesObj = ensureObject(libObj.value("natives")); for (auto it = nativesObj.begin(); it != nativesObj.end(); ++it) { if (!it.value().isString()) @@ -101,24 +75,19 @@ VersionFile::Library VersionFile::Library::fromJson(const QJsonObject &libObj, out.applyRules = true; out.rules = rulesFromJsonV4(libObj); } - isError = false; return out; } -VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filename, - const bool requireOrder, bool &isError, const bool isFTB) +VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filename, const bool requireOrder, const bool isFTB) { VersionFile out; - isError = true; if (doc.isEmpty() || doc.isNull()) { - QLOG_ERROR() << filename << "is empty or null"; - return out; + throw JSONValidationError(filename + " is empty or null"); } if (!doc.isObject()) { - QLOG_ERROR() << "The root of" << filename << "is not an object"; - return out; + throw JSONValidationError("The root of " + filename + " is not an object"); } QJsonObject root = doc.object(); @@ -127,18 +96,11 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen { if (root.contains("order")) { - if (root.value("order").isDouble()) - { - out.order = root.value("order").toDouble(); - } - else - { - QLOG_ERROR() << "'order' field contains an invalid value in" << filename; - return out; - } + out.order = ensureInteger(root.value("order")); } else { + // FIXME: evaluate if we don't want to throw exceptions here instead 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)) { - QJsonValue val = root.value(key); - if (!val.isString()) - { - QLOG_WARN() << key << "is not a string in" << filename << "(skipping)"; - } - else - { - variable = val.toString(); - } + variable = ensureString(root.value(key)); } }; - // FTB id attribute is completely bogus. We ignore it. + // FIXME: This should be ignored when applying. if (!isFTB) { readString("id", out.id); @@ -180,108 +134,47 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen readString("releaseTime", out.releaseTime); readString("time", out.time); readString("assets", out.assets); + if (root.contains("minimumLauncherVersion")) { - QJsonValue val = root.value("minimumLauncherVersion"); - if (!val.isDouble()) - { - QLOG_WARN() << "minimumLauncherVersion is not an int in" << filename - << "(skipping)"; - } - else - { - out.minimumLauncherVersion = val.toDouble(); - } + out.minimumLauncherVersion = ensureInteger(root.value("minimumLauncherVersion")); } 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; - QJsonArray tweakers = root.value("tweakers").toArray(); - for (auto tweakerVal : tweakers) + for (auto tweakerVal : ensureArray(root.value("tweakers"))) { - if (!tweakerVal.isString()) - { - QLOG_ERROR() << filename - << "contains a 'tweakers' field entry that's not a string"; - return out; - } - out.overwriteTweakers.append(tweakerVal.toString()); + out.overwriteTweakers.append(ensureString(tweakerVal)); } } + 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; - } - QJsonArray tweakers = root.value("+tweakers").toArray(); - for (auto tweakerVal : tweakers) + for (auto tweakerVal : ensureArray(root.value("+tweakers"))) { - if (!tweakerVal.isString()) - { - QLOG_ERROR() << filename - << "contains a '+tweakers' field entry that's not a string"; - return out; - } - out.addTweakers.append(tweakerVal.toString()); + out.addTweakers.append(ensureString(tweakerVal)); } } + 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; - QJsonArray tweakers = root.value("-tweakers").toArray(); - for (auto tweakerVal : tweakers) + for (auto tweakerVal : ensureArray(root.value("-tweakers"))) { - if (!tweakerVal.isString()) - { - QLOG_ERROR() << filename - << "contains a '-tweakers' field entry that's not a string"; - return out; - } - out.removeTweakers.append(tweakerVal.toString()); + out.removeTweakers.append(ensureString(tweakerVal)); } } if (root.contains("libraries")) { + // FIXME: This should be done when applying. out.shouldOverwriteLibs = !isFTB; - QJsonValue librariesVal = root.value("libraries"); - if (!librariesVal.isArray()) - { - QLOG_ERROR() << filename << "contains a 'libraries' field, but its not an array"; - return out; - } - QJsonArray librariesArray = librariesVal.toArray(); - for (auto libVal : librariesArray) + for (auto libVal : ensureArray(root.value("libraries"))) { - 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; - } + auto libObj = ensureObject(libVal); + + Library lib = Library::fromJson(libObj, filename); + // FIXME: This should be done when applying. if (isFTB) { lib.hint = "local"; @@ -294,36 +187,18 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen } } } + if (root.contains("+libraries")) { - QJsonValue librariesVal = root.value("+libraries"); - if (!librariesVal.isArray()) - { - QLOG_ERROR() << filename << "contains a '+libraries' field, but its not an array"; - return out; - } - QJsonArray librariesArray = librariesVal.toArray(); - for (auto libVal : librariesArray) + for (auto libVal : ensureArray(root.value("+libraries"))) { - 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 (!libObj.contains("insert")) - { - QLOG_ERROR() << "Missing 'insert' field in '+libraries' field in" << filename; - return out; - } - QJsonValue insertVal = libObj.value("insert"); + QJsonObject libObj = ensureObject(libVal); + QJsonValue insertVal = ensureExists(libObj.value("insert")); + + // parse the library + Library lib = Library::fromJson(libObj, filename); + + // TODO: utility functions for handling this case. templates? QString insertString; { if (insertVal.isString()) @@ -335,8 +210,8 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen QJsonObject insertObj = insertVal.toObject(); if (insertObj.isEmpty()) { - QLOG_ERROR() << "One library has an empty insert object in" << filename; - return out; + throw JSONValidationError("One library has an empty insert object in " + + filename); } insertString = insertObj.keys().first(); lib.insertData = insertObj.value(insertString).toString(); @@ -360,13 +235,12 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen } else { - QLOG_ERROR() << "A '+' library in" << filename - << "contains an invalid insert type"; - return out; + throw JSONValidationError("A '+' library in " + filename + + " contains an invalid insert type"); } - 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") { lib.dependType = Library::Hard; @@ -377,9 +251,8 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen } else { - QLOG_ERROR() << "A '+' library in" << filename - << "contains an invalid depend type"; - return out; + throw JSONValidationError("A '+' library in " + filename + + " contains an invalid depend type"); } } out.addLibs.append(lib); @@ -387,36 +260,12 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen } if (root.contains("-libraries")) { - QJsonValue librariesVal = root.value("-libraries"); - if (!librariesVal.isArray()) - { - QLOG_ERROR() << filename << "contains a '-libraries' field, but its not an array"; - return out; - } - QJsonArray librariesArray = librariesVal.toArray(); - for (auto libVal : librariesArray) + for (auto libVal : ensureArray(root.value("-libraries"))) { - 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()); + auto libObj = ensureObject(libVal); + out.removeLibs.append(ensureString(libObj.value("name"))); } } - - isError = false; return out; } @@ -457,16 +306,15 @@ int VersionFile::findLibrary(QList> haystack, return -1; } -VersionFile::ApplyError VersionFile::applyTo(VersionFinal *version) +void VersionFile::applyTo(VersionFinal *version) { if (minimumLauncherVersion != -1) { if (minimumLauncherVersion > CURRENT_MINIMUM_LAUNCHER_VERSION) { - QLOG_ERROR() << filename << "is for a different launcher version (" - << minimumLauncherVersion << "), current supported is" - << CURRENT_MINIMUM_LAUNCHER_VERSION; - return LauncherVersionError; + throw VersionBuildError( + QString("%1 is for a different launcher version (%2), current supported is %3") + .arg(filename, minimumLauncherVersion, CURRENT_MINIMUM_LAUNCHER_VERSION)); } } @@ -475,8 +323,8 @@ VersionFile::ApplyError VersionFile::applyTo(VersionFinal *version) if (QRegExp(mcVersion, Qt::CaseInsensitive, QRegExp::Wildcard).indexIn(version->id) == -1) { - QLOG_ERROR() << filename << "is for a different version of Minecraft"; - return OtherError; + throw VersionBuildError( + QString("%1 is for a different version of Minecraft").arg(filename)); } } @@ -587,7 +435,7 @@ VersionFile::ApplyError VersionFile::applyTo(VersionFinal *version) } else { - QLOG_WARN() << "Couldn't find" << lib.insertData << "(skipping)"; + QLOG_WARN() << "Couldn't find" << lib.name << "(skipping)"; } break; } @@ -623,10 +471,10 @@ VersionFile::ApplyError VersionFile::applyTo(VersionFinal *version) if (ourVersion > otherVersion || (lib.dependType == Library::Hard && ourVersion != otherVersion)) { - QLOG_ERROR() << "Error resolving library dependencies between" - << otherLib->rawName() << "and" << lib.name << "in" - << filename; - return OtherError; + throw VersionBuildError( + QString( + "Error resolving library dependencies between %1 and %2 in %3.") + .arg(otherLib->rawName(), lib.name, filename)); } else { @@ -651,10 +499,10 @@ VersionFile::ApplyError VersionFile::applyTo(VersionFinal *version) // it: fail if (lib.dependType == Library::Hard) { - QLOG_ERROR() << "Error resolving library dependencies between" - << otherLib->rawName() << "and" << lib.name << "in" - << filename; - return OtherError; + throw VersionBuildError(QString( + "Error resolving library dependencies between %1 and %2 in %3.") + .arg(otherLib->rawName(), lib.name, + filename)); } } } @@ -697,6 +545,4 @@ VersionFile::ApplyError VersionFile::applyTo(VersionFinal *version) versionFile.filename = filename; versionFile.order = order; version->versionFiles.append(versionFile); - - return NoApplyError; } -- cgit v1.2.3 From 29cdc9364b0153d04a211adf3eab86076174c0a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Mon, 3 Mar 2014 01:23:10 +0100 Subject: More code butchery related to version files. No end in sight. --- logic/VersionFile.cpp | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'logic/VersionFile.cpp') diff --git a/logic/VersionFile.cpp b/logic/VersionFile.cpp index 4423e733..b7695907 100644 --- a/logic/VersionFile.cpp +++ b/logic/VersionFile.cpp @@ -13,10 +13,10 @@ using namespace MMCJson; #define CURRENT_MINIMUM_LAUNCHER_VERSION 14 -VersionFile::Library VersionFile::Library::fromJson(const QJsonObject &libObj, +RawLibrary RawLibrary::fromJson(const QJsonObject &libObj, const QString &filename) { - Library out; + RawLibrary out; if (!libObj.contains("name")) { throw JSONValidationError(filename + @@ -78,7 +78,8 @@ VersionFile::Library VersionFile::Library::fromJson(const QJsonObject &libObj, return out; } -VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filename, const bool requireOrder, const bool isFTB) +VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filename, + const bool requireOrder, const bool isFTB) { VersionFile out; if (doc.isEmpty() || doc.isNull()) @@ -173,12 +174,12 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen { auto libObj = ensureObject(libVal); - Library lib = Library::fromJson(libObj, filename); + RawLibrary lib = RawLibrary::fromJson(libObj, filename); // FIXME: This should be done when applying. if (isFTB) { lib.hint = "local"; - lib.insertType = Library::Prepend; + lib.insertType = RawLibrary::Prepend; out.addLibs.prepend(lib); } else @@ -196,7 +197,7 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen QJsonValue insertVal = ensureExists(libObj.value("insert")); // parse the library - Library lib = Library::fromJson(libObj, filename); + RawLibrary lib = RawLibrary::fromJson(libObj, filename); // TODO: utility functions for handling this case. templates? QString insertString; @@ -219,19 +220,19 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen } if (insertString == "apply") { - lib.insertType = Library::Apply; + lib.insertType = RawLibrary::Apply; } else if (insertString == "prepend") { - lib.insertType = Library::Prepend; + lib.insertType = RawLibrary::Prepend; } else if (insertString == "append") { - lib.insertType = Library::Prepend; + lib.insertType = RawLibrary::Prepend; } else if (insertString == "replace") { - lib.insertType = Library::Replace; + lib.insertType = RawLibrary::Replace; } else { @@ -243,11 +244,11 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen const QString dependString = ensureString(libObj.value("MMC-depend")); if (dependString == "hard") { - lib.dependType = Library::Hard; + lib.dependType = RawLibrary::Hard; } else if (dependString == "soft") { - lib.dependType = Library::Soft; + lib.dependType = RawLibrary::Soft; } else { @@ -269,7 +270,7 @@ VersionFile VersionFile::fromJson(const QJsonDocument &doc, const QString &filen return out; } -std::shared_ptr VersionFile::createLibrary(const VersionFile::Library &lib) +std::shared_ptr VersionFile::createLibrary(const RawLibrary &lib) { std::shared_ptr out(new OneSixLibrary(lib.name)); if (!lib.url.isEmpty()) @@ -396,7 +397,7 @@ void VersionFile::applyTo(VersionFinal *version) { switch (lib.insertType) { - case Library::Apply: + case RawLibrary::Apply: { int index = findLibrary(version->libraries, lib.name); @@ -439,8 +440,8 @@ void VersionFile::applyTo(VersionFinal *version) } break; } - case Library::Append: - case Library::Prepend: + case RawLibrary::Append: + case RawLibrary::Prepend: { const int startOfVersion = lib.name.lastIndexOf(':') + 1; @@ -448,7 +449,7 @@ void VersionFile::applyTo(VersionFinal *version) version->libraries, QString(lib.name).replace(startOfVersion, INT_MAX, '*')); if (index < 0) { - if (lib.insertType == Library::Append) + if (lib.insertType == RawLibrary::Append) { version->libraries.append(createLibrary(lib)); } @@ -469,7 +470,7 @@ 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 == Library::Hard && ourVersion != otherVersion)) + (lib.dependType == RawLibrary::Hard && ourVersion != otherVersion)) { throw VersionBuildError( QString( @@ -497,7 +498,7 @@ void VersionFile::applyTo(VersionFinal *version) { // our version is smaller than the existing version, but we require // it: fail - if (lib.dependType == Library::Hard) + if (lib.dependType == RawLibrary::Hard) { throw VersionBuildError(QString( "Error resolving library dependencies between %1 and %2 in %3.") @@ -509,7 +510,7 @@ void VersionFile::applyTo(VersionFinal *version) } break; } - case Library::Replace: + case RawLibrary::Replace: { int index = findLibrary(version->libraries, lib.insertData); if (index >= 0) -- cgit v1.2.3 From 47bc7e5ee377dacfeb7cf3d13f07cfd24db906fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Wed, 5 Mar 2014 01:50:05 +0100 Subject: More refactor. --- logic/VersionFile.cpp | 189 ++++++++++++++++++++++++-------------------------- 1 file changed, 89 insertions(+), 100 deletions(-) (limited to 'logic/VersionFile.cpp') 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); } -- cgit v1.2.3 From b2c803a378695026f12aabc3729eb2139bee1b2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 9 Mar 2014 23:42:25 +0100 Subject: Improve reporting of version file errors.x --- logic/VersionFile.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'logic/VersionFile.cpp') 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)); -- cgit v1.2.3