diff options
| author | Petr Mrázek <peterix@gmail.com> | 2013-09-22 14:03:05 +0200 |
|---|---|---|
| committer | Petr Mrázek <peterix@gmail.com> | 2013-09-22 14:03:05 +0200 |
| commit | 1bed5e4023f503cb95a914c1e6886e045b7f34e2 (patch) | |
| tree | 6c2a2eb34b93bdd24e7db1f9378b231084c0b27a /logic | |
| parent | 930b07afd4229e952d0cd47ca62cd94235499a0c (diff) | |
| parent | 9d03a9c1e3b9c24a4146adedb2971591d23b037a (diff) | |
| download | PrismLauncher-1bed5e4023f503cb95a914c1e6886e045b7f34e2.tar.gz PrismLauncher-1bed5e4023f503cb95a914c1e6886e045b7f34e2.tar.bz2 PrismLauncher-1bed5e4023f503cb95a914c1e6886e045b7f34e2.zip | |
Merge branch 'feature_onesix_forge' into develop
Diffstat (limited to 'logic')
| -rw-r--r-- | logic/BaseInstance.cpp | 2 | ||||
| -rw-r--r-- | logic/BaseInstance.h | 2 | ||||
| -rw-r--r-- | logic/ForgeInstaller.cpp | 135 | ||||
| -rw-r--r-- | logic/ForgeInstaller.h | 25 | ||||
| -rw-r--r-- | logic/LegacyUpdate.cpp | 4 | ||||
| -rw-r--r-- | logic/ModList.h | 3 | ||||
| -rw-r--r-- | logic/NostalgiaInstance.h | 1 | ||||
| -rw-r--r-- | logic/OneSixInstance.cpp | 129 | ||||
| -rw-r--r-- | logic/OneSixInstance.h | 4 | ||||
| -rw-r--r-- | logic/OneSixLibrary.cpp | 98 | ||||
| -rw-r--r-- | logic/OneSixLibrary.h | 26 | ||||
| -rw-r--r-- | logic/OneSixRule.cpp | 66 | ||||
| -rw-r--r-- | logic/OneSixRule.h | 6 | ||||
| -rw-r--r-- | logic/OneSixUpdate.cpp | 85 | ||||
| -rw-r--r-- | logic/OneSixVersion.cpp | 261 | ||||
| -rw-r--r-- | logic/OneSixVersion.h | 60 | ||||
| -rw-r--r-- | logic/OpSys.cpp | 11 | ||||
| -rw-r--r-- | logic/OpSys.h | 1 | ||||
| -rw-r--r-- | logic/VersionFactory.cpp | 196 | ||||
| -rw-r--r-- | logic/VersionFactory.h | 24 | ||||
| -rw-r--r-- | logic/lists/ForgeVersionList.cpp | 118 | ||||
| -rw-r--r-- | logic/lists/ForgeVersionList.h | 52 | ||||
| -rw-r--r-- | logic/net/CacheDownload.cpp | 63 | ||||
| -rw-r--r-- | logic/net/HttpMetaCache.h | 19 | ||||
| -rw-r--r-- | logic/tasks/LoginTask.cpp | 2 |
25 files changed, 873 insertions, 520 deletions
diff --git a/logic/BaseInstance.cpp b/logic/BaseInstance.cpp index 10bb4573..ec86596a 100644 --- a/logic/BaseInstance.cpp +++ b/logic/BaseInstance.cpp @@ -132,7 +132,7 @@ InstanceList *BaseInstance::instList() const return NULL; } -BaseVersionList *BaseInstance::versionList() const +QSharedPointer<BaseVersionList> BaseInstance::versionList() const { return MMC->minecraftlist(); } diff --git a/logic/BaseInstance.h b/logic/BaseInstance.h index fa317ba1..374d1437 100644 --- a/logic/BaseInstance.h +++ b/logic/BaseInstance.h @@ -134,7 +134,7 @@ public: * \brief Gets a pointer to this instance's version list. * \return A pointer to the available version list for this instance. */ - virtual BaseVersionList *versionList() const; + virtual QSharedPointer<BaseVersionList> versionList() const; /*! * \brief Gets this instance's settings object. diff --git a/logic/ForgeInstaller.cpp b/logic/ForgeInstaller.cpp new file mode 100644 index 00000000..bcba00e9 --- /dev/null +++ b/logic/ForgeInstaller.cpp @@ -0,0 +1,135 @@ +#include "ForgeInstaller.h" +#include "OneSixVersion.h" +#include "OneSixLibrary.h" +#include "net/HttpMetaCache.h" +#include <quazip.h> +#include <quazipfile.h> +#include <pathutils.h> +#include <QStringList> +#include "MultiMC.h" + +ForgeInstaller::ForgeInstaller(QString filename, QString universal_url) +{ + QSharedPointer<OneSixVersion> newVersion; + m_universal_url = universal_url; + + QuaZip zip(filename); + if (!zip.open(QuaZip::mdUnzip)) + return; + + QuaZipFile file(&zip); + + // read the install profile + if (!zip.setCurrentFile("install_profile.json")) + return; + + QJsonParseError jsonError; + if (!file.open(QIODevice::ReadOnly)) + return; + QJsonDocument jsonDoc = QJsonDocument::fromJson(file.readAll(), &jsonError); + file.close(); + if (jsonError.error != QJsonParseError::NoError) + return; + + if (!jsonDoc.isObject()) + return; + + QJsonObject root = jsonDoc.object(); + + auto installVal = root.value("install"); + auto versionInfoVal = root.value("versionInfo"); + if (!installVal.isObject() || !versionInfoVal.isObject()) + return; + + // read the forge version info + { + newVersion = OneSixVersion::fromJson(versionInfoVal.toObject()); + if (!newVersion) + return; + } + + QJsonObject installObj = installVal.toObject(); + QString libraryName = installObj.value("path").toString(); + internalPath = installObj.value("filePath").toString(); + + // where do we put the library? decode the mojang path + OneSixLibrary lib(libraryName); + lib.finalize(); + + auto cacheentry = MMC->metacache()->resolveEntry("libraries", lib.storagePath()); + finalPath = "libraries/" + lib.storagePath(); + if (!ensureFilePathExists(finalPath)) + return; + + if (!zip.setCurrentFile(internalPath)) + return; + if (!file.open(QIODevice::ReadOnly)) + return; + { + QByteArray data = file.readAll(); + // extract file + QSaveFile extraction(finalPath); + if (!extraction.open(QIODevice::WriteOnly)) + return; + if (extraction.write(data) != data.size()) + return; + if (!extraction.commit()) + return; + QCryptographicHash md5sum(QCryptographicHash::Md5); + md5sum.addData(data); + + cacheentry->stale = false; + cacheentry->md5sum = md5sum.result().toHex().constData(); + MMC->metacache()->updateEntry(cacheentry); + } + file.close(); + + m_forge_version = newVersion; + realVersionId = m_forge_version->id = installObj.value("minecraft").toString(); +} + +bool ForgeInstaller::apply(QSharedPointer<OneSixVersion> to) +{ + if (!m_forge_version) + return false; + to->externalUpdateStart(); + int sliding_insert_window = 0; + { + // for each library in the version we are adding (except for the blacklisted) + QSet<QString> blacklist{"lwjgl", "lwjgl_util", "lwjgl-platform"}; + for (auto lib : m_forge_version->libraries) + { + QString libName = lib->name(); + // if this is the actual forge lib, set an absolute url for the download + if (libName.contains("minecraftforge")) + { + lib->setAbsoluteUrl(m_universal_url); + } + if (blacklist.contains(libName)) + continue; + + // find an entry that matches this one + bool found = false; + for (auto tolib : to->libraries) + { + if (tolib->name() != libName) + continue; + found = true; + // replace lib + tolib = lib; + break; + } + if (!found) + { + // add lib + to->libraries.insert(sliding_insert_window, lib); + sliding_insert_window++; + } + } + to->mainClass = m_forge_version->mainClass; + to->minecraftArguments = m_forge_version->minecraftArguments; + to->processArguments = m_forge_version->processArguments; + } + to->externalUpdateFinish(); + return to->toOriginalFile(); +} diff --git a/logic/ForgeInstaller.h b/logic/ForgeInstaller.h new file mode 100644 index 00000000..f4ceaaef --- /dev/null +++ b/logic/ForgeInstaller.h @@ -0,0 +1,25 @@ +#pragma once +#include <QString> +#include <QSharedPointer> + +class OneSixVersion; + +class ForgeInstaller +{ +public: + ForgeInstaller(QString filename, QString universal_url); + + bool apply(QSharedPointer<OneSixVersion> to); + +private: + // the version, read from the installer + QSharedPointer<OneSixVersion> m_forge_version; + QString internalPath; + QString finalPath; + QString realVersionId; + QString m_universal_url; +}; + + + + diff --git a/logic/LegacyUpdate.cpp b/logic/LegacyUpdate.cpp index 0f58e3e3..84d3d830 100644 --- a/logic/LegacyUpdate.cpp +++ b/logic/LegacyUpdate.cpp @@ -60,7 +60,7 @@ void LegacyUpdate::lwjglStart() m_reply = QSharedPointer<QNetworkReply> (rep, &QObject::deleteLater); connect(rep, SIGNAL(downloadProgress(qint64,qint64)), SIGNAL(progress(qint64,qint64))); - connect(worker, SIGNAL(finished(QNetworkReply*)), SLOT(lwjglFinished(QNetworkReply*))); + connect(worker.data(), SIGNAL(finished(QNetworkReply*)), SLOT(lwjglFinished(QNetworkReply*))); //connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(downloadError(QNetworkReply::NetworkError))); } @@ -77,7 +77,7 @@ void LegacyUpdate::lwjglFinished(QNetworkReply* reply) "\nSometimes you have to wait a bit if you download many LWJGL versions in a row. YMMV"); return; } - auto *worker = MMC->qnam(); + auto worker = MMC->qnam(); //Here i check if there is a cookie for me in the reply and extract it QList<QNetworkCookie> cookies = qvariant_cast<QList<QNetworkCookie>>(reply->header(QNetworkRequest::SetCookieHeader)); if(cookies.count() != 0) diff --git a/logic/ModList.h b/logic/ModList.h index 5395e9ae..e99b6c82 100644 --- a/logic/ModList.h +++ b/logic/ModList.h @@ -112,3 +112,6 @@ protected: QString m_list_id; QList<Mod> mods; }; + + + diff --git a/logic/NostalgiaInstance.h b/logic/NostalgiaInstance.h index f2df1828..1436e48d 100644 --- a/logic/NostalgiaInstance.h +++ b/logic/NostalgiaInstance.h @@ -9,3 +9,4 @@ public: explicit NostalgiaInstance(const QString &rootDir, SettingsObject * settings, QObject *parent = 0); virtual QString getStatusbarDescription(); }; + diff --git a/logic/OneSixInstance.cpp b/logic/OneSixInstance.cpp index 7b038c46..e22a8890 100644 --- a/logic/OneSixInstance.cpp +++ b/logic/OneSixInstance.cpp @@ -2,7 +2,7 @@ #include "OneSixInstance_p.h" #include "OneSixUpdate.h" #include "MinecraftProcess.h" -#include "VersionFactory.h" +#include "OneSixVersion.h" #include <setting.h> #include <pathutils.h> @@ -10,8 +10,9 @@ #include <JlCompress.h> #include <gui/OneSixModEditDialog.h> -OneSixInstance::OneSixInstance ( const QString& rootDir, SettingsObject* setting_obj, QObject* parent ) -: BaseInstance ( new OneSixInstancePrivate(), rootDir, setting_obj, parent ) +OneSixInstance::OneSixInstance(const QString &rootDir, SettingsObject *setting_obj, + QObject *parent) + : BaseInstance(new OneSixInstancePrivate(), rootDir, setting_obj, parent) { I_D(OneSixInstance); d->m_settings->registerSetting(new Setting("IntendedVersion", "")); @@ -19,7 +20,7 @@ OneSixInstance::OneSixInstance ( const QString& rootDir, SettingsObject* setting reloadFullVersion(); } -BaseUpdate* OneSixInstance::doUpdate() +BaseUpdate *OneSixInstance::doUpdate() { return new OneSixUpdate(this); } @@ -34,10 +35,10 @@ QString replaceTokensIn(QString text, QMap<QString, QString> with) int head = 0; while ((head = token_regexp.indexIn(text, head)) != -1) { - result.append(text.mid(tail, head-tail)); + result.append(text.mid(tail, head - tail)); QString key = token_regexp.cap(1); auto iter = with.find(key); - if(iter != with.end()) + if (iter != with.end()) { result.append(*iter); } @@ -48,26 +49,26 @@ QString replaceTokensIn(QString text, QMap<QString, QString> with) return result; } -QStringList OneSixInstance::processMinecraftArgs( QString user, QString session ) +QStringList OneSixInstance::processMinecraftArgs(QString user, QString session) { I_D(OneSixInstance); auto version = d->version; QString args_pattern = version->minecraftArguments; - + QMap<QString, QString> token_mapping; token_mapping["auth_username"] = user; token_mapping["auth_session"] = session; - //FIXME: user and player name are DIFFERENT! + // FIXME: user and player name are DIFFERENT! token_mapping["auth_player_name"] = user; - //FIXME: WTF is this. I just plugged in a random UUID here. + // FIXME: WTF is this. I just plugged in a random UUID here. token_mapping["auth_uuid"] = "7d4bacf0-fd62-11e2-b778-0800200c9a66"; // obviously fake. - + // this is for offline: /* map["auth_player_name"] = "Player"; map["auth_player_name"] = "00000000-0000-0000-0000-000000000000"; */ - + token_mapping["profile_name"] = name(); token_mapping["version_name"] = version->id; @@ -75,8 +76,8 @@ QStringList OneSixInstance::processMinecraftArgs( QString user, QString session token_mapping["game_directory"] = absRootDir; QString absAssetsDir = QDir("assets/").absolutePath(); token_mapping["game_assets"] = absAssetsDir; - - QStringList parts = args_pattern.split(' ',QString::SkipEmptyParts); + + QStringList parts = args_pattern.split(' ', QString::SkipEmptyParts); for (int i = 0; i < parts.length(); i++) { parts[i] = replaceTokensIn(parts[i], token_mapping); @@ -84,27 +85,28 @@ QStringList OneSixInstance::processMinecraftArgs( QString user, QString session return parts; } -MinecraftProcess* OneSixInstance::prepareForLaunch ( QString user, QString session ) +MinecraftProcess *OneSixInstance::prepareForLaunch(QString user, QString session) { I_D(OneSixInstance); cleanupAfterRun(); auto version = d->version; - if(!version) + if (!version) return nullptr; auto libs_to_extract = version->getActiveNativeLibs(); QString natives_dir_raw = PathCombine(instanceRoot(), "natives/"); bool success = ensureFolderPathExists(natives_dir_raw); - if(!success) + if (!success) { // FIXME: handle errors return nullptr; } - - for(auto lib: libs_to_extract) + + for (auto lib : libs_to_extract) { QString path = "libraries/" + lib->storagePath(); qDebug() << "Will extract " << path.toLocal8Bit(); - if(JlCompress::extractWithExceptions(path, natives_dir_raw, lib->extract_excludes).isEmpty()) + if (JlCompress::extractWithExceptions(path, natives_dir_raw, lib->extract_excludes) + .isEmpty()) { return nullptr; } @@ -116,11 +118,11 @@ MinecraftProcess* OneSixInstance::prepareForLaunch ( QString user, QString sessi args << QString("-Xmx%1m").arg(settings().get("MaxMemAlloc").toInt()); args << QString("-XX:PermSize=%1m").arg(settings().get("PermGen").toInt()); QDir natives_dir(natives_dir_raw); - args << QString("-Djava.library.path=%1").arg( natives_dir.absolutePath() ); + args << QString("-Djava.library.path=%1").arg(natives_dir.absolutePath()); QString classPath; { auto libs = version->getActiveNormalLibs(); - for (auto lib: libs) + for (auto lib : libs) { QFileInfo fi(QString("libraries/") + lib->storagePath()); classPath.append(fi.absoluteFilePath()); @@ -134,16 +136,16 @@ MinecraftProcess* OneSixInstance::prepareForLaunch ( QString user, QString sessi QFileInfo fi(targetstr); classPath.append(fi.absoluteFilePath()); } - if(classPath.size()) + if (classPath.size()) { args << "-cp"; args << classPath; } args << version->mainClass; args.append(processMinecraftArgs(user, session)); - + // create the process and set its parameters - MinecraftProcess * proc = new MinecraftProcess(this); + MinecraftProcess *proc = new MinecraftProcess(this); proc->setMinecraftArguments(args); proc->setMinecraftWorkdir(minecraftRoot()); return proc; @@ -156,10 +158,10 @@ void OneSixInstance::cleanupAfterRun() dir.removeRecursively(); } -QSharedPointer< ModList > OneSixInstance::loaderModList() +QSharedPointer<ModList> OneSixInstance::loaderModList() { I_D(OneSixInstance); - if(!d->loader_mod_list) + if (!d->loader_mod_list) { d->loader_mod_list.reset(new ModList(loaderModsDir())); } @@ -168,10 +170,10 @@ QSharedPointer< ModList > OneSixInstance::loaderModList() return d->loader_mod_list; } -QSharedPointer< ModList > OneSixInstance::resourcePackList() +QSharedPointer<ModList> OneSixInstance::resourcePackList() { I_D(OneSixInstance); - if(!d->resource_pack_list) + if (!d->resource_pack_list) { d->resource_pack_list.reset(new ModList(resourcePacksDir())); } @@ -180,13 +182,12 @@ QSharedPointer< ModList > OneSixInstance::resourcePackList() return d->resource_pack_list; } - -QDialog * OneSixInstance::createModEditDialog ( QWidget* parent ) +QDialog *OneSixInstance::createModEditDialog(QWidget *parent) { return new OneSixModEditDialog(this, parent); } -bool OneSixInstance::setIntendedVersionId ( QString version ) +bool OneSixInstance::setIntendedVersionId(QString version) { settings().set("IntendedVersion", version); setShouldUpdate(true); @@ -198,16 +199,16 @@ QString OneSixInstance::intendedVersionId() const return settings().get("IntendedVersion").toString(); } -void OneSixInstance::setShouldUpdate ( bool val ) +void OneSixInstance::setShouldUpdate(bool val) { - settings().set ( "ShouldUpdate", val ); + settings().set("ShouldUpdate", val); } bool OneSixInstance::shouldUpdate() const { I_D(OneSixInstance); - QVariant var = settings().get ( "ShouldUpdate" ); - if ( !var.isValid() || var.toBool() == false ) + QVariant var = settings().get("ShouldUpdate"); + if (!var.isValid() || var.toBool() == false) { return intendedVersionId() != currentVersionId(); } @@ -226,34 +227,53 @@ QString OneSixInstance::currentVersionId() const return intendedVersionId(); } +bool OneSixInstance::customizeVersion() +{ + if (!versionIsCustom()) + { + auto pathCustom = PathCombine(instanceRoot(), "custom.json"); + auto pathOrig = PathCombine(instanceRoot(), "version.json"); + QFile::copy(pathOrig, pathCustom); + return reloadFullVersion(); + } + else + return true; +} + +bool OneSixInstance::revertCustomVersion() +{ + if (versionIsCustom()) + { + auto path = PathCombine(instanceRoot(), "custom.json"); + QFile::remove(path); + return reloadFullVersion(); + } + else + return true; +} + bool OneSixInstance::reloadFullVersion() { I_D(OneSixInstance); - + QString verpath = PathCombine(instanceRoot(), "version.json"); { QString verpath_custom = PathCombine(instanceRoot(), "custom.json"); QFile versionfile(verpath_custom); - if(versionfile.exists()) + if (versionfile.exists()) verpath = verpath_custom; } - - QFile versionfile(verpath); - if(versionfile.exists() && versionfile.open(QIODevice::ReadOnly)) + + auto version = OneSixVersion::fromFile(verpath); + if (version) { - FullVersionFactory fvf; - auto version = fvf.parse(versionfile.readAll()); - versionfile.close(); - if(version) - { - d->version = version; - return true; - } - }; + d->version = version; + return true; + } return false; } -QSharedPointer< OneSixVersion > OneSixInstance::getFullVersion() +QSharedPointer<OneSixVersion> OneSixInstance::getFullVersion() { I_D(OneSixInstance); return d->version; @@ -269,9 +289,9 @@ QString OneSixInstance::defaultCustomBaseJar() const return PathCombine(instanceRoot(), "custom.jar"); } -bool OneSixInstance::menuActionEnabled ( QString action_name ) const +bool OneSixInstance::menuActionEnabled(QString action_name) const { - if(action_name == "actionChangeInstLWJGLVersion") + if (action_name == "actionChangeInstLWJGLVersion") return false; return true; } @@ -279,7 +299,7 @@ bool OneSixInstance::menuActionEnabled ( QString action_name ) const QString OneSixInstance::getStatusbarDescription() { QString descr = "One Six : " + intendedVersionId(); - if(versionIsCustom()) + if (versionIsCustom()) { descr + " (custom)"; } @@ -300,3 +320,4 @@ QString OneSixInstance::instanceConfigFolder() const { return PathCombine(minecraftRoot(), "config"); } + diff --git a/logic/OneSixInstance.h b/logic/OneSixInstance.h index 72bde630..0139b645 100644 --- a/logic/OneSixInstance.h +++ b/logic/OneSixInstance.h @@ -41,6 +41,10 @@ public: bool reloadFullVersion(); /// get the current full version info QSharedPointer<OneSixVersion> getFullVersion(); + /// revert the current custom version back to base + bool revertCustomVersion(); + /// customize the current base version + bool customizeVersion(); /// is the current version original, or custom? bool versionIsCustom(); diff --git a/logic/OneSixLibrary.cpp b/logic/OneSixLibrary.cpp index a45a4aec..8da1fde7 100644 --- a/logic/OneSixLibrary.cpp +++ b/logic/OneSixLibrary.cpp @@ -1,18 +1,19 @@ #include "OneSixLibrary.h" #include "OneSixRule.h" #include "OpSys.h" +#include <QJsonArray> void OneSixLibrary::finalize() { - QStringList parts = m_name.split ( ':' ); + QStringList parts = m_name.split(':'); QString relative = parts[0]; - relative.replace ( '.','/' ); + relative.replace('.', '/'); relative += '/' + parts[1] + '/' + parts[2] + '/' + parts[1] + '-' + parts[2]; - - if ( !m_is_native ) + + if (!m_is_native) relative += ".jar"; else { - if ( m_native_suffixes.contains ( currentSystem ) ) + if (m_native_suffixes.contains(currentSystem)) { relative += "-" + m_native_suffixes[currentSystem] + ".jar"; } @@ -22,30 +23,30 @@ void OneSixLibrary::finalize() relative += ".jar"; } } - + m_decentname = parts[1]; m_decentversion = parts[2]; m_storage_path = relative; - m_download_path = m_base_url + relative; - - if ( m_rules.empty() ) + m_download_url = m_base_url + relative; + + if (m_rules.empty()) { m_is_active = true; } else { RuleAction result = Disallow; - for ( auto rule: m_rules ) + for (auto rule : m_rules) { - RuleAction temp = rule->apply ( this ); - if ( temp != Defer ) + RuleAction temp = rule->apply(this); + if (temp != Defer) result = temp; } - m_is_active = ( result == Allow ); + m_is_active = (result == Allow); } - if ( m_is_native ) + if (m_is_native) { - m_is_active = m_is_active && m_native_suffixes.contains ( currentSystem ); + m_is_active = m_is_active && m_native_suffixes.contains(currentSystem); m_decenttype = "Native"; } else @@ -54,11 +55,11 @@ void OneSixLibrary::finalize() } } -void OneSixLibrary::setName ( QString name ) +void OneSixLibrary::setName(QString name) { m_name = name; } -void OneSixLibrary::setBaseUrl ( QString base_url ) +void OneSixLibrary::setBaseUrl(QString base_url) { m_base_url = base_url; } @@ -66,12 +67,12 @@ void OneSixLibrary::setIsNative() { m_is_native = true; } -void OneSixLibrary::addNative ( OpSys os, QString suffix ) +void OneSixLibrary::addNative(OpSys os, QString suffix) { m_is_native = true; m_native_suffixes[os] = suffix; } -void OneSixLibrary::setRules ( QList< QSharedPointer< Rule > > rules ) +void OneSixLibrary::setRules(QList<QSharedPointer<Rule>> rules) { m_rules = rules; } @@ -83,11 +84,66 @@ bool OneSixLibrary::isNative() { return m_is_native; } -QString OneSixLibrary::downloadPath() +QString OneSixLibrary::downloadUrl() { - return m_download_path; + if(m_absolute_url.size()) + return m_absolute_url; + return m_download_url; } QString OneSixLibrary::storagePath() { return m_storage_path; } + +void OneSixLibrary::setAbsoluteUrl(QString absolute_url) +{ + m_absolute_url = absolute_url; +} + +QString OneSixLibrary::absoluteUrl() +{ + return m_absolute_url; +} + +QJsonObject OneSixLibrary::toJson() +{ + QJsonObject libRoot; + libRoot.insert("name", m_name); + if(m_absolute_url.size()) + libRoot.insert("MMC-absulute_url", m_absolute_url); + if(m_base_url != "https://s3.amazonaws.com/Minecraft.Download/libraries/") + libRoot.insert("url", m_base_url); + if (isNative() && m_native_suffixes.size()) + { + QJsonObject nativeList; + auto iter = m_native_suffixes.begin(); + while (iter != m_native_suffixes.end()) + { + nativeList.insert(OpSys_toString(iter.key()), iter.value()); + iter++; + } + libRoot.insert("natives", nativeList); + } + if (isNative() && extract_excludes.size()) + { + QJsonArray excludes; + QJsonObject extract; + for (auto exclude : extract_excludes) + { + excludes.append(exclude); + } + extract.insert("exclude", excludes); + libRoot.insert("extract", extract); + } + if (m_rules.size()) + { + QJsonArray allRules; + for (auto &rule : m_rules) + { + QJsonObject ruleObj = rule->toJson(); + allRules.append(ruleObj); + } + libRoot.insert("rules", allRules); + } + return libRoot; +} diff --git a/logic/OneSixLibrary.h b/logic/OneSixLibrary.h index ac16d3d3..f3106483 100644 --- a/logic/OneSixLibrary.h +++ b/logic/OneSixLibrary.h @@ -3,6 +3,7 @@ #include <QStringList> #include <QMap> #include <QSharedPointer> +#include <QJsonObject> #include "OpSys.h" class Rule; @@ -12,9 +13,13 @@ class OneSixLibrary private: // basic values used internally (so far) QString m_name; - QString m_base_url; + QString m_base_url = "https://s3.amazonaws.com/Minecraft.Download/libraries/"; QList<QSharedPointer<Rule> > m_rules; - + + // custom values + /// absolute URL. takes precedence over m_download_path, if defined + QString m_absolute_url; + // derived values used for real things /// a decent name fit for display QString m_decentname; @@ -25,11 +30,11 @@ private: /// where to store the lib locally QString m_storage_path; /// where to download the lib from - QString m_download_path; + QString m_download_url; /// is this lib actually active on the current OS? - bool m_is_active; + bool m_is_active = false; /// is the library a native? - bool m_is_native; + bool m_is_native = false; /// native suffixes per OS QMap<OpSys, QString> m_native_suffixes; public: @@ -39,12 +44,11 @@ public: /// Constructor OneSixLibrary(QString name) { - m_is_native = false; - m_is_active = false; m_name = name; - m_base_url = "https://s3.amazonaws.com/Minecraft.Download/libraries/"; } + QJsonObject toJson(); + /** * finalize the library, processing the input values into derived values and state * @@ -84,7 +88,11 @@ public: /// Returns true if the library is native bool isNative(); /// Get the URL to download the library from - QString downloadPath(); + QString downloadUrl(); /// Get the relative path where the library should be saved QString storagePath(); + + /// set an absolute URL for the library. This is an MMC extension. + void setAbsoluteUrl(QString absolute_url); + QString absoluteUrl(); }; diff --git a/logic/OneSixRule.cpp b/logic/OneSixRule.cpp index 85f7d434..545cd641 100644 --- a/logic/OneSixRule.cpp +++ b/logic/OneSixRule.cpp @@ -1,10 +1,72 @@ #include "OneSixRule.h" +#include <QJsonObject> +#include <QJsonArray> + +QList<QSharedPointer<Rule>> rulesFromJsonV4(QJsonObject &objectWithRules) +{ + QList<QSharedPointer<Rule>> rules; + auto rulesVal = objectWithRules.value("rules"); + if (!rulesVal.isArray()) + return rules; + + 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()) + { + // add a new implicit action rule + rules.append(ImplicitRule::create(action)); + continue; + } + + 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(); + // add a new OS rule + rules.append(OsRule::create(action, requiredOs, versionRegex)); + } +} + +QJsonObject ImplicitRule::toJson() +{ + QJsonObject ruleObj; + ruleObj.insert("action", m_result == Allow ? QString("allow") : QString("disallow")); + return ruleObj; +} + +QJsonObject OsRule::toJson() +{ + QJsonObject ruleObj; + ruleObj.insert("action", m_result == Allow ? QString("allow") : QString("disallow")); + QJsonObject osObj; + { + osObj.insert("name", OpSys_toString(m_system)); + osObj.insert("version", m_version_regexp); + } + ruleObj.insert("os", osObj); + return ruleObj; +} RuleAction RuleAction_fromString(QString name) { - if(name == "allow") + if (name == "allow") return Allow; - if(name == "disallow") + if (name == "disallow") return Disallow; return Defer; }
\ No newline at end of file diff --git a/logic/OneSixRule.h b/logic/OneSixRule.h index 465c963f..23d20ff4 100644 --- a/logic/OneSixRule.h +++ b/logic/OneSixRule.h @@ -1,8 +1,6 @@ #pragma once #include <QString> #include <QSharedPointer> - -class OneSixLibrary; #include "OneSixLibrary.h" |
