From ece826bdbc5ca525e253cafcfef3d93e492949f5 Mon Sep 17 00:00:00 2001 From: Jan Dalheimer Date: Sun, 2 Feb 2014 14:05:07 +0100 Subject: Add a MMC-depend field (soft/hard) for version checking --- depends/util/CMakeLists.txt | 3 + depends/util/include/modutils.h | 32 ++++++ depends/util/src/modutils.cpp | 216 ++++++++++++++++++++++++++++++++++++++++ logic/ForgeInstaller.cpp | 6 +- logic/LiteLoaderInstaller.cpp | 3 +- logic/OneSixLibrary.cpp | 2 +- logic/OneSixLibrary.h | 11 +- logic/OneSixVersionBuilder.cpp | 110 ++++++++++++++------ 8 files changed, 351 insertions(+), 32 deletions(-) create mode 100644 depends/util/include/modutils.h create mode 100644 depends/util/src/modutils.cpp diff --git a/depends/util/CMakeLists.txt b/depends/util/CMakeLists.txt index db7d70e6..7f6573bd 100644 --- a/depends/util/CMakeLists.txt +++ b/depends/util/CMakeLists.txt @@ -35,6 +35,9 @@ src/userutils.cpp include/cmdutils.h src/cmdutils.cpp + +include/modutils.h +src/modutils.cpp ) # Set the include dir path. diff --git a/depends/util/include/modutils.h b/depends/util/include/modutils.h new file mode 100644 index 00000000..e04db66f --- /dev/null +++ b/depends/util/include/modutils.h @@ -0,0 +1,32 @@ +#pragma once + +#include +#include "libutil_config.h" + +class QUrl; + +namespace Util +{ +struct Version +{ + Version(const QString &str); + + bool operator<(const Version &other) const; + bool operator<=(const Version &other) const; + bool operator>(const Version &other) const; + bool operator==(const Version &other) const; + bool operator!=(const Version &other) const; + + QString toString() const + { + return m_string; + } + +private: + QString m_string; +}; + +LIBUTIL_EXPORT QUrl expandQMURL(const QString &in); +LIBUTIL_EXPORT bool versionIsInInterval(const QString &version, const QString &interval); +} + diff --git a/depends/util/src/modutils.cpp b/depends/util/src/modutils.cpp new file mode 100644 index 00000000..44a04b72 --- /dev/null +++ b/depends/util/src/modutils.cpp @@ -0,0 +1,216 @@ +#include "include/modutils.h" + +#include +#include +#include +#include + +Util::Version::Version(const QString &str) : m_string(str) +{ +} + +bool Util::Version::operator<(const Version &other) const +{ + QStringList parts1 = m_string.split('.'); + QStringList parts2 = other.m_string.split('.'); + + while (!parts1.isEmpty() && !parts2.isEmpty()) + { + QString part1 = parts1.isEmpty() ? "0" : parts1.takeFirst(); + QString part2 = parts2.isEmpty() ? "0" : parts2.takeFirst(); + bool ok1 = false; + bool ok2 = false; + int int1 = part1.toInt(&ok1); + int int2 = part2.toInt(&ok2); + if (ok1 && ok2) + { + if (int1 == int2) + { + continue; + } + else + { + return int1 < int2; + } + } + else + { + if (part1 == part2) + { + continue; + } + else + { + return part1 < part2; + } + } + } + + return false; +} +bool Util::Version::operator<=(const Util::Version &other) const +{ + return *this < other || *this == other; +} +bool Util::Version::operator>(const Version &other) const +{ + QStringList parts1 = m_string.split('.'); + QStringList parts2 = other.m_string.split('.'); + + while (!parts1.isEmpty() && !parts2.isEmpty()) + { + QString part1 = parts1.isEmpty() ? "0" : parts1.takeFirst(); + QString part2 = parts2.isEmpty() ? "0" : parts2.takeFirst(); + bool ok1 = false; + bool ok2 = false; + int int1 = part1.toInt(&ok1); + int int2 = part2.toInt(&ok2); + if (ok1 && ok2) + { + if (int1 == int2) + { + continue; + } + else + { + return int1 > int2; + } + } + else + { + if (part1 == part2) + { + continue; + } + else + { + return part1 > part2; + } + } + } + + return false; +} +bool Util::Version::operator==(const Version &other) const +{ + QStringList parts1 = m_string.split('.'); + QStringList parts2 = other.m_string.split('.'); + + while (!parts1.isEmpty() && !parts2.isEmpty()) + { + QString part1 = parts1.isEmpty() ? "0" : parts1.takeFirst(); + QString part2 = parts2.isEmpty() ? "0" : parts2.takeFirst(); + bool ok1 = false; + bool ok2 = false; + int int1 = part1.toInt(&ok1); + int int2 = part2.toInt(&ok2); + if (ok1 && ok2) + { + if (int1 == int2) + { + continue; + } + else + { + return false; + } + } + else + { + if (part1 == part2) + { + continue; + } + else + { + return false; + } + } + } + + return true; +} +bool Util::Version::operator!=(const Version &other) const +{ + return !operator==(other); +} + +QUrl Util::expandQMURL(const QString &in) +{ + QUrl inUrl(in); + if (inUrl.scheme() == "github") + { + // needed because QUrl makes the host all lower cases + const QString repo = in.mid(in.indexOf(inUrl.host(), 0, Qt::CaseInsensitive), inUrl.host().size()); + QUrl out; + out.setScheme("https"); + out.setHost("raw.github.com"); + out.setPath(QString("/%1/%2/%3%4") + .arg(inUrl.userInfo(), repo, + inUrl.fragment().isEmpty() ? "master" : inUrl.fragment(), inUrl.path())); + return out; + } + else if (inUrl.scheme() == "mcf") + { + QUrl out; + out.setScheme("http"); + out.setHost("www.minecraftforum.net"); + out.setPath(QString("/topic/%1-").arg(inUrl.path())); + return out; + } + else + { + return in; + } +} + +bool Util::versionIsInInterval(const QString &version, const QString &interval) +{ + if (interval.isEmpty() || version == interval) + { + return true; + } + + // Interval notation is used + QRegularExpression exp( + "(?[\\[\\]\\(\\)])(?.*?)(,(?.*?))?(?[\\[\\]\\(\\)])"); + QRegularExpressionMatch match = exp.match(interval); + if (match.hasMatch()) + { + const QChar start = match.captured("start").at(0); + const QChar end = match.captured("end").at(0); + const QString bottom = match.captured("bottom"); + const QString top = match.captured("top"); + + // check if in range (bottom) + if (!bottom.isEmpty()) + { + if ((start == '[') && !(version >= bottom)) + { + return false; + } + else if ((start == '(') && !(version > bottom)) + { + return false; + } + } + + // check if in range (top) + if (!top.isEmpty()) + { + if ((end == ']') && !(version <= top)) + { + return false; + } + else if ((end == ')') && !(version < top)) + { + return false; + } + } + + return true; + } + + return false; +} + diff --git a/logic/ForgeInstaller.cpp b/logic/ForgeInstaller.cpp index 47c42694..6b498d70 100644 --- a/logic/ForgeInstaller.cpp +++ b/logic/ForgeInstaller.cpp @@ -171,7 +171,11 @@ bool ForgeInstaller::add(OneSixInstance *to) if (!found) { // add lib - libObj.insert("insert", QString("prepend-if-not-exists")); + libObj.insert("insert", QString("prepend")); + if (lib->name() == "minecraftforge") + { + libObj.insert("MMC-depend", QString("hard")); + } sliding_insert_window++; } librariesPlus.prepend(libObj); diff --git a/logic/LiteLoaderInstaller.cpp b/logic/LiteLoaderInstaller.cpp index 60a43d49..c363cad6 100644 --- a/logic/LiteLoaderInstaller.cpp +++ b/logic/LiteLoaderInstaller.cpp @@ -63,7 +63,7 @@ bool LiteLoaderInstaller::add(OneSixInstance *to) OneSixLibrary launchwrapperLib("net.minecraft:launchwrapper:" + m_launcherWrapperVersionMapping[to->intendedVersionId()]); launchwrapperLib.finalize(); QJsonObject lwLibObj = launchwrapperLib.toJson(); - lwLibObj.insert("insert", QString("prepend-if-not-exists")); + lwLibObj.insert("insert", QString("prepend")); libraries.append(lwLibObj); } @@ -74,6 +74,7 @@ bool LiteLoaderInstaller::add(OneSixInstance *to) liteloaderLib.finalize(); QJsonObject llLibObj = liteloaderLib.toJson(); llLibObj.insert("insert", QString("prepend")); + llLibObj.insert("MMC-depend", QString("hard")); libraries.append(llLibObj); } diff --git a/logic/OneSixLibrary.cpp b/logic/OneSixLibrary.cpp index 2b1f0600..c78679d1 100644 --- a/logic/OneSixLibrary.cpp +++ b/logic/OneSixLibrary.cpp @@ -46,7 +46,7 @@ void OneSixLibrary::finalize() } m_decentname = parts[1]; - m_decentversion = parts[2]; + m_decentversion = minVersion = parts[2]; m_storage_path = relative; m_download_url = m_base_url + relative; diff --git a/logic/OneSixLibrary.h b/logic/OneSixLibrary.h index 5384015a..371ca6f4 100644 --- a/logic/OneSixLibrary.h +++ b/logic/OneSixLibrary.h @@ -60,12 +60,21 @@ private: public: QStringList extract_excludes; + QString minVersion; + + enum DependType + { + Soft, + Hard + }; + DependType dependType; public: /// Constructor - OneSixLibrary(const QString &name) + OneSixLibrary(const QString &name, const DependType type = Soft) { m_name = name; + dependType = type; } /// Returns the raw name field diff --git a/logic/OneSixVersionBuilder.cpp b/logic/OneSixVersionBuilder.cpp index 4d6fb05a..55c7d48e 100644 --- a/logic/OneSixVersionBuilder.cpp +++ b/logic/OneSixVersionBuilder.cpp @@ -29,6 +29,7 @@ #include "OneSixVersion.h" #include "OneSixInstance.h" #include "OneSixRule.h" +#include "modutils.h" #include "logger/QsLog.h" struct VersionFile @@ -78,12 +79,16 @@ struct VersionFile Apply, Append, Prepend, - AppendIfNotExists, - PrependIfNotExists, Replace }; - InsertType insertType; + InsertType insertType = Append; QString insertData; + enum DependType + { + Soft, + Hard + }; + DependType dependType = Soft; }; bool shouldOverwriteLibs = false; QList overwriteLibs; @@ -414,21 +419,13 @@ struct VersionFile { lib.insertType = Library::Apply; } - else if (insertString == "append") - { - lib.insertType = Library::Append; - } else if (insertString == "prepend") { lib.insertType = Library::Prepend; } - else if (insertString == "prepend-if-not-exists") - { - lib.insertType = Library::PrependIfNotExists; - } - else if (insertString == "append-if-not-exists") + else if (insertString == "append") { - lib.insertType = Library::PrependIfNotExists; + lib.insertType = Library::Prepend; } else if (insertString == "replace") { @@ -440,6 +437,24 @@ struct VersionFile << "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); } } @@ -629,28 +644,67 @@ struct VersionFile break; } case Library::Append: - version->libraries.append(createLibrary(lib)); - break; case Library::Prepend: - version->libraries.prepend(createLibrary(lib)); - break; - case Library::AppendIfNotExists: { - int index = findLibrary(version->libraries, lib.name); + const int startOfVersion = lib.name.lastIndexOf(':') + 1; + const int index = findLibrary(version->libraries, QString(lib.name).replace(startOfVersion, INT_MAX, '*')); if (index < 0) { - version->libraries.append(createLibrary(lib)); + if (lib.insertType == Library::Append) + { + version->libraries.append(createLibrary(lib)); + } + else + { + version->libraries.prepend(createLibrary(lib)); + } } - break; - } - case Library::PrependIfNotExists: - { - - int index = findLibrary(version->libraries, lib.name); - if (index < 0) + else { - version->libraries.prepend(createLibrary(lib)); + 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; + } + 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; + } + } + } } break; } -- cgit v1.2.3