Add a MMC-depend field (soft/hard) for version checking

This commit is contained in:
Jan Dalheimer 2014-02-02 14:05:07 +01:00
parent 790402bdce
commit ece826bdbc
8 changed files with 351 additions and 32 deletions

View File

@ -35,6 +35,9 @@ src/userutils.cpp
include/cmdutils.h
src/cmdutils.cpp
include/modutils.h
src/modutils.cpp
)
# Set the include dir path.

View File

@ -0,0 +1,32 @@
#pragma once
#include <QString>
#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);
}

View File

@ -0,0 +1,216 @@
#include "include/modutils.h"
#include <QStringList>
#include <QUrl>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
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(
"(?<start>[\\[\\]\\(\\)])(?<bottom>.*?)(,(?<top>.*?))?(?<end>[\\[\\]\\(\\)])");
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;
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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;

View File

@ -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

View File

@ -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<Library> 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")
else if (insertString == "append")
{
lib.insertType = Library::PrependIfNotExists;
}
else if (insertString == "append-if-not-exists")
{
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,29 +644,68 @@ 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)
{
if (lib.insertType == Library::Append)
{
version->libraries.append(createLibrary(lib));
}
break;
}
case Library::PrependIfNotExists:
{
int index = findLibrary(version->libraries, lib.name);
if (index < 0)
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;
}
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;
}
case Library::Replace: