NOISSUE simplify.

This commit is contained in:
Petr Mrázek 2018-07-24 00:11:24 +02:00
parent 7b439c85c0
commit 76d6ec91a4
21 changed files with 140 additions and 224 deletions

View File

@ -284,19 +284,3 @@ std::shared_ptr<LaunchTask> BaseInstance::getLaunchTask()
{
return m_launchProcess;
}
void BaseInstance::setProvider(BaseInstanceProvider* provider)
{
// only once.
assert(!m_provider);
if(m_provider)
{
qWarning() << "Provider set more than once for instance" << id();
}
m_provider = provider;
}
BaseInstanceProvider* BaseInstance::provider() const
{
return m_provider;
}

View File

@ -38,7 +38,6 @@ class QDir;
class Task;
class LaunchTask;
class BaseInstance;
class BaseInstanceProvider;
// pointer for lazy people
typedef std::shared_ptr<BaseInstance> InstancePtr;
@ -89,9 +88,6 @@ public:
int64_t totalTimePlayed() const;
void resetTimePlayed();
void setProvider(BaseInstanceProvider * provider);
BaseInstanceProvider * provider() const;
/// get the type of this instance
QString instanceType() const;
@ -271,7 +267,6 @@ protected: /* data */
bool m_isRunning = false;
std::shared_ptr<LaunchTask> m_launchProcess;
QDateTime m_timeStarted;
BaseInstanceProvider * m_provider = nullptr;
private: /* data */
Status m_status = Status::Present;

View File

@ -1,58 +0,0 @@
#pragma once
#include <QObject>
#include <QString>
#include "BaseInstance.h"
#include "settings/SettingsObject.h"
#include "multimc_logic_export.h"
using InstanceId = QString;
using GroupId = QString;
using InstanceLocator = std::pair<InstancePtr, int>;
enum class InstCreateError
{
NoCreateError = 0,
NoSuchVersion,
UnknownCreateError,
InstExists,
CantCreateDir
};
class MULTIMC_LOGIC_EXPORT BaseInstanceProvider : public QObject
{
Q_OBJECT
public:
BaseInstanceProvider(SettingsObjectPtr settings) : m_globalSettings(settings)
{
// nil
}
public:
virtual QList<InstanceId> discoverInstances() = 0;
virtual InstancePtr loadInstance(const InstanceId &id) = 0;
virtual void loadGroupList() = 0;
virtual void saveGroupList() = 0;
virtual QString getStagedInstancePath()
{
return QString();
}
virtual bool commitStagedInstance(const QString & path, const QString& instanceName, const QString & groupName)
{
return false;
}
virtual bool destroyStagingPath(const QString & path)
{
return true;
}
signals:
// Emit this when the list of provided instances changed
void instancesChanged();
// Emit when the set of groups your provider supplies changes.
void groupsChanged(QSet<QString> groups);
protected:
SettingsObjectPtr m_globalSettings;
};

View File

@ -16,7 +16,6 @@ set(CORE_SOURCES
LoggedProcess.cpp
MessageLevel.cpp
MessageLevel.h
BaseInstanceProvider.h
FolderInstanceProvider.h
FolderInstanceProvider.cpp
BaseVersion.h

View 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;
};

View File

