2014-02-16 09:46:14 +00:00
|
|
|
#include "MCEditTool.h"
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QProcess>
|
2014-02-21 19:13:12 +00:00
|
|
|
#include <QUrl>
|
2014-02-16 09:46:14 +00:00
|
|
|
|
2015-02-09 00:51:14 +00:00
|
|
|
#include "settings/SettingsObject.h"
|
|
|
|
#include "BaseInstance.h"
|
|
|
|
#include "minecraft/MinecraftInstance.h"
|
2014-02-16 09:46:14 +00:00
|
|
|
|
2015-02-09 00:51:14 +00:00
|
|
|
MCEditTool::MCEditTool(SettingsObjectPtr settings, InstancePtr instance, QObject *parent)
|
|
|
|
: BaseDetachedTool(settings, instance, parent)
|
2014-02-16 09:46:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-01-27 21:31:07 +00:00
|
|
|
QString MCEditTool::getSave() const
|
|
|
|
{
|
|
|
|
auto mcInstance = std::dynamic_pointer_cast<MinecraftInstance>(m_instance);
|
|
|
|
if(!mcInstance)
|
|
|
|
{
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
QDir saves(mcInstance->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;
|
2016-04-10 13:53:05 +00:00
|
|
|
// FIXME: mixing logic and UI!!!!
|
2016-04-10 02:29:29 +00:00
|
|
|
/*
|
2015-02-09 00:51:14 +00:00
|
|
|
const QString save = QInputDialog::getItem(QApplication::activeWindow(), tr("MCEdit"), tr("Choose which world to open:"),
|
2015-01-27 21:31:07 +00:00
|
|
|
worlds, 0, false, &ok);
|
|
|
|
if (ok)
|
|
|
|
{
|
|
|
|
return saves.absoluteFilePath(save);
|
|
|
|
}
|
2016-04-10 02:29:29 +00:00
|
|
|
*/
|
2015-01-27 21:31:07 +00:00
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2014-02-16 09:46:14 +00:00
|
|
|
void MCEditTool::runImpl()
|
|
|
|
{
|
2015-02-09 00:51:14 +00:00
|
|
|
const QString mceditPath = globalSettings->get("MCEditPath").toString();
|
2014-02-16 11:08:39 +00:00
|
|
|
const QString save = getSave();
|
|
|
|
if (save.isNull())
|
2014-02-16 09:46:14 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-02-21 19:13:12 +00:00
|
|
|
#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
|
2014-02-16 10:49:55 +00:00
|
|
|
QDir mceditDir(mceditPath);
|
|
|
|
QString program;
|
2015-06-10 21:51:05 +00:00
|
|
|
#ifdef Q_OS_LINUX
|
2014-02-16 10:49:55 +00:00
|
|
|
if (mceditDir.exists("mcedit.py"))
|
|
|
|
{
|
|
|
|
program = mceditDir.absoluteFilePath("mcedit.py");
|
|
|
|
}
|
2015-06-11 00:33:02 +00:00
|
|
|
else if (mceditDir.exists("mcedit.sh"))
|
2015-06-10 21:51:05 +00:00
|
|
|
{
|
2015-06-11 00:33:02 +00:00
|
|
|
program = mceditDir.absoluteFilePath("mcedit.sh");
|
2015-06-10 21:51:05 +00:00
|
|
|
}
|
|
|
|
#elif defined(Q_OS_WIN32)
|
|
|
|
if (mceditDir.exists("mcedit.exe"))
|
2014-02-16 10:49:55 +00:00
|
|
|
{
|
|
|
|
program = mceditDir.absoluteFilePath("mcedit.exe");
|
|
|
|
}
|
2015-04-01 20:43:18 +00:00
|
|
|
else if (mceditDir.exists("mcedit2.exe"))
|
|
|
|
{
|
|
|
|
program = mceditDir.absoluteFilePath("mcedit2.exe");
|
|
|
|
}
|
2015-06-10 21:51:05 +00:00
|
|
|
#endif
|
2016-04-10 02:29:29 +00:00
|
|
|
/*
|
2015-06-10 21:51:05 +00:00
|
|
|
if(program.size())
|
|
|
|
{
|
2016-01-05 06:32:52 +00:00
|
|
|
DesktopServices::openFile(program, save, mceditPath);
|
2015-06-10 21:51:05 +00:00
|
|
|
}
|
2016-04-10 02:29:29 +00:00
|
|
|
*/
|
2014-02-21 19:13:12 +00:00
|
|
|
#endif
|
2014-02-16 09:46:14 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 00:51:14 +00:00
|
|
|
void MCEditFactory::registerSettings(SettingsObjectPtr settings)
|
2014-02-16 09:46:14 +00:00
|
|
|
{
|
|
|
|
settings->registerSetting("MCEditPath");
|
2015-02-09 00:51:14 +00:00
|
|
|
globalSettings = settings;
|
2014-02-16 09:46:14 +00:00
|
|
|
}
|
2014-03-30 18:11:05 +00:00
|
|
|
BaseExternalTool *MCEditFactory::createTool(InstancePtr instance, QObject *parent)
|
2014-02-16 09:46:14 +00:00
|
|
|
{
|
2015-02-09 00:51:14 +00:00
|
|
|
return new MCEditTool(globalSettings, instance, parent);
|
2014-02-16 09:46:14 +00:00
|
|
|
}
|
|
|
|
bool MCEditFactory::check(QString *error)
|
|
|
|
{
|
2015-02-09 00:51:14 +00:00
|
|
|
return check(globalSettings->get("MCEditPath").toString(), error);
|
2014-02-16 09:46:14 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2015-06-11 00:33:02 +00:00
|
|
|
if (!dir.exists("mcedit.sh") && !dir.exists("mcedit.py") && !dir.exists("mcedit.exe") && !dir.exists("Contents") && !dir.exists("mcedit2.exe"))
|
2014-02-16 09:46:14 +00:00
|
|
|
{
|
2014-02-21 19:13:12 +00:00
|
|
|
*error = QObject::tr("Path does not seem to be a MCEdit path");
|
2014-02-16 09:46:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|