SCRATCH eliminate InstanceFactory

This commit is contained in:
Petr Mrázek 2015-02-01 03:08:25 +01:00
parent c088d3bef0
commit 154d19bb74
23 changed files with 205 additions and 303 deletions

View File

@ -394,8 +394,6 @@ SET(MULTIMC_SOURCES
logic/BaseVersion.h logic/BaseVersion.h
logic/BaseProcess.h logic/BaseProcess.h
logic/BaseProcess.cpp logic/BaseProcess.cpp
logic/InstanceFactory.h
logic/InstanceFactory.cpp
logic/BaseInstance.h logic/BaseInstance.h
logic/BaseInstance.cpp logic/BaseInstance.cpp
logic/Mod.h logic/Mod.h

View File

@ -205,7 +205,7 @@ MultiMC::MultiMC(int &argc, char **argv, bool test_mode) : QApplication(argc, ar
QLOG_WARN() QLOG_WARN()
<< "Your instance path contains \'!\' and this is known to cause java problems"; << "Your instance path contains \'!\' and this is known to cause java problems";
} }
m_instances.reset(new InstanceList(InstDirSetting->get().toString(), this)); m_instances.reset(new InstanceList(m_settings, InstDirSetting->get().toString(), this));
QLOG_INFO() << "Loading Instances..."; QLOG_INFO() << "Loading Instances...";
m_instances->loadList(); m_instances->loadList();
connect(InstDirSetting.get(), SIGNAL(SettingChanged(const Setting &, QVariant)), connect(InstDirSetting.get(), SIGNAL(SettingChanged(const Setting &, QVariant)),
@ -577,6 +577,10 @@ QIcon MultiMC::getThemedIcon(const QString& name)
void MultiMC::onExit() void MultiMC::onExit()
{ {
if(m_instances)
{
m_instances->saveGroupList();
}
if (m_updateOnExitPath.size()) if (m_updateOnExitPath.size())
{ {
installUpdates(m_updateOnExitPath, m_updateOnExitFlags); installUpdates(m_updateOnExitPath, m_updateOnExitFlags);

View File

@ -378,7 +378,6 @@ namespace Ui {
#include "logic/BaseInstance.h" #include "logic/BaseInstance.h"
#include "logic/OneSixInstance.h" #include "logic/OneSixInstance.h"
#include "logic/InstanceFactory.h"
#include "logic/BaseProcess.h" #include "logic/BaseProcess.h"
#include "logic/OneSixUpdate.h" #include "logic/OneSixUpdate.h"
#include "logic/java/JavaUtils.h" #include "logic/java/JavaUtils.h"
@ -1061,7 +1060,7 @@ void MainWindow::instanceFromZipPack(QString instName, QString instGroup, QStrin
QString instancesDir = MMC->settings()->get("InstanceDir").toString(); QString instancesDir = MMC->settings()->get("InstanceDir").toString();
QString instDirName = DirNameFromString(instName, instancesDir); QString instDirName = DirNameFromString(instName, instancesDir);
QString instDir = PathCombine(instancesDir, instDirName); QString instDir = PathCombine(instancesDir, instDirName);
auto &loader = InstanceFactory::get();
QString archivePath; QString archivePath;
if (url.isLocalFile()) if (url.isLocalFile())
{ {
@ -1105,15 +1104,15 @@ void MainWindow::instanceFromZipPack(QString instName, QString instGroup, QStrin
return; return;
} }
auto error = loader.loadInstance(newInstance, instDir); auto error = MMC->instances()->loadInstance(newInstance, instDir);
QString errorMsg = tr("Failed to load instance %1: ").arg(instDirName); QString errorMsg = tr("Failed to load instance %1: ").arg(instDirName);
switch (error) switch (error)
{ {
case InstanceFactory::UnknownLoadError: case InstanceList::UnknownLoadError:
errorMsg += tr("Unkown error"); errorMsg += tr("Unkown error");
CustomMessageBox::selectable(this, tr("Error"), errorMsg, QMessageBox::Warning)->show(); CustomMessageBox::selectable(this, tr("Error"), errorMsg, QMessageBox::Warning)->show();
return; return;
case InstanceFactory::NotAnInstance: case InstanceList::NotAnInstance:
errorMsg += tr("Not an instance"); errorMsg += tr("Not an instance");
CustomMessageBox::selectable(this, tr("Error"), errorMsg, QMessageBox::Warning)->show(); CustomMessageBox::selectable(this, tr("Error"), errorMsg, QMessageBox::Warning)->show();
return; return;
@ -1135,20 +1134,21 @@ void MainWindow::instanceFromVersion(QString instName, QString instGroup, QStrin
QString instancesDir = MMC->settings()->get("InstanceDir").toString(); QString instancesDir = MMC->settings()->get("InstanceDir").toString();
QString instDirName = DirNameFromString(instName, instancesDir); QString instDirName = DirNameFromString(instName, instancesDir);
QString instDir = PathCombine(instancesDir, instDirName); QString instDir = PathCombine(instancesDir, instDirName);
auto &loader = InstanceFactory::get(); auto error = MMC->instances()->createInstance(newInstance, version, instDir);
auto error = loader.createInstance(newInstance, version, instDir);
QString errorMsg = tr("Failed to create instance %1: ").arg(instDirName); QString errorMsg = tr("Failed to create instance %1: ").arg(instDirName);
switch (error) switch (error)
{ {
case InstanceFactory::NoCreateError: break; case InstanceList::NoCreateError:
case InstanceFactory::InstExists: break;
case InstanceList::InstExists:
{ {
errorMsg += tr("An instance with the given directory name already exists."); errorMsg += tr("An instance with the given directory name already exists.");
CustomMessageBox::selectable(this, tr("Error"), errorMsg, QMessageBox::Warning)->show(); CustomMessageBox::selectable(this, tr("Error"), errorMsg, QMessageBox::Warning)->show();
return; return;
} }
case InstanceFactory::CantCreateDir: case InstanceList::CantCreateDir:
{ {
errorMsg += tr("Failed to create the instance directory."); errorMsg += tr("Failed to create the instance directory.");
CustomMessageBox::selectable(this, tr("Error"), errorMsg, QMessageBox::Warning)->show(); CustomMessageBox::selectable(this, tr("Error"), errorMsg, QMessageBox::Warning)->show();
@ -1231,29 +1231,27 @@ void MainWindow::on_actionCopyInstance_triggered()
QString instDirName = DirNameFromString(copyInstDlg.instName(), instancesDir); QString instDirName = DirNameFromString(copyInstDlg.instName(), instancesDir);
QString instDir = PathCombine(instancesDir, instDirName); QString instDir = PathCombine(instancesDir, instDirName);
auto &loader = InstanceFactory::get();
InstancePtr newInstance; InstancePtr newInstance;
auto error = loader.copyInstance(newInstance, m_selectedInstance, instDir); auto error = MMC->instances()->copyInstance(newInstance, m_selectedInstance, instDir);
QString errorMsg = tr("Failed to create instance %1: ").arg(instDirName); QString errorMsg = tr("Failed to create instance %1: ").arg(instDirName);
switch (error) switch (error)
{ {
case InstanceFactory::NoCreateError: case InstanceList::NoCreateError:
newInstance->setName(copyInstDlg.instName()); newInstance->setName(copyInstDlg.instName());
newInstance->setGroupInitial(copyInstDlg.instGroup()); newInstance->setGroupInitial(copyInstDlg.instGroup());
newInstance->setIconKey(copyInstDlg.iconKey()); newInstance->setIconKey(copyInstDlg.iconKey());
MMC->instances()->add(newInstance); MMC->instances()->add(newInstance);
return; return;
case InstanceFactory::InstExists: case InstanceList::InstExists:
{ {
errorMsg += tr("An instance with the given directory name already exists."); errorMsg += tr("An instance with the given directory name already exists.");
CustomMessageBox::selectable(this, tr("Error"), errorMsg, QMessageBox::Warning)->show(); CustomMessageBox::selectable(this, tr("Error"), errorMsg, QMessageBox::Warning)->show();
break; break;
} }
case InstanceFactory::CantCreateDir: case InstanceList::CantCreateDir:
{ {
errorMsg += tr("Failed to create the instance directory."); errorMsg += tr("Failed to create the instance directory.");
CustomMessageBox::selectable(this, tr("Error"), errorMsg, QMessageBox::Warning)->show(); CustomMessageBox::selectable(this, tr("Error"), errorMsg, QMessageBox::Warning)->show();

View File

@ -25,7 +25,6 @@
#include "gui/dialogs/ProgressDialog.h" #include "gui/dialogs/ProgressDialog.h"
#include "gui/dialogs/IconPickerDialog.h" #include "gui/dialogs/IconPickerDialog.h"
#include "logic/InstanceFactory.h"
#include "logic/BaseVersion.h" #include "logic/BaseVersion.h"
#include "logic/icons/IconList.h" #include "logic/icons/IconList.h"
#include "logic/tasks/Task.h" #include "logic/tasks/Task.h"

View File

@ -17,7 +17,6 @@
#include "NewInstanceDialog.h" #include "NewInstanceDialog.h"
#include "ui_NewInstanceDialog.h" #include "ui_NewInstanceDialog.h"
#include "logic/InstanceFactory.h"
#include "logic/BaseVersion.h" #include "logic/BaseVersion.h"
#include "logic/icons/IconList.h" #include "logic/icons/IconList.h"
#include "logic/minecraft/MinecraftVersionList.h" #include "logic/minecraft/MinecraftVersionList.h"

View File

@ -29,11 +29,10 @@
#include "logic/minecraft/MinecraftVersionList.h" #include "logic/minecraft/MinecraftVersionList.h"
#include "logic/icons/IconList.h" #include "logic/icons/IconList.h"
BaseInstance::BaseInstance(const QString &rootDir, SettingsObject *settings, QObject *parent) BaseInstance::BaseInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString &rootDir)
: QObject(parent) : QObject()
{ {
m_settings = settings;
m_settings = std::shared_ptr<SettingsObject>(settings);
m_rootDir = rootDir; m_rootDir = rootDir;
m_settings->registerSetting("name", "Unnamed Instance"); m_settings->registerSetting("name", "Unnamed Instance");
@ -42,8 +41,6 @@ BaseInstance::BaseInstance(const QString &rootDir, SettingsObject *settings, QOb
m_settings->registerSetting("notes", ""); m_settings->registerSetting("notes", "");
m_settings->registerSetting("lastLaunchTime", 0); m_settings->registerSetting("lastLaunchTime", 0);
auto globalSettings = MMC->settings();
// Java Settings // Java Settings
m_settings->registerSetting("OverrideJava", false); m_settings->registerSetting("OverrideJava", false);
m_settings->registerSetting("OverrideJavaLocation", false); m_settings->registerSetting("OverrideJavaLocation", false);

View File

@ -51,7 +51,7 @@ class BaseInstance : public QObject, public std::enable_shared_from_this<BaseIns
Q_OBJECT Q_OBJECT
protected: protected:
/// no-touchy! /// no-touchy!
BaseInstance(const QString &rootDir, SettingsObject *settings, QObject *parent = 0); BaseInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString &rootDir);
public: public:
/// virtual destructor to make sure the destruction is COMPLETE /// virtual destructor to make sure the destruction is COMPLETE

View File

@ -1,135 +0,0 @@
/* Copyright 2013-2015 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 <QFileInfo>
#include "logic/settings/INIFile.h"
#include "logic/settings/INISettingsObject.h"
#include "logic/settings/Setting.h"
#include <pathutils.h>
#include "logger/QsLog.h"
#include "logic/InstanceFactory.h"
#include "logic/BaseInstance.h"
#include "logic/LegacyInstance.h"
#include "logic/OneSixInstance.h"
#include "logic/OneSixInstance.h"
#include "logic/BaseVersion.h"
#include "logic/minecraft/MinecraftVersion.h"
InstanceFactory InstanceFactory::loader;
InstanceFactory::InstanceFactory() : QObject(NULL)
{
}
InstanceFactory::InstLoadError InstanceFactory::loadInstance(InstancePtr &inst,
const QString &instDir)
{
auto m_settings = new INISettingsObject(PathCombine(instDir, "instance.cfg"));
m_settings->registerSetting("InstanceType", "Legacy");
QString inst_type = m_settings->get("InstanceType").toString();
// FIXME: replace with a map lookup, where instance classes register their types
if (inst_type == "OneSix" || inst_type == "Nostalgia")
{
inst.reset(new OneSixInstance(instDir, m_settings));
}
else if (inst_type == "Legacy")
{
inst.reset(new LegacyInstance(instDir, m_settings));
}
else
{
return InstanceFactory::UnknownLoadError;
}
inst->init();
return NoLoadError;
}
InstanceFactory::InstCreateError
InstanceFactory::createInstance(InstancePtr &inst, BaseVersionPtr version, const QString &instDir)
{
QDir rootDir(instDir);
QLOG_DEBUG() << instDir.toUtf8();
if (!rootDir.exists() && !rootDir.mkpath("."))
{
QLOG_ERROR() << "Can't create instance folder" << instDir;
return InstanceFactory::CantCreateDir;
}
if (!version)
{
QLOG_ERROR() << "Can't create instance for non-existing MC version";
return InstanceFactory::NoSuchVersion;
}
auto m_settings = new INISettingsObject(PathCombine(instDir, "instance.cfg"));
m_settings->registerSetting("InstanceType", "Legacy");
auto minecraftVersion = std::dynamic_pointer_cast<MinecraftVersion>(version);
if(minecraftVersion)
{
auto mcVer = std::dynamic_pointer_cast<MinecraftVersion>(version);
m_settings->set("InstanceType", "OneSix");
inst.reset(new OneSixInstance(instDir, m_settings));
inst->setIntendedVersionId(version->descriptor());
inst->init();
return InstanceFactory::NoCreateError;
}
delete m_settings;
return InstanceFactory::NoSuchVersion;
}
InstanceFactory::InstCreateError InstanceFactory::copyInstance(InstancePtr &newInstance,
InstancePtr &oldInstance,
const QString &instDir)
{
QDir rootDir(instDir);
QLOG_DEBUG() << instDir.toUtf8();
if (!copyPath(oldInstance->instanceRoot(), instDir))
{
rootDir.removeRecursively();
return InstanceFactory::CantCreateDir;
}
INISettingsObject settings_obj(PathCombine(instDir, "instance.cfg"));
settings_obj.registerSetting("InstanceType", "Legacy");
QString inst_type = settings_obj.get("InstanceType").toString();
oldInstance->copy(instDir);
auto error = loadInstance(newInstance, instDir);
switch (error)
{
case NoLoadError:
return NoCreateError;
case NotAnInstance:
rootDir.removeRecursively();
return CantCreateDir;
default:
case UnknownLoadError:
rootDir.removeRecursively();
return UnknownCreateError;
}
}

View File

@ -1,100 +0,0 @@
/* Copyright 2013-2015 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 <QMap>
#include <QList>
#include "BaseVersion.h"
#include "BaseInstance.h"
struct BaseVersion;
class BaseInstance;
/*!
* The \b InstanceFactory\b is a singleton that manages loading and creating instances.
*/
class InstanceFactory : public QObject
{
Q_OBJECT
public:
/*!
* \brief Gets a reference to the instance loader.
*/
static InstanceFactory &get()
{
return loader;
}
enum InstLoadError
{
NoLoadError = 0,
UnknownLoadError,
NotAnInstance
};
enum InstCreateError
{
NoCreateError = 0,
NoSuchVersion,
UnknownCreateError,
InstExists,
CantCreateDir
};
/*!
* \brief Creates a stub instance
*
* \param inst Pointer to store the created instance in.
* \param version Game version to use for the instance
* \param instDir The new instance's directory.
* \param type The type of instance to create
* \return An InstCreateError error code.
* - InstExists if the given instance directory is already an instance.
* - CantCreateDir if the given instance directory cannot be created.
*/
InstCreateError createInstance(InstancePtr &inst, BaseVersionPtr version,
const QString &instDir);
/*!
* \brief Creates a copy of an existing instance with a new name
*
* \param newInstance Pointer to store the created instance in.
* \param oldInstance The instance to copy
* \param instDir The new instance's directory.
* \return An InstCreateError error code.
* - InstExists if the given instance directory is already an instance.
* - CantCreateDir if the given instance directory cannot be created.
*/
InstCreateError copyInstance(InstancePtr &newInstance, InstancePtr &oldInstance,
const QString &instDir);
/*!
* \brief Loads an instance from the given directory.
* Checks the instance's INI file to figure out what the instance's type is first.
* \param inst Pointer to store the loaded instance in.
* \param instDir The instance's directory.
* \return An InstLoadError error code.
* - NotAnInstance if the given instance directory isn't a valid instance.
*/
InstLoadError loadInstance(InstancePtr &inst, const QString &instDir);
private:
InstanceFactory();
static InstanceFactory loader;
};

View File

@ -31,18 +31,19 @@
#include "logic/icons/IconList.h" #include "logic/icons/IconList.h"
#include "logic/minecraft/MinecraftVersionList.h" #include "logic/minecraft/MinecraftVersionList.h"
#include "logic/BaseInstance.h" #include "logic/BaseInstance.h"
#include "logic/InstanceFactory.h" #include "logic/ftb/FTBPlugin.h"
#include "ftb/FTBPlugin.h" #include "settings/INISettingsObject.h"
#include "OneSixInstance.h"
#include "LegacyInstance.h"
#include "logger/QsLog.h" #include "logger/QsLog.h"
#include "gui/groupview/GroupView.h" #include "gui/groupview/GroupView.h"
const static int GROUP_FILE_FORMAT_VERSION = 1; const static int GROUP_FILE_FORMAT_VERSION = 1;
InstanceList::InstanceList(const QString &instDir, QObject *parent) InstanceList::InstanceList(SettingsObjectPtr globalSettings, const QString &instDir, QObject *parent)
: QAbstractListModel(parent), m_instDir(instDir) : QAbstractListModel(parent), m_instDir(instDir)
{ {
connect(MMC, &MultiMC::aboutToQuit, this, &InstanceList::saveGroupList); m_globalSettings = globalSettings;
if (!QDir::current().exists(m_instDir)) if (!QDir::current().exists(m_instDir))
{ {
QDir::current().mkpath(m_instDir); QDir::current().mkpath(m_instDir);
@ -301,7 +302,7 @@ InstanceList::InstListError InstanceList::loadList()
continue; continue;
QLOG_INFO() << "Loading MultiMC instance from " << subDir; QLOG_INFO() << "Loading MultiMC instance from " << subDir;
InstancePtr instPtr; InstancePtr instPtr;
auto error = InstanceFactory::get().loadInstance(instPtr, subDir); auto error = loadInstance(instPtr, subDir);
if(!continueProcessInstance(instPtr, error, subDir, groupMap)) if(!continueProcessInstance(instPtr, error, subDir, groupMap))
continue; continue;
tempList.append(instPtr); tempList.append(instPtr);
@ -399,7 +400,7 @@ int InstanceList::getInstIndex(BaseInstance *inst) const
bool InstanceList::continueProcessInstance(InstancePtr instPtr, const int error, bool InstanceList::continueProcessInstance(InstancePtr instPtr, const int error,
const QDir &dir, QMap<QString, QString> &groupMap) const QDir &dir, QMap<QString, QString> &groupMap)
{ {
if (error != InstanceFactory::NoLoadError && error != InstanceFactory::NotAnInstance) if (error != InstanceList::NoLoadError && error != InstanceList::NotAnInstance)
{ {
QString errorMsg = QString("Failed to load instance %1: ") QString errorMsg = QString("Failed to load instance %1: ")
.arg(QFileInfo(dir.absolutePath()).baseName()) .arg(QFileInfo(dir.absolutePath()).baseName())
@ -433,6 +434,100 @@ bool InstanceList::continueProcessInstance(InstancePtr instPtr, const int error,
} }
} }
InstanceList::InstLoadError
InstanceList::loadInstance(InstancePtr &inst, const QString &instDir)
{
auto instanceSettings = std::make_shared<INISettingsObject>(PathCombine(instDir, "instance.cfg"));
instanceSettings->registerSetting("InstanceType", "Legacy");
QString inst_type = instanceSettings->get("InstanceType").toString();
// FIXME: replace with a map lookup, where instance classes register their types
if (inst_type == "OneSix" || inst_type == "Nostalgia")
{
inst.reset(new OneSixInstance(m_globalSettings, instanceSettings, instDir));
}
else if (inst_type == "Legacy")
{
inst.reset(new LegacyInstance(m_globalSettings, instanceSettings, instDir));
}
else
{
return InstanceList::UnknownLoadError;
}
inst->init();
return NoLoadError;
}
InstanceList::InstCreateError
InstanceList::createInstance(InstancePtr &inst, BaseVersionPtr version, const QString &instDir)
{
QDir rootDir(instDir);
QLOG_DEBUG() << instDir.toUtf8();
if (!rootDir.exists() && !rootDir.mkpath("."))
{
QLOG_ERROR() << "Can't create instance folder" << instDir;
return InstanceList::CantCreateDir;
}
if (!version)
{
QLOG_ERROR() << "Can't create instance for non-existing MC version";
return InstanceList::NoSuchVersion;
}
auto instanceSettings = std::make_shared<INISettingsObject>(PathCombine(instDir, "instance.cfg"));
instanceSettings->registerSetting("InstanceType", "Legacy");
auto minecraftVersion = std::dynamic_pointer_cast<MinecraftVersion>(version);
if(minecraftVersion)
{
auto mcVer = std::dynamic_pointer_cast<MinecraftVersion>(version);
instanceSettings->set("InstanceType", "OneSix");
inst.reset(new OneSixInstance(m_globalSettings, instanceSettings, instDir));
inst->setIntendedVersionId(version->descriptor());
inst->init();
return InstanceList::NoCreateError;
}
return InstanceList::NoSuchVersion;
}
InstanceList::InstCreateError
InstanceList::copyInstance(InstancePtr &newInstance, InstancePtr &oldInstance, const QString &instDir)
{
QDir rootDir(instDir);
QLOG_DEBUG() << instDir.toUtf8();
if (!copyPath(oldInstance->instanceRoot(), instDir))
{
rootDir.removeRecursively();
return InstanceList::CantCreateDir;
}
INISettingsObject settings_obj(PathCombine(instDir, "instance.cfg"));
settings_obj.registerSetting("InstanceType", "Legacy");
QString inst_type = settings_obj.get("InstanceType").toString();
oldInstance->copy(instDir);
auto error = loadInstance(newInstance, instDir);
switch (error)
{
case NoLoadError:
return NoCreateError;
case NotAnInstance:
rootDir.removeRecursively();
return CantCreateDir;
default:
case UnknownLoadError:
rootDir.removeRecursively();
return UnknownCreateError;
}
}
void InstanceList::instanceNuked(BaseInstance *inst) void InstanceList::instanceNuked(BaseInstance *inst)
{ {
int i = getInstIndex(inst); int i = getInstIndex(inst);

View File

@ -23,7 +23,6 @@
#include "logic/BaseInstance.h" #include "logic/BaseInstance.h"
class BaseInstance; class BaseInstance;
class QDir; class QDir;
class InstanceList : public QAbstractListModel class InstanceList : public QAbstractListModel
@ -37,7 +36,7 @@ slots:
void saveGroupList(); void saveGroupList();
public: public:
explicit InstanceList(const QString &instDir, QObject *parent = 0); explicit InstanceList(SettingsObjectPtr globalSettings, const QString &instDir, QObject *parent = 0);
virtual ~InstanceList(); virtual ~InstanceList();
public: public:
@ -62,6 +61,22 @@ public:
UnknownError UnknownError
}; };
enum InstLoadError
{
NoLoadError = 0,
UnknownLoadError,
NotAnInstance
};
enum InstCreateError
{
NoCreateError = 0,
NoSuchVersion,
UnknownCreateError,
InstExists,
CantCreateDir
};
QString instDir() const QString instDir() const
{ {
return m_instDir; return m_instDir;
@ -98,6 +113,43 @@ public:
// FIXME: instead of iterating through all instances and forming a set, keep the set around // FIXME: instead of iterating through all instances and forming a set, keep the set around
QStringList getGroups(); QStringList getGroups();
/*!
* \brief Creates a stub instance
*
* \param inst Pointer to store the created instance in.
* \param version Game version to use for the instance
* \param instDir The new instance's directory.
* \return An InstCreateError error code.
* - InstExists if the given instance directory is already an instance.
* - CantCreateDir if the given instance directory cannot be created.
*/
InstCreateError createInstance(InstancePtr &inst, BaseVersionPtr version,
const QString &instDir);
/*!
* \brief Creates a copy of an existing instance with a new name
*
* \param newInstance Pointer to store the created instance in.
* \param oldInstance The instance to copy
* \param instDir The new instance's directory.
* \return An InstCreateError error code.
* - InstExists if the given instance directory is already an instance.
* - CantCreateDir if the given instance directory cannot be created.
*/
InstCreateError copyInstance(InstancePtr &newInstance, InstancePtr &oldInstance,
const QString &instDir);
/*!
* \brief Loads an instance from the given directory.
* Checks the instance's INI file to figure out what the instance's type is first.
* \param inst Pointer to store the loaded instance in.
* \param instDir The instance's directory.
* \return An InstLoadError error code.
* - NotAnInstance if the given instance directory isn't a valid instance.
*/
InstLoadError loadInstance(InstancePtr &inst, const QString &instDir);
signals: signals:
void dataIsInvalid(); void dataIsInvalid();
@ -127,6 +179,7 @@ protected:
QString m_instDir; QString m_instDir;
QList<InstancePtr> m_instances; QList<InstancePtr> m_instances;
QSet<QString> m_groups; QSet<QString> m_groups;
SettingsObjectPtr m_globalSettings;
}; };
class InstanceProxyModel : public GroupedProxyModel class InstanceProxyModel : public GroupedProxyModel

View File

@ -35,8 +35,8 @@
#include <gui/pages/NotesPage.h> #include <gui/pages/NotesPage.h>
#include <gui/pages/ScreenshotsPage.h> #include <gui/pages/ScreenshotsPage.h>
LegacyInstance::LegacyInstance(const QString &rootDir, SettingsObject *settings, QObject *parent) LegacyInstance::LegacyInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString &rootDir)
: MinecraftInstance(rootDir, settings, parent) : MinecraftInstance(globalSettings, settings, rootDir)
{ {
settings->registerSetting("NeedsRebuild", true); settings->registerSetting("NeedsRebuild", true);
settings->registerSetting("ShouldUpdate", false); settings->registerSetting("ShouldUpdate", false);

View File

@ -26,8 +26,7 @@ class LegacyInstance : public MinecraftInstance, public BasePageProvider
Q_OBJECT Q_OBJECT
public: public:
explicit LegacyInstance(const QString &rootDir, SettingsObject *settings, explicit LegacyInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString &rootDir);
QObject *parent = 0);
virtual void init() {}; virtual void init() {};

View File

@ -38,9 +38,8 @@
#include "gui/pages/NotesPage.h" #include "gui/pages/NotesPage.h"
#include "gui/pages/ScreenshotsPage.h" #include "gui/pages/ScreenshotsPage.h"
#include "gui/pages/OtherLogsPage.h" #include "gui/pages/OtherLogsPage.h"
OneSixInstance::OneSixInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString &rootDir)
OneSixInstance::OneSixInstance(const QString &rootDir, SettingsObject *settings, QObject *parent) : MinecraftInstance(globalSettings, settings, rootDir)
: MinecraftInstance(rootDir, settings, parent)
{ {
m_settings->registerSetting({"IntendedVersion", "MinecraftVersion"}, ""); m_settings->registerSetting({"IntendedVersion", "MinecraftVersion"}, "");
} }

View File

@ -25,8 +25,7 @@ class OneSixInstance : public MinecraftInstance, public BasePageProvider
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit OneSixInstance(const QString &rootDir, SettingsObject *settings, explicit OneSixInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString &rootDir);
QObject *parent = 0);
virtual ~OneSixInstance(){}; virtual ~OneSixInstance(){};
virtual void init(); virtual void init();

View File

@ -4,7 +4,6 @@
#include "OneSixFTBInstance.h" #include "OneSixFTBInstance.h"
#include <logic/BaseInstance.h> #include <logic/BaseInstance.h>
#include <logic/icons/IconList.h> #include <logic/icons/IconList.h>
#include <logic/InstanceFactory.h>
#include <logic/InstanceList.h> #include <logic/InstanceList.h>
#include <logic/minecraft/MinecraftVersionList.h> #include <logic/minecraft/MinecraftVersionList.h>
#include <logic/settings/INISettingsObject.h> #include <logic/settings/INISettingsObject.h>
@ -134,7 +133,7 @@ QSet<FTBRecord> discoverFTBInstances()
InstancePtr loadInstance(const QString &instDir) InstancePtr loadInstance(const QString &instDir)
{ {
auto m_settings = new INISettingsObject(PathCombine(instDir, "instance.cfg")); auto m_settings = std::make_shared<INISettingsObject>(PathCombine(instDir, "instance.cfg"));
InstancePtr inst; InstancePtr inst;
@ -144,11 +143,11 @@ InstancePtr loadInstance(const QString &instDir)
if (inst_type == "LegacyFTB") if (inst_type == "LegacyFTB")
{ {
inst.reset(new LegacyFTBInstance(instDir, m_settings)); inst.reset(new LegacyFTBInstance(MMC->settings(), m_settings, instDir));
} }
else if (inst_type == "OneSixFTB") else if (inst_type == "OneSixFTB")
{ {
inst.reset(new OneSixFTBInstance(instDir, m_settings)); inst.reset(new OneSixFTBInstance(MMC->settings(), m_settings, instDir));
} }
inst->init(); inst->init();
return inst; return inst;
@ -173,19 +172,19 @@ InstancePtr createInstance(MinecraftVersionPtr version, const QString &instDir)
return nullptr; return nullptr;
} }
auto m_settings = new INISettingsObject(PathCombine(instDir, "instance.cfg")); auto m_settings = std::make_shared<INISettingsObject>(PathCombine(instDir, "instance.cfg"));
m_settings->registerSetting("InstanceType", "Legacy"); m_settings->registerSetting("InstanceType", "Legacy");
if (version->usesLegacyLauncher()) if (version->usesLegacyLauncher())
{ {
m_settings->set("InstanceType", "LegacyFTB"); m_settings->set("InstanceType", "LegacyFTB");
inst.reset(new LegacyFTBInstance(instDir, m_settings)); inst.reset(new LegacyFTBInstance(MMC->settings(),m_settings, instDir));
inst->setIntendedVersionId(version->descriptor()); inst->setIntendedVersionId(version->descriptor());
} }
else else
{ {
m_settings->set("InstanceType", "OneSixFTB"); m_settings->set("InstanceType", "OneSixFTB");
inst.reset(new OneSixFTBInstance(instDir, m_settings)); inst.reset(new OneSixFTBInstance(MMC->settings(),m_settings, instDir));
inst->setIntendedVersionId(version->descriptor()); inst->setIntendedVersionId(version->descriptor());
inst->init(); inst->init();
} }
@ -239,7 +238,7 @@ void FTBPlugin::loadInstances(QMap<QString, QString> &groupMap, QList<InstancePt
instPtr->setIconKey(iconKey); instPtr->setIconKey(iconKey);
instPtr->setIntendedVersionId(record.mcVersion); instPtr->setIntendedVersionId(record.mcVersion);
instPtr->setNotes(record.description); instPtr->setNotes(record.description);
if (!InstanceList::continueProcessInstance(instPtr, InstanceFactory::NoCreateError, record.instanceDir, groupMap)) if (!InstanceList::continueProcessInstance(instPtr, InstanceList::NoCreateError, record.instanceDir, groupMap))
continue; continue;
tempList.append(InstancePtr(instPtr)); tempList.append(InstancePtr(instPtr));
} }
@ -259,7 +258,7 @@ void FTBPlugin::loadInstances(QMap<QString, QString> &groupMap, QList<InstancePt
instPtr->setIntendedVersionId(record.mcVersion); instPtr->setIntendedVersionId(record.mcVersion);
} }
instPtr->setNotes(record.description); instPtr->setNotes(record.description);
if (!InstanceList::continueProcessInstance(instPtr, InstanceFactory::NoCreateError, record.instanceDir, groupMap)) if (!InstanceList::continueProcessInstance(instPtr, InstanceList::NoCreateError, record.instanceDir, groupMap))
continue; continue;
tempList.append(InstancePtr(instPtr)); tempList.append(InstancePtr(instPtr));
} }

View File

@ -2,8 +2,8 @@
#include <logic/settings/INISettingsObject.h> #include <logic/settings/INISettingsObject.h>
#include <QDir> #include <QDir>
LegacyFTBInstance::LegacyFTBInstance(const QString &rootDir, SettingsObject *settings, QObject *parent) : LegacyFTBInstance::LegacyFTBInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString &rootDir) :
LegacyInstance(rootDir, settings, parent) LegacyInstance(globalSettings, settings, rootDir)
{ {
} }

View File

@ -6,8 +6,7 @@ class LegacyFTBInstance : public LegacyInstance
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit LegacyFTBInstance(const QString &rootDir, SettingsObject *settings, explicit LegacyFTBInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString &rootDir);
QObject *parent = 0);
virtual QString getStatusbarDescription(); virtual QString getStatusbarDescription();
virtual QString id() const; virtual QString id() const;
virtual void copy(const QDir &newDir); virtual void copy(const QDir &newDir);

View File

@ -11,8 +11,8 @@
#include "MultiMC.h" #include "MultiMC.h"
#include "pathutils.h" #include "pathutils.h"
OneSixFTBInstance::OneSixFTBInstance(const QString &rootDir, SettingsObject *settings, QObject *parent) : OneSixFTBInstance::OneSixFTBInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString &rootDir) :
OneSixInstance(rootDir, settings, parent) OneSixInstance(globalSettings, settings, rootDir)
{ {
} }

View File

@ -8,8 +8,7 @@ class OneSixFTBInstance : public OneSixInstance
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit OneSixFTBInstance(const QString &rootDir, SettingsObject *settings, explicit OneSixFTBInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString &rootDir);
QObject *parent = 0);
virtual ~OneSixFTBInstance(){}; virtual ~OneSixFTBInstance(){};
void copy(const QDir &newDir) override; void copy(const QDir &newDir) override;

View File

@ -4,11 +4,9 @@
#include <pathutils.h> #include <pathutils.h>
#include "logic/minecraft/MinecraftVersionList.h" #include "logic/minecraft/MinecraftVersionList.h"
MinecraftInstance::MinecraftInstance(const QString &rootDir, SettingsObject *settings, QObject *parent) MinecraftInstance::MinecraftInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString &rootDir)
: BaseInstance(rootDir, settings, parent) : BaseInstance(globalSettings, settings, rootDir)
{ {
auto globalSettings = MMC->settings();
// Java Settings // Java Settings
m_settings->registerSetting("OverrideJava", false); m_settings->registerSetting("OverrideJava", false);
m_settings->registerSetting("OverrideJavaLocation", false); m_settings->registerSetting("OverrideJavaLocation", false);

View File

@ -4,7 +4,7 @@
class MinecraftInstance: public BaseInstance class MinecraftInstance: public BaseInstance
{ {
public: public:
MinecraftInstance(const QString& rootDir, SettingsObject* settings, QObject* parent = 0); MinecraftInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString &rootDir);
virtual ~MinecraftInstance() {}; virtual ~MinecraftInstance() {};
/// Path to the instance's minecraft directory. /// Path to the instance's minecraft directory.

View File

@ -175,3 +175,5 @@ protected:
private: private:
QMap<QString, std::shared_ptr<Setting>> m_settings; QMap<QString, std::shared_ptr<Setting>> m_settings;
}; };
typedef std::shared_ptr<SettingsObject> SettingsObjectPtr;