@ -4,6 +4,9 @@
#include "minecraft/MinecraftInstance.h"
#include "minecraft/legacy/LegacyInstance.h"
#include "NullInstance.h"
#include "ExponentialSeries.h"
#include "WatchLock.h"
#include <QDir>
#include <QDirIterator>
@ -16,23 +19,8 @@
const static int GROUP_FILE_FORMAT_VERSION = 1;
struct WatchLock
{
WatchLock(QFileSystemWatcher * watcher, const QString& instDir)
: m_watcher(watcher), m_instDir(instDir)
{
m_watcher->removePath(m_instDir);
}
~WatchLock()
{
m_watcher->addPath(m_instDir);
}
QFileSystemWatcher * m_watcher;
QString m_instDir;
};
FolderInstanceProvider::FolderInstanceProvider(SettingsObjectPtr settings, const QString& instDir)
: BaseInstanceProvider(settings)
: m_globalSettings(settings)
{
// Create aand normalize path
if (!QDir::current().exists(instDir))
@ -105,7 +93,6 @@ InstancePtr FolderInstanceProvider::loadInstance(const InstanceId& id)
inst.reset(new NullInstance(m_globalSettings, instanceSettings, instanceRoot));
}
inst->init();
inst->setProvider(this);
auto iter = groupMap.find(id);
if (iter != groupMap.end())
{
@ -313,55 +300,13 @@ void FolderInstanceProvider::on_InstFolderChanged(const Setting &setting, QVaria
}
}
template <typename T>
static void clamp(T& current, T min, T max)
{
if (current < min)
{
current = min;
}
else if(current > max)
{
current = max;
}
}
namespace {
// 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;
};
}
class FolderInstanceStaging : public Task
class InstanceStaging : public Task
{
Q_OBJECT
const unsigned minBackoff = 1;
const unsigned maxBackoff = 16;
public:
FolderInstanceStaging (
InstanceStaging (
FolderInstanceProvider * parent,
Task * child,
const QString & stagingPath,
@ -371,18 +316,18 @@ public:
{
m_parent = parent;
m_child.reset(child);
connect(child, &Task::succeeded, this, &FolderInstanceStaging::childSucceded);
connect(child, &Task::failed, this, &FolderInstanceStaging::childFailed);
connect(child, &Task::status, this, &FolderInstanceStaging::setStatus);
connect(child, &Task::progress, this, &FolderInstanceStaging::setProgress);
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, &FolderInstanceStaging::childSucceded);
connect(&m_backoffTimer, &QTimer::timeout, this, &InstanceStaging::childSucceded);
}
virtual ~FolderInstanceStaging() {};
virtual ~InstanceStaging() {};
protected:
virtual void executeTask() override
@ -439,7 +384,7 @@ Task * FolderInstanceProvider::wrapInstanceTask(InstanceTask * task)
auto stagingPath = getStagedInstancePath();
task->setStagingPath(stagingPath);
task->setParentSettings(m_globalSettings);
return new FolderInstanceStaging(this, task, stagingPath, task->name(), task->group());
return new InstanceStaging(this, task, stagingPath, task->name(), task->group());
}
QString FolderInstanceProvider::getStagedInstancePath()

View File

@ -1,23 +1,42 @@
#pragma once
#include "BaseInstanceProvider.h"
#include <QObject>
#include <QString>
#include <QMap>
#include "BaseInstance.h"
#include "settings/SettingsObject.h"
#include "multimc_logic_export.h"
class QFileSystemWatcher;
class InstanceTask;
using InstanceId = QString;
using GroupId = QString;
using InstanceLocator = std::pair<InstancePtr, int>;
class MULTIMC_LOGIC_EXPORT FolderInstanceProvider : public BaseInstanceProvider
enum class InstCreateError
{
NoCreateError = 0,
NoSuchVersion,
UnknownCreateError,
InstExists,
CantCreateDir
};
class MULTIMC_LOGIC_EXPORT FolderInstanceProvider : public QObject
{
Q_OBJECT
public:
FolderInstanceProvider(SettingsObjectPtr settings, const QString & instDir);
virtual ~FolderInstanceProvider() = default;
public:
/// used by InstanceList to @return a list of plausible IDs to probe for
QList<InstanceId> discoverInstances() override;
QList<InstanceId> discoverInstances();
/// used by InstanceList to (re)load an instance with the given @id.
InstancePtr loadInstance(const InstanceId& id) override;
InstancePtr loadInstance(const InstanceId& id);
// Wrap an instance creation task in some more task machinery and make it ready to be used
Task * wrapInstanceTask(InstanceTask * task);
@ -26,17 +45,24 @@ public:
* 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() override;
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) override;
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) override;
bool destroyStagingPath(const QString & keyPath);
signals:
// Emit this when the list of provided instances changed
void instancesChanged();
// Emit when the set of groups your provider supplies changes.
void groupsChanged(QSet<QString> groups);
public slots:
void on_InstFolderChanged(const Setting &setting, QVariant value);
@ -46,10 +72,11 @@ private slots:
void groupChanged();
private: /* methods */
void loadGroupList() override;
void saveGroupList() override;
void loadGroupList();
void saveGroupList();
private: /* data */
SettingsObjectPtr m_globalSettings;
QString m_instDir;
QFileSystemWatcher * m_watcher;
QMap<InstanceId, GroupId> groupMap;

