From 0a592ab99bc68ad918f74206edf2a77cc257683c Mon Sep 17 00:00:00 2001 From: Jan Dalheimer Date: Wed, 22 Jan 2014 15:20:48 +0100 Subject: [PATCH] Work towards liteloader support. Fix creating new instance --- gui/dialogs/DerpModEditDialog.cpp | 8 +- logic/DerpInstance.cpp | 22 ++++-- logic/DerpInstance.h | 2 + logic/DerpVersion.cpp | 15 ++++ logic/DerpVersion.h | 1 + logic/DerpVersionBuilder.cpp | 18 +---- logic/LiteLoaderInstaller.cpp | 123 +++++++++++++++++------------- logic/LiteLoaderInstaller.h | 18 +++-- 8 files changed, 123 insertions(+), 84 deletions(-) diff --git a/gui/dialogs/DerpModEditDialog.cpp b/gui/dialogs/DerpModEditDialog.cpp index be17404c..aafaa2e2 100644 --- a/gui/dialogs/DerpModEditDialog.cpp +++ b/gui/dialogs/DerpModEditDialog.cpp @@ -96,7 +96,7 @@ void DerpModEditDialog::updateVersionControls() { bool customVersion = m_inst->versionIsCustom(); ui->forgeBtn->setEnabled(true); - ui->liteloaderBtn->setEnabled(LiteLoaderInstaller(m_inst->intendedVersionId()).canApply()); + ui->liteloaderBtn->setEnabled(LiteLoaderInstaller().canApply(m_inst)); ui->customEditorBtn->setEnabled(customVersion); } @@ -165,8 +165,8 @@ void DerpModEditDialog::on_forgeBtn_clicked() void DerpModEditDialog::on_liteloaderBtn_clicked() { - LiteLoaderInstaller liteloader(m_inst->intendedVersionId()); - if (!liteloader.canApply()) + LiteLoaderInstaller liteloader; + if (!liteloader.canApply(m_inst)) { QMessageBox::critical( this, tr("LiteLoader"), @@ -174,7 +174,7 @@ void DerpModEditDialog::on_liteloaderBtn_clicked() "into this version of Minecraft")); return; } - if (!liteloader.apply(m_version)) + if (!liteloader.add(m_inst)) { QMessageBox::critical( this, tr("LiteLoader"), diff --git a/logic/DerpInstance.cpp b/logic/DerpInstance.cpp index 672ba6c4..e6b92b1f 100644 --- a/logic/DerpInstance.cpp +++ b/logic/DerpInstance.cpp @@ -35,10 +35,16 @@ DerpInstance::DerpInstance(const QString &rootDir, SettingsObject *settings, QOb d->m_settings->registerSetting("IntendedVersion", ""); d->m_settings->registerSetting("ShouldUpdate", false); d->version.reset(new DerpVersion(this, this)); - reloadFullVersion(); + if (QDir(instanceRoot()).exists("version.json")) + { + reloadFullVersion(); + } + else + { + clearFullVersion(); + } } - std::shared_ptr DerpInstance::doUpdate(bool only_prepare) { return std::shared_ptr(new DerpUpdate(this, only_prepare)); @@ -280,9 +286,8 @@ bool DerpInstance::setIntendedVersionId(QString version) { settings().set("IntendedVersion", version); setShouldUpdate(true); - auto pathOrig = PathCombine(instanceRoot(), "version.json"); - QFile::remove(pathOrig); - reloadFullVersion(); + QFile::remove(PathCombine(instanceRoot(), "version.json")); + clearFullVersion(); return true; } @@ -327,6 +332,13 @@ bool DerpInstance::reloadFullVersion(QWidget *widgetParent) return ret; } +void DerpInstance::clearFullVersion() +{ + I_D(DerpInstance); + d->version->clear(); + emit versionReloaded(); +} + std::shared_ptr DerpInstance::getFullVersion() { I_D(DerpInstance); diff --git a/logic/DerpInstance.h b/logic/DerpInstance.h index 37d3df52..46f953c0 100644 --- a/logic/DerpInstance.h +++ b/logic/DerpInstance.h @@ -53,6 +53,8 @@ public: /// reload the full version json files. return true on success! bool reloadFullVersion(QWidget *widgetParent = 0); + /// clears all version information in preparation for an update + void clearFullVersion(); /// get the current full version info std::shared_ptr getFullVersion(); /// is the current version original, or custom? diff --git a/logic/DerpVersion.cpp b/logic/DerpVersion.cpp index f13ac620..cabb2b24 100644 --- a/logic/DerpVersion.cpp +++ b/logic/DerpVersion.cpp @@ -22,6 +22,7 @@ DerpVersion::DerpVersion(DerpInstance *instance, QObject *parent) : QAbstractListModel(parent), m_instance(instance) { + clear(); } bool DerpVersion::reload(QWidget *widgetParent) @@ -29,6 +30,20 @@ bool DerpVersion::reload(QWidget *widgetParent) return DerpVersionBuilder::build(this, m_instance, widgetParent); } +void DerpVersion::clear() +{ + id.clear(); + time.clear(); + releaseTime.clear(); + type.clear(); + assets.clear(); + processArguments.clear(); + minecraftArguments.clear(); + minimumLauncherVersion = 0xDEADBEAF; + mainClass.clear(); + libraries.clear(); +} + QList > DerpVersion::getActiveNormalLibs() { QList > output; diff --git a/logic/DerpVersion.h b/logic/DerpVersion.h index cadfa850..f2132ad8 100644 --- a/logic/DerpVersion.h +++ b/logic/DerpVersion.h @@ -39,6 +39,7 @@ public: virtual Qt::ItemFlags flags(const QModelIndex &index) const; bool reload(QWidget *widgetParent); + void clear(); public: QList> getActiveNormalLibs(); diff --git a/logic/DerpVersionBuilder.cpp b/logic/DerpVersionBuilder.cpp index d8091f32..b0da8205 100644 --- a/logic/DerpVersionBuilder.cpp +++ b/logic/DerpVersionBuilder.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include "DerpVersion.h" #include "DerpInstance.h" @@ -45,7 +46,7 @@ bool DerpVersionBuilder::build(DerpVersion *version, DerpInstance *instance, QWi bool DerpVersionBuilder::build() { - clear(); + m_version->clear(); QDir root(m_instance->instanceRoot()); QDir patches(root.absoluteFilePath("patches/")); @@ -112,20 +113,6 @@ bool DerpVersionBuilder::build() return true; } -void DerpVersionBuilder::clear() -{ - m_version->id.clear(); - m_version->time.clear(); - m_version->releaseTime.clear(); - m_version->type.clear(); - m_version->assets.clear(); - m_version->processArguments.clear(); - m_version->minecraftArguments.clear(); - m_version->minimumLauncherVersion = 0xDEADBEAF; - m_version->mainClass.clear(); - m_version->libraries.clear(); -} - void applyString(const QJsonObject &obj, const QString &key, QString &out) { if (obj.contains(key) && obj.value(key).isString()) @@ -181,6 +168,7 @@ bool DerpVersionBuilder::apply(const QJsonObject &object) } // libraries + if (object.contains("libraries")) { auto librariesValue = object.value("libraries"); if (!librariesValue.isArray()) diff --git a/logic/LiteLoaderInstaller.cpp b/logic/LiteLoaderInstaller.cpp index 8a120eab..8aed47e7 100644 --- a/logic/LiteLoaderInstaller.cpp +++ b/logic/LiteLoaderInstaller.cpp @@ -15,12 +15,18 @@ #include "LiteLoaderInstaller.h" +#include +#include + +#include "logger/QsLog.h" + #include "DerpVersion.h" #include "DerpLibrary.h" +#include "DerpInstance.h" QMap LiteLoaderInstaller::m_launcherWrapperVersionMapping; -LiteLoaderInstaller::LiteLoaderInstaller(const QString &mcVersion) : m_mcVersion(mcVersion) +LiteLoaderInstaller::LiteLoaderInstaller() { if (m_launcherWrapperVersionMapping.isEmpty()) { @@ -31,71 +37,82 @@ LiteLoaderInstaller::LiteLoaderInstaller(const QString &mcVersion) : m_mcVersion } } -bool LiteLoaderInstaller::canApply() const +bool LiteLoaderInstaller::canApply(DerpInstance *instance) const { - return m_launcherWrapperVersionMapping.contains(m_mcVersion); + return m_launcherWrapperVersionMapping.contains(instance->intendedVersionId()); } -bool LiteLoaderInstaller::apply(std::shared_ptr to) +bool LiteLoaderInstaller::isApplied(DerpInstance *on) { - // DERPFIX + return QFile::exists(filename(on->instanceRoot())); +} - applyLaunchwrapper(to); - applyLiteLoader(to); - - to->mainClass = "net.minecraft.launchwrapper.Launch"; - if (!to->minecraftArguments.contains( - " --tweakClass com.mumfrey.liteloader.launch.LiteLoaderTweaker")) +bool LiteLoaderInstaller::add(DerpInstance *to) +{ + if (!patchesDir(to->instanceRoot()).exists()) { - to->minecraftArguments.append( - " --tweakClass com.mumfrey.liteloader.launch.LiteLoaderTweaker"); + QDir(to->instanceRoot()).mkdir("patches"); } + if (isApplied(to)) + { + if (!remove(to)) + { + return false; + } + } + + QJsonObject obj; + + obj.insert("mainClass", QString("net.minecraft.launchwrapper.Launch")); + obj.insert("+minecraftArguments", QString(" --tweakClass com.mumfrey.liteloader.launch.LiteLoaderTweaker")); + obj.insert("order", 10); + + QJsonArray libraries; + + // launchwrapper + { + DerpLibrary launchwrapperLib("net.minecraft:launchwrapper:" + m_launcherWrapperVersionMapping[to->intendedVersionId()]); + launchwrapperLib.finalize(); + QJsonObject lwLibObj = launchwrapperLib.toJson(); + lwLibObj.insert("insert", QString("beginning")); + libraries.append(lwLibObj); + } + + // liteloader + { + DerpLibrary liteloaderLib("com.mumfrey:liteloader:" + to->intendedVersionId()); + liteloaderLib.setBaseUrl("http://dl.liteloader.com/versions/"); + liteloaderLib.finalize(); + QJsonObject llLibObj = liteloaderLib.toJson(); + llLibObj.insert("insert", QString("beginning")); + libraries.append(llLibObj); + } + + obj.insert("+libraries", libraries); + + QFile file(filename(to->instanceRoot())); + if (!file.open(QFile::WriteOnly)) + { + QLOG_ERROR() << "Error opening" << file.fileName() << "for reading:" << file.errorString(); + return false; + } + file.write(QJsonDocument(obj).toJson()); + file.close(); + return true; } -void LiteLoaderInstaller::applyLaunchwrapper(std::shared_ptr to) +bool LiteLoaderInstaller::remove(DerpInstance *from) { - const QString intendedVersion = m_launcherWrapperVersionMapping[m_mcVersion]; - - QMutableListIterator> it(to->libraries); - while (it.hasNext()) - { - it.next(); - if (it.value()->rawName().startsWith("net.minecraft:launchwrapper:")) - { - if (it.value()->version() >= intendedVersion) - { - return; - } - else - { - it.remove(); - } - } - } - - std::shared_ptr lib(new DerpLibrary( - "net.minecraft:launchwrapper:" + m_launcherWrapperVersionMapping[m_mcVersion])); - lib->finalize(); - to->libraries.prepend(lib); + return QFile::remove(filename(from->instanceRoot())); } -void LiteLoaderInstaller::applyLiteLoader(std::shared_ptr to) +QString LiteLoaderInstaller::filename(const QString &root) const { - QMutableListIterator> it(to->libraries); - while (it.hasNext()) - { - it.next(); - if (it.value()->rawName().startsWith("com.mumfrey:liteloader:")) - { - it.remove(); - } - } - - std::shared_ptr lib( - new DerpLibrary("com.mumfrey:liteloader:" + m_mcVersion)); - lib->setBaseUrl("http://dl.liteloader.com/versions/"); - lib->finalize(); - to->libraries.prepend(lib); + return patchesDir(root).absoluteFilePath(id() + ".json"); +} +QDir LiteLoaderInstaller::patchesDir(const QString &root) const +{ + return QDir(root + "/patches/"); } diff --git a/logic/LiteLoaderInstaller.h b/logic/LiteLoaderInstaller.h index d7b8ce77..48ef4baf 100644 --- a/logic/LiteLoaderInstaller.h +++ b/logic/LiteLoaderInstaller.h @@ -19,21 +19,25 @@ #include class DerpVersion; +class DerpInstance; +class QDir; +// TODO base class class LiteLoaderInstaller { public: - LiteLoaderInstaller(const QString &mcVersion); + LiteLoaderInstaller(); - bool canApply() const; + bool canApply(DerpInstance *instance) const; + bool isApplied(DerpInstance *on); - bool apply(std::shared_ptr to); + bool add(DerpInstance *to); + bool remove(DerpInstance *from); private: - QString m_mcVersion; - - void applyLaunchwrapper(std::shared_ptr to); - void applyLiteLoader(std::shared_ptr to); + virtual QString id() const { return "com.mumfrey.liteloader"; } + QString filename(const QString &root) const; + QDir patchesDir(const QString &root) const; static QMap m_launcherWrapperVersionMapping; };