diff options
Diffstat (limited to 'libmultimc')
-rw-r--r-- | libmultimc/include/fullversion.h | 5 | ||||
-rw-r--r-- | libmultimc/include/library.h | 117 | ||||
-rw-r--r-- | libmultimc/src/fullversion.cpp | 2 | ||||
-rw-r--r-- | libmultimc/src/fullversionfactory.cpp | 137 | ||||
-rw-r--r-- | libmultimc/src/library.cpp | 19 |
5 files changed, 246 insertions, 34 deletions
diff --git a/libmultimc/include/fullversion.h b/libmultimc/include/fullversion.h index b1de02a3..523ac87c 100644 --- a/libmultimc/include/fullversion.h +++ b/libmultimc/include/fullversion.h @@ -1,6 +1,7 @@ #pragma once #include <QString> -#include <QStringList> + +class Library; class FullVersion { @@ -40,7 +41,7 @@ public: QString mainClass; // the list of libs. just the names for now. expand to full-blown strutures! - QStringList libraries; + QList<QSharedPointer<Library> > libraries; // is this actually a legacy version? if so, none of the other stuff here will be ever used. // added by FullVersionFactory diff --git a/libmultimc/include/library.h b/libmultimc/include/library.h index 10ecb9f3..30270bc1 100644 --- a/libmultimc/include/library.h +++ b/libmultimc/include/library.h @@ -15,4 +15,121 @@ #pragma once +#include <QtCore> +class Library; + +enum RuleAction +{ + Allow, + Disallow, + Defer +}; + +RuleAction RuleAction_fromString(QString); + +class Rule +{ +protected: + RuleAction m_result; + virtual bool applies(Library * parent) = 0; +public: + Rule(RuleAction result) + :m_result(result) {} + virtual ~Rule(){}; + RuleAction apply(Library * parent) + { + if(applies(parent)) + return m_result; + else + return Defer; + }; +}; + +enum OpSys +{ + Os_Windows, + Os_Linux, + Os_OSX, + Os_Other +}; + +OpSys OpSys_fromString(QString); + +#ifdef Q_OS_MAC + #define currentSystem Os_OSX +#endif + +#ifdef Q_OS_LINUX + #define currentSystem Os_Linux +#endif + +#ifdef Q_OS_WIN32 + #define currentSystem Os_Windows +#endif + +#ifndef currentSystem + #define currentSystem Os_Other +#endif + + +class OsRule : public Rule +{ +private: + // the OS + OpSys m_system; + // the OS version regexp + QString m_version_regexp; +protected: + virtual bool applies ( Library* ) + { + return (m_system == currentSystem); + } +public: + OsRule(RuleAction result, OpSys system, QString version_regexp) + : Rule(result), m_system(system), m_version_regexp(version_regexp) {} +}; + +class ImplicitRule : public Rule +{ +protected: + virtual bool applies ( Library* ) + { + return true; + } +public: + ImplicitRule(RuleAction result) + : Rule(result) {} +}; + +class Library +{ +public: + QString base_url; + QString name; + QList<QSharedPointer<Rule> > rules; + QMap<OpSys, QString> natives; + QStringList extract_excludes; + + void AddRule(RuleAction result) + { + rules.append(QSharedPointer<Rule>(new ImplicitRule(result))); + } + void AddRule(RuleAction result, OpSys system, QString version_regexp) + { + rules.append(QSharedPointer<Rule>(new OsRule(result, system, version_regexp))); + } + bool applies() + { + if(rules.empty()) + return true; + RuleAction result = Disallow; + for(auto rule: rules) + { + RuleAction temp = rule->apply( this ); + if(temp != Defer) + result = temp; + } + return result == Allow; + } +}; diff --git a/libmultimc/src/fullversion.cpp b/libmultimc/src/fullversion.cpp index d66ce5bb..6f4662b4 100644 --- a/libmultimc/src/fullversion.cpp +++ b/libmultimc/src/fullversion.cpp @@ -1,4 +1,4 @@ +#include <QtCore> #include "fullversion.h" - // ECHO, echo, echo, ....
\ No newline at end of file diff --git a/libmultimc/src/fullversionfactory.cpp b/libmultimc/src/fullversionfactory.cpp index 8873ee2d..9b4ae266 100644 --- a/libmultimc/src/fullversionfactory.cpp +++ b/libmultimc/src/fullversionfactory.cpp @@ -1,69 +1,144 @@ #include "fullversionfactory.h" #include "fullversion.h" +#include <library.h> -QSharedPointer<FullVersion> FullVersionFactory::parse4(QJsonObject root, QSharedPointer<FullVersion> product) +QSharedPointer<FullVersion> FullVersionFactory::parse4(QJsonObject root, QSharedPointer<FullVersion> fullVersion) { - product->id = root.value("id").toString(); + fullVersion->id = root.value("id").toString(); // if it's on our legacy list, it's legacy - if(legacyWhitelist.contains(product->id)) - product->isLegacy = true; + if(legacyWhitelist.contains(fullVersion->id)) + fullVersion->isLegacy = true; - product->mainClass = root.value("mainClass").toString(); + fullVersion->mainClass = root.value("mainClass").toString(); auto procArgsValue = root.value("processArguments"); if(procArgsValue.isString()) { - product->processArguments = procArgsValue.toString(); - QString toCompare = product->processArguments.toLower(); + fullVersion->processArguments = procArgsValue.toString(); + QString toCompare = fullVersion->processArguments.toLower(); if(toCompare == "legacy") { - product->minecraftArguments = " ${auth_player_name} ${auth_session}"; - product->isLegacy = true; + fullVersion->minecraftArguments = " ${auth_player_name} ${auth_session}"; + fullVersion->isLegacy = true; } else if(toCompare == "username_session") { - product->minecraftArguments = "--username ${auth_player_name} --session ${auth_session}"; + fullVersion->minecraftArguments = "--username ${auth_player_name} --session ${auth_session}"; } else if(toCompare == "username_session_version") { - product->minecraftArguments = "--username ${auth_player_name} --session ${auth_session} --version ${profile_name}"; + fullVersion->minecraftArguments = "--username ${auth_player_name} --session ${auth_session} --version ${profile_name}"; } } auto minecraftArgsValue = root.value("minecraftArguments"); if(minecraftArgsValue.isString()) { - product->minecraftArguments = minecraftArgsValue.toString(); + fullVersion->minecraftArguments = minecraftArgsValue.toString(); } - product->releaseTime = root.value("releaseTime").toString(); - product->time = root.value("time").toString(); + fullVersion->releaseTime = root.value("releaseTime").toString(); + fullVersion->time = root.value("time").toString(); - // Iterate through the list. + // Iterate through the list, if it's a list. auto librariesValue = root.value("libraries"); - if(librariesValue.isArray()) + if(!librariesValue.isArray()) + return fullVersion; + + QJsonArray libList = root.value("libraries").toArray(); + for (auto libVal : libList) { - QJsonArray libList = root.value("libraries").toArray(); - for (auto lib : libList) + QSharedPointer<Library> library(new Library()); + if (!libVal.isObject()) + { + continue; + } + + QJsonObject libObj = libVal.toObject(); + + // Library name + auto nameVal = libObj.value("name"); + if(!nameVal.isString()) + continue; + library->name = nameVal.toString(); + + // Extract excludes (if any) + auto extractVal = libObj.value("extract"); + if(extractVal.isObject()) { - if (!lib.isObject()) + QStringList excludes; + auto extractObj = extractVal.toObject(); + auto excludesVal = extractObj.value("exclude"); + if(!excludesVal.isArray()) + goto SKIP_EXTRACTS; + auto excludesList = excludesVal.toArray(); + for(auto excludeVal : excludesList) { - continue; + if(excludeVal.isString()) + excludes.append(excludeVal.toString()); } + library->extract_excludes = excludes; + } + SKIP_EXTRACTS: + + auto nativesVal = libObj.value("natives"); + if(nativesVal.isObject()) + { + auto nativesObj = nativesVal.toObject(); + auto iter = nativesObj.begin(); + while(iter != nativesObj.end()) + { + auto osType = OpSys_fromString(iter.key()); + if(osType == Os_Other) + continue; + if(!iter.value().isString()) + continue; + library->natives[osType] = iter.value().toString(); + iter++; + } + } + + // Library rules (if any) + auto rulesVal = libObj.value("rules"); + if(rulesVal.isArray()) + { + QList<QSharedPointer<Rule> > rules; - QJsonObject libObj = lib.toObject(); - - QString crud = libObj.value("name").toString(); - product->libraries.append(crud); - - // TODO: improve! - /* - auto parts = crud.split(':'); - int zz = parts.size(); - */ + QJsonArray ruleList = rulesVal.toArray(); + for(auto ruleVal : ruleList) + { + QSharedPointer<Rule> rule; + if(!ruleVal.isObject()) + continue; + auto ruleObj = ruleVal.toObject(); + auto actionVal = ruleObj.value("action"); + if(!actionVal.isString()) + continue; + auto action = RuleAction_fromString(actionVal.toString()); + if(action == Defer) + continue; + + auto osVal = ruleObj.value("os"); + if(!osVal.isObject()) + { + rule.reset(new ImplicitRule(action)); + } + else + { + auto osObj = osVal.toObject(); + auto osNameVal = osObj.value("name"); + if(!osNameVal.isString()) + continue; + OpSys requiredOs = OpSys_fromString(osNameVal.toString()); + QString versionRegex = osObj.value("version").toString(); + rule.reset(new OsRule(action, requiredOs, versionRegex)); + } + rules.append(rule); + } + library->rules = rules; } } - return product; + return fullVersion; } QSharedPointer<FullVersion> FullVersionFactory::parse(QByteArray data) diff --git a/libmultimc/src/library.cpp b/libmultimc/src/library.cpp index b0490616..0fb4f9d3 100644 --- a/libmultimc/src/library.cpp +++ b/libmultimc/src/library.cpp @@ -15,4 +15,23 @@ #include "include/library.h" +RuleAction RuleAction_fromString(QString name) +{ + if(name == "allow") + return Allow; + if(name == "disallow") + return Disallow; + return Defer; +} + +OpSys OpSys_fromString(QString name) +{ + if(name == "linux") + return Os_Linux; + if(name == "windows") + return Os_Windows; + if(name == "osx") + return Os_OSX; + return Os_Other; +} // default url for lib: https://s3.amazonaws.com/Minecraft.Download/libraries/ |