From 156bc8f27656c115bf1b023cd4ebc5f629df3887 Mon Sep 17 00:00:00 2001 From: Jan Dalheimer Date: Thu, 23 Jan 2014 21:31:41 +0100 Subject: [PATCH] Forge works now too, and so does forge+liteloader --- CMakeLists.txt | 2 + gui/dialogs/DerpModEditDialog.cpp | 16 +++++--- logic/BaseInstaller.cpp | 66 +++++++++++++++++++++++++++++++ logic/BaseInstaller.h | 39 ++++++++++++++++++ logic/DerpFTBInstance.cpp | 2 +- logic/DerpVersion.cpp | 47 ++++++++++++++-------- logic/DerpVersion.h | 4 ++ logic/DerpVersionBuilder.cpp | 37 ++++++++++++++++- logic/DerpVersionBuilder.h | 2 + logic/ForgeInstaller.cpp | 48 ++++++++++++++++------ logic/ForgeInstaller.h | 9 ++++- logic/LiteLoaderInstaller.cpp | 32 ++------------- logic/LiteLoaderInstaller.h | 22 ++++------- 13 files changed, 245 insertions(+), 81 deletions(-) create mode 100644 logic/BaseInstaller.cpp create mode 100644 logic/BaseInstaller.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 43de626b..c5339a30 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -416,6 +416,8 @@ logic/DerpRule.h logic/DerpRule.cpp logic/OpSys.h logic/OpSys.cpp +logic/BaseInstaller.h +logic/BaseInstaller.cpp logic/ForgeInstaller.h logic/ForgeInstaller.cpp logic/LiteLoaderInstaller.h diff --git a/gui/dialogs/DerpModEditDialog.cpp b/gui/dialogs/DerpModEditDialog.cpp index aafaa2e2..216aa59d 100644 --- a/gui/dialogs/DerpModEditDialog.cpp +++ b/gui/dialogs/DerpModEditDialog.cpp @@ -55,7 +55,6 @@ DerpModEditDialog::DerpModEditDialog(DerpInstance *inst, QWidget *parent) main_model->setSourceModel(m_version.get()); ui->libraryTreeView->setModel(main_model); ui->libraryTreeView->installEventFilter(this); - ui->mainClassEdit->setText(m_version->mainClass); updateVersionControls(); } else @@ -98,6 +97,7 @@ void DerpModEditDialog::updateVersionControls() ui->forgeBtn->setEnabled(true); ui->liteloaderBtn->setEnabled(LiteLoaderInstaller().canApply(m_inst)); ui->customEditorBtn->setEnabled(customVersion); + ui->mainClassEdit->setText(m_version->mainClass); } void DerpModEditDialog::disableVersionControls() @@ -105,6 +105,7 @@ void DerpModEditDialog::disableVersionControls() ui->forgeBtn->setEnabled(false); ui->liteloaderBtn->setEnabled(false); ui->customEditorBtn->setEnabled(false); + ui->mainClassEdit->setText(""); } void DerpModEditDialog::on_customEditorBtn_clicked() @@ -140,9 +141,9 @@ void DerpModEditDialog::on_forgeBtn_clicked() // install QString forgePath = entry->getFullPath(); ForgeInstaller forge(forgePath, forgeVersion->universal_url); - if (!forge.apply(m_version)) + if (!forge.add(m_inst)) { - // failure notice + QLOG_ERROR() << "Failure installing forge"; } } else @@ -155,12 +156,13 @@ void DerpModEditDialog::on_forgeBtn_clicked() // install QString forgePath = entry->getFullPath(); ForgeInstaller forge(forgePath, forgeVersion->universal_url); - if (!forge.apply(m_version)) + if (!forge.add(m_inst)) { - // failure notice + QLOG_ERROR() << "Failure installing forge"; } } } + m_inst->reloadFullVersion(this); } void DerpModEditDialog::on_liteloaderBtn_clicked() @@ -180,6 +182,10 @@ void DerpModEditDialog::on_liteloaderBtn_clicked() this, tr("LiteLoader"), tr("For reasons unknown, the LiteLoader installation failed. Check your MultiMC log files for details.")); } + else + { + m_inst->reloadFullVersion(this); + } } bool DerpModEditDialog::loaderListFilter(QKeyEvent *keyEvent) diff --git a/logic/BaseInstaller.cpp b/logic/BaseInstaller.cpp new file mode 100644 index 00000000..74a8e909 --- /dev/null +++ b/logic/BaseInstaller.cpp @@ -0,0 +1,66 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "BaseInstaller.h" + +#include + +#include "DerpVersion.h" +#include "DerpLibrary.h" +#include "DerpInstance.h" + +#include "cmdutils.h" + +BaseInstaller::BaseInstaller() +{ + +} + +bool BaseInstaller::isApplied(DerpInstance *on) +{ + return QFile::exists(filename(on->instanceRoot())); +} + +bool BaseInstaller::add(DerpInstance *to) +{ + if (!patchesDir(to->instanceRoot()).exists()) + { + QDir(to->instanceRoot()).mkdir("patches"); + } + + if (isApplied(to)) + { + if (!remove(to)) + { + return false; + } + } + + return true; +} + +bool BaseInstaller::remove(DerpInstance *from) +{ + return QFile::remove(filename(from->instanceRoot())); +} + +QString BaseInstaller::filename(const QString &root) const +{ + return patchesDir(root).absoluteFilePath(id() + ".json"); +} +QDir BaseInstaller::patchesDir(const QString &root) const +{ + return QDir(root + "/patches/"); +} diff --git a/logic/BaseInstaller.h b/logic/BaseInstaller.h new file mode 100644 index 00000000..08f78bfc --- /dev/null +++ b/logic/BaseInstaller.h @@ -0,0 +1,39 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +class DerpInstance; +class QDir; +class QString; + +class BaseInstaller +{ +public: + BaseInstaller(); + + virtual bool canApply(DerpInstance *instance) const { return true; } + bool isApplied(DerpInstance *on); + + virtual bool add(DerpInstance *to); + virtual bool remove(DerpInstance *from); + +protected: + virtual QString id() const = 0; + QString filename(const QString &root) const; + QDir patchesDir(const QString &root) const; +}; diff --git a/logic/DerpFTBInstance.cpp b/logic/DerpFTBInstance.cpp index f9aeeca0..9b687461 100644 --- a/logic/DerpFTBInstance.cpp +++ b/logic/DerpFTBInstance.cpp @@ -61,7 +61,7 @@ slots: return; } auto version = instance->getFullVersion(); - if (!forge.apply(version)) + if (!forge.add(instance)) { emitFailed(tr("Couldn't install Forge")); return; diff --git a/logic/DerpVersion.cpp b/logic/DerpVersion.cpp index cabb2b24..f4a8dd97 100644 --- a/logic/DerpVersion.cpp +++ b/logic/DerpVersion.cpp @@ -44,6 +44,26 @@ void DerpVersion::clear() libraries.clear(); } +void DerpVersion::dump() const +{ + qDebug().nospace() << "DerpVersion(" + << "\n\tid=" << id + << "\n\ttime=" << time + << "\n\treleaseTime=" << releaseTime + << "\n\ttype=" << type + << "\n\tassets=" << assets + << "\n\tprocessArguments=" << processArguments + << "\n\tminecraftArguments=" << minecraftArguments + << "\n\tminimumLauncherVersion=" << minimumLauncherVersion + << "\n\tmainClass=" << mainClass + << "\n\tlibraries="; + for (auto lib : libraries) + { + qDebug().nospace() << "\n\t\t" << lib.get(); + } + qDebug().nospace() << "\n)"; +} + QList > DerpVersion::getActiveNormalLibs() { QList > output; @@ -70,6 +90,16 @@ QList > DerpVersion::getActiveNativeLibs() return output; } +std::shared_ptr DerpVersion::fromJson(const QJsonObject &obj) +{ + std::shared_ptr version(new DerpVersion(0)); + if (DerpVersionBuilder::read(version.get(), obj)) + { + return version; + } + return 0; +} + QVariant DerpVersion::data(const QModelIndex &index, int role) const { if (!index.isValid()) @@ -143,22 +173,7 @@ int DerpVersion::columnCount(const QModelIndex &parent) const QDebug operator<<(QDebug &dbg, const DerpVersion *version) { - dbg.nospace() << "DerpVersion(" - << "\n\tid=" << version->id - << "\n\ttime=" << version->time - << "\n\treleaseTime=" << version->releaseTime - << "\n\ttype=" << version->type - << "\n\tassets=" << version->assets - << "\n\tprocessArguments=" << version->processArguments - << "\n\tminecraftArguments=" << version->minecraftArguments - << "\n\tminimumLauncherVersion=" << version->minimumLauncherVersion - << "\n\tmainClass=" << version->mainClass - << "\n\tlibraries="; - for (auto lib : version->libraries) - { - dbg.nospace() << "\n\t\t" << lib.get(); - } - dbg.nospace() << "\n)"; + version->dump(); return dbg.maybeSpace(); } QDebug operator<<(QDebug &dbg, const DerpLibrary *library) diff --git a/logic/DerpVersion.h b/logic/DerpVersion.h index f2132ad8..4481d146 100644 --- a/logic/DerpVersion.h +++ b/logic/DerpVersion.h @@ -41,10 +41,14 @@ public: bool reload(QWidget *widgetParent); void clear(); + void dump() const; + public: QList> getActiveNormalLibs(); QList> getActiveNativeLibs(); + static std::shared_ptr fromJson(const QJsonObject &obj); + // data members public: /// the ID - determines which jar to use! ACTUALLY IMPORTANT! diff --git a/logic/DerpVersionBuilder.cpp b/logic/DerpVersionBuilder.cpp index eb215cca..20f43404 100644 --- a/logic/DerpVersionBuilder.cpp +++ b/logic/DerpVersionBuilder.cpp @@ -29,6 +29,7 @@ #include "DerpVersion.h" #include "DerpInstance.h" #include "DerpRule.h" +#include "logger/QsLog.h" DerpVersionBuilder::DerpVersionBuilder() { @@ -44,6 +45,15 @@ bool DerpVersionBuilder::build(DerpVersion *version, DerpInstance *instance, QWi return builder.build(); } +bool DerpVersionBuilder::read(DerpVersion *version, const QJsonObject &obj) +{ + DerpVersionBuilder builder; + builder.m_version = version; + builder.m_instance = 0; + builder.m_widgetParent = 0; + return builder.read(obj); +} + bool DerpVersionBuilder::build() { m_version->clear(); @@ -55,6 +65,7 @@ bool DerpVersionBuilder::build() // version.json { + QLOG_INFO() << "Reading version.json"; QJsonObject obj; if (!read(QFileInfo(root.absoluteFilePath("version.json")), &obj)) { @@ -73,6 +84,7 @@ bool DerpVersionBuilder::build() QMap objects; for (auto info : patches.entryInfoList(QStringList() << "*.json", QDir::Files)) { + QLOG_INFO() << "Reading" << info.fileName(); QJsonObject obj; if (!read(info, &obj)) { @@ -87,6 +99,7 @@ bool DerpVersionBuilder::build() } for (auto object : objects.values()) { + qDebug() << "Applying object with order" << objects.key(object); if (!apply(object)) { return false; @@ -98,6 +111,7 @@ bool DerpVersionBuilder::build() { if (QFile::exists(root.absoluteFilePath("custom.json"))) { + QLOG_INFO() << "Reading custom.json"; QJsonObject obj; if (!read(QFileInfo(root.absoluteFilePath("custom.json")), &obj)) { @@ -113,6 +127,13 @@ bool DerpVersionBuilder::build() return true; } +bool DerpVersionBuilder::read(const QJsonObject &obj) +{ + m_version->clear(); + + return apply(obj); +} + void applyString(const QJsonObject &obj, const QString &key, QString &out, const bool onlyOverride = true) { if (obj.contains(key) && obj.value(key).isString()) @@ -142,7 +163,9 @@ bool DerpVersionBuilder::apply(const QJsonObject &object) { applyString(object, "id", m_version->id); applyString(object, "mainClass", m_version->mainClass); + applyString(object, "minecraftArguments", m_version->minecraftArguments, false); applyString(object, "processArguments", m_version->processArguments, false); + if (m_version->minecraftArguments.isEmpty()) { const QString toCompare = m_version->processArguments.toLower(); if (toCompare == "legacy") @@ -158,7 +181,6 @@ bool DerpVersionBuilder::apply(const QJsonObject &object) m_version->minecraftArguments = "--username ${auth_player_name} --session ${auth_session} --version ${profile_name}"; } } - applyString(object, "minecraftArguments", m_version->minecraftArguments, false); applyString(object, "type", m_version->type); applyString(object, "releaseTime", m_version->releaseTime); applyString(object, "time", m_version->time); @@ -282,6 +304,18 @@ bool DerpVersionBuilder::applyLibrary(const QJsonObject &lib, const DerpVersionB std::shared_ptr library; + if (lib.value("insert").toString() != "apply" && type == Add) + { + QMutableListIterator > it(m_version->libraries); + while (it.hasNext()) + { + if (it.next()->rawName() == name) + { + it.remove(); + } + } + } + if (lib.value("insert").toString() == "apply" && type == Add) { library = m_version->libraries[findLibrary(m_version->libraries, name)]; @@ -343,7 +377,6 @@ bool DerpVersionBuilder::applyLibrary(const QJsonObject &lib, const DerpVersionB library->finalize(); if (type == Override) { - qDebug() << "appending" << library->rawName(); m_version->libraries.append(library); } else if (lib.value("insert").toString() != "apply") diff --git a/logic/DerpVersionBuilder.h b/logic/DerpVersionBuilder.h index 5354cb0e..b94cd8e9 100644 --- a/logic/DerpVersionBuilder.h +++ b/logic/DerpVersionBuilder.h @@ -28,6 +28,7 @@ class DerpVersionBuilder DerpVersionBuilder(); public: static bool build(DerpVersion *version, DerpInstance *instance, QWidget *widgetParent); + static bool read(DerpVersion *version, const QJsonObject &obj); private: DerpVersion *m_version; @@ -42,6 +43,7 @@ private: }; bool build(); + bool read(const QJsonObject &obj); void clear(); bool apply(const QJsonObject &object); diff --git a/logic/ForgeInstaller.cpp b/logic/ForgeInstaller.cpp index 29735a7d..35663ddd 100644 --- a/logic/ForgeInstaller.cpp +++ b/logic/ForgeInstaller.cpp @@ -22,13 +22,13 @@ #include #include #include "MultiMC.h" +#include "DerpInstance.h" #include +#include #include #include -// DERPFIX - ForgeInstaller::ForgeInstaller(QString filename, QString universal_url) { std::shared_ptr newVersion; @@ -64,7 +64,7 @@ ForgeInstaller::ForgeInstaller(QString filename, QString universal_url) // read the forge version info { - // DERPFIX newVersion = DerpVersion::fromJson(versionInfoVal.toObject()); + newVersion = DerpVersion::fromJson(versionInfoVal.toObject()); if (!newVersion) return; } @@ -109,12 +109,22 @@ ForgeInstaller::ForgeInstaller(QString filename, QString universal_url) realVersionId = m_forge_version->id = installObj.value("minecraft").toString(); } -bool ForgeInstaller::apply(std::shared_ptr to) +bool ForgeInstaller::add(DerpInstance *to) { + if (!BaseInstaller::add(to)) + { + return false; + } + + QJsonObject obj; + obj.insert("order", 5); + if (!m_forge_version) return false; int sliding_insert_window = 0; { + QJsonArray librariesPlus; + // for each library in the version we are adding (except for the blacklisted) QSet blacklist{"lwjgl", "lwjgl_util", "lwjgl-platform"}; for (auto lib : m_forge_version->libraries) @@ -133,27 +143,43 @@ bool ForgeInstaller::apply(std::shared_ptr to) if (blacklist.contains(libName)) continue; - // find an entry that matches this one + QJsonObject libObj = lib->toJson(); + bool found = false; - for (auto tolib : to->libraries) + // find an entry that matches this one + for (auto tolib : to->getFullVersion()->libraries) { if (tolib->name() != libName) continue; found = true; // replace lib - tolib = lib; + libObj.insert("insert", QString("apply")); break; } if (!found) { // add lib - to->libraries.insert(sliding_insert_window, lib); + QJsonObject insertObj; + insertObj.insert("before", to->getFullVersion()->libraries.at(sliding_insert_window)->rawName()); + libObj.insert("insert", insertObj); sliding_insert_window++; } + librariesPlus.append(libObj); } - to->mainClass = m_forge_version->mainClass; - to->minecraftArguments = m_forge_version->minecraftArguments; - to->processArguments = m_forge_version->processArguments; + obj.insert("+libraries", librariesPlus); + obj.insert("mainClass", m_forge_version->mainClass); + obj.insert("minecraftArguments", m_forge_version->minecraftArguments); + obj.insert("processArguments", m_forge_version->processArguments); } + + 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; } diff --git a/logic/ForgeInstaller.h b/logic/ForgeInstaller.h index 65cad87c..6490b5bf 100644 --- a/logic/ForgeInstaller.h +++ b/logic/ForgeInstaller.h @@ -14,17 +14,22 @@ */ #pragma once + +#include "BaseInstaller.h" + #include #include class DerpVersion; -class ForgeInstaller +class ForgeInstaller : public BaseInstaller { public: ForgeInstaller(QString filename, QString universal_url); - bool apply(std::shared_ptr to); + bool add(DerpInstance *to) override; + + QString id() const override { return "net.minecraftforge"; } private: // the version, read from the installer diff --git a/logic/LiteLoaderInstaller.cpp b/logic/LiteLoaderInstaller.cpp index d582f03a..71c7011f 100644 --- a/logic/LiteLoaderInstaller.cpp +++ b/logic/LiteLoaderInstaller.cpp @@ -27,6 +27,7 @@ QMap LiteLoaderInstaller::m_launcherWrapperVersionMapping; LiteLoaderInstaller::LiteLoaderInstaller() + : BaseInstaller() { if (m_launcherWrapperVersionMapping.isEmpty()) { @@ -42,24 +43,11 @@ bool LiteLoaderInstaller::canApply(DerpInstance *instance) const return m_launcherWrapperVersionMapping.contains(instance->intendedVersionId()); } -bool LiteLoaderInstaller::isApplied(DerpInstance *on) -{ - return QFile::exists(filename(on->instanceRoot())); -} - bool LiteLoaderInstaller::add(DerpInstance *to) { - if (!patchesDir(to->instanceRoot()).exists()) + if (!BaseInstaller::add(to)) { - QDir(to->instanceRoot()).mkdir("patches"); - } - - if (isApplied(to)) - { - if (!remove(to)) - { - return false; - } + return false; } QJsonObject obj; @@ -102,17 +90,3 @@ bool LiteLoaderInstaller::add(DerpInstance *to) return true; } - -bool LiteLoaderInstaller::remove(DerpInstance *from) -{ - return QFile::remove(filename(from->instanceRoot())); -} - -QString LiteLoaderInstaller::filename(const QString &root) const -{ - 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 48ef4baf..c128ee22 100644 --- a/logic/LiteLoaderInstaller.h +++ b/logic/LiteLoaderInstaller.h @@ -14,30 +14,22 @@ */ #pragma once + +#include "BaseInstaller.h" + #include #include -#include -class DerpVersion; -class DerpInstance; -class QDir; - -// TODO base class -class LiteLoaderInstaller +class LiteLoaderInstaller : public BaseInstaller { public: LiteLoaderInstaller(); - bool canApply(DerpInstance *instance) const; - bool isApplied(DerpInstance *on); - - bool add(DerpInstance *to); - bool remove(DerpInstance *from); + bool canApply(DerpInstance *instance) const override; + bool add(DerpInstance *to) override; private: - virtual QString id() const { return "com.mumfrey.liteloader"; } - QString filename(const QString &root) const; - QDir patchesDir(const QString &root) const; + virtual QString id() const override { return "com.mumfrey.liteloader"; } static QMap m_launcherWrapperVersionMapping; };