NOISSUE Flatten gui and logic libraries into MultiMC
This commit is contained in:
61
launcher/BaseInstaller.cpp
Normal file
61
launcher/BaseInstaller.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
/* Copyright 2013-2021 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 <QFile>
|
||||
|
||||
#include "BaseInstaller.h"
|
||||
#include "minecraft/MinecraftInstance.h"
|
||||
|
||||
BaseInstaller::BaseInstaller()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool BaseInstaller::isApplied(MinecraftInstance *on)
|
||||
{
|
||||
return QFile::exists(filename(on->instanceRoot()));
|
||||
}
|
||||
|
||||
bool BaseInstaller::add(MinecraftInstance *to)
|
||||
{
|
||||
if (!patchesDir(to->instanceRoot()).exists())
|
||||
{
|
||||
QDir(to->instanceRoot()).mkdir("patches");
|
||||
}
|
||||
|
||||
if (isApplied(to))
|
||||
{
|
||||
if (!remove(to))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BaseInstaller::remove(MinecraftInstance *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/");
|
||||
}
|
44
launcher/BaseInstaller.h
Normal file
44
launcher/BaseInstaller.h
Normal file
@ -0,0 +1,44 @@
|
||||
/* Copyright 2013-2021 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 <memory>
|
||||
|
||||
class MinecraftInstance;
|
||||
class QDir;
|
||||
class QString;
|
||||
class QObject;
|
||||
class Task;
|
||||
class BaseVersion;
|
||||
typedef std::shared_ptr<BaseVersion> BaseVersionPtr;
|
||||
|
||||
class BaseInstaller
|
||||
{
|
||||
public:
|
||||
BaseInstaller();
|
||||
virtual ~BaseInstaller(){};
|
||||
bool isApplied(MinecraftInstance *on);
|
||||
|
||||
virtual bool add(MinecraftInstance *to);
|
||||
virtual bool remove(MinecraftInstance *from);
|
||||
|
||||
virtual Task *createInstallTask(MinecraftInstance *instance, BaseVersionPtr version, QObject *parent) = 0;
|
||||
|
||||
protected:
|
||||
virtual QString id() const = 0;
|
||||
QString filename(const QString &root) const;
|
||||
QDir patchesDir(const QString &root) const;
|
||||
};
|
275
launcher/BaseInstance.cpp
Normal file
275
launcher/BaseInstance.cpp
Normal file
@ -0,0 +1,275 @@
|
||||
/* Copyright 2013-2021 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 "BaseInstance.h"
|
||||
|
||||
#include <QFileInfo>
|
||||
#include <QDir>
|
||||
#include <QDebug>
|
||||
|
||||
#include "settings/INISettingsObject.h"
|
||||
#include "settings/Setting.h"
|
||||
#include "settings/OverrideSetting.h"
|
||||
|
||||
#include "FileSystem.h"
|
||||
#include "Commandline.h"
|
||||
|
||||
BaseInstance::BaseInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString &rootDir)
|
||||
: QObject()
|
||||
{
|
||||
m_settings = settings;
|
||||
m_rootDir = rootDir;
|
||||
|
||||
m_settings->registerSetting("name", "Unnamed Instance");
|
||||
m_settings->registerSetting("iconKey", "default");
|
||||
m_settings->registerSetting("notes", "");
|
||||
m_settings->registerSetting("lastLaunchTime", 0);
|
||||
m_settings->registerSetting("totalTimePlayed", 0);
|
||||
m_settings->registerSetting("lastTimePlayed", 0);
|
||||
|
||||
// Custom Commands
|
||||
auto commandSetting = m_settings->registerSetting({"OverrideCommands","OverrideLaunchCmd"}, false);
|
||||
m_settings->registerOverride(globalSettings->getSetting("PreLaunchCommand"), commandSetting);
|
||||
m_settings->registerOverride(globalSettings->getSetting("WrapperCommand"), commandSetting);
|
||||
m_settings->registerOverride(globalSettings->getSetting("PostExitCommand"), commandSetting);
|
||||
|
||||
// Console
|
||||
auto consoleSetting = m_settings->registerSetting("OverrideConsole", false);
|
||||
m_settings->registerOverride(globalSettings->getSetting("ShowConsole"), consoleSetting);
|
||||
m_settings->registerOverride(globalSettings->getSetting("AutoCloseConsole"), consoleSetting);
|
||||
m_settings->registerOverride(globalSettings->getSetting("ShowConsoleOnError"), consoleSetting);
|
||||
m_settings->registerOverride(globalSettings->getSetting("LogPrePostOutput"), consoleSetting);
|
||||
|
||||
m_settings->registerPassthrough(globalSettings->getSetting("ConsoleMaxLines"), nullptr);
|
||||
m_settings->registerPassthrough(globalSettings->getSetting("ConsoleOverflowStop"), nullptr);
|
||||
}
|
||||
|
||||
QString BaseInstance::getPreLaunchCommand()
|
||||
{
|
||||
return settings()->get("PreLaunchCommand").toString();
|
||||
}
|
||||
|
||||
QString BaseInstance::getWrapperCommand()
|
||||
{
|
||||
return settings()->get("WrapperCommand").toString();
|
||||
}
|
||||
|
||||
QString BaseInstance::getPostExitCommand()
|
||||
{
|
||||
return settings()->get("PostExitCommand").toString();
|
||||
}
|
||||
|
||||
int BaseInstance::getConsoleMaxLines() const
|
||||
{
|
||||
auto lineSetting = settings()->getSetting("ConsoleMaxLines");
|
||||
bool conversionOk = false;
|
||||
int maxLines = lineSetting->get().toInt(&conversionOk);
|
||||
if(!conversionOk)
|
||||
{
|
||||
maxLines = lineSetting->defValue().toInt();
|
||||
qWarning() << "ConsoleMaxLines has nonsensical value, defaulting to" << maxLines;
|
||||
}
|
||||
return maxLines;
|
||||
}
|
||||
|
||||
bool BaseInstance::shouldStopOnConsoleOverflow() const
|
||||
{
|
||||
return settings()->get("ConsoleOverflowStop").toBool();
|
||||
}
|
||||
|
||||
void BaseInstance::iconUpdated(QString key)
|
||||
{
|
||||
if(iconKey() == key)
|
||||
{
|
||||
emit propertiesChanged(this);
|
||||
}
|
||||
}
|
||||
|
||||
void BaseInstance::invalidate()
|
||||
{
|
||||
changeStatus(Status::Gone);
|
||||
qDebug() << "Instance" << id() << "has been invalidated.";
|
||||
}
|
||||
|
||||
void BaseInstance::changeStatus(BaseInstance::Status newStatus)
|
||||
{
|
||||
Status status = currentStatus();
|
||||
if(status != newStatus)
|
||||
{
|
||||
m_status = newStatus;
|
||||
emit statusChanged(status, newStatus);
|
||||
}
|
||||
}
|
||||
|
||||
BaseInstance::Status BaseInstance::currentStatus() const
|
||||
{
|
||||
return m_status;
|
||||
}
|
||||
|
||||
QString BaseInstance::id() const
|
||||
{
|
||||
return QFileInfo(instanceRoot()).fileName();
|
||||
}
|
||||
|
||||
bool BaseInstance::isRunning() const
|
||||
{
|
||||
return m_isRunning;
|
||||
}
|
||||
|
||||
void BaseInstance::setRunning(bool running)
|
||||
{
|
||||
if(running == m_isRunning)
|
||||
return;
|
||||
|
||||
m_isRunning = running;
|
||||
|
||||
if(!m_settings->get("RecordGameTime").toBool())
|
||||
{
|
||||
emit runningStatusChanged(running);
|
||||
return;
|
||||
}
|
||||
|
||||
if(running)
|
||||
{
|
||||
m_timeStarted = QDateTime::currentDateTime();
|
||||
}
|
||||
else
|
||||
{
|
||||
QDateTime timeEnded = QDateTime::currentDateTime();
|
||||
|
||||
qint64 current = settings()->get("totalTimePlayed").toLongLong();
|
||||
settings()->set("totalTimePlayed", current + m_timeStarted.secsTo(timeEnded));
|
||||
settings()->set("lastTimePlayed", m_timeStarted.secsTo(timeEnded));
|
||||
|
||||
emit propertiesChanged(this);
|
||||
}
|
||||
|
||||
emit runningStatusChanged(running);
|
||||
}
|
||||
|
||||
int64_t BaseInstance::totalTimePlayed() const
|
||||
{
|
||||
qint64 current = settings()->get("totalTimePlayed").toLongLong();
|
||||
if(m_isRunning)
|
||||
{
|
||||
QDateTime timeNow = QDateTime::currentDateTime();
|
||||
return current + m_timeStarted.secsTo(timeNow);
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
int64_t BaseInstance::lastTimePlayed() const
|
||||
{
|
||||
if(m_isRunning)
|
||||
{
|
||||
QDateTime timeNow = QDateTime::currentDateTime();
|
||||
return m_timeStarted.secsTo(timeNow);
|
||||
}
|
||||
return settings()->get("lastTimePlayed").toLongLong();
|
||||
}
|
||||
|
||||
void BaseInstance::resetTimePlayed()
|
||||
{
|
||||
settings()->reset("totalTimePlayed");
|
||||
settings()->reset("lastTimePlayed");
|
||||
}
|
||||
|
||||
QString BaseInstance::instanceType() const
|
||||
{
|
||||
return m_settings->get("InstanceType").toString();
|
||||
}
|
||||
|
||||
QString BaseInstance::instanceRoot() const
|
||||
{
|
||||
return m_rootDir;
|
||||
}
|
||||
|
||||
SettingsObjectPtr BaseInstance::settings() const
|
||||
{
|
||||
return m_settings;
|
||||
}
|
||||
|
||||
bool BaseInstance::canLaunch() const
|
||||
{
|
||||
return (!hasVersionBroken() && !isRunning());
|
||||
}
|
||||
|
||||
bool BaseInstance::reloadSettings()
|
||||
{
|
||||
return m_settings->reload();
|
||||
}
|
||||
|
||||
qint64 BaseInstance::lastLaunch() const
|
||||
{
|
||||
return m_settings->get("lastLaunchTime").value<qint64>();
|
||||
}
|
||||
|
||||
void BaseInstance::setLastLaunch(qint64 val)
|
||||
{
|
||||
//FIXME: if no change, do not set. setting involves saving a file.
|
||||
m_settings->set("lastLaunchTime", val);
|
||||
emit propertiesChanged(this);
|
||||
}
|
||||
|
||||
void BaseInstance::setNotes(QString val)
|
||||
{
|
||||
//FIXME: if no change, do not set. setting involves saving a file.
|
||||
m_settings->set("notes", val);
|
||||
}
|
||||
|
||||
QString BaseInstance::notes() const
|
||||
{
|
||||
return m_settings->get("notes").toString();
|
||||
}
|
||||
|
||||
void BaseInstance::setIconKey(QString val)
|
||||
{
|
||||
//FIXME: if no change, do not set. setting involves saving a file.
|
||||
m_settings->set("iconKey", val);
|
||||
emit propertiesChanged(this);
|
||||
}
|
||||
|
||||
QString BaseInstance::iconKey() const
|
||||
{
|
||||
return m_settings->get("iconKey").toString();
|
||||
}
|
||||
|
||||
void BaseInstance::setName(QString val)
|
||||
{
|
||||
//FIXME: if no change, do not set. setting involves saving a file.
|
||||
m_settings->set("name", val);
|
||||
emit propertiesChanged(this);
|
||||
}
|
||||
|
||||
QString BaseInstance::name() const
|
||||
{
|
||||
return m_settings->get("name").toString();
|
||||
}
|
||||
|
||||
QString BaseInstance::windowTitle() const
|
||||
{
|
||||
return "MultiMC: " + name().replace(QRegExp("[ \n\r\t]+"), " ");
|
||||
}
|
||||
|
||||
// FIXME: why is this here? move it to MinecraftInstance!!!
|
||||
QStringList BaseInstance::extraArguments() const
|
||||
{
|
||||
return Commandline::splitArgs(settings()->get("JvmArgs").toString());
|
||||
}
|
||||
|
||||
shared_qobject_ptr<LaunchTask> BaseInstance::getLaunchTask()
|
||||
{
|
||||
return m_launchProcess;
|
||||
}
|
270
launcher/BaseInstance.h
Normal file
270
launcher/BaseInstance.h
Normal file
@ -0,0 +1,270 @@
|
||||
/* Copyright 2013-2021 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 <cassert>
|
||||
|
||||
#include <QObject>
|
||||
#include "QObjectPtr.h"
|
||||
#include <QDateTime>
|
||||
#include <QSet>
|
||||
#include <QProcess>
|
||||
|
||||
#include "settings/SettingsObject.h"
|
||||
|
||||
#include "settings/INIFile.h"
|
||||
#include "BaseVersionList.h"
|
||||
#include "minecraft/auth/MojangAccount.h"
|
||||
#include "MessageLevel.h"
|
||||
#include "pathmatcher/IPathMatcher.h"
|
||||
|
||||
#include "net/Mode.h"
|
||||
|
||||
#include "minecraft/launch/MinecraftServerTarget.h"
|
||||
|
||||
class QDir;
|
||||
class Task;
|
||||
class LaunchTask;
|
||||
class BaseInstance;
|
||||
|
||||
// pointer for lazy people
|
||||
typedef std::shared_ptr<BaseInstance> InstancePtr;
|
||||
|
||||
/*!
|
||||
* \brief Base class for instances.
|
||||
* This class implements many functions that are common between instances and
|
||||
* provides a standard interface for all instances.
|
||||
*
|
||||
* To create a new instance type, create a new class inheriting from this class
|
||||
* and implement the pure virtual functions.
|
||||
*/
|
||||
class BaseInstance : public QObject, public std::enable_shared_from_this<BaseInstance>
|
||||
{
|
||||
Q_OBJECT
|
||||
protected:
|
||||
/// no-touchy!
|
||||
BaseInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString &rootDir);
|
||||
|
||||
public: /* types */
|
||||
enum class Status
|
||||
{
|
||||
Present,
|
||||
Gone // either nuked or invalidated
|
||||
};
|
||||
|
||||
public:
|
||||
/// virtual destructor to make sure the destruction is COMPLETE
|
||||
virtual ~BaseInstance() {};
|
||||
|
||||
virtual void saveNow() = 0;
|
||||
|
||||
/***
|
||||
* the instance has been invalidated - it is no longer tracked by MultiMC for some reason,
|
||||
* but it has not necessarily been deleted.
|
||||
*
|
||||
* Happens when the instance folder changes to some other location, or the instance is removed by external means.
|
||||
*/
|
||||
void invalidate();
|
||||
|
||||
/// The instance's ID. The ID SHALL be determined by MMC internally. The ID IS guaranteed to
|
||||
/// be unique.
|
||||
virtual QString id() const;
|
||||
|
||||
void setRunning(bool running);
|
||||
bool isRunning() const;
|
||||
int64_t totalTimePlayed() const;
|
||||
int64_t lastTimePlayed() const;
|
||||
void resetTimePlayed();
|
||||
|
||||
/// get the type of this instance
|
||||
QString instanceType() const;
|
||||
|
||||
/// Path to the instance's root directory.
|
||||
QString instanceRoot() const;
|
||||
|
||||
/// Path to the instance's game root directory.
|
||||
virtual QString gameRoot() const
|
||||
{
|
||||
return instanceRoot();
|
||||
}
|
||||
|
||||
QString name() const;
|
||||
void setName(QString val);
|
||||
|
||||
/// Value used for instance window titles
|
||||
QString windowTitle() const;
|
||||
|
||||
QString iconKey() const;
|
||||
void setIconKey(QString val);
|
||||
|
||||
QString notes() const;
|
||||
void setNotes(QString val);
|
||||
|
||||
QString getPreLaunchCommand();
|
||||
QString getPostExitCommand();
|
||||
QString getWrapperCommand();
|
||||
|
||||
/// guess log level from a line of game log
|
||||
virtual MessageLevel::Enum guessLevel(const QString &line, MessageLevel::Enum level)
|
||||
{
|
||||
return level;
|
||||
};
|
||||
|
||||
virtual QStringList extraArguments() const;
|
||||
|
||||
/// Traits. Normally inside the version, depends on instance implementation.
|
||||
virtual QSet <QString> traits() const = 0;
|
||||
|
||||
/**
|
||||
* Gets the time that the instance was last launched.
|
||||
* Stored in milliseconds since epoch.
|
||||
*/
|
||||
qint64 lastLaunch() const;
|
||||
/// Sets the last launched time to 'val' milliseconds since epoch
|
||||
void setLastLaunch(qint64 val = QDateTime::currentMSecsSinceEpoch());
|
||||
|
||||
/*!
|
||||
* \brief Gets this instance's settings object.
|
||||
* This settings object stores instance-specific settings.
|
||||
* \return A pointer to this instance's settings object.
|
||||
*/
|
||||
virtual SettingsObjectPtr settings() const;
|
||||
|
||||
/// returns a valid update task
|
||||
virtual shared_qobject_ptr<Task> createUpdateTask(Net::Mode mode) = 0;
|
||||
|
||||
/// returns a valid launcher (task container)
|
||||
virtual shared_qobject_ptr<LaunchTask> createLaunchTask(
|
||||
AuthSessionPtr account, MinecraftServerTargetPtr serverToJoin) = 0;
|
||||
|
||||
/// returns the current launch task (if any)
|
||||
shared_qobject_ptr<LaunchTask> getLaunchTask();
|
||||
|
||||
/*!
|
||||
* Create envrironment variables for running the instance
|
||||
*/
|
||||
virtual QProcessEnvironment createEnvironment() = 0;
|
||||
|
||||
/*!
|
||||
* Returns a matcher that can maps relative paths within the instance to whether they are 'log files'
|
||||
*/
|
||||
virtual IPathMatcher::Ptr getLogFileMatcher() = 0;
|
||||
|
||||
/*!
|
||||
* Returns the root folder to use for looking up log files
|
||||
*/
|
||||
virtual QString getLogFileRoot() = 0;
|
||||
|
||||
virtual QString getStatusbarDescription() = 0;
|
||||
|
||||
/// FIXME: this really should be elsewhere...
|
||||
virtual QString instanceConfigFolder() const = 0;
|
||||
|
||||
/// get variables this instance exports
|
||||
virtual QMap<QString, QString> getVariables() const = 0;
|
||||
|
||||
virtual QString typeName() const = 0;
|
||||
|
||||
bool hasVersionBroken() const
|
||||
{
|
||||
return m_hasBrokenVersion;
|
||||
}
|
||||
void setVersionBroken(bool value)
|
||||
{
|
||||
if(m_hasBrokenVersion != value)
|
||||
{
|
||||
m_hasBrokenVersion = value;
|
||||
emit propertiesChanged(this);
|
||||
}
|
||||
}
|
||||
|
||||
bool hasUpdateAvailable() const
|
||||
{
|
||||
return m_hasUpdate;
|
||||
}
|
||||
void setUpdateAvailable(bool value)
|
||||
{
|
||||
if(m_hasUpdate != value)
|
||||
{
|
||||
m_hasUpdate = value;
|
||||
emit propertiesChanged(this);
|
||||
}
|
||||
}
|
||||
|
||||
bool hasCrashed() const
|
||||
{
|
||||
return m_crashed;
|
||||
}
|
||||
void setCrashed(bool value)
|
||||
{
|
||||
if(m_crashed != value)
|
||||
{
|
||||
m_crashed = value;
|
||||
emit propertiesChanged(this);
|
||||
}
|
||||
}
|
||||
|
||||
virtual bool canLaunch() const;
|
||||
virtual bool canEdit() const = 0;
|
||||
virtual bool canExport() const = 0;
|
||||
|
||||
bool reloadSettings();
|
||||
|
||||
/**
|
||||
* 'print' a verbose description of the instance into a QStringList
|
||||
*/
|
||||
virtual QStringList verboseDescription(AuthSessionPtr session, MinecraftServerTargetPtr serverToJoin) = 0;
|
||||
|
||||
Status currentStatus() const;
|
||||
|
||||
int getConsoleMaxLines() const;
|
||||
bool shouldStopOnConsoleOverflow() const;
|
||||
|
||||
protected:
|
||||
void changeStatus(Status newStatus);
|
||||
|
||||
signals:
|
||||
/*!
|
||||
* \brief Signal emitted when properties relevant to the instance view change
|
||||
*/
|
||||
void propertiesChanged(BaseInstance *inst);
|
||||
|
||||
void launchTaskChanged(shared_qobject_ptr<LaunchTask>);
|
||||
|
||||
void runningStatusChanged(bool running);
|
||||
|
||||
void statusChanged(Status from, Status to);
|
||||
|
||||
protected slots:
|
||||
void iconUpdated(QString key);
|
||||
|
||||
protected: /* data */
|
||||
QString m_rootDir;
|
||||
SettingsObjectPtr m_settings;
|
||||
// InstanceFlags m_flags;
|
||||
bool m_isRunning = false;
|
||||
shared_qobject_ptr<LaunchTask> m_launchProcess;
|
||||
QDateTime m_timeStarted;
|
||||
|
||||
private: /* data */
|
||||
Status m_status = Status::Present;
|
||||
bool m_crashed = false;
|
||||
bool m_hasUpdate = false;
|
||||
bool m_hasBrokenVersion = false;
|
||||
};
|
||||
|
||||
Q_DECLARE_METATYPE(shared_qobject_ptr<BaseInstance>)
|
||||
//Q_DECLARE_METATYPE(BaseInstance::InstanceFlag)
|
||||
//Q_DECLARE_OPERATORS_FOR_FLAGS(BaseInstance::InstanceFlags)
|
59
launcher/BaseVersion.h
Normal file
59
launcher/BaseVersion.h
Normal file
@ -0,0 +1,59 @@
|
||||
/* Copyright 2013-2021 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 <memory>
|
||||
#include <QString>
|
||||
#include <QMetaType>
|
||||
|
||||
/*!
|
||||
* An abstract base class for versions.
|
||||
*/
|
||||
class BaseVersion
|
||||
{
|
||||
public:
|
||||
virtual ~BaseVersion() {}
|
||||
/*!
|
||||
* A string used to identify this version in config files.
|
||||
* This should be unique within the version list or shenanigans will occur.
|
||||
*/
|
||||
virtual QString descriptor() = 0;
|
||||
|
||||
/*!
|
||||
* The name of this version as it is displayed to the user.
|
||||
* For example: "1.5.1"
|
||||
*/
|
||||
virtual QString name() = 0;
|
||||
|
||||
/*!
|
||||
* This should return a string that describes
|
||||
* the kind of version this is (Stable, Beta, Snapshot, whatever)
|
||||
*/
|
||||
virtual QString typeString() const = 0;
|
||||
|
||||
virtual bool operator<(BaseVersion &a)
|
||||
{
|
||||
return name() < a.name();
|
||||
};
|
||||
virtual bool operator>(BaseVersion &a)
|
||||
{
|
||||
return name() > a.name();
|
||||
};
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<BaseVersion> BaseVersionPtr;
|
||||
|
||||
Q_DECLARE_METATYPE(BaseVersionPtr)
|
99
launcher/BaseVersionList.cpp
Normal file
99
launcher/BaseVersionList.cpp
Normal file
@ -0,0 +1,99 @@
|
||||
/* Copyright 2013-2021 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 "BaseVersionList.h"
|
||||
#include "BaseVersion.h"
|
||||
|
||||
BaseVersionList::BaseVersionList(QObject *parent) : QAbstractListModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
BaseVersionPtr BaseVersionList::findVersion(const QString &descriptor)
|
||||
{
|
||||
for (int i = 0; i < count(); i++)
|
||||
{
|
||||
if (at(i)->descriptor() == descriptor)
|
||||
return at(i);
|
||||
}
|
||||
return BaseVersionPtr();
|
||||
}
|
||||
|
||||
BaseVersionPtr BaseVersionList::getRecommended() const
|
||||
{
|
||||
if (count() <= 0)
|
||||
return BaseVersionPtr();
|
||||
else
|
||||
return at(0);
|
||||
}
|
||||
|
||||
QVariant BaseVersionList::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid())
|
||||
return QVariant();
|
||||
|
||||
if (index.row() > count())
|
||||
return QVariant();
|
||||
|
||||
BaseVersionPtr version = at(index.row());
|
||||
|
||||
switch (role)
|
||||
{
|
||||
case VersionPointerRole:
|
||||
return qVariantFromValue(version);
|
||||
|
||||
case VersionRole:
|
||||
return version->name();
|
||||
|
||||
case VersionIdRole:
|
||||
return version->descriptor();
|
||||
|
||||
case TypeRole:
|
||||
return version->typeString();
|
||||
|
||||
default:
|
||||
return QVariant();
|
||||
}
|
||||
}
|
||||
|
||||
BaseVersionList::RoleList BaseVersionList::providesRoles() const
|
||||
{
|
||||
return {VersionPointerRole, VersionRole, VersionIdRole, TypeRole};
|
||||
}
|
||||
|
||||
int BaseVersionList::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
// Return count
|
||||
return count();
|
||||
}
|
||||
|
||||
int BaseVersionList::columnCount(const QModelIndex &parent) const
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> BaseVersionList::roleNames() const
|
||||
{
|
||||
QHash<int, QByteArray> roles = QAbstractListModel::roleNames();
|
||||
roles.insert(VersionRole, "version");
|
||||
roles.insert(VersionIdRole, "versionId");
|
||||
roles.insert(ParentVersionRole, "parentGameVersion");
|
||||
roles.insert(RecommendedRole, "recommended");
|
||||
roles.insert(LatestRole, "latest");
|
||||
roles.insert(TypeRole, "type");
|
||||
roles.insert(BranchRole, "branch");
|
||||
roles.insert(PathRole, "path");
|
||||
roles.insert(ArchitectureRole, "architecture");
|
||||
return roles;
|
||||
}
|
121
launcher/BaseVersionList.h
Normal file
121
launcher/BaseVersionList.h
Normal file
@ -0,0 +1,121 @@
|
||||
/* Copyright 2013-2021 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 <QObject>
|
||||
#include <QVariant>
|
||||
#include <QAbstractListModel>
|
||||
|
||||
#include "BaseVersion.h"
|
||||
#include "tasks/Task.h"
|
||||
#include "QObjectPtr.h"
|
||||
|
||||
/*!
|
||||
* \brief Class that each instance type's version list derives from.
|
||||
* Version lists are the lists that keep track of the available game versions
|
||||
* for that instance. This list will not be loaded on startup. It will be loaded
|
||||
* when the list's load function is called. Before using the version list, you
|
||||
* should check to see if it has been loaded yet and if not, load the list.
|
||||
*
|
||||
* Note that this class also inherits from QAbstractListModel. Methods from that
|
||||
* class determine how this version list shows up in a list view. Said methods
|
||||
* all have a default implementation, but they can be overridden by plugins to
|
||||
* change the behavior of the list.
|
||||
*/
|
||||
class BaseVersionList : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum ModelRoles
|
||||
{
|
||||
VersionPointerRole = Qt::UserRole,
|
||||
VersionRole,
|
||||
VersionIdRole,
|
||||
ParentVersionRole,
|
||||
RecommendedRole,
|
||||
LatestRole,
|
||||
TypeRole,
|
||||
BranchRole,
|
||||
PathRole,
|
||||
ArchitectureRole,
|
||||
SortRole
|
||||
};
|
||||
typedef QList<int> RoleList;
|
||||
|
||||
explicit BaseVersionList(QObject *parent = 0);
|
||||
|
||||
/*!
|
||||
* \brief Gets a task that will reload the version list.
|
||||
* Simply execute the task to load the list.
|
||||
* The task returned by this function should reset the model when it's done.
|
||||
* \return A pointer to a task that reloads the version list.
|
||||
*/
|
||||
virtual shared_qobject_ptr<Task> getLoadTask() = 0;
|
||||
|
||||
//! Checks whether or not the list is loaded. If this returns false, the list should be
|
||||
//loaded.
|
||||
virtual bool isLoaded() = 0;
|
||||
|
||||
//! Gets the version at the given index.
|
||||
virtual const BaseVersionPtr at(int i) const = 0;
|
||||
|
||||
//! Returns the number of versions in the list.
|
||||
virtual int count() const = 0;
|
||||
|
||||
//////// List Model Functions ////////
|
||||
QVariant data(const QModelIndex &index, int role) const override;
|
||||
int rowCount(const QModelIndex &parent) const override;
|
||||
int columnCount(const QModelIndex &parent) const override;
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
//! which roles are provided by this version list?
|
||||
virtual RoleList providesRoles() const;
|
||||
|
||||
/*!
|
||||
* \brief Finds a version by its descriptor.
|
||||
* \param descriptor The descriptor of the version to find.
|
||||
* \return A const pointer to the version with the given descriptor. NULL if
|
||||
* one doesn't exist.
|
||||
*/
|
||||
virtual BaseVersionPtr findVersion(const QString &descriptor);
|
||||
|
||||
/*!
|
||||
* \brief Gets the recommended version from this list
|
||||
* If the list doesn't support recommended versions, this works exactly as getLatestStable
|
||||
*/
|
||||
virtual BaseVersionPtr getRecommended() const;
|
||||
|
||||
/*!
|
||||
* Sorts the version list.
|
||||
*/
|
||||
virtual void sortVersions() = 0;
|
||||
|
||||
protected
|
||||
slots:
|
||||
/*!
|
||||
* Updates this list with the given list of versions.
|
||||
* This is done by copying each version in the given list and inserting it
|
||||
* into this one.
|
||||
* We need to do this so that we can set the parents of the versions are set to this
|
||||
* version list. This can't be done in the load task, because the versions the load
|
||||
* task creates are on the load task's thread and Qt won't allow their parents
|
||||
* to be set to something created on another thread.
|
||||
* To get around that problem, we invoke this method on the GUI thread, which
|
||||
* then copies the versions and sets their parents correctly.
|
||||
* \param versions List of versions whose parents should be set.
|
||||
*/
|
||||
virtual void updateListData(QList<BaseVersionPtr> versions) = 0;
|
||||
};
|
1001
launcher/CMakeLists.txt
Normal file
1001
launcher/CMakeLists.txt
Normal file
File diff suppressed because it is too large
Load Diff
35
launcher/ColorCache.cpp
Normal file
35
launcher/ColorCache.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
#include "ColorCache.h"
|
||||
|
||||
|
||||
/**
|
||||
* Blend the color with the front color, adapting to the back color
|
||||
*/
|
||||
QColor ColorCache::blend(QColor color)
|
||||
{
|
||||
if (Rainbow::luma(m_front) > Rainbow::luma(m_back))
|
||||
{
|
||||
// for dark color schemes, produce a fitting color first
|
||||
color = Rainbow::tint(m_front, color, 0.5);
|
||||
}
|
||||
// adapt contrast
|
||||
return Rainbow::mix(m_front, color, m_bias);
|
||||
}
|
||||
|
||||
/**
|
||||
* Blend the color with the back color
|
||||
*/
|
||||
QColor ColorCache::blendBackground(QColor color)
|
||||
{
|
||||
// adapt contrast
|
||||
return Rainbow::mix(m_back, color, m_bias);
|
||||
}
|
||||
|
||||
void ColorCache::recolorAll()
|
||||
{
|
||||
auto iter = m_colors.begin();
|
||||
while(iter != m_colors.end())
|
||||
{
|
||||
iter->front = blend(iter->original);
|
||||
iter->back = blendBackground(iter->original);
|
||||
}
|
||||
}
|
119
launcher/ColorCache.h
Normal file
119
launcher/ColorCache.h
Normal file
@ -0,0 +1,119 @@
|
||||
#pragma once
|
||||
#include <QtGui/QColor>
|
||||
#include <rainbow.h>
|
||||
#include <MessageLevel.h>
|
||||
#include <QMap>
|
||||
|
||||
class ColorCache
|
||||
{
|
||||
public:
|
||||
ColorCache(QColor front, QColor back, qreal bias)
|
||||
{
|
||||
m_front = front;
|
||||
m_back = back;
|
||||
m_bias = bias;
|
||||
};
|
||||
|
||||
void addColor(int key, QColor color)
|
||||
{
|
||||
m_colors[key] = {color, blend(color), blendBackground(color)};
|
||||
}
|
||||
|
||||
void setForeground(QColor front)
|
||||
{
|
||||
if(m_front != front)
|
||||
{
|
||||
m_front = front;
|
||||
recolorAll();
|
||||
}
|
||||
}
|
||||
|
||||
void setBackground(QColor back)
|
||||
{
|
||||
if(m_back != back)
|
||||
{
|
||||
m_back = back;
|
||||
recolorAll();
|
||||
}
|
||||
}
|
||||
|
||||
QColor getFront(int key)
|
||||
{
|
||||
auto iter = m_colors.find(key);
|
||||
if(iter == m_colors.end())
|
||||
{
|
||||
return QColor();
|
||||
}
|
||||
return (*iter).front;
|
||||
}
|
||||
|
||||
QColor getBack(int key)
|
||||
{
|
||||
auto iter = m_colors.find(key);
|
||||
if(iter == m_colors.end())
|
||||
{
|
||||
return QColor();
|
||||
}
|
||||
return (*iter).back;
|
||||
}
|
||||
|
||||
/**
|
||||
* Blend the color with the front color, adapting to the back color
|
||||
*/
|
||||
QColor blend(QColor color);
|
||||
|
||||
/**
|
||||
* Blend the color with the back color
|
||||
*/
|
||||
QColor blendBackground(QColor color);
|
||||
|
||||
protected:
|
||||
void recolorAll();
|
||||
|
||||
protected:
|
||||
struct ColorEntry
|
||||
{
|
||||
QColor original;
|
||||
QColor front;
|
||||
QColor back;
|
||||
};
|
||||
|
||||
protected:
|
||||
qreal m_bias;
|
||||
QColor m_front;
|
||||
QColor m_back;
|
||||
QMap<int, ColorEntry> m_colors;
|
||||
};
|
||||
|
||||
class LogColorCache : public ColorCache
|
||||
{
|
||||
public:
|
||||
LogColorCache(QColor front, QColor back)
|
||||
: ColorCache(front, back, 1.0)
|
||||
{
|
||||
addColor((int)MessageLevel::MultiMC, QColor("purple"));
|
||||
addColor((int)MessageLevel::Debug, QColor("green"));
|
||||
addColor((int)MessageLevel::Warning, QColor("orange"));
|
||||
addColor((int)MessageLevel::Error, QColor("red"));
|
||||
addColor((int)MessageLevel::Fatal, QColor("red"));
|
||||
addColor((int)MessageLevel::Message, front);
|
||||
}
|
||||
|
||||
QColor getFront(MessageLevel::Enum level)
|
||||
{
|
||||
if(!m_colors.contains((int) level))
|
||||
{
|
||||
return ColorCache::getFront((int)MessageLevel::Message);
|
||||
}
|
||||
return ColorCache::getFront((int)level);
|
||||
}
|
||||
|
||||
QColor getBack(MessageLevel::Enum level)
|
||||
{
|
||||
if(level == MessageLevel::Fatal)
|
||||
{
|
||||
return QColor(Qt::black);
|
||||
}
|
||||
return QColor(Qt::transparent);
|
||||
}
|
||||
};
|
199
launcher/ColumnResizer.cpp
Normal file
199
launcher/ColumnResizer.cpp
Normal file
@ -0,0 +1,199 @@
|
||||
/*
|
||||
* Copyright 2011 Aurélien Gâteau <agateau@kde.org>
|
||||
* License: BSD-3-Clause
|
||||
*/
|
||||
#include <ColumnResizer.h>
|
||||
|
||||
#include <QDebug>
|
||||
#include <QEvent>
|
||||
#include <QFormLayout>
|
||||
#include <QGridLayout>
|
||||
#include <QTimer>
|
||||
#include <QWidget>
|
||||
|
||||
class FormLayoutWidgetItem : public QWidgetItem
|
||||
{
|
||||
public:
|
||||
FormLayoutWidgetItem(QWidget* widget, QFormLayout* formLayout, QFormLayout::ItemRole itemRole)
|
||||
: QWidgetItem(widget)
|
||||
, m_width(-1)
|
||||
, m_formLayout(formLayout)
|
||||
, m_itemRole(itemRole)
|
||||
{}
|
||||
|
||||
QSize sizeHint() const
|
||||
{
|
||||
QSize size = QWidgetItem::sizeHint();
|
||||
if (m_width != -1) {
|
||||
size.setWidth(m_width);
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
QSize minimumSize() const
|
||||
{
|
||||
QSize size = QWidgetItem::minimumSize();
|
||||
if (m_width != -1) {
|
||||
size.setWidth(m_width);
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
QSize maximumSize() const
|
||||
{
|
||||
QSize size = QWidgetItem::maximumSize();
|
||||
if (m_width != -1) {
|
||||
size.setWidth(m_width);
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
void setWidth(int width)
|
||||
{
|
||||
if (width != m_width) {
|
||||
m_width = width;
|
||||
invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
void setGeometry(const QRect& _rect)
|
||||
{
|
||||
QRect rect = _rect;
|
||||
int width = widget()->sizeHint().width();
|
||||
if (m_itemRole == QFormLayout::LabelRole && m_formLayout->labelAlignment() & Qt::AlignRight) {
|
||||
rect.setLeft(rect.right() - width);
|
||||
}
|
||||
QWidgetItem::setGeometry(rect);
|
||||
}
|
||||
|
||||
QFormLayout* formLayout() const
|
||||
{
|
||||
return m_formLayout;
|
||||
}
|
||||
|
||||
private:
|
||||
int m_width;
|
||||
QFormLayout* m_formLayout;
|
||||
QFormLayout::ItemRole m_itemRole;
|
||||
};
|
||||
|
||||
typedef QPair<QGridLayout*, int> GridColumnInfo;
|
||||
|
||||
class ColumnResizerPrivate
|
||||
{
|
||||
public:
|
||||
ColumnResizerPrivate(ColumnResizer* q_ptr)
|
||||
: q(q_ptr)
|
||||
, m_updateTimer(new QTimer(q))
|
||||
{
|
||||
m_updateTimer->setSingleShot(true);
|
||||
m_updateTimer->setInterval(0);
|
||||
QObject::connect(m_updateTimer, SIGNAL(timeout()), q, SLOT(updateWidth()));
|
||||
}
|
||||
|
||||
void scheduleWidthUpdate()
|
||||
{
|
||||
m_updateTimer->start();
|
||||
}
|
||||
|
||||
ColumnResizer* q;
|
||||
QTimer* m_updateTimer;
|
||||
QList<QWidget*> m_widgets;
|
||||
QList<FormLayoutWidgetItem*> m_wrWidgetItemList;
|
||||
QList<GridColumnInfo> m_gridColumnInfoList;
|
||||
};
|
||||
|
||||
ColumnResizer::ColumnResizer(QObject* parent)
|
||||
: QObject(parent)
|
||||
, d(new ColumnResizerPrivate(this))
|
||||
{}
|
||||
|
||||
ColumnResizer::~ColumnResizer()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
void ColumnResizer::addWidget(QWidget* widget)
|
||||
{
|
||||
d->m_widgets.append(widget);
|
||||
widget->installEventFilter(this);
|
||||
d->scheduleWidthUpdate();
|
||||
}
|
||||
|
||||
void ColumnResizer::updateWidth()
|
||||
{
|
||||
int width = 0;
|
||||
Q_FOREACH(QWidget* widget, d->m_widgets) {
|
||||
width = qMax(widget->sizeHint().width(), width);
|
||||
}
|
||||
Q_FOREACH(FormLayoutWidgetItem* item, d->m_wrWidgetItemList) {
|
||||
item->setWidth(width);
|
||||
item->formLayout()->update();
|
||||
}
|
||||
Q_FOREACH(GridColumnInfo info, d->m_gridColumnInfoList) {
|
||||
info.first->setColumnMinimumWidth(info.second, width);
|
||||
}
|
||||
}
|
||||
|
||||
bool ColumnResizer::eventFilter(QObject*, QEvent* event)
|
||||
{
|
||||
if (event->type() == QEvent::Resize) {
|
||||
d->scheduleWidthUpdate();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ColumnResizer::addWidgetsFromLayout(QLayout* layout, int column)
|
||||
{
|
||||
Q_ASSERT(column >= 0);
|
||||
QGridLayout* gridLayout = qobject_cast<QGridLayout*>(layout);
|
||||
QFormLayout* formLayout = qobject_cast<QFormLayout*>(layout);
|
||||
if (gridLayout) {
|
||||
addWidgetsFromGridLayout(gridLayout, column);
|
||||
} else if (formLayout) {
|
||||
if (column > QFormLayout::SpanningRole) {
|
||||
qCritical() << "column should not be more than" << QFormLayout::SpanningRole << "for QFormLayout";
|
||||
return;
|
||||
}
|
||||
QFormLayout::ItemRole role = static_cast<QFormLayout::ItemRole>(column);
|
||||
addWidgetsFromFormLayout(formLayout, role);
|
||||
} else {
|
||||
qCritical() << "Don't know how to handle layout" << layout;
|
||||
}
|
||||
}
|
||||
|
||||
void ColumnResizer::addWidgetsFromGridLayout(QGridLayout* layout, int column)
|
||||
{
|
||||
for (int row = 0; row < layout->rowCount(); ++row) {
|
||||
QLayoutItem* item = layout->itemAtPosition(row, column);
|
||||
if (!item) {
|
||||
continue;
|
||||
}
|
||||
QWidget* widget = item->widget();
|
||||
if (!widget) {
|
||||
continue;
|
||||
}
|
||||
addWidget(widget);
|
||||
}
|
||||
d->m_gridColumnInfoList << GridColumnInfo(layout, column);
|
||||
}
|
||||
|
||||
void ColumnResizer::addWidgetsFromFormLayout(QFormLayout* layout, QFormLayout::ItemRole role)
|
||||
{
|
||||
for (int row = 0; row < layout->rowCount(); ++row) {
|
||||
QLayoutItem* item = layout->itemAt(row, role);
|
||||
if (!item) {
|
||||
continue;
|
||||
}
|
||||
QWidget* widget = item->widget();
|
||||
if (!widget) {
|
||||
continue;
|
||||
}
|
||||
layout->removeItem(item);
|
||||
delete item;
|
||||
FormLayoutWidgetItem* newItem = new FormLayoutWidgetItem(widget, layout, role);
|
||||
layout->setItem(row, role, newItem);
|
||||
addWidget(widget);
|
||||
d->m_wrWidgetItemList << newItem;
|
||||
}
|
||||
}
|
41
launcher/ColumnResizer.h
Normal file
41
launcher/ColumnResizer.h
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2011 Aurélien Gâteau <agateau@kde.org>
|
||||
* License: BSD-3-Clause
|
||||
*/
|
||||
#ifndef COLUMNRESIZER_H
|
||||
#define COLUMNRESIZER_H
|
||||
|
||||
#include <QFormLayout>
|
||||
|
||||
#include <QtCore/QObject>
|
||||
#include <QtCore/QList>
|
||||
|
||||
class QEvent;
|
||||
class QGridLayout;
|
||||
class QLayout;
|
||||
class QWidget;
|
||||
|
||||
class ColumnResizerPrivate;
|
||||
class ColumnResizer : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ColumnResizer(QObject* parent = 0);
|
||||
~ColumnResizer();
|
||||
|
||||
void addWidget(QWidget* widget);
|
||||
void addWidgetsFromLayout(QLayout*, int column);
|
||||
void addWidgetsFromGridLayout(QGridLayout*, int column);
|
||||
void addWidgetsFromFormLayout(QFormLayout*, QFormLayout::ItemRole role);
|
||||
|
||||
private Q_SLOTS:
|
||||
void updateWidth();
|
||||
|
||||
protected:
|
||||
bool eventFilter(QObject*, QEvent* event);
|
||||
|
||||
private:
|
||||
ColumnResizerPrivate* const d;
|
||||
};
|
||||
|
||||
#endif /* COLUMNRESIZER_H */
|
483
launcher/Commandline.cpp
Normal file
483
launcher/Commandline.cpp
Normal file
@ -0,0 +1,483 @@
|
||||
/* Copyright 2013-2021 MultiMC Contributors
|
||||
*
|
||||
* Authors: Orochimarufan <orochimarufan.x3@gmail.com>
|
||||
*
|
||||
* 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 "Commandline.h"
|
||||
|
||||
/**
|
||||
* @file libutil/src/cmdutils.cpp
|
||||
*/
|
||||
|
||||
namespace Commandline
|
||||
{
|
||||
|
||||
// commandline splitter
|
||||
QStringList splitArgs(QString args)
|
||||
{
|
||||
QStringList argv;
|
||||
QString current;
|
||||
bool escape = false;
|
||||
QChar inquotes;
|
||||
for (int i = 0; i < args.length(); i++)
|
||||
{
|
||||
QChar cchar = args.at(i);
|
||||
|
||||
// \ escaped
|
||||
if (escape)
|
||||
{
|
||||
current += cchar;
|
||||
escape = false;
|
||||
// in "quotes"
|
||||
}
|
||||
else if (!inquotes.isNull())
|
||||
{
|
||||
if (cchar == '\\')
|
||||
escape = true;
|
||||
else if (cchar == inquotes)
|
||||
inquotes = 0;
|
||||
else
|
||||
current += cchar;
|
||||
// otherwise
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cchar == ' ')
|
||||
{
|
||||
if (!current.isEmpty())
|
||||
{
|
||||
argv << current;
|
||||
current.clear();
|
||||
}
|
||||
}
|
||||
else if (cchar == '"' || cchar == '\'')
|
||||
inquotes = cchar;
|
||||
else
|
||||
current += cchar;
|
||||
}
|
||||
}
|
||||
if (!current.isEmpty())
|
||||
argv << current;
|
||||
return argv;
|
||||
}
|
||||
|
||||
Parser::Parser(FlagStyle::Enum flagStyle, ArgumentStyle::Enum argStyle)
|
||||
{
|
||||
m_flagStyle = flagStyle;
|
||||
m_argStyle = argStyle;
|
||||
}
|
||||
|
||||
// styles setter/getter
|
||||
void Parser::setArgumentStyle(ArgumentStyle::Enum style)
|
||||
{
|
||||
m_argStyle = style;
|
||||
}
|
||||
ArgumentStyle::Enum Parser::argumentStyle()
|
||||
{
|
||||
return m_argStyle;
|
||||
}
|
||||
|
||||
void Parser::setFlagStyle(FlagStyle::Enum style)
|
||||
{
|
||||
m_flagStyle = style;
|
||||
}
|
||||
FlagStyle::Enum Parser::flagStyle()
|
||||
{
|
||||
return m_flagStyle;
|
||||
}
|
||||
|
||||
// setup methods
|
||||
void Parser::addSwitch(QString name, bool def)
|
||||
{
|
||||
if (m_params.contains(name))
|
||||
throw "Name not unique";
|
||||
|
||||
OptionDef *param = new OptionDef;
|
||||
param->type = otSwitch;
|
||||
param->name = name;
|
||||
param->metavar = QString("<%1>").arg(name);
|
||||
param->def = def;
|
||||
|
||||
m_options[name] = param;
|
||||
m_params[name] = (CommonDef *)param;
|
||||
m_optionList.append(param);
|
||||
}
|
||||
|
||||
void Parser::addOption(QString name, QVariant def)
|
||||
{
|
||||
if (m_params.contains(name))
|
||||
throw "Name not unique";
|
||||
|
||||
OptionDef *param = new OptionDef;
|
||||
param->type = otOption;
|
||||
param->name = name;
|
||||
param->metavar = QString("<%1>").arg(name);
|
||||
param->def = def;
|
||||
|
||||
m_options[name] = param;
|
||||
m_params[name] = (CommonDef *)param;
|
||||
m_optionList.append(param);
|
||||
}
|
||||
|
||||
void Parser::addArgument(QString name, bool required, QVariant def)
|
||||
{
|
||||
if (m_params.contains(name))
|
||||
throw "Name not unique";
|
||||
|
||||
PositionalDef *param = new PositionalDef;
|
||||
param->name = name;
|
||||
param->def = def;
|
||||
param->required = required;
|
||||
param->metavar = name;
|
||||
|
||||
m_positionals.append(param);
|
||||
m_params[name] = (CommonDef *)param;
|
||||
}
|
||||
|
||||
void Parser::addDocumentation(QString name, QString doc, QString metavar)
|
||||
{
|
||||
if (!m_params.contains(name))
|
||||
throw "Name does not exist";
|
||||
|
||||
CommonDef *param = m_params[name];
|
||||
param->doc = doc;
|
||||
if (!metavar.isNull())
|
||||
param->metavar = metavar;
|
||||
}
|
||||
|
||||
void Parser::addShortOpt(QString name, QChar flag)
|
||||
{
|
||||
if (!m_params.contains(name))
|
||||
throw "Name does not exist";
|
||||
if (!m_options.contains(name))
|
||||
throw "Name is not an Option or Swtich";
|
||||
|
||||
OptionDef *param = m_options[name];
|
||||
m_flags[flag] = param;
|
||||
param->flag = flag;
|
||||
}
|
||||
|
||||
// help methods
|
||||
QString Parser::compileHelp(QString progName, int helpIndent, bool useFlags)
|
||||
{
|
||||
QStringList help;
|
||||
help << compileUsage(progName, useFlags) << "\r\n";
|
||||
|
||||
// positionals
|
||||
if (!m_positionals.isEmpty())
|
||||
{
|
||||
help << "\r\n";
|
||||
help << "Positional arguments:\r\n";
|
||||
QListIterator<PositionalDef *> it2(m_positionals);
|
||||
while (it2.hasNext())
|
||||
{
|
||||
PositionalDef *param = it2.next();
|
||||
help << " " << param->metavar;
|
||||
help << " " << QString(helpIndent - param->metavar.length() - 1, ' ');
|
||||
help << param->doc << "\r\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Options
|
||||
if (!m_optionList.isEmpty())
|
||||
{
|
||||
help << "\r\n";
|
||||
QString optPrefix, flagPrefix;
|
||||
getPrefix(optPrefix, flagPrefix);
|
||||
|
||||
help << "Options & Switches:\r\n";
|
||||
QListIterator<OptionDef *> it(m_optionList);
|
||||
while (it.hasNext())
|
||||
{
|
||||
OptionDef *option = it.next();
|
||||
help << " ";
|
||||
int nameLength = optPrefix.length() + option->name.length();
|
||||
if (!option->flag.isNull())
|
||||
{
|
||||
nameLength += 3 + flagPrefix.length();
|
||||
help << flagPrefix << option->flag << ", ";
|
||||
}
|
||||
help << optPrefix << option->name;
|
||||
if (option->type == otOption)
|
||||
{
|
||||
QString arg = QString("%1%2").arg(
|
||||
((m_argStyle == ArgumentStyle::Equals) ? "=" : " "), option->metavar);
|
||||
nameLength += arg.length();
|
||||
help << arg;
|
||||
}
|
||||
help << " " << QString(helpIndent - nameLength - 1, ' ');
|
||||
help << option->doc << "\r\n";
|
||||
}
|
||||
}
|
||||
|
||||
return help.join("");
|
||||
}
|
||||
|
||||
QString Parser::compileUsage(QString progName, bool useFlags)
|
||||
{
|
||||
QStringList usage;
|
||||
usage << "Usage: " << progName;
|
||||
|
||||
QString optPrefix, flagPrefix;
|
||||
getPrefix(optPrefix, flagPrefix);
|
||||
|
||||
// options
|
||||
QListIterator<OptionDef *> it(m_optionList);
|
||||
while (it.hasNext())
|
||||
{
|
||||
OptionDef *option = it.next();
|
||||
usage << " [";
|
||||
if (!option->flag.isNull() && useFlags)
|
||||
usage << flagPrefix << option->flag;
|
||||
else
|
||||
usage << optPrefix << option->name;
|
||||
if (option->type == otOption)
|
||||
usage << ((m_argStyle == ArgumentStyle::Equals) ? "=" : " ") << option->metavar;
|
||||
usage << "]";
|
||||
}
|
||||
|
||||
// arguments
|
||||
QListIterator<PositionalDef *> it2(m_positionals);
|
||||
while (it2.hasNext())
|
||||
{
|
||||
PositionalDef *param = it2.next();
|
||||
usage << " " << (param->required ? "<" : "[");
|
||||
usage << param->metavar;
|
||||
usage << (param->required ? ">" : "]");
|
||||
}
|
||||
|
||||
return usage.join("");
|
||||
}
|
||||
|
||||
// parsing
|
||||
QHash<QString, QVariant> Parser::parse(QStringList argv)
|
||||
{
|
||||
QHash<QString, QVariant> map;
|
||||
|
||||
QStringListIterator it(argv);
|
||||
QString programName = it.next();
|
||||
|
||||
QString optionPrefix;
|
||||
QString flagPrefix;
|
||||
QListIterator<PositionalDef *> positionals(m_positionals);
|
||||
QStringList expecting;
|
||||
|
||||
getPrefix(optionPrefix, flagPrefix);
|
||||
|
||||
while (it.hasNext())
|
||||
{
|
||||
QString arg = it.next();
|
||||
|
||||
if (!expecting.isEmpty())
|
||||
// we were expecting an argument
|
||||
{
|
||||
QString name = expecting.first();
|
||||
/*
|
||||
if (map.contains(name))
|
||||
throw ParsingError(
|
||||
QString("Option %2%1 was given multiple times").arg(name, optionPrefix));
|
||||
*/
|
||||
map[name] = QVariant(arg);
|
||||
|
||||
expecting.removeFirst();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arg.startsWith(optionPrefix))
|
||||
// we have an option
|
||||
{
|
||||
// qDebug("Found option %s", qPrintable(arg));
|
||||
|
||||
QString name = arg.mid(optionPrefix.length());
|
||||
QString equals;
|
||||
|
||||
if ((m_argStyle == ArgumentStyle::Equals ||
|
||||
m_argStyle == ArgumentStyle::SpaceAndEquals) &&
|
||||
name.contains("="))
|
||||
{
|
||||
int i = name.indexOf("=");
|
||||
equals = name.mid(i + 1);
|
||||
name = name.left(i);
|
||||
}
|
||||
|
||||
if (m_options.contains(name))
|
||||
{
|
||||
/*
|
||||
if (map.contains(name))
|
||||
throw ParsingError(QString("Option %2%1 was given multiple times")
|
||||
.arg(name, optionPrefix));
|
||||
*/
|
||||
OptionDef *option = m_options[name];
|
||||
if (option->type == otSwitch)
|
||||
map[name] = true;
|
||||
else // if (option->type == otOption)
|
||||
{
|
||||
if (m_argStyle == ArgumentStyle::Space)
|
||||
expecting.append(name);
|
||||
else if (!equals.isNull())
|
||||
map[name] = equals;
|
||||
else if (m_argStyle == ArgumentStyle::SpaceAndEquals)
|
||||
expecting.append(name);
|
||||
else
|
||||
throw ParsingError(QString("Option %2%1 reqires an argument.")
|
||||
.arg(name, optionPrefix));
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
throw ParsingError(QString("Unknown Option %2%1").arg(name, optionPrefix));
|
||||
}
|
||||
|
||||
if (arg.startsWith(flagPrefix))
|
||||
// we have (a) flag(s)
|
||||
{
|
||||
// qDebug("Found flags %s", qPrintable(arg));
|
||||
|
||||
QString flags = arg.mid(flagPrefix.length());
|
||||
QString equals;
|
||||
|
||||
if ((m_argStyle == ArgumentStyle::Equals ||
|
||||
m_argStyle == ArgumentStyle::SpaceAndEquals) &&
|
||||
flags.contains("="))
|
||||
{
|
||||
int i = flags.indexOf("=");
|
||||
equals = flags.mid(i + 1);
|
||||
flags = flags.left(i);
|
||||
}
|
||||
|
||||
for (int i = 0; i < flags.length(); i++)
|
||||
{
|
||||
QChar flag = flags.at(i);
|
||||
|
||||
if (!m_flags.contains(flag))
|
||||
throw ParsingError(QString("Unknown flag %2%1").arg(flag, flagPrefix));
|
||||
|
||||
OptionDef *option = m_flags[flag];
|
||||
/*
|
||||
if (map.contains(option->name))
|
||||
throw ParsingError(QString("Option %2%1 was given multiple times")
|
||||
.arg(option->name, optionPrefix));
|
||||
*/
|
||||
if (option->type == otSwitch)
|
||||
map[option->name] = true;
|
||||
else // if (option->type == otOption)
|
||||
{
|
||||
if (m_argStyle == ArgumentStyle::Space)
|
||||
expecting.append(option->name);
|
||||
else if (!equals.isNull())
|
||||
if (i == flags.length() - 1)
|
||||
map[option->name] = equals;
|
||||
else
|
||||
throw ParsingError(QString("Flag %4%2 of Argument-requiring Option "
|
||||
"%1 not last flag in %4%3")
|
||||
.arg(option->name, flag, flags, flagPrefix));
|
||||
else if (m_argStyle == ArgumentStyle::SpaceAndEquals)
|
||||
expecting.append(option->name);
|
||||
else
|
||||
throw ParsingError(QString("Option %1 reqires an argument. (flag %3%2)")
|
||||
.arg(option->name, flag, flagPrefix));
|
||||
}
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// must be a positional argument
|
||||
if (!positionals.hasNext())
|
||||
throw ParsingError(QString("Don't know what to do with '%1'").arg(arg));
|
||||
|
||||
PositionalDef *param = positionals.next();
|
||||
|
||||
map[param->name] = arg;
|
||||
}
|
||||
|
||||
// check if we're missing something
|
||||
if (!expecting.isEmpty())
|
||||
throw ParsingError(QString("Was still expecting arguments for %2%1").arg(
|
||||
expecting.join(QString(", ") + optionPrefix), optionPrefix));
|
||||
|
||||
while (positionals.hasNext())
|
||||
{
|
||||
PositionalDef *param = positionals.next();
|
||||
if (param->required)
|
||||
throw ParsingError(
|
||||
QString("Missing required positional argument '%1'").arg(param->name));
|
||||
else
|
||||
map[param->name] = param->def;
|
||||
}
|
||||
|
||||
// fill out gaps
|
||||
QListIterator<OptionDef *> iter(m_optionList);
|
||||
while (iter.hasNext())
|
||||
{
|
||||
OptionDef *option = iter.next();
|
||||
if (!map.contains(option->name))
|
||||
map[option->name] = option->def;
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
// clear defs
|
||||
void Parser::clear()
|
||||
{
|
||||
m_flags.clear();
|
||||
m_params.clear();
|
||||
m_options.clear();
|
||||
|
||||
QMutableListIterator<OptionDef *> it(m_optionList);
|
||||
while (it.hasNext())
|
||||
{
|
||||
OptionDef *option = it.next();
|
||||
it.remove();
|
||||
delete option;
|
||||
}
|
||||
|
||||
QMutableListIterator<PositionalDef *> it2(m_positionals);
|
||||
while (it2.hasNext())
|
||||
{
|
||||
PositionalDef *arg = it2.next();
|
||||
it2.remove();
|
||||
delete arg;
|
||||
}
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Parser::~Parser()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
// getPrefix
|
||||
void Parser::getPrefix(QString &opt, QString &flag)
|
||||
{
|
||||
if (m_flagStyle == FlagStyle::Windows)
|
||||
opt = flag = "/";
|
||||
else if (m_flagStyle == FlagStyle::Unix)
|
||||
opt = flag = "-";
|
||||
// else if (m_flagStyle == FlagStyle::GNU)
|
||||
else
|
||||
{
|
||||
opt = "--";
|
||||
flag = "-";
|
||||
}
|
||||
}
|
||||
|
||||
// ParsingError
|
||||
ParsingError::ParsingError(const QString &what) : std::runtime_error(what.toStdString())
|
||||
{
|
||||
}
|
||||
}
|
250
launcher/Commandline.h
Normal file
250
launcher/Commandline.h
Normal file
@ -0,0 +1,250 @@
|
||||
/* Copyright 2013-2021 MultiMC Contributors
|
||||
*
|
||||
* Authors: Orochimarufan <orochimarufan.x3@gmail.com>
|
||||
*
|
||||
* 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 <exception>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <QString>
|
||||
#include <QVariant>
|
||||
#include <QHash>
|
||||
#include <QStringList>
|
||||
|
||||
/**
|
||||
* @file libutil/include/cmdutils.h
|
||||
* @brief commandline parsing and processing utilities
|
||||
*/
|
||||
|
||||
namespace Commandline
|
||||
{
|
||||
|
||||
/**
|
||||
* @brief split a string into argv items like a shell would do
|
||||
* @param args the argument string
|
||||
* @return a QStringList containing all arguments
|
||||
*/
|
||||
QStringList splitArgs(QString args);
|
||||
|
||||
/**
|
||||
* @brief The FlagStyle enum
|
||||
* Specifies how flags are decorated
|
||||
*/
|
||||
|
||||
namespace FlagStyle
|
||||
{
|
||||
enum Enum
|
||||
{
|
||||
GNU, /**< --option and -o (GNU Style) */
|
||||
Unix, /**< -option and -o (Unix Style) */
|
||||
Windows, /**< /option and /o (Windows Style) */
|
||||
#ifdef Q_OS_WIN32
|
||||
Default = Windows
|
||||
#else
|
||||
Default = GNU
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The ArgumentStyle enum
|
||||
*/
|
||||
namespace ArgumentStyle
|
||||
{
|
||||
enum Enum
|
||||
{
|
||||
Space, /**< --option value */
|
||||
Equals, /**< --option=value */
|
||||
SpaceAndEquals, /**< --option[= ]value */
|
||||
#ifdef Q_OS_WIN32
|
||||
Default = Equals
|
||||
#else
|
||||
Default = SpaceAndEquals
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The ParsingError class
|
||||
*/
|
||||
class ParsingError : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
ParsingError(const QString &what);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief The Parser class
|
||||
*/
|
||||
class Parser
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Parser constructor
|
||||
* @param flagStyle the FlagStyle to use in this Parser
|
||||
* @param argStyle the ArgumentStyle to use in this Parser
|
||||
*/
|
||||
Parser(FlagStyle::Enum flagStyle = FlagStyle::Default,
|
||||
ArgumentStyle::Enum argStyle = ArgumentStyle::Default);
|
||||
|
||||
/**
|
||||
* @brief set the flag style
|
||||
* @param style
|
||||
*/
|
||||
void setFlagStyle(FlagStyle::Enum style);
|
||||
|
||||
/**
|
||||
* @brief get the flag style
|
||||
* @return
|
||||
*/
|
||||
FlagStyle::Enum flagStyle();
|
||||
|
||||
/**
|
||||
* @brief set the argument style
|
||||
* @param style
|
||||
*/
|
||||
void setArgumentStyle(ArgumentStyle::Enum style);
|
||||
|
||||
/**
|
||||
* @brief get the argument style
|
||||
* @return
|
||||
*/
|
||||
ArgumentStyle::Enum argumentStyle();
|
||||
|
||||
/**
|
||||
* @brief define a boolean switch
|
||||
* @param name the parameter name
|
||||
* @param def the default value
|
||||
*/
|
||||
void addSwitch(QString name, bool def = false);
|
||||
|
||||
/**
|
||||
* @brief define an option that takes an additional argument
|
||||
* @param name the parameter name
|
||||
* @param def the default value
|
||||
*/
|
||||
void addOption(QString name, QVariant def = QVariant());
|
||||
|
||||
/**
|
||||
* @brief define a positional argument
|
||||
* @param name the parameter name
|
||||
* @param required wether this argument is required
|
||||
* @param def the default value
|
||||
*/
|
||||
void addArgument(QString name, bool required = true, QVariant def = QVariant());
|
||||
|
||||
/**
|
||||
* @brief adds a flag to an existing parameter
|
||||
* @param name the (existing) parameter name
|
||||
* @param flag the flag character
|
||||
* @see addSwitch addArgument addOption
|
||||
* Note: any one parameter can only have one flag
|
||||
*/
|
||||
void addShortOpt(QString name, QChar flag);
|
||||
|
||||
/**
|
||||
* @brief adds documentation to a Parameter
|
||||
* @param name the parameter name
|
||||
* @param metavar a string to be displayed as placeholder for the value
|
||||
* @param doc a QString containing the documentation
|
||||
* Note: on positional arguments, metavar replaces the name as displayed.
|
||||
* on options , metavar replaces the value placeholder
|
||||
*/
|
||||
void addDocumentation(QString name, QString doc, QString metavar = QString());
|
||||
|
||||
/**
|
||||
* @brief generate a help message
|
||||
* @param progName the program name to use in the help message
|
||||
* @param helpIndent how much the parameter documentation should be indented
|
||||
* @param flagsInUsage whether we should use flags instead of options in the usage
|
||||
* @return a help message
|
||||
*/
|
||||
QString compileHelp(QString progName, int helpIndent = 22, bool flagsInUsage = true);
|
||||
|
||||
/**
|
||||
* @brief generate a short usage message
|
||||
* @param progName the program name to use in the usage message
|
||||
* @param useFlags whether we should use flags instead of options
|
||||
* @return a usage message
|
||||
*/
|
||||
QString compileUsage(QString progName, bool useFlags = true);
|
||||
|
||||
/**
|
||||
* @brief parse
|
||||
* @param argv a QStringList containing the program ARGV
|
||||
* @return a QHash mapping argument names to their values
|
||||
*/
|
||||
QHash<QString, QVariant> parse(QStringList argv);
|
||||
|
||||
/**
|
||||
* @brief clear all definitions
|
||||
*/
|
||||
void clear();
|
||||
|
||||
~Parser();
|
||||
|
||||
private:
|
||||
FlagStyle::Enum m_flagStyle;
|
||||
ArgumentStyle::Enum m_argStyle;
|
||||
|
||||
enum OptionType
|
||||
{
|
||||
otSwitch,
|
||||
otOption
|
||||
};
|
||||
|
||||
// Important: the common part MUST BE COMMON ON ALL THREE structs
|
||||
struct CommonDef
|
||||
{
|
||||
QString name;
|
||||
QString doc;
|
||||
QString metavar;
|
||||
QVariant def;
|
||||
};
|
||||
|
||||
struct OptionDef
|
||||
{
|
||||
// common
|
||||
QString name;
|
||||
QString doc;
|
||||
QString metavar;
|
||||
QVariant def;
|
||||
// option
|
||||
OptionType type;
|
||||
QChar flag;
|
||||
};
|
||||
|
||||
struct PositionalDef
|
||||
{
|
||||
// common
|
||||
QString name;
|
||||
QString doc;
|
||||
QString metavar;
|
||||
QVariant def;
|
||||
// positional
|
||||
bool required;
|
||||
};
|
||||
|
||||
QHash<QString, OptionDef *> m_options;
|
||||
QHash<QChar, OptionDef *> m_flags;
|
||||
QHash<QString, CommonDef *> m_params;
|
||||
QList<PositionalDef *> m_positionals;
|
||||
QList<OptionDef *> m_optionList;
|
||||
|
||||
void getPrefix(QString &opt, QString &flag);
|
||||
};
|
||||
}
|
35
launcher/DefaultVariable.h
Normal file
35
launcher/DefaultVariable.h
Normal file
@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
template <typename T>
|
||||
class DefaultVariable
|
||||
{
|
||||
public:
|
||||
DefaultVariable(const T & value)
|
||||
{
|
||||
defaultValue = value;
|
||||
}
|
||||
DefaultVariable<T> & operator =(const T & value)
|
||||
{
|
||||
currentValue = value;
|
||||
is_default = currentValue == defaultValue;
|
||||
is_explicit = true;
|
||||
return *this;
|
||||
}
|
||||
operator const T &() const
|
||||
{
|
||||
return is_default ? defaultValue : currentValue;
|
||||
}
|
||||
bool isDefault() const
|
||||
{
|
||||
return is_default;
|
||||
}
|
||||
bool isExplicit() const
|
||||
{
|
||||
return is_explicit;
|
||||
}
|
||||
private:
|
||||
T currentValue;
|
||||
T defaultValue;
|
||||
bool is_default = true;
|
||||
bool is_explicit = false;
|
||||
};
|
149
launcher/DesktopServices.cpp
Normal file
149
launcher/DesktopServices.cpp
Normal file
@ -0,0 +1,149 @@
|
||||
#include "DesktopServices.h"
|
||||
#include <QDir>
|
||||
#include <QDesktopServices>
|
||||
#include <QProcess>
|
||||
#include <QDebug>
|
||||
|
||||
/**
|
||||
* This shouldn't exist, but until QTBUG-9328 and other unreported bugs are fixed, it needs to be a thing.
|
||||
*/
|
||||
#if defined(Q_OS_LINUX)
|
||||
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
template <typename T>
|
||||
bool IndirectOpen(T callable, qint64 *pid_forked = nullptr)
|
||||
{
|
||||
auto pid = fork();
|
||||
if(pid_forked)
|
||||
{
|
||||
if(pid > 0)
|
||||
*pid_forked = pid;
|
||||
else
|
||||
*pid_forked = 0;
|
||||
}
|
||||
if(pid == -1)
|
||||
{
|
||||
qWarning() << "IndirectOpen failed to fork: " << errno;
|
||||
return false;
|
||||
}
|
||||
// child - do the stuff
|
||||
if(pid == 0)
|
||||
{
|
||||
// unset all this garbage so it doesn't get passed to the child process
|
||||
qunsetenv("LD_PRELOAD");
|
||||
qunsetenv("LD_LIBRARY_PATH");
|
||||
qunsetenv("LD_DEBUG");
|
||||
qunsetenv("QT_PLUGIN_PATH");
|
||||
qunsetenv("QT_FONTPATH");
|
||||
|
||||
// open the URL
|
||||
auto status = callable();
|
||||
|
||||
// detach from the parent process group.
|
||||
setsid();
|
||||
|
||||
// die. now. do not clean up anything, it would just hang forever.
|
||||
_exit(status ? 0 : 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
//parent - assume it worked.
|
||||
int status;
|
||||
while (waitpid(pid, &status, 0))
|
||||
{
|
||||
if(WIFEXITED(status))
|
||||
{
|
||||
return WEXITSTATUS(status) == 0;
|
||||
}
|
||||
if(WIFSIGNALED(status))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace DesktopServices {
|
||||
bool openDirectory(const QString &path, bool ensureExists)
|
||||
{
|
||||
qDebug() << "Opening directory" << path;
|
||||
QDir parentPath;
|
||||
QDir dir(path);
|
||||
if (!dir.exists())
|
||||
{
|
||||
parentPath.mkpath(dir.absolutePath());
|
||||
}
|
||||
auto f = [&]()
|
||||
{
|
||||
return QDesktopServices::openUrl(QUrl::fromLocalFile(dir.absolutePath()));
|
||||
};
|
||||
#if defined(Q_OS_LINUX)
|
||||
return IndirectOpen(f);
|
||||
#else
|
||||
return f();
|
||||
#endif
|
||||
}
|
||||
|
||||
bool openFile(const QString &path)
|
||||
{
|
||||
qDebug() << "Opening file" << path;
|
||||
auto f = [&]()
|
||||
{
|
||||
return QDesktopServices::openUrl(QUrl::fromLocalFile(path));
|
||||
};
|
||||
#if defined(Q_OS_LINUX)
|
||||
return IndirectOpen(f);
|
||||
#else
|
||||
return f();
|
||||
#endif
|
||||
}
|
||||
|
||||
bool openFile(const QString &application, const QString &path, const QString &workingDirectory, qint64 *pid)
|
||||
{
|
||||
qDebug() << "Opening file" << path << "using" << application;
|
||||
#if defined(Q_OS_LINUX)
|
||||
// FIXME: the pid here is fake. So if something depends on it, it will likely misbehave
|
||||
return IndirectOpen([&]()
|
||||
{
|
||||
return QProcess::startDetached(application, QStringList() << path, workingDirectory);
|
||||
}, pid);
|
||||
#else
|
||||
return QProcess::startDetached(application, QStringList() << path, workingDirectory, pid);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool run(const QString &application, const QStringList &args, const QString &workingDirectory, qint64 *pid)
|
||||
{
|
||||
qDebug() << "Running" << application << "with args" << args.join(' ');
|
||||
#if defined(Q_OS_LINUX)
|
||||
// FIXME: the pid here is fake. So if something depends on it, it will likely misbehave
|
||||
return IndirectOpen([&]()
|
||||
{
|
||||
return QProcess::startDetached(application, args, workingDirectory);
|
||||
}, pid);
|
||||
#else
|
||||
return QProcess::startDetached(application, args, workingDirectory, pid);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool openUrl(const QUrl &url)
|
||||
{
|
||||
qDebug() << "Opening URL" << url.toString();
|
||||
auto f = [&]()
|
||||
{
|
||||
return QDesktopServices::openUrl(url);
|
||||
};
|
||||
#if defined(Q_OS_LINUX)
|
||||
return IndirectOpen(f);
|
||||
#else
|
||||
return f();
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
36
launcher/DesktopServices.h
Normal file
36
launcher/DesktopServices.h
Normal file
@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <QUrl>
|
||||
#include <QString>
|
||||
|
||||
/**
|
||||
* This wraps around QDesktopServices and adds workarounds where needed
|
||||
* Use this instead of QDesktopServices!
|
||||
*/
|
||||
namespace DesktopServices
|
||||
{
|
||||
/**
|
||||
* Open a file in whatever application is applicable
|
||||
*/
|
||||
bool openFile(const QString &path);
|
||||
|
||||
/**
|
||||
* Open a file in the specified application
|
||||
*/
|
||||
bool openFile(const QString &application, const QString &path, const QString & workingDirectory = QString(), qint64 *pid = 0);
|
||||
|
||||
/**
|
||||
* Run an application
|
||||
*/
|
||||
bool run(const QString &application,const QStringList &args, const QString & workingDirectory = QString(), qint64 *pid = 0);
|
||||
|
||||
/**
|
||||
* Open a directory
|
||||
*/
|
||||
bool openDirectory(const QString &path, bool ensureExists = false);
|
||||
|
||||
/**
|
||||
* Open the URL, most likely in a browser. Maybe.
|
||||
*/
|
||||
bool openUrl(const QUrl &url);
|
||||
}
|
211
launcher/Env.cpp
Normal file
211
launcher/Env.cpp
Normal file
@ -0,0 +1,211 @@
|
||||
#include "Env.h"
|
||||
#include "net/HttpMetaCache.h"
|
||||
#include "BaseVersion.h"
|
||||
#include "BaseVersionList.h"
|
||||
#include <QDir>
|
||||
#include <QCoreApplication>
|
||||
#include <QNetworkProxy>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QDebug>
|
||||
#include "tasks/Task.h"
|
||||
#include "meta/Index.h"
|
||||
#include "FileSystem.h"
|
||||
#include <QDebug>
|
||||
|
||||
|
||||
struct Env::Private
|
||||
{
|
||||
QNetworkAccessManager m_qnam;
|
||||
shared_qobject_ptr<HttpMetaCache> m_metacache;
|
||||
std::shared_ptr<IIconList> m_iconlist;
|
||||
shared_qobject_ptr<Meta::Index> m_metadataIndex;
|
||||
QString m_jarsPath;
|
||||
QSet<QString> m_features;
|
||||
};
|
||||
|
||||
static Env * instance;
|
||||
|
||||
/*
|
||||
* The *NEW* global rat nest of an object. Handle with care.
|
||||
*/
|
||||
|
||||
Env::Env()
|
||||
{
|
||||
d = new Private();
|
||||
}
|
||||
|
||||
Env::~Env()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
Env& Env::Env::getInstance()
|
||||
{
|
||||
if(!instance)
|
||||
{
|
||||
instance = new Env();
|
||||
}
|
||||
return *instance;
|
||||
}
|
||||
|
||||
void Env::dispose()
|
||||
{
|
||||
delete instance;
|
||||
instance = nullptr;
|
||||
}
|
||||
|
||||
shared_qobject_ptr< HttpMetaCache > Env::metacache()
|
||||
{
|
||||
return d->m_metacache;
|
||||
}
|
||||
|
||||
QNetworkAccessManager& Env::qnam() const
|
||||
{
|
||||
return d->m_qnam;
|
||||
}
|
||||
|
||||
std::shared_ptr<IIconList> Env::icons()
|
||||
{
|
||||
return d->m_iconlist;
|
||||
}
|
||||
|
||||
void Env::registerIconList(std::shared_ptr<IIconList> iconlist)
|
||||
{
|
||||
d->m_iconlist = iconlist;
|
||||
}
|
||||
|
||||
shared_qobject_ptr<Meta::Index> Env::metadataIndex()
|
||||
{
|
||||
if (!d->m_metadataIndex)
|
||||
{
|
||||
d->m_metadataIndex.reset(new Meta::Index());
|
||||
}
|
||||
return d->m_metadataIndex;
|
||||
}
|
||||
|
||||
|
||||
void Env::initHttpMetaCache()
|
||||
{
|
||||
auto &m_metacache = d->m_metacache;
|
||||
m_metacache.reset(new HttpMetaCache("metacache"));
|
||||
m_metacache->addBase("asset_indexes", QDir("assets/indexes").absolutePath());
|
||||
m_metacache->addBase("asset_objects", QDir("assets/objects").absolutePath());
|
||||
m_metacache->addBase("versions", QDir("versions").absolutePath());
|
||||
m_metacache->addBase("libraries", QDir("libraries").absolutePath());
|
||||
m_metacache->addBase("minecraftforge", QDir("mods/minecraftforge").absolutePath());
|
||||
m_metacache->addBase("fmllibs", QDir("mods/minecraftforge/libs").absolutePath());
|
||||
m_metacache->addBase("liteloader", QDir("mods/liteloader").absolutePath());
|
||||
m_metacache->addBase("general", QDir("cache").absolutePath());
|
||||
m_metacache->addBase("ATLauncherPacks", QDir("cache/ATLauncherPacks").absolutePath());
|
||||
m_metacache->addBase("FTBPacks", QDir("cache/FTBPacks").absolutePath());
|
||||
m_metacache->addBase("ModpacksCHPacks", QDir("cache/ModpacksCHPacks").absolutePath());
|
||||
m_metacache->addBase("TechnicPacks", QDir("cache/TechnicPacks").absolutePath());
|
||||
m_metacache->addBase("FlamePacks", QDir("cache/FlamePacks").absolutePath());
|
||||
m_metacache->addBase("skins", QDir("accounts/skins").absolutePath());
|
||||
m_metacache->addBase("root", QDir::currentPath());
|
||||
m_metacache->addBase("translations", QDir("translations").absolutePath());
|
||||
m_metacache->addBase("icons", QDir("cache/icons").absolutePath());
|
||||
m_metacache->addBase("meta", QDir("meta").absolutePath());
|
||||
m_metacache->Load();
|
||||
}
|
||||
|
||||
void Env::updateProxySettings(QString proxyTypeStr, QString addr, int port, QString user, QString password)
|
||||
{
|
||||
// Set the application proxy settings.
|
||||
if (proxyTypeStr == "SOCKS5")
|
||||
{
|
||||
QNetworkProxy::setApplicationProxy(
|
||||
QNetworkProxy(QNetworkProxy::Socks5Proxy, addr, port, user, password));
|
||||
}
|
||||
else if (proxyTypeStr == "HTTP")
|
||||
{
|
||||
QNetworkProxy::setApplicationProxy(
|
||||
QNetworkProxy(QNetworkProxy::HttpProxy, addr, port, user, password));
|
||||
}
|
||||
else if (proxyTypeStr == "None")
|
||||
{
|
||||
// If we have no proxy set, set no proxy and return.
|
||||
QNetworkProxy::setApplicationProxy(QNetworkProxy(QNetworkProxy::NoProxy));
|
||||
}
|
||||
else
|
||||
{
|
||||
// If we have "Default" selected, set Qt to use the system proxy settings.
|
||||
QNetworkProxyFactory::setUseSystemConfiguration(true);
|
||||
}
|
||||
|
||||
qDebug() << "Detecting proxy settings...";
|
||||
QNetworkProxy proxy = QNetworkProxy::applicationProxy();
|
||||
d->m_qnam.setProxy(proxy);
|
||||
QString proxyDesc;
|
||||
if (proxy.type() == QNetworkProxy::NoProxy)
|
||||
{
|
||||
qDebug() << "Using no proxy is an option!";
|
||||
return;
|
||||
}
|
||||
switch (proxy.type())
|
||||
{
|
||||
case QNetworkProxy::DefaultProxy:
|
||||
proxyDesc = "Default proxy: ";
|
||||
break;
|
||||
case QNetworkProxy::Socks5Proxy:
|
||||
proxyDesc = "Socks5 proxy: ";
|
||||
break;
|
||||
case QNetworkProxy::HttpProxy:
|
||||
proxyDesc = "HTTP proxy: ";
|
||||
break;
|
||||
case QNetworkProxy::HttpCachingProxy:
|
||||
proxyDesc = "HTTP caching: ";
|
||||
break;
|
||||
case QNetworkProxy::FtpCachingProxy:
|
||||
proxyDesc = "FTP caching: ";
|
||||
break;
|
||||
default:
|
||||
proxyDesc = "DERP proxy: ";
|
||||
break;
|
||||
}
|
||||
proxyDesc += QString("%1:%2")
|
||||
.arg(proxy.hostName())
|
||||
.arg(proxy.port());
|
||||
qDebug() << proxyDesc;
|
||||
}
|
||||
|
||||
QString Env::getJarsPath()
|
||||
{
|
||||
if(d->m_jarsPath.isEmpty())
|
||||
{
|
||||
return FS::PathCombine(QCoreApplication::applicationDirPath(), "jars");
|
||||
}
|
||||
return d->m_jarsPath;
|
||||
}
|
||||
|
||||
void Env::setJarsPath(const QString& path)
|
||||
{
|
||||
d->m_jarsPath = path;
|
||||
}
|
||||
|
||||
void Env::enableFeature(const QString& featureName, bool state)
|
||||
{
|
||||
if(state)
|
||||
{
|
||||
d->m_features.insert(featureName);
|
||||
}
|
||||
else
|
||||
{
|
||||
d->m_features.remove(featureName);
|
||||
}
|
||||
}
|
||||
|
||||
bool Env::isFeatureEnabled(const QString& featureName) const
|
||||
{
|
||||
return d->m_features.contains(featureName);
|
||||
}
|
||||
|
||||
void Env::getEnabledFeatures(QSet<QString>& features) const
|
||||
{
|
||||
features = d->m_features;
|
||||
}
|
||||
|
||||
void Env::setEnabledFeatures(const QSet<QString>& features) const
|
||||
{
|
||||
d->m_features = features;
|
||||
}
|
63
launcher/Env.h
Normal file
63
launcher/Env.h
Normal file
@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "icons/IIconList.h"
|
||||
#include <QString>
|
||||
#include <QMap>
|
||||
|
||||
#include "QObjectPtr.h"
|
||||
|
||||
class QNetworkAccessManager;
|
||||
class HttpMetaCache;
|
||||
class BaseVersionList;
|
||||
class BaseVersion;
|
||||
|
||||
namespace Meta
|
||||
{
|
||||
class Index;
|
||||
}
|
||||
|
||||
#if defined(ENV)
|
||||
#undef ENV
|
||||
#endif
|
||||
#define ENV (Env::getInstance())
|
||||
|
||||
|
||||
class Env
|
||||
{
|
||||
friend class MultiMC;
|
||||
private:
|
||||
struct Private;
|
||||
Env();
|
||||
~Env();
|
||||
static void dispose();
|
||||
public:
|
||||
static Env& getInstance();
|
||||
|
||||
QNetworkAccessManager &qnam() const;
|
||||
|
||||
shared_qobject_ptr<HttpMetaCache> metacache();
|
||||
|
||||
std::shared_ptr<IIconList> icons();
|
||||
|
||||
/// init the cache. FIXME: possible future hook point
|
||||
void initHttpMetaCache();
|
||||
|
||||
/// Updates the application proxy settings from the settings object.
|
||||
void updateProxySettings(QString proxyTypeStr, QString addr, int port, QString user, QString password);
|
||||
|
||||
void registerIconList(std::shared_ptr<IIconList> iconlist);
|
||||
|
||||
shared_qobject_ptr<Meta::Index> metadataIndex();
|
||||
|
||||
QString getJarsPath();
|
||||
void setJarsPath(const QString & path);
|
||||
|
||||
bool isFeatureEnabled(const QString & featureName) const;
|
||||
void enableFeature(const QString & featureName, bool state = true);
|
||||
void getEnabledFeatures(QSet<QString> & features) const;
|
||||
void setEnabledFeatures(const QSet<QString> & features) const;
|
||||
|
||||
protected:
|
||||
Private * d;
|
||||
};
|
32
launcher/Exception.h
Normal file
32
launcher/Exception.h
Normal file
@ -0,0 +1,32 @@
|
||||
// Licensed under the Apache-2.0 license. See README.md for details.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <QDebug>
|
||||
#include <exception>
|
||||
|
||||
class Exception : public std::exception
|
||||
{
|
||||
public:
|
||||
Exception(const QString &message) : std::exception(), m_message(message)
|
||||
{
|
||||
qCritical() << "Exception:" << message;
|
||||
}
|
||||
Exception(const Exception &other)
|
||||
: std::exception(), m_message(other.cause())
|
||||
{
|
||||
}
|
||||
virtual ~Exception() noexcept {}
|
||||
const char *what() const noexcept
|
||||
{
|
||||
return m_message.toLatin1().constData();
|
||||
}
|
||||
QString cause() const
|
||||
{
|
||||
return m_message;
|
||||
}
|
||||
|
||||
private:
|
||||
QString m_message;
|
||||
};
|
43
launcher/ExponentialSeries.h
Normal file
43
launcher/ExponentialSeries.h
Normal file
@ -0,0 +1,43 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
template <typename T>
|
||||
inline void clamp(T& current, T min, T max)
|
||||
{
|
||||
if (current < min)
|
||||
{
|
||||
current = min;
|
||||
}
|
||||
else if(current > max)
|
||||
{
|
||||
current = max;
|
||||
}
|
||||
}
|
||||
|
||||
// List of numbers from min to max. Next is exponent times bigger than previous.
|
||||
|
||||
class ExponentialSeries
|
||||
{
|
||||
public:
|
||||
ExponentialSeries(unsigned min, unsigned max, unsigned exponent = 2)
|
||||
{
|
||||
m_current = m_min = min;
|
||||
m_max = max;
|
||||
m_exponent = exponent;
|
||||
}
|
||||
void reset()
|
||||
{
|
||||
m_current = m_min;
|
||||
}
|
||||
unsigned operator()()
|
||||
{
|
||||
unsigned retval = m_current;
|
||||
m_current *= m_exponent;
|
||||
clamp(m_current, m_min, m_max);
|
||||
return retval;
|
||||
}
|
||||
unsigned m_current;
|
||||
unsigned m_min;
|
||||
unsigned m_max;
|
||||
unsigned m_exponent;
|
||||
};
|
457
launcher/FileSystem.cpp
Normal file
457
launcher/FileSystem.cpp
Normal file
@ -0,0 +1,457 @@
|
||||
// Licensed under the Apache-2.0 license. See README.md for details.
|
||||
|
||||
#include "FileSystem.h"
|
||||
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QSaveFile>
|
||||
#include <QFileInfo>
|
||||
#include <QDebug>
|
||||
#include <QUrl>
|
||||
#include <QStandardPaths>
|
||||
#include <QTextStream>
|
||||
|
||||
#if defined Q_OS_WIN32
|
||||
#include <windows.h>
|
||||
#include <string>
|
||||
#include <sys/utime.h>
|
||||
#include <winnls.h>
|
||||
#include <shobjidl.h>
|
||||
#include <objbase.h>
|
||||
#include <objidl.h>
|
||||
#include <shlguid.h>
|
||||
#include <shlobj.h>
|
||||
#else
|
||||
#include <utime.h>
|
||||
#endif
|
||||
|
||||
namespace FS {
|
||||
|
||||
void ensureExists(const QDir &dir)
|
||||
{
|
||||
if (!QDir().mkpath(dir.absolutePath()))
|
||||
{
|
||||
throw FileSystemException("Unable to create folder " + dir.dirName() + " (" +
|
||||
dir.absolutePath() + ")");
|
||||
}
|
||||
}
|
||||
|
||||
void write(const QString &filename, const QByteArray &data)
|
||||
{
|
||||
ensureExists(QFileInfo(filename).dir());
|
||||
QSaveFile file(filename);
|
||||
if (!file.open(QSaveFile::WriteOnly))
|
||||
{
|
||||
throw FileSystemException("Couldn't open " + filename + " for writing: " +
|
||||
file.errorString());
|
||||
}
|
||||
if (data.size() != file.write(data))
|
||||
{
|
||||
throw FileSystemException("Error writing data to " + filename + ": " +
|
||||
file.errorString());
|
||||
}
|
||||
if (!file.commit())
|
||||
{
|
||||
throw FileSystemException("Error while committing data to " + filename + ": " +
|
||||
file.errorString());
|
||||
}
|
||||
}
|
||||
|
||||
QByteArray read(const QString &filename)
|
||||
{
|
||||
QFile file(filename);
|
||||
if (!file.open(QFile::ReadOnly))
|
||||
{
|
||||
throw FileSystemException("Unable to open " + filename + " for reading: " +
|
||||
file.errorString());
|
||||
}
|
||||
const qint64 size = file.size();
|
||||
QByteArray data(int(size), 0);
|
||||
const qint64 ret = file.read(data.data(), size);
|
||||
if (ret == -1 || ret != size)
|
||||
{
|
||||
throw FileSystemException("Error reading data from " + filename + ": " +
|
||||
file.errorString());
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
bool updateTimestamp(const QString& filename)
|
||||
{
|
||||
#ifdef Q_OS_WIN32
|
||||
std::wstring filename_utf_16 = filename.toStdWString();
|
||||
return (_wutime64(filename_utf_16.c_str(), nullptr) == 0);
|
||||
#else
|
||||
QByteArray filenameBA = QFile::encodeName(filename);
|
||||
return (utime(filenameBA.data(), nullptr) == 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool ensureFilePathExists(QString filenamepath)
|
||||
{
|
||||
QFileInfo a(filenamepath);
|
||||
QDir dir;
|
||||
QString ensuredPath = a.path();
|
||||
bool success = dir.mkpath(ensuredPath);
|
||||
return success;
|
||||
}
|
||||
|
||||
bool ensureFolderPathExists(QString foldernamepath)
|
||||
{
|
||||
QFileInfo a(foldernamepath);
|
||||
QDir dir;
|
||||
QString ensuredPath = a.filePath();
|
||||
bool success = dir.mkpath(ensuredPath);
|
||||
return success;
|
||||
}
|
||||
|
||||
bool copy::operator()(const QString &offset)
|
||||
{
|
||||
//NOTE always deep copy on windows. the alternatives are too messy.
|
||||
#if defined Q_OS_WIN32
|
||||
m_followSymlinks = true;
|
||||
#endif
|
||||
|
||||
auto src = PathCombine(m_src.absolutePath(), offset);
|
||||
auto dst = PathCombine(m_dst.absolutePath(), offset);
|
||||
|
||||
QFileInfo currentSrc(src);
|
||||
if (!currentSrc.exists())
|
||||
return false;
|
||||
|
||||
if(!m_followSymlinks && currentSrc.isSymLink())
|
||||
{
|
||||
qDebug() << "creating symlink" << src << " - " << dst;
|
||||
if (!ensureFilePathExists(dst))
|
||||
{
|
||||
qWarning() << "Cannot create path!";
|
||||
return false;
|
||||
}
|
||||
return QFile::link(currentSrc.symLinkTarget(), dst);
|
||||
}
|
||||
else if(currentSrc.isFile())
|
||||
{
|
||||
qDebug() << "copying file" << src << " - " << dst;
|
||||
if (!ensureFilePathExists(dst))
|
||||
{
|
||||
qWarning() << "Cannot create path!";
|
||||
return false;
|
||||
}
|
||||
return QFile::copy(src, dst);
|
||||
}
|
||||
else if(currentSrc.isDir())
|
||||
{
|
||||
qDebug() << "recursing" << offset;
|
||||
if (!ensureFolderPathExists(dst))
|
||||
{
|
||||
qWarning() << "Cannot create path!";
|
||||
return false;
|
||||
}
|
||||
QDir currentDir(src);
|
||||
for(auto & f : currentDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System))
|
||||
{
|
||||
auto inner_offset = PathCombine(offset, f);
|
||||
// ignore and skip stuff that matches the blacklist.
|
||||
if(m_blacklist && m_blacklist->matches(inner_offset))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if(!operator()(inner_offset))
|
||||
{
|
||||
qWarning() << "Failed to copy" << inner_offset;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
qCritical() << "Copy ERROR: Unknown filesystem object:" << src;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool deletePath(QString path)
|
||||
{
|
||||
bool OK = true;
|
||||
QFileInfo finfo(path);
|
||||
if(finfo.isFile()) {
|
||||
return QFile::remove(path);
|
||||
}
|
||||
|
||||
QDir dir(path);
|
||||
|
||||
if (!dir.exists())
|
||||
{
|
||||
return OK;
|
||||
}
|
||||
auto allEntries = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden |
|
||||
QDir::AllDirs | QDir::Files,
|
||||
QDir::DirsFirst);
|
||||
|
||||
for(auto & info: allEntries)
|
||||
{
|
||||
#if defined Q_OS_WIN32
|
||||
QString nativePath = QDir::toNativeSeparators(info.absoluteFilePath());
|
||||
auto wString = nativePath.toStdWString();
|
||||
DWORD dwAttrs = GetFileAttributesW(wString.c_str());
|
||||
// Windows: check for junctions, reparse points and other nasty things of that sort
|
||||
if(dwAttrs & FILE_ATTRIBUTE_REPARSE_POINT)
|
||||
{
|
||||
if (info.isFile())
|
||||
{
|
||||
OK &= QFile::remove(info.absoluteFilePath());
|
||||
}
|
||||
else if (info.isDir())
|
||||
{
|
||||
OK &= dir.rmdir(info.absoluteFilePath());
|
||||
}
|
||||
}
|
||||
#else
|
||||
// We do not trust Qt with reparse points, but do trust it with unix symlinks.
|
||||
if(info.isSymLink())
|
||||
{
|
||||
OK &= QFile::remove(info.absoluteFilePath());
|
||||
}
|
||||
#endif
|
||||
else if (info.isDir())
|
||||
{
|
||||
OK &= deletePath(info.absoluteFilePath());
|
||||
}
|
||||
else if (info.isFile())
|
||||
{
|
||||
OK &= QFile::remove(info.absoluteFilePath());
|
||||
}
|
||||
else
|
||||
{
|
||||
OK = false;
|
||||
qCritical() << "Delete ERROR: Unknown filesystem object:" << info.absoluteFilePath();
|
||||
}
|
||||
}
|
||||
OK &= dir.rmdir(dir.absolutePath());
|
||||
return OK;
|
||||
}
|
||||
|
||||
|
||||
QString PathCombine(const QString & path1, const QString & path2)
|
||||
{
|
||||
if(!path1.size())
|
||||
return path2;
|
||||
if(!path2.size())
|
||||
return path1;
|
||||
return QDir::cleanPath(path1 + QDir::separator() + path2);
|
||||
}
|
||||
|
||||
QString PathCombine(const QString & path1, const QString & path2, const QString & path3)
|
||||
{
|
||||
return PathCombine(PathCombine(path1, path2), path3);
|
||||
}
|
||||
|
||||
QString PathCombine(const QString & path1, const QString & path2, const QString & path3, const QString & path4)
|
||||
{
|
||||
return PathCombine(PathCombine(path1, path2, path3), path4);
|
||||
}
|
||||
|
||||
QString AbsolutePath(QString path)
|
||||
{
|
||||
return QFileInfo(path).absolutePath();
|
||||
}
|
||||
|
||||
QString ResolveExecutable(QString path)
|
||||
{
|
||||
if (path.isEmpty())
|
||||
{
|
||||
return QString();
|
||||
}
|
||||
if(!path.contains('/'))
|
||||
{
|
||||
path = QStandardPaths::findExecutable(path);
|
||||
}
|
||||
QFileInfo pathInfo(path);
|
||||
if(!pathInfo.exists() || !pathInfo.isExecutable())
|
||||
{
|
||||
return QString();
|
||||
}
|
||||
return pathInfo.absoluteFilePath();
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize path
|
||||
*
|
||||
* Any paths inside the current folder will be normalized to relative paths (to current)
|
||||
* Other paths will be made absolute
|
||||
*/
|
||||
QString NormalizePath(QString path)
|
||||
{
|
||||
QDir a = QDir::currentPath();
|
||||
QString currentAbsolute = a.absolutePath();
|
||||
|
||||
QDir b(path);
|
||||
QString newAbsolute = b.absolutePath();
|
||||
|
||||
if (newAbsolute.startsWith(currentAbsolute))
|
||||
{
|
||||
return a.relativeFilePath(newAbsolute);
|
||||
}
|
||||
else
|
||||
{
|
||||
return newAbsolute;
|
||||
}
|
||||
}
|
||||
|
||||
QString badFilenameChars = "\"\\/?<>:;*|!+\r\n";
|
||||
|
||||
QString RemoveInvalidFilenameChars(QString string, QChar replaceWith)
|
||||
{
|
||||
for (int i = 0; i < string.length(); i++)
|
||||
{
|
||||
if (badFilenameChars.contains(string[i]))
|
||||
{
|
||||
string[i] = replaceWith;
|
||||
}
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
QString DirNameFromString(QString string, QString inDir)
|
||||
{
|
||||
int num = 0;
|
||||
QString baseName = RemoveInvalidFilenameChars(string, '-');
|
||||
QString dirName;
|
||||
do
|
||||
{
|
||||
if(num == 0)
|
||||
{
|
||||
dirName = baseName;
|
||||
}
|
||||
else
|
||||
{
|
||||
dirName = baseName + QString::number(num);;
|
||||
}
|
||||
|
||||
// If it's over 9000
|
||||
if (num > 9000)
|
||||
return "";
|
||||
num++;
|
||||
} while (QFileInfo(PathCombine(inDir, dirName)).exists());
|
||||
return dirName;
|
||||
}
|
||||
|
||||
// Does the folder path contain any '!'? If yes, return true, otherwise false.
|
||||
// (This is a problem for Java)
|
||||
bool checkProblemticPathJava(QDir folder)
|
||||
{
|
||||
QString pathfoldername = folder.absolutePath();
|
||||
return pathfoldername.contains("!", Qt::CaseInsensitive);
|
||||
}
|
||||
|
||||
// Win32 crap
|
||||
#if defined Q_OS_WIN
|
||||
|
||||
bool called_coinit = false;
|
||||
|
||||
HRESULT CreateLink(LPCSTR linkPath, LPCSTR targetPath, LPCSTR args)
|
||||
{
|
||||
HRESULT hres;
|
||||
|
||||
if (!called_coinit)
|
||||
{
|
||||
hres = CoInitialize(NULL);
|
||||
called_coinit = true;
|
||||
|
||||
if (!SUCCEEDED(hres))
|
||||
{
|
||||
qWarning("Failed to initialize COM. Error 0x%08lX", hres);
|
||||
return hres;
|
||||
}
|
||||
}
|
||||
|
||||
IShellLink *link;
|
||||
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink,
|
||||
(LPVOID *)&link);
|
||||
|
||||
if (SUCCEEDED(hres))
|
||||
{
|
||||
IPersistFile *persistFile;
|
||||
|
||||
link->SetPath(targetPath);
|
||||
link->SetArguments(args);
|
||||
|
||||
hres = link->QueryInterface(IID_IPersistFile, (LPVOID *)&persistFile);
|
||||
if (SUCCEEDED(hres))
|
||||
{
|
||||
WCHAR wstr[MAX_PATH];
|
||||
|
||||
MultiByteToWideChar(CP_ACP, 0, linkPath, -1, wstr, MAX_PATH);
|
||||
|
||||
hres = persistFile->Save(wstr, TRUE);
|
||||
persistFile->Release();
|
||||
}
|
||||
link->Release();
|
||||
}
|
||||
return hres;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
QString getDesktopDir()
|
||||
{
|
||||
return QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
|
||||
}
|
||||
|
||||
// Cross-platform Shortcut creation
|
||||
bool createShortCut(QString location, QString dest, QStringList args, QString name,
|
||||
QString icon)
|
||||
{
|
||||
#if defined Q_OS_LINUX
|
||||
location = PathCombine(location, name + ".desktop");
|
||||
|
||||
QFile f(location);
|
||||
f.open(QIODevice::WriteOnly | QIODevice::Text);
|
||||
QTextStream stream(&f);
|
||||
|
||||
QString argstring;
|
||||
if (!args.empty())
|
||||
argstring = " '" + args.join("' '") + "'";
|
||||
|
||||
stream << "[Desktop Entry]"
|
||||
<< "\n";
|
||||
stream << "Type=Application"
|
||||
<< "\n";
|
||||
stream << "TryExec=" << dest.toLocal8Bit() << "\n";
|
||||
stream << "Exec=" << dest.toLocal8Bit() << argstring.toLocal8Bit() << "\n";
|
||||
stream << "Name=" << name.toLocal8Bit() << "\n";
|
||||
stream << "Icon=" << icon.toLocal8Bit() << "\n";
|
||||
|
||||
stream.flush();
|
||||
f.close();
|
||||
|
||||
f.setPermissions(f.permissions() | QFileDevice::ExeOwner | QFileDevice::ExeGroup |
|
||||
QFileDevice::ExeOther);
|
||||
|
||||
return true;
|
||||
#elif defined Q_OS_WIN
|
||||
// TODO: Fix
|
||||
// QFile file(PathCombine(location, name + ".lnk"));
|
||||
// WCHAR *file_w;
|
||||
// WCHAR *dest_w;
|
||||
// WCHAR *args_w;
|
||||
// file.fileName().toWCharArray(file_w);
|
||||
// dest.toWCharArray(dest_w);
|
||||
|
||||
// QString argStr;
|
||||
// for (int i = 0; i < args.count(); i++)
|
||||
// {
|
||||
// argStr.append(args[i]);
|
||||
// argStr.append(" ");
|
||||
// }
|
||||
// argStr.toWCharArray(args_w);
|
||||
|
||||
// return SUCCEEDED(CreateLink(file_w, dest_w, args_w));
|
||||
return false;
|
||||
#else
|
||||
qWarning("Desktop Shortcuts not supported on your platform!");
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
}
|
127
launcher/FileSystem.h
Normal file
127
launcher/FileSystem.h
Normal file
@ -0,0 +1,127 @@
|
||||
// Licensed under the Apache-2.0 license. See README.md for details.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Exception.h"
|
||||
#include "pathmatcher/IPathMatcher.h"
|
||||
|
||||
#include <QDir>
|
||||
#include <QFlags>
|
||||
|
||||
namespace FS
|
||||
{
|
||||
|
||||
class FileSystemException : public ::Exception
|
||||
{
|
||||
public:
|
||||
FileSystemException(const QString &message) : Exception(message) {}
|
||||
};
|
||||
|
||||
/**
|
||||
* write data to a file safely
|
||||
*/
|
||||
void write(const QString &filename, const QByteArray &data);
|
||||
|
||||
/**
|
||||
* read data from a file safely\
|
||||
*/
|
||||
QByteArray read(const QString &filename);
|
||||
|
||||
/**
|
||||
* Update the last changed timestamp of an existing file
|
||||
*/
|
||||
bool updateTimestamp(const QString & filename);
|
||||
|
||||
/**
|
||||
* Creates all the folders in a path for the specified path
|
||||
* last segment of the path is treated as a file name and is ignored!
|
||||
*/
|
||||
bool ensureFilePathExists(QString filenamepath);
|
||||
|
||||
/**
|
||||
* Creates all the folders in a path for the specified path
|
||||
* last segment of the path is treated as a folder name and is created!
|
||||
*/
|
||||
bool ensureFolderPathExists(QString filenamepath);
|
||||
|
||||
class copy
|
||||
{
|
||||
public:
|
||||
copy(const QString & src, const QString & dst)
|
||||
{
|
||||
m_src = src;
|
||||
m_dst = dst;
|
||||
}
|
||||
copy & followSymlinks(const bool follow)
|
||||
{
|
||||
m_followSymlinks = follow;
|
||||
return *this;
|
||||
}
|
||||
copy & blacklist(const IPathMatcher * filter)
|
||||
{
|
||||
m_blacklist = filter;
|
||||
return *this;
|
||||
}
|
||||
bool operator()()
|
||||
{
|
||||
return operator()(QString());
|
||||
}
|
||||
|
||||
private:
|
||||
bool operator()(const QString &offset);
|
||||
|
||||
private:
|
||||
bool m_followSymlinks = true;
|
||||
const IPathMatcher * m_blacklist = nullptr;
|
||||
QDir m_src;
|
||||
QDir m_dst;
|
||||
};
|
||||
|
||||
/**
|
||||
* Delete a folder recursively
|
||||
*/
|
||||
bool deletePath(QString path);
|
||||
|
||||
QString PathCombine(const QString &path1, const QString &path2);
|
||||
QString PathCombine(const QString &path1, const QString &path2, const QString &path3);
|
||||
QString PathCombine(const QString &path1, const QString &path2, const QString &path3, const QString &path4);
|
||||
|
||||
QString AbsolutePath(QString path);
|
||||
|
||||
/**
|
||||
* Resolve an executable
|
||||
*
|
||||
* Will resolve:
|
||||
* single executable (by name)
|
||||
* relative path
|
||||
* absolute path
|
||||
*
|
||||
* @return absolute path to executable or null string
|
||||
*/
|
||||
QString ResolveExecutable(QString path);
|
||||
|
||||
/**
|
||||
* Normalize path
|
||||
*
|
||||
* Any paths inside the current directory will be normalized to relative paths (to current)
|
||||
* Other paths will be made absolute
|
||||
*
|
||||
* Returns false if the path logic somehow filed (and normalizedPath in invalid)
|
||||
*/
|
||||
QString NormalizePath(QString path);
|
||||
|
||||
QString RemoveInvalidFilenameChars(QString string, QChar replaceWith = '-');
|
||||
|
||||
QString DirNameFromString(QString string, QString inDir = ".");
|
||||
|
||||
/// Checks if the a given Path contains "!"
|
||||
bool checkProblemticPathJava(QDir folder);
|
||||
|
||||
// Get the Directory representing the User's Desktop
|
||||
QString getDesktopDir();
|
||||
|
||||
// Create a shortcut at *location*, pointing to *dest* called with the arguments *args*
|
||||
// call it *name* and assign it the icon *icon*
|
||||
// return true if operation succeeded
|
||||
bool createShortCut(QString location, QString dest, QStringList args, QString name, QString iconLocation);
|
||||
}
|
164
launcher/FileSystem_test.cpp
Normal file
164
launcher/FileSystem_test.cpp
Normal file
@ -0,0 +1,164 @@
|
||||
#include <QTest>
|
||||
#include <QTemporaryDir>
|
||||
#include <QStandardPaths>
|
||||
#include "TestUtil.h"
|
||||
|
||||
#include "FileSystem.h"
|
||||
|
||||
class FileSystemTest : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
const QString bothSlash = "/foo/";
|
||||
const QString trailingSlash = "foo/";
|
||||
const QString leadingSlash = "/foo";
|
||||
|
||||
private
|
||||
slots:
|
||||
void test_pathCombine()
|
||||
{
|
||||
QCOMPARE(QString("/foo/foo"), FS::PathCombine(bothSlash, bothSlash));
|
||||
QCOMPARE(QString("foo/foo"), FS::PathCombine(trailingSlash, trailingSlash));
|
||||
QCOMPARE(QString("/foo/foo"), FS::PathCombine(leadingSlash, leadingSlash));
|
||||
|
||||
QCOMPARE(QString("/foo/foo/foo"), FS::PathCombine(bothSlash, bothSlash, bothSlash));
|
||||
QCOMPARE(QString("foo/foo/foo"), FS::PathCombine(trailingSlash, trailingSlash, trailingSlash));
|
||||
QCOMPARE(QString("/foo/foo/foo"), FS::PathCombine(leadingSlash, leadingSlash, leadingSlash));
|
||||
}
|
||||
|
||||
void test_PathCombine1_data()
|
||||
{
|
||||
QTest::addColumn<QString>("result");
|
||||
QTest::addColumn<QString>("path1");
|
||||
QTest::addColumn<QString>("path2");
|
||||
|
||||
QTest::newRow("qt 1") << "/abc/def/ghi/jkl" << "/abc/def" << "ghi/jkl";
|
||||
QTest::newRow("qt 2") << "/abc/def/ghi/jkl" << "/abc/def/" << "ghi/jkl";
|
||||
#if defined(Q_OS_WIN)
|
||||
QTest::newRow("win native, from C:") << "C:/abc" << "C:" << "abc";
|
||||
QTest::newRow("win native 1") << "C:/abc/def/ghi/jkl" << "C:\\abc\\def" << "ghi\\jkl";
|
||||
QTest::newRow("win native 2") << "C:/abc/def/ghi/jkl" << "C:\\abc\\def\\" << "ghi\\jkl";
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_PathCombine1()
|
||||
{
|
||||
QFETCH(QString, result);
|
||||
QFETCH(QString, path1);
|
||||
QFETCH(QString, path2);
|
||||
|
||||
QCOMPARE(FS::PathCombine(path1, path2), result);
|
||||
}
|
||||
|
||||
void test_PathCombine2_data()
|
||||
{
|
||||
QTest::addColumn<QString>("result");
|
||||
QTest::addColumn<QString>("path1");
|
||||
QTest::addColumn<QString>("path2");
|
||||
QTest::addColumn<QString>("path3");
|
||||
|
||||
QTest::newRow("qt 1") << "/abc/def/ghi/jkl" << "/abc" << "def" << "ghi/jkl";
|
||||
QTest::newRow("qt 2") << "/abc/def/ghi/jkl" << "/abc/" << "def" << "ghi/jkl";
|
||||
QTest::newRow("qt 3") << "/abc/def/ghi/jkl" << "/abc" << "def/" << "ghi/jkl";
|
||||
QTest::newRow("qt 4") << "/abc/def/ghi/jkl" << "/abc/" << "def/" << "ghi/jkl";
|
||||
#if defined(Q_OS_WIN)
|
||||
QTest::newRow("win 1") << "C:/abc/def/ghi/jkl" << "C:\\abc" << "def" << "ghi\\jkl";
|
||||
QTest::newRow("win 2") << "C:/abc/def/ghi/jkl" << "C:\\abc\\" << "def" << "ghi\\jkl";
|
||||
QTest::newRow("win 3") << "C:/abc/def/ghi/jkl" << "C:\\abc" << "def\\" << "ghi\\jkl";
|
||||
QTest::newRow("win 4") << "C:/abc/def/ghi/jkl" << "C:\\abc\\" << "def" << "ghi\\jkl";
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_PathCombine2()
|
||||
{
|
||||
QFETCH(QString, result);
|
||||
QFETCH(QString, path1);
|
||||
QFETCH(QString, path2);
|
||||
QFETCH(QString, path3);
|
||||
|
||||
QCOMPARE(FS::PathCombine(path1, path2, path3), result);
|
||||
}
|
||||
|
||||
void test_copy()
|
||||
{
|
||||
QString folder = QFINDTESTDATA("data/test_folder");
|
||||
auto f = [&folder]()
|
||||
{
|
||||
QTemporaryDir tempDir;
|
||||
tempDir.setAutoRemove(true);
|
||||
qDebug() << "From:" << folder << "To:" << tempDir.path();
|
||||
|
||||
QDir target_dir(FS::PathCombine(tempDir.path(), "test_folder"));
|
||||
qDebug() << tempDir.path();
|
||||
qDebug() << target_dir.path();
|
||||
FS::copy c(folder, target_dir.path());
|
||||
c();
|
||||
|
||||
for(auto entry: target_dir.entryList())
|
||||
{
|
||||
qDebug() << entry;
|
||||
}
|
||||
QVERIFY(target_dir.entryList().contains("pack.mcmeta"));
|
||||
QVERIFY(target_dir.entryList().contains("assets"));
|
||||
};
|
||||
|
||||
// first try variant without trailing /
|
||||
QVERIFY(!folder.endsWith('/'));
|
||||
f();
|
||||
|
||||
// then variant with trailing /
|
||||
folder.append('/');
|
||||
QVERIFY(folder.endsWith('/'));
|
||||
f();
|
||||
}
|
||||
|
||||
void test_getDesktop()
|
||||
{
|
||||
QCOMPARE(FS::getDesktopDir(), QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
|
||||
}
|
||||
|
||||
// this is only valid on linux
|
||||
// FIXME: implement on windows, OSX, then test.
|
||||
#if defined(Q_OS_LINUX)
|
||||
void test_createShortcut_data()
|
||||
{
|
||||
QTest::addColumn<QString>("location");
|
||||
QTest::addColumn<QString>("dest");
|
||||
QTest::addColumn<QStringList>("args");
|
||||
QTest::addColumn<QString>("name");
|
||||
QTest::addColumn<QString>("iconLocation");
|
||||
QTest::addColumn<QByteArray>("result");
|
||||
|
||||
QTest::newRow("unix") << QDir::currentPath()
|
||||
<< "asdfDest"
|
||||
<< (QStringList() << "arg1" << "arg2")
|
||||
<< "asdf"
|
||||
<< QString()
|
||||
#if defined(Q_OS_LINUX)
|
||||
<< MULTIMC_GET_TEST_FILE("data/FileSystem-test_createShortcut-unix")
|
||||
#elif defined(Q_OS_WIN)
|
||||
<< QByteArray()
|
||||
#endif
|
||||
;
|
||||
}
|
||||
|
||||
void test_createShortcut()
|
||||
{
|
||||
QFETCH(QString, location);
|
||||
QFETCH(QString, dest);
|
||||
QFETCH(QStringList, args);
|
||||
QFETCH(QString, name);
|
||||
QFETCH(QString, iconLocation);
|
||||
QFETCH(QByteArray, result);
|
||||
|
||||
QVERIFY(FS::createShortCut(location, dest, args, name, iconLocation));
|
||||
QCOMPARE(QString::fromLocal8Bit(TestsInternal::readFile(location + QDir::separator() + name + ".desktop")), QString::fromLocal8Bit(result));
|
||||
|
||||
//QDir().remove(location);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
QTEST_GUILESS_MAIN(FileSystemTest)
|
||||
|
||||
#include "FileSystem_test.moc"
|
31
launcher/Filter.cpp
Normal file
31
launcher/Filter.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
#include "Filter.h"
|
||||
|
||||
Filter::~Filter(){}
|
||||
|
||||
ContainsFilter::ContainsFilter(const QString& pattern) : pattern(pattern){}
|
||||
ContainsFilter::~ContainsFilter(){}
|
||||
bool ContainsFilter::accepts(const QString& value)
|
||||
{
|
||||
return value.contains(pattern);
|
||||
}
|
||||
|
||||
ExactFilter::ExactFilter(const QString& pattern) : pattern(pattern){}
|
||||
ExactFilter::~ExactFilter(){}
|
||||
bool ExactFilter::accepts(const QString& value)
|
||||
{
|
||||
return value == pattern;
|
||||
}
|
||||
|
||||
RegexpFilter::RegexpFilter(const QString& regexp, bool invert)
|
||||
:invert(invert)
|
||||
{
|
||||
pattern.setPattern(regexp);
|
||||
pattern.optimize();
|
||||
}
|
||||
RegexpFilter::~RegexpFilter(){}
|
||||
bool RegexpFilter::accepts(const QString& value)
|
||||
{
|
||||
auto match = pattern.match(value);
|
||||
bool matched = match.hasMatch();
|
||||
return invert ? (!matched) : (matched);
|
||||
}
|
42
launcher/Filter.h
Normal file
42
launcher/Filter.h
Normal file
@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <QRegularExpression>
|
||||
|
||||
class Filter
|
||||
{
|
||||
public:
|
||||
virtual ~Filter();
|
||||
virtual bool accepts(const QString & value) = 0;
|
||||
};
|
||||
|
||||
class ContainsFilter: public Filter
|
||||
{
|
||||
public:
|
||||
ContainsFilter(const QString &pattern);
|
||||
virtual ~ContainsFilter();
|
||||
bool accepts(const QString & value) override;
|
||||
private:
|
||||
QString pattern;
|
||||
};
|
||||
|
||||
class ExactFilter: public Filter
|
||||
{
|
||||
public:
|
||||
ExactFilter(const QString &pattern);
|
||||
virtual ~ExactFilter();
|
||||
bool accepts(const QString & value) override;
|
||||
private:
|
||||
QString pattern;
|
||||
};
|
||||
|
||||
class RegexpFilter: public Filter
|
||||
{
|
||||
public:
|
||||
RegexpFilter(const QString ®exp, bool invert);
|
||||
virtual ~RegexpFilter();
|
||||
bool accepts(const QString & value) override;
|
||||
private:
|
||||
QRegularExpression pattern;
|
||||
bool invert = false;
|
||||
};
|
115
launcher/GZip.cpp
Normal file
115
launcher/GZip.cpp
Normal file
@ -0,0 +1,115 @@
|
||||
#include "GZip.h"
|
||||
#include <zlib.h>
|
||||
#include <QByteArray>
|
||||
|
||||
bool GZip::unzip(const QByteArray &compressedBytes, QByteArray &uncompressedBytes)
|
||||
{
|
||||
if (compressedBytes.size() == 0)
|
||||
{
|
||||
uncompressedBytes = compressedBytes;
|
||||
return true;
|
||||
}
|
||||
|
||||
unsigned uncompLength = compressedBytes.size();
|
||||
uncompressedBytes.clear();
|
||||
uncompressedBytes.resize(uncompLength);
|
||||
|
||||
z_stream strm;
|
||||
memset(&strm, 0, sizeof(strm));
|
||||
strm.next_in = (Bytef *)compressedBytes.data();
|
||||
strm.avail_in = compressedBytes.size();
|
||||
|
||||
bool done = false;
|
||||
|
||||
if (inflateInit2(&strm, (16 + MAX_WBITS)) != Z_OK)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int err = Z_OK;
|
||||
|
||||
while (!done)
|
||||
{
|
||||
// If our output buffer is too small
|
||||
if (strm.total_out >= uncompLength)
|
||||
{
|
||||
uncompressedBytes.resize(uncompLength * 2);
|
||||
uncompLength *= 2;
|
||||
}
|
||||
|
||||
strm.next_out = (Bytef *)(uncompressedBytes.data() + strm.total_out);
|
||||
strm.avail_out = uncompLength - strm.total_out;
|
||||
|
||||
// Inflate another chunk.
|
||||
err = inflate(&strm, Z_SYNC_FLUSH);
|
||||
if (err == Z_STREAM_END)
|
||||
done = true;
|
||||
else if (err != Z_OK)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (inflateEnd(&strm) != Z_OK || !done)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
uncompressedBytes.resize(strm.total_out);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GZip::zip(const QByteArray &uncompressedBytes, QByteArray &compressedBytes)
|
||||
{
|
||||
if (uncompressedBytes.size() == 0)
|
||||
{
|
||||
compressedBytes = uncompressedBytes;
|
||||
return true;
|
||||
}
|
||||
|
||||
unsigned compLength = std::min(uncompressedBytes.size(), 16);
|
||||
compressedBytes.clear();
|
||||
compressedBytes.resize(compLength);
|
||||
|
||||
z_stream zs;
|
||||
memset(&zs, 0, sizeof(zs));
|
||||
|
||||
if (deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (16 + MAX_WBITS), 8, Z_DEFAULT_STRATEGY) != Z_OK)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
zs.next_in = (Bytef*)uncompressedBytes.data();
|
||||
zs.avail_in = uncompressedBytes.size();
|
||||
|
||||
int ret;
|
||||
compressedBytes.resize(uncompressedBytes.size());
|
||||
|
||||
unsigned offset = 0;
|
||||
unsigned temp = 0;
|
||||
do
|
||||
{
|
||||
auto remaining = compressedBytes.size() - offset;
|
||||
if(remaining < 1)
|
||||
{
|
||||
compressedBytes.resize(compressedBytes.size() * 2);
|
||||
}
|
||||
zs.next_out = (Bytef *) (compressedBytes.data() + offset);
|
||||
temp = zs.avail_out = compressedBytes.size() - offset;
|
||||
ret = deflate(&zs, Z_FINISH);
|
||||
offset += temp - zs.avail_out;
|
||||
} while (ret == Z_OK);
|
||||
|
||||
compressedBytes.resize(offset);
|
||||
|
||||
if (deflateEnd(&zs) != Z_OK)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ret != Z_STREAM_END)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
10
launcher/GZip.h
Normal file
10
launcher/GZip.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include <QByteArray>
|
||||
|
||||
class GZip
|
||||
{
|
||||
public:
|
||||
static bool unzip(const QByteArray &compressedBytes, QByteArray &uncompressedBytes);
|
||||
static bool zip(const QByteArray &uncompressedBytes, QByteArray &compressedBytes);
|
||||
};
|
||||
|
57
launcher/GZip_test.cpp
Normal file
57
launcher/GZip_test.cpp
Normal file
@ -0,0 +1,57 @@
|
||||
#include <QTest>
|
||||
#include "TestUtil.h"
|
||||
|
||||
#include "GZip.h"
|
||||
#include <random>
|
||||
|
||||
void fib(int &prev, int &cur)
|
||||
{
|
||||
auto ret = prev + cur;
|
||||
prev = cur;
|
||||
cur = ret;
|
||||
}
|
||||
|
||||
class GZipTest : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
private
|
||||
slots:
|
||||
|
||||
void test_Through()
|
||||
{
|
||||
// test up to 10 MB
|
||||
static const int size = 10 * 1024 * 1024;
|
||||
QByteArray random;
|
||||
QByteArray compressed;
|
||||
QByteArray decompressed;
|
||||
std::default_random_engine eng((std::random_device())());
|
||||
std::uniform_int_distribution<uint8_t> idis(0, std::numeric_limits<uint8_t>::max());
|
||||
|
||||
// initialize random buffer
|
||||
for(int i = 0; i < size; i++)
|
||||
{
|
||||
random.append((char)idis(eng));
|
||||
}
|
||||
|
||||
// initialize fibonacci
|
||||
int prev = 1;
|
||||
int cur = 1;
|
||||
|
||||
// test if fibonacci long random buffers pass through GZip
|
||||
do
|
||||
{
|
||||
QByteArray copy = random;
|
||||
copy.resize(cur);
|
||||
compressed.clear();
|
||||
decompressed.clear();
|
||||
QVERIFY(GZip::zip(copy, compressed));
|
||||
QVERIFY(GZip::unzip(compressed, decompressed));
|
||||
QCOMPARE(decompressed, copy);
|
||||
fib(prev, cur);
|
||||
} while (cur < size);
|
||||
}
|
||||
};
|
||||
|
||||
QTEST_GUILESS_MAIN(GZipTest)
|
||||
|
||||
#include "GZip_test.moc"
|
131
launcher/GuiUtil.cpp
Normal file
131
launcher/GuiUtil.cpp
Normal file
@ -0,0 +1,131 @@
|
||||
#include "GuiUtil.h"
|
||||
|
||||
#include <QClipboard>
|
||||
#include <QApplication>
|
||||
#include <QFileDialog>
|
||||
|
||||
#include "dialogs/ProgressDialog.h"
|
||||
#include "net/PasteUpload.h"
|
||||
#include "dialogs/CustomMessageBox.h"
|
||||
|
||||
#include "MultiMC.h"
|
||||
#include <settings/SettingsObject.h>
|
||||
#include <DesktopServices.h>
|
||||
#include <BuildConfig.h>
|
||||
|
||||
QString GuiUtil::uploadPaste(const QString &text, QWidget *parentWidget)
|
||||
{
|
||||
ProgressDialog dialog(parentWidget);
|
||||
auto APIKeySetting = MMC->settings()->get("PasteEEAPIKey").toString();
|
||||
if(APIKeySetting == "multimc")
|
||||
{
|
||||
APIKeySetting = BuildConfig.PASTE_EE_KEY;
|
||||
}
|
||||
std::unique_ptr<PasteUpload> paste(new PasteUpload(parentWidget, text, APIKeySetting));
|
||||
|
||||
if (!paste->validateText())
|
||||
{
|
||||
CustomMessageBox::selectable(
|
||||
parentWidget, QObject::tr("Upload failed"),
|
||||
QObject::tr("The log file is too big. You'll have to upload it manually."),
|
||||
QMessageBox::Warning)->exec();
|
||||
return QString();
|
||||
}
|
||||
|
||||
dialog.execWithTask(paste.get());
|
||||
if (!paste->wasSuccessful())
|
||||
{
|
||||
CustomMessageBox::selectable(parentWidget, QObject::tr("Upload failed"),
|
||||
paste->failReason(), QMessageBox::Critical)->exec();
|
||||
return QString();
|
||||
}
|
||||
else
|
||||
{
|
||||
const QString link = paste->pasteLink();
|
||||
setClipboardText(link);
|
||||
CustomMessageBox::selectable(
|
||||
parentWidget, QObject::tr("Upload finished"),
|
||||
QObject::tr("The <a href=\"%1\">link to the uploaded log</a> has been placed in your clipboard.").arg(link),
|
||||
QMessageBox::Information)->exec();
|
||||
return link;
|
||||
}
|
||||
}
|
||||
|
||||
void GuiUtil::setClipboardText(const QString &text)
|
||||
{
|
||||
QApplication::clipboard()->setText(text);
|
||||
}
|
||||
|
||||
static QStringList BrowseForFileInternal(QString context, QString caption, QString filter, QString defaultPath, QWidget *parentWidget, bool single)
|
||||
{
|
||||
static QMap<QString, QString> savedPaths;
|
||||
|
||||
QFileDialog w(parentWidget, caption);
|
||||
QSet<QString> locations;
|
||||
auto f = [&](QStandardPaths::StandardLocation l)
|
||||
{
|
||||
QString location = QStandardPaths::writableLocation(l);
|
||||
QFileInfo finfo(location);
|
||||
if (!finfo.exists())
|
||||
return;
|
||||
locations.insert(location);
|
||||
};
|
||||
f(QStandardPaths::DesktopLocation);
|
||||
f(QStandardPaths::DocumentsLocation);
|
||||
f(QStandardPaths::DownloadLocation);
|
||||
f(QStandardPaths::HomeLocation);
|
||||
QList<QUrl> urls;
|
||||
for (auto location : locations)
|
||||
{
|
||||
urls.append(QUrl::fromLocalFile(location));
|
||||
}
|
||||
urls.append(QUrl::fromLocalFile(defaultPath));
|
||||
|
||||
w.setFileMode(single ? QFileDialog::ExistingFile : QFileDialog::ExistingFiles);
|
||||
w.setAcceptMode(QFileDialog::AcceptOpen);
|
||||
w.setNameFilter(filter);
|
||||
|
||||
QString pathToOpen;
|
||||
if(savedPaths.contains(context))
|
||||
{
|
||||
pathToOpen = savedPaths[context];
|
||||
}
|
||||
else
|
||||
{
|
||||
pathToOpen = defaultPath;
|
||||
}
|
||||
if(!pathToOpen.isEmpty())
|
||||
{
|
||||
QFileInfo finfo(pathToOpen);
|
||||
if(finfo.exists() && finfo.isDir())
|
||||
{
|
||||
w.setDirectory(finfo.absoluteFilePath());
|
||||
}
|
||||
}
|
||||
|
||||
w.setSidebarUrls(urls);
|
||||
|
||||
if (w.exec())
|
||||
{
|
||||
savedPaths[context] = w.directory().absolutePath();
|
||||
return w.selectedFiles();
|
||||
}
|
||||
savedPaths[context] = w.directory().absolutePath();
|
||||
return {};
|
||||
}
|
||||
|
||||
QString GuiUtil::BrowseForFile(QString context, QString caption, QString filter, QString defaultPath, QWidget *parentWidget)
|
||||
{
|
||||
auto resultList = BrowseForFileInternal(context, caption, filter, defaultPath, parentWidget, true);
|
||||
if(resultList.size())
|
||||
{
|
||||
return resultList[0];
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
|
||||
QStringList GuiUtil::BrowseForFiles(QString context, QString caption, QString filter, QString defaultPath, QWidget *parentWidget)
|
||||
{
|
||||
return BrowseForFileInternal(context, caption, filter, defaultPath, parentWidget, false);
|
||||
}
|
11
launcher/GuiUtil.h
Normal file
11
launcher/GuiUtil.h
Normal file
@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
namespace GuiUtil
|
||||
{
|
||||
QString uploadPaste(const QString &text, QWidget *parentWidget);
|
||||
void setClipboardText(const QString &text);
|
||||
QStringList BrowseForFiles(QString context, QString caption, QString filter, QString defaultPath, QWidget *parentWidget);
|
||||
QString BrowseForFile(QString context, QString caption, QString filter, QString defaultPath, QWidget *parentWidget);
|
||||
}
|
76
launcher/HoeDown.h
Normal file
76
launcher/HoeDown.h
Normal file
@ -0,0 +1,76 @@
|
||||
/* Copyright 2013-2021 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 <hoedown/html.h>
|
||||
#include <hoedown/document.h>
|
||||
#include <QString>
|
||||
#include <QByteArray>
|
||||
|
||||
/**
|
||||
* hoedown wrapper, because dealing with resource lifetime in C is stupid
|
||||
*/
|
||||
class HoeDown
|
||||
{
|
||||
public:
|
||||
class buffer
|
||||
{
|
||||
public:
|
||||
buffer(size_t unit = 4096)
|
||||
{
|
||||
buf = hoedown_buffer_new(unit);
|
||||
}
|
||||
~buffer()
|
||||
{
|
||||
hoedown_buffer_free(buf);
|
||||
}
|
||||
const char * cstr()
|
||||
{
|
||||
return hoedown_buffer_cstr(buf);
|
||||
}
|
||||
void put(QByteArray input)
|
||||
{
|
||||
hoedown_buffer_put(buf, (uint8_t *) input.data(), input.size());
|
||||
}
|
||||
const uint8_t * data() const
|
||||
{
|
||||
return buf->data;
|
||||
}
|
||||
size_t size() const
|
||||
{
|
||||
return buf->size;
|
||||
}
|
||||
hoedown_buffer * buf;
|
||||
} ib, ob;
|
||||
HoeDown()
|
||||
{
|
||||
renderer = hoedown_html_renderer_new((hoedown_html_flags) 0,0);
|
||||
document = hoedown_document_new(renderer, (hoedown_extensions) 0, 8);
|
||||
}
|
||||
~HoeDown()
|
||||
{
|
||||
hoedown_document_free(document);
|
||||
hoedown_html_renderer_free(renderer);
|
||||
}
|
||||
QString process(QByteArray input)
|
||||
{
|
||||
ib.put(input);
|
||||
hoedown_document_render(document, ob.buf, ib.data(), ib.size());
|
||||
return ob.cstr();
|
||||
}
|
||||
private:
|
||||
hoedown_document * document;
|
||||
hoedown_renderer * renderer;
|
||||
};
|
60
launcher/InstanceCopyTask.cpp
Normal file
60
launcher/InstanceCopyTask.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
#include "InstanceCopyTask.h"
|
||||
#include "settings/INISettingsObject.h"
|
||||
#include "FileSystem.h"
|
||||
#include "NullInstance.h"
|
||||
#include "pathmatcher/RegexpMatcher.h"
|
||||
#include <QtConcurrentRun>
|
||||
|
||||
InstanceCopyTask::InstanceCopyTask(InstancePtr origInstance, bool copySaves, bool keepPlaytime)
|
||||
{
|
||||
m_origInstance = origInstance;
|
||||
m_keepPlaytime = keepPlaytime;
|
||||
|
||||
if(!copySaves)
|
||||
{
|
||||
// FIXME: get this from the original instance type...
|
||||
auto matcherReal = new RegexpMatcher("[.]?minecraft/saves");
|
||||
matcherReal->caseSensitive(false);
|
||||
m_matcher.reset(matcherReal);
|
||||
}
|
||||
}
|
||||
|
||||
void InstanceCopyTask::executeTask()
|
||||
{
|
||||
setStatus(tr("Copying instance %1").arg(m_origInstance->name()));
|
||||
|
||||
FS::copy folderCopy(m_origInstance->instanceRoot(), m_stagingPath);
|
||||
folderCopy.followSymlinks(false).blacklist(m_matcher.get());
|
||||
|
||||
m_copyFuture = QtConcurrent::run(QThreadPool::globalInstance(), folderCopy);
|
||||
connect(&m_copyFutureWatcher, &QFutureWatcher<bool>::finished, this, &InstanceCopyTask::copyFinished);
|
||||
connect(&m_copyFutureWatcher, &QFutureWatcher<bool>::canceled, this, &InstanceCopyTask::copyAborted);
|
||||
m_copyFutureWatcher.setFuture(m_copyFuture);
|
||||
}
|
||||
|
||||
void InstanceCopyTask::copyFinished()
|
||||
{
|
||||
auto successful = m_copyFuture.result();
|
||||
if(!successful)
|
||||
{
|
||||
emitFailed(tr("Instance folder copy failed."));
|
||||
return;
|
||||
}
|
||||
// FIXME: shouldn't this be able to report errors?
|
||||
auto instanceSettings = std::make_shared<INISettingsObject>(FS::PathCombine(m_stagingPath, "instance.cfg"));
|
||||
instanceSettings->registerSetting("InstanceType", "Legacy");
|
||||
|
||||
InstancePtr inst(new NullInstance(m_globalSettings, instanceSettings, m_stagingPath));
|
||||
inst->setName(m_instName);
|
||||
inst->setIconKey(m_instIcon);
|
||||
if(!m_keepPlaytime) {
|
||||
inst->resetTimePlayed();
|
||||
}
|
||||
emitSucceeded();
|
||||
}
|
||||
|
||||
void InstanceCopyTask::copyAborted()
|
||||
{
|
||||
emitFailed(tr("Instance folder copy has been aborted."));
|
||||
return;
|
||||
}
|
31
launcher/InstanceCopyTask.h
Normal file
31
launcher/InstanceCopyTask.h
Normal file
@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include "tasks/Task.h"
|
||||
#include "net/NetJob.h"
|
||||
#include <QUrl>
|
||||
#include <QFuture>
|
||||
#include <QFutureWatcher>
|
||||
#include "settings/SettingsObject.h"
|
||||
#include "BaseVersion.h"
|
||||
#include "BaseInstance.h"
|
||||
#include "InstanceTask.h"
|
||||
|
||||
class InstanceCopyTask : public InstanceTask
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit InstanceCopyTask(InstancePtr origInstance, bool copySaves, bool keepPlaytime);
|
||||
|
||||
protected:
|
||||
//! Entry point for tasks.
|
||||
virtual void executeTask() override;
|
||||
void copyFinished();
|
||||
void copyAborted();
|
||||
|
||||
private: /* data */
|
||||
InstancePtr m_origInstance;
|
||||
QFuture<bool> m_copyFuture;
|
||||
QFutureWatcher<bool> m_copyFutureWatcher;
|
||||
std::unique_ptr<IPathMatcher> m_matcher;
|
||||
bool m_keepPlaytime;
|
||||
};
|
31
launcher/InstanceCreationTask.cpp
Normal file
31
launcher/InstanceCreationTask.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
#include "InstanceCreationTask.h"
|
||||
#include "settings/INISettingsObject.h"
|
||||
#include "FileSystem.h"
|
||||
|
||||
//FIXME: remove this
|
||||
#include "minecraft/MinecraftInstance.h"
|
||||
#include "minecraft/PackProfile.h"
|
||||
|
||||
InstanceCreationTask::InstanceCreationTask(BaseVersionPtr version)
|
||||
{
|
||||
m_version = version;
|
||||
}
|
||||
|
||||
void InstanceCreationTask::executeTask()
|
||||
{
|
||||
setStatus(tr("Creating instance from version %1").arg(m_version->name()));
|
||||
{
|
||||
auto instanceSettings = std::make_shared<INISettingsObject>(FS::PathCombine(m_stagingPath, "instance.cfg"));
|
||||
instanceSettings->suspendSave();
|
||||
instanceSettings->registerSetting("InstanceType", "Legacy");
|
||||
instanceSettings->set("InstanceType", "OneSix");
|
||||
MinecraftInstance inst(m_globalSettings, instanceSettings, m_stagingPath);
|
||||
auto components = inst.getPackProfile();
|
||||
components->buildingFromScratch();
|
||||
components->setComponentVersion("net.minecraft", m_version->descriptor(), true);
|
||||
inst.setName(m_instName);
|
||||
inst.setIconKey(m_instIcon);
|
||||
instanceSettings->resumeSave();
|
||||
}
|
||||
emitSucceeded();
|
||||
}
|
22
launcher/InstanceCreationTask.h
Normal file
22
launcher/InstanceCreationTask.h
Normal file
@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include "tasks/Task.h"
|
||||
#include "net/NetJob.h"
|
||||
#include <QUrl>
|
||||
#include "settings/SettingsObject.h"
|
||||
#include "BaseVersion.h"
|
||||
#include "InstanceTask.h"
|
||||
|
||||
class InstanceCreationTask : public InstanceTask
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit InstanceCreationTask(BaseVersionPtr version);
|
||||
|
||||
protected:
|
||||
//! Entry point for tasks.
|
||||
virtual void executeTask() override;
|
||||
|
||||
private: /* data */
|
||||
BaseVersionPtr m_version;
|
||||
};
|
456
launcher/InstanceImportTask.cpp
Normal file
456
launcher/InstanceImportTask.cpp
Normal file
@ -0,0 +1,456 @@
|
||||
/* Copyright 2013-2021 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 "InstanceImportTask.h"
|
||||
#include "BaseInstance.h"
|
||||
#include "FileSystem.h"
|
||||
#include "Env.h"
|
||||
#include "MMCZip.h"
|
||||
#include "NullInstance.h"
|
||||
#include "settings/INISettingsObject.h"
|
||||
#include "icons/IIconList.h"
|
||||
#include "icons/IconUtils.h"
|
||||
#include <QtConcurrentRun>
|
||||
|
||||
// FIXME: this does not belong here, it's Minecraft/Flame specific
|
||||
#include "minecraft/MinecraftInstance.h"
|
||||
#include "minecraft/PackProfile.h"
|
||||
#include "modplatform/flame/FileResolvingTask.h"
|
||||
#include "modplatform/flame/PackManifest.h"
|
||||
#include "Json.h"
|
||||
#include <quazipdir.h>
|
||||
#include "modplatform/technic/TechnicPackProcessor.h"
|
||||
|
||||
InstanceImportTask::InstanceImportTask(const QUrl sourceUrl)
|
||||
{
|
||||
m_sourceUrl = sourceUrl;
|
||||
}
|
||||
|
||||
void InstanceImportTask::executeTask()
|
||||
{
|
||||
if (m_sourceUrl.isLocalFile())
|
||||
{
|
||||
m_archivePath = m_sourceUrl.toLocalFile();
|
||||
processZipPack();
|
||||
}
|
||||
else
|
||||
{
|
||||
setStatus(tr("Downloading modpack:\n%1").arg(m_sourceUrl.toString()));
|
||||
m_downloadRequired = true;
|
||||
|
||||
const QString path = m_sourceUrl.host() + '/' + m_sourceUrl.path();
|
||||
auto entry = ENV.metacache()->resolveEntry("general", path);
|
||||
entry->setStale(true);
|
||||
m_filesNetJob.reset(new NetJob(tr("Modpack download")));
|
||||
m_filesNetJob->addNetAction(Net::Download::makeCached(m_sourceUrl, entry));
|
||||
m_archivePath = entry->getFullPath();
|
||||
auto job = m_filesNetJob.get();
|
||||
connect(job, &NetJob::succeeded, this, &InstanceImportTask::downloadSucceeded);
|
||||
connect(job, &NetJob::progress, this, &InstanceImportTask::downloadProgressChanged);
|
||||
connect(job, &NetJob::failed, this, &InstanceImportTask::downloadFailed);
|
||||
m_filesNetJob->start();
|
||||
}
|
||||
}
|
||||
|
||||
void InstanceImportTask::downloadSucceeded()
|
||||
{
|
||||
processZipPack();
|
||||
m_filesNetJob.reset();
|
||||
}
|
||||
|
||||
void InstanceImportTask::downloadFailed(QString reason)
|
||||
{
|
||||
emitFailed(reason);
|
||||
m_filesNetJob.reset();
|
||||
}
|
||||
|
||||
void InstanceImportTask::downloadProgressChanged(qint64 current, qint64 total)
|
||||
{
|
||||
setProgress(current / 2, total);
|
||||
}
|
||||
|
||||
void InstanceImportTask::processZipPack()
|
||||
{
|
||||
setStatus(tr("Extracting modpack"));
|
||||
QDir extractDir(m_stagingPath);
|
||||
qDebug() << "Attempting to create instance from" << m_archivePath;
|
||||
|
||||
// open the zip and find relevant files in it
|
||||
m_packZip.reset(new QuaZip(m_archivePath));
|
||||
if (!m_packZip->open(QuaZip::mdUnzip))
|
||||
{
|
||||
emitFailed(tr("Unable to open supplied modpack zip file."));
|
||||
return;
|
||||
}
|
||||
|
||||
QStringList blacklist = {"instance.cfg", "manifest.json"};
|
||||
QString mmcFound = MMCZip::findFolderOfFileInZip(m_packZip.get(), "instance.cfg");
|
||||
bool technicFound = QuaZipDir(m_packZip.get()).exists("/bin/modpack.jar") || QuaZipDir(m_packZip.get()).exists("/bin/version.json");
|
||||
QString flameFound = MMCZip::findFolderOfFileInZip(m_packZip.get(), "manifest.json");
|
||||
QString root;
|
||||
if(!mmcFound.isNull())
|
||||
{
|
||||
// process as MultiMC instance/pack
|
||||
qDebug() << "MultiMC:" << mmcFound;
|
||||
root = mmcFound;
|
||||
m_modpackType = ModpackType::MultiMC;
|
||||
}
|
||||
else if (technicFound)
|
||||
{
|
||||
// process as Technic pack
|
||||
qDebug() << "Technic:" << technicFound;
|
||||
extractDir.mkpath(".minecraft");
|
||||
extractDir.cd(".minecraft");
|
||||
m_modpackType = ModpackType::Technic;
|
||||
}
|
||||
else if(!flameFound.isNull())
|
||||
{
|
||||
// process as Flame pack
|
||||
qDebug() << "Flame:" << flameFound;
|
||||
root = flameFound;
|
||||
m_modpackType = ModpackType::Flame;
|
||||
}
|
||||
if(m_modpackType == ModpackType::Unknown)
|
||||
{
|
||||
emitFailed(tr("Archive does not contain a recognized modpack type."));
|
||||
return;
|
||||
}
|
||||
|
||||
// make sure we extract just the pack
|
||||
m_extractFuture = QtConcurrent::run(QThreadPool::globalInstance(), MMCZip::extractSubDir, m_packZip.get(), root, extractDir.absolutePath());
|
||||
connect(&m_extractFutureWatcher, &QFutureWatcher<QStringList>::finished, this, &InstanceImportTask::extractFinished);
|
||||
connect(&m_extractFutureWatcher, &QFutureWatcher<QStringList>::canceled, this, &InstanceImportTask::extractAborted);
|
||||
m_extractFutureWatcher.setFuture(m_extractFuture);
|
||||
}
|
||||
|
||||
void InstanceImportTask::extractFinished()
|
||||
{
|
||||
m_packZip.reset();
|
||||
if (!m_extractFuture.result())
|
||||
{
|
||||
emitFailed(tr("Failed to extract modpack"));
|
||||
return;
|
||||
}
|
||||
QDir extractDir(m_stagingPath);
|
||||
|
||||
qDebug() << "Fixing permissions for extracted pack files...";
|
||||
QDirIterator it(extractDir, QDirIterator::Subdirectories);
|
||||
while (it.hasNext())
|
||||
{
|
||||
auto filepath = it.next();
|
||||
QFileInfo file(filepath);
|
||||
auto permissions = QFile::permissions(filepath);
|
||||
auto origPermissions = permissions;
|
||||
if(file.isDir())
|
||||
{
|
||||
// Folder +rwx for current user
|
||||
permissions |= QFileDevice::Permission::ReadUser | QFileDevice::Permission::WriteUser | QFileDevice::Permission::ExeUser;
|
||||
}
|
||||
else
|
||||
{
|
||||
// File +rw for current user
|
||||
permissions |= QFileDevice::Permission::ReadUser | QFileDevice::Permission::WriteUser;
|
||||
}
|
||||
if(origPermissions != permissions)
|
||||
{
|
||||
if(!QFile::setPermissions(filepath, permissions))
|
||||
{
|
||||
logWarning(tr("Could not fix permissions for %1").arg(filepath));
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "Fixed" << filepath;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch(m_modpackType)
|
||||
{
|
||||
case ModpackType::Flame:
|
||||
processFlame();
|
||||
return;
|
||||
case ModpackType::MultiMC:
|
||||
processMultiMC();
|
||||
return;
|
||||
case ModpackType::Technic:
|
||||
processTechnic();
|
||||
return;
|
||||
case ModpackType::Unknown:
|
||||
emitFailed(tr("Archive does not contain a recognized modpack type."));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void InstanceImportTask::extractAborted()
|
||||
{
|
||||
emitFailed(tr("Instance import has been aborted."));
|
||||
return;
|
||||
}
|
||||
|
||||
void InstanceImportTask::processFlame()
|
||||
{
|
||||
const static QMap<QString,QString> forgemap = {
|
||||
{"1.2.5", "3.4.9.171"},
|
||||
{"1.4.2", "6.0.1.355"},
|
||||
{"1.4.7", "6.6.2.534"},
|
||||
{"1.5.2", "7.8.1.737"}
|
||||
};
|
||||
Flame::Manifest pack;
|
||||
try
|
||||
{
|
||||
QString configPath = FS::PathCombine(m_stagingPath, "manifest.json");
|
||||
Flame::loadManifest(pack, configPath);
|
||||
QFile::remove(configPath);
|
||||
}
|
||||
catch (const JSONValidationError &e)
|
||||
{
|
||||
emitFailed(tr("Could not understand pack manifest:\n") + e.cause());
|
||||
return;
|
||||
}
|
||||
if(!pack.overrides.isEmpty())
|
||||
{
|
||||
QString overridePath = FS::PathCombine(m_stagingPath, pack.overrides);
|
||||
if (QFile::exists(overridePath))
|
||||
{
|
||||
QString mcPath = FS::PathCombine(m_stagingPath, "minecraft");
|
||||
if (!QFile::rename(overridePath, mcPath))
|
||||
{
|
||||
emitFailed(tr("Could not rename the overrides folder:\n") + pack.overrides);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
logWarning(tr("The specified overrides folder (%1) is missing. Maybe the modpack was already used before?").arg(pack.overrides));
|
||||
}
|
||||
}
|
||||
|
||||
QString forgeVersion;
|
||||
QString fabricVersion;
|
||||
for(auto &loader: pack.minecraft.modLoaders)
|
||||
{
|
||||
auto id = loader.id;
|
||||
if(id.startsWith("forge-"))
|
||||
{
|
||||
id.remove("forge-");
|
||||
forgeVersion = id;
|
||||
continue;
|
||||
}
|
||||
if(id.startsWith("fabric-"))
|
||||
{
|
||||
id.remove("fabric-");
|
||||
fabricVersion = id;
|
||||
continue;
|
||||
}
|
||||
logWarning(tr("Unknown mod loader in manifest: %1").arg(id));
|
||||
}
|
||||
|
||||
QString configPath = FS::PathCombine(m_stagingPath, "instance.cfg");
|
||||
auto instanceSettings = std::make_shared<INISettingsObject>(configPath);
|
||||
instanceSettings->registerSetting("InstanceType", "Legacy");
|
||||
instanceSettings->set("InstanceType", "OneSix");
|
||||
MinecraftInstance instance(m_globalSettings, instanceSettings, m_stagingPath);
|
||||
auto mcVersion = pack.minecraft.version;
|
||||
// Hack to correct some 'special sauce'...
|
||||
if(mcVersion.endsWith('.'))
|
||||
{
|
||||
mcVersion.remove(QRegExp("[.]+$"));
|
||||
logWarning(tr("Mysterious trailing dots removed from Minecraft version while importing pack."));
|
||||
}
|
||||
auto components = instance.getPackProfile();
|
||||
components->buildingFromScratch();
|
||||
components->setComponentVersion("net.minecraft", mcVersion, true);
|
||||
if(!forgeVersion.isEmpty())
|
||||
{
|
||||
// FIXME: dirty, nasty, hack. Proper solution requires dependency resolution and knowledge of the metadata.
|
||||
if(forgeVersion == "recommended")
|
||||
{
|
||||
if(forgemap.contains(mcVersion))
|
||||
{
|
||||
forgeVersion = forgemap[mcVersion];
|
||||
}
|
||||
else
|
||||
{
|
||||
logWarning(tr("Could not map recommended forge version for Minecraft %1").arg(mcVersion));
|
||||
}
|
||||
}
|
||||
components->setComponentVersion("net.minecraftforge", forgeVersion);
|
||||
}
|
||||
if(!fabricVersion.isEmpty())
|
||||
{
|
||||
components->setComponentVersion("net.fabricmc.fabric-loader", fabricVersion);
|
||||
}
|
||||
if (m_instIcon != "default")
|
||||
{
|
||||
instance.setIconKey(m_instIcon);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(pack.name.contains("Direwolf20"))
|
||||
{
|
||||
instance.setIconKey("steve");
|
||||
}
|
||||
else if(pack.name.contains("FTB") || pack.name.contains("Feed The Beast"))
|
||||
{
|
||||
instance.setIconKey("ftb_logo");
|
||||
}
|
||||
else
|
||||
{
|
||||
// default to something other than the MultiMC default to distinguish these
|
||||
instance.setIconKey("flame");
|
||||
}
|
||||
}
|
||||
QString jarmodsPath = FS::PathCombine(m_stagingPath, "minecraft", "jarmods");
|
||||
QFileInfo jarmodsInfo(jarmodsPath);
|
||||
if(jarmodsInfo.isDir())
|
||||
{
|
||||
// install all the jar mods
|
||||
qDebug() << "Found jarmods:";
|
||||
QDir jarmodsDir(jarmodsPath);
|
||||
QStringList jarMods;
|
||||
for (auto info: jarmodsDir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files))
|
||||
{
|
||||
qDebug() << info.fileName();
|
||||
jarMods.push_back(info.absoluteFilePath());
|
||||
}
|
||||
auto profile = instance.getPackProfile();
|
||||
profile->installJarMods(jarMods);
|
||||
// nuke the original files
|
||||
FS::deletePath(jarmodsPath);
|
||||
}
|
||||
instance.setName(m_instName);
|
||||
m_modIdResolver.reset(new Flame::FileResolvingTask(pack));
|
||||
connect(m_modIdResolver.get(), &Flame::FileResolvingTask::succeeded, [&]()
|
||||
{
|
||||
auto results = m_modIdResolver->getResults();
|
||||
m_filesNetJob.reset(new NetJob(tr("Mod download")));
|
||||
for(auto result: results.files)
|
||||
{
|
||||
QString filename = result.fileName;
|
||||
if(!result.required)
|
||||
{
|
||||
filename += ".disabled";
|
||||
}
|
||||
|
||||
auto relpath = FS::PathCombine("minecraft", result.targetFolder, filename);
|
||||
auto path = FS::PathCombine(m_stagingPath , relpath);
|
||||
|
||||
switch(result.type)
|
||||
{
|
||||
case Flame::File::Type::Folder:
|
||||
{
|
||||
logWarning(tr("This 'Folder' may need extracting: %1").arg(relpath));
|
||||
// fall-through intentional, we treat these as plain old mods and dump them wherever.
|
||||
}
|
||||
case Flame::File::Type::SingleFile:
|
||||
case Flame::File::Type::Mod:
|
||||
{
|
||||
qDebug() << "Will download" << result.url << "to" << path;
|
||||
auto dl = Net::Download::makeFile(result.url, path);
|
||||
m_filesNetJob->addNetAction(dl);
|
||||
break;
|
||||
}
|
||||
case Flame::File::Type::Modpack:
|
||||
logWarning(tr("Nesting modpacks in modpacks is not implemented, nothing was downloaded: %1").arg(relpath));
|
||||
break;
|
||||
case Flame::File::Type::Cmod2:
|
||||
case Flame::File::Type::Ctoc:
|
||||
case Flame::File::Type::Unknown:
|
||||
logWarning(tr("Unrecognized/unhandled PackageType for: %1").arg(relpath));
|
||||
break;
|
||||
}
|
||||
}
|
||||
m_modIdResolver.reset();
|
||||
connect(m_filesNetJob.get(), &NetJob::succeeded, this, [&]()
|
||||
{
|
||||
m_filesNetJob.reset();
|
||||
emitSucceeded();
|
||||
}
|
||||
);
|
||||
connect(m_filesNetJob.get(), &NetJob::failed, [&](QString reason)
|
||||
{
|
||||
m_filesNetJob.reset();
|
||||
emitFailed(reason);
|
||||
});
|
||||
connect(m_filesNetJob.get(), &NetJob::progress, [&](qint64 current, qint64 total)
|
||||
{
|
||||
setProgress(current, total);
|
||||
});
|
||||
setStatus(tr("Downloading mods..."));
|
||||
m_filesNetJob->start();
|
||||
}
|
||||
);
|
||||
connect(m_modIdResolver.get(), &Flame::FileResolvingTask::failed, [&](QString reason)
|
||||
{
|
||||
m_modIdResolver.reset();
|
||||
emitFailed(tr("Unable to resolve mod IDs:\n") + reason);
|
||||
});
|
||||
connect(m_modIdResolver.get(), &Flame::FileResolvingTask::progress, [&](qint64 current, qint64 total)
|
||||
{
|
||||
setProgress(current, total);
|
||||
});
|
||||
connect(m_modIdResolver.get(), &Flame::FileResolvingTask::status, [&](QString status)
|
||||
{
|
||||
setStatus(status);
|
||||
});
|
||||
m_modIdResolver->start();
|
||||
}
|
||||
|
||||
void InstanceImportTask::processTechnic()
|
||||
{
|
||||
shared_qobject_ptr<Technic::TechnicPackProcessor> packProcessor = new Technic::TechnicPackProcessor();
|
||||
connect(packProcessor.get(), &Technic::TechnicPackProcessor::succeeded, this, &InstanceImportTask::emitSucceeded);
|
||||
connect(packProcessor.get(), &Technic::TechnicPackProcessor::failed, this, &InstanceImportTask::emitFailed);
|
||||
packProcessor->run(m_globalSettings, m_instName, m_instIcon, m_stagingPath);
|
||||
}
|
||||
|
||||
void InstanceImportTask::processMultiMC()
|
||||
{
|
||||
// FIXME: copy from FolderInstanceProvider!!! FIX IT!!!
|
||||
QString configPath = FS::PathCombine(m_stagingPath, "instance.cfg");
|
||||
auto instanceSettings = std::make_shared<INISettingsObject>(configPath);
|
||||
instanceSettings->registerSetting("InstanceType", "Legacy");
|
||||
|
||||
NullInstance instance(m_globalSettings, instanceSettings, m_stagingPath);
|
||||
|
||||
// reset time played on import... because packs.
|
||||
instance.resetTimePlayed();
|
||||
|
||||
// set a new nice name
|
||||
instance.setName(m_instName);
|
||||
|
||||
// if the icon was specified by user, use that. otherwise pull icon from the pack
|
||||
if (m_instIcon != "default")
|
||||
{
|
||||
instance.setIconKey(m_instIcon);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_instIcon = instance.iconKey();
|
||||
|
||||
auto importIconPath = IconUtils::findBestIconIn(instance.instanceRoot(), m_instIcon);
|
||||
if (!importIconPath.isNull() && QFile::exists(importIconPath))
|
||||
{
|
||||
// import icon
|
||||
auto iconList = ENV.icons();
|
||||
if (iconList->iconFileExists(m_instIcon))
|
||||
{
|
||||
iconList->deleteIcon(m_instIcon);
|
||||
}
|
||||
iconList->installIcons({importIconPath});
|
||||
}
|
||||
}
|
||||
emitSucceeded();
|
||||
}
|
72
launcher/InstanceImportTask.h
Normal file
72
launcher/InstanceImportTask.h
Normal file
@ -0,0 +1,72 @@
|
||||
/* Copyright 2013-2021 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 "InstanceTask.h"
|
||||
#include "net/NetJob.h"
|
||||
#include <QUrl>
|
||||
#include <QFuture>
|
||||
#include <QFutureWatcher>
|
||||
#include "settings/SettingsObject.h"
|
||||
#include "QObjectPtr.h"
|
||||
|
||||
#include <nonstd/optional>
|
||||
|
||||
class QuaZip;
|
||||
namespace Flame
|
||||
{
|
||||
class FileResolvingTask;
|
||||
}
|
||||
|
||||
class InstanceImportTask : public InstanceTask
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit InstanceImportTask(const QUrl sourceUrl);
|
||||
|
||||
protected:
|
||||
//! Entry point for tasks.
|
||||
virtual void executeTask() override;
|
||||
|
||||
private:
|
||||
void processZipPack();
|
||||
void processMultiMC();
|
||||
void processFlame();
|
||||
void processTechnic();
|
||||
|
||||
private slots:
|
||||
void downloadSucceeded();
|
||||
void downloadFailed(QString reason);
|
||||
void downloadProgressChanged(qint64 current, qint64 total);
|
||||
void extractFinished();
|
||||
void extractAborted();
|
||||
|
||||
private: /* data */
|
||||
NetJobPtr m_filesNetJob;
|
||||
shared_qobject_ptr<Flame::FileResolvingTask> m_modIdResolver;
|
||||
QUrl m_sourceUrl;
|
||||
QString m_archivePath;
|
||||
bool m_downloadRequired = false;
|
||||
std::unique_ptr<QuaZip> m_packZip;
|
||||
QFuture<nonstd::optional<QStringList>> m_extractFuture;
|
||||
QFutureWatcher<nonstd::optional<QStringList>> m_extractFutureWatcher;
|
||||
enum class ModpackType{
|
||||
Unknown,
|
||||
MultiMC,
|
||||
Flame,
|
||||
Technic
|
||||
} m_modpackType = ModpackType::Unknown;
|
||||
};
|
867
launcher/InstanceList.cpp
Normal file
867
launcher/InstanceList.cpp
Normal file
@ -0,0 +1,867 @@
|
||||
/* Copyright 2013-2021 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 <QDir>
|
||||
#include <QDirIterator>
|
||||
#include <QSet>
|
||||
#include <QFile>
|
||||
#include <QThread>
|
||||
#include <QTextStream>
|
||||
#include <QXmlStreamReader>
|
||||
#include <QTimer>
|
||||
#include <QDebug>
|
||||
#include <QFileSystemWatcher>
|
||||
#include <QUuid>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
|
||||
#include "InstanceList.h"
|
||||
#include "BaseInstance.h"
|
||||
#include "InstanceTask.h"
|
||||
#include "settings/INISettingsObject.h"
|
||||
#include "minecraft/legacy/LegacyInstance.h"
|
||||
#include "NullInstance.h"
|
||||
#include "minecraft/MinecraftInstance.h"
|
||||
#include "FileSystem.h"
|
||||
#include "ExponentialSeries.h"
|
||||
#include "WatchLock.h"
|
||||
|
||||
const static int GROUP_FILE_FORMAT_VERSION = 1;
|
||||
|
||||
InstanceList::InstanceList(SettingsObjectPtr settings, const QString & instDir, QObject *parent)
|
||||
: QAbstractListModel(parent), m_globalSettings(settings)
|
||||
{
|
||||
resumeWatch();
|
||||
// Create aand normalize path
|
||||
if (!QDir::current().exists(instDir))
|
||||
{
|
||||
QDir::current().mkpath(instDir);
|
||||
}
|
||||
|
||||
connect(this, &InstanceList::instancesChanged, this, &InstanceList::providerUpdated);
|
||||
|
||||
// NOTE: canonicalPath requires the path to exist. Do not move this above the creation block!
|
||||
m_instDir = QDir(instDir).canonicalPath();
|
||||
m_watcher = new QFileSystemWatcher(this);
|
||||
connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, &InstanceList::instanceDirContentsChanged);
|
||||
m_watcher->addPath(m_instDir);
|
||||
}
|
||||
|
||||
InstanceList::~InstanceList()
|
||||
{
|
||||
}
|
||||
|
||||
int InstanceList::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
Q_UNUSED(parent);
|
||||
return m_instances.count();
|
||||
}
|
||||
|
||||
QModelIndex InstanceList::index(int row, int column, const QModelIndex &parent) const
|
||||
{
|
||||
Q_UNUSED(parent);
|
||||
if (row < 0 || row >= m_instances.size())
|
||||
return QModelIndex();
|
||||
return createIndex(row, column, (void *)m_instances.at(row).get());
|
||||
}
|
||||
|
||||
QVariant InstanceList::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid())
|
||||
{
|
||||
return QVariant();
|
||||
}
|
||||
BaseInstance *pdata = static_cast<BaseInstance *>(index.internalPointer());
|
||||
switch (role)
|
||||
{
|
||||
case InstancePointerRole:
|
||||
{
|
||||
QVariant v = qVariantFromValue((void *)pdata);
|
||||
return v;
|
||||
}
|
||||
case InstanceIDRole:
|
||||
{
|
||||
return pdata->id();
|
||||
}
|
||||
case Qt::EditRole:
|
||||
case Qt::DisplayRole:
|
||||
{
|
||||
return pdata->name();
|
||||
}
|
||||
case Qt::AccessibleTextRole:
|
||||
{
|
||||
return tr("%1 Instance").arg(pdata->name());
|
||||
}
|
||||
case Qt::ToolTipRole:
|
||||
{
|
||||
return pdata->instanceRoot();
|
||||
}
|
||||
case Qt::DecorationRole:
|
||||
{
|
||||
return pdata->iconKey();
|
||||
}
|
||||
// HACK: see GroupView.h in gui!
|
||||
case GroupRole:
|
||||
{
|
||||
return getInstanceGroup(pdata->id());
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
bool InstanceList::setData(const QModelIndex& index, const QVariant& value, int role)
|
||||
{
|
||||
if (!index.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if(role != Qt::EditRole)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
BaseInstance *pdata = static_cast<BaseInstance *>(index.internalPointer());
|
||||
auto newName = value.toString();
|
||||
if(pdata->name() == newName)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
pdata->setName(newName);
|
||||
return true;
|
||||
}
|
||||
|
||||
Qt::ItemFlags InstanceList::flags(const QModelIndex &index) const
|
||||
{
|
||||
Qt::ItemFlags f;
|
||||
if (index.isValid())
|
||||
{
|
||||
f |= (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable);
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
GroupId InstanceList::getInstanceGroup(const InstanceId& id) const
|
||||
{
|
||||
auto inst = getInstanceById(id);
|
||||
if(!inst)
|
||||
{
|
||||
return GroupId();
|
||||
}
|
||||
auto iter = m_instanceGroupIndex.find(inst->id());
|
||||
if(iter != m_instanceGroupIndex.end())
|
||||
{
|
||||
return *iter;
|
||||
}
|
||||
return GroupId();
|
||||
}
|
||||
|
||||
void InstanceList::setInstanceGroup(const InstanceId& id, const GroupId& name)
|
||||
{
|
||||
auto inst = getInstanceById(id);
|
||||
if(!inst)
|
||||
{
|
||||
qDebug() << "Attempt to set a null instance's group";
|
||||
return;
|
||||
}
|
||||
|
||||
bool changed = false;
|
||||
auto iter = m_instanceGroupIndex.find(inst->id());
|
||||
if(iter != m_instanceGroupIndex.end())
|
||||
{
|
||||
if(*iter != name)
|
||||
{
|
||||
*iter = name;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
changed = true;
|
||||
m_instanceGroupIndex[id] = name;
|
||||
}
|
||||
|
||||
if(changed)
|
||||
{
|
||||
m_groupNameCache.insert(name);
|
||||
auto idx = getInstIndex(inst.get());
|
||||
emit dataChanged(index(idx), index(idx), {GroupRole});
|
||||
saveGroupList();
|
||||
}
|
||||
}
|
||||
|
||||
QStringList InstanceList::getGroups()
|
||||
{
|
||||
return m_groupNameCache.toList();
|
||||
}
|
||||
|
||||
void InstanceList::deleteGroup(const QString& name)
|
||||
{
|
||||
bool removed = false;
|
||||
qDebug() << "Delete group" << name;
|
||||
for(auto & instance: m_instances)
|
||||
{
|
||||
const auto & instID = instance->id();
|
||||
auto instGroupName = getInstanceGroup(instID);
|
||||
if(instGroupName == name)
|
||||
{
|
||||
m_instanceGroupIndex.remove(instID);
|
||||
qDebug() << "Remove" << instID << "from group" << name;
|
||||
removed = true;
|
||||
auto idx = getInstIndex(instance.get());
|
||||
if(idx > 0)
|
||||
{
|
||||
emit dataChanged(index(idx), index(idx), {GroupRole});
|
||||
}
|
||||
}
|
||||
}
|
||||
if(removed)
|
||||
{
|
||||
saveGroupList();
|
||||
}
|
||||
}
|
||||
|
||||
bool InstanceList::isGroupCollapsed(const QString& group)
|
||||
{
|
||||
return m_collapsedGroups.contains(group);
|
||||
}
|
||||
|
||||
void InstanceList::deleteInstance(const InstanceId& id)
|
||||
{
|
||||
auto inst = getInstanceById(id);
|
||||
if(!inst)
|
||||
{
|
||||
qDebug() << "Cannot delete instance" << id << ". No such instance is present (deleted externally?).";
|
||||
return;
|
||||
}
|
||||
|
||||
if(m_instanceGroupIndex.remove(id))
|
||||
{
|
||||
saveGroupList();
|
||||
}
|
||||
|
||||
qDebug() << "Will delete instance" << id;
|
||||
if(!FS::deletePath(inst->instanceRoot()))
|
||||
{
|
||||
qWarning() << "Deletion of instance" << id << "has not been completely successful ...";
|
||||
return;
|
||||
}
|
||||
|
||||
qDebug() << "Instance" << id << "has been deleted by MultiMC.";
|
||||
}
|
||||
|
||||
static QMap<InstanceId, InstanceLocator> getIdMapping(const QList<InstancePtr> &list)
|
||||
{
|
||||
QMap<InstanceId, InstanceLocator> out;
|
||||
int i = 0;
|
||||
for(auto & item: list)
|
||||
{
|
||||
auto id = item->id();
|
||||
if(out.contains(id))
|
||||
{
|
||||
qWarning() << "Duplicate ID" << id << "in instance list";
|
||||
}
|
||||
out[id] = std::make_pair(item, i);
|
||||
i++;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
QList< InstanceId > InstanceList::discoverInstances()
|
||||
{
|
||||
qDebug() << "Discovering instances in" << m_instDir;
|
||||
QList<InstanceId> out;
|
||||
QDirIterator iter(m_instDir, QDir::Dirs | QDir::NoDot | QDir::NoDotDot | QDir::Readable | QDir::Hidden, QDirIterator::FollowSymlinks);
|
||||
while (iter.hasNext())
|
||||
{
|
||||
QString subDir = iter.next();
|
||||
QFileInfo dirInfo(subDir);
|
||||
if (!QFileInfo(FS::PathCombine(subDir, "instance.cfg")).exists())
|
||||
continue;
|
||||
// if it is a symlink, ignore it if it goes to the instance folder
|
||||
if(dirInfo.isSymLink())
|
||||
{
|
||||
QFileInfo targetInfo(dirInfo.symLinkTarget());
|
||||
QFileInfo instDirInfo(m_instDir);
|
||||
if(targetInfo.canonicalPath() == instDirInfo.canonicalFilePath())
|
||||
{
|
||||
qDebug() << "Ignoring symlink" << subDir << "that leads into the instances folder";
|
||||
continue;
|
||||
}
|
||||
}
|
||||
auto id = dirInfo.fileName();
|
||||
out.append(id);
|
||||
qDebug() << "Found instance ID" << id;
|
||||
}
|
||||
instanceSet = out.toSet();
|
||||
m_instancesProbed = true;
|
||||
return out;
|
||||
}
|
||||
|
||||
InstanceList::InstListError InstanceList::loadList()
|
||||
{
|
||||
auto existingIds = getIdMapping(m_instances);
|
||||
|
||||
QList<InstancePtr> newList;
|
||||
|
||||
for(auto & id: discoverInstances())
|
||||
{
|
||||
if(existingIds.contains(id))
|
||||
{
|
||||
auto instPair = existingIds[id];
|
||||
existingIds.remove(id);
|
||||
qDebug() << "Should keep and soft-reload" << id;
|
||||
}
|
||||
else
|
||||
{
|
||||
InstancePtr instPtr = loadInstance(id);
|
||||
if(instPtr)
|
||||
{
|
||||
newList.append(instPtr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: looks like a general algorithm with a few specifics inserted. Do something about it.
|
||||
if(!existingIds.isEmpty())
|
||||
{
|
||||
// get the list of removed instances and sort it by their original index, from last to first
|
||||
auto deadList = existingIds.values();
|
||||
auto orderSortPredicate = [](const InstanceLocator & a, const InstanceLocator & b) -> bool
|
||||
{
|
||||
return a.second > b.second;
|
||||
};
|
||||
std::sort(deadList.begin(), deadList.end(), orderSortPredicate);
|
||||
// remove the contiguous ranges of rows
|
||||
int front_bookmark = -1;
|
||||
int back_bookmark = -1;
|
||||
int currentItem = -1;
|
||||
auto removeNow = [&]()
|
||||
{
|
||||
beginRemoveRows(QModelIndex(), front_bookmark, back_bookmark);
|
||||
m_instances.erase(m_instances.begin() + front_bookmark, m_instances.begin() + back_bookmark + 1);
|
||||
endRemoveRows();
|
||||
front_bookmark = -1;
|
||||
back_bookmark = currentItem;
|
||||
};
|
||||
for(auto & removedItem: deadList)
|
||||
{
|
||||
auto instPtr = removedItem.first;
|
||||
instPtr->invalidate();
|
||||
currentItem = removedItem.second;
|
||||
if(back_bookmark == -1)
|
||||
{
|
||||
// no bookmark yet
|
||||
back_bookmark = currentItem;
|
||||
}
|
||||
else if(currentItem == front_bookmark - 1)
|
||||
{
|
||||
// part of contiguous sequence, continue
|
||||
}
|
||||
else
|
||||
{
|
||||
// seam between previous and current item
|
||||
removeNow();
|
||||
}
|
||||
front_bookmark = currentItem;
|
||||
}
|
||||
if(back_bookmark != -1)
|
||||
{
|
||||
removeNow();
|
||||
}
|
||||
}
|
||||
if(newList.size())
|
||||
{
|
||||
add(newList);
|
||||
}
|
||||
m_dirty = false;
|
||||
updateTotalPlayTime();
|
||||
return NoError;
|
||||
}
|
||||
|
||||
void InstanceList::updateTotalPlayTime()
|
||||
{
|
||||
totalPlayTime = 0;
|
||||
for(auto const& itr : m_instances)
|
||||
{
|
||||
totalPlayTime += itr.get()->totalTimePlayed();
|
||||
}
|
||||
}
|
||||
|
||||
void InstanceList::saveNow()
|
||||
{
|
||||
for(auto & item: m_instances)
|
||||
{
|
||||
item->saveNow();
|
||||
}
|
||||
}
|
||||
|
||||
void InstanceList::add(const QList<InstancePtr> &t)
|
||||
{
|
||||
beginInsertRows(QModelIndex(), m_instances.count(), m_instances.count() + t.size() - 1);
|
||||
m_instances.append(t);
|
||||
for(auto & ptr : t)
|
||||
{
|
||||
connect(ptr.get(), &BaseInstance::propertiesChanged, this, &InstanceList::propertiesChanged);
|
||||
}
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
void InstanceList::resumeWatch()
|
||||
{
|
||||
if(m_watchLevel > 0)
|
||||
{
|
||||
qWarning() << "Bad suspend level resume in instance list";
|
||||
return;
|
||||
}
|
||||
m_watchLevel++;
|
||||
if(m_watchLevel > 0 && m_dirty)
|
||||
{
|
||||
loadList();
|
||||
}
|
||||
}
|
||||
|
||||
void InstanceList::suspendWatch()
|
||||
{
|
||||
m_watchLevel --;
|
||||
}
|
||||
|
||||
void InstanceList::providerUpdated()
|
||||
{
|
||||
m_dirty = true;
|
||||
if(m_watchLevel == 1)
|
||||
{
|
||||
loadList();
|
||||
}
|
||||
}
|
||||
|
||||
InstancePtr InstanceList::getInstanceById(QString instId) const
|
||||
{
|
||||
if(instId.isEmpty())
|
||||
return InstancePtr();
|
||||
for(auto & inst: m_instances)
|
||||
{
|
||||
if (inst->id() == instId)
|
||||
{
|
||||
return inst;
|
||||
}
|
||||
}
|
||||
return InstancePtr();
|
||||
}
|
||||
|
||||
QModelIndex InstanceList::getInstanceIndexById(const QString &id) const
|
||||
{
|
||||
return index(getInstIndex(getInstanceById(id).get()));
|
||||
}
|
||||
|
||||
int InstanceList::getInstIndex(BaseInstance *inst) const
|
||||
{
|
||||
int count = m_instances.count();
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (inst == m_instances[i].get())
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void InstanceList::propertiesChanged(BaseInstance *inst)
|
||||
{
|
||||
int i = getInstIndex(inst);
|
||||
if (i != -1)
|
||||
{
|
||||
emit dataChanged(index(i), index(i));
|
||||
updateTotalPlayTime();
|
||||
}
|
||||
}
|
||||
|
||||
InstancePtr InstanceList::loadInstance(const InstanceId& id)
|
||||
{
|
||||
if(!m_groupsLoaded)
|
||||
{
|
||||
loadGroupList();
|
||||
}
|
||||
|
||||
auto instanceRoot = FS::PathCombine(m_instDir, id);
|
||||
auto instanceSettings = std::make_shared<INISettingsObject>(FS::PathCombine(instanceRoot, "instance.cfg"));
|
||||
InstancePtr inst;
|
||||
|
||||
instanceSettings->registerSetting("InstanceType", "Legacy");
|
||||
|
||||
QString inst_type = instanceSettings->get("InstanceType").toString();
|
||||
|
||||
if (inst_type == "OneSix" || inst_type == "Nostalgia")
|
||||
{
|
||||
inst.reset(new MinecraftInstance(m_globalSettings, instanceSettings, instanceRoot));
|
||||
}
|
||||
else if (inst_type == "Legacy")
|
||||
{
|
||||
inst.reset(new LegacyInstance(m_globalSettings, instanceSettings, instanceRoot));
|
||||
}
|
||||
else
|
||||
{
|
||||
inst.reset(new NullInstance(m_globalSettings, instanceSettings, instanceRoot));
|
||||
}
|
||||
qDebug() << "Loaded instance " << inst->name() << " from " << inst->instanceRoot();
|
||||
return inst;
|
||||
}
|
||||
|
||||
void InstanceList::saveGroupList()
|
||||
{
|
||||
qDebug() << "Will save group list now.";
|
||||
if(!m_instancesProbed)
|
||||
{
|
||||
qDebug() << "Group saving prevented because we don't know the full list of instances yet.";
|
||||
return;
|
||||
}
|
||||
WatchLock foo(m_watcher, m_instDir);
|
||||
QString groupFileName = m_instDir + "/instgroups.json";
|
||||
QMap<QString, QSet<QString>> reverseGroupMap;
|
||||
for (auto iter = m_instanceGroupIndex.begin(); iter != m_instanceGroupIndex.end(); iter++)
|
||||
{
|
||||
QString id = iter.key();
|
||||
QString group = iter.value();
|
||||
if (group.isEmpty())
|
||||
continue;
|
||||
if(!instanceSet.contains(id))
|
||||
{
|
||||
qDebug() << "Skipping saving missing instance" << id << "to groups list.";
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!reverseGroupMap.count(group))
|
||||
{
|
||||
QSet<QString> set;
|
||||
set.insert(id);
|
||||
reverseGroupMap[group] = set;
|
||||
}
|
||||
else
|
||||
{
|
||||
QSet<QString> &set = reverseGroupMap[group];
|
||||
set.insert(id);
|
||||
}
|
||||
}
|
||||
QJsonObject toplevel;
|
||||
toplevel.insert("formatVersion", QJsonValue(QString("1")));
|
||||
QJsonObject groupsArr;
|
||||
for (auto iter = reverseGroupMap.begin(); iter != reverseGroupMap.end(); iter++)
|
||||
{
|
||||
auto list = iter.value();
|
||||
auto name = iter.key();
|
||||
QJsonObject groupObj;
|
||||
QJsonArray instanceArr;
|
||||
groupObj.insert("hidden", QJsonValue(m_collapsedGroups.contains(name)));
|
||||
for (auto item : list)
|
||||
{
|
||||
instanceArr.append(QJsonValue(item));
|
||||
}
|
||||
groupObj.insert("instances", instanceArr);
|
||||
groupsArr.insert(name, groupObj);
|
||||
}
|
||||
toplevel.insert("groups", groupsArr);
|
||||
QJsonDocument doc(toplevel);
|
||||
try
|
||||
{
|
||||
FS::write(groupFileName, doc.toJson());
|
||||
qDebug() << "Group list saved.";
|
||||
}
|
||||
catch (const FS::FileSystemException &e)
|
||||
{
|
||||
qCritical() << "Failed to write instance group file :" << e.cause();
|
||||
}
|
||||
}
|
||||
|
||||
void InstanceList::loadGroupList()
|
||||
{
|
||||
qDebug() << "Will load group list now.";
|
||||
|
||||
QString groupFileName = m_instDir + "/instgroups.json";
|
||||
|
||||
// if there's no group file, fail
|
||||
if (!QFileInfo(groupFileName).exists())
|
||||
return;
|
||||
|
||||
QByteArray jsonData;
|
||||
try
|
||||
{
|
||||
jsonData = FS::read(groupFileName);
|
||||
}
|
||||
catch (const FS::FileSystemException &e)
|
||||
{
|
||||
qCritical() << "Failed to read instance group file :" << e.cause();
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonParseError error;
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &error);
|
||||
|
||||
// if the json was bad, fail
|
||||
if (error.error != QJsonParseError::NoError)
|
||||
{
|
||||
qCritical() << QString("Failed to parse instance group file: %1 at offset %2")
|
||||
.arg(error.errorString(), QString::number(error.offset))
|
||||
.toUtf8();
|
||||
return;
|
||||
}
|
||||
|
||||
// if the root of the json wasn't an object, fail
|
||||
if (!jsonDoc.isObject())
|
||||
{
|
||||
qWarning() << "Invalid group file. Root entry should be an object.";
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject rootObj = jsonDoc.object();
|
||||
|
||||
// Make sure the format version matches, otherwise fail.
|
||||
if (rootObj.value("formatVersion").toVariant().toInt() != GROUP_FILE_FORMAT_VERSION)
|
||||
return;
|
||||
|
||||
// Get the groups. if it's not an object, fail
|
||||
if (!rootObj.value("groups").isObject())
|
||||
{
|
||||
qWarning() << "Invalid group list JSON: 'groups' should be an object.";
|
||||
return;
|
||||
}
|
||||
|
||||
QSet<QString> groupSet;
|
||||
m_instanceGroupIndex.clear();
|
||||
|
||||
// Iterate through all the groups.
|
||||
QJsonObject groupMapping = rootObj.value("groups").toObject();
|
||||
for (QJsonObject::iterator iter = groupMapping.begin(); iter != groupMapping.end(); iter++)
|
||||
{
|
||||
QString groupName = iter.key();
|
||||
|
||||
// If not an object, complain and skip to the next one.
|
||||
if (!iter.value().isObject())
|
||||
{
|
||||
qWarning() << QString("Group '%1' in the group list should be an object.").arg(groupName).toUtf8();
|
||||
continue;
|
||||
}
|
||||
|
||||
QJsonObject groupObj = iter.value().toObject();
|
||||
if (!groupObj.value("instances").isArray())
|
||||
{
|
||||
qWarning() << QString("Group '%1' in the group list is invalid. It should contain an array called 'instances'.").arg(groupName).toUtf8();
|
||||
continue;
|
||||
}
|
||||
|
||||
// keep a list/set of groups for choosing
|
||||
groupSet.insert(groupName);
|
||||
|
||||
auto hidden = groupObj.value("hidden").toBool(false);
|
||||
if(hidden) {
|
||||
m_collapsedGroups.insert(groupName);
|
||||
}
|
||||
|
||||
// Iterate through the list of instances in the group.
|
||||
QJsonArray instancesArray = groupObj.value("instances").toArray();
|
||||
|
||||
for (QJsonArray::iterator iter2 = instancesArray.begin(); iter2 != instancesArray.end(); iter2++)
|
||||
{
|
||||
m_instanceGroupIndex[(*iter2).toString()] = groupName;
|
||||
}
|
||||
}
|
||||
m_groupsLoaded = true;
|
||||
m_groupNameCache.unite(groupSet);
|
||||
qDebug() << "Group list loaded.";
|
||||
}
|
||||
|
||||
void InstanceList::instanceDirContentsChanged(const QString& path)
|
||||
{
|
||||
Q_UNUSED(path);
|
||||
emit instancesChanged();
|
||||
}
|
||||
|
||||
void InstanceList::on_InstFolderChanged(const Setting &setting, QVariant value)
|
||||
{
|
||||
QString newInstDir = QDir(value.toString()).canonicalPath();
|
||||
if(newInstDir != m_instDir)
|
||||
{
|
||||
if(m_groupsLoaded)
|
||||
{
|
||||
saveGroupList();
|
||||
}
|
||||
m_instDir = newInstDir;
|
||||
m_groupsLoaded = false;
|
||||
emit instancesChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void InstanceList::on_GroupStateChanged(const QString& group, bool collapsed)
|
||||
{
|
||||
qDebug() << "Group" << group << (collapsed ? "collapsed" : "expanded");
|
||||
if(collapsed) {
|
||||
m_collapsedGroups.insert(group);
|
||||
} else {
|
||||
m_collapsedGroups.remove(group);
|
||||
}
|
||||
saveGroupList();
|
||||
}
|
||||
|
||||
class InstanceStaging : public Task
|
||||
{
|
||||
Q_OBJECT
|
||||
const unsigned minBackoff = 1;
|
||||
const unsigned maxBackoff = 16;
|
||||
public:
|
||||
InstanceStaging (
|
||||
InstanceList * parent,
|
||||
Task * child,
|
||||
const QString & stagingPath,
|
||||
const QString& instanceName,
|
||||
const QString& groupName )
|
||||
: backoff(minBackoff, maxBackoff)
|
||||
{
|
||||
m_parent = parent;
|
||||
m_child.reset(child);
|
||||
connect(child, &Task::succeeded, this, &InstanceStaging::childSucceded);
|
||||
connect(child, &Task::failed, this, &InstanceStaging::childFailed);
|
||||
connect(child, &Task::status, this, &InstanceStaging::setStatus);
|
||||
connect(child, &Task::progress, this, &InstanceStaging::setProgress);
|
||||
m_instanceName = instanceName;
|
||||
m_groupName = groupName;
|
||||
m_stagingPath = stagingPath;
|
||||
m_backoffTimer.setSingleShot(true);
|
||||
connect(&m_backoffTimer, &QTimer::timeout, this, &InstanceStaging::childSucceded);
|
||||
}
|
||||
|
||||
virtual ~InstanceStaging() {};
|
||||
|
||||
|
||||
// FIXME/TODO: add ability to abort during instance commit retries
|
||||
bool abort() override
|
||||
{
|
||||
if(m_child && m_child->canAbort())
|
||||
{
|
||||
return m_child->abort();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool canAbort() const override
|
||||
{
|
||||
if(m_child && m_child->canAbort())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void executeTask() override
|
||||
{
|
||||
m_child->start();
|
||||
}
|
||||
QStringList warnings() const override
|
||||
{
|
||||
return m_child->warnings();
|
||||
}
|
||||
|
||||
private slots:
|
||||
void childSucceded()
|
||||
{
|
||||
unsigned sleepTime = backoff();
|
||||
if(m_parent->commitStagedInstance(m_stagingPath, m_instanceName, m_groupName))
|
||||
{
|
||||
emitSucceeded();
|
||||
return;
|
||||
}
|
||||
// we actually failed, retry?
|
||||
if(sleepTime == maxBackoff)
|
||||
{
|
||||
emitFailed(tr("Failed to commit instance, even after multiple retries. It is being blocked by something."));
|
||||
return;
|
||||
}
|
||||
qDebug() << "Failed to commit instance" << m_instanceName << "Initiating backoff:" << sleepTime;
|
||||
m_backoffTimer.start(sleepTime * 500);
|
||||
}
|
||||
void childFailed(const QString & reason)
|
||||
{
|
||||
m_parent->destroyStagingPath(m_stagingPath);
|
||||
emitFailed(reason);
|
||||
}
|
||||
|
||||
private:
|
||||
/*
|
||||
* WHY: the whole reason why this uses an exponential backoff retry scheme is antivirus on Windows.
|
||||
* Basically, it starts messing things up while MultiMC is extracting/creating instances
|
||||
* and causes that horrible failure that is NTFS to lock files in place because they are open.
|
||||
*/
|
||||
ExponentialSeries backoff;
|
||||
QString m_stagingPath;
|
||||
InstanceList * m_parent;
|
||||
unique_qobject_ptr<Task> m_child;
|
||||
QString m_instanceName;
|
||||
QString m_groupName;
|
||||
QTimer m_backoffTimer;
|
||||
};
|
||||
|
||||
Task * InstanceList::wrapInstanceTask(InstanceTask * task)
|
||||
{
|
||||
auto stagingPath = getStagedInstancePath();
|
||||
task->setStagingPath(stagingPath);
|
||||
task->setParentSettings(m_globalSettings);
|
||||
return new InstanceStaging(this, task, stagingPath, task->name(), task->group());
|
||||
}
|
||||
|
||||
QString InstanceList::getStagedInstancePath()
|
||||
{
|
||||
QString key = QUuid::createUuid().toString();
|
||||
QString relPath = FS::PathCombine("_MMC_TEMP/" , key);
|
||||
QDir rootPath(m_instDir);
|
||||
auto path = FS::PathCombine(m_instDir, relPath);
|
||||
if(!rootPath.mkpath(relPath))
|
||||
{
|
||||
return QString();
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
bool InstanceList::commitStagedInstance(const QString& path, const QString& instanceName, const QString& groupName)
|
||||
{
|
||||
QDir dir;
|
||||
QString instID = FS::DirNameFromString(instanceName, m_instDir);
|
||||
{
|
||||
WatchLock lock(m_watcher, m_instDir);
|
||||
QString destination = FS::PathCombine(m_instDir, instID);
|
||||
if(!dir.rename(path, destination))
|
||||
{
|
||||
qWarning() << "Failed to move" << path << "to" << destination;
|
||||
return false;
|
||||
}
|
||||
m_instanceGroupIndex[instID] = groupName;
|
||||
instanceSet.insert(instID);
|
||||
m_groupNameCache.insert(groupName);
|
||||
emit instancesChanged();
|
||||
emit instanceSelectRequest(instID);
|
||||
}
|
||||
saveGroupList();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool InstanceList::destroyStagingPath(const QString& keyPath)
|
||||
{
|
||||
return FS::deletePath(keyPath);
|
||||
}
|
||||
|
||||
int InstanceList::getTotalPlayTime() {
|
||||
updateTotalPlayTime();
|
||||
return totalPlayTime;
|
||||
}
|
||||
|
||||
#include "InstanceList.moc"
|
173
launcher/InstanceList.h
Normal file
173
launcher/InstanceList.h
Normal file
@ -0,0 +1,173 @@
|
||||
/* Copyright 2013-2021 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 <QObject>
|
||||
#include <QAbstractListModel>
|
||||
#include <QSet>
|
||||
#include <QList>
|
||||
|
||||
#include "BaseInstance.h"
|
||||
|
||||
#include "QObjectPtr.h"
|
||||
|
||||
class QFileSystemWatcher;
|
||||
class InstanceTask;
|
||||
using InstanceId = QString;
|
||||
using GroupId = QString;
|
||||
using InstanceLocator = std::pair<InstancePtr, int>;
|
||||
|
||||
enum class InstCreateError
|
||||
{
|
||||
NoCreateError = 0,
|
||||
NoSuchVersion,
|
||||
UnknownCreateError,
|
||||
InstExists,
|
||||
CantCreateDir
|
||||
};
|
||||
|
||||
enum class GroupsState
|
||||
{
|
||||
NotLoaded,
|
||||
Steady,
|
||||
Dirty
|
||||
};
|
||||
|
||||
|
||||
class InstanceList : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit InstanceList(SettingsObjectPtr settings, const QString & instDir, QObject *parent = 0);
|
||||
virtual ~InstanceList();
|
||||
|
||||
public:
|
||||
QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const override;
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role) const override;
|
||||
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
|
||||
bool setData(const QModelIndex & index, const QVariant & value, int role) override;
|
||||
|
||||
enum AdditionalRoles
|
||||
{
|
||||
GroupRole = Qt::UserRole,
|
||||
InstancePointerRole = 0x34B1CB48, ///< Return pointer to real instance
|
||||
InstanceIDRole = 0x34B1CB49 ///< Return id if the instance
|
||||
};
|
||||
/*!
|
||||
* \brief Error codes returned by functions in the InstanceList class.
|
||||
* NoError Indicates that no error occurred.
|
||||
* UnknownError indicates that an unspecified error occurred.
|
||||
*/
|
||||
enum InstListError
|
||||
{
|
||||
NoError = 0,
|
||||
UnknownError
|
||||
};
|
||||
|
||||
InstancePtr at(int i) const
|
||||
{
|
||||
return m_instances.at(i);
|
||||
}
|
||||
|
||||
int count() const
|
||||
{
|
||||
return m_instances.count();
|
||||
}
|
||||
|
||||
InstListError loadList();
|
||||
void saveNow();
|
||||
|
||||
|
||||
InstancePtr getInstanceById(QString id) const;
|
||||
QModelIndex getInstanceIndexById(const QString &id) const;
|
||||
QStringList getGroups();
|
||||
bool isGroupCollapsed(const QString &groupName);
|
||||
|
||||
GroupId getInstanceGroup(const InstanceId & id) const;
|
||||
void setInstanceGroup(const InstanceId & id, const GroupId& name);
|
||||
|
||||
void deleteGroup(const GroupId & name);
|
||||
void deleteInstance(const InstanceId & id);
|
||||
|
||||
// Wrap an instance creation task in some more task machinery and make it ready to be used
|
||||
Task * wrapInstanceTask(InstanceTask * task);
|
||||
|
||||
/**
|
||||
* Create a new empty staging area for instance creation and @return a path/key top commit it later.
|
||||
* Used by instance manipulation tasks.
|
||||
*/
|
||||
QString getStagedInstancePath();
|
||||
|
||||
/**
|
||||
* Commit the staging area given by @keyPath to the provider - used when creation succeeds.
|
||||
* Used by instance manipulation tasks.
|
||||
*/
|
||||
bool commitStagedInstance(const QString & keyPath, const QString& instanceName, const QString & groupName);
|
||||
|
||||
/**
|
||||
* Destroy a previously created staging area given by @keyPath - used when creation fails.
|
||||
* Used by instance manipulation tasks.
|
||||
*/
|
||||
bool destroyStagingPath(const QString & keyPath);
|
||||
|
||||
int getTotalPlayTime();
|
||||
|
||||
signals:
|
||||
void dataIsInvalid();
|
||||
void instancesChanged();
|
||||
void instanceSelectRequest(QString instanceId);
|
||||
void groupsChanged(QSet<QString> groups);
|
||||
|
||||
public slots:
|
||||
void on_InstFolderChanged(const Setting &setting, QVariant value);
|
||||
void on_GroupStateChanged(const QString &group, bool collapsed);
|
||||
|
||||
private slots:
|
||||
void propertiesChanged(BaseInstance *inst);
|
||||
void providerUpdated();
|
||||
void instanceDirContentsChanged(const QString &path);
|
||||
|
||||
private:
|
||||
int getInstIndex(BaseInstance *inst) const;
|
||||
void updateTotalPlayTime();
|
||||
void suspendWatch();
|
||||
void resumeWatch();
|
||||
void add(const QList<InstancePtr> &list);
|
||||
void loadGroupList();
|
||||
void saveGroupList();
|
||||
QList<InstanceId> discoverInstances();
|
||||
InstancePtr loadInstance(const InstanceId& id);
|
||||
|
||||
private:
|
||||
int m_watchLevel = 0;
|
||||
int totalPlayTime = 0;
|
||||
bool m_dirty = false;
|
||||
QList<InstancePtr> m_instances;
|
||||
QSet<QString> m_groupNameCache;
|
||||
|
||||
SettingsObjectPtr m_globalSettings;
|
||||
QString m_instDir;
|
||||
QFileSystemWatcher * m_watcher;
|
||||
// FIXME: this is so inefficient that looking at it is almost painful.
|
||||
QSet<QString> m_collapsedGroups;
|
||||
QMap<InstanceId, GroupId> m_instanceGroupIndex;
|
||||
QSet<InstanceId> instanceSet;
|
||||
bool m_groupsLoaded = false;
|
||||
bool m_instancesProbed = false;
|
||||
};
|
76
launcher/InstancePageProvider.h
Normal file
76
launcher/InstancePageProvider.h
Normal file
@ -0,0 +1,76 @@
|
||||
#pragma once
|
||||
#include "minecraft/MinecraftInstance.h"
|
||||
#include "minecraft/legacy/LegacyInstance.h"
|
||||
#include <FileSystem.h>
|
||||
#include "pages/BasePage.h"
|
||||
#include "pages/BasePageProvider.h"
|
||||
#include "pages/instance/LogPage.h"
|
||||
#include "pages/instance/VersionPage.h"
|
||||
#include "pages/instance/ModFolderPage.h"
|
||||
#include "pages/instance/ResourcePackPage.h"
|
||||
#include "pages/instance/TexturePackPage.h"
|
||||
#include "pages/instance/NotesPage.h"
|
||||
#include "pages/instance/ScreenshotsPage.h"
|
||||
#include "pages/instance/InstanceSettingsPage.h"
|
||||
#include "pages/instance/OtherLogsPage.h"
|
||||
#include "pages/instance/LegacyUpgradePage.h"
|
||||
#include "pages/instance/WorldListPage.h"
|
||||
#include "pages/instance/ServersPage.h"
|
||||
#include "pages/instance/GameOptionsPage.h"
|
||||
|
||||
#include "Env.h"
|
||||
|
||||
class InstancePageProvider : public QObject, public BasePageProvider
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit InstancePageProvider(InstancePtr parent)
|
||||
{
|
||||
inst = parent;
|
||||
}
|
||||
|
||||
virtual ~InstancePageProvider() {};
|
||||
virtual QList<BasePage *> getPages() override
|
||||
{
|
||||
QList<BasePage *> values;
|
||||
values.append(new LogPage(inst));
|
||||
std::shared_ptr<MinecraftInstance> onesix = std::dynamic_pointer_cast<MinecraftInstance>(inst);
|
||||
if(onesix)
|
||||
{
|
||||
values.append(new VersionPage(onesix.get()));
|
||||
auto modsPage = new ModFolderPage(onesix.get(), onesix->loaderModList(), "mods", "loadermods", tr("Loader mods"), "Loader-mods");
|
||||
modsPage->setFilter("%1 (*.zip *.jar *.litemod)");
|
||||
values.append(modsPage);
|
||||
values.append(new CoreModFolderPage(onesix.get(), onesix->coreModList(), "coremods", "coremods", tr("Core mods"), "Core-mods"));
|
||||
values.append(new ResourcePackPage(onesix.get()));
|
||||
values.append(new TexturePackPage(onesix.get()));
|
||||
values.append(new NotesPage(onesix.get()));
|
||||
values.append(new WorldListPage(onesix.get(), onesix->worldList()));
|
||||
values.append(new ServersPage(onesix));
|
||||
// values.append(new GameOptionsPage(onesix.get()));
|
||||
values.append(new ScreenshotsPage(FS::PathCombine(onesix->gameRoot(), "screenshots")));
|
||||
values.append(new InstanceSettingsPage(onesix.get()));
|
||||
}
|
||||
std::shared_ptr<LegacyInstance> legacy = std::dynamic_pointer_cast<LegacyInstance>(inst);
|
||||
if(legacy)
|
||||
{
|
||||
values.append(new LegacyUpgradePage(legacy));
|
||||
values.append(new NotesPage(legacy.get()));
|
||||
values.append(new WorldListPage(legacy.get(), legacy->worldList()));
|
||||
values.append(new ScreenshotsPage(FS::PathCombine(legacy->gameRoot(), "screenshots")));
|
||||
}
|
||||
auto logMatcher = inst->getLogFileMatcher();
|
||||
if(logMatcher)
|
||||
{
|
||||
values.append(new OtherLogsPage(inst->getLogFileRoot(), logMatcher));
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
virtual QString dialogTitle() override
|
||||
{
|
||||
return tr("Edit Instance (%1)").arg(inst->name());
|
||||
}
|
||||
protected:
|
||||
InstancePtr inst;
|
||||
};
|
34
launcher/InstanceProxyModel.cpp
Normal file
34
launcher/InstanceProxyModel.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
#include "InstanceProxyModel.h"
|
||||
#include "MultiMC.h"
|
||||
#include <BaseInstance.h>
|
||||
#include <icons/IconList.h>
|
||||
|
||||
InstanceProxyModel::InstanceProxyModel(QObject *parent) : GroupedProxyModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
QVariant InstanceProxyModel::data(const QModelIndex & index, int role) const
|
||||
{
|
||||
QVariant data = QSortFilterProxyModel::data(index, role);
|
||||
if(role == Qt::DecorationRole)
|
||||
{
|
||||
return QVariant(MMC->icons()->getIcon(data.toString()));
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
bool InstanceProxyModel::subSortLessThan(const QModelIndex &left,
|
||||
const QModelIndex &right) const
|
||||
{
|
||||
BaseInstance *pdataLeft = static_cast<BaseInstance *>(left.internalPointer());
|
||||
BaseInstance *pdataRight = static_cast<BaseInstance *>(right.internalPointer());
|
||||
QString sortMode = MMC->settings()->get("InstSortMode").toString();
|
||||
if (sortMode == "LastLaunch")
|
||||
{
|
||||
return pdataLeft->lastLaunch() > pdataRight->lastLaunch();
|
||||
}
|
||||
else
|
||||
{
|
||||
return QString::localeAwareCompare(pdataLeft->name(), pdataRight->name()) < 0;
|
||||
}
|
||||
}
|
16
launcher/InstanceProxyModel.h
Normal file
16
launcher/InstanceProxyModel.h
Normal file
@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "groupview/GroupedProxyModel.h"
|
||||
|
||||
/**
|
||||
* A proxy model that is responsible for sorting instances into groups
|
||||
*/
|
||||
class InstanceProxyModel : public GroupedProxyModel
|
||||
{
|
||||
public:
|
||||
explicit InstanceProxyModel(QObject *parent = 0);
|
||||
QVariant data(const QModelIndex & index, int role) const override;
|
||||
|
||||
protected:
|
||||
virtual bool subSortLessThan(const QModelIndex &left, const QModelIndex &right) const override;
|
||||
};
|
9
launcher/InstanceTask.cpp
Normal file
9
launcher/InstanceTask.cpp
Normal file
@ -0,0 +1,9 @@
|
||||
#include "InstanceTask.h"
|
||||
|
||||
InstanceTask::InstanceTask()
|
||||
{
|
||||
}
|
||||
|
||||
InstanceTask::~InstanceTask()
|
||||
{
|
||||
}
|
52
launcher/InstanceTask.h
Normal file
52
launcher/InstanceTask.h
Normal file
@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
#include "tasks/Task.h"
|
||||
#include "settings/SettingsObject.h"
|
||||
|
||||
class InstanceTask : public Task
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit InstanceTask();
|
||||
virtual ~InstanceTask();
|
||||
|
||||
void setParentSettings(SettingsObjectPtr settings)
|
||||
{
|
||||
m_globalSettings = settings;
|
||||
}
|
||||
|
||||
void setStagingPath(const QString &stagingPath)
|
||||
{
|
||||
m_stagingPath = stagingPath;
|
||||
}
|
||||
|
||||
void setName(const QString &name)
|
||||
{
|
||||
m_instName = name;
|
||||
}
|
||||
QString name() const
|
||||
{
|
||||
return m_instName;
|
||||
}
|
||||
|
||||
void setIcon(const QString &icon)
|
||||
{
|
||||
m_instIcon = icon;
|
||||
}
|
||||
|
||||
void setGroup(const QString &group)
|
||||
{
|
||||
m_instGroup = group;
|
||||
}
|
||||
QString group() const
|
||||
{
|
||||
return m_instGroup;
|
||||
}
|
||||
|
||||
protected: /* data */
|
||||
SettingsObjectPtr m_globalSettings;
|
||||
QString m_instName;
|
||||
QString m_instIcon;
|
||||
QString m_instGroup;
|
||||
QString m_stagingPath;
|
||||
};
|
236
launcher/InstanceWindow.cpp
Normal file
236
launcher/InstanceWindow.cpp
Normal file
@ -0,0 +1,236 @@
|
||||
/* Copyright 2013-2021 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 "InstanceWindow.h"
|
||||
#include "MultiMC.h"
|
||||
|
||||
#include <QScrollBar>
|
||||
#include <QMessageBox>
|
||||
#include <QHBoxLayout>
|
||||
#include <QPushButton>
|
||||
#include <qlayoutitem.h>
|
||||
#include <QCloseEvent>
|
||||
|
||||
#include <dialogs/CustomMessageBox.h>
|
||||
#include <dialogs/ProgressDialog.h>
|
||||
#include "widgets/PageContainer.h"
|
||||
#include "InstancePageProvider.h"
|
||||
|
||||
#include "icons/IconList.h"
|
||||
|
||||
InstanceWindow::InstanceWindow(InstancePtr instance, QWidget *parent)
|
||||
: QMainWindow(parent), m_instance(instance)
|
||||
{
|
||||
setAttribute(Qt::WA_DeleteOnClose);
|
||||
|
||||
auto icon = MMC->icons()->getIcon(m_instance->iconKey());
|
||||
QString windowTitle = tr("Console window for ") + m_instance->name();
|
||||
|
||||
// Set window properties
|
||||
{
|
||||
setWindowIcon(icon);
|
||||
setWindowTitle(windowTitle);
|
||||
}
|
||||
|
||||
// Add page container
|
||||
{
|
||||
auto provider = std::make_shared<InstancePageProvider>(m_instance);
|
||||
m_container = new PageContainer(provider.get(), "console", this);
|
||||
m_container->setParentContainer(this);
|
||||
setCentralWidget(m_container);
|
||||
setContentsMargins(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
// Add custom buttons to the page container layout.
|
||||
{
|
||||
auto horizontalLayout = new QHBoxLayout();
|
||||
horizontalLayout->setObjectName(QStringLiteral("horizontalLayout"));
|
||||
horizontalLayout->setContentsMargins(6, -1, 6, -1);
|
||||
|
||||
auto btnHelp = new QPushButton();
|
||||
btnHelp->setText(tr("Help"));
|
||||
horizontalLayout->addWidget(btnHelp);
|
||||
connect(btnHelp, SIGNAL(clicked(bool)), m_container, SLOT(help()));
|
||||
|
||||
auto spacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
|
||||
horizontalLayout->addSpacerItem(spacer);
|
||||
|
||||
m_killButton = new QPushButton();
|
||||
horizontalLayout->addWidget(m_killButton);
|
||||
connect(m_killButton, SIGNAL(clicked(bool)), SLOT(on_btnKillMinecraft_clicked()));
|
||||
|
||||
m_launchOfflineButton = new QPushButton();
|
||||
horizontalLayout->addWidget(m_launchOfflineButton);
|
||||
m_launchOfflineButton->setText(tr("Launch Offline"));
|
||||
updateLaunchButtons();
|
||||
connect(m_launchOfflineButton, SIGNAL(clicked(bool)), SLOT(on_btnLaunchMinecraftOffline_clicked()));
|
||||
|
||||
m_closeButton = new QPushButton();
|
||||
m_closeButton->setText(tr("Close"));
|
||||
horizontalLayout->addWidget(m_closeButton);
|
||||
connect(m_closeButton, SIGNAL(clicked(bool)), SLOT(on_closeButton_clicked()));
|
||||
|
||||
m_container->addButtons(horizontalLayout);
|
||||
}
|
||||
|
||||
// restore window state
|
||||
{
|
||||
auto base64State = MMC->settings()->get("ConsoleWindowState").toByteArray();
|
||||
restoreState(QByteArray::fromBase64(base64State));
|
||||
auto base64Geometry = MMC->settings()->get("ConsoleWindowGeometry").toByteArray();
|
||||
restoreGeometry(QByteArray::fromBase64(base64Geometry));
|
||||
}
|
||||
|
||||
// set up instance and launch process recognition
|
||||
{
|
||||
auto launchTask = m_instance->getLaunchTask();
|
||||
on_InstanceLaunchTask_changed(launchTask);
|
||||
connect(m_instance.get(), &BaseInstance::launchTaskChanged, this, &InstanceWindow::on_InstanceLaunchTask_changed);
|
||||
connect(m_instance.get(), &BaseInstance::runningStatusChanged, this, &InstanceWindow::on_RunningState_changed);
|
||||
}
|
||||
|
||||
// set up instance destruction detection
|
||||
{
|
||||
connect(m_instance.get(), &BaseInstance::statusChanged, this, &InstanceWindow::on_instanceStatusChanged);
|
||||
}
|
||||
show();
|
||||
}
|
||||
|
||||
void InstanceWindow::on_instanceStatusChanged(BaseInstance::Status, BaseInstance::Status newStatus)
|
||||
{
|
||||
if(newStatus == BaseInstance::Status::Gone)
|
||||
{
|
||||
m_doNotSave = true;
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
void InstanceWindow::updateLaunchButtons()
|
||||
{
|
||||
if(m_instance->isRunning())
|
||||
{
|
||||
m_launchOfflineButton->setEnabled(false);
|
||||
m_killButton->setText(tr("Kill"));
|
||||
m_killButton->setObjectName("killButton");
|
||||
m_killButton->setToolTip(tr("Kill the running instance"));
|
||||
}
|
||||
else if(!m_instance->canLaunch())
|
||||
{
|
||||
m_launchOfflineButton->setEnabled(false);
|
||||
m_killButton->setText(tr("Launch"));
|
||||
m_killButton->setObjectName("launchButton");
|
||||
m_killButton->setToolTip(tr("Launch the instance"));
|
||||
m_killButton->setEnabled(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_launchOfflineButton->setEnabled(true);
|
||||
m_killButton->setText(tr("Launch"));
|
||||
m_killButton->setObjectName("launchButton");
|
||||
m_killButton->setToolTip(tr("Launch the instance"));
|
||||
}
|
||||
// NOTE: this is a hack to force the button to recalculate its style
|
||||
m_killButton->setStyleSheet("/* */");
|
||||
m_killButton->setStyleSheet(QString());
|
||||
}
|
||||
|
||||
void InstanceWindow::on_btnLaunchMinecraftOffline_clicked()
|
||||
{
|
||||
MMC->launch(m_instance, false, nullptr);
|
||||
}
|
||||
|
||||
void InstanceWindow::on_InstanceLaunchTask_changed(shared_qobject_ptr<LaunchTask> proc)
|
||||
{
|
||||
m_proc = proc;
|
||||
}
|
||||
|
||||
void InstanceWindow::on_RunningState_changed(bool running)
|
||||
{
|
||||
updateLaunchButtons();
|
||||
m_container->refreshContainer();
|
||||
if(running) {
|
||||
selectPage("log");
|
||||
}
|
||||
}
|
||||
|
||||
void InstanceWindow::on_closeButton_clicked()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
void InstanceWindow::closeEvent(QCloseEvent *event)
|
||||
{
|
||||
bool proceed = true;
|
||||
if(!m_doNotSave)
|
||||
{
|
||||
proceed &= m_container->prepareToClose();
|
||||
}
|
||||
|
||||
if(!proceed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
MMC->settings()->set("ConsoleWindowState", saveState().toBase64());
|
||||
MMC->settings()->set("ConsoleWindowGeometry", saveGeometry().toBase64());
|
||||
emit isClosing();
|
||||
event->accept();
|
||||
}
|
||||
|
||||
bool InstanceWindow::saveAll()
|
||||
{
|
||||
return m_container->saveAll();
|
||||
}
|
||||
|
||||
void InstanceWindow::on_btnKillMinecraft_clicked()
|
||||
{
|
||||
if(m_instance->isRunning())
|
||||
{
|
||||
MMC->kill(m_instance);
|
||||
}
|
||||
else
|
||||
{
|
||||
MMC->launch(m_instance, true, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
QString InstanceWindow::instanceId()
|
||||
{
|
||||
return m_instance->id();
|
||||
}
|
||||
|
||||
bool InstanceWindow::selectPage(QString pageId)
|
||||
{
|
||||
return m_container->selectPage(pageId);
|
||||
}
|
||||
|
||||
void InstanceWindow::refreshContainer()
|
||||
{
|
||||
m_container->refreshContainer();
|
||||
}
|
||||
|
||||
InstanceWindow::~InstanceWindow()
|
||||
{
|
||||
}
|
||||
|
||||
bool InstanceWindow::requestClose()
|
||||
{
|
||||
if(m_container->prepareToClose())
|
||||
{
|
||||
close();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
73
launcher/InstanceWindow.h
Normal file
73
launcher/InstanceWindow.h
Normal file
@ -0,0 +1,73 @@
|
||||
/* Copyright 2013-2021 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 <QMainWindow>
|
||||
#include "LaunchController.h"
|
||||
#include <QObjectPtr.h>
|
||||
#include <QSystemTrayIcon>
|
||||
#include "launch/LaunchTask.h"
|
||||
#include "pages/BasePageContainer.h"
|
||||
|
||||
class QPushButton;
|
||||
class PageContainer;
|
||||
class InstanceWindow : public QMainWindow, public BasePageContainer
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit InstanceWindow(InstancePtr proc, QWidget *parent = 0);
|
||||
virtual ~InstanceWindow();
|
||||
|
||||
bool selectPage(QString pageId) override;
|
||||
void refreshContainer() override;
|
||||
|
||||
QString instanceId();
|
||||
|
||||
// save all settings and changes (prepare for launch)
|
||||
bool saveAll();
|
||||
|
||||
// request closing the window (from a page)
|
||||
bool requestClose() override;
|
||||
|
||||
signals:
|
||||
void isClosing();
|
||||
|
||||
private
|
||||
slots:
|
||||
void on_closeButton_clicked();
|
||||
void on_btnKillMinecraft_clicked();
|
||||
void on_btnLaunchMinecraftOffline_clicked();
|
||||
|
||||
void on_InstanceLaunchTask_changed(shared_qobject_ptr<LaunchTask> proc);
|
||||
void on_RunningState_changed(bool running);
|
||||
void on_instanceStatusChanged(BaseInstance::Status, BaseInstance::Status newStatus);
|
||||
|
||||
protected:
|
||||
void closeEvent(QCloseEvent *) override;
|
||||
|
||||
private:
|
||||
void updateLaunchButtons();
|
||||
|
||||
private:
|
||||
shared_qobject_ptr<LaunchTask> m_proc;
|
||||
InstancePtr m_instance;
|
||||
bool m_doNotSave = false;
|
||||
PageContainer *m_container = nullptr;
|
||||
QPushButton *m_closeButton = nullptr;
|
||||
QPushButton *m_killButton = nullptr;
|
||||
QPushButton *m_launchOfflineButton = nullptr;
|
||||
};
|
104
launcher/JavaCommon.cpp
Normal file
104
launcher/JavaCommon.cpp
Normal file
@ -0,0 +1,104 @@
|
||||
#include "JavaCommon.h"
|
||||
#include "dialogs/CustomMessageBox.h"
|
||||
#include <MMCStrings.h>
|
||||
|
||||
bool JavaCommon::checkJVMArgs(QString jvmargs, QWidget *parent)
|
||||
{
|
||||
if (jvmargs.contains("-XX:PermSize=") || jvmargs.contains(QRegExp("-Xm[sx]"))
|
||||
|| jvmargs.contains("-XX-MaxHeapSize") || jvmargs.contains("-XX:InitialHeapSize"))
|
||||
{
|
||||
auto warnStr = QObject::tr(
|
||||
"You tried to manually set a JVM memory option (using \"-XX:PermSize\", \"-XX-MaxHeapSize\", \"-XX:InitialHeapSize\", \"-Xmx\" or \"-Xms\").\n"
|
||||
"There are dedicated boxes for these in the settings (Java tab, in the Memory group at the top).\n"
|
||||
"This message will be displayed until you remove them from the JVM arguments.");
|
||||
CustomMessageBox::selectable(
|
||||
parent, QObject::tr("JVM arguments warning"),
|
||||
warnStr,
|
||||
QMessageBox::Warning)->exec();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void JavaCommon::javaWasOk(QWidget *parent, JavaCheckResult result)
|
||||
{
|
||||
QString text;
|
||||
text += QObject::tr("Java test succeeded!<br />Platform reported: %1<br />Java version "
|
||||
"reported: %2<br />Java vendor "
|
||||
"reported: %3<br />").arg(result.realPlatform, result.javaVersion.toString(), result.javaVendor);
|
||||
if (result.errorLog.size())
|
||||
{
|
||||
auto htmlError = result.errorLog;
|
||||
htmlError.replace('\n', "<br />");
|
||||
text += QObject::tr("<br />Warnings:<br /><font color=\"orange\">%1</font>").arg(htmlError);
|
||||
}
|
||||
CustomMessageBox::selectable(parent, QObject::tr("Java test success"), text, QMessageBox::Information)->show();
|
||||
}
|
||||
|
||||
void JavaCommon::javaArgsWereBad(QWidget *parent, JavaCheckResult result)
|
||||
{
|
||||
auto htmlError = result.errorLog;
|
||||
QString text;
|
||||
htmlError.replace('\n', "<br />");
|
||||
text += QObject::tr("The specified java binary didn't work with the arguments you provided:<br />");
|
||||
text += QString("<font color=\"red\">%1</font>").arg(htmlError);
|
||||
CustomMessageBox::selectable(parent, QObject::tr("Java test failure"), text, QMessageBox::Warning)->show();
|
||||
}
|
||||
|
||||
void JavaCommon::javaBinaryWasBad(QWidget *parent, JavaCheckResult result)
|
||||
{
|
||||
QString text;
|
||||
text += QObject::tr(
|
||||
"The specified java binary didn't work.<br />You should use the auto-detect feature, "
|
||||
"or set the path to the java executable.<br />");
|
||||
CustomMessageBox::selectable(parent, QObject::tr("Java test failure"), text, QMessageBox::Warning)->show();
|
||||
}
|
||||
|
||||
void JavaCommon::TestCheck::run()
|
||||
{
|
||||
if (!JavaCommon::checkJVMArgs(m_args, m_parent))
|
||||
{
|
||||
emit finished();
|
||||
return;
|
||||
}
|
||||
checker.reset(new JavaChecker());
|
||||
connect(checker.get(), SIGNAL(checkFinished(JavaCheckResult)), this,
|
||||
SLOT(checkFinished(JavaCheckResult)));
|
||||
checker->m_path = m_path;
|
||||
checker->performCheck();
|
||||
}
|
||||
|
||||
void JavaCommon::TestCheck::checkFinished(JavaCheckResult result)
|
||||
{
|
||||
if (result.validity != JavaCheckResult::Validity::Valid)
|
||||
{
|
||||
javaBinaryWasBad(m_parent, result);
|
||||
emit finished();
|
||||
return;
|
||||
}
|
||||
checker.reset(new JavaChecker());
|
||||
connect(checker.get(), SIGNAL(checkFinished(JavaCheckResult)), this,
|
||||
SLOT(checkFinishedWithArgs(JavaCheckResult)));
|
||||
checker->m_path = m_path;
|
||||
checker->m_args = m_args;
|
||||
checker->m_minMem = m_minMem;
|
||||
checker->m_maxMem = m_maxMem;
|
||||
if (result.javaVersion.requiresPermGen())
|
||||
{
|
||||
checker->m_permGen = m_permGen;
|
||||
}
|
||||
checker->performCheck();
|
||||
}
|
||||
|
||||
void JavaCommon::TestCheck::checkFinishedWithArgs(JavaCheckResult result)
|
||||
{
|
||||
if (result.validity == JavaCheckResult::Validity::Valid)
|
||||
{
|
||||
javaWasOk(m_parent, result);
|
||||
emit finished();
|
||||
return;
|
||||
}
|
||||
javaArgsWereBad(m_parent, result);
|
||||
emit finished();
|
||||
}
|
||||
|
48
launcher/JavaCommon.h
Normal file
48
launcher/JavaCommon.h
Normal file
@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
#include <java/JavaChecker.h>
|
||||
|
||||
class QWidget;
|
||||
|
||||
/**
|
||||
* Common UI bits for the java pages to use.
|
||||
*/
|
||||
namespace JavaCommon
|
||||
{
|
||||
bool checkJVMArgs(QString args, QWidget *parent);
|
||||
|
||||
// Show a dialog saying that the Java binary was not usable
|
||||
void javaBinaryWasBad(QWidget *parent, JavaCheckResult result);
|
||||
// Show a dialog saying that the Java binary was not usable because of bad options
|
||||
void javaArgsWereBad(QWidget *parent, JavaCheckResult result);
|
||||
// Show a dialog saying that the Java binary was usable
|
||||
void javaWasOk(QWidget *parent, JavaCheckResult result);
|
||||
|
||||
class TestCheck : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
TestCheck(QWidget *parent, QString path, QString args, int minMem, int maxMem, int permGen)
|
||||
:m_parent(parent), m_path(path), m_args(args), m_minMem(minMem), m_maxMem(maxMem), m_permGen(permGen)
|
||||
{
|
||||
}
|
||||
virtual ~TestCheck() {};
|
||||
|
||||
void run();
|
||||
|
||||
signals:
|
||||
void finished();
|
||||
|
||||
private slots:
|
||||
void checkFinished(JavaCheckResult result);
|
||||
void checkFinishedWithArgs(JavaCheckResult result);
|
||||
|
||||
private:
|
||||
std::shared_ptr<JavaChecker> checker;
|
||||
QWidget *m_parent = nullptr;
|
||||
QString m_path;
|
||||
QString m_args;
|
||||
int m_minMem = 0;
|
||||
int m_maxMem = 0;
|
||||
int m_permGen = 64;
|
||||
};
|
||||
}
|
272
launcher/Json.cpp
Normal file
272
launcher/Json.cpp
Normal file
@ -0,0 +1,272 @@
|
||||
// Licensed under the Apache-2.0 license. See README.md for details.
|
||||
|
||||
#include "Json.h"
|
||||
|
||||
#include <QFile>
|
||||
|
||||
#include "FileSystem.h"
|
||||
#include <math.h>
|
||||
|
||||
namespace Json
|
||||
{
|
||||
void write(const QJsonDocument &doc, const QString &filename)
|
||||
{
|
||||
FS::write(filename, doc.toJson());
|
||||
}
|
||||
void write(const QJsonObject &object, const QString &filename)
|
||||
{
|
||||
write(QJsonDocument(object), filename);
|
||||
}
|
||||
void write(const QJsonArray &array, const QString &filename)
|
||||
{
|
||||
write(QJsonDocument(array), filename);
|
||||
}
|
||||
|
||||
QByteArray toBinary(const QJsonObject &obj)
|
||||
{
|
||||
return QJsonDocument(obj).toBinaryData();
|
||||
}
|
||||
QByteArray toBinary(const QJsonArray &array)
|
||||
{
|
||||
return QJsonDocument(array).toBinaryData();
|
||||
}
|
||||
QByteArray toText(const QJsonObject &obj)
|
||||
{
|
||||
return QJsonDocument(obj).toJson(QJsonDocument::Compact);
|
||||
}
|
||||
QByteArray toText(const QJsonArray &array)
|
||||
{
|
||||
return QJsonDocument(array).toJson(QJsonDocument::Compact);
|
||||
}
|
||||
|
||||
static bool isBinaryJson(const QByteArray &data)
|
||||
{
|
||||
decltype(QJsonDocument::BinaryFormatTag) tag = QJsonDocument::BinaryFormatTag;
|
||||
return memcmp(data.constData(), &tag, sizeof(QJsonDocument::BinaryFormatTag)) == 0;
|
||||
}
|
||||
QJsonDocument requireDocument(const QByteArray &data, const QString &what)
|
||||
{
|
||||
if (isBinaryJson(data))
|
||||
{
|
||||
QJsonDocument doc = QJsonDocument::fromBinaryData(data);
|
||||
if (doc.isNull())
|
||||
{
|
||||
throw JsonException(what + ": Invalid JSON (binary JSON detected)");
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
else
|
||||
{
|
||||
QJsonParseError error;
|
||||
QJsonDocument doc = QJsonDocument::fromJson(data, &error);
|
||||
if (error.error != QJsonParseError::NoError)
|
||||
{
|
||||
throw JsonException(what + ": Error parsing JSON: " + error.errorString());
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
}
|
||||
QJsonDocument requireDocument(const QString &filename, const QString &what)
|
||||
{
|
||||
return requireDocument(FS::read(filename), what);
|
||||
}
|
||||
QJsonObject requireObject(const QJsonDocument &doc, const QString &what)
|
||||
{
|
||||
if (!doc.isObject())
|
||||
{
|
||||
throw JsonException(what + " is not an object");
|
||||
}
|
||||
return doc.object();
|
||||
}
|
||||
QJsonArray requireArray(const QJsonDocument &doc, const QString &what)
|
||||
{
|
||||
if (!doc.isArray())
|
||||
{
|
||||
throw JsonException(what + " is not an array");
|
||||
}
|
||||
return doc.array();
|
||||
}
|
||||
|
||||
void writeString(QJsonObject &to, const QString &key, const QString &value)
|
||||
{
|
||||
if (!value.isEmpty())
|
||||
{
|
||||
to.insert(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
void writeStringList(QJsonObject &to, const QString &key, const QStringList &values)
|
||||
{
|
||||
if (!values.isEmpty())
|
||||
{
|
||||
QJsonArray array;
|
||||
for(auto value: values)
|
||||
{
|
||||
array.append(value);
|
||||
}
|
||||
to.insert(key, array);
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
QJsonValue toJson<QUrl>(const QUrl &url)
|
||||
{
|
||||
return QJsonValue(url.toString(QUrl::FullyEncoded));
|
||||
}
|
||||
template<>
|
||||
QJsonValue toJson<QByteArray>(const QByteArray &data)
|
||||
{
|
||||
return QJsonValue(QString::fromLatin1(data.toHex()));
|
||||
}
|
||||
template<>
|
||||
QJsonValue toJson<QDateTime>(const QDateTime &datetime)
|
||||
{
|
||||
return QJsonValue(datetime.toString(Qt::ISODate));
|
||||
}
|
||||
template<>
|
||||
QJsonValue toJson<QDir>(const QDir &dir)
|
||||
{
|
||||
return QDir::current().relativeFilePath(dir.absolutePath());
|
||||
}
|
||||
template<>
|
||||
QJsonValue toJson<QUuid>(const QUuid &uuid)
|
||||
{
|
||||
return uuid.toString();
|
||||
}
|
||||
template<>
|
||||
QJsonValue toJson<QVariant>(const QVariant &variant)
|
||||
{
|
||||
return QJsonValue::fromVariant(variant);
|
||||
}
|
||||
|
||||
|
||||
template<> QByteArray requireIsType<QByteArray>(const QJsonValue &value, const QString &what)
|
||||
{
|
||||
const QString string = ensureIsType<QString>(value, what);
|
||||
// ensure that the string can be safely cast to Latin1
|
||||
if (string != QString::fromLatin1(string.toLatin1()))
|
||||
{
|
||||
throw JsonException(what + " is not encodable as Latin1");
|
||||
}
|
||||
return QByteArray::fromHex(string.toLatin1());
|
||||
}
|
||||
|
||||
template<> QJsonArray requireIsType<QJsonArray>(const QJsonValue &value, const QString &what)
|
||||
{
|
||||
if (!value.isArray())
|
||||
{
|
||||
throw JsonException(what + " is not an array");
|
||||
}
|
||||
return value.toArray();
|
||||
}
|
||||
|
||||
|
||||
template<> QString requireIsType<QString>(const QJsonValue &value, const QString &what)
|
||||
{
|
||||
if (!value.isString())
|
||||
{
|
||||
throw JsonException(what + " is not a string");
|
||||
}
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
template<> bool requireIsType<bool>(const QJsonValue &value, const QString &what)
|
||||
{
|
||||
if (!value.isBool())
|
||||
{
|
||||
throw JsonException(what + " is not a bool");
|
||||
}
|
||||
return value.toBool();
|
||||
}
|
||||
|
||||
template<> double requireIsType<double>(const QJsonValue &value, const QString &what)
|
||||
{
|
||||
if (!value.isDouble())
|
||||
{
|
||||
throw JsonException(what + " is not a double");
|
||||
}
|
||||
return value.toDouble();
|
||||
}
|
||||
|
||||
template<> int requireIsType<int>(const QJsonValue &value, const QString &what)
|
||||
{
|
||||
const double doubl = requireIsType<double>(value, what);
|
||||
if (fmod(doubl, 1) != 0)
|
||||
{
|
||||
throw JsonException(what + " is not an integer");
|
||||
}
|
||||
return int(doubl);
|
||||
}
|
||||
|
||||
template<> QDateTime requireIsType<QDateTime>(const QJsonValue &value, const QString &what)
|
||||
{
|
||||
const QString string = requireIsType<QString>(value, what);
|
||||
const QDateTime datetime = QDateTime::fromString(string, Qt::ISODate);
|
||||
if (!datetime.isValid())
|
||||
{
|
||||
throw JsonException(what + " is not a ISO formatted date/time value");
|
||||
}
|
||||
return datetime;
|
||||
}
|
||||
|
||||
template<> QUrl requireIsType<QUrl>(const QJsonValue &value, const QString &what)
|
||||
{
|
||||
const QString string = ensureIsType<QString>(value, what);
|
||||
if (string.isEmpty())
|
||||
{
|
||||
return QUrl();
|
||||
}
|
||||
const QUrl url = QUrl(string, QUrl::StrictMode);
|
||||
if (!url.isValid())
|
||||
{
|
||||
throw JsonException(what + " is not a correctly formatted URL");
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
template<> QDir requireIsType<QDir>(const QJsonValue &value, const QString &what)
|
||||
{
|
||||
const QString string = requireIsType<QString>(value, what);
|
||||
// FIXME: does not handle invalid characters!
|
||||
return QDir::current().absoluteFilePath(string);
|
||||
}
|
||||
|
||||
template<> QUuid requireIsType<QUuid>(const QJsonValue &value, const QString &what)
|
||||
{
|
||||
const QString string = requireIsType<QString>(value, what);
|
||||
const QUuid uuid = QUuid(string);
|
||||
if (uuid.toString() != string) // converts back => valid
|
||||
{
|
||||
throw JsonException(what + " is not a valid UUID");
|
||||
}
|
||||
return uuid;
|
||||
}
|
||||
|
||||
template<> QJsonObject requireIsType<QJsonObject>(const QJsonValue &value, const QString &what)
|
||||
{
|
||||
if (!value.isObject())
|
||||
{
|
||||
throw JsonException(what + " is not an object");
|
||||
}
|
||||
return value.toObject();
|
||||
}
|
||||
|
||||
template<> QVariant requireIsType<QVariant>(const QJsonValue &value, const QString &what)
|
||||
{
|
||||
if (value.isNull() || value.isUndefined())
|
||||
{
|
||||
throw JsonException(what + " is null or undefined");
|
||||
}
|
||||
return value.toVariant();
|
||||
}
|
||||
|
||||
template<> QJsonValue requireIsType<QJsonValue>(const QJsonValue &value, const QString &what)
|
||||
{
|
||||
if (value.isNull() || value.isUndefined())
|
||||
{
|
||||
throw JsonException(what + " is null or undefined");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
249
launcher/Json.h
Normal file
249
launcher/Json.h
Normal file
@ -0,0 +1,249 @@
|
||||
// Licensed under the Apache-2.0 license. See README.md for details.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QDateTime>
|
||||
#include <QUrl>
|
||||
#include <QDir>
|
||||
#include <QUuid>
|
||||
#include <QVariant>
|
||||
#include <memory>
|
||||
|
||||
#include "Exception.h"
|
||||
|
||||
namespace Json
|
||||
{
|
||||
class JsonException : public ::Exception
|
||||
{
|
||||
public:
|
||||
JsonException(const QString &message) : Exception(message) {}
|
||||
};
|
||||
|
||||
/// @throw FileSystemException
|
||||
void write(const QJsonDocument &doc, const QString &filename);
|
||||
/// @throw FileSystemException
|
||||
void write(const QJsonObject &object, const QString &filename);
|
||||
/// @throw FileSystemException
|
||||
void write(const QJsonArray &array, const QString &filename);
|
||||
|
||||
QByteArray toBinary(const QJsonObject &obj);
|
||||
QByteArray toBinary(const QJsonArray &array);
|
||||
QByteArray toText(const QJsonObject &obj);
|
||||
QByteArray toText(const QJsonArray &array);
|
||||
|
||||
/// @throw JsonException
|
||||
QJsonDocument requireDocument(const QByteArray &data, const QString &what = "Document");
|
||||
/// @throw JsonException
|
||||
QJsonDocument requireDocument(const QString &filename, const QString &what = "Document");
|
||||
/// @throw JsonException
|
||||
QJsonObject requireObject(const QJsonDocument &doc, const QString &what = "Document");
|
||||
/// @throw JsonException
|
||||
QJsonArray requireArray(const QJsonDocument &doc, const QString &what = "Document");
|
||||
|
||||
/////////////////// WRITING ////////////////////
|
||||
|
||||
void writeString(QJsonObject & to, const QString &key, const QString &value);
|
||||
void writeStringList(QJsonObject & to, const QString &key, const QStringList &values);
|
||||
|
||||
template<typename T>
|
||||
QJsonValue toJson(const T &t)
|
||||
{
|
||||
return QJsonValue(t);
|
||||
}
|
||||
template<>
|
||||
QJsonValue toJson<QUrl>(const QUrl &url);
|
||||
template<>
|
||||
QJsonValue toJson<QByteArray>(const QByteArray &data);
|
||||
template<>
|
||||
QJsonValue toJson<QDateTime>(const QDateTime &datetime);
|
||||
template<>
|
||||
QJsonValue toJson<QDir>(const QDir &dir);
|
||||
template<>
|
||||
QJsonValue toJson<QUuid>(const QUuid &uuid);
|
||||
template<>
|
||||
QJsonValue toJson<QVariant>(const QVariant &variant);
|
||||
|
||||
template<typename T>
|
||||
QJsonArray toJsonArray(const QList<T> &container)
|
||||
{
|
||||
QJsonArray array;
|
||||
for (const T item : container)
|
||||
{
|
||||
array.append(toJson<T>(item));
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
////////////////// READING ////////////////////
|
||||
|
||||
/// @throw JsonException
|
||||
template <typename T>
|
||||
T requireIsType(const QJsonValue &value, const QString &what = "Value");
|
||||
|
||||
/// @throw JsonException
|
||||
template<> double requireIsType<double>(const QJsonValue &value, const QString &what);
|
||||
/// @throw JsonException
|
||||
template<> bool requireIsType<bool>(const QJsonValue &value, const QString &what);
|
||||
/// @throw JsonException
|
||||
template<> int requireIsType<int>(const QJsonValue &value, const QString &what);
|
||||
/// @throw JsonException
|
||||
template<> QJsonObject requireIsType<QJsonObject>(const QJsonValue &value, const QString &what);
|
||||
/// @throw JsonException
|
||||
template<> QJsonArray requireIsType<QJsonArray>(const QJsonValue &value, const QString &what);
|
||||
/// @throw JsonException
|
||||
template<> QJsonValue requireIsType<QJsonValue>(const QJsonValue &value, const QString &what);
|
||||
/// @throw JsonException
|
||||
template<> QByteArray requireIsType<QByteArray>(const QJsonValue &value, const QString &what);
|
||||
/// @throw JsonException
|
||||
template<> QDateTime requireIsType<QDateTime>(const QJsonValue &value, const QString &what);
|
||||
/// @throw JsonException
|
||||
template<> QVariant requireIsType<QVariant>(const QJsonValue &value, const QString &what);
|
||||
/// @throw JsonException
|
||||
template<> QString requireIsType<QString>(const QJsonValue &value, const QString &what);
|
||||
/// @throw JsonException
|
||||
template<> QUuid requireIsType<QUuid>(const QJsonValue &value, const QString &what);
|
||||
/// @throw JsonException
|
||||
template<> QDir requireIsType<QDir>(const QJsonValue &value, const QString &what);
|
||||
/// @throw JsonException
|
||||
template<> QUrl requireIsType<QUrl>(const QJsonValue &value, const QString &what);
|
||||
|
||||
// the following functions are higher level functions, that make use of the above functions for
|
||||
// type conversion
|
||||
template <typename T>
|
||||
T ensureIsType(const QJsonValue &value, const T default_ = T(), const QString &what = "Value")
|
||||
{
|
||||
if (value.isUndefined() || value.isNull())
|
||||
{
|
||||
return default_;
|
||||
}
|
||||
try
|
||||
{
|
||||
return requireIsType<T>(value, what);
|
||||
}
|
||||
catch (const JsonException &)
|
||||
{
|
||||
return default_;
|
||||
}
|
||||
}
|
||||
|
||||
/// @throw JsonException
|
||||
template <typename T>
|
||||
T requireIsType(const QJsonObject &parent, const QString &key, const QString &what = "__placeholder__")
|
||||
{
|
||||
const QString localWhat = QString(what).replace("__placeholder__", '\'' + key + '\'');
|
||||
if (!parent.contains(key))
|
||||
{
|
||||
throw JsonException(localWhat + "s parent does not contain " + localWhat);
|
||||
}
|
||||
return requireIsType<T>(parent.value(key), localWhat);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T ensureIsType(const QJsonObject &parent, const QString &key, const T default_ = T(), const QString &what = "__placeholder__")
|
||||
{
|
||||
const QString localWhat = QString(what).replace("__placeholder__", '\'' + key + '\'');
|
||||
if (!parent.contains(key))
|
||||
{
|
||||
return default_;
|
||||
}
|
||||
return ensureIsType<T>(parent.value(key), default_, localWhat);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
QVector<T> requireIsArrayOf(const QJsonDocument &doc)
|
||||
{
|
||||
const QJsonArray array = requireArray(doc);
|
||||
QVector<T> out;
|
||||
for (const QJsonValue val : array)
|
||||
{
|
||||
out.append(requireIsType<T>(val, "Document"));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
QVector<T> ensureIsArrayOf(const QJsonValue &value, const QString &what = "Value")
|
||||
{
|
||||
const QJsonArray array = ensureIsType<QJsonArray>(value, QJsonArray(), what);
|
||||
QVector<T> out;
|
||||
for (const QJsonValue val : array)
|
||||
{
|
||||
out.append(requireIsType<T>(val, what));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
QVector<T> ensureIsArrayOf(const QJsonValue &value, const QVector<T> default_, const QString &what = "Value")
|
||||
{
|
||||
if (value.isUndefined())
|
||||
{
|
||||
return default_;
|
||||
}
|
||||
return ensureIsArrayOf<T>(value, what);
|
||||
}
|
||||
|
||||
/// @throw JsonException
|
||||
template <typename T>
|
||||
QVector<T> requireIsArrayOf(const QJsonObject &parent, const QString &key, const QString &what = "__placeholder__")
|
||||
{
|
||||
const QString localWhat = QString(what).replace("__placeholder__", '\'' + key + '\'');
|
||||
if (!parent.contains(key))
|
||||
{
|
||||
throw JsonException(localWhat + "s parent does not contain " + localWhat);
|
||||
}
|
||||
return ensureIsArrayOf<T>(parent.value(key), localWhat);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
QVector<T> ensureIsArrayOf(const QJsonObject &parent, const QString &key,
|
||||
const QVector<T> &default_ = QVector<T>(), const QString &what = "__placeholder__")
|
||||
{
|
||||
const QString localWhat = QString(what).replace("__placeholder__", '\'' + key + '\'');
|
||||
if (!parent.contains(key))
|
||||
{
|
||||
return default_;
|
||||
}
|
||||
return ensureIsArrayOf<T>(parent.value(key), default_, localWhat);
|
||||
}
|
||||
|
||||
// this macro part could be replaced by variadic functions that just pass on their arguments, but that wouldn't work well with IDE helpers
|
||||
#define JSON_HELPERFUNCTIONS(NAME, TYPE) \
|
||||
inline TYPE require##NAME(const QJsonValue &value, const QString &what = "Value") \
|
||||
{ \
|
||||
return requireIsType<TYPE>(value, what); \
|
||||
} \
|
||||
inline TYPE ensure##NAME(const QJsonValue &value, const TYPE default_ = TYPE(), const QString &what = "Value") \
|
||||
{ \
|
||||
return ensureIsType<TYPE>(value, default_, what); \
|
||||
} \
|
||||
inline TYPE require##NAME(const QJsonObject &parent, const QString &key, const QString &what = "__placeholder__") \
|
||||
{ \
|
||||
return requireIsType<TYPE>(parent, key, what); \
|
||||
} \
|
||||
inline TYPE ensure##NAME(const QJsonObject &parent, const QString &key, const TYPE default_ = TYPE(), const QString &what = "__placeholder") \
|
||||
{ \
|
||||
return ensureIsType<TYPE>(parent, key, default_, what); \
|
||||
}
|
||||
|
||||
JSON_HELPERFUNCTIONS(Array, QJsonArray)
|
||||
JSON_HELPERFUNCTIONS(Object, QJsonObject)
|
||||
JSON_HELPERFUNCTIONS(JsonValue, QJsonValue)
|
||||
JSON_HELPERFUNCTIONS(String, QString)
|
||||
JSON_HELPERFUNCTIONS(Boolean, bool)
|
||||
JSON_HELPERFUNCTIONS(Double, double)
|
||||
JSON_HELPERFUNCTIONS(Integer, int)
|
||||
JSON_HELPERFUNCTIONS(DateTime, QDateTime)
|
||||
JSON_HELPERFUNCTIONS(Url, QUrl)
|
||||
JSON_HELPERFUNCTIONS(ByteArray, QByteArray)
|
||||
JSON_HELPERFUNCTIONS(Dir, QDir)
|
||||
JSON_HELPERFUNCTIONS(Uuid, QUuid)
|
||||
JSON_HELPERFUNCTIONS(Variant, QVariant)
|
||||
|
||||
#undef JSON_HELPERFUNCTIONS
|
||||
|
||||
}
|
||||
using JSONValidationError = Json::JsonException;
|
44
launcher/KonamiCode.cpp
Normal file
44
launcher/KonamiCode.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
#include "KonamiCode.h"
|
||||
|
||||
#include <array>
|
||||
#include <QDebug>
|
||||
|
||||
namespace {
|
||||
const std::array<Qt::Key, 10> konamiCode =
|
||||
{
|
||||
{
|
||||
Qt::Key_Up, Qt::Key_Up,
|
||||
Qt::Key_Down, Qt::Key_Down,
|
||||
Qt::Key_Left, Qt::Key_Right,
|
||||
Qt::Key_Left, Qt::Key_Right,
|
||||
Qt::Key_B, Qt::Key_A
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
KonamiCode::KonamiCode(QObject* parent) : QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void KonamiCode::input(QEvent* event)
|
||||
{
|
||||
if( event->type() == QEvent::KeyPress )
|
||||
{
|
||||
QKeyEvent *keyEvent = static_cast<QKeyEvent*>( event );
|
||||
auto key = Qt::Key(keyEvent->key());
|
||||
if(key == konamiCode[m_progress])
|
||||
{
|
||||
m_progress ++;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_progress = 0;
|
||||
}
|
||||
if(m_progress == static_cast<int>(konamiCode.size()))
|
||||
{
|
||||
m_progress = 0;
|
||||
emit triggered();
|
||||
}
|
||||
}
|
||||
}
|
17
launcher/KonamiCode.h
Normal file
17
launcher/KonamiCode.h
Normal file
@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <QKeyEvent>
|
||||
|
||||
class KonamiCode : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
KonamiCode(QObject *parent = 0);
|
||||
void input(QEvent *event);
|
||||
|
||||
signals:
|
||||
void triggered();
|
||||
|
||||
private:
|
||||
int m_progress = 0;
|
||||
};
|
353
launcher/LaunchController.cpp
Normal file
353
launcher/LaunchController.cpp
Normal file
@ -0,0 +1,353 @@
|
||||
#include "LaunchController.h"
|
||||
#include "MainWindow.h"
|
||||
#include <minecraft/auth/MojangAccountList.h>
|
||||
#include "MultiMC.h"
|
||||
#include "dialogs/CustomMessageBox.h"
|
||||
#include "dialogs/ProfileSelectDialog.h"
|
||||
#include "dialogs/ProgressDialog.h"
|
||||
#include "dialogs/EditAccountDialog.h"
|
||||
#include "InstanceWindow.h"
|
||||
#include "BuildConfig.h"
|
||||
#include "JavaCommon.h"
|
||||
#include <QLineEdit>
|
||||
#include <QInputDialog>
|
||||
#include <tasks/Task.h>
|
||||
#include <minecraft/auth/YggdrasilTask.h>
|
||||
#include <launch/steps/TextPrint.h>
|
||||
#include <QStringList>
|
||||
#include <QHostInfo>
|
||||
#include <QList>
|
||||
#include <QHostAddress>
|
||||
|
||||
LaunchController::LaunchController(QObject *parent) : Task(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void LaunchController::executeTask()
|
||||
{
|
||||
if (!m_instance)
|
||||
{
|
||||
emitFailed(tr("No instance specified!"));
|
||||
return;
|
||||
}
|
||||
|
||||
login();
|
||||
}
|
||||
|
||||
// FIXME: minecraft specific
|
||||
void LaunchController::login()
|
||||
{
|
||||
JavaCommon::checkJVMArgs(m_instance->settings()->get("JvmArgs").toString(), m_parentWidget);
|
||||
|
||||
// Find an account to use.
|
||||
std::shared_ptr<MojangAccountList> accounts = MMC->accounts();
|
||||
MojangAccountPtr account = accounts->activeAccount();
|
||||
if (accounts->count() <= 0)
|
||||
{
|
||||
// Tell the user they need to log in at least one account in order to play.
|
||||
auto reply = CustomMessageBox::selectable(
|
||||
m_parentWidget, tr("No Accounts"),
|
||||
tr("In order to play Minecraft, you must have at least one Mojang or Minecraft "
|
||||
"account logged in to MultiMC."
|
||||
"Would you like to open the account manager to add an account now?"),
|
||||
QMessageBox::Information, QMessageBox::Yes | QMessageBox::No)->exec();
|
||||
|
||||
if (reply == QMessageBox::Yes)
|
||||
{
|
||||
// Open the account manager.
|
||||
MMC->ShowGlobalSettings(m_parentWidget, "accounts");
|
||||
}
|
||||
}
|
||||
else if (account.get() == nullptr)
|
||||
{
|
||||
// If no default account is set, ask the user which one to use.
|
||||
ProfileSelectDialog selectDialog(tr("Which profile would you like to use?"),
|
||||
ProfileSelectDialog::GlobalDefaultCheckbox, m_parentWidget);
|
||||
|
||||
selectDialog.exec();
|
||||
|
||||
// Launch the instance with the selected account.
|
||||
account = selectDialog.selectedAccount();
|
||||
|
||||
// If the user said to use the account as default, do that.
|
||||
if (selectDialog.useAsGlobalDefault() && account.get() != nullptr)
|
||||
accounts->setActiveAccount(account->username());
|
||||
}
|
||||
|
||||
// if no account is selected, we bail
|
||||
if (!account.get())
|
||||
{
|
||||
emitFailed(tr("No account selected for launch."));
|
||||
return;
|
||||
}
|
||||
|
||||
// we try empty password first :)
|
||||
QString password;
|
||||
// we loop until the user succeeds in logging in or gives up
|
||||
bool tryagain = true;
|
||||
// the failure. the default failure.
|
||||
const QString needLoginAgain = tr("Your account is currently not logged in. Please enter your password to log in again. <br /> <br /> This could be caused by a password change.");
|
||||
QString failReason = needLoginAgain;
|
||||
|
||||
while (tryagain)
|
||||
{
|
||||
m_session = std::make_shared<AuthSession>();
|
||||
m_session->wants_online = m_online;
|
||||
auto task = account->login(m_session, password);
|
||||
if (task)
|
||||
{
|
||||
// We'll need to validate the access token to make sure the account
|
||||
// is still logged in.
|
||||
ProgressDialog progDialog(m_parentWidget);
|
||||
if (m_online)
|
||||
{
|
||||
progDialog.setSkipButton(true, tr("Play Offline"));
|
||||
}
|
||||
progDialog.execWithTask(task.get());
|
||||
if (!task->wasSuccessful())
|
||||
{
|
||||
auto failReasonNew = task->failReason();
|
||||
if(failReasonNew == "Invalid token.")
|
||||
{
|
||||
account->invalidateClientToken();
|
||||
failReason = needLoginAgain;
|
||||
}
|
||||
else failReason = failReasonNew;
|
||||
}
|
||||
}
|
||||
switch (m_session->status)
|
||||
{
|
||||
case AuthSession::Undetermined:
|
||||
{
|
||||
qCritical() << "Received undetermined session status during login. Bye.";
|
||||
tryagain = false;
|
||||
emitFailed(tr("Received undetermined session status during login."));
|
||||
break;
|
||||
}
|
||||
case AuthSession::RequiresPassword:
|
||||
{
|
||||
EditAccountDialog passDialog(failReason, m_parentWidget, EditAccountDialog::PasswordField);
|
||||
auto username = m_session->username;
|
||||
auto chopN = [](QString toChop, int N) -> QString
|
||||
{
|
||||
if(toChop.size() > N)
|
||||
{
|
||||
auto left = toChop.left(N);
|
||||
left += QString("\u25CF").repeated(toChop.size() - N);
|
||||
return left;
|
||||
}
|
||||
return toChop;
|
||||
};
|
||||
|
||||
if(username.contains('@'))
|
||||
{
|
||||
auto parts = username.split('@');
|
||||
auto mailbox = chopN(parts[0],3);
|
||||
QString domain = chopN(parts[1], 3);
|
||||
username = mailbox + '@' + domain;
|
||||
}
|
||||
passDialog.setUsername(username);
|
||||
if (passDialog.exec() == QDialog::Accepted)
|
||||
{
|
||||
password = passDialog.password();
|
||||
}
|
||||
else
|
||||
{
|
||||
tryagain = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case AuthSession::PlayableOffline:
|
||||
{
|
||||
// we ask the user for a player name
|
||||
bool ok = false;
|
||||
QString usedname = m_session->player_name;
|
||||
QString name = QInputDialog::getText(m_parentWidget, tr("Player name"),
|
||||
tr("Choose your offline mode player name."),
|
||||
QLineEdit::Normal, m_session->player_name, &ok);
|
||||
if (!ok)
|
||||
{
|
||||
tryagain = false;
|
||||
break;
|
||||
}
|
||||
if (name.length())
|
||||
{
|
||||
usedname = name;
|
||||
}
|
||||
m_session->MakeOffline(usedname);
|
||||
// offline flavored game from here :3
|
||||
}
|
||||
case AuthSession::PlayableOnline:
|
||||
{
|
||||
launchInstance();
|
||||
tryagain = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
emitFailed(tr("Failed to launch."));
|
||||
}
|
||||
|
||||
void LaunchController::launchInstance()
|
||||
{
|
||||
Q_ASSERT_X(m_instance != NULL, "launchInstance", "instance is NULL");
|
||||
Q_ASSERT_X(m_session.get() != nullptr, "launchInstance", "session is NULL");
|
||||
|
||||
if(!m_instance->reloadSettings())
|
||||
{
|
||||
QMessageBox::critical(m_parentWidget, tr("Error!"), tr("Couldn't load the instance profile."));
|
||||
emitFailed(tr("Couldn't load the instance profile."));
|
||||
return;
|
||||
}
|
||||
|
||||
m_launcher = m_instance->createLaunchTask(m_session, m_serverToJoin);
|
||||
if (!m_launcher)
|
||||
{
|
||||
emitFailed(tr("Couldn't instantiate a launcher."));
|
||||
return;
|
||||
}
|
||||
|
||||
auto console = qobject_cast<InstanceWindow *>(m_parentWidget);
|
||||
auto showConsole = m_instance->settings()->get("ShowConsole").toBool();
|
||||
if(!console && showConsole)
|
||||
{
|
||||
MMC->showInstanceWindow(m_instance);
|
||||
}
|
||||
connect(m_launcher.get(), &LaunchTask::readyForLaunch, this, &LaunchController::readyForLaunch);
|
||||
connect(m_launcher.get(), &LaunchTask::succeeded, this, &LaunchController::onSucceeded);
|
||||
connect(m_launcher.get(), &LaunchTask::failed, this, &LaunchController::onFailed);
|
||||
connect(m_launcher.get(), &LaunchTask::requestProgress, this, &LaunchController::onProgressRequested);
|
||||
|
||||
// Prepend Online and Auth Status
|
||||
QString online_mode;
|
||||
if(m_session->wants_online) {
|
||||
online_mode = "online";
|
||||
|
||||
// Prepend Server Status
|
||||
QStringList servers = {"authserver.mojang.com", "session.minecraft.net", "textures.minecraft.net", "api.mojang.com"};
|
||||
QString resolved_servers = "";
|
||||
QHostInfo host_info;
|
||||
|
||||
for(QString server : servers) {
|
||||
host_info = QHostInfo::fromName(server);
|
||||
resolved_servers = resolved_servers + server + " resolves to:\n [";
|
||||
if(!host_info.addresses().isEmpty()) {
|
||||
for(QHostAddress address : host_info.addresses()) {
|
||||
resolved_servers = resolved_servers + address.toString();
|
||||
if(!host_info.addresses().endsWith(address)) {
|
||||
resolved_servers = resolved_servers + ", ";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
resolved_servers = resolved_servers + "N/A";
|
||||
}
|
||||
resolved_servers = resolved_servers + "]\n\n";
|
||||
}
|
||||
m_launcher->prependStep(new TextPrint(m_launcher.get(), resolved_servers, MessageLevel::MultiMC));
|
||||
} else {
|
||||
online_mode = "offline";
|
||||
}
|
||||
|
||||
QString auth_server_status;
|
||||
if(m_session->auth_server_online) {
|
||||
auth_server_status = "online";
|
||||
} else {
|
||||
auth_server_status = "offline";
|
||||
}
|
||||
|
||||
m_launcher->prependStep(new TextPrint(m_launcher.get(), "Launched instance in " + online_mode + " mode\nAuthentication server is " + auth_server_status + "\n", MessageLevel::MultiMC));
|
||||
|
||||
// Prepend Version
|
||||
m_launcher->prependStep(new TextPrint(m_launcher.get(), "MultiMC version: " + BuildConfig.printableVersionString() + "\n\n", MessageLevel::MultiMC));
|
||||
m_launcher->start();
|
||||
}
|
||||
|
||||
void LaunchController::readyForLaunch()
|
||||
{
|
||||
if (!m_profiler)
|
||||
{
|
||||
m_launcher->proceed();
|
||||
return;
|
||||
}
|
||||
|
||||
QString error;
|
||||
if (!m_profiler->check(&error))
|
||||
{
|
||||
m_launcher->abort();
|
||||
QMessageBox::critical(m_parentWidget, tr("Error!"), tr("Couldn't start profiler: %1").arg(error));
|
||||
emitFailed("Profiler startup failed!");
|
||||
return;
|
||||
}
|
||||
BaseProfiler *profilerInstance = m_profiler->createProfiler(m_launcher->instance(), this);
|
||||
|
||||
connect(profilerInstance, &BaseProfiler::readyToLaunch, [this](const QString & message)
|
||||
{
|
||||
QMessageBox msg;
|
||||
msg.setText(tr("The game launch is delayed until you press the "
|
||||
"button. This is the right time to setup the profiler, as the "
|
||||
"profiler server is running now.\n\n%1").arg(message));
|
||||
msg.setWindowTitle(tr("Waiting."));
|
||||
msg.setIcon(QMessageBox::Information);
|
||||
msg.addButton(tr("Launch"), QMessageBox::AcceptRole);
|
||||
msg.setModal(true);
|
||||
msg.exec();
|
||||
m_launcher->proceed();
|
||||
});
|
||||
connect(profilerInstance, &BaseProfiler::abortLaunch, [this](const QString & message)
|
||||
{
|
||||
QMessageBox msg;
|
||||
msg.setText(tr("Couldn't start the profiler: %1").arg(message));
|
||||
msg.setWindowTitle(tr("Error"));
|
||||
msg.setIcon(QMessageBox::Critical);
|
||||
msg.addButton(QMessageBox::Ok);
|
||||
msg.setModal(true);
|
||||
msg.exec();
|
||||
m_launcher->abort();
|
||||
emitFailed("Profiler startup failed!");
|
||||
});
|
||||
profilerInstance->beginProfiling(m_launcher);
|
||||
}
|
||||
|
||||
void LaunchController::onSucceeded()
|
||||
{
|
||||
emitSucceeded();
|
||||
}
|
||||
|
||||
void LaunchController::onFailed(QString reason)
|
||||
{
|
||||
if(m_instance->settings()->get("ShowConsoleOnError").toBool())
|
||||
{
|
||||
MMC->showInstanceWindow(m_instance, "console");
|
||||
}
|
||||
emitFailed(reason);
|
||||
}
|
||||
|
||||
void LaunchController::onProgressRequested(Task* task)
|
||||
{
|
||||
ProgressDialog progDialog(m_parentWidget);
|
||||
progDialog.setSkipButton(true, tr("Abort"));
|
||||
m_launcher->proceed();
|
||||
progDialog.execWithTask(task);
|
||||
}
|
||||
|
||||
bool LaunchController::abort()
|
||||
{
|
||||
if(!m_launcher)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if(!m_launcher->canAbort())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
auto response = CustomMessageBox::selectable(
|
||||
m_parentWidget, tr("Kill Minecraft?"),
|
||||
tr("This can cause the instance to get corrupted and should only be used if Minecraft "
|
||||
"is frozen for some reason"),
|
||||
QMessageBox::Question, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes)->exec();
|
||||
if (response == QMessageBox::Yes)
|
||||
{
|
||||
return m_launcher->abort();
|
||||
}
|
||||
return false;
|
||||
}
|
68
launcher/LaunchController.h
Normal file
68
launcher/LaunchController.h
Normal file
@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
#include <QObject>
|
||||
#include <BaseInstance.h>
|
||||
#include <tools/BaseProfiler.h>
|
||||
|
||||
#include "minecraft/launch/MinecraftServerTarget.h"
|
||||
|
||||
class InstanceWindow;
|
||||
class LaunchController: public Task
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
void executeTask() override;
|
||||
|
||||
LaunchController(QObject * parent = nullptr);
|
||||
virtual ~LaunchController(){};
|
||||
|
||||
void setInstance(InstancePtr instance)
|
||||
{
|
||||
m_instance = instance;
|
||||
}
|
||||
InstancePtr instance()
|
||||
{
|
||||
return m_instance;
|
||||
}
|
||||
void setOnline(bool online)
|
||||
{
|
||||
m_online = online;
|
||||
}
|
||||
void setProfiler(BaseProfilerFactory *profiler)
|
||||
{
|
||||
m_profiler = profiler;
|
||||
}
|
||||
void setParentWidget(QWidget * widget)
|
||||
{
|
||||
m_parentWidget = widget;
|
||||
}
|
||||
void setServerToJoin(MinecraftServerTargetPtr serverToJoin)
|
||||
{
|
||||
m_serverToJoin = std::move(serverToJoin);
|
||||
}
|
||||
QString id()
|
||||
{
|
||||
return m_instance->id();
|
||||
}
|
||||
bool abort() override;
|
||||
|
||||
private:
|
||||
void login();
|
||||
void launchInstance();
|
||||
|
||||
private slots:
|
||||
void readyForLaunch();
|
||||
|
||||
void onSucceeded();
|
||||
void onFailed(QString reason);
|
||||
void onProgressRequested(Task *task);
|
||||
|
||||
private:
|
||||
BaseProfilerFactory *m_profiler = nullptr;
|
||||
bool m_online = true;
|
||||
InstancePtr m_instance;
|
||||
QWidget * m_parentWidget = nullptr;
|
||||
InstanceWindow *m_console = nullptr;
|
||||
AuthSessionPtr m_session;
|
||||
shared_qobject_ptr<LaunchTask> m_launcher;
|
||||
MinecraftServerTargetPtr m_serverToJoin;
|
||||
};
|
176
launcher/LoggedProcess.cpp
Normal file
176
launcher/LoggedProcess.cpp
Normal file
@ -0,0 +1,176 @@
|
||||
#include "LoggedProcess.h"
|
||||
#include "MessageLevel.h"
|
||||
#include <QDebug>
|
||||
|
||||
LoggedProcess::LoggedProcess(QObject *parent) : QProcess(parent)
|
||||
{
|
||||
// QProcess has a strange interface... let's map a lot of those into a few.
|
||||
connect(this, &QProcess::readyReadStandardOutput, this, &LoggedProcess::on_stdOut);
|
||||
connect(this, &QProcess::readyReadStandardError, this, &LoggedProcess::on_stdErr);
|
||||
connect(this, SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(on_exit(int,QProcess::ExitStatus)));
|
||||
connect(this, SIGNAL(error(QProcess::ProcessError)), this, SLOT(on_error(QProcess::ProcessError)));
|
||||
connect(this, &QProcess::stateChanged, this, &LoggedProcess::on_stateChange);
|
||||
}
|
||||
|
||||
LoggedProcess::~LoggedProcess()
|
||||
{
|
||||
if(m_is_detachable)
|
||||
{
|
||||
setProcessState(QProcess::NotRunning);
|
||||
}
|
||||
}
|
||||
|
||||
QStringList reprocess(const QByteArray & data, QString & leftover)
|
||||
{
|
||||
QString str = leftover + QString::fromLocal8Bit(data);
|
||||
|
||||
str.remove('\r');
|
||||
QStringList lines = str.split("\n");
|
||||
leftover = lines.takeLast();
|
||||
return lines;
|
||||
}
|
||||
|
||||
void LoggedProcess::on_stdErr()
|
||||
{
|
||||
auto lines = reprocess(readAllStandardError(), m_err_leftover);
|
||||
emit log(lines, MessageLevel::StdErr);
|
||||
}
|
||||
|
||||
void LoggedProcess::on_stdOut()
|
||||
{
|
||||
auto lines = reprocess(readAllStandardOutput(), m_out_leftover);
|
||||
emit log(lines, MessageLevel::StdOut);
|
||||
}
|
||||
|
||||
void LoggedProcess::on_exit(int exit_code, QProcess::ExitStatus status)
|
||||
{
|
||||
// save the exit code
|
||||
m_exit_code = exit_code;
|
||||
|
||||
// Flush console window
|
||||
if (!m_err_leftover.isEmpty())
|
||||
{
|
||||
emit log({m_err_leftover}, MessageLevel::StdErr);
|
||||
m_err_leftover.clear();
|
||||
}
|
||||
if (!m_out_leftover.isEmpty())
|
||||
{
|
||||
emit log({m_err_leftover}, MessageLevel::StdOut);
|
||||
m_out_leftover.clear();
|
||||
}
|
||||
|
||||
// based on state, send signals
|
||||
if (!m_is_aborting)
|
||||
{
|
||||
if (status == QProcess::NormalExit)
|
||||
{
|
||||
//: Message displayed on instance exit
|
||||
emit log({tr("Process exited with code %1.").arg(exit_code)}, MessageLevel::MultiMC);
|
||||
changeState(LoggedProcess::Finished);
|
||||
}
|
||||
else
|
||||
{
|
||||
//: Message displayed on instance crashed
|
||||
if(exit_code == -1)
|
||||
emit log({tr("Process crashed.")}, MessageLevel::MultiMC);
|
||||
else
|
||||
emit log({tr("Process crashed with exitcode %1.").arg(exit_code)}, MessageLevel::MultiMC);
|
||||
changeState(LoggedProcess::Crashed);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//: Message displayed after the instance exits due to kill request
|
||||
emit log({tr("Process was killed by user.")}, MessageLevel::Error);
|
||||
changeState(LoggedProcess::Aborted);
|
||||
}
|
||||
}
|
||||
|
||||
void LoggedProcess::on_error(QProcess::ProcessError error)
|
||||
{
|
||||
switch(error)
|
||||
{
|
||||
case QProcess::FailedToStart:
|
||||
{
|
||||
emit log({tr("The process failed to start.")}, MessageLevel::Fatal);
|
||||
changeState(LoggedProcess::FailedToStart);
|
||||
break;
|
||||
}
|
||||
// we'll just ignore those... never needed them
|
||||
case QProcess::Crashed:
|
||||
case QProcess::ReadError:
|
||||
case QProcess::Timedout:
|
||||
case QProcess::UnknownError:
|
||||
case QProcess::WriteError:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void LoggedProcess::kill()
|
||||
{
|
||||
m_is_aborting = true;
|
||||
QProcess::kill();
|
||||
}
|
||||
|
||||
int LoggedProcess::exitCode() const
|
||||
{
|
||||
return m_exit_code;
|
||||
}
|
||||
|
||||
void LoggedProcess::changeState(LoggedProcess::State state)
|
||||
{
|
||||
if(state == m_state)
|
||||
return;
|
||||
m_state = state;
|
||||
emit stateChanged(m_state);
|
||||
}
|
||||
|
||||
LoggedProcess::State LoggedProcess::state() const
|
||||
{
|
||||
return m_state;
|
||||
}
|
||||
|
||||
void LoggedProcess::on_stateChange(QProcess::ProcessState state)
|
||||
{
|
||||
switch(state)
|
||||
{
|
||||
case QProcess::NotRunning:
|
||||
break; // let's not - there are too many that handle this already.
|
||||
case QProcess::Starting:
|
||||
{
|
||||
if(m_state != LoggedProcess::NotRunning)
|
||||
{
|
||||
qWarning() << "Wrong state change for process from state" << m_state << "to" << (int) LoggedProcess::Starting;
|
||||
}
|
||||
changeState(LoggedProcess::Starting);
|
||||
return;
|
||||
}
|
||||
case QProcess::Running:
|
||||
{
|
||||
if(m_state != LoggedProcess::Starting)
|
||||
{
|
||||
qWarning() << "Wrong state change for process from state" << m_state << "to" << (int) LoggedProcess::Running;
|
||||
}
|
||||
changeState(LoggedProcess::Running);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if defined Q_OS_WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
qint64 LoggedProcess::processId() const
|
||||
{
|
||||
#ifdef Q_OS_WIN
|
||||
return pid() ? pid()->dwProcessId : 0;
|
||||
#else
|
||||
return pid();
|
||||
#endif
|
||||
}
|
||||
|
||||
void LoggedProcess::setDetachable(bool detachable)
|
||||
{
|
||||
m_is_detachable = detachable;
|
||||
}
|
79
launcher/LoggedProcess.h
Normal file
79
launcher/LoggedProcess.h
Normal file
@ -0,0 +1,79 @@
|
||||
/* Copyright 2013-2021 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 <QProcess>
|
||||
#include "MessageLevel.h"
|
||||
|
||||
/*
|
||||
* This is a basic process.
|
||||
* It has line-based logging support and hides some of the nasty bits.
|
||||
*/
|
||||
class LoggedProcess : public QProcess
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum State
|
||||
{
|
||||
NotRunning,
|
||||
Starting,
|
||||
FailedToStart,
|
||||
Running,
|
||||
Finished,
|
||||
Crashed,
|
||||
Aborted
|
||||
};
|
||||
|
||||
public:
|
||||
explicit LoggedProcess(QObject* parent = 0);
|
||||
virtual ~LoggedProcess();
|
||||
|
||||
State state() const;
|
||||
int exitCode() const;
|
||||
qint64 processId() const;
|
||||
|
||||
void setDetachable(bool detachable);
|
||||
|
||||
signals:
|
||||
void log(QStringList lines, MessageLevel::Enum level);
|
||||
void stateChanged(LoggedProcess::State state);
|
||||
|
||||
public slots:
|
||||
/**
|
||||
* @brief kill the process - equivalent to kill -9
|
||||
*/
|
||||
void kill();
|
||||
|
||||
|
||||
private slots:
|
||||
void on_stdErr();
|
||||
void on_stdOut();
|
||||
void on_exit(int exit_code, QProcess::ExitStatus status);
|
||||
void on_error(QProcess::ProcessError error);
|
||||
void on_stateChange(QProcess::ProcessState);
|
||||
|
||||
private:
|
||||
void changeState(LoggedProcess::State state);
|
||||
|
||||
private:
|
||||
QString m_err_leftover;
|
||||
QString m_out_leftover;
|
||||
bool m_killed = false;
|
||||
State m_state = NotRunning;
|
||||
int m_exit_code = 0;
|
||||
bool m_is_aborting = false;
|
||||
bool m_is_detachable = false;
|
||||
};
|
76
launcher/MMCStrings.cpp
Normal file
76
launcher/MMCStrings.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
#include "MMCStrings.h"
|
||||
|
||||
/// TAKEN FROM Qt, because it doesn't expose it intelligently
|
||||
static inline QChar getNextChar(const QString &s, int location)
|
||||
{
|
||||
return (location < s.length()) ? s.at(location) : QChar();
|
||||
}
|
||||
|
||||
/// TAKEN FROM Qt, because it doesn't expose it intelligently
|
||||
int Strings::naturalCompare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs)
|
||||
{
|
||||
for (int l1 = 0, l2 = 0; l1 <= s1.count() && l2 <= s2.count(); ++l1, ++l2)
|
||||
{
|
||||
// skip spaces, tabs and 0's
|
||||
QChar c1 = getNextChar(s1, l1);
|
||||
while (c1.isSpace())
|
||||
c1 = getNextChar(s1, ++l1);
|
||||
QChar c2 = getNextChar(s2, l2);
|
||||
while (c2.isSpace())
|
||||
c2 = getNextChar(s2, ++l2);
|
||||
|
||||
if (c1.isDigit() && c2.isDigit())
|
||||
{
|
||||
while (c1.digitValue() == 0)
|
||||
c1 = getNextChar(s1, ++l1);
|
||||
while (c2.digitValue() == 0)
|
||||
c2 = getNextChar(s2, ++l2);
|
||||
|
||||
int lookAheadLocation1 = l1;
|
||||
int lookAheadLocation2 = l2;
|
||||
int currentReturnValue = 0;
|
||||
// find the last digit, setting currentReturnValue as we go if it isn't equal
|
||||
for (QChar lookAhead1 = c1, lookAhead2 = c2;
|
||||
(lookAheadLocation1 <= s1.length() && lookAheadLocation2 <= s2.length());
|
||||
lookAhead1 = getNextChar(s1, ++lookAheadLocation1),
|
||||
lookAhead2 = getNextChar(s2, ++lookAheadLocation2))
|
||||
{
|
||||
bool is1ADigit = !lookAhead1.isNull() && lookAhead1.isDigit();
|
||||
bool is2ADigit = !lookAhead2.isNull() && lookAhead2.isDigit();
|
||||
if (!is1ADigit && !is2ADigit)
|
||||
break;
|
||||
if (!is1ADigit)
|
||||
return -1;
|
||||
if (!is2ADigit)
|
||||
return 1;
|
||||
if (currentReturnValue == 0)
|
||||
{
|
||||
if (lookAhead1 < lookAhead2)
|
||||
{
|
||||
currentReturnValue = -1;
|
||||
}
|
||||
else if (lookAhead1 > lookAhead2)
|
||||
{
|
||||
currentReturnValue = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (currentReturnValue != 0)
|
||||
return currentReturnValue;
|
||||
}
|
||||
if (cs == Qt::CaseInsensitive)
|
||||
{
|
||||
if (!c1.isLower())
|
||||
c1 = c1.toLower();
|
||||
if (!c2.isLower())
|
||||
c2 = c2.toLower();
|
||||
}
|
||||
int r = QString::localeAwareCompare(c1, c2);
|
||||
if (r < 0)
|
||||
return -1;
|
||||
if (r > 0)
|
||||
return 1;
|
||||
}
|
||||
// The two strings are the same (02 == 2) so fall back to the normal sort
|
||||
return QString::compare(s1, s2, cs);
|
||||
}
|
8
launcher/MMCStrings.h
Normal file
8
launcher/MMCStrings.h
Normal file
@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
|
||||
namespace Strings
|
||||
{
|
||||
int naturalCompare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs);
|
||||
}
|
312
launcher/MMCZip.cpp
Normal file
312
launcher/MMCZip.cpp
Normal file
@ -0,0 +1,312 @@
|
||||
/* Copyright 2013-2021 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 <quazip.h>
|
||||
#include <quazipdir.h>
|
||||
#include <quazipfile.h>
|
||||
#include <JlCompress.h>
|
||||
#include "MMCZip.h"
|
||||
#include "FileSystem.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
// ours
|
||||
bool MMCZip::mergeZipFiles(QuaZip *into, QFileInfo from, QSet<QString> &contained, const JlCompress::FilterFunction filter)
|
||||
{
|
||||
QuaZip modZip(from.filePath());
|
||||
modZip.open(QuaZip::mdUnzip);
|
||||
|
||||
QuaZipFile fileInsideMod(&modZip);
|
||||
QuaZipFile zipOutFile(into);
|
||||
for (bool more = modZip.goToFirstFile(); more; more = modZip.goToNextFile())
|
||||
{
|
||||
QString filename = modZip.getCurrentFileName();
|
||||
if (filter && !filter(filename))
|
||||
{
|
||||
qDebug() << "Skipping file " << filename << " from "
|
||||
<< from.fileName() << " - filtered";
|
||||
continue;
|
||||
}
|
||||
if (contained.contains(filename))
|
||||
{
|
||||
qDebug() << "Skipping already contained file " << filename << " from "
|
||||
<< from.fileName();
|
||||
continue;
|
||||
}
|
||||
contained.insert(filename);
|
||||
|
||||
if (!fileInsideMod.open(QIODevice::ReadOnly))
|
||||
{
|
||||
qCritical() << "Failed to open " << filename << " from " << from.fileName();
|
||||
return false;
|
||||
}
|
||||
|
||||
QuaZipNewInfo info_out(fileInsideMod.getActualFileName());
|
||||
|
||||
if (!zipOutFile.open(QIODevice::WriteOnly, info_out))
|
||||
{
|
||||
qCritical() << "Failed to open " << filename << " in the jar";
|
||||
fileInsideMod.close();
|
||||
return false;
|
||||
}
|
||||
if (!JlCompress::copyData(fileInsideMod, zipOutFile))
|
||||
{
|
||||
zipOutFile.close();
|
||||
fileInsideMod.close();
|
||||
qCritical() << "Failed to copy data of " << filename << " into the jar";
|
||||
return false;
|
||||
}
|
||||
zipOutFile.close();
|
||||
fileInsideMod.close();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// ours
|
||||
bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const QList<Mod>& mods)
|
||||
{
|
||||
QuaZip zipOut(targetJarPath);
|
||||
if (!zipOut.open(QuaZip::mdCreate))
|
||||
{
|
||||
QFile::remove(targetJarPath);
|
||||
qCritical() << "Failed to open the minecraft.jar for modding";
|
||||
return false;
|
||||
}
|
||||
// Files already added to the jar.
|
||||
// These files will be skipped.
|
||||
QSet<QString> addedFiles;
|
||||
|
||||
// Modify the jar
|
||||
QListIterator<Mod> i(mods);
|
||||
i.toBack();
|
||||
while (i.hasPrevious())
|
||||
{
|
||||
const Mod &mod = i.previous();
|
||||
// do not merge disabled mods.
|
||||
if (!mod.enabled())
|
||||
continue;
|
||||
if (mod.type() == Mod::MOD_ZIPFILE)
|
||||
{
|
||||
if (!mergeZipFiles(&zipOut, mod.filename(), addedFiles))
|
||||
{
|
||||
zipOut.close();
|
||||
QFile::remove(targetJarPath);
|
||||
qCritical() << "Failed to add" << mod.filename().fileName() << "to the jar.";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (mod.type() == Mod::MOD_SINGLEFILE)
|
||||
{
|
||||
// FIXME: buggy - does not work with addedFiles
|
||||
auto filename = mod.filename();
|
||||
if (!JlCompress::compressFile(&zipOut, filename.absoluteFilePath(), filename.fileName()))
|
||||
{
|
||||
zipOut.close();
|
||||
QFile::remove(targetJarPath);
|
||||
qCritical() << "Failed to add" << mod.filename().fileName() << "to the jar.";
|
||||
return false;
|
||||
}
|
||||
addedFiles.insert(filename.fileName());
|
||||
}
|
||||
else if (mod.type() == Mod::MOD_FOLDER)
|
||||
{
|
||||
// FIXME: buggy - does not work with addedFiles
|
||||
auto filename = mod.filename();
|
||||
QString what_to_zip = filename.absoluteFilePath();
|
||||
QDir dir(what_to_zip);
|
||||
dir.cdUp();
|
||||
QString parent_dir = dir.absolutePath();
|
||||
if (!JlCompress::compressSubDir(&zipOut, what_to_zip, parent_dir, addedFiles))
|
||||
{
|
||||
zipOut.close();
|
||||
QFile::remove(targetJarPath);
|
||||
qCritical() << "Failed to add" << mod.filename().fileName() << "to the jar.";
|
||||
return false;
|
||||
}
|
||||
qDebug() << "Adding folder " << filename.fileName() << " from "
|
||||
<< filename.absoluteFilePath();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Make sure we do not continue launching when something is missing or undefined...
|
||||
zipOut.close();
|
||||
QFile::remove(targetJarPath);
|
||||
qCritical() << "Failed to add unknown mod type" << mod.filename().fileName() << "to the jar.";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!mergeZipFiles(&zipOut, QFileInfo(sourceJarPath), addedFiles, [](const QString key){return !key.contains("META-INF");}))
|
||||
{
|
||||
zipOut.close();
|
||||
QFile::remove(targetJarPath);
|
||||
qCritical() << "Failed to insert minecraft.jar contents.";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Recompress the jar
|
||||
zipOut.close();
|
||||
if (zipOut.getZipError() != 0)
|
||||
{
|
||||
QFile::remove(targetJarPath);
|
||||
qCritical() << "Failed to finalize minecraft.jar!";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// ours
|
||||
QString MMCZip::findFolderOfFileInZip(QuaZip * zip, const QString & what, const QString &root)
|
||||
{
|
||||
QuaZipDir rootDir(zip, root);
|
||||
for(auto fileName: rootDir.entryList(QDir::Files))
|
||||
{
|
||||
if(fileName == what)
|
||||
return root;
|
||||
}
|
||||
for(auto fileName: rootDir.entryList(QDir::Dirs))
|
||||
{
|
||||
QString result = findFolderOfFileInZip(zip, what, root + fileName);
|
||||
if(!result.isEmpty())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
// ours
|
||||
bool MMCZip::findFilesInZip(QuaZip * zip, const QString & what, QStringList & result, const QString &root)
|
||||
{
|
||||
QuaZipDir rootDir(zip, root);
|
||||
for(auto fileName: rootDir.entryList(QDir::Files))
|
||||
{
|
||||
if(fileName == what)
|
||||
{
|
||||
result.append(root);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for(auto fileName: rootDir.entryList(QDir::Dirs))
|
||||
{
|
||||
findFilesInZip(zip, what, result, root + fileName);
|
||||
}
|
||||
return !result.isEmpty();
|
||||
}
|
||||
|
||||
|
||||
// ours
|
||||
nonstd::optional<QStringList> MMCZip::extractSubDir(QuaZip *zip, const QString & subdir, const QString &target)
|
||||
{
|
||||
QDir directory(target);
|
||||
QStringList extracted;
|
||||
|
||||
qDebug() << "Extracting subdir" << subdir << "from" << zip->getZipName() << "to" << target;
|
||||
auto numEntries = zip->getEntriesCount();
|
||||
if(numEntries < 0) {
|
||||
qWarning() << "Failed to enumerate files in archive";
|
||||
return nonstd::nullopt;
|
||||
}
|
||||
else if(numEntries == 0) {
|
||||
qDebug() << "Extracting empty archives seems odd...";
|
||||
return extracted;
|
||||
}
|
||||
else if (!zip->goToFirstFile())
|
||||
{
|
||||
qWarning() << "Failed to seek to first file in zip";
|
||||
return nonstd::nullopt;
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
QString name = zip->getCurrentFileName();
|
||||
if(!name.startsWith(subdir))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
name.remove(0, subdir.size());
|
||||
QString absFilePath = directory.absoluteFilePath(name);
|
||||
if(name.isEmpty())
|
||||
{
|
||||
absFilePath += "/";
|
||||
}
|
||||
if (!JlCompress::extractFile(zip, "", absFilePath))
|
||||
{
|
||||
qWarning() << "Failed to extract file" << name << "to" << absFilePath;
|
||||
JlCompress::removeFile(extracted);
|
||||
return nonstd::nullopt;
|
||||
}
|
||||
extracted.append(absFilePath);
|
||||
qDebug() << "Extracted file" << name;
|
||||
} while (zip->goToNextFile());
|
||||
return extracted;
|
||||
}
|
||||
|
||||
// ours
|
||||
bool MMCZip::extractRelFile(QuaZip *zip, const QString &file, const QString &target)
|
||||
{
|
||||
return JlCompress::extractFile(zip, file, target);
|
||||
}
|
||||
|
||||
// ours
|
||||
nonstd::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString dir)
|
||||
{
|
||||
QuaZip zip(fileCompressed);
|
||||
if (!zip.open(QuaZip::mdUnzip))
|
||||
{
|
||||
// check if this is a minimum size empty zip file...
|
||||
QFileInfo fileInfo(fileCompressed);
|
||||
if(fileInfo.size() == 22) {
|
||||
return QStringList();
|
||||
}
|
||||
qWarning() << "Could not open archive for unzipping:" << fileCompressed << "Error:" << zip.getZipError();;
|
||||
return nonstd::nullopt;
|
||||
}
|
||||
return MMCZip::extractSubDir(&zip, "", dir);
|
||||
}
|
||||
|
||||
// ours
|
||||
nonstd::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString subdir, QString dir)
|
||||
{
|
||||
QuaZip zip(fileCompressed);
|
||||
if (!zip.open(QuaZip::mdUnzip))
|
||||
{
|
||||
// check if this is a minimum size empty zip file...
|
||||
QFileInfo fileInfo(fileCompressed);
|
||||
if(fileInfo.size() == 22) {
|
||||
return QStringList();
|
||||
}
|
||||
qWarning() << "Could not open archive for unzipping:" << fileCompressed << "Error:" << zip.getZipError();;
|
||||
return nonstd::nullopt;
|
||||
}
|
||||
return MMCZip::extractSubDir(&zip, subdir, dir);
|
||||
}
|
||||
|
||||
// ours
|
||||
bool MMCZip::extractFile(QString fileCompressed, QString file, QString target)
|
||||
{
|
||||
QuaZip zip(fileCompressed);
|
||||
if (!zip.open(QuaZip::mdUnzip))
|
||||
{
|
||||
// check if this is a minimum size empty zip file...
|
||||
QFileInfo fileInfo(fileCompressed);
|
||||
if(fileInfo.size() == 22) {
|
||||
return true;
|
||||
}
|
||||
qWarning() << "Could not open archive for unzipping:" << fileCompressed << "Error:" << zip.getZipError();
|
||||
return false;
|
||||
}
|
||||
return MMCZip::extractRelFile(&zip, file, target);
|
||||
}
|
92
launcher/MMCZip.h
Normal file
92
launcher/MMCZip.h
Normal file
@ -0,0 +1,92 @@
|
||||
/* Copyright 2013-2021 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 <QString>
|
||||
#include <QFileInfo>
|
||||
#include <QSet>
|
||||
#include "minecraft/mod/Mod.h"
|
||||
#include <functional>
|
||||
|
||||
#include <JlCompress.h>
|
||||
#include <nonstd/optional>
|
||||
|
||||
namespace MMCZip
|
||||
{
|
||||
|
||||
/**
|
||||
* Merge two zip files, using a filter function
|
||||
*/
|
||||
bool mergeZipFiles(QuaZip *into, QFileInfo from, QSet<QString> &contained,
|
||||
const JlCompress::FilterFunction filter = nullptr);
|
||||
|
||||
/**
|
||||
* take a source jar, add mods to it, resulting in target jar
|
||||
*/
|
||||
bool createModdedJar(QString sourceJarPath, QString targetJarPath, const QList<Mod>& mods);
|
||||
|
||||
/**
|
||||
* Find a single file in archive by file name (not path)
|
||||
*
|
||||
* \return the path prefix where the file is
|
||||
*/
|
||||
QString findFolderOfFileInZip(QuaZip * zip, const QString & what, const QString &root = QString(""));
|
||||
|
||||
/**
|
||||
* Find a multiple files of the same name in archive by file name
|
||||
* If a file is found in a path, no deeper paths are searched
|
||||
*
|
||||
* \return true if anything was found
|
||||
*/
|
||||
bool findFilesInZip(QuaZip * zip, const QString & what, QStringList & result, const QString &root = QString());
|
||||
|
||||
/**
|
||||
* Extract a subdirectory from an archive
|
||||
*/
|
||||
nonstd::optional<QStringList> extractSubDir(QuaZip *zip, const QString & subdir, const QString &target);
|
||||
|
||||
bool extractRelFile(QuaZip *zip, const QString & file, const QString &target);
|
||||
|
||||
/**
|
||||
* Extract a whole archive.
|
||||
*
|
||||
* \param fileCompressed The name of the archive.
|
||||
* \param dir The directory to extract to, the current directory if left empty.
|
||||
* \return The list of the full paths of the files extracted, empty on failure.
|
||||
*/
|
||||
nonstd::optional<QStringList> extractDir(QString fileCompressed, QString dir);
|
||||
|
||||
/**
|
||||
* Extract a subdirectory from an archive
|
||||
*
|
||||
* \param fileCompressed The name of the archive.
|
||||
* \param subdir The directory within the archive to extract
|
||||
* \param dir The directory to extract to, the current directory if left empty.
|
||||
* \return The list of the full paths of the files extracted, empty on failure.
|
||||
*/
|
||||
nonstd::optional<QStringList> extractDir(QString fileCompressed, QString subdir, QString dir);
|
||||
|
||||
/**
|
||||
* Extract a single file from an archive into a directory
|
||||
*
|
||||
* \param fileCompressed The name of the archive.
|
||||
* \param file The file within the archive to extract
|
||||
* \param dir The directory to extract to, the current directory if left empty.
|
||||
* \return true for success or false for failure
|
||||
*/
|
||||
bool extractFile(QString fileCompressed, QString file, QString dir);
|
||||
|
||||
}
|
1952
launcher/MainWindow.cpp
Normal file
1952
launcher/MainWindow.cpp
Normal file
File diff suppressed because it is too large
Load Diff
226
launcher/MainWindow.h
Normal file
226
launcher/MainWindow.h
Normal file
@ -0,0 +1,226 @@
|
||||
/* Copyright 2013-2021 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 <memory>
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QProcess>
|
||||
#include <QTimer>
|
||||
|
||||
#include "BaseInstance.h"
|
||||
#include "minecraft/auth/MojangAccount.h"
|
||||
#include "net/NetJob.h"
|
||||
#include "updater/GoUpdate.h"
|
||||
|
||||
class LaunchController;
|
||||
class NewsChecker;
|
||||
class NotificationChecker;
|
||||
class QToolButton;
|
||||
class InstanceProxyModel;
|
||||
class LabeledToolButton;
|
||||
class QLabel;
|
||||
class MinecraftLauncher;
|
||||
class BaseProfilerFactory;
|
||||
class GroupView;
|
||||
class ServerStatus;
|
||||
class KonamiCode;
|
||||
class InstanceTask;
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
class Ui;
|
||||
|
||||
public:
|
||||
explicit MainWindow(QWidget *parent = 0);
|
||||
~MainWindow();
|
||||
|
||||
bool eventFilter(QObject *obj, QEvent *ev) override;
|
||||
void closeEvent(QCloseEvent *event) override;
|
||||
void changeEvent(QEvent * event) override;
|
||||
|
||||
void checkInstancePathForProblems();
|
||||
|
||||
void updatesAllowedChanged(bool allowed);
|
||||
|
||||
void droppedURLs(QList<QUrl> urls);
|
||||
signals:
|
||||
void isClosing();
|
||||
|
||||
protected:
|
||||
QMenu * createPopupMenu() override;
|
||||
|
||||
private slots:
|
||||
void onCatToggled(bool);
|
||||
|
||||
void on_actionAbout_triggered();
|
||||
|
||||
void on_actionAddInstance_triggered();
|
||||
|
||||
void on_actionREDDIT_triggered();
|
||||
|
||||
void on_actionDISCORD_triggered();
|
||||
|
||||
void on_actionCopyInstance_triggered();
|
||||
|
||||
void on_actionChangeInstGroup_triggered();
|
||||
|
||||
void on_actionChangeInstIcon_triggered();
|
||||
void on_changeIconButton_clicked(bool)
|
||||
{
|
||||
on_actionChangeInstIcon_triggered();
|
||||
}
|
||||
|
||||
void on_actionViewInstanceFolder_triggered();
|
||||
|
||||
void on_actionConfig_Folder_triggered();
|
||||
|
||||
void on_actionViewSelectedInstFolder_triggered();
|
||||
|
||||
void on_actionViewSelectedMCFolder_triggered();
|
||||
|
||||
void refreshInstances();
|
||||
|
||||
void on_actionViewCentralModsFolder_triggered();
|
||||
|
||||
void checkForUpdates();
|
||||
|
||||
void on_actionSettings_triggered();
|
||||
|
||||
void on_actionInstanceSettings_triggered();
|
||||
|
||||
void on_actionManageAccounts_triggered();
|
||||
|
||||
void on_actionReportBug_triggered();
|
||||
|
||||
void on_actionPatreon_triggered();
|
||||
|
||||
void on_actionMoreNews_triggered();
|
||||
|
||||
void newsButtonClicked();
|
||||
|
||||
void on_actionLaunchInstance_triggered();
|
||||
|
||||
void on_actionLaunchInstanceOffline_triggered();
|
||||
|
||||
void on_actionDeleteInstance_triggered();
|
||||
|
||||
void deleteGroup();
|
||||
|
||||
void on_actionExportInstance_triggered();
|
||||
|
||||
void on_actionRenameInstance_triggered();
|
||||
void on_renameButton_clicked(bool)
|
||||
{
|
||||
on_actionRenameInstance_triggered();
|
||||
}
|
||||
|
||||
void on_actionEditInstance_triggered();
|
||||
|
||||
void on_actionEditInstNotes_triggered();
|
||||
|
||||
void on_actionWorlds_triggered();
|
||||
|
||||
void on_actionScreenshots_triggered();
|
||||
|
||||
void taskEnd();
|
||||
|
||||
/**
|
||||
* called when an icon is changed in the icon model.
|
||||
*/
|
||||
void iconUpdated(QString);
|
||||
|
||||
void showInstanceContextMenu(const QPoint &);
|
||||
|
||||
void updateToolsMenu();
|
||||
|
||||
void skinJobFinished();
|
||||
|
||||
void instanceActivated(QModelIndex);
|
||||
|
||||
void instanceChanged(const QModelIndex ¤t, const QModelIndex &previous);
|
||||
|
||||
void instanceSelectRequest(QString id);
|
||||
|
||||
void instanceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
|
||||
|
||||
void selectionBad();
|
||||
|
||||
void startTask(Task *task);
|
||||
|
||||
void updateAvailable(GoUpdate::Status status);
|
||||
|
||||
void updateNotAvailable();
|
||||
|
||||
void notificationsChanged();
|
||||
|
||||
void activeAccountChanged();
|
||||
|
||||
void changeActiveAccount();
|
||||
|
||||
void repopulateAccountsMenu();
|
||||
|
||||
void updateNewsLabel();
|
||||
|
||||
/*!
|
||||
* Runs the DownloadTask and installs updates.
|
||||
*/
|
||||
void downloadUpdates(GoUpdate::Status status);
|
||||
|
||||
void konamiTriggered();
|
||||
|
||||
void globalSettingsClosed();
|
||||
|
||||
private:
|
||||
void retranslateUi();
|
||||
|
||||
void addInstance(QString url = QString());
|
||||
void activateInstance(InstancePtr instance);
|
||||
void setCatBackground(bool enabled);
|
||||
void updateInstanceToolIcon(QString new_icon);
|
||||
void setSelectedInstanceById(const QString &id);
|
||||
void updateStatusCenter();
|
||||
|
||||
void runModalTask(Task *task);
|
||||
void instanceFromInstanceTask(InstanceTask *task);
|
||||
void finalizeInstance(InstancePtr inst);
|
||||
|
||||
private:
|
||||
std::unique_ptr<Ui> ui;
|
||||
|
||||
// these are managed by Qt's memory management model!
|
||||
GroupView *view = nullptr;
|
||||
InstanceProxyModel *proxymodel = nullptr;
|
||||
QToolButton *newsLabel = nullptr;
|
||||
QLabel *m_statusLeft = nullptr;
|
||||
QLabel *m_statusCenter = nullptr;
|
||||
ServerStatus *m_statusRight = nullptr;
|
||||
QMenu *accountMenu = nullptr;
|
||||
QToolButton *accountMenuButton = nullptr;
|
||||
KonamiCode * secretEventFilter = nullptr;
|
||||
|
||||
unique_qobject_ptr<NetJob> skin_download_job;
|
||||
unique_qobject_ptr<NewsChecker> m_newsChecker;
|
||||
unique_qobject_ptr<NotificationChecker> m_notificationChecker;
|
||||
|
||||
InstancePtr m_selectedInstance;
|
||||
QString m_currentInstIcon;
|
||||
|
||||
// managed by the application object
|
||||
Task *m_versionLoadTask = nullptr;
|
||||
};
|
36
launcher/MessageLevel.cpp
Normal file
36
launcher/MessageLevel.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
#include "MessageLevel.h"
|
||||
|
||||
MessageLevel::Enum MessageLevel::getLevel(const QString& levelName)
|
||||
{
|
||||
if (levelName == "MultiMC")
|
||||
return MessageLevel::MultiMC;
|
||||
else if (levelName == "Debug")
|
||||
return MessageLevel::Debug;
|
||||
else if (levelName == "Info")
|
||||
return MessageLevel::Info;
|
||||
else if (levelName == "Message")
|
||||
return MessageLevel::Message;
|
||||
else if (levelName == "Warning")
|
||||
return MessageLevel::Warning;
|
||||
else if (levelName == "Error")
|
||||
return MessageLevel::Error;
|
||||
else if (levelName == "Fatal")
|
||||
return MessageLevel::Fatal;
|
||||
// Skip PrePost, it's not exposed to !![]!
|
||||
// Also skip StdErr and StdOut
|
||||
else
|
||||
return MessageLevel::Unknown;
|
||||
}
|
||||
|
||||
MessageLevel::Enum MessageLevel::fromLine(QString &line)
|
||||
{
|
||||
// Level prefix
|
||||
int endmark = line.indexOf("]!");
|
||||
if (line.startsWith("!![") && endmark != -1)
|
||||
{
|
||||
auto level = MessageLevel::getLevel(line.left(endmark).mid(3));
|
||||
line = line.mid(endmark + 2);
|
||||
return level;
|
||||
}
|
||||
return MessageLevel::Unknown;
|
||||
}
|
28
launcher/MessageLevel.h
Normal file
28
launcher/MessageLevel.h
Normal file
@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
|
||||
/**
|
||||
* @brief the MessageLevel Enum
|
||||
* defines what level a log message is
|
||||
*/
|
||||
namespace MessageLevel
|
||||
{
|
||||
enum Enum
|
||||
{
|
||||
Unknown, /**< No idea what this is or where it came from */
|
||||
StdOut, /**< Undetermined stderr messages */
|
||||
StdErr, /**< Undetermined stdout messages */
|
||||
MultiMC, /**< MultiMC Messages */
|
||||
Debug, /**< Debug Messages */
|
||||
Info, /**< Info Messages */
|
||||
Message, /**< Standard Messages */
|
||||
Warning, /**< Warnings */
|
||||
Error, /**< Errors */
|
||||
Fatal, /**< Fatal Errors */
|
||||
};
|
||||
MessageLevel::Enum getLevel(const QString &levelName);
|
||||
|
||||
/* Get message level from a line. Line is modified if it was successful. */
|
||||
MessageLevel::Enum fromLine(QString &line);
|
||||
}
|
1448
launcher/MultiMC.cpp
Normal file
1448
launcher/MultiMC.cpp
Normal file
File diff suppressed because it is too large
Load Diff
235
launcher/MultiMC.h
Normal file
235
launcher/MultiMC.h
Normal file
@ -0,0 +1,235 @@
|
||||
#pragma once
|
||||
|
||||
#include <QApplication>
|
||||
#include <memory>
|
||||
#include <QDebug>
|
||||
#include <QFlag>
|
||||
#include <QIcon>
|
||||
#include <QDateTime>
|
||||
#include <QUrl>
|
||||
#include <updater/GoUpdate.h>
|
||||
|
||||
#include <BaseInstance.h>
|
||||
|
||||
#include "minecraft/launch/MinecraftServerTarget.h"
|
||||
|
||||
class LaunchController;
|
||||
class LocalPeer;
|
||||
class InstanceWindow;
|
||||
class MainWindow;
|
||||
class SetupWizard;
|
||||
class FolderInstanceProvider;
|
||||
class GenericPageProvider;
|
||||
class QFile;
|
||||
class HttpMetaCache;
|
||||
class SettingsObject;
|
||||
class InstanceList;
|
||||
class MojangAccountList;
|
||||
class IconList;
|
||||
class QNetworkAccessManager;
|
||||
class JavaInstallList;
|
||||
class UpdateChecker;
|
||||
class BaseProfilerFactory;
|
||||
class BaseDetachedToolFactory;
|
||||
class TranslationsModel;
|
||||
class ITheme;
|
||||
class MCEditTool;
|
||||
class GAnalytics;
|
||||
|
||||
#if defined(MMC)
|
||||
#undef MMC
|
||||
#endif
|
||||
#define MMC (static_cast<MultiMC *>(QCoreApplication::instance()))
|
||||
|
||||
class MultiMC : public QApplication
|
||||
{
|
||||
// friends for the purpose of limiting access to deprecated stuff
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum Status
|
||||
{
|
||||
StartingUp,
|
||||
Failed,
|
||||
Succeeded,
|
||||
Initialized
|
||||
};
|
||||
|
||||
public:
|
||||
MultiMC(int &argc, char **argv);
|
||||
virtual ~MultiMC();
|
||||
|
||||
GAnalytics *analytics() const
|
||||
{
|
||||
return m_analytics;
|
||||
}
|
||||
|
||||
std::shared_ptr<SettingsObject> settings() const
|
||||
{
|
||||
return m_settings;
|
||||
}
|
||||
|
||||
qint64 timeSinceStart() const
|
||||
{
|
||||
return startTime.msecsTo(QDateTime::currentDateTime());
|
||||
}
|
||||
|
||||
QIcon getThemedIcon(const QString& name);
|
||||
|
||||
void setIconTheme(const QString& name);
|
||||
|
||||
std::vector<ITheme *> getValidApplicationThemes();
|
||||
|
||||
void setApplicationTheme(const QString& name, bool initial);
|
||||
|
||||
// DownloadUpdateTask
|
||||
std::shared_ptr<UpdateChecker> updateChecker()
|
||||
{
|
||||
return m_updateChecker;
|
||||
}
|
||||
|
||||
std::shared_ptr<TranslationsModel> translations();
|
||||
|
||||
std::shared_ptr<JavaInstallList> javalist();
|
||||
|
||||
std::shared_ptr<InstanceList> instances() const
|
||||
{
|
||||
return m_instances;
|
||||
}
|
||||
|
||||
FolderInstanceProvider * folderProvider() const
|
||||
{
|
||||
return m_instanceFolder;
|
||||
}
|
||||
|
||||
std::shared_ptr<IconList> icons() const
|
||||
{
|
||||
return m_icons;
|
||||
}
|
||||
|
||||
MCEditTool *mcedit() const
|
||||
{
|
||||
return m_mcedit.get();
|
||||
}
|
||||
|
||||
std::shared_ptr<MojangAccountList> accounts() const
|
||||
{
|
||||
return m_accounts;
|
||||
}
|
||||
|
||||
Status status() const
|
||||
{
|
||||
return m_status;
|
||||
}
|
||||
|
||||
const QMap<QString, std::shared_ptr<BaseProfilerFactory>> &profilers() const
|
||||
{
|
||||
return m_profilers;
|
||||
}
|
||||
|
||||
/// this is the root of the 'installation'. Used for automatic updates
|
||||
const QString &root()
|
||||
{
|
||||
return m_rootPath;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Opens a json file using either a system default editor, or, if not empty, the editor
|
||||
* specified in the settings
|
||||
*/
|
||||
bool openJsonEditor(const QString &filename);
|
||||
|
||||
InstanceWindow *showInstanceWindow(InstancePtr instance, QString page = QString());
|
||||
MainWindow *showMainWindow(bool minimized = false);
|
||||
|
||||
void updateIsRunning(bool running);
|
||||
bool updatesAreAllowed();
|
||||
|
||||
void ShowGlobalSettings(class QWidget * parent, QString open_page = QString());
|
||||
|
||||
signals:
|
||||
void updateAllowedChanged(bool status);
|
||||
void globalSettingsAboutToOpen();
|
||||
void globalSettingsClosed();
|
||||
|
||||
public slots:
|
||||
bool launch(
|
||||
InstancePtr instance,
|
||||
bool online = true,
|
||||
BaseProfilerFactory *profiler = nullptr,
|
||||
MinecraftServerTargetPtr serverToJoin = nullptr
|
||||
);
|
||||
bool kill(InstancePtr instance);
|
||||
|
||||
private slots:
|
||||
void on_windowClose();
|
||||
void messageReceived(const QString & message);
|
||||
void controllerSucceeded();
|
||||
void controllerFailed(const QString & error);
|
||||
void analyticsSettingChanged(const Setting &setting, QVariant value);
|
||||
void setupWizardFinished(int status);
|
||||
|
||||
private:
|
||||
bool createSetupWizard();
|
||||
void performMainStartupAction();
|
||||
|
||||
// sets the fatal error message and m_status to Failed.
|
||||
void showFatalErrorMessage(const QString & title, const QString & content);
|
||||
|
||||
private:
|
||||
void addRunningInstance();
|
||||
void subRunningInstance();
|
||||
bool shouldExitNow() const;
|
||||
|
||||
private:
|
||||
QDateTime startTime;
|
||||
|
||||
std::shared_ptr<SettingsObject> m_settings;
|
||||
std::shared_ptr<InstanceList> m_instances;
|
||||
FolderInstanceProvider * m_instanceFolder = nullptr;
|
||||
std::shared_ptr<IconList> m_icons;
|
||||
std::shared_ptr<UpdateChecker> m_updateChecker;
|
||||
std::shared_ptr<MojangAccountList> m_accounts;
|
||||
std::shared_ptr<JavaInstallList> m_javalist;
|
||||
std::shared_ptr<TranslationsModel> m_translations;
|
||||
std::shared_ptr<GenericPageProvider> m_globalSettingsProvider;
|
||||
std::map<QString, std::unique_ptr<ITheme>> m_themes;
|
||||
std::unique_ptr<MCEditTool> m_mcedit;
|
||||
|
||||
QMap<QString, std::shared_ptr<BaseProfilerFactory>> m_profilers;
|
||||
|
||||
QString m_rootPath;
|
||||
Status m_status = MultiMC::StartingUp;
|
||||
|
||||
#if defined Q_OS_WIN32
|
||||
// used on Windows to attach the standard IO streams
|
||||
bool consoleAttached = false;
|
||||
#endif
|
||||
|
||||
// FIXME: attach to instances instead.
|
||||
struct InstanceXtras
|
||||
{
|
||||
InstanceWindow * window = nullptr;
|
||||
shared_qobject_ptr<LaunchController> controller;
|
||||
};
|
||||
std::map<QString, InstanceXtras> m_instanceExtras;
|
||||
|
||||
// main state variables
|
||||
size_t m_openWindows = 0;
|
||||
size_t m_runningInstances = 0;
|
||||
bool m_updateRunning = false;
|
||||
|
||||
// main window, if any
|
||||
MainWindow * m_mainWindow = nullptr;
|
||||
|
||||
// peer MultiMC instance connector - used to implement single instance MultiMC and signalling
|
||||
LocalPeer * m_peerInstance = nullptr;
|
||||
|
||||
GAnalytics * m_analytics = nullptr;
|
||||
SetupWizard * m_setupWizard = nullptr;
|
||||
public:
|
||||
QString m_instanceIdToLaunch;
|
||||
QString m_serverToJoin;
|
||||
bool m_liveCheck = false;
|
||||
QUrl m_zipToImport;
|
||||
std::unique_ptr<QFile> logFile;
|
||||
};
|
76
launcher/NullInstance.h
Normal file
76
launcher/NullInstance.h
Normal file
@ -0,0 +1,76 @@
|
||||
#pragma once
|
||||
#include "BaseInstance.h"
|
||||
#include "launch/LaunchTask.h"
|
||||
|
||||
class NullInstance: public BaseInstance
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
NullInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString& rootDir)
|
||||
:BaseInstance(globalSettings, settings, rootDir)
|
||||
{
|
||||
setVersionBroken(true);
|
||||
}
|
||||
virtual ~NullInstance() {};
|
||||
void saveNow() override
|
||||
{
|
||||
}
|
||||
QString getStatusbarDescription() override
|
||||
{
|
||||
return tr("Unknown instance type");
|
||||
};
|
||||
QSet< QString > traits() const override
|
||||
{
|
||||
return {};
|
||||
};
|
||||
QString instanceConfigFolder() const override
|
||||
{
|
||||
return instanceRoot();
|
||||
};
|
||||
shared_qobject_ptr<LaunchTask> createLaunchTask(AuthSessionPtr, MinecraftServerTargetPtr) override
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
shared_qobject_ptr< Task > createUpdateTask(Net::Mode mode) override
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
QProcessEnvironment createEnvironment() override
|
||||
{
|
||||
return QProcessEnvironment();
|
||||
}
|
||||
QMap<QString, QString> getVariables() const override
|
||||
{
|
||||
return QMap<QString, QString>();
|
||||
}
|
||||
IPathMatcher::Ptr getLogFileMatcher() override
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
QString getLogFileRoot() override
|
||||
{
|
||||
return instanceRoot();
|
||||
}
|
||||
QString typeName() const override
|
||||
{
|
||||
return "Null";
|
||||
}
|
||||
bool canExport() const override
|
||||
{
|
||||
return false;
|
||||
}
|
||||
bool canEdit() const override
|
||||
{
|
||||
return false;
|
||||
}
|
||||
bool canLaunch() const override
|
||||
{
|
||||
return false;
|
||||
}
|
||||
QStringList verboseDescription(AuthSessionPtr session, MinecraftServerTargetPtr serverToJoin) override
|
||||
{
|
||||
QStringList out;
|
||||
out << "Null instance - placeholder.";
|
||||
return out;
|
||||
}
|
||||
};
|
47
launcher/ProblemProvider.h
Normal file
47
launcher/ProblemProvider.h
Normal file
@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
enum class ProblemSeverity
|
||||
{
|
||||
None,
|
||||
Warning,
|
||||
Error
|
||||
};
|
||||
|
||||
struct PatchProblem
|
||||
{
|
||||
ProblemSeverity m_severity;
|
||||
QString m_description;
|
||||
};
|
||||
|
||||
class ProblemProvider
|
||||
{
|
||||
public:
|
||||
virtual ~ProblemProvider() {};
|
||||
virtual const QList<PatchProblem> getProblems() const = 0;
|
||||
virtual ProblemSeverity getProblemSeverity() const = 0;
|
||||
};
|
||||
|
||||
class ProblemContainer : public ProblemProvider
|
||||
{
|
||||
public:
|
||||
const QList<PatchProblem> getProblems() const override
|
||||
{
|
||||
return m_problems;
|
||||
}
|
||||
ProblemSeverity getProblemSeverity() const override
|
||||
{
|
||||
return m_problemSeverity;
|
||||
}
|
||||
virtual void addProblem(ProblemSeverity severity, const QString &description)
|
||||
{
|
||||
if(severity > m_problemSeverity)
|
||||
{
|
||||
m_problemSeverity = severity;
|
||||
}
|
||||
m_problems.append({severity, description});
|
||||
}
|
||||
|
||||
private:
|
||||
QList<PatchProblem> m_problems;
|
||||
ProblemSeverity m_problemSeverity = ProblemSeverity::None;
|
||||
};
|
83
launcher/QObjectPtr.h
Normal file
83
launcher/QObjectPtr.h
Normal file
@ -0,0 +1,83 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <QObject>
|
||||
|
||||
namespace details
|
||||
{
|
||||
struct DeleteQObjectLater
|
||||
{
|
||||
void operator()(QObject *obj) const
|
||||
{
|
||||
obj->deleteLater();
|
||||
}
|
||||
};
|
||||
}
|
||||
/**
|
||||
* A unique pointer class with unique pointer semantics intended for derivates of QObject
|
||||
* Calls deleteLater() instead of destroying the contained object immediately
|
||||
*/
|
||||
template<typename T> using unique_qobject_ptr = std::unique_ptr<T, details::DeleteQObjectLater>;
|
||||
|
||||
/**
|
||||
* A shared pointer class with shared pointer semantics intended for derivates of QObject
|
||||
* Calls deleteLater() instead of destroying the contained object immediately
|
||||
*/
|
||||
template <typename T>
|
||||
class shared_qobject_ptr
|
||||
{
|
||||
public:
|
||||
shared_qobject_ptr(){}
|
||||
shared_qobject_ptr(T * wrap)
|
||||
{
|
||||
reset(wrap);
|
||||
}
|
||||
shared_qobject_ptr(const shared_qobject_ptr<T>& other)
|
||||
{
|
||||
m_ptr = other.m_ptr;
|
||||
}
|
||||
template<typename Derived>
|
||||
shared_qobject_ptr(const shared_qobject_ptr<Derived> &other)
|
||||
{
|
||||
m_ptr = other.unwrap();
|
||||
}
|
||||
|
||||
public:
|
||||
void reset(T * wrap)
|
||||
{
|
||||
using namespace std::placeholders;
|
||||
m_ptr.reset(wrap, std::bind(&QObject::deleteLater, _1));
|
||||
}
|
||||
void reset(const shared_qobject_ptr<T> &other)
|
||||
{
|
||||
m_ptr = other.m_ptr;
|
||||
}
|
||||
void reset()
|
||||
{
|
||||
m_ptr.reset();
|
||||
}
|
||||
T * get() const
|
||||
{
|
||||
return m_ptr.get();
|
||||
}
|
||||
T * operator->() const
|
||||
{
|
||||
return m_ptr.get();
|
||||
}
|
||||
T & operator*() const
|
||||
{
|
||||
return *m_ptr.get();
|
||||
}
|
||||
operator bool() const
|
||||
{
|
||||
return m_ptr.get() != nullptr;
|
||||
}
|
||||
const std::shared_ptr <T> unwrap() const
|
||||
{
|
||||
return m_ptr;
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr <T> m_ptr;
|
||||
};
|
66
launcher/RWStorage.h
Normal file
66
launcher/RWStorage.h
Normal file
@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
#include <QWriteLocker>
|
||||
#include <QReadLocker>
|
||||
#include <QMap>
|
||||
#include <QSet>
|
||||
|
||||
template <typename K, typename V>
|
||||
class RWStorage
|
||||
{
|
||||
public:
|
||||
void add(K key, V value)
|
||||
{
|
||||
QWriteLocker l(&lock);
|
||||
cache[key] = value;
|
||||
stale_entries.remove(key);
|
||||
}
|
||||
V get(K key)
|
||||
{
|
||||
QReadLocker l(&lock);
|
||||
if(cache.contains(key))
|
||||
{
|
||||
return cache[key];
|
||||
}
|
||||
else return V();
|
||||
}
|
||||
bool get(K key, V& value)
|
||||
{
|
||||
QReadLocker l(&lock);
|
||||
if(cache.contains(key))
|
||||
{
|
||||
value = cache[key];
|
||||
return true;
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
bool has(K key)
|
||||
{
|
||||
QReadLocker l(&lock);
|
||||
return cache.contains(key);
|
||||
}
|
||||
bool stale(K key)
|
||||
{
|
||||
QReadLocker l(&lock);
|
||||
if(!cache.contains(key))
|
||||
return true;
|
||||
return stale_entries.contains(key);
|
||||
}
|
||||
void setStale(K key)
|
||||
{
|
||||
QWriteLocker l(&lock);
|
||||
if(cache.contains(key))
|
||||
{
|
||||
stale_entries.insert(key);
|
||||
}
|
||||
}
|
||||
void clear()
|
||||
{
|
||||
QWriteLocker l(&lock);
|
||||
cache.clear();
|
||||
stale_entries.clear();
|
||||
}
|
||||
private:
|
||||
QReadWriteLock lock;
|
||||
QMap<K, V> cache;
|
||||
QSet<K> stale_entries;
|
||||
};
|
111
launcher/RecursiveFileSystemWatcher.cpp
Normal file
111
launcher/RecursiveFileSystemWatcher.cpp
Normal file
@ -0,0 +1,111 @@
|
||||
#include "RecursiveFileSystemWatcher.h"
|
||||
|
||||
#include <QRegularExpression>
|
||||
#include <QDebug>
|
||||
|
||||
RecursiveFileSystemWatcher::RecursiveFileSystemWatcher(QObject *parent)
|
||||
: QObject(parent), m_watcher(new QFileSystemWatcher(this))
|
||||
{
|
||||
connect(m_watcher, &QFileSystemWatcher::fileChanged, this,
|
||||
&RecursiveFileSystemWatcher::fileChange);
|
||||
connect(m_watcher, &QFileSystemWatcher::directoryChanged, this,
|
||||
&RecursiveFileSystemWatcher::directoryChange);
|
||||
}
|
||||
|
||||
void RecursiveFileSystemWatcher::setRootDir(const QDir &root)
|
||||
{
|
||||
bool wasEnabled = m_isEnabled;
|
||||
disable();
|
||||
m_root = root;
|
||||
setFiles(scanRecursive(m_root));
|
||||
if (wasEnabled)
|
||||
{
|
||||
enable();
|
||||
}
|
||||
}
|
||||
void RecursiveFileSystemWatcher::setWatchFiles(const bool watchFiles)
|
||||
{
|
||||
bool wasEnabled = m_isEnabled;
|
||||
disable();
|
||||
m_watchFiles = watchFiles;
|
||||
if (wasEnabled)
|
||||
{
|
||||
enable();
|
||||
}
|
||||
}
|
||||
|
||||
void RecursiveFileSystemWatcher::enable()
|
||||
{
|
||||
if (m_isEnabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Q_ASSERT(m_root != QDir::root());
|
||||
addFilesToWatcherRecursive(m_root);
|
||||
m_isEnabled = true;
|
||||
}
|
||||
void RecursiveFileSystemWatcher::disable()
|
||||
{
|
||||
if (!m_isEnabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
m_isEnabled = false;
|
||||
m_watcher->removePaths(m_watcher->files());
|
||||
m_watcher->removePaths(m_watcher->directories());
|
||||
}
|
||||
|
||||
void RecursiveFileSystemWatcher::setFiles(const QStringList &files)
|
||||
{
|
||||
if (files != m_files)
|
||||
{
|
||||
m_files = files;
|
||||
emit filesChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void RecursiveFileSystemWatcher::addFilesToWatcherRecursive(const QDir &dir)
|
||||
{
|
||||
m_watcher->addPath(dir.absolutePath());
|
||||
for (const QString &directory : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot))
|
||||
{
|
||||
addFilesToWatcherRecursive(dir.absoluteFilePath(directory));
|
||||
}
|
||||
if (m_watchFiles)
|
||||
{
|
||||
for (const QFileInfo &info : dir.entryInfoList(QDir::Files))
|
||||
{
|
||||
m_watcher->addPath(info.absoluteFilePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
QStringList RecursiveFileSystemWatcher::scanRecursive(const QDir &directory)
|
||||
{
|
||||
QStringList ret;
|
||||
if(!m_matcher)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
for (const QString &dir : directory.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden))
|
||||
{
|
||||
ret.append(scanRecursive(directory.absoluteFilePath(dir)));
|
||||
}
|
||||
for (const QString &file : directory.entryList(QDir::Files | QDir::Hidden))
|
||||
{
|
||||
auto relPath = m_root.relativeFilePath(directory.absoluteFilePath(file));
|
||||
if (m_matcher->matches(relPath))
|
||||
{
|
||||
ret.append(relPath);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void RecursiveFileSystemWatcher::fileChange(const QString &path)
|
||||
{
|
||||
emit fileChanged(path);
|
||||
}
|
||||
void RecursiveFileSystemWatcher::directoryChange(const QString &path)
|
||||
{
|
||||
setFiles(scanRecursive(m_root));
|
||||
}
|
61
launcher/RecursiveFileSystemWatcher.h
Normal file
61
launcher/RecursiveFileSystemWatcher.h
Normal file
@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <QFileSystemWatcher>
|
||||
#include <QDir>
|
||||
#include "pathmatcher/IPathMatcher.h"
|
||||
|
||||
class RecursiveFileSystemWatcher : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
RecursiveFileSystemWatcher(QObject *parent);
|
||||
|
||||
void setRootDir(const QDir &root);
|
||||
QDir rootDir() const
|
||||
{
|
||||
return m_root;
|
||||
}
|
||||
|
||||
// WARNING: setting this to true may be bad for performance
|
||||
void setWatchFiles(const bool watchFiles);
|
||||
bool watchFiles() const
|
||||
{
|
||||
return m_watchFiles;
|
||||
}
|
||||
|
||||
void setMatcher(IPathMatcher::Ptr matcher)
|
||||
{
|
||||
m_matcher = matcher;
|
||||
}
|
||||
|
||||
QStringList files() const
|
||||
{
|
||||
return m_files;
|
||||
}
|
||||
|
||||
signals:
|
||||
void filesChanged();
|
||||
void fileChanged(const QString &path);
|
||||
|
||||
public slots:
|
||||
void enable();
|
||||
void disable();
|
||||
|
||||
private:
|
||||
QDir m_root;
|
||||
bool m_watchFiles = false;
|
||||
bool m_isEnabled = false;
|
||||
IPathMatcher::Ptr m_matcher;
|
||||
|
||||
QFileSystemWatcher *m_watcher;
|
||||
|
||||
QStringList m_files;
|
||||
void setFiles(const QStringList &files);
|
||||
|
||||
void addFilesToWatcherRecursive(const QDir &dir);
|
||||
QStringList scanRecursive(const QDir &dir);
|
||||
|
||||
private slots:
|
||||
void fileChange(const QString &path);
|
||||
void directoryChange(const QString &path);
|
||||
};
|
298
launcher/SeparatorPrefixTree.h
Normal file
298
launcher/SeparatorPrefixTree.h
Normal file
@ -0,0 +1,298 @@
|
||||
#pragma once
|
||||
#include <QString>
|
||||
#include <QMap>
|
||||
#include <QStringList>
|
||||
|
||||
template <char Tseparator>
|
||||
class SeparatorPrefixTree
|
||||
{
|
||||
public:
|
||||
SeparatorPrefixTree(QStringList paths)
|
||||
{
|
||||
insert(paths);
|
||||
}
|
||||
|
||||
SeparatorPrefixTree(bool contained = false)
|
||||
{
|
||||
m_contained = contained;
|
||||
}
|
||||
|
||||
void insert(QStringList paths)
|
||||
{
|
||||
for(auto &path: paths)
|
||||
{
|
||||
insert(path);
|
||||
}
|
||||
}
|
||||
|
||||
/// insert an exact path into the tree
|
||||
SeparatorPrefixTree & insert(QString path)
|
||||
{
|
||||
auto sepIndex = path.indexOf(Tseparator);
|
||||
if(sepIndex == -1)
|
||||
{
|
||||
children[path] = SeparatorPrefixTree(true);
|
||||
return children[path];
|
||||
}
|
||||
else
|
||||
{
|
||||
auto prefix = path.left(sepIndex);
|
||||
if(!children.contains(prefix))
|
||||
{
|
||||
children[prefix] = SeparatorPrefixTree(false);
|
||||
}
|
||||
return children[prefix].insert(path.mid(sepIndex + 1));
|
||||
}
|
||||
}
|
||||
|
||||
/// is the path fully contained in the tree?
|
||||
bool contains(QString path) const
|
||||
{
|
||||
auto node = find(path);
|
||||
return node != nullptr;
|
||||
}
|
||||
|
||||
/// does the tree cover a path? That means the prefix of the path is contained in the tree
|
||||
bool covers(QString path) const
|
||||
{
|
||||
// if we found some valid node, it's good enough. the tree covers the path
|
||||
if(m_contained)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
auto sepIndex = path.indexOf(Tseparator);
|
||||
if(sepIndex == -1)
|
||||
{
|
||||
auto found = children.find(path);
|
||||
if(found == children.end())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return (*found).covers(QString());
|
||||
}
|
||||
else
|
||||
{
|
||||
auto prefix = path.left(sepIndex);
|
||||
auto found = children.find(prefix);
|
||||
if(found == children.end())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return (*found).covers(path.mid(sepIndex + 1));
|
||||
}
|
||||
}
|
||||
|
||||
/// return the contained path that covers the path specified
|
||||
QString cover(QString path) const
|
||||
{
|
||||
// if we found some valid node, it's good enough. the tree covers the path
|
||||
if(m_contained)
|
||||
{
|
||||
return QString("");
|
||||
}
|
||||
auto sepIndex = path.indexOf(Tseparator);
|
||||
if(sepIndex == -1)
|
||||
{
|
||||
auto found = children.find(path);
|
||||
if(found == children.end())
|
||||
{
|
||||
return QString();
|
||||
}
|
||||
auto nested = (*found).cover(QString());
|
||||
if(nested.isNull())
|
||||
{
|
||||
return nested;
|
||||
}
|
||||
if(nested.isEmpty())
|
||||
return path;
|
||||
return path + Tseparator + nested;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto prefix = path.left(sepIndex);
|
||||
auto found = children.find(prefix);
|
||||
if(found == children.end())
|
||||
{
|
||||
return QString();
|
||||
}
|
||||
auto nested = (*found).cover(path.mid(sepIndex + 1));
|
||||
if(nested.isNull())
|
||||
{
|
||||
return nested;
|
||||
}
|
||||
if(nested.isEmpty())
|
||||
return prefix;
|
||||
return prefix + Tseparator + nested;
|
||||
}
|
||||
}
|
||||
|
||||
/// Does the path-specified node exist in the tree? It does not have to be contained.
|
||||
bool exists(QString path) const
|
||||
{
|
||||
auto sepIndex = path.indexOf(Tseparator);
|
||||
if(sepIndex == -1)
|
||||
{
|
||||
auto found = children.find(path);
|
||||
if(found == children.end())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto prefix = path.left(sepIndex);
|
||||
auto found = children.find(prefix);
|
||||
if(found == children.end())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return (*found).exists(path.mid(sepIndex + 1));
|
||||
}
|
||||
}
|
||||
|
||||
/// find a node in the tree by name
|
||||
const SeparatorPrefixTree * find(QString path) const
|
||||
{
|
||||
auto sepIndex = path.indexOf(Tseparator);
|
||||
if(sepIndex == -1)
|
||||
{
|
||||
auto found = children.find(path);
|
||||
if(found == children.end())
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
return &(*found);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto prefix = path.left(sepIndex);
|
||||
auto found = children.find(prefix);
|
||||
if(found == children.end())
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
return (*found).find(path.mid(sepIndex + 1));
|
||||
}
|
||||
}
|
||||
|
||||
/// is this a leaf node?
|
||||
bool leaf() const
|
||||
{
|
||||
return children.isEmpty();
|
||||
}
|
||||
|
||||
/// is this node actually contained in the tree, or is it purely structural?
|
||||
bool contained() const
|
||||
{
|
||||
return m_contained;
|
||||
}
|
||||
|
||||
/// Remove a path from the tree
|
||||
bool remove(QString path)
|
||||
{
|
||||
return removeInternal(path) != Failed;
|
||||
}
|
||||
|
||||
/// Clear all children of this node tree node
|
||||
void clear()
|
||||
{
|
||||
children.clear();
|
||||
}
|
||||
|
||||
QStringList toStringList() const
|
||||
{
|
||||
QStringList collected;
|
||||
// collecting these is more expensive.
|
||||
auto iter = children.begin();
|
||||
while(iter != children.end())
|
||||
{
|
||||
QStringList list = iter.value().toStringList();
|
||||
for(int i = 0; i < list.size(); i++)
|
||||
{
|
||||
list[i] = iter.key() + Tseparator + list[i];
|
||||
}
|
||||
collected.append(list);
|
||||
if((*iter).m_contained)
|
||||
{
|
||||
collected.append(iter.key());
|
||||
}
|
||||
iter++;
|
||||
}
|
||||
return collected;
|
||||
}
|
||||
private:
|
||||
enum Removal
|
||||
{
|
||||
Failed,
|
||||
Succeeded,
|
||||
HasChildren
|
||||
};
|
||||
Removal removeInternal(QString path = QString())
|
||||
{
|
||||
if(path.isEmpty())
|
||||
{
|
||||
if(!m_contained)
|
||||
{
|
||||
// remove all children - we are removing a prefix
|
||||
clear();
|
||||
return Succeeded;
|
||||
}
|
||||
m_contained = false;
|
||||
if(children.size())
|
||||
{
|
||||
return HasChildren;
|
||||
}
|
||||
return Succeeded;
|
||||
}
|
||||
Removal remStatus = Failed;
|
||||
QString childToRemove;
|
||||
auto sepIndex = path.indexOf(Tseparator);
|
||||
if(sepIndex == -1)
|
||||
{
|
||||
childToRemove = path;
|
||||
auto found = children.find(childToRemove);
|
||||
if(found == children.end())
|
||||
{
|
||||
return Failed;
|
||||
}
|
||||
remStatus = (*found).removeInternal();
|
||||
}
|
||||
else
|
||||
{
|
||||
childToRemove = path.left(sepIndex);
|
||||
auto found = children.find(childToRemove);
|
||||
if(found == children.end())
|
||||
{
|
||||
return Failed;
|
||||
}
|
||||
remStatus = (*found).removeInternal(path.mid(sepIndex + 1));
|
||||
}
|
||||
switch (remStatus)
|
||||
{
|
||||
case Failed:
|
||||
case HasChildren:
|
||||
{
|
||||
return remStatus;
|
||||
}
|
||||
case Succeeded:
|
||||
{
|
||||
children.remove(childToRemove);
|
||||
if(m_contained)
|
||||
{
|
||||
return HasChildren;
|
||||
}
|
||||
if(children.size())
|
||||
{
|
||||
return HasChildren;
|
||||
}
|
||||
return Succeeded;
|
||||
}
|
||||
}
|
||||
return Failed;
|
||||
}
|
||||
|
||||
private:
|
||||
QMap<QString,SeparatorPrefixTree<Tseparator>> children;
|
||||
bool m_contained = false;
|
||||
};
|
52
launcher/SkinUtils.cpp
Normal file
52
launcher/SkinUtils.cpp
Normal file
@ -0,0 +1,52 @@
|
||||
/* Copyright 2013-2021 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 "SkinUtils.h"
|
||||
#include "net/HttpMetaCache.h"
|
||||
#include "Env.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QPainter>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
|
||||
namespace SkinUtils
|
||||
{
|
||||
/*
|
||||
* Given a username, return a pixmap of the cached skin (if it exists), QPixmap() otherwise
|
||||
*/
|
||||
QPixmap getFaceFromCache(QString username, int height, int width)
|
||||
{
|
||||
QFile fskin(ENV.metacache()
|
||||
->resolveEntry("skins", username + ".png")
|
||||
->getFullPath());
|
||||
|
||||
if (fskin.exists())
|
||||
{
|
||||
QPixmap skinTexture(fskin.fileName());
|
||||
if(!skinTexture.isNull())
|
||||
{
|
||||
QPixmap skin = QPixmap(8, 8);
|
||||
QPainter painter(&skin);
|
||||
painter.drawPixmap(0, 0, skinTexture.copy(8, 8, 8, 8));
|
||||
painter.drawPixmap(0, 0, skinTexture.copy(40, 8, 8, 8));
|
||||
return skin.scaled(height, width, Qt::KeepAspectRatio);
|
||||
}
|
||||
}
|
||||
|
||||
return QPixmap();
|
||||
}
|
||||
}
|
23
launcher/SkinUtils.h
Normal file
23
launcher/SkinUtils.h
Normal file
@ -0,0 +1,23 @@
|
||||
/* Copyright 2013-2021 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 <QPixmap>
|
||||
|
||||
namespace SkinUtils
|
||||
{
|
||||
QPixmap getFaceFromCache(QString id, int height = 64, int width = 64);
|
||||
}
|
449
launcher/UpdateController.cpp
Normal file
449
launcher/UpdateController.cpp
Normal file
@ -0,0 +1,449 @@
|
||||
#include <QFile>
|
||||
#include <QMessageBox>
|
||||
#include <FileSystem.h>
|
||||
#include <updater/GoUpdate.h>
|
||||
#include "UpdateController.h"
|
||||
#include <QApplication>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <LocalPeer.h>
|
||||
|
||||
// from <sys/stat.h>
|
||||
#ifndef S_IRUSR
|
||||
#define __S_IREAD 0400 /* Read by owner. */
|
||||
#define __S_IWRITE 0200 /* Write by owner. */
|
||||
#define __S_IEXEC 0100 /* Execute by owner. */
|
||||
#define S_IRUSR __S_IREAD /* Read by owner. */
|
||||
#define S_IWUSR __S_IWRITE /* Write by owner. */
|
||||
#define S_IXUSR __S_IEXEC /* Execute by owner. */
|
||||
|
||||
#define S_IRGRP (S_IRUSR >> 3) /* Read by group. */
|
||||
#define S_IWGRP (S_IWUSR >> 3) /* Write by group. */
|
||||
#define S_IXGRP (S_IXUSR >> 3) /* Execute by group. */
|
||||
|
||||
#define S_IROTH (S_IRGRP >> 3) /* Read by others. */
|
||||
#define S_IWOTH (S_IWGRP >> 3) /* Write by others. */
|
||||
#define S_IXOTH (S_IXGRP >> 3) /* Execute by others. */
|
||||
#endif
|
||||
static QFile::Permissions unixModeToPermissions(const int mode)
|
||||
{
|
||||
QFile::Permissions perms;
|
||||
|
||||
if (mode & S_IRUSR)
|
||||
{
|
||||
perms |= QFile::ReadUser;
|
||||
}
|
||||
if (mode & S_IWUSR)
|
||||
{
|
||||
perms |= QFile::WriteUser;
|
||||
}
|
||||
if (mode & S_IXUSR)
|
||||
{
|
||||
perms |= QFile::ExeUser;
|
||||
}
|
||||
|
||||
if (mode & S_IRGRP)
|
||||
{
|
||||
perms |= QFile::ReadGroup;
|
||||
}
|
||||
if (mode & S_IWGRP)
|
||||
{
|
||||
perms |= QFile::WriteGroup;
|
||||
}
|
||||
if (mode & S_IXGRP)
|
||||
{
|
||||
perms |= QFile::ExeGroup;
|
||||
}
|
||||
|
||||
if (mode & S_IROTH)
|
||||
{
|
||||
perms |= QFile::ReadOther;
|
||||
}
|
||||
if (mode & S_IWOTH)
|
||||
{
|
||||
perms |= QFile::WriteOther;
|
||||
}
|
||||
if (mode & S_IXOTH)
|
||||
{
|
||||
perms |= QFile::ExeOther;
|
||||
}
|
||||
return perms;
|
||||
}
|
||||
|
||||
static const QLatin1String liveCheckFile("live.check");
|
||||
|
||||
UpdateController::UpdateController(QWidget * parent, const QString& root, const QString updateFilesDir, GoUpdate::OperationList operations)
|
||||
{
|
||||
m_parent = parent;
|
||||
m_root = root;
|
||||
m_updateFilesDir = updateFilesDir;
|
||||
m_operations = operations;
|
||||
}
|
||||
|
||||
|
||||
void UpdateController::installUpdates()
|
||||
{
|
||||
qint64 pid = -1;
|
||||
QStringList args;
|
||||
bool started = false;
|
||||
|
||||
qDebug() << "Installing updates.";
|
||||
#ifdef Q_OS_WIN
|
||||
QString finishCmd = QApplication::applicationFilePath();
|
||||
#elif defined Q_OS_LINUX
|
||||
QString finishCmd = FS::PathCombine(m_root, "MultiMC");
|
||||
#elif defined Q_OS_MAC
|
||||
QString finishCmd = QApplication::applicationFilePath();
|
||||
#else
|
||||
#error Unsupported operating system.
|
||||
#endif
|
||||
|
||||
QString backupPath = FS::PathCombine(m_root, "update", "backup");
|
||||
QDir origin(m_root);
|
||||
|
||||
// clean up the backup folder. it should be empty before we start
|
||||
if(!FS::deletePath(backupPath))
|
||||
{
|
||||
qWarning() << "couldn't remove previous backup folder" << backupPath;
|
||||
}
|
||||
// and it should exist.
|
||||
if(!FS::ensureFolderPathExists(backupPath))
|
||||
{
|
||||
qWarning() << "couldn't create folder" << backupPath;
|
||||
return;
|
||||
}
|
||||
|
||||
bool useXPHack = false;
|
||||
QString exePath;
|
||||
QString exeOrigin;
|
||||
QString exeBackup;
|
||||
|
||||
// perform the update operations
|
||||
for(auto op: m_operations)
|
||||
{
|
||||
switch(op.type)
|
||||
{
|
||||
// replace = move original out to backup, if it exists, move the new file in its place
|
||||
case GoUpdate::Operation::OP_REPLACE:
|
||||
{
|
||||
#ifdef Q_OS_WIN32
|
||||
// hack for people renaming the .exe because ... reasons :)
|
||||
if(op.destination == "MultiMC.exe")
|
||||
{
|
||||
op.destination = QFileInfo(QApplication::applicationFilePath()).fileName();
|
||||
}
|
||||
#endif
|
||||
QFileInfo destination (FS::PathCombine(m_root, op.destination));
|
||||
#ifdef Q_OS_WIN32
|
||||
if(QSysInfo::windowsVersion() < QSysInfo::WV_VISTA)
|
||||
{
|
||||
if(destination.fileName() == "MultiMC.exe")
|
||||
{
|
||||
QDir rootDir(m_root);
|
||||
exeOrigin = rootDir.relativeFilePath(op.source);
|
||||
exePath = rootDir.relativeFilePath(op.destination);
|
||||
exeBackup = rootDir.relativeFilePath(FS::PathCombine(backupPath, destination.fileName()));
|
||||
useXPHack = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if(destination.exists())
|
||||
{
|
||||
QString backupName = op.destination;
|
||||
backupName.replace('/', '_');
|
||||
QString backupFilePath = FS::PathCombine(backupPath, backupName);
|
||||
if(!QFile::rename(destination.absoluteFilePath(), backupFilePath))
|
||||
{
|
||||
qWarning() << "Couldn't move:" << destination.absoluteFilePath() << "to" << backupFilePath;
|
||||
m_failedOperationType = Replace;
|
||||
m_failedFile = op.destination;
|
||||
fail();
|
||||
return;
|
||||
}
|
||||
BackupEntry be;
|
||||
be.original = destination.absoluteFilePath();
|
||||
be.backup = backupFilePath;
|
||||
be.update = op.source;
|
||||
m_replace_backups.append(be);
|
||||
}
|
||||
// make sure the folder we are putting this into exists
|
||||
if(!FS::ensureFilePathExists(destination.absoluteFilePath()))
|
||||
{
|
||||
qWarning() << "REPLACE: Couldn't create folder:" << destination.absoluteFilePath();
|
||||
m_failedOperationType = Replace;
|
||||
m_failedFile = op.destination;
|
||||
fail();
|
||||
return;
|
||||
}
|
||||
// now move the new file in
|
||||
if(!QFile::rename(op.source, destination.absoluteFilePath()))
|
||||
{
|
||||
qWarning() << "REPLACE: Couldn't move:" << op.source << "to" << destination.absoluteFilePath();
|
||||
m_failedOperationType = Replace;
|
||||
m_failedFile = op.destination;
|
||||
fail();
|
||||
return;
|
||||
}
|
||||
QFile::setPermissions(destination.absoluteFilePath(), unixModeToPermissions(op.destinationMode));
|
||||
}
|
||||
break;
|
||||
// delete = move original to backup
|
||||
case GoUpdate::Operation::OP_DELETE:
|
||||
{
|
||||
QString destFilePath = FS::PathCombine(m_root, op.destination);
|
||||
if(QFile::exists(destFilePath))
|
||||
{
|
||||
QString backupName = op.destination;
|
||||
backupName.replace('/', '_');
|
||||
QString trashFilePath = FS::PathCombine(backupPath, backupName);
|
||||
|
||||
if(!QFile::rename(destFilePath, trashFilePath))
|
||||
{
|
||||
qWarning() << "DELETE: Couldn't move:" << op.destination << "to" << trashFilePath;
|
||||
m_failedFile = op.destination;
|
||||
m_failedOperationType = Delete;
|
||||
fail();
|
||||
return;
|
||||
}
|
||||
BackupEntry be;
|
||||
be.original = destFilePath;
|
||||
be.backup = trashFilePath;
|
||||
m_delete_backups.append(be);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// try to start the new binary
|
||||
args = qApp->arguments();
|
||||
args.removeFirst();
|
||||
|
||||
// on old Windows, do insane things... no error checking here, this is just to have something.
|
||||
if(useXPHack)
|
||||
{
|
||||
QString script;
|
||||
auto nativePath = QDir::toNativeSeparators(exePath);
|
||||
auto nativeOriginPath = QDir::toNativeSeparators(exeOrigin);
|
||||
auto nativeBackupPath = QDir::toNativeSeparators(exeBackup);
|
||||
|
||||
// so we write this vbscript thing...
|
||||
QTextStream out(&script);
|
||||
out << "WScript.Sleep 1000\n";
|
||||
out << "Set fso=CreateObject(\"Scripting.FileSystemObject\")\n";
|
||||
out << "Set shell=CreateObject(\"WScript.Shell\")\n";
|
||||
out << "fso.MoveFile \"" << nativePath << "\", \"" << nativeBackupPath << "\"\n";
|
||||
out << "fso.MoveFile \"" << nativeOriginPath << "\", \"" << nativePath << "\"\n";
|
||||
out << "shell.Run \"" << nativePath << "\"\n";
|
||||
|
||||
QString scriptPath = FS::PathCombine(m_root, "update", "update.vbs");
|
||||
|
||||
// we save it
|
||||
QFile scriptFile(scriptPath);
|
||||
scriptFile.open(QIODevice::WriteOnly);
|
||||
scriptFile.write(script.toLocal8Bit().replace("\n", "\r\n"));
|
||||
scriptFile.close();
|
||||
|
||||
// we run it
|
||||
started = QProcess::startDetached("wscript", {scriptPath}, m_root);
|
||||
|
||||
// and we quit. conscious thought.
|
||||
qApp->quit();
|
||||
return;
|
||||
}
|
||||
bool doLiveCheck = true;
|
||||
bool startFailed = false;
|
||||
|
||||
// remove live check file, if any
|
||||
if(QFile::exists(liveCheckFile))
|
||||
{
|
||||
if(!QFile::remove(liveCheckFile))
|
||||
{
|
||||
qWarning() << "Couldn't remove the" << liveCheckFile << "file! We will proceed without :(";
|
||||
doLiveCheck = false;
|
||||
}
|
||||
}
|
||||
|
||||
if(doLiveCheck)
|
||||
{
|
||||
if(!args.contains("--alive"))
|
||||
{
|
||||
args.append("--alive");
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: reparse args and construct a safe variant from scratch. This is a workaround for GH-1874:
|
||||
QStringList realargs;
|
||||
int skip = 0;
|
||||
for(auto & arg: args)
|
||||
{
|
||||
if(skip)
|
||||
{
|
||||
skip--;
|
||||
continue;
|
||||
}
|
||||
if(arg == "-l")
|
||||
{
|
||||
skip = 1;
|
||||
continue;
|
||||
}
|
||||
realargs.append(arg);
|
||||
}
|
||||
|
||||
// start the updated application
|
||||
started = QProcess::startDetached(finishCmd, realargs, QDir::currentPath(), &pid);
|
||||
// much dumber check - just find out if the call
|
||||
if(!started || pid == -1)
|
||||
{
|
||||
qWarning() << "Couldn't start new process properly!";
|
||||
startFailed = true;
|
||||
}
|
||||
if(!startFailed && doLiveCheck)
|
||||
{
|
||||
int attempts = 0;
|
||||
while(attempts < 10)
|
||||
{
|
||||
attempts++;
|
||||
QString key;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||
if(!QFile::exists(liveCheckFile))
|
||||
{
|
||||
qWarning() << "Couldn't find the" << liveCheckFile << "file!";
|
||||
startFailed = true;
|
||||
continue;
|
||||
}
|
||||
try
|
||||
{
|
||||
key = QString::fromUtf8(FS::read(liveCheckFile));
|
||||
auto id = ApplicationId::fromRawString(key);
|
||||
LocalPeer peer(nullptr, id);
|
||||
if(peer.isClient())
|
||||
{
|
||||
startFailed = false;
|
||||
qDebug() << "Found process started with key " << key;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
startFailed = true;
|
||||
qDebug() << "Process started with key " << key << "apparently died or is not reponding...";
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (const Exception &e)
|
||||
{
|
||||
qWarning() << "Couldn't read the" << liveCheckFile << "file!";
|
||||
startFailed = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(startFailed)
|
||||
{
|
||||
m_failedOperationType = Start;
|
||||
fail();
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
origin.rmdir(m_updateFilesDir);
|
||||
qApp->quit();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateController::fail()
|
||||
{
|
||||
qWarning() << "Update failed!";
|
||||
|
||||
QString msg;
|
||||
bool doRollback = false;
|
||||
QString failTitle = QObject::tr("Update failed!");
|
||||
QString rollFailTitle = QObject::tr("Rollback failed!");
|
||||
switch (m_failedOperationType)
|
||||
{
|
||||
case Replace:
|
||||
{
|
||||
msg = QObject::tr("Couldn't replace file %1. Changes will be reverted.\n"
|
||||
"See the MultiMC log file for details.").arg(m_failedFile);
|
||||
doRollback = true;
|
||||
QMessageBox::critical(m_parent, failTitle, msg);
|
||||
break;
|
||||
}
|
||||
case Delete:
|
||||
{
|
||||
msg = QObject::tr("Couldn't remove file %1. Changes will be reverted.\n"
|
||||
"See the MultiMC log file for details.").arg(m_failedFile);
|
||||
doRollback = true;
|
||||
QMessageBox::critical(m_parent, failTitle, msg);
|
||||
break;
|
||||
}
|
||||
case Start:
|
||||
{
|
||||
msg = QObject::tr("The new version didn't start or is too old and doesn't respond to startup checks.\n"
|
||||
"\n"
|
||||
"Roll back to previous version?");
|
||||
auto result = QMessageBox::critical(
|
||||
m_parent,
|
||||
failTitle,
|
||||
msg,
|
||||
QMessageBox::Yes | QMessageBox::No,
|
||||
QMessageBox::Yes
|
||||
);
|
||||
doRollback = (result == QMessageBox::Yes);
|
||||
break;
|
||||
}
|
||||
case Nothing:
|
||||
default:
|
||||
return;
|
||||
}
|
||||
if(doRollback)
|
||||
{
|
||||
auto rollbackOK = rollback();
|
||||
if(!rollbackOK)
|
||||
{
|
||||
msg = QObject::tr("The rollback failed too.\n"
|
||||
"You will have to repair MultiMC manually.\n"
|
||||
"Please let us know why and how this happened.").arg(m_failedFile);
|
||||
QMessageBox::critical(m_parent, rollFailTitle, msg);
|
||||
qApp->quit();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
qApp->quit();
|
||||
}
|
||||
}
|
||||
|
||||
bool UpdateController::rollback()
|
||||
{
|
||||
bool revertOK = true;
|
||||
// if the above failed, roll back changes
|
||||
for(auto backup:m_replace_backups)
|
||||
{
|
||||
qWarning() << "restoring" << backup.original << "from" << backup.backup;
|
||||
if(!QFile::rename(backup.original, backup.update))
|
||||
{
|
||||
revertOK = false;
|
||||
qWarning() << "moving new" << backup.original << "back to" << backup.update << "failed!";
|
||||
continue;
|
||||
}
|
||||
|
||||
if(!QFile::rename(backup.backup, backup.original))
|
||||
{
|
||||
revertOK = false;
|
||||
qWarning() << "restoring" << backup.original << "failed!";
|
||||
}
|
||||
}
|
||||
for(auto backup:m_delete_backups)
|
||||
{
|
||||
qWarning() << "restoring" << backup.original << "from" << backup.backup;
|
||||
if(!QFile::rename(backup.backup, backup.original))
|
||||
{
|
||||
revertOK = false;
|
||||
qWarning() << "restoring" << backup.original << "failed!";
|
||||
}
|
||||
}
|
||||
return revertOK;
|
||||
}
|
44
launcher/UpdateController.h
Normal file
44
launcher/UpdateController.h
Normal file
@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <QList>
|
||||
#include <updater/GoUpdate.h>
|
||||
|
||||
class QWidget;
|
||||
|
||||
class UpdateController
|
||||
{
|
||||
public:
|
||||
UpdateController(QWidget * parent, const QString &root, const QString updateFilesDir, GoUpdate::OperationList operations);
|
||||
void installUpdates();
|
||||
|
||||
private:
|
||||
void fail();
|
||||
bool rollback();
|
||||
|
||||
private:
|
||||
QString m_root;
|
||||
QString m_updateFilesDir;
|
||||
GoUpdate::OperationList m_operations;
|
||||
QWidget * m_parent;
|
||||
|
||||
struct BackupEntry
|
||||
{
|
||||
// path where we got the new file from
|
||||
QString update;
|
||||
// path of what is being actually updated
|
||||
QString original;
|
||||
// path where the backup of the updated file was placed
|
||||
QString backup;
|
||||
};
|
||||
QList <BackupEntry> m_replace_backups;
|
||||
QList <BackupEntry> m_delete_backups;
|
||||
enum Failure
|
||||
{
|
||||
Replace,
|
||||
Delete,
|
||||
Start,
|
||||
Nothing
|
||||
} m_failedOperationType = Nothing;
|
||||
QString m_failedFile;
|
||||
};
|
58
launcher/Usable.h
Normal file
58
launcher/Usable.h
Normal file
@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
|
||||
class Usable;
|
||||
|
||||
/**
|
||||
* Base class for things that can be used by multiple other things and we want to track the use count.
|
||||
*
|
||||
* @see UseLock
|
||||
*/
|
||||
class Usable
|
||||
{
|
||||
friend class UseLock;
|
||||
public:
|
||||
std::size_t useCount()
|
||||
{
|
||||
return m_useCount;
|
||||
}
|
||||
bool isInUse()
|
||||
{
|
||||
return m_useCount > 0;
|
||||
}
|
||||
protected:
|
||||
virtual void decrementUses()
|
||||
{
|
||||
m_useCount--;
|
||||
}
|
||||
virtual void incrementUses()
|
||||
{
|
||||
m_useCount++;
|
||||
}
|
||||
private:
|
||||
std::size_t m_useCount = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Lock class to use for keeping track of uses of other things derived from Usable
|
||||
*
|
||||
* @see Usable
|
||||
*/
|
||||
class UseLock
|
||||
{
|
||||
public:
|
||||
UseLock(std::shared_ptr<Usable> usable)
|
||||
: m_usable(usable)
|
||||
{
|
||||
// this doesn't use shared pointer use count, because that wouldn't be correct. this count is separate.
|
||||
m_usable->incrementUses();
|
||||
}
|
||||
~UseLock()
|
||||
{
|
||||
m_usable->decrementUses();
|
||||
}
|
||||
private:
|
||||
std::shared_ptr<Usable> m_usable;
|
||||
};
|
85
launcher/Version.cpp
Normal file
85
launcher/Version.cpp
Normal file
@ -0,0 +1,85 @@
|
||||
#include "Version.h"
|
||||
|
||||
#include <QStringList>
|
||||
#include <QUrl>
|
||||
#include <QRegularExpression>
|
||||
#include <QRegularExpressionMatch>
|
||||
|
||||
Version::Version(const QString &str) : m_string(str)
|
||||
{
|
||||
parse();
|
||||
}
|
||||
|
||||
bool Version::operator<(const Version &other) const
|
||||
{
|
||||
const int size = qMax(m_sections.size(), other.m_sections.size());
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
const Section sec1 = (i >= m_sections.size()) ? Section("0") : m_sections.at(i);
|
||||
const Section sec2 =
|
||||
(i >= other.m_sections.size()) ? Section("0") : other.m_sections.at(i);
|
||||
if (sec1 != sec2)
|
||||
{
|
||||
return sec1 < sec2;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
bool Version::operator<=(const Version &other) const
|
||||
{
|
||||
return *this < other || *this == other;
|
||||
}
|
||||
bool Version::operator>(const Version &other) const
|
||||
{
|
||||
const int size = qMax(m_sections.size(), other.m_sections.size());
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
const Section sec1 = (i >= m_sections.size()) ? Section("0") : m_sections.at(i);
|
||||
const Section sec2 =
|
||||
(i >= other.m_sections.size()) ? Section("0") : other.m_sections.at(i);
|
||||
if (sec1 != sec2)
|
||||
{
|
||||
return sec1 > sec2;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
bool Version::operator>=(const Version &other) const
|
||||
{
|
||||
return *this > other || *this == other;
|
||||
}
|
||||
bool Version::operator==(const Version &other) const
|
||||
{
|
||||
const int size = qMax(m_sections.size(), other.m_sections.size());
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
const Section sec1 = (i >= m_sections.size()) ? Section("0") : m_sections.at(i);
|
||||
const Section sec2 =
|
||||
(i >= other.m_sections.size()) ? Section("0") : other.m_sections.at(i);
|
||||
if (sec1 != sec2)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
bool Version::operator!=(const Version &other) const
|
||||
{
|
||||
return !operator==(other);
|
||||
}
|
||||
|
||||
void Version::parse()
|
||||
{
|
||||
m_sections.clear();
|
||||
|
||||
// FIXME: this is bad. versions can contain a lot more separators...
|
||||
QStringList parts = m_string.split('.');
|
||||
|
||||
for (const auto &part : parts)
|
||||
{
|
||||
m_sections.append(Section(part));
|
||||
}
|
||||
}
|
105
launcher/Version.h
Normal file
105
launcher/Version.h
Normal file
@ -0,0 +1,105 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <QList>
|
||||
|
||||
class QUrl;
|
||||
|
||||
class Version
|
||||
{
|
||||
public:
|
||||
Version(const QString &str);
|
||||
Version() {}
|
||||
|
||||
bool operator<(const Version &other) const;
|
||||
bool operator<=(const Version &other) const;
|
||||
bool operator>(const Version &other) const;
|
||||
bool operator>=(const Version &other) const;
|
||||
bool operator==(const Version &other) const;
|
||||
bool operator!=(const Version &other) const;
|
||||
|
||||
QString toString() const
|
||||
{
|
||||
return m_string;
|
||||
}
|
||||
|
||||
private:
|
||||
QString m_string;
|
||||
struct Section
|
||||
{
|
||||
explicit Section(const QString &fullString)
|
||||
{
|
||||
m_fullString = fullString;
|
||||
int cutoff = m_fullString.size();
|
||||
for(int i = 0; i < m_fullString.size(); i++)
|
||||
{
|
||||
if(!m_fullString[i].isDigit())
|
||||
{
|
||||
cutoff = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
auto numPart = m_fullString.leftRef(cutoff);
|
||||
if(numPart.size())
|
||||
{
|
||||
numValid = true;
|
||||
m_numPart = numPart.toInt();
|
||||
}
|
||||
auto stringPart = m_fullString.midRef(cutoff);
|
||||
if(stringPart.size())
|
||||
{
|
||||
m_stringPart = stringPart.toString();
|
||||
}
|
||||
}
|
||||
explicit Section() {}
|
||||
bool numValid = false;
|
||||
int m_numPart = 0;
|
||||
QString m_stringPart;
|
||||
QString m_fullString;
|
||||
|
||||
inline bool operator!=(const Section &other) const
|
||||
{
|
||||
if(numValid && other.numValid)
|
||||
{
|
||||
return m_numPart != other.m_numPart || m_stringPart != other.m_stringPart;
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_fullString != other.m_fullString;
|
||||
}
|
||||
}
|
||||
inline bool operator<(const Section &other) const
|
||||
{
|
||||
if(numValid && other.numValid)
|
||||
{
|
||||
if(m_numPart < other.m_numPart)
|
||||
return true;
|
||||
if(m_numPart == other.m_numPart && m_stringPart < other.m_stringPart)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_fullString < other.m_fullString;
|
||||
}
|
||||
}
|
||||
inline bool operator>(const Section &other) const
|
||||
{
|
||||
if(numValid && other.numValid)
|
||||
{
|
||||
if(m_numPart > other.m_numPart)
|
||||
return true;
|
||||
if(m_numPart == other.m_numPart && m_stringPart > other.m_stringPart)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_fullString > other.m_fullString;
|
||||
}
|
||||
}
|
||||
};
|
||||
QList<Section> m_sections;
|
||||
|
||||
void parse();
|
||||
};
|
447
launcher/VersionProxyModel.cpp
Normal file
447
launcher/VersionProxyModel.cpp
Normal file
@ -0,0 +1,447 @@
|
||||
#include "VersionProxyModel.h"
|
||||
#include "MultiMC.h"
|
||||
#include <QSortFilterProxyModel>
|
||||
#include <QPixmapCache>
|
||||
#include <Version.h>
|
||||
#include <meta/VersionList.h>
|
||||
|
||||
class VersionFilterModel : public QSortFilterProxyModel
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
VersionFilterModel(VersionProxyModel *parent) : QSortFilterProxyModel(parent)
|
||||
{
|
||||
m_parent = parent;
|
||||
setSortRole(BaseVersionList::SortRole);
|
||||
sort(0, Qt::DescendingOrder);
|
||||
}
|
||||
|
||||
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
|
||||
{
|
||||
const auto &filters = m_parent->filters();
|
||||
for (auto it = filters.begin(); it != filters.end(); ++it)
|
||||
{
|
||||
auto idx = sourceModel()->index(source_row, 0, source_parent);
|
||||
auto data = sourceModel()->data(idx, it.key());
|
||||
auto match = data.toString();
|
||||
if(!it.value()->accepts(match))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void filterChanged()
|
||||
{
|
||||
invalidateFilter();
|
||||
}
|
||||
private:
|
||||
VersionProxyModel *m_parent;
|
||||
};
|
||||
|
||||
VersionProxyModel::VersionProxyModel(QObject *parent) : QAbstractProxyModel(parent)
|
||||
{
|
||||
filterModel = new VersionFilterModel(this);
|
||||
connect(filterModel, &QAbstractItemModel::dataChanged, this, &VersionProxyModel::sourceDataChanged);
|
||||
connect(filterModel, &QAbstractItemModel::rowsAboutToBeInserted, this, &VersionProxyModel::sourceRowsAboutToBeInserted);
|
||||
connect(filterModel, &QAbstractItemModel::rowsInserted, this, &VersionProxyModel::sourceRowsInserted);
|
||||
connect(filterModel, &QAbstractItemModel::rowsAboutToBeRemoved, this, &VersionProxyModel::sourceRowsAboutToBeRemoved);
|
||||
connect(filterModel, &QAbstractItemModel::rowsRemoved, this, &VersionProxyModel::sourceRowsRemoved);
|
||||
// FIXME: implement when needed
|
||||
/*
|
||||
connect(replacing, &QAbstractItemModel::rowsAboutToBeMoved, this, &VersionProxyModel::sourceRowsAboutToBeMoved);
|
||||
connect(replacing, &QAbstractItemModel::rowsMoved, this, &VersionProxyModel::sourceRowsMoved);
|
||||
connect(replacing, &QAbstractItemModel::layoutAboutToBeChanged, this, &VersionProxyModel::sourceLayoutAboutToBeChanged);
|
||||
connect(replacing, &QAbstractItemModel::layoutChanged, this, &VersionProxyModel::sourceLayoutChanged);
|
||||
*/
|
||||
connect(filterModel, &QAbstractItemModel::modelAboutToBeReset, this, &VersionProxyModel::sourceAboutToBeReset);
|
||||
connect(filterModel, &QAbstractItemModel::modelReset, this, &VersionProxyModel::sourceReset);
|
||||
|
||||
QAbstractProxyModel::setSourceModel(filterModel);
|
||||
}
|
||||
|
||||
QVariant VersionProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
if(section < 0 || section >= m_columns.size())
|
||||
return QVariant();
|
||||
if(orientation != Qt::Horizontal)
|
||||
return QVariant();
|
||||
auto column = m_columns[section];
|
||||
if(role == Qt::DisplayRole)
|
||||
{
|
||||
switch(column)
|
||||
{
|
||||
case Name:
|
||||
return tr("Version");
|
||||
case ParentVersion:
|
||||
return tr("Minecraft"); //FIXME: this should come from metadata
|
||||
case Branch:
|
||||
return tr("Branch");
|
||||
case Type:
|
||||
return tr("Type");
|
||||
case Architecture:
|
||||
return tr("Architecture");
|
||||
case Path:
|
||||
return tr("Path");
|
||||
case Time:
|
||||
return tr("Released");
|
||||
}
|
||||
}
|
||||
else if(role == Qt::ToolTipRole)
|
||||
{
|
||||
switch(column)
|
||||
{
|
||||
case Name:
|
||||
return tr("The name of the version.");
|
||||
case ParentVersion:
|
||||
return tr("Minecraft version"); //FIXME: this should come from metadata
|
||||
case Branch:
|
||||
return tr("The version's branch");
|
||||
case Type:
|
||||
return tr("The version's type");
|
||||
case Architecture:
|
||||
return tr("CPU Architecture");
|
||||
case Path:
|
||||
return tr("Filesystem path to this version");
|
||||
case Time:
|
||||
return tr("Release date of this version");
|
||||
}
|
||||
}
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
QVariant VersionProxyModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if(!index.isValid())
|
||||
{
|
||||
return QVariant();
|
||||
}
|
||||
auto column = m_columns[index.column()];
|
||||
auto parentIndex = mapToSource(index);
|
||||
switch(role)
|
||||
{
|
||||
case Qt::DisplayRole:
|
||||
{
|
||||
switch(column)
|
||||
{
|
||||
case Name:
|
||||
{
|
||||
QString version = sourceModel()->data(parentIndex, BaseVersionList::VersionRole).toString();
|
||||
if(version == m_currentVersion)
|
||||
{
|
||||
return tr("%1 (installed)").arg(version);
|
||||
}
|
||||
return version;
|
||||
}
|
||||
case ParentVersion:
|
||||
return sourceModel()->data(parentIndex, BaseVersionList::ParentVersionRole);
|
||||
case Branch:
|
||||
return sourceModel()->data(parentIndex, BaseVersionList::BranchRole);
|
||||
case Type:
|
||||
return sourceModel()->data(parentIndex, BaseVersionList::TypeRole);
|
||||
case Architecture:
|
||||
return sourceModel()->data(parentIndex, BaseVersionList::ArchitectureRole);
|
||||
case Path:
|
||||
return sourceModel()->data(parentIndex, BaseVersionList::PathRole);
|
||||
case Time:
|
||||
return sourceModel()->data(parentIndex, Meta::VersionList::TimeRole).toDate();
|
||||
default:
|
||||
return QVariant();
|
||||
}
|
||||
}
|
||||
case Qt::ToolTipRole:
|
||||
{
|
||||
switch(column)
|
||||
{
|
||||
case Name:
|
||||
{
|
||||
if(hasRecommended)
|
||||
{
|
||||
auto value = sourceModel()->data(parentIndex, BaseVersionList::RecommendedRole);
|
||||
if(value.toBool())
|
||||
{
|
||||
return tr("Recommended");
|
||||
}
|
||||
else if(hasLatest)
|
||||
{
|
||||
auto value = sourceModel()->data(parentIndex, BaseVersionList::LatestRole);
|
||||
if(value.toBool())
|
||||
{
|
||||
return tr("Latest");
|
||||
}
|
||||
}
|
||||
else if(index.row() == 0)
|
||||
{
|
||||
return tr("Latest");
|
||||
}
|
||||
}
|
||||
}
|
||||
default:
|
||||
{
|
||||
return sourceModel()->data(parentIndex, BaseVersionList::VersionIdRole);
|
||||
}
|
||||
}
|
||||
}
|
||||
case Qt::DecorationRole:
|
||||
{
|
||||
switch(column)
|
||||
{
|
||||
case Name:
|
||||
{
|
||||
if(hasRecommended)
|
||||
{
|
||||
auto value = sourceModel()->data(parentIndex, BaseVersionList::RecommendedRole);
|
||||
if(value.toBool())
|
||||
{
|
||||
return MMC->getThemedIcon("star");
|
||||
}
|
||||
else if(hasLatest)
|
||||
{
|
||||
auto value = sourceModel()->data(parentIndex, BaseVersionList::LatestRole);
|
||||
if(value.toBool())
|
||||
{
|
||||
return MMC->getThemedIcon("bug");
|
||||
}
|
||||
}
|
||||
else if(index.row() == 0)
|
||||
{
|
||||
return MMC->getThemedIcon("bug");
|
||||
}
|
||||
auto pixmap = QPixmapCache::find("placeholder");
|
||||
if(!pixmap)
|
||||
{
|
||||
QPixmap px(16,16);
|
||||
px.fill(Qt::transparent);
|
||||
QPixmapCache::insert("placeholder", px);
|
||||
return px;
|
||||
}
|
||||
return *pixmap;
|
||||
}
|
||||
}
|
||||
default:
|
||||
{
|
||||
return QVariant();
|
||||
}
|
||||
}
|
||||
}
|
||||
default:
|
||||
{
|
||||
if(roles.contains((BaseVersionList::ModelRoles)role))
|
||||
{
|
||||
return sourceModel()->data(parentIndex, role);
|
||||
}
|
||||
return QVariant();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QModelIndex VersionProxyModel::parent(const QModelIndex &child) const
|
||||
{
|
||||
return QModelIndex();
|
||||
}
|
||||
|
||||
QModelIndex VersionProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
|
||||
{
|
||||
if(sourceIndex.isValid())
|
||||
{
|
||||
return index(sourceIndex.row(), 0);
|
||||
}
|
||||
return QModelIndex();
|
||||
}
|
||||
|
||||
QModelIndex VersionProxyModel::mapToSource(const QModelIndex &proxyIndex) const
|
||||
{
|
||||
if(proxyIndex.isValid())
|
||||
{
|
||||
return sourceModel()->index(proxyIndex.row(), 0);
|
||||
}
|
||||
return QModelIndex();
|
||||
}
|
||||
|
||||
QModelIndex VersionProxyModel::index(int row, int column, const QModelIndex &parent) const
|
||||
{
|
||||
// no trees here... shoo
|
||||
if(parent.isValid())
|
||||
{
|
||||
return QModelIndex();
|
||||
}
|
||||
if(row < 0 || row >= sourceModel()->rowCount())
|
||||
return QModelIndex();
|
||||
if(column < 0 || column >= columnCount())
|
||||
return QModelIndex();
|
||||
return QAbstractItemModel::createIndex(row, column);
|
||||
}
|
||||
|
||||
int VersionProxyModel::columnCount(const QModelIndex &parent) const
|
||||
{
|
||||
return m_columns.size();
|
||||
}
|
||||
|
||||
int VersionProxyModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
if(sourceModel())
|
||||
{
|
||||
return sourceModel()->rowCount();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void VersionProxyModel::sourceDataChanged(const QModelIndex &source_top_left,
|
||||
const QModelIndex &source_bottom_right)
|
||||
{
|
||||
if(source_top_left.parent() != source_bottom_right.parent())
|
||||
return;
|
||||
|
||||
// whole row is getting changed
|
||||
auto topLeft = createIndex(source_top_left.row(), 0);
|
||||
auto bottomRight = createIndex(source_bottom_right.row(), columnCount() - 1);
|
||||
emit dataChanged(topLeft, bottomRight);
|
||||
}
|
||||
|
||||
void VersionProxyModel::setSourceModel(QAbstractItemModel *replacingRaw)
|
||||
{
|
||||
auto replacing = dynamic_cast<BaseVersionList *>(replacingRaw);
|
||||
beginResetModel();
|
||||
|
||||
m_columns.clear();
|
||||
if(!replacing)
|
||||
{
|
||||
roles.clear();
|
||||
filterModel->setSourceModel(replacing);
|
||||
return;
|
||||
}
|
||||
|
||||
roles = replacing->providesRoles();
|
||||
if(roles.contains(BaseVersionList::VersionRole))
|
||||
{
|
||||
m_columns.push_back(Name);
|
||||
}
|
||||
/*
|
||||
if(roles.contains(BaseVersionList::ParentVersionRole))
|
||||
{
|
||||
m_columns.push_back(ParentVersion);
|
||||
}
|
||||
*/
|
||||
if(roles.contains(BaseVersionList::ArchitectureRole))
|
||||
{
|
||||
m_columns.push_back(Architecture);
|
||||
}
|
||||
if(roles.contains(BaseVersionList::PathRole))
|
||||
{
|
||||
m_columns.push_back(Path);
|
||||
}
|
||||
if(roles.contains(Meta::VersionList::TimeRole))
|
||||
{
|
||||
m_columns.push_back(Time);
|
||||
}
|
||||
if(roles.contains(BaseVersionList::BranchRole))
|
||||
{
|
||||
m_columns.push_back(Branch);
|
||||
}
|
||||
if(roles.contains(BaseVersionList::TypeRole))
|
||||
{
|
||||
m_columns.push_back(Type);
|
||||
}
|
||||
if(roles.contains(BaseVersionList::RecommendedRole))
|
||||
{
|
||||
hasRecommended = true;
|
||||
}
|
||||
if(roles.contains(BaseVersionList::LatestRole))
|
||||
{
|
||||
hasLatest = true;
|
||||
}
|
||||
filterModel->setSourceModel(replacing);
|
||||
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
QModelIndex VersionProxyModel::getRecommended() const
|
||||
{
|
||||
if(!roles.contains(BaseVersionList::RecommendedRole))
|
||||
{
|
||||
return index(0, 0);
|
||||
}
|
||||
int recommended = 0;
|
||||
for (int i = 0; i < rowCount(); i++)
|
||||
{
|
||||
auto value = sourceModel()->data(mapToSource(index(i, 0)), BaseVersionList::RecommendedRole);
|
||||
if (value.toBool())
|
||||
{
|
||||
recommended = i;
|
||||
}
|
||||
}
|
||||
return index(recommended, 0);
|
||||
}
|
||||
|
||||
QModelIndex VersionProxyModel::getVersion(const QString& version) const
|
||||
{
|
||||
int found = -1;
|
||||
for (int i = 0; i < rowCount(); i++)
|
||||
{
|
||||
auto value = sourceModel()->data(mapToSource(index(i, 0)), BaseVersionList::VersionRole);
|
||||
if (value.toString() == version)
|
||||
{
|
||||
found = i;
|
||||
}
|
||||
}
|
||||
if(found == -1)
|
||||
{
|
||||
return QModelIndex();
|
||||
}
|
||||
return index(found, 0);
|
||||
}
|
||||
|
||||
void VersionProxyModel::clearFilters()
|
||||
{
|
||||
m_filters.clear();
|
||||
filterModel->filterChanged();
|
||||
}
|
||||
|
||||
void VersionProxyModel::setFilter(const BaseVersionList::ModelRoles column, Filter * f)
|
||||
{
|
||||
m_filters[column].reset(f);
|
||||
filterModel->filterChanged();
|
||||
}
|
||||
|
||||
const VersionProxyModel::FilterMap &VersionProxyModel::filters() const
|
||||
{
|
||||
return m_filters;
|
||||
}
|
||||
|
||||
void VersionProxyModel::sourceAboutToBeReset()
|
||||
{
|
||||
beginResetModel();
|
||||
}
|
||||
|
||||
void VersionProxyModel::sourceReset()
|
||||
{
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
void VersionProxyModel::sourceRowsAboutToBeInserted(const QModelIndex& parent, int first, int last)
|
||||
{
|
||||
beginInsertRows(parent, first, last);
|
||||
}
|
||||
|
||||
void VersionProxyModel::sourceRowsInserted(const QModelIndex& parent, int first, int last)
|
||||
{
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
void VersionProxyModel::sourceRowsAboutToBeRemoved(const QModelIndex& parent, int first, int last)
|
||||
{
|
||||
beginRemoveRows(parent, first, last);
|
||||
}
|
||||
|
||||
void VersionProxyModel::sourceRowsRemoved(const QModelIndex& parent, int first, int last)
|
||||
{
|
||||
endRemoveRows();
|
||||
}
|
||||
|
||||
void VersionProxyModel::setCurrentVersion(const QString &version)
|
||||
{
|
||||
m_currentVersion = version;
|
||||
}
|
||||
|
||||
#include "VersionProxyModel.moc"
|
67
launcher/VersionProxyModel.h
Normal file
67
launcher/VersionProxyModel.h
Normal file
@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
#include <QAbstractProxyModel>
|
||||
#include "BaseVersionList.h"
|
||||
|
||||
#include <Filter.h>
|
||||
|
||||
class VersionFilterModel;
|
||||
|
||||
class VersionProxyModel: public QAbstractProxyModel
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
||||
enum Column
|
||||
{
|
||||
Name,
|
||||
ParentVersion,
|
||||
Branch,
|
||||
Type,
|
||||
Architecture,
|
||||
Path,
|
||||
Time
|
||||
};
|
||||
typedef QHash<BaseVersionList::ModelRoles, std::shared_ptr<Filter>> FilterMap;
|
||||
|
||||
public:
|
||||
VersionProxyModel ( QObject* parent = 0 );
|
||||
virtual ~VersionProxyModel() {};
|
||||
|
||||
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override;
|
||||
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const override;
|
||||
virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
|
||||
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
|
||||
virtual QModelIndex parent(const QModelIndex &child) const override;
|
||||
virtual void setSourceModel(QAbstractItemModel *sourceModel) override;
|
||||
|
||||
const FilterMap &filters() const;
|
||||
void setFilter(const BaseVersionList::ModelRoles column, Filter * filter);
|
||||
void clearFilters();
|
||||
QModelIndex getRecommended() const;
|
||||
QModelIndex getVersion(const QString & version) const;
|
||||
void setCurrentVersion(const QString &version);
|
||||
private slots:
|
||||
|
||||
void sourceDataChanged(const QModelIndex &source_top_left,const QModelIndex &source_bottom_right);
|
||||
|
||||
void sourceAboutToBeReset();
|
||||
void sourceReset();
|
||||
|
||||
void sourceRowsAboutToBeInserted(const QModelIndex &parent, int first, int last);
|
||||
void sourceRowsInserted(const QModelIndex &parent, int first, int last);
|
||||
|
||||
void sourceRowsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
|
||||
void sourceRowsRemoved(const QModelIndex &parent, int first, int last);
|
||||
|
||||
private:
|
||||
QList<Column> m_columns;
|
||||
FilterMap m_filters;
|
||||
BaseVersionList::RoleList roles;
|
||||
VersionFilterModel * filterModel;
|
||||
bool hasRecommended = false;
|
||||
bool hasLatest = false;
|
||||
QString m_currentVersion;
|
||||
};
|
85
launcher/Version_test.cpp
Normal file
85
launcher/Version_test.cpp
Normal file
@ -0,0 +1,85 @@
|
||||
/* Copyright 2013-2021 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 <QTest>
|
||||
|
||||
#include "TestUtil.h"
|
||||
#include <Version.h>
|
||||
|
||||
class ModUtilsTest : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
void setupVersions()
|
||||
{
|
||||
QTest::addColumn<QString>("first");
|
||||
QTest::addColumn<QString>("second");
|
||||
QTest::addColumn<bool>("lessThan");
|
||||
QTest::addColumn<bool>("equal");
|
||||
|
||||
QTest::newRow("equal, explicit") << "1.2.0" << "1.2.0" << false << true;
|
||||
QTest::newRow("equal, implicit 1") << "1.2" << "1.2.0" << false << true;
|
||||
QTest::newRow("equal, implicit 2") << "1.2.0" << "1.2" << false << true;
|
||||
QTest::newRow("equal, two-digit") << "1.42" << "1.42" << false << true;
|
||||
|
||||
QTest::newRow("lessThan, explicit 1") << "1.2.0" << "1.2.1" << true << false;
|
||||
QTest::newRow("lessThan, explicit 2") << "1.2.0" << "1.3.0" << true << false;
|
||||
QTest::newRow("lessThan, explicit 3") << "1.2.0" << "2.2.0" << true << false;
|
||||
QTest::newRow("lessThan, implicit 1") << "1.2" << "1.2.1" << true << false;
|
||||
QTest::newRow("lessThan, implicit 2") << "1.2" << "1.3.0" << true << false;
|
||||
QTest::newRow("lessThan, implicit 3") << "1.2" << "2.2.0" << true << false;
|
||||
QTest::newRow("lessThan, two-digit") << "1.41" << "1.42" << true << false;
|
||||
|
||||
QTest::newRow("greaterThan, explicit 1") << "1.2.1" << "1.2.0" << false << false;
|
||||
QTest::newRow("greaterThan, explicit 2") << "1.3.0" << "1.2.0" << false << false;
|
||||
QTest::newRow("greaterThan, explicit 3") << "2.2.0" << "1.2.0" << false << false;
|
||||
QTest::newRow("greaterThan, implicit 1") << "1.2.1" << "1.2" << false << false;
|
||||
QTest::newRow("greaterThan, implicit 2") << "1.3.0" << "1.2" << false << false;
|
||||
QTest::newRow("greaterThan, implicit 3") << "2.2.0" << "1.2" << false << false;
|
||||
QTest::newRow("greaterThan, two-digit") << "1.42" << "1.41" << false << false;
|
||||
}
|
||||
|
||||
private slots:
|
||||
void initTestCase()
|
||||
{
|
||||
|
||||
}
|
||||
void cleanupTestCase()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void test_versionCompare_data()
|
||||
{
|
||||
setupVersions();
|
||||
}
|
||||
void test_versionCompare()
|
||||
{
|
||||
QFETCH(QString, first);
|
||||
QFETCH(QString, second);
|
||||
QFETCH(bool, lessThan);
|
||||
QFETCH(bool, equal);
|
||||
|
||||
const auto v1 = Version(first);
|
||||
const auto v2 = Version(second);
|
||||
|
||||
QCOMPARE(v1 < v2, lessThan);
|
||||
QCOMPARE(v1 > v2, !lessThan && !equal);
|
||||
QCOMPARE(v1 == v2, equal);
|
||||
}
|
||||
};
|
||||
|
||||
QTEST_GUILESS_MAIN(ModUtilsTest)
|
||||
|
||||
#include "Version_test.moc"
|
20
launcher/WatchLock.h
Normal file
20
launcher/WatchLock.h
Normal file
@ -0,0 +1,20 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <QFileSystemWatcher>
|
||||
|
||||
struct WatchLock
|
||||
{
|
||||
WatchLock(QFileSystemWatcher * watcher, const QString& directory)
|
||||
: m_watcher(watcher), m_directory(directory)
|
||||
{
|
||||
m_watcher->removePath(m_directory);
|
||||
}
|
||||
~WatchLock()
|
||||
{
|
||||
m_watcher->addPath(m_directory);
|
||||
}
|
||||
QFileSystemWatcher * m_watcher;
|
||||
QString m_directory;
|
||||
};
|
138
launcher/dialogs/AboutDialog.cpp
Normal file
138
launcher/dialogs/AboutDialog.cpp
Normal file
@ -0,0 +1,138 @@
|
||||
/* Copyright 2013-2021 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 "AboutDialog.h"
|
||||
#include "ui_AboutDialog.h"
|
||||
#include <QIcon>
|
||||
#include "MultiMC.h"
|
||||
#include "BuildConfig.h"
|
||||
|
||||
#include <net/NetJob.h>
|
||||
|
||||
#include "HoeDown.h"
|
||||
|
||||
namespace {
|
||||
// Credits
|
||||
// This is a hack, but I can't think of a better way to do this easily without screwing with QTextDocument...
|
||||
QString getCreditsHtml(QStringList patrons)
|
||||
{
|
||||
QString patronsHeading = QObject::tr("Patrons", "About Credits");
|
||||
QString output;
|
||||
QTextStream stream(&output);
|
||||
stream << "<center>\n";
|
||||
// TODO: possibly retrieve from git history at build time?
|
||||
stream << "<h3>" << QObject::tr("MultiMC Developers", "About Credits") << "</h3>\n";
|
||||
stream << "<p>Andrew Okin <<a href='mailto:forkk@forkk.net'>forkk@forkk.net</a>></p>\n";
|
||||
stream << "<p>Petr Mrázek <<a href='mailto:peterix@gmail.com'>peterix@gmail.com</a>></p>\n";
|
||||
stream << "<p>Sky Welch <<a href='mailto:multimc@bunnies.io'>multimc@bunnies.io</a>></p>\n";
|
||||
stream << "<p>Jan (02JanDal) <<a href='mailto:02jandal@gmail.com'>02jandal@gmail.com</a>></p>\n";
|
||||
stream << "<p>RoboSky <<a href='https://twitter.com/RoboSky_'>@RoboSky_</a>></p>\n";
|
||||
stream << "<br />\n";
|
||||
|
||||
stream << "<h3>" << QObject::tr("With thanks to", "About Credits") << "</h3>\n";
|
||||
stream << "<p>Orochimarufan <<a href='mailto:orochimarufan.x3@gmail.com'>orochimarufan.x3@gmail.com</a>></p>\n";
|
||||
stream << "<p>TakSuyu <<a href='mailto:taksuyu@gmail.com'>taksuyu@gmail.com</a>></p>\n";
|
||||
stream << "<p>Kilobyte <<a href='mailto:stiepen22@gmx.de'>stiepen22@gmx.de</a>></p>\n";
|
||||
stream << "<p>Rootbear75 <<a href='https://twitter.com/rootbear75'>@rootbear75</a>></p>\n";
|
||||
stream << "<p>Zeker Zhayard <<a href='https://twitter.com/zeker_zhayard'>@Zeker_Zhayard</a>></p>\n";
|
||||
stream << "<br />\n";
|
||||
|
||||
if(!patrons.isEmpty()) {
|
||||
stream << "<h3>" << QObject::tr("Patrons", "About Credits") << "</h3>\n";
|
||||
for (QString patron : patrons)
|
||||
{
|
||||
stream << "<p>" << patron << "</p>\n";
|
||||
}
|
||||
}
|
||||
stream << "</center>\n";
|
||||
return output;
|
||||
}
|
||||
|
||||
QString getLicenseHtml()
|
||||
{
|
||||
HoeDown hoedown;
|
||||
QFile dataFile(":/documents/COPYING.md");
|
||||
dataFile.open(QIODevice::ReadOnly);
|
||||
QString output = hoedown.process(dataFile.readAll());
|
||||
return output;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
AboutDialog::AboutDialog(QWidget *parent) : QDialog(parent), ui(new Ui::AboutDialog)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
QString chtml = getCreditsHtml(QStringList());
|
||||
ui->creditsText->setHtml(chtml);
|
||||
|
||||
QString lhtml = getLicenseHtml();
|
||||
ui->licenseText->setHtml(lhtml);
|
||||
|
||||
ui->urlLabel->setOpenExternalLinks(true);
|
||||
|
||||
ui->icon->setPixmap(MMC->getThemedIcon("logo").pixmap(64));
|
||||
ui->title->setText("MultiMC 5");
|
||||
|
||||
ui->versionLabel->setText(tr("Version") +": " + BuildConfig.printableVersionString());
|
||||
ui->platformLabel->setText(tr("Platform") +": " + BuildConfig.BUILD_PLATFORM);
|
||||
|
||||
if (BuildConfig.VERSION_BUILD >= 0)
|
||||
ui->buildNumLabel->setText(tr("Build Number") +": " + QString::number(BuildConfig.VERSION_BUILD));
|
||||
else
|
||||
ui->buildNumLabel->setVisible(false);
|
||||
|
||||
if (!BuildConfig.VERSION_CHANNEL.isEmpty())
|
||||
ui->channelLabel->setText(tr("Channel") +": " + BuildConfig.VERSION_CHANNEL);
|
||||
else
|
||||
ui->channelLabel->setVisible(false);
|
||||
|
||||
ui->redistributionText->setHtml(tr(
|
||||
"<p>We keep MultiMC open source because we think it's important to be able to see the source code for a project like this, and we do so using the Apache license.</p>\n"
|
||||
"<p>Part of the reason for using the Apache license is we don't want people using the "MultiMC" name when redistributing the project. "
|
||||
"This means people must take the time to go through the source code and remove all references to "MultiMC", including but not limited to the project "
|
||||
"icon and the title of windows, (no <b>MultiMC-fork</b> in the title).</p>\n"
|
||||
"<p>The Apache license covers reasonable use for the name - a mention of the project's origins in the About dialog and the license is acceptable. "
|
||||
"However, it should be abundantly clear that the project is a fork <b>without</b> implying that you have our blessing.</p>"
|
||||
));
|
||||
|
||||
connect(ui->closeButton, SIGNAL(clicked()), SLOT(close()));
|
||||
|
||||
connect(ui->aboutQt, &QPushButton::clicked, &QApplication::aboutQt);
|
||||
|
||||
loadPatronList();
|
||||
}
|
||||
|
||||
AboutDialog::~AboutDialog()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void AboutDialog::loadPatronList()
|
||||
{
|
||||
netJob.reset(new NetJob("Patreon Patron List"));
|
||||
netJob->addNetAction(Net::Download::makeByteArray(QUrl("https://files.multimc.org/patrons.txt"), &dataSink));
|
||||
connect(netJob.get(), &NetJob::succeeded, this, &AboutDialog::patronListLoaded);
|
||||
netJob->start();
|
||||
}
|
||||
|
||||
void AboutDialog::patronListLoaded()
|
||||
{
|
||||
QString patronListStr(dataSink);
|
||||
dataSink.clear();
|
||||
QString html = getCreditsHtml(patronListStr.split("\n", QString::SkipEmptyParts));
|
||||
ui->creditsText->setHtml(html);
|
||||
}
|
||||
|
47
launcher/dialogs/AboutDialog.h
Normal file
47
launcher/dialogs/AboutDialog.h
Normal file
@ -0,0 +1,47 @@
|
||||
/* Copyright 2013-2021 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 <QDialog>
|
||||
#include <net/NetJob.h>
|
||||
|
||||
namespace Ui
|
||||
{
|
||||
class AboutDialog;
|
||||
}
|
||||
|
||||
class AboutDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit AboutDialog(QWidget *parent = 0);
|
||||
~AboutDialog();
|
||||
|
||||
public
|
||||
slots:
|
||||
/// Starts loading a list of Patreon patrons.
|
||||
void loadPatronList();
|
||||
|
||||
/// Slot for when the patron list loads successfully.
|
||||
void patronListLoaded();
|
||||
|
||||
private:
|
||||
Ui::AboutDialog *ui;
|
||||
|
||||
NetJobPtr netJob;
|
||||
QByteArray dataSink;
|
||||
};
|
312
launcher/dialogs/AboutDialog.ui
Normal file
312
launcher/dialogs/AboutDialog.ui
Normal file
@ -0,0 +1,312 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>AboutDialog</class>
|
||||
<widget class="QDialog" name="AboutDialog">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>783</width>
|
||||
<height>843</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>450</width>
|
||||
<height>400</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>About MultiMC</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<spacer name="horizontalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="icon">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>64</width>
|
||||
<height>64</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="baseSize">
|
||||
<size>
|
||||
<width>64</width>
|
||||
<height>64</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="horizontalSpacer_2">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="title">
|
||||
<property name="font">
|
||||
<font>
|
||||
<pointsize>15</pointsize>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string notr="true">MultiMC 5</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QTabWidget" name="tabWidget">
|
||||
<property name="currentIndex">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<widget class="QWidget" name="aboutTab">
|
||||
<attribute name="title">
|
||||
<string>About</string>
|
||||
</attribute>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_5">
|
||||
<item>
|
||||
<widget class="QLabel" name="versionLabel">
|
||||
<property name="text">
|
||||
<string>Version:</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="platformLabel">
|
||||
<property name="text">
|
||||
<string>Platform:</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="buildNumLabel">
|
||||
<property name="text">
|
||||
<string>Build Number:</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="channelLabel">
|
||||
<property name="text">
|
||||
<string>Channel:</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="aboutLabel">
|
||||
<property name="enabled">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string><html><head/><body><p>MultiMC is a custom launcher that makes managing Minecraft easier by allowing you to have multiple instances of Minecraft at once.</p></body></html></string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
</property>
|
||||
<property name="wordWrap">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="copyLabel">
|
||||
<property name="font">
|
||||
<font>
|
||||
<pointsize>8</pointsize>
|
||||
<kerning>true</kerning>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>© 2012-2021 MultiMC Contributors</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="urlLabel">
|
||||
<property name="font">
|
||||
<font>
|
||||
<pointsize>10</pointsize>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string notr="true"><html><head/><body><p><a href="https://github.com/MultiMC/MultiMC5">https://github.com/MultiMC/MultiMC5</a></p></body></html></string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="verticalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>212</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="creditsTab">
|
||||
<attribute name="title">
|
||||
<string>Credits</string>
|
||||
</attribute>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_2">
|
||||
<item>
|
||||
<widget class="QTextEdit" name="creditsText">
|
||||
<property name="readOnly">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="textInteractionFlags">
|
||||
<set>Qt::TextBrowserInteraction</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="licenseTab">
|
||||
<attribute name="title">
|
||||
<string>License</string>
|
||||
</attribute>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_3">
|
||||
<item>
|
||||
<widget class="QTextEdit" name="licenseText">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="font">
|
||||
<font>
|
||||
<family>DejaVu Sans Mono</family>
|
||||
</font>
|
||||
</property>
|
||||
<property name="readOnly">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="textInteractionFlags">
|
||||
<set>Qt::TextBrowserInteraction</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="forkingTab">
|
||||
<attribute name="title">
|
||||
<string>Forking/Redistribution</string>
|
||||
</attribute>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_4">
|
||||
<item>
|
||||
<widget class="QTextEdit" name="redistributionText">
|
||||
<property name="textInteractionFlags">
|
||||
<set>Qt::LinksAccessibleByKeyboard|Qt::LinksAccessibleByMouse|Qt::TextBrowserInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||
<item>
|
||||
<widget class="QPushButton" name="aboutQt">
|
||||
<property name="autoFillBackground">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>About Qt</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="horizontalSpacer_3">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="closeButton">
|
||||
<property name="text">
|
||||
<string>Close</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<tabstops>
|
||||
<tabstop>tabWidget</tabstop>
|
||||
<tabstop>creditsText</tabstop>
|
||||
<tabstop>licenseText</tabstop>
|
||||
<tabstop>redistributionText</tabstop>
|
||||
<tabstop>aboutQt</tabstop>
|
||||
<tabstop>closeButton</tabstop>
|
||||
</tabstops>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
144
launcher/dialogs/CopyInstanceDialog.cpp
Normal file
144
launcher/dialogs/CopyInstanceDialog.cpp
Normal file
@ -0,0 +1,144 @@
|
||||
/* Copyright 2013-2021 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 <QLayout>
|
||||
#include <QPushButton>
|
||||
|
||||
#include "MultiMC.h"
|
||||
#include "CopyInstanceDialog.h"
|
||||
#include "ui_CopyInstanceDialog.h"
|
||||
|
||||
#include "dialogs/IconPickerDialog.h"
|
||||
|
||||
#include "BaseVersion.h"
|
||||
#include "icons/IconList.h"
|
||||
#include "tasks/Task.h"
|
||||
#include "BaseInstance.h"
|
||||
#include "InstanceList.h"
|
||||
|
||||
CopyInstanceDialog::CopyInstanceDialog(InstancePtr original, QWidget *parent)
|
||||
:QDialog(parent), ui(new Ui::CopyInstanceDialog), m_original(original)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
resize(minimumSizeHint());
|
||||
layout()->setSizeConstraint(QLayout::SetFixedSize);
|
||||
|
||||
InstIconKey = original->iconKey();
|
||||
ui->iconButton->setIcon(MMC->icons()->getIcon(InstIconKey));
|
||||
ui->instNameTextBox->setText(original->name());
|
||||
ui->instNameTextBox->setFocus();
|
||||
auto groups = MMC->instances()->getGroups().toSet();
|
||||
auto groupList = QStringList(groups.toList());
|
||||
groupList.sort(Qt::CaseInsensitive);
|
||||
groupList.removeOne("");
|
||||
groupList.push_front("");
|
||||
ui->groupBox->addItems(groupList);
|
||||
int index = groupList.indexOf(MMC->instances()->getInstanceGroup(m_original->id()));
|
||||
if(index == -1)
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
ui->groupBox->setCurrentIndex(index);
|
||||
ui->groupBox->lineEdit()->setPlaceholderText(tr("No group"));
|
||||
ui->copySavesCheckbox->setChecked(m_copySaves);
|
||||
ui->keepPlaytimeCheckbox->setChecked(m_keepPlaytime);
|
||||
}
|
||||
|
||||
CopyInstanceDialog::~CopyInstanceDialog()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void CopyInstanceDialog::updateDialogState()
|
||||
{
|
||||
auto allowOK = !instName().isEmpty();
|
||||
auto OkButton = ui->buttonBox->button(QDialogButtonBox::Ok);
|
||||
if(OkButton->isEnabled() != allowOK)
|
||||
{
|
||||
OkButton->setEnabled(allowOK);
|
||||
}
|
||||
}
|
||||
|
||||
QString CopyInstanceDialog::instName() const
|
||||
{
|
||||
auto result = ui->instNameTextBox->text().trimmed();
|
||||
if(result.size())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
QString CopyInstanceDialog::iconKey() const
|
||||
{
|
||||
return InstIconKey;
|
||||
}
|
||||
|
||||
QString CopyInstanceDialog::instGroup() const
|
||||
{
|
||||
return ui->groupBox->currentText();
|
||||
}
|
||||
|
||||
void CopyInstanceDialog::on_iconButton_clicked()
|
||||
{
|
||||
IconPickerDialog dlg(this);
|
||||
dlg.execWithSelection(InstIconKey);
|
||||
|
||||
if (dlg.result() == QDialog::Accepted)
|
||||
{
|
||||
InstIconKey = dlg.selectedIconKey;
|
||||
ui->iconButton->setIcon(MMC->icons()->getIcon(InstIconKey));
|
||||
}
|
||||
}
|
||||
|
||||
void CopyInstanceDialog::on_instNameTextBox_textChanged(const QString &arg1)
|
||||
{
|
||||
updateDialogState();
|
||||
}
|
||||
|
||||
bool CopyInstanceDialog::shouldCopySaves() const
|
||||
{
|
||||
return m_copySaves;
|
||||
}
|
||||
|
||||
void CopyInstanceDialog::on_copySavesCheckbox_stateChanged(int state)
|
||||
{
|
||||
if(state == Qt::Unchecked)
|
||||
{
|
||||
m_copySaves = false;
|
||||
}
|
||||
else if(state == Qt::Checked)
|
||||
{
|
||||
m_copySaves = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool CopyInstanceDialog::shouldKeepPlaytime() const
|
||||
{
|
||||
return m_keepPlaytime;
|
||||
}
|
||||
|
||||
|
||||
void CopyInstanceDialog::on_keepPlaytimeCheckbox_stateChanged(int state)
|
||||
{
|
||||
if(state == Qt::Unchecked)
|
||||
{
|
||||
m_keepPlaytime = false;
|
||||
}
|
||||
else if(state == Qt::Checked)
|
||||
{
|
||||
m_keepPlaytime = true;
|
||||
}
|
||||
}
|
58
launcher/dialogs/CopyInstanceDialog.h
Normal file
58
launcher/dialogs/CopyInstanceDialog.h
Normal file
@ -0,0 +1,58 @@
|
||||
/* Copyright 2013-2021 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 <QDialog>
|
||||
#include "BaseVersion.h"
|
||||
#include <BaseInstance.h>
|
||||
|
||||
class BaseInstance;
|
||||
|
||||
namespace Ui
|
||||
{
|
||||
class CopyInstanceDialog;
|
||||
}
|
||||
|
||||
class CopyInstanceDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit CopyInstanceDialog(InstancePtr original, QWidget *parent = 0);
|
||||
~CopyInstanceDialog();
|
||||
|
||||
void updateDialogState();
|
||||
|
||||
QString instName() const;
|
||||
QString instGroup() const;
|
||||
QString iconKey() const;
|
||||
bool shouldCopySaves() const;
|
||||
bool shouldKeepPlaytime() const;
|
||||
|
||||
private
|
||||
slots:
|
||||
void on_iconButton_clicked();
|
||||
void on_instNameTextBox_textChanged(const QString &arg1);
|
||||
void on_copySavesCheckbox_stateChanged(int state);
|
||||
void on_keepPlaytimeCheckbox_stateChanged(int state);
|
||||
|
||||
private:
|
||||
Ui::CopyInstanceDialog *ui;
|
||||
QString InstIconKey;
|
||||
InstancePtr m_original;
|
||||
bool m_copySaves = true;
|
||||
bool m_keepPlaytime = true;
|
||||
};
|
182
launcher/dialogs/CopyInstanceDialog.ui
Normal file
182
launcher/dialogs/CopyInstanceDialog.ui
Normal file
@ -0,0 +1,182 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>CopyInstanceDialog</class>
|
||||
<widget class="QDialog" name="CopyInstanceDialog">
|
||||
<property name="windowModality">
|
||||
<enum>Qt::ApplicationModal</enum>
|
||||
</property>
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>345</width>
|
||||
<height>323</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Copy Instance</string>
|
||||
</property>
|
||||
<property name="windowIcon">
|
||||
<iconset>
|
||||
<normaloff>:/icons/toolbar/copy</normaloff>:/icons/toolbar/copy</iconset>
|
||||
</property>
|
||||
<property name="modal">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="iconBtnLayout">
|
||||
<item>
|
||||
<spacer name="iconBtnLeftSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QToolButton" name="iconButton">
|
||||
<property name="icon">
|
||||
<iconset>
|
||||
<normaloff>:/icons/instances/infinity</normaloff>:/icons/instances/infinity</iconset>
|
||||
</property>
|
||||
<property name="iconSize">
|
||||
<size>
|
||||
<width>80</width>
|
||||
<height>80</height>
|
||||
</size>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="iconBtnRightSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="instNameTextBox">
|
||||
<property name="placeholderText">
|
||||
<string>Name</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="Line" name="line">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="labelVersion_3">
|
||||
<property name="text">
|
||||
<string>&Group</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>groupBox</cstring>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1" colspan="2">
|
||||
<widget class="QComboBox" name="groupBox">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="editable">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QCheckBox" name="copySavesCheckbox">
|
||||
<property name="text">
|
||||
<string>Copy saves</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QCheckBox" name="keepPlaytimeCheckbox">
|
||||
<property name="text">
|
||||
<string>Keep play time</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QDialogButtonBox" name="buttonBox">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="standardButtons">
|
||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<tabstops>
|
||||
<tabstop>iconButton</tabstop>
|
||||
<tabstop>instNameTextBox</tabstop>
|
||||
<tabstop>groupBox</tabstop>
|
||||
<tabstop>copySavesCheckbox</tabstop>
|
||||
<tabstop>keepPlaytimeCheckbox</tabstop>
|
||||
</tabstops>
|
||||
<resources>
|
||||
<include location="../../graphics.qrc"/>
|
||||
</resources>
|
||||
<connections>
|
||||
<connection>
|
||||
<sender>buttonBox</sender>
|
||||
<signal>accepted()</signal>
|
||||
<receiver>CopyInstanceDialog</receiver>
|
||||
<slot>accept()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>248</x>
|
||||
<y>254</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>157</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>buttonBox</sender>
|
||||
<signal>rejected()</signal>
|
||||
<receiver>CopyInstanceDialog</receiver>
|
||||
<slot>reject()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>316</x>
|
||||
<y>260</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>286</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
</ui>
|
35
launcher/dialogs/CustomMessageBox.cpp
Normal file
35
launcher/dialogs/CustomMessageBox.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
/* Copyright 2013-2021 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 "CustomMessageBox.h"
|
||||
|
||||
namespace CustomMessageBox
|
||||
{
|
||||
QMessageBox *selectable(QWidget *parent, const QString &title, const QString &text,
|
||||
QMessageBox::Icon icon, QMessageBox::StandardButtons buttons,
|
||||
QMessageBox::StandardButton defaultButton)
|
||||
{
|
||||
QMessageBox *messageBox = new QMessageBox(parent);
|
||||
messageBox->setWindowTitle(title);
|
||||
messageBox->setText(text);
|
||||
messageBox->setStandardButtons(buttons);
|
||||
messageBox->setDefaultButton(defaultButton);
|
||||
messageBox->setTextInteractionFlags(Qt::TextSelectableByMouse);
|
||||
messageBox->setIcon(icon);
|
||||
messageBox->setTextInteractionFlags(Qt::TextBrowserInteraction);
|
||||
|
||||
return messageBox;
|
||||
}
|
||||
}
|
26
launcher/dialogs/CustomMessageBox.h
Normal file
26
launcher/dialogs/CustomMessageBox.h
Normal file
@ -0,0 +1,26 @@
|
||||
/* Copyright 2013-2021 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 <QMessageBox>
|
||||
|
||||
namespace CustomMessageBox
|
||||
{
|
||||
QMessageBox *selectable(QWidget *parent, const QString &title, const QString &text,
|
||||
QMessageBox::Icon icon = QMessageBox::NoIcon,
|
||||
QMessageBox::StandardButtons buttons = QMessageBox::Ok,
|
||||
QMessageBox::StandardButton defaultButton = QMessageBox::NoButton);
|
||||
}
|
61
launcher/dialogs/EditAccountDialog.cpp
Normal file
61
launcher/dialogs/EditAccountDialog.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
/* Copyright 2013-2021 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 "EditAccountDialog.h"
|
||||
#include "ui_EditAccountDialog.h"
|
||||
#include <DesktopServices.h>
|
||||
#include <QUrl>
|
||||
|
||||
EditAccountDialog::EditAccountDialog(const QString &text, QWidget *parent, int flags)
|
||||
: QDialog(parent), ui(new Ui::EditAccountDialog)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
ui->label->setText(text);
|
||||
ui->label->setVisible(!text.isEmpty());
|
||||
|
||||
ui->userTextBox->setEnabled(flags & UsernameField);
|
||||
ui->passTextBox->setEnabled(flags & PasswordField);
|
||||
}
|
||||
|
||||
EditAccountDialog::~EditAccountDialog()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void EditAccountDialog::on_label_linkActivated(const QString &link)
|
||||
{
|
||||
DesktopServices::openUrl(QUrl(link));
|
||||
}
|
||||
|
||||
void EditAccountDialog::setUsername(const QString & user) const
|
||||
{
|
||||
ui->userTextBox->setText(user);
|
||||
}
|
||||
|
||||
QString EditAccountDialog::username() const
|
||||
{
|
||||
return ui->userTextBox->text();
|
||||
}
|
||||
|
||||
void EditAccountDialog::setPassword(const QString & pass) const
|
||||
{
|
||||
ui->passTextBox->setText(pass);
|
||||
}
|
||||
|
||||
QString EditAccountDialog::password() const
|
||||
{
|
||||
return ui->passTextBox->text();
|
||||
}
|
56
launcher/dialogs/EditAccountDialog.h
Normal file
56
launcher/dialogs/EditAccountDialog.h
Normal file
@ -0,0 +1,56 @@
|
||||
/* Copyright 2013-2021 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 <QDialog>
|
||||
|
||||
namespace Ui
|
||||
{
|
||||
class EditAccountDialog;
|
||||
}
|
||||
|
||||
class EditAccountDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit EditAccountDialog(const QString &text = "", QWidget *parent = 0,
|
||||
int flags = UsernameField | PasswordField);
|
||||
~EditAccountDialog();
|
||||
|
||||
void setUsername(const QString & user) const;
|
||||
void setPassword(const QString & pass) const;
|
||||
|
||||
QString username() const;
|
||||
QString password() const;
|
||||
|
||||
enum Flags
|
||||
{
|
||||
NoFlags = 0,
|
||||
|
||||
//! Specifies that the dialog should have a username field.
|
||||
UsernameField,
|
||||
|
||||
//! Specifies that the dialog should have a password field.
|
||||
PasswordField,
|
||||
};
|
||||
|
||||
private slots:
|
||||
void on_label_linkActivated(const QString &link);
|
||||
|
||||
private:
|
||||
Ui::EditAccountDialog *ui;
|
||||
};
|
94
launcher/dialogs/EditAccountDialog.ui
Normal file
94
launcher/dialogs/EditAccountDialog.ui
Normal file
@ -0,0 +1,94 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>EditAccountDialog</class>
|
||||
<widget class="QDialog" name="EditAccountDialog">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>400</width>
|
||||
<height>148</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Login</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string notr="true">Message label placeholder.</string>
|
||||
</property>
|
||||
<property name="textFormat">
|
||||
<enum>Qt::RichText</enum>
|
||||
</property>
|
||||
<property name="textInteractionFlags">
|
||||
<set>Qt::LinksAccessibleByKeyboard|Qt::LinksAccessibleByMouse|Qt::TextBrowserInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="userTextBox">
|
||||
<property name="placeholderText">
|
||||
<string>Email</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="passTextBox">
|
||||
<property name="echoMode">
|
||||
<enum>QLineEdit::Password</enum>
|
||||
</property>
|
||||
<property name="placeholderText">
|
||||
<string>Password</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QDialogButtonBox" name="buttonBox">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="standardButtons">
|
||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections>
|
||||
<connection>
|
||||
<sender>buttonBox</sender>
|
||||
<signal>accepted()</signal>
|
||||
<receiver>EditAccountDialog</receiver>
|
||||
<slot>accept()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>248</x>
|
||||
<y>254</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>157</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>buttonBox</sender>
|
||||
<signal>rejected()</signal>
|
||||
<receiver>EditAccountDialog</receiver>
|
||||
<slot>reject()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>316</x>
|
||||
<y>260</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>286</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
</ui>
|
482
launcher/dialogs/ExportInstanceDialog.cpp
Normal file
482
launcher/dialogs/ExportInstanceDialog.cpp
Normal file
@ -0,0 +1,482 @@
|
||||
/* Copyright 2013-2021 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 "ExportInstanceDialog.h"
|
||||
#include "ui_ExportInstanceDialog.h"
|
||||
#include <BaseInstance.h>
|
||||
#include <MMCZip.h>
|
||||
#include <QFileDialog>
|
||||
#include <QMessageBox>
|
||||
#include <qfilesystemmodel.h>
|
||||
|
||||
#include <QSortFilterProxyModel>
|
||||
#include <QDebug>
|
||||
#include <qstack.h>
|
||||
#include <QSaveFile>
|
||||
#include "MMCStrings.h"
|
||||
#include "SeparatorPrefixTree.h"
|
||||
#include "MultiMC.h"
|
||||
#include <icons/IconList.h>
|
||||
#include <FileSystem.h>
|
||||
|
||||
class PackIgnoreProxy : public QSortFilterProxyModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
PackIgnoreProxy(InstancePtr instance, QObject *parent) : QSortFilterProxyModel(parent)
|
||||
{
|
||||
m_instance = instance;
|
||||
}
|
||||
// NOTE: Sadly, we have to do sorting ourselves.
|
||||
bool lessThan(const QModelIndex &left, const QModelIndex &right) const
|
||||
{
|
||||
QFileSystemModel *fsm = qobject_cast<QFileSystemModel *>(sourceModel());
|
||||
if (!fsm)
|
||||
{
|
||||
return QSortFilterProxyModel::lessThan(left, right);
|
||||
}
|
||||
bool asc = sortOrder() == Qt::AscendingOrder ? true : false;
|
||||
|
||||
QFileInfo leftFileInfo = fsm->fileInfo(left);
|
||||
QFileInfo rightFileInfo = fsm->fileInfo(right);
|
||||
|
||||
if (!leftFileInfo.isDir() && rightFileInfo.isDir())
|
||||
{
|
||||
return !asc;
|
||||
}
|
||||
if (leftFileInfo.isDir() && !rightFileInfo.isDir())
|
||||
{
|
||||
return asc;
|
||||
}
|
||||
|
||||
// sort and proxy model breaks the original model...
|
||||
if (sortColumn() == 0)
|
||||
{
|
||||
return Strings::naturalCompare(leftFileInfo.fileName(), rightFileInfo.fileName(),
|
||||
Qt::CaseInsensitive) < 0;
|
||||
}
|
||||
if (sortColumn() == 1)
|
||||
{
|
||||
auto leftSize = leftFileInfo.size();
|
||||
auto rightSize = rightFileInfo.size();
|
||||
if ((leftSize == rightSize) || (leftFileInfo.isDir() && rightFileInfo.isDir()))
|
||||
{
|
||||
return Strings::naturalCompare(leftFileInfo.fileName(),
|
||||
rightFileInfo.fileName(),
|
||||
Qt::CaseInsensitive) < 0
|
||||
? asc
|
||||
: !asc;
|
||||
}
|
||||
return leftSize < rightSize;
|
||||
}
|
||||
return QSortFilterProxyModel::lessThan(left, right);
|
||||
}
|
||||
|
||||
virtual Qt::ItemFlags flags(const QModelIndex &index) const
|
||||
{
|
||||
if (!index.isValid())
|
||||
return Qt::NoItemFlags;
|
||||
|
||||
auto sourceIndex = mapToSource(index);
|
||||
Qt::ItemFlags flags = sourceIndex.flags();
|
||||
if (index.column() == 0)
|
||||
{
|
||||
flags |= Qt::ItemIsUserCheckable;
|
||||
if (sourceIndex.model()->hasChildren(sourceIndex))
|
||||
{
|
||||
flags |= Qt::ItemIsTristate;
|
||||
}
|
||||
}
|
||||
|
||||
return flags;
|
||||
}
|
||||
|
||||
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
|
||||
{
|
||||
QModelIndex sourceIndex = mapToSource(index);
|
||||
|
||||
if (index.column() == 0 && role == Qt::CheckStateRole)
|
||||
{
|
||||
QFileSystemModel *fsm = qobject_cast<QFileSystemModel *>(sourceModel());
|
||||
auto blockedPath = relPath(fsm->filePath(sourceIndex));
|
||||
auto cover = blocked.cover(blockedPath);
|
||||
if (!cover.isNull())
|
||||
{
|
||||
return QVariant(Qt::Unchecked);
|
||||
}
|
||||
else if (blocked.exists(blockedPath))
|
||||
{
|
||||
return QVariant(Qt::PartiallyChecked);
|
||||
}
|
||||
else
|
||||
{
|
||||
return QVariant(Qt::Checked);
|
||||
}
|
||||
}
|
||||
|
||||
return sourceIndex.data(role);
|
||||
}
|
||||
|
||||
virtual bool setData(const QModelIndex &index, const QVariant &value,
|
||||
int role = Qt::EditRole)
|
||||
{
|
||||
if (index.column() == 0 && role == Qt::CheckStateRole)
|
||||
{
|
||||
Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt());
|
||||
return setFilterState(index, state);
|
||||
}
|
||||
|
||||
QModelIndex sourceIndex = mapToSource(index);
|
||||
return QSortFilterProxyModel::sourceModel()->setData(sourceIndex, value, role);
|
||||
}
|
||||
|
||||
QString relPath(const QString &path) const
|
||||
{
|
||||
QString prefix = QDir().absoluteFilePath(m_instance->instanceRoot());
|
||||
prefix += '/';
|
||||
if (!path.startsWith(prefix))
|
||||
{
|
||||
return QString();
|
||||
}
|
||||
return path.mid(prefix.size());
|
||||
}
|
||||
|
||||
bool setFilterState(QModelIndex index, Qt::CheckState state)
|
||||
{
|
||||
QFileSystemModel *fsm = qobject_cast<QFileSystemModel *>(sourceModel());
|
||||
|
||||
if (!fsm)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
QModelIndex sourceIndex = mapToSource(index);
|
||||
auto blockedPath = relPath(fsm->filePath(sourceIndex));
|
||||
bool changed = false;
|
||||
if (state == Qt::Unchecked)
|
||||
{
|
||||
// blocking a path
|
||||
auto &node = blocked.insert(blockedPath);
|
||||
// get rid of all blocked nodes below
|
||||
node.clear();
|
||||
changed = true;
|
||||
}
|
||||
else if (state == Qt::Checked || state == Qt::PartiallyChecked)
|
||||
{
|
||||
if (!blocked.remove(blockedPath))
|
||||
{
|
||||
auto cover = blocked.cover(blockedPath);
|
||||
qDebug() << "Blocked by cover" << cover;
|
||||
// uncover
|
||||
blocked.remove(cover);
|
||||
// block all contents, except for any cover
|
||||
QModelIndex rootIndex =
|
||||
fsm->index(FS::PathCombine(m_instance->instanceRoot(), cover));
|
||||
QModelIndex doing = rootIndex;
|
||||
int row = 0;
|
||||
QStack<QModelIndex> todo;
|
||||
while (1)
|
||||
{
|
||||
auto node = doing.child(row, 0);
|
||||
if (!node.isValid())
|
||||
{
|
||||
if (!todo.size())
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
doing = todo.pop();
|
||||
row = 0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
auto relpath = relPath(fsm->filePath(node));
|
||||
if (blockedPath.startsWith(relpath)) // cover found?
|
||||
{
|
||||
// continue processing cover later
|
||||
todo.push(node);
|
||||
}
|
||||
else
|
||||
{
|
||||
// or just block this one.
|
||||
blocked.insert(relpath);
|
||||
}
|
||||
row++;
|
||||
}
|
||||
}
|
||||
changed = true;
|
||||
}
|
||||
if (changed)
|
||||
{
|
||||
// update the thing
|
||||
emit dataChanged(index, index, {Qt::CheckStateRole});
|
||||
// update everything above index
|
||||
QModelIndex up = index.parent();
|
||||
while (1)
|
||||
{
|
||||
if (!up.isValid())
|
||||
break;
|
||||
emit dataChanged(up, up, {Qt::CheckStateRole});
|
||||
up = up.parent();
|
||||
}
|
||||
// and everything below the index
|
||||
QModelIndex doing = index;
|
||||
int row = 0;
|
||||
QStack<QModelIndex> todo;
|
||||
while (1)
|
||||
{
|
||||
auto node = doing.child(row, 0);
|
||||
if (!node.isValid())
|
||||
{
|
||||
if (!todo.size())
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
doing = todo.pop();
|
||||
row = 0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
emit dataChanged(node, node, {Qt::CheckStateRole});
|
||||
todo.push(node);
|
||||
row++;
|
||||
}
|
||||
// siblings and unrelated nodes are ignored
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool shouldExpand(QModelIndex index)
|
||||
{
|
||||
QModelIndex sourceIndex = mapToSource(index);
|
||||
QFileSystemModel *fsm = qobject_cast<QFileSystemModel *>(sourceModel());
|
||||
if (!fsm)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
auto blockedPath = relPath(fsm->filePath(sourceIndex));
|
||||
auto found = blocked.find(blockedPath);
|
||||
if(found)
|
||||
{
|
||||
return !found->leaf();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void setBlockedPaths(QStringList paths)
|
||||
{
|
||||
beginResetModel();
|
||||
blocked.clear();
|
||||
blocked.insert(paths);
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
const SeparatorPrefixTree<'/'> & blockedPaths() const
|
||||
{
|
||||
return blocked;
|
||||
}
|
||||
|
||||
protected:
|
||||
bool filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const
|
||||
{
|
||||
Q_UNUSED(source_parent)
|
||||
|
||||
// adjust the columns you want to filter out here
|
||||
// return false for those that will be hidden
|
||||
if (source_column == 2 || source_column == 3)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
InstancePtr m_instance;
|
||||
SeparatorPrefixTree<'/'> blocked;
|
||||
};
|
||||
|
||||
ExportInstanceDialog::ExportInstanceDialog(InstancePtr instance, QWidget *parent)
|
||||
: QDialog(parent), ui(new Ui::ExportInstanceDialog), m_instance(instance)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
auto model = new QFileSystemModel(this);
|
||||
proxyModel = new PackIgnoreProxy(m_instance, this);
|
||||
loadPackIgnore();
|
||||
proxyModel->setSourceModel(model);
|
||||
auto root = instance->instanceRoot();
|
||||
ui->treeView->setModel(proxyModel);
|
||||
ui->treeView->setRootIndex(proxyModel->mapFromSource(model->index(root)));
|
||||
ui->treeView->sortByColumn(0, Qt::AscendingOrder);
|
||||
|
||||
connect(proxyModel, SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(rowsInserted(QModelIndex,int,int)));
|
||||
|
||||
model->setFilter(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden);
|
||||
model->setRootPath(root);
|
||||
auto headerView = ui->treeView->header();
|
||||
headerView->setSectionResizeMode(QHeaderView::ResizeToContents);
|
||||
headerView->setSectionResizeMode(0, QHeaderView::Stretch);
|
||||
}
|
||||
|
||||
ExportInstanceDialog::~ExportInstanceDialog()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
/// Save icon to instance's folder is needed
|
||||
void SaveIcon(InstancePtr m_instance)
|
||||
{
|
||||
auto iconKey = m_instance->iconKey();
|
||||
auto iconList = MMC->icons();
|
||||
auto mmcIcon = iconList->icon(iconKey);
|
||||
if(!mmcIcon || mmcIcon->isBuiltIn()) {
|
||||
return;
|
||||
}
|
||||
auto path = mmcIcon->getFilePath();
|
||||
if(!path.isNull()) {
|
||||
QFileInfo inInfo (path);
|
||||
FS::copy(path, FS::PathCombine(m_instance->instanceRoot(), inInfo.fileName())) ();
|
||||
return;
|
||||
}
|
||||
auto & image = mmcIcon->m_images[mmcIcon->type()];
|
||||
auto & icon = image.icon;
|
||||
auto sizes = icon.availableSizes();
|
||||
if(sizes.size() == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
auto areaOf = [](QSize size)
|
||||
{
|
||||
return size.width() * size.height();
|
||||
};
|
||||
QSize largest = sizes[0];
|
||||
// find variant with largest area
|
||||
for(auto size: sizes)
|
||||
{
|
||||
if(areaOf(largest) < areaOf(size))
|
||||
{
|
||||
largest = size;
|
||||
}
|
||||
}
|
||||
auto pixmap = icon.pixmap(largest);
|
||||
pixmap.save(FS::PathCombine(m_instance->instanceRoot(), iconKey + ".png"));
|
||||
}
|
||||
|
||||
bool ExportInstanceDialog::doExport()
|
||||
{
|
||||
auto name = FS::RemoveInvalidFilenameChars(m_instance->name());
|
||||
|
||||
const QString output = QFileDialog::getSaveFileName(
|
||||
this, tr("Export %1").arg(m_instance->name()),
|
||||
FS::PathCombine(QDir::homePath(), name + ".zip"), "Zip (*.zip)", nullptr, QFileDialog::DontConfirmOverwrite);
|
||||
if (output.isEmpty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (QFile::exists(output))
|
||||
{
|
||||
int ret =
|
||||
QMessageBox::question(this, tr("Overwrite?"),
|
||||
tr("This file already exists. Do you want to overwrite it?"),
|
||||
QMessageBox::No, QMessageBox::Yes);
|
||||
if (ret == QMessageBox::No)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
SaveIcon(m_instance);
|
||||
|
||||
auto & blocked = proxyModel->blockedPaths();
|
||||
using std::placeholders::_1;
|
||||
if (!JlCompress::compressDir(output, m_instance->instanceRoot(), name, std::bind(&SeparatorPrefixTree<'/'>::covers, blocked, _1)))
|
||||
{
|
||||
QMessageBox::warning(this, tr("Error"), tr("Unable to export instance"));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void ExportInstanceDialog::done(int result)
|
||||
{
|
||||
savePackIgnore();
|
||||
if (result == QDialog::Accepted)
|
||||
{
|
||||
if (doExport())
|
||||
{
|
||||
QDialog::done(QDialog::Accepted);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
QDialog::done(result);
|
||||
}
|
||||
|
||||
void ExportInstanceDialog::rowsInserted(QModelIndex parent, int top, int bottom)
|
||||
{
|
||||
//WARNING: possible off-by-one?
|
||||
for(int i = top; i < bottom; i++)
|
||||
{
|
||||
auto node = parent.child(i, 0);
|
||||
if(proxyModel->shouldExpand(node))
|
||||
{
|
||||
auto expNode = node.parent();
|
||||
if(!expNode.isValid())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
ui->treeView->expand(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QString ExportInstanceDialog::ignoreFileName()
|
||||
{
|
||||
return FS::PathCombine(m_instance->instanceRoot(), ".packignore");
|
||||
}
|
||||
|
||||
void ExportInstanceDialog::loadPackIgnore()
|
||||
{
|
||||
auto filename = ignoreFileName();
|
||||
QFile ignoreFile(filename);
|
||||
if(!ignoreFile.open(QIODevice::ReadOnly))
|
||||
{
|
||||
return;
|
||||
}
|
||||
auto data = ignoreFile.readAll();
|
||||
auto string = QString::fromUtf8(data);
|
||||
proxyModel->setBlockedPaths(string.split('\n', QString::SkipEmptyParts));
|
||||
}
|
||||
|
||||
void ExportInstanceDialog::savePackIgnore()
|
||||
{
|
||||
auto data = proxyModel->blockedPaths().toStringList().join('\n').toUtf8();
|
||||
auto filename = ignoreFileName();
|
||||
try
|
||||
{
|
||||
FS::write(filename, data);
|
||||
}
|
||||
catch (const Exception &e)
|
||||
{
|
||||
qWarning() << e.cause();
|
||||
}
|
||||
}
|
||||
|
||||
#include "ExportInstanceDialog.moc"
|
54
launcher/dialogs/ExportInstanceDialog.h
Normal file
54
launcher/dialogs/ExportInstanceDialog.h
Normal file
@ -0,0 +1,54 @@
|
||||
/* Copyright 2013-2021 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 <QDialog>
|
||||
#include <QModelIndex>
|
||||
#include <memory>
|
||||
|
||||
class BaseInstance;
|
||||
class PackIgnoreProxy;
|
||||
typedef std::shared_ptr<BaseInstance> InstancePtr;
|
||||
|
||||
namespace Ui
|
||||
{
|
||||
class ExportInstanceDialog;
|
||||
}
|
||||
|
||||
class ExportInstanceDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit ExportInstanceDialog(InstancePtr instance, QWidget *parent = 0);
|
||||
~ExportInstanceDialog();
|
||||
|
||||
virtual void done(int result);
|
||||
|
||||
private:
|
||||
bool doExport();
|
||||
void loadPackIgnore();
|
||||
void savePackIgnore();
|
||||
QString ignoreFileName();
|
||||
|
||||
private:
|
||||
Ui::ExportInstanceDialog *ui;
|
||||
InstancePtr m_instance;
|
||||
PackIgnoreProxy * proxyModel;
|
||||
|
||||
private slots:
|
||||
void rowsInserted(QModelIndex parent, int top, int bottom);
|
||||
};
|
83
launcher/dialogs/ExportInstanceDialog.ui
Normal file
83
launcher/dialogs/ExportInstanceDialog.ui
Normal file
@ -0,0 +1,83 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>ExportInstanceDialog</class>
|
||||
<widget class="QDialog" name="ExportInstanceDialog">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>720</width>
|
||||
<height>625</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Export Instance</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QTreeView" name="treeView">
|
||||
<property name="alternatingRowColors">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="selectionMode">
|
||||
<enum>QAbstractItemView::ExtendedSelection</enum>
|
||||
</property>
|
||||
<property name="sortingEnabled">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<attribute name="headerStretchLastSection">
|
||||
<bool>false</bool>
|
||||
</attribute>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QDialogButtonBox" name="buttonBox">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="standardButtons">
|
||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<tabstops>
|
||||
<tabstop>treeView</tabstop>
|
||||
</tabstops>
|
||||
<resources/>
|
||||
<connections>
|
||||
<connection>
|
||||
<sender>buttonBox</sender>
|
||||
<signal>accepted()</signal>
|
||||
<receiver>ExportInstanceDialog</receiver>
|
||||
<slot>accept()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>248</x>
|
||||
<y>254</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>157</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>buttonBox</sender>
|
||||
<signal>rejected()</signal>
|
||||
<receiver>ExportInstanceDialog</receiver>
|
||||
<slot>reject()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>316</x>
|
||||
<y>260</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>286</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
</ui>
|
163
launcher/dialogs/IconPickerDialog.cpp
Normal file
163
launcher/dialogs/IconPickerDialog.cpp
Normal file
@ -0,0 +1,163 @@
|
||||
/* Copyright 2013-2021 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 <QKeyEvent>
|
||||
#include <QPushButton>
|
||||
#include <QFileDialog>
|
||||
|
||||
#include "MultiMC.h"
|
||||
|
||||
#include "IconPickerDialog.h"
|
||||
#include "ui_IconPickerDialog.h"
|
||||
|
||||
#include "groupview/InstanceDelegate.h"
|
||||
|
||||
#include "icons/IconList.h"
|
||||
#include "icons/IconUtils.h"
|
||||
#include <DesktopServices.h>
|
||||
|
||||
IconPickerDialog::IconPickerDialog(QWidget *parent)
|
||||
: QDialog(parent), ui(new Ui::IconPickerDialog)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
setWindowModality(Qt::WindowModal);
|
||||
|
||||
auto contentsWidget = ui->iconView;
|
||||
contentsWidget->setViewMode(QListView::IconMode);
|
||||
contentsWidget->setFlow(QListView::LeftToRight);
|
||||
contentsWidget->setIconSize(QSize(48, 48));
|
||||
contentsWidget->setMovement(QListView::Static);
|
||||
contentsWidget->setResizeMode(QListView::Adjust);
|
||||
contentsWidget->setSelectionMode(QAbstractItemView::SingleSelection);
|
||||
contentsWidget->setSpacing(5);
|
||||
contentsWidget->setWordWrap(false);
|
||||
contentsWidget->setWrapping(true);
|
||||
contentsWidget->setUniformItemSizes(true);
|
||||
contentsWidget->setTextElideMode(Qt::ElideRight);
|
||||
contentsWidget->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
|
||||
contentsWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
|
||||
contentsWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||
contentsWidget->setItemDelegate(new ListViewDelegate());
|
||||
|
||||
// contentsWidget->setAcceptDrops(true);
|
||||
contentsWidget->setDropIndicatorShown(true);
|
||||
contentsWidget->viewport()->setAcceptDrops(true);
|
||||
contentsWidget->setDragDropMode(QAbstractItemView::DropOnly);
|
||||
contentsWidget->setDefaultDropAction(Qt::CopyAction);
|
||||
|
||||
contentsWidget->installEventFilter(this);
|
||||
|
||||
contentsWidget->setModel(MMC->icons().get());
|
||||
|
||||
// NOTE: ResetRole forces the button to be on the left, while the OK/Cancel ones are on the right. We win.
|
||||
auto buttonAdd = ui->buttonBox->addButton(tr("Add Icon"), QDialogButtonBox::ResetRole);
|
||||
auto buttonRemove = ui->buttonBox->addButton(tr("Remove Icon"), QDialogButtonBox::ResetRole);
|
||||
|
||||
connect(buttonAdd, SIGNAL(clicked(bool)), SLOT(addNewIcon()));
|
||||
connect(buttonRemove, SIGNAL(clicked(bool)), SLOT(removeSelectedIcon()));
|
||||
|
||||
connect(contentsWidget, SIGNAL(doubleClicked(QModelIndex)), SLOT(activated(QModelIndex)));
|
||||
|
||||
connect(contentsWidget->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SLOT(selectionChanged(QItemSelection, QItemSelection)));
|
||||
|
||||
auto buttonFolder = ui->buttonBox->addButton(tr("Open Folder"), QDialogButtonBox::ResetRole);
|
||||
connect(buttonFolder, &QPushButton::clicked, this, &IconPickerDialog::openFolder);
|
||||
}
|
||||
|
||||
bool IconPickerDialog::eventFilter(QObject *obj, QEvent *evt)
|
||||
{
|
||||
if (obj != ui->iconView)
|
||||
return QDialog::eventFilter(obj, evt);
|
||||
if (evt->type() != QEvent::KeyPress)
|
||||
{
|
||||
return QDialog::eventFilter(obj, evt);
|
||||
}
|
||||
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(evt);
|
||||
switch (keyEvent->key())
|
||||
{
|
||||
case Qt::Key_Delete:
|
||||
removeSelectedIcon();
|
||||
return true;
|
||||
case Qt::Key_Plus:
|
||||
addNewIcon();
|
||||
return true;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return QDialog::eventFilter(obj, evt);
|
||||
}
|
||||
|
||||
void IconPickerDialog::addNewIcon()
|
||||
{
|
||||
//: The title of the select icons open file dialog
|
||||
QString selectIcons = tr("Select Icons");
|
||||
//: The type of icon files
|
||||
auto filter = IconUtils::getIconFilter();
|
||||
QStringList fileNames = QFileDialog::getOpenFileNames(this, selectIcons, QString(), tr("Icons %1").arg(filter));
|
||||
MMC->icons()->installIcons(fileNames);
|
||||
}
|
||||
|
||||
void IconPickerDialog::removeSelectedIcon()
|
||||
{
|
||||
MMC->icons()->deleteIcon(selectedIconKey);
|
||||
}
|
||||
|
||||
void IconPickerDialog::activated(QModelIndex index)
|
||||
{
|
||||
selectedIconKey = index.data(Qt::UserRole).toString();
|
||||
accept();
|
||||
}
|
||||
|
||||
void IconPickerDialog::selectionChanged(QItemSelection selected, QItemSelection deselected)
|
||||
{
|
||||
if (selected.empty())
|
||||
return;
|
||||
|
||||
QString key = selected.first().indexes().first().data(Qt::UserRole).toString();
|
||||
if (!key.isEmpty())
|
||||
selectedIconKey = key;
|
||||
}
|
||||
|
||||
int IconPickerDialog::execWithSelection(QString selection)
|
||||
{
|
||||
auto list = MMC->icons();
|
||||
auto contentsWidget = ui->iconView;
|
||||
selectedIconKey = selection;
|
||||
|
||||
int index_nr = list->getIconIndex(selection);
|
||||
auto model_index = list->index(index_nr);
|
||||
contentsWidget->selectionModel()->select(
|
||||
model_index, QItemSelectionModel::Current | QItemSelectionModel::Select);
|
||||
|
||||
QMetaObject::invokeMethod(this, "delayed_scroll", Qt::QueuedConnection,
|
||||
Q_ARG(QModelIndex, model_index));
|
||||
return QDialog::exec();
|
||||
}
|
||||
|
||||
void IconPickerDialog::delayed_scroll(QModelIndex model_index)
|
||||
{
|
||||
auto contentsWidget = ui->iconView;
|
||||
contentsWidget->scrollTo(model_index);
|
||||
}
|
||||
|
||||
IconPickerDialog::~IconPickerDialog()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void IconPickerDialog::openFolder()
|
||||
{
|
||||
DesktopServices::openDirectory(MMC->icons()->getDirectory(), true);
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user