View File

@ -1,5 +1,4 @@
#include "InstanceCopyTask.h"
#include "BaseInstanceProvider.h"
#include "settings/INISettingsObject.h"
#include "FileSystem.h"
#include "NullInstance.h"

View File

@ -11,8 +11,6 @@
#include "BaseInstance.h"
#include "InstanceTask.h"
class BaseInstanceProvider;
class MULTIMC_LOGIC_EXPORT InstanceCopyTask : public InstanceTask
{
Q_OBJECT

View File

@ -1,5 +1,4 @@
#include "InstanceCreationTask.h"
#include "BaseInstanceProvider.h"
#include "settings/INISettingsObject.h"
#include "FileSystem.h"

View File

@ -1,6 +1,5 @@
#include "InstanceImportTask.h"
#include "BaseInstance.h"
#include "BaseInstanceProvider.h"
#include "FileSystem.h"
#include "Env.h"
#include "MMCZip.h"

View File

@ -10,7 +10,6 @@
#include "QObjectPtr.h"
class QuaZip;
class BaseInstanceProvider;
namespace Flame
{
class FileResolvingTask;

View File

@ -155,48 +155,27 @@ static QMap<InstanceId, InstanceLocator> getIdMapping(const QList<InstancePtr> &
return out;
}
InstanceList::InstListError InstanceList::loadList(bool complete)
InstanceList::InstListError InstanceList::loadList()
{
auto existingIds = getIdMapping(m_instances);
QList<InstancePtr> newList;
auto processIds = [&](BaseInstanceProvider * provider, QList<InstanceId> ids)
for(auto & id: m_provider->discoverInstances())
{
for(auto & id: ids)
if(existingIds.contains(id))
{
if(existingIds.contains(id))
{
auto instPair = existingIds[id];
/*
auto & instPtr = instPair.first;
auto & instIdx = instPair.second;
*/
existingIds.remove(id);
qDebug() << "Should keep and soft-reload" << id;
}
else
{
InstancePtr instPtr = provider->loadInstance(id);
if(instPtr)
{
newList.append(instPtr);
}
}
auto instPair = existingIds[id];
existingIds.remove(id);
qDebug() << "Should keep and soft-reload" << id;
}
};
if(complete)
{
for(auto & item: m_providers)
else
{
processIds(item.get(), item->discoverInstances());
}
}
else
{
for (auto & item: m_updatedProviders)
{
processIds(item, item->discoverInstances());
InstancePtr instPtr = m_provider->loadInstance(id);
if(instPtr)
{
newList.append(instPtr);
}
}
}
@ -225,10 +204,6 @@ InstanceList::InstListError InstanceList::loadList(bool complete)
for(auto & removedItem: deadList)
{
auto instPtr = removedItem.first;
if(!complete && !m_updatedProviders.contains(instPtr->provider()))
{
continue;
}
instPtr->invalidate();
currentItem = removedItem.second;
if(back_bookmark == -1)
@ -256,7 +231,7 @@ InstanceList::InstListError InstanceList::loadList(bool complete)
{
add(newList);
}
m_updatedProviders.clear();
m_dirty = false;
return NoError;
}
@ -287,7 +262,7 @@ void InstanceList::resumeWatch()
return;
}
m_watchLevel++;
if(m_watchLevel > 0 && !m_updatedProviders.isEmpty())
if(m_watchLevel > 0 && m_dirty)
{
loadList();
}
@ -300,13 +275,13 @@ void InstanceList::suspendWatch()
void InstanceList::providerUpdated()
{
auto provider = dynamic_cast<BaseInstanceProvider *>(QObject::sender());
auto provider = dynamic_cast<FolderInstanceProvider *>(QObject::sender());
if(!provider)
{
qWarning() << "InstanceList::providerUpdated triggered by a non-provider";
return;
}
m_updatedProviders.insert(provider);
m_dirty = true;
if(m_watchLevel == 1)
{
loadList();
@ -318,11 +293,11 @@ void InstanceList::groupsPublished(QSet<QString> newGroups)
m_groups.unite(newGroups);
}
void InstanceList::addInstanceProvider(BaseInstanceProvider* provider)
void InstanceList::addInstanceProvider(FolderInstanceProvider* provider)
{
connect(provider, &BaseInstanceProvider::instancesChanged, this, &InstanceList::providerUpdated);
connect(provider, &BaseInstanceProvider::groupsChanged, this, &InstanceList::groupsPublished);
m_providers.append(provider);
connect(provider, &FolderInstanceProvider::instancesChanged, this, &InstanceList::providerUpdated);
connect(provider, &FolderInstanceProvider::groupsChanged, this, &InstanceList::groupsPublished);
m_provider = provider;
}
InstancePtr InstanceList::getInstanceById(QString instId) const

View File

@ -21,14 +21,12 @@
#include <QList>
#include "BaseInstance.h"
#include "BaseInstanceProvider.h"
#include "FolderInstanceProvider.h"
#include "multimc_logic_export.h"
#include "QObjectPtr.h"
class BaseInstance;
class MULTIMC_LOGIC_EXPORT InstanceList : public QAbstractListModel
{
Q_OBJECT
@ -70,11 +68,11 @@ public:
return m_instances.count();
}
InstListError loadList(bool complete = false);
InstListError loadList();
void saveNow();
/// Add an instance provider. Takes ownership of it. Should only be done before the first load.
void addInstanceProvider(BaseInstanceProvider * provider);
void addInstanceProvider(FolderInstanceProvider * provider);
InstancePtr getInstanceById(QString id) const;
QModelIndex getInstanceIndexById(const QString &id) const;
@ -99,8 +97,8 @@ private:
protected:
int m_watchLevel = 0;
QSet<BaseInstanceProvider *> m_updatedProviders;
bool m_dirty = false;
QList<InstancePtr> m_instances;
QSet<QString> m_groups;
QVector<shared_qobject_ptr<BaseInstanceProvider>> m_providers;
FolderInstanceProvider * m_provider;
};

View File

@ -4,8 +4,6 @@
#include "multimc_logic_export.h"
#include "settings/SettingsObject.h"
class BaseInstanceProvider;
class MULTIMC_LOGIC_EXPORT InstanceTask : public Task
{
Q_OBJECT

20
api/logic/WatchLock.h Normal file
View 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;
};

View File

@ -1,5 +1,4 @@
#include "LegacyUpgradeTask.h"
#include "BaseInstanceProvider.h"
#include "settings/INISettingsObject.h"
#include "FileSystem.h"
#include "NullInstance.h"

View File

@ -11,8 +11,6 @@
#include "BaseInstance.h"
class BaseInstanceProvider;
class MULTIMC_LOGIC_EXPORT LegacyUpgradeTask : public InstanceTask
{
Q_OBJECT

View File

@ -1,6 +1,5 @@
#pragma once
#include "InstanceTask.h"
#include "BaseInstanceProvider.h"
#include "net/NetJob.h"
#include "quazip.h"
#include "quazipdir.h"

View File

@ -1490,7 +1490,7 @@ void MainWindow::on_actionViewInstanceFolder_triggered()
void MainWindow::refreshInstances()
{
MMC->instances()->loadList(true);
MMC->instances()->loadList();
}
void MainWindow::on_actionViewCentralModsFolder_triggered()
@ -1524,7 +1524,7 @@ void MainWindow::on_actionSettings_triggered()
{
SettingsUI::ShowPageDialog(MMC->globalSettingsPages(), this, "global-settings");
// FIXME: quick HACK to make this work. improve, optimize.
MMC->instances()->loadList(true);
MMC->instances()->loadList();
proxymodel->invalidate();
proxymodel->sort(0);
updateToolsMenu();

View File

@ -602,7 +602,7 @@ MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv)
connect(InstDirSetting.get(), &Setting::SettingChanged, m_instanceFolder, &FolderInstanceProvider::on_InstFolderChanged);
m_instances->addInstanceProvider(m_instanceFolder);
qDebug() << "Loading Instances...";
m_instances->loadList(true);
m_instances->loadList();
qDebug() << "<> Instances loaded.";
}