diff options
Diffstat (limited to 'logic/tools')
-rw-r--r-- | logic/tools/BaseExternalTool.cpp | 77 | ||||
-rw-r--r-- | logic/tools/BaseExternalTool.h | 57 | ||||
-rw-r--r-- | logic/tools/BaseProfiler.cpp | 35 | ||||
-rw-r--r-- | logic/tools/BaseProfiler.h | 36 | ||||
-rw-r--r-- | logic/tools/JProfiler.cpp | 78 | ||||
-rw-r--r-- | logic/tools/JProfiler.h | 23 | ||||
-rw-r--r-- | logic/tools/JVisualVM.cpp | 74 | ||||
-rw-r--r-- | logic/tools/JVisualVM.h | 23 | ||||
-rw-r--r-- | logic/tools/MCEditTool.cpp | 77 | ||||
-rw-r--r-- | logic/tools/MCEditTool.h | 23 |
10 files changed, 503 insertions, 0 deletions
diff --git a/logic/tools/BaseExternalTool.cpp b/logic/tools/BaseExternalTool.cpp new file mode 100644 index 00000000..69cddd00 --- /dev/null +++ b/logic/tools/BaseExternalTool.cpp @@ -0,0 +1,77 @@ +#include "BaseExternalTool.h" + +#include <QProcess> +#include <QDir> +#include <QInputDialog> + +#ifdef Q_OS_WIN +#include <windows.h> +#endif + +#include "logic/BaseInstance.h" +#include "MultiMC.h" + +BaseExternalTool::BaseExternalTool(BaseInstance *instance, QObject *parent) + : QObject(parent), m_instance(instance) +{ +} + +BaseExternalTool::~BaseExternalTool() +{ +} + +qint64 BaseExternalTool::pid(QProcess *process) +{ +#ifdef Q_OS_WIN + struct _PROCESS_INFORMATION *procinfo = process->pid(); + return procinfo->dwProcessId; +#else + return process->pid(); +#endif +} + +QString BaseExternalTool::getSave() const +{ + QDir saves(m_instance->minecraftRoot() + "/saves"); + QStringList worlds = saves.entryList(QDir::Dirs | QDir::NoDotAndDotDot); + QMutableListIterator<QString> it(worlds); + while (it.hasNext()) + { + it.next(); + if (!QDir(saves.absoluteFilePath(it.value())).exists("level.dat")) + { + it.remove(); + } + } + bool ok = true; + const QString save = QInputDialog::getItem( + MMC->activeWindow(), tr("MCEdit"), tr("Choose which world to open:"), + worlds, 0, false, &ok); + if (ok) + { + return saves.absoluteFilePath(save); + } + return QString(); +} + + +BaseDetachedTool::BaseDetachedTool(BaseInstance *instance, QObject *parent) + : BaseExternalTool(instance, parent) +{ + +} + +void BaseDetachedTool::run() +{ + runImpl(); +} + + +BaseExternalToolFactory::~BaseExternalToolFactory() +{ +} + +BaseDetachedTool *BaseDetachedToolFactory::createDetachedTool(BaseInstance *instance, QObject *parent) +{ + return qobject_cast<BaseDetachedTool *>(createTool(instance, parent)); +} diff --git a/logic/tools/BaseExternalTool.h b/logic/tools/BaseExternalTool.h new file mode 100644 index 00000000..e8965bfd --- /dev/null +++ b/logic/tools/BaseExternalTool.h @@ -0,0 +1,57 @@ +#pragma once + +#include <QObject> + +class BaseInstance; +class SettingsObject; +class MinecraftProcess; +class QProcess; + +class BaseExternalTool : public QObject +{ + Q_OBJECT +public: + explicit BaseExternalTool(BaseInstance *instance, QObject *parent = 0); + virtual ~BaseExternalTool(); + +protected: + BaseInstance *m_instance; + + qint64 pid(QProcess *process); + QString getSave() const; +}; + +class BaseDetachedTool : public BaseExternalTool +{ + Q_OBJECT +public: + explicit BaseDetachedTool(BaseInstance *instance, QObject *parent = 0); + +public +slots: + void run(); + +protected: + virtual void runImpl() = 0; +}; + +class BaseExternalToolFactory +{ +public: + virtual ~BaseExternalToolFactory(); + + virtual QString name() const = 0; + + virtual void registerSettings(SettingsObject *settings) = 0; + + virtual BaseExternalTool *createTool(BaseInstance *instance, QObject *parent = 0) = 0; + + virtual bool check(QString *error) = 0; + virtual bool check(const QString &path, QString *error) = 0; +}; + +class BaseDetachedToolFactory : public BaseExternalToolFactory +{ +public: + virtual BaseDetachedTool *createDetachedTool(BaseInstance *instance, QObject *parent = 0); +}; diff --git a/logic/tools/BaseProfiler.cpp b/logic/tools/BaseProfiler.cpp new file mode 100644 index 00000000..9aaca793 --- /dev/null +++ b/logic/tools/BaseProfiler.cpp @@ -0,0 +1,35 @@ +#include "BaseProfiler.h" + +#include <QProcess> + +BaseProfiler::BaseProfiler(BaseInstance *instance, QObject *parent) + : BaseExternalTool(instance, parent) +{ +} + +void BaseProfiler::beginProfiling(MinecraftProcess *process) +{ + beginProfilingImpl(process); +} + +void BaseProfiler::abortProfiling() +{ + abortProfilingImpl(); +} + +void BaseProfiler::abortProfilingImpl() +{ + if (!m_profilerProcess) + { + return; + } + m_profilerProcess->terminate(); + m_profilerProcess->deleteLater(); + m_profilerProcess = 0; + emit abortLaunch(tr("Profiler aborted")); +} + +BaseProfiler *BaseProfilerFactory::createProfiler(BaseInstance *instance, QObject *parent) +{ + return qobject_cast<BaseProfiler *>(createTool(instance, parent)); +} diff --git a/logic/tools/BaseProfiler.h b/logic/tools/BaseProfiler.h new file mode 100644 index 00000000..ec57578e --- /dev/null +++ b/logic/tools/BaseProfiler.h @@ -0,0 +1,36 @@ +#pragma once + +#include "BaseExternalTool.h" + +class BaseInstance; +class SettingsObject; +class MinecraftProcess; +class QProcess; + +class BaseProfiler : public BaseExternalTool +{ + Q_OBJECT +public: + explicit BaseProfiler(BaseInstance *instance, QObject *parent = 0); + +public +slots: + void beginProfiling(MinecraftProcess *process); + void abortProfiling(); + +protected: + QProcess *m_profilerProcess; + + virtual void beginProfilingImpl(MinecraftProcess *process) = 0; + virtual void abortProfilingImpl(); + +signals: + void readyToLaunch(const QString &message); + void abortLaunch(const QString &message); +}; + +class BaseProfilerFactory : public BaseExternalToolFactory +{ +public: + virtual BaseProfiler *createProfiler(BaseInstance *instance, QObject *parent = 0); +}; diff --git a/logic/tools/JProfiler.cpp b/logic/tools/JProfiler.cpp new file mode 100644 index 00000000..bb851f0b --- /dev/null +++ b/logic/tools/JProfiler.cpp @@ -0,0 +1,78 @@ +#include "JProfiler.h" + +#include <QDir> +#include <QMessageBox> + +#include "settingsobject.h" +#include "logic/MinecraftProcess.h" +#include "logic/BaseInstance.h" +#include "MultiMC.h" + +JProfiler::JProfiler(BaseInstance *instance, QObject *parent) : BaseProfiler(instance, parent) +{ +} + +void JProfiler::beginProfilingImpl(MinecraftProcess *process) +{ + int port = MMC->settings()->get("JProfilerPort").toInt(); + QProcess *profiler = new QProcess(this); + profiler->setArguments(QStringList() << "-d" << QString::number(pid(process)) << "--gui" + << "-p" << QString::number(port)); + profiler->setProgram(QDir(MMC->settings()->get("JProfilerPath").toString()) + .absoluteFilePath("bin/jpenable")); + connect(profiler, &QProcess::started, [this, port]() + { emit readyToLaunch(tr("Listening on port: %1").arg(port)); }); + connect(profiler, + static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), + [this](int exit, QProcess::ExitStatus status) + { + if (status == QProcess::CrashExit) + { + emit abortLaunch(tr("Profiler aborted")); + } + if (m_profilerProcess) + { + m_profilerProcess->deleteLater(); + m_profilerProcess = 0; + } + }); + profiler->start(); + m_profilerProcess = profiler; +} + +void JProfilerFactory::registerSettings(SettingsObject *settings) +{ + settings->registerSetting("JProfilerPath"); + settings->registerSetting("JProfilerPort", 42042); +} + +BaseExternalTool *JProfilerFactory::createTool(BaseInstance *instance, QObject *parent) +{ + return new JProfiler(instance, parent); +} + +bool JProfilerFactory::check(QString *error) +{ + return check(MMC->settings()->get("JProfilerPath").toString(), error); +} + +bool JProfilerFactory::check(const QString &path, QString *error) +{ + if (path.isEmpty()) + { + *error = QObject::tr("Empty path"); + return false; + } + QDir dir(path); + if (!dir.exists()) + { + *error = QObject::tr("Path does not exist"); + return false; + } + if (!dir.exists("bin") || !dir.exists("bin/jprofiler") || !dir.exists("bin/agent.jar")) + { + *error = QObject::tr("Invalid JProfiler install"); + return false; + } + return true; +} diff --git a/logic/tools/JProfiler.h b/logic/tools/JProfiler.h new file mode 100644 index 00000000..88a02462 --- /dev/null +++ b/logic/tools/JProfiler.h @@ -0,0 +1,23 @@ +#pragma once + +#include "BaseProfiler.h" + +class JProfiler : public BaseProfiler +{ + Q_OBJECT +public: + JProfiler(BaseInstance *instance, QObject *parent = 0); + +protected: + void beginProfilingImpl(MinecraftProcess *process); +}; + +class JProfilerFactory : public BaseProfilerFactory +{ +public: + QString name() const override { return "JProfiler"; } + void registerSettings(SettingsObject *settings) override; + BaseExternalTool *createTool(BaseInstance *instance, QObject *parent = 0) override; + bool check(QString *error) override; + bool check(const QString &path, QString *error) override; +}; diff --git a/logic/tools/JVisualVM.cpp b/logic/tools/JVisualVM.cpp new file mode 100644 index 00000000..02938028 --- /dev/null +++ b/logic/tools/JVisualVM.cpp @@ -0,0 +1,74 @@ +#include "JVisualVM.h" + +#include <QDir> +#include <QStandardPaths> + +#include "settingsobject.h" +#include "logic/MinecraftProcess.h" +#include "logic/BaseInstance.h" +#include "MultiMC.h" + +JVisualVM::JVisualVM(BaseInstance *instance, QObject *parent) : BaseProfiler(instance, parent) +{ +} + +void JVisualVM::beginProfilingImpl(MinecraftProcess *process) +{ + QProcess *profiler = new QProcess(this); + profiler->setArguments(QStringList() << "--openpid" << QString::number(pid(process))); + profiler->setProgram(MMC->settings()->get("JVisualVMPath").toString()); + connect(profiler, &QProcess::started, [this]() + { emit readyToLaunch(tr("JVisualVM started")); }); + connect(profiler, + static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), + [this](int exit, QProcess::ExitStatus status) + { + if (exit != 0 || status == QProcess::CrashExit) + { + emit abortLaunch(tr("Profiler aborted")); + } + if (m_profilerProcess) + { + m_profilerProcess->deleteLater(); + m_profilerProcess = 0; + } + }); + profiler->start(); + m_profilerProcess = profiler; +} + +void JVisualVMFactory::registerSettings(SettingsObject *settings) +{ + QString defaultValue = QStandardPaths::findExecutable("jvisualvm"); + if (defaultValue.isNull()) + { + defaultValue = QStandardPaths::findExecutable("visualvm"); + } + settings->registerSetting("JVisualVMPath", defaultValue); +} + +BaseExternalTool *JVisualVMFactory::createTool(BaseInstance *instance, QObject *parent) +{ + return new JVisualVM(instance, parent); +} + +bool JVisualVMFactory::check(QString *error) +{ + return check(MMC->settings()->get("JVisualVMPath").toString(), error); +} + +bool JVisualVMFactory::check(const QString &path, QString *error) +{ + if (path.isEmpty()) + { + *error = QObject::tr("Empty path"); + return false; + } + QString resolved = QStandardPaths::findExecutable(path); + if (resolved.isEmpty() && !QDir::isAbsolutePath(path)) + { + *error = QObject::tr("Invalid path to JVisualVM"); + return false; + } + return true; +} diff --git a/logic/tools/JVisualVM.h b/logic/tools/JVisualVM.h new file mode 100644 index 00000000..af94fe55 --- /dev/null +++ b/logic/tools/JVisualVM.h @@ -0,0 +1,23 @@ +#pragma once + +#include "BaseProfiler.h" + +class JVisualVM : public BaseProfiler +{ + Q_OBJECT +public: + JVisualVM(BaseInstance *instance, QObject *parent = 0); + +protected: + void beginProfilingImpl(MinecraftProcess *process); +}; + +class JVisualVMFactory : public BaseProfilerFactory +{ +public: + QString name() const override { return "JVisualVM"; } + void registerSettings(SettingsObject *settings) override; + BaseExternalTool *createTool(BaseInstance *instance, QObject *parent = 0) override; + bool check(QString *error) override; + bool check(const QString &path, QString *error) override; +}; diff --git a/logic/tools/MCEditTool.cpp b/logic/tools/MCEditTool.cpp new file mode 100644 index 00000000..e22a5d4a --- /dev/null +++ b/logic/tools/MCEditTool.cpp @@ -0,0 +1,77 @@ +#include "MCEditTool.h" + +#include <QDir> +#include <QProcess> +#include <QDesktopServices> +#include <QUrl> + +#include "settingsobject.h" +#include "logic/BaseInstance.h" +#include "MultiMC.h" + +MCEditTool::MCEditTool(BaseInstance *instance, QObject *parent) + : BaseDetachedTool(instance, parent) +{ +} + +void MCEditTool::runImpl() +{ + const QString mceditPath = MMC->settings()->get("MCEditPath").toString(); + const QString save = getSave(); + if (save.isNull()) + { + return; + } +#ifdef Q_OS_OSX + QProcess *process = new QProcess(); + connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), process, SLOT(deleteLater())); + process->setProgram(mceditPath); + process->setArguments(QStringList() << save); + process->start(); +#else + QDir mceditDir(mceditPath); + QString program; + if (mceditDir.exists("mcedit.py")) + { + program = mceditDir.absoluteFilePath("mcedit.py"); + } + else if (mceditDir.exists("mcedit.exe")) + { + program = mceditDir.absoluteFilePath("mcedit.exe"); + } + QProcess::startDetached(program, QStringList() << save, mceditPath); +#endif +} + +void MCEditFactory::registerSettings(SettingsObject *settings) +{ + settings->registerSetting("MCEditPath"); +} +BaseExternalTool *MCEditFactory::createTool(BaseInstance *instance, QObject *parent) +{ + return new MCEditTool(instance, parent); +} +bool MCEditFactory::check(QString *error) +{ + return check(MMC->settings()->get("MCEditPath").toString(), error); +} +bool MCEditFactory::check(const QString &path, QString *error) +{ + if (path.isEmpty()) + { + *error = QObject::tr("Path is empty"); + return false; + } + const QDir dir(path); + if (!dir.exists()) + { + *error = QObject::tr("Path does not exist"); + return false; + } + if (!dir.exists("mcedit.py") && !dir.exists("mcedit.exe") && !dir.exists("Contents")) + { + *error = QObject::tr("Path does not seem to be a MCEdit path"); + return false; + } + return true; +} diff --git a/logic/tools/MCEditTool.h b/logic/tools/MCEditTool.h new file mode 100644 index 00000000..b0ed1ad4 --- /dev/null +++ b/logic/tools/MCEditTool.h @@ -0,0 +1,23 @@ +#pragma once + +#include "BaseExternalTool.h" + +class MCEditTool : public BaseDetachedTool +{ + Q_OBJECT +public: + explicit MCEditTool(BaseInstance *instance, QObject *parent = 0); + +protected: + void runImpl() override; +}; + +class MCEditFactory : public BaseDetachedToolFactory +{ +public: + QString name() const override { return "MCEdit"; } + void registerSettings(SettingsObject *settings) override; + BaseExternalTool *createTool(BaseInstance *instance, QObject *parent = 0) override; + bool check(QString *error) override; + bool check(const QString &path, QString *error) override; +}; |