From 4b1680f2428f33dc8ecbf3f4f4b964b0a02e7e06 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 14 Feb 2013 22:40:00 -0600 Subject: Started reworking the instance system. --- data/appsettings.h | 7 +- data/inst/instance.cpp | 21 +++ data/inst/instance.h | 320 +++++++++++++++++++++++++++++++++++++++ data/inst/instancelist.cpp | 23 +++ data/inst/instancelist.h | 39 +++++ data/instancebase.cpp | 109 ------------- data/instancebase.h | 58 ------- data/instancemodel.h | 6 +- data/siglist_imp.h | 156 ------------------- data/siglist_impl.h | 156 +++++++++++++++++++ data/stdinstance.cpp | 22 --- data/stdinstance.h | 28 ---- data/version/instversion.cpp | 32 ++++ data/version/instversion.h | 51 +++++++ data/version/instversionlist.cpp | 21 +++ data/version/instversionlist.h | 43 ++++++ 16 files changed, 711 insertions(+), 381 deletions(-) create mode 100644 data/inst/instance.cpp create mode 100644 data/inst/instance.h create mode 100644 data/inst/instancelist.cpp create mode 100644 data/inst/instancelist.h delete mode 100644 data/instancebase.cpp delete mode 100644 data/instancebase.h delete mode 100644 data/siglist_imp.h create mode 100644 data/siglist_impl.h delete mode 100644 data/stdinstance.cpp delete mode 100644 data/stdinstance.h create mode 100644 data/version/instversion.cpp create mode 100644 data/version/instversion.h create mode 100644 data/version/instversionlist.cpp create mode 100644 data/version/instversionlist.h (limited to 'data') diff --git a/data/appsettings.h b/data/appsettings.h index a9068bfd..8a786db0 100644 --- a/data/appsettings.h +++ b/data/appsettings.h @@ -39,8 +39,6 @@ #define DEFINE_SETTING(name, valType, defVal) \ DEFINE_SETTING_ADVANCED(name, STR_VAL(name), valType, defVal) -#define DEFINE_OVERRIDE_SETTING(overrideName) \ - class SettingsBase : public QObject { @@ -91,7 +89,6 @@ public: DEFINE_SETTING(PreLaunchCommand, QString, "") DEFINE_SETTING(PostExitCommand, QString, "") -protected: virtual QVariant getValue(const QString& name, QVariant defVal = QVariant()) const = 0; virtual void setValue(const QString& name, QVariant val) = 0; }; @@ -104,10 +101,10 @@ public: QSettings& getConfig() { return config; } -protected: virtual QVariant getValue(const QString &name, QVariant defVal = QVariant()) const; virtual void setValue(const QString& name, QVariant val); - + +protected: QSettings config; }; diff --git a/data/inst/instance.cpp b/data/inst/instance.cpp new file mode 100644 index 00000000..5cde2233 --- /dev/null +++ b/data/inst/instance.cpp @@ -0,0 +1,21 @@ +/* Copyright 2013 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 "instance.h" + +Instance::Instance(QObject *parent) : + SettingsBase(parent) +{ +} diff --git a/data/inst/instance.h b/data/inst/instance.h new file mode 100644 index 00000000..6b1991b6 --- /dev/null +++ b/data/inst/instance.h @@ -0,0 +1,320 @@ +/* Copyright 2013 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. + */ + +#ifndef INSTANCE_H +#define INSTANCE_H + +#include +#include + +#include "data/appsettings.h" +#include "data/inifile.h" + +#define DEFINE_OVERRIDDEN_SETTING_ADVANCED(funcName, cfgEntryName, typeName) \ + typeName get ## funcName() const { return getField(cfgEntryName, settings->get ## funcName()).value(); } + +#define DEFINE_OVERRIDDEN_SETTING(funcName, typeName) \ + DEFINE_OVERRIDDEN_SETTING_ADVANCED(funcName, STR_VAL(funcName), typeName) + +class InstanceList; + +/*! + * \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 Instance : public SettingsBase +{ + Q_OBJECT +public: + explicit Instance(QObject *parent = 0); + + // Please, for the sake of my (and everyone else's) sanity, at least keep this shit + // *somewhat* organized. Also, documentation is semi-important here. Please don't + // leave undocumented stuff behind. + + + //////// STUFF //////// + + /*! + * \brief Get the instance's ID. + * This is a unique identifier string that is, by default, set to the + * instance's folder name. It's not always the instance's folder name, + * however, as any class deriving from Instance can override the id() + * method and change how the ID is determined. The instance's ID + * should always remain constant. Undefined behavior results if an + * already loaded instance's ID changes. + * + * \return The instance's ID. + */ + virtual QString id(); + + /*! + * \brief Gets the path to the instance's root directory. + * \return The path to the instance's root directory. + */ + virtual QString rootDir(); + + /*! + * \brief Gets the instance list that this instance is a part of. + * Returns NULL if this instance is not in a list + * (the parent is not an InstanceList). + * \return A pointer to the InstanceList containing this instance. + */ + virtual InstanceList *instList(); + + + /*! + * \brief Gets this instance's group. + * This function is used by the instance grouping system and should + * not be touched by classes deriving from Instance. + * \return The instance's group name. + * \sa setGroup() + */ + QString group() const { return m_group; } + + /*! + * \brief Sets the instance's group. + * \param group The instance's new group name. + * \sa group() + */ + void setGroup(const QString &group) { m_group = group; emit groupChanged(this, group); } + + + //////// FIELDS AND SETTINGS //////// + // Fields are options stored in the instance's config file that are specific + // to instances (not a part of SettingsBase). Settings are things overridden + // from SettingsBase. + + + ////// Fields ////// + + //// General Info //// + + /*! + * \brief Gets this instance's name. + * This is the name that will be displayed to the user. + * \return The instance's name. + * \sa setName + */ + virtual QString name() { return getField("name", "Unnamed Instance").value(); } + + /*! + * \brief Sets the instance's name + * \param val The instance's new name. + */ + virtual void setName(QString val) { setField("name", val); } + + /*! + * \brief Gets the instance's icon key. + * \return The instance's icon key. + * \sa setIconKey() + */ + virtual QString iconKey() const { return getField("iconKey", "default").value(); } + + /*! + * \brief Sets the instance's icon key. + * \param val The new icon key. + */ + virtual void setIconKey(QString val) { setField("iconKey", val); } + + + /*! + * \brief Gets the instance's notes. + * \return The instances notes. + */ + virtual QString notes() const { return getField("notes", "").value(); } + + /*! + * \brief Sets the instance's notes. + * \param val The instance's new notes. + */ + virtual void setNotes(QString val) { setField("notes", val); } + + + /*! + * \brief Checks if the instance's minecraft.jar needs to be rebuilt. + * If this is true, the instance's mods will be reinstalled to its + * minecraft.jar file. This value is automatically set to true when + * the jar mod list changes. + * \return Whether or not the instance's jar file should be rebuilt. + */ + virtual bool shouldRebuild() const { return getField("NeedsRebuild", false).value(); } + + /*! + * \brief Sets whether or not the instance's minecraft.jar needs to be rebuilt. + * \param val Whether the instance's minecraft needs to be rebuilt or not. + */ + virtual void setShouldRebuild(bool val) { setField("NeedsRebuild", val); } + + + //// Version Stuff //// + + /*! + * \brief Sets the instance's current version. + * This value represents the instance's current version. If this value + * is different from intendedVersion(), the instance should be updated. + * This value is updated by the updateCurrentVersion() function. + * \return A string representing the instance's current version. + */ + virtual QString currentVersion() { return getField("JarVersion", "Unknown").value(); } + + /*! + * \brief Sets the instance's current version. + * This is used to keep track of the instance's current version. Don't + * mess with this unless you know what you're doing. + * \param val The new value. + */ + virtual void setCurrentVersion(QString val) { setField("JarVersion", val); } + + + /*! + * \brief Gets the version of LWJGL that this instance should use. + * If no LWJGL version is specified in the instance's config file, + * defaults to "Mojang" + * \return The instance's LWJGL version. + */ + virtual QString lwjglVersion() { return getField("LwjglVersion", "Mojang").value(); } + + /*! + * \brief Sets the version of LWJGL that this instance should use. + * \param val The LWJGL version to use + */ + virtual void setLWJGLVersion(QString val) { setField("LwjglVersion", val); } + + + /*! + * \brief Gets the version that this instance should try to update to. + * If this value differs from currentVersion(), the instance will + * download the intended version when it launches. + * \return The instance's intended version. + */ + virtual QString intendedVersion() { return getField("IntendedJarVersion", currentVersion()).value(); } + + /*! + * \brief Sets the version that this instance should try to update to. + * \param val The instance's new intended version. + */ + virtual void setIntendedVersion(QString val) { setField("IntendedJarVersion", val); } + + + + //// Timestamps //// + + /*! + * \brief Gets the time that the instance was last launched. + * Measured in milliseconds since epoch. QDateTime::currentMSecsSinceEpoch() + * \return The time that the instance was last launched. + */ + virtual qint64 lastLaunch() { return getField("lastLaunchTime", 0).value(); } + + /*! + * \brief Sets the time that the instance was last launched. + * \param val The time to set. Defaults to QDateTime::currentMSecsSinceEpoch() + */ + virtual void setLastLaunch(qint64 val = QDateTime::currentMSecsSinceEpoch()) + { setField("lastLaunchTime", val); } + + + ////// Settings ////// + + //// Java Settings //// + DEFINE_OVERRIDDEN_SETTING_ADVANCED(JavaPath, JPATHKEY, QString) + DEFINE_OVERRIDDEN_SETTING(JvmArgs, QString) + + //// Custom Commands //// + DEFINE_OVERRIDDEN_SETTING(PreLaunchCommand, QString) + DEFINE_OVERRIDDEN_SETTING(PostExitCommand, QString) + + //// Memory //// + DEFINE_OVERRIDDEN_SETTING(MinMemAlloc, int) + DEFINE_OVERRIDDEN_SETTING(MaxMemAlloc, int) + + //// Auto login //// + DEFINE_OVERRIDDEN_SETTING(AutoLogin, bool) + + // This little guy here is to keep Qt Creator from being a dumbass and + // auto-indenting the lines below the macros. Seriously, it's really annoying. + ; + + + //////// OTHER FUNCTIONS //////// + + //// Version System //// + + /*! + * \brief Checks whether or not the currentVersion of the instance needs to be updated. + * If this returns true, updateCurrentVersion is called. In the + * standard instance, this is determined by checking a timestamp + * stored in the instance config file against the last modified time of Minecraft.jar. + * \return True if updateCurrentVersion() should be called. + */ + virtual bool shouldUpdateCurrentVersion() = 0; + + /*! + * \brief Updates the current version. + * This function should first set the current version timestamp + * (setCurrentVersionTimestamp()) to the current time. Next, if + * keepCurrent is false, this function should check what the + * instance's current version is and call setCurrentVersion() to + * update it. This function will automatically be called when the + * instance is loaded if shouldUpdateCurrentVersion returns true. + * \param keepCurrent If true, only the version timestamp will be updated. + */ + virtual void updateCurrentVersion(bool keepCurrent = false) = 0; + +signals: + /*! + * \brief Signal emitted when the instance's group changes. + * \param inst Pointer to the instance whose group changed. + * \param newGroup The instance's new group. + */ + void groupChanged(Instance *inst, QString newGroup); + +protected: + /*! + * \brief Gets the value of the given field in the instance's config file. + * If the value isn't in the config file, defVal is returned instead. + * \param name The name of the field in the config file. + * \param defVal The default value. + * \return The value of the given field or defVal if the field doesn't exist. + * \sa setField() + */ + virtual QVariant getField(const QString &name, QVariant defVal = QVariant()) const; + + /*! + * \brief Sets the value of the given field in the config file. + * \param name The name of the field in the config file. + * \param val The value to set the field to. + * \sa getField() + */ + virtual void setField(const QString &name, QVariant val); + + // Overrides for SettingBase stuff. + virtual QVariant getValue(const QString &name, QVariant defVal = QVariant()) const + { return settings->getValue(name, defVal); } + virtual void setValue(const QString &name, QVariant val) + { setField(name, val); } + + INIFile config; + +private: + QString m_group; +}; + +#endif // INSTANCE_H diff --git a/data/inst/instancelist.cpp b/data/inst/instancelist.cpp new file mode 100644 index 00000000..62367ed6 --- /dev/null +++ b/data/inst/instancelist.cpp @@ -0,0 +1,23 @@ +/* Copyright 2013 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 "instancelist.h" + +#include "data/siglist_impl.h" + +InstanceList::InstanceList(QObject *parent) : + QObject(parent) +{ +} diff --git a/data/inst/instancelist.h b/data/inst/instancelist.h new file mode 100644 index 00000000..72dec5f0 --- /dev/null +++ b/data/inst/instancelist.h @@ -0,0 +1,39 @@ +/* Copyright 2013 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. + */ + +#ifndef INSTANCELIST_H +#define INSTANCELIST_H + +#include + +#include + +#include "data/siglist.h" + +class Instance; + +class InstanceList : public QObject, SigList> +{ + Q_OBJECT +public: + explicit InstanceList(QObject *parent = 0); + +signals: + +public slots: + +}; + +#endif // INSTANCELIST_H diff --git a/data/instancebase.cpp b/data/instancebase.cpp deleted file mode 100644 index a5ef35a2..00000000 --- a/data/instancebase.cpp +++ /dev/null @@ -1,109 +0,0 @@ -/* Copyright 2013 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 "instancebase.h" - -#include -#include - -#include "../util/pathutils.h" - -InstanceBase::InstanceBase(QString dir, QObject *parent) : - QObject(parent), - rootDir(dir) -{ - QFileInfo cfgFile(PathCombine(rootDir, "instance.cfg")); - - if (cfgFile.exists()) - { - if(!config.loadFile(cfgFile.absoluteFilePath())) - { - QString debugmsg("Can't load instance config file for instance "); - debugmsg+= getInstID(); - qDebug(debugmsg.toLocal8Bit()); - } - } - else - { - QString debugmsg("Can't find instance config file for instance "); - debugmsg+= getInstID(); - debugmsg += " : "; - debugmsg += - debugmsg+=" ... is this an instance even?"; - qDebug(debugmsg.toLocal8Bit()); - } - currentGroup = nullptr; -} - -QString InstanceBase::getRootDir() const -{ - return rootDir; -} - - -///////////// Config Values ///////////// - -// Name -QString InstanceBase::getInstName() const -{ - return config.get("name", "Unnamed").toString(); -} - -void InstanceBase::setInstName(QString name) -{ - config.set("name", name); -} - -QString InstanceBase::getInstID() const -{ - return QDir(rootDir).dirName(); -} - -InstanceModelItem* InstanceBase::getParent() const -{ - return currentGroup; -} - -QVariant InstanceBase::data ( int role ) const -{ - switch(role) - { - case Qt::DisplayRole: - return getInstName(); - default: - return QVariant(); - } -} -int InstanceBase::getRow() const -{ - return currentGroup->getIndexOf((InstanceBase*)this); -} - -InstanceModelItem* InstanceBase::getChild ( int index ) const -{ - return nullptr; -} -InstanceModel* InstanceBase::getModel() const -{ - return currentGroup->getModel(); -} -IMI_type InstanceBase::getModelItemType() const -{ - return IMI_Instance; -} -int InstanceBase::numChildren() const -{ - return 0; -} diff --git a/data/instancebase.h b/data/instancebase.h deleted file mode 100644 index fa043c5f..00000000 --- a/data/instancebase.h +++ /dev/null @@ -1,58 +0,0 @@ -/* Copyright 2013 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. - */ - -#ifndef INSTANCEBASE_H -#define INSTANCEBASE_H - -#include -#include - -#include "../data/inifile.h" -#include "instancemodel.h" - -class InstanceBase : public QObject, public InstanceModelItem -{ - friend class InstanceGroup; - Q_OBJECT -public: - explicit InstanceBase(QString rootDir, QObject *parent = 0); - - QString getRootDir() const; - - QString getInstName() const; - void setInstName(QString name); - - QString getInstID() const; - - virtual IMI_type getModelItemType() const; - virtual InstanceModelItem* getParent() const; - virtual int numChildren() const; - virtual InstanceModelItem* getChild ( int index ) const; - virtual InstanceModel* getModel() const; - virtual QVariant data ( int column ) const; - virtual int getRow() const; - -private: - void setGroup ( InstanceGroup* group ) - { - currentGroup = group; - }; - - QString rootDir; - INIFile config; - InstanceGroup * currentGroup; -}; - -#endif // INSTANCEBASE_H diff --git a/data/instancemodel.h b/data/instancemodel.h index 4bd34831..9678fef0 100644 --- a/data/instancemodel.h +++ b/data/instancemodel.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef INSTANCELIST_H -#define INSTANCELIST_H +#ifndef INSTANCEMODEL_H +#define INSTANCEMODEL_H #include #include @@ -134,4 +134,4 @@ private: InstanceGroup * implicitGroup; }; -#endif // INSTANCELIST_H +#endif // INSTANCEMODEL_H diff --git a/data/siglist_imp.h b/data/siglist_imp.h deleted file mode 100644 index 16ddd9b0..00000000 --- a/data/siglist_imp.h +++ /dev/null @@ -1,156 +0,0 @@ -/* Copyright 2013 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 "siglist.h" - -template -void SigList::append(const T &value) -{ - QList::append(value); - onItemAdded(value, length() - 1); -} - -template -void SigList::prepend(const T &value) -{ - QList::prepend(value); - onItemAdded(value, 0); -} - -template -void SigList::append(const QList &other) -{ - int index = length(); - QList::append(other); - onItemsAdded(other, index); -} - -template -void SigList::clear() -{ - QList::clear(); - onInvalidated(); -} - -template -void SigList::erase(QList::iterator pos) -{ - T value = *pos; - int index = indexOf(*pos); - QList::erase(pos); - onItemRemoved(value, index); -} - -template -void SigList::erase(QList::iterator first, QList::iterator last) -{ - QList removedValues; - int firstIndex = indexOf(*first); - - for (QList::iterator iter = first; iter < last; iter++) - { - removedValues << *iter; - QList::erase(iter); - } - - onItemsRemoved(removedValues, firstIndex); -} - -template -void SigList::insert(int i, const T &t) -{ - QList::insert(i, t); - onItemAdded(t, i); -} - -template -void SigList::insert(QList::iterator before, const T &t) -{ - QList::insert(before, t); - onItemAdded(t, indexOf(t)); -} - -template -void SigList::move(int from, int to) -{ - const T &item = at(from); - QList::move(from, to); - onItemMoved(item, from, to); -} - -template -int SigList::removeAll(const T &t) -{ - int retVal = QList::removeAll(t); - onInvalidated(); - return retVal; -} - -template -bool SigList::removeOne(const T &t) -{ - int index = indexOf(t); - if (QList::removeOne(t)) - { - onItemRemoved(t, index); - return true; - } - return false; -} - -template -void SigList::swap(QList &other) -{ - QList::swap(other); - onInvalidated(); -} - -template -void SigList::swap(int i, int j) -{ - const T &item1 = at(i); - const T &item2 = at(j); - QList::swap(i, j); - onItemMoved(item1, i, j); - onItemMoved(item2, j, i); -} - -template -T SigList::takeAt(int i) -{ - T val = QList::takeAt(i); - onItemRemoved(val, i); - return val; -} - -template -T SigList::takeFirst() -{ - return takeAt(0); -} - -template -T SigList::takeLast() -{ - return takeAt(length() - 1); -} - -template -QList &SigList::operator =(const QList &other) -{ - QList::operator =(other); - onInvalidated(); - return *this; -} diff --git a/data/siglist_impl.h b/data/siglist_impl.h new file mode 100644 index 00000000..16ddd9b0 --- /dev/null +++ b/data/siglist_impl.h @@ -0,0 +1,156 @@ +/* Copyright 2013 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 "siglist.h" + +template +void SigList::append(const T &value) +{ + QList::append(value); + onItemAdded(value, length() - 1); +} + +template +void SigList::prepend(const T &value) +{ + QList::prepend(value); + onItemAdded(value, 0); +} + +template +void SigList::append(const QList &other) +{ + int index = length(); + QList::append(other); + onItemsAdded(other, index); +} + +template +void SigList::clear() +{ + QList::clear(); + onInvalidated(); +} + +template +void SigList::erase(QList::iterator pos) +{ + T value = *pos; + int index = indexOf(*pos); + QList::erase(pos); + onItemRemoved(value, index); +} + +template +void SigList::erase(QList::iterator first, QList::iterator last) +{ + QList removedValues; + int firstIndex = indexOf(*first); + + for (QList::iterator iter = first; iter < last; iter++) + { + removedValues << *iter; + QList::erase(iter); + } + + onItemsRemoved(removedValues, firstIndex); +} + +template +void SigList::insert(int i, const T &t) +{ + QList::insert(i, t); + onItemAdded(t, i); +} + +template +void SigList::insert(QList::iterator before, const T &t) +{ + QList::insert(before, t); + onItemAdded(t, indexOf(t)); +} + +template +void SigList::move(int from, int to) +{ + const T &item = at(from); + QList::move(from, to); + onItemMoved(item, from, to); +} + +template +int SigList::removeAll(const T &t) +{ + int retVal = QList::removeAll(t); + onInvalidated(); + return retVal; +} + +template +bool SigList::removeOne(const T &t) +{ + int index = indexOf(t); + if (QList::removeOne(t)) + { + onItemRemoved(t, index); + return true; + } + return false; +} + +template +void SigList::swap(QList &other) +{ + QList::swap(other); + onInvalidated(); +} + +template +void SigList::swap(int i, int j) +{ + const T &item1 = at(i); + const T &item2 = at(j); + QList::swap(i, j); + onItemMoved(item1, i, j); + onItemMoved(item2, j, i); +} + +template +T SigList::takeAt(int i) +{ + T val = QList::takeAt(i); + onItemRemoved(val, i); + return val; +} + +template +T SigList::takeFirst() +{ + return takeAt(0); +} + +template +T SigList::takeLast() +{ + return takeAt(length() - 1); +} + +template +QList &SigList::operator =(const QList &other) +{ + QList::operator =(other); + onInvalidated(); + return *this; +} diff --git a/data/stdinstance.cpp b/data/stdinstance.cpp deleted file mode 100644 index 4618f5ca..00000000 --- a/data/stdinstance.cpp +++ /dev/null @@ -1,22 +0,0 @@ -/* Copyright 2013 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 "stdinstance.h" - -StdInstance::StdInstance(QString rootDir, QObject* parent) : - InstanceBase(rootDir, parent) -{ - -} diff --git a/data/stdinstance.h b/data/stdinstance.h deleted file mode 100644 index 79b87601..00000000 --- a/data/stdinstance.h +++ /dev/null @@ -1,28 +0,0 @@ -/* Copyright 2013 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. - */ - -#ifndef STDINSTANCE_H -#define STDINSTANCE_H - -#include "instancebase.h" - -// Standard client instance. -class StdInstance : public InstanceBase -{ -public: - explicit StdInstance(QString rootDir, QObject *parent = 0); -}; - -#endif // STDINSTANCE_H diff --git a/data/version/instversion.cpp b/data/version/instversion.cpp new file mode 100644 index 00000000..1493153a --- /dev/null +++ b/data/version/instversion.cpp @@ -0,0 +1,32 @@ +/* Copyright 2013 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 "instversion.h" +#include "instversionlist.h" + +InstVersion::InstVersion(InstVersionList *parent) : + QObject(parent) +{ + +} + +InstVersionList *InstVersion::versionList() const +{ + // Parent should *always* be an InstVersionList + if (!parent() || !parent()->inherits("InstVersionList")) + return NULL; + else + return (InstVersionList *)parent(); +} diff --git a/data/version/instversion.h b/data/version/instversion.h new file mode 100644 index 00000000..361563fd --- /dev/null +++ b/data/version/instversion.h @@ -0,0 +1,51 @@ +/* Copyright 2013 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. + */ + +#ifndef INSTVERSION_H +#define INSTVERSION_H + +#include + +class InstVersionList; + +class InstVersion : public QObject +{ + Q_OBJECT +public: + // Constructs a new InstVersion with the given parent. The parent *must* + // be the InstVersionList that contains this InstVersion. The InstVersion + // should be added to the list immediately after being created as any calls + // to id() will likely fail unless the InstVersion is in a list. + explicit InstVersion(InstVersionList *parent = 0); + + // Returns this InstVersion's ID. This is usually just the InstVersion's index + // within its InstVersionList, but not always. + // If this InstVersion is not in an InstVersionList, returns -1. + virtual int id() const = 0; + + // Returns this InstVersion's name. This is displayed to the user in the GUI + // and is usually just the version number ("1.4.7"), for example. + virtual QString name() const = 0; + + // Returns this InstVersion's name. This is usually displayed to the user + // in the GUI and specifies what kind of version this is. For example: it + // could be "Snapshot", "Latest Version", "MCNostalgia", etc. + virtual QString type() const = 0; + + // Returns the version list that this InstVersion is a part of. + virtual InstVersionList *versionList() const; +}; + +#endif // INSTVERSION_H diff --git a/data/version/instversionlist.cpp b/data/version/instversionlist.cpp new file mode 100644 index 00000000..ab57f94c --- /dev/null +++ b/data/version/instversionlist.cpp @@ -0,0 +1,21 @@ +/* Copyright 2013 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 "instversionlist.h" + +InstVersionList::InstVersionList() : + QObject(NULL) +{ +} diff --git a/data/version/instversionlist.h b/data/version/instversionlist.h new file mode 100644 index 00000000..f79bc1b0 --- /dev/null +++ b/data/version/instversionlist.h @@ -0,0 +1,43 @@ +/* Copyright 2013 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. + */ + +#ifndef INSTVERSIONLIST_H +#define INSTVERSIONLIST_H + +#include + +class InstVersion; + +// 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. +class InstVersionList : public QObject +{ + Q_OBJECT +public: + explicit InstVersionList(); + + // Reloads the version list. + virtual void loadVersionList() = 0; + + // Gets the version at the given index. + virtual const InstVersion *at(int i) const = 0; + + // Returns the number of versions in the list. + virtual int count() const = 0; +}; + +#endif // INSTVERSIONLIST_H -- cgit v1.2.3 From 15c7efffa1af8c7b4fba710c30c53b6126bfa9db Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 18 Feb 2013 14:50:11 -0600 Subject: Added InstanceLoader and InstanceType headers. --- data/inst/instance.cpp | 2 +- data/inst/instance.h | 4 +- data/inst/instanceloader.cpp | 21 ++++++++++ data/inst/instanceloader.h | 94 ++++++++++++++++++++++++++++++++++++++++++++ data/inst/instancetype.cpp | 21 ++++++++++ data/inst/instancetype.h | 81 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 221 insertions(+), 2 deletions(-) create mode 100644 data/inst/instanceloader.cpp create mode 100644 data/inst/instanceloader.h create mode 100644 data/inst/instancetype.cpp create mode 100644 data/inst/instancetype.h (limited to 'data') diff --git a/data/inst/instance.cpp b/data/inst/instance.cpp index 5cde2233..d2240bc5 100644 --- a/data/inst/instance.cpp +++ b/data/inst/instance.cpp @@ -15,7 +15,7 @@ #include "instance.h" -Instance::Instance(QObject *parent) : +Instance::Instance(const QString &rootDir, QObject *parent) : SettingsBase(parent) { } diff --git a/data/inst/instance.h b/data/inst/instance.h index 6b1991b6..1b282b03 100644 --- a/data/inst/instance.h +++ b/data/inst/instance.h @@ -42,7 +42,7 @@ class Instance : public SettingsBase { Q_OBJECT public: - explicit Instance(QObject *parent = 0); + explicit Instance(const QString &rootDir, QObject *parent = 0); // Please, for the sake of my (and everyone else's) sanity, at least keep this shit // *somewhat* organized. Also, documentation is semi-important here. Please don't @@ -315,6 +315,8 @@ protected: private: QString m_group; + + QString m_rootDir; }; #endif // INSTANCE_H diff --git a/data/inst/instanceloader.cpp b/data/inst/instanceloader.cpp new file mode 100644 index 00000000..59fed951 --- /dev/null +++ b/data/inst/instanceloader.cpp @@ -0,0 +1,21 @@ +/* Copyright 2013 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 "instanceloader.h" + +InstanceLoader::InstanceLoader(QObject *parent) : + QObject(parent) +{ +} diff --git a/data/inst/instanceloader.h b/data/inst/instanceloader.h new file mode 100644 index 00000000..943a9b2c --- /dev/null +++ b/data/inst/instanceloader.h @@ -0,0 +1,94 @@ +/* Copyright 2013 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. + */ + +#ifndef INSTANCELOADER_H +#define INSTANCELOADER_H + +#include + +class InstanceType; +class Instance; + +/*! + * \brief The InstanceLoader is a singleton that manages all of the instance types and handles loading and creating instances. + * Instance types are registered with the instance loader through its registerInstType() function. + * Creating instances is done through the InstanceLoader's createInstance() function. This function takes + */ +class InstanceLoader : public QObject +{ + Q_OBJECT +public: + static InstanceLoader instLoader; + + /*! + * \brief Error codes returned by functions in the InstanceLoader and InstanceType classes. + * NoError indicates that no error occurred. + * OtherError indicates that an unspecified error occurred. + * TypeIDExists is returned by registerInstanceType() if the ID of the type being registered already exists. + * + * TypeNotRegistered is returned by createInstance() and loadInstance() when the given type is not registered. + * InstExists is returned by createInstance() if the given instance directory is already an instance. + * NotAnInstance is returned by loadInstance() if the given instance directory is not a valid instance. + */ + enum InstTypeError + { + NoError = 0, + OtherError, + + TypeIDExists, + + TypeNotRegistered, + InstExists, + NotAnInstance + }; + + /*! + * \brief Registers the given InstanceType with the instance loader. + * This causes the instance loader to take ownership of the given + * instance type (meaning the instance type's parent will be set to + * the instance loader). + * \param type The InstanceType to register. + * \return An InstTypeError error code. + * TypeIDExists if the given type's is already registered to another instance type. + */ + InstTypeError registerInstanceType(InstanceType *type); + + /*! + * \brief Creates an instance with the given type and stores it in inst. + * \param inst Pointer to store the created instance in. + * \param type The type of instance to create. + * \param instDir The instance's directory. + * \return An InstTypeError error code. + * TypeNotRegistered if the given type is not registered with the InstanceLoader. + * InstExists if the given instance directory is already an instance. + */ + InstTypeError createInstance(Instance *inst, const InstanceType *type, const QString &instDir); + + /*! + * \brief Loads an instance from the given directory. + * \param inst Pointer to store the loaded instance in. + * \param type The type of instance to load. + * \param instDir The instance's directory. + * \return An InstTypeError error code. + * TypeNotRegistered if the given type is not registered with the InstanceLoader. + * NotAnInstance if the given instance directory isn't a valid instance. + */ + InstTypeError loadInstance(Instance *inst, const InstanceType *type, const QString &instDir); + +private: + explicit InstanceLoader(QObject *parent = 0); +}; + +#endif // INSTANCELOADER_H diff --git a/data/inst/instancetype.cpp b/data/inst/instancetype.cpp new file mode 100644 index 00000000..4f27542d --- /dev/null +++ b/data/inst/instancetype.cpp @@ -0,0 +1,21 @@ +/* Copyright 2013 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 "instancetype.h" + +InstanceType::InstanceType(QObject *parent) : + QObject(parent) +{ +} diff --git a/data/inst/instancetype.h b/data/inst/instancetype.h new file mode 100644 index 00000000..1f187310 --- /dev/null +++ b/data/inst/instancetype.h @@ -0,0 +1,81 @@ +/* Copyright 2013 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. + */ + +#ifndef INSTANCETYPE_H +#define INSTANCETYPE_H + +#include + +#include "instanceloader.h" + +/*! + * \brief The InstanceType class is a base class for all instance types. + * It handles loading and creating instances of a certain type. There should be + * one of these for each type of instance and they should be registered with the + * InstanceLoader. + * To create an instance, the InstanceLoader calls the type's createInstance() + * function. Loading is done through the loadInstance() function. + */ +class InstanceType : public QObject +{ + Q_OBJECT +public: + explicit InstanceType(QObject *parent = 0); + + /*! + * \brief Gets the ID for this instance type. + * By default this is the name of the Instance class that this type + * creates, but this can be changed by overriding this function. + * The type ID should be unique as it is used to identify the type + * of instances when they are loaded. + * Changing this value at runtime results in undefined behavior. + * \return This instance type's ID string. + */ + virtual QString typeID() const = 0; + + /*! + * \brief Gets the name of this instance type as it is displayed to the user. + * \return The instance type's display name. + */ + virtual QString displayName() const = 0; + + /*! + * \brief Gets a longer, more detailed description of this instance type. + * \return The instance type's description. + */ + virtual QString description() const = 0; + + /*! + * \brief Creates an instance and stores it in inst. + * \param inst Pointer to store the created instance in. + * \param instDir The instance's directory. + * \return An InstTypeError error code. + * TypeNotRegistered if the given type is not registered with the InstanceLoader. + * InstExists if the given instance directory is already an instance. + */ + virtual InstanceLoader::InstTypeError createInstance(Instance *inst, const QString &instDir) = 0; + + /*! + * \brief Loads an instance from the given directory. + * \param inst Pointer to store the loaded instance in. + * \param instDir The instance's directory. + * \return An InstTypeError error code. + * TypeNotRegistered if the given type is not registered with the InstanceLoader. + * NotAnInstance if the given instance directory isn't a valid instance. + */ + virtual InstanceLoader::InstTypeError loadInstance(Instance *inst, const QString &instDir) = 0; +}; + +#endif // INSTANCETYPE_H -- cgit v1.2.3 From d3c4db8f3444c8531cb5a02ce3c95d5673ffb1a5 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 18 Feb 2013 15:39:01 -0600 Subject: Implemented instance loader. --- data/inst/instanceloader.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++ data/inst/instanceloader.h | 25 +++++++++++++++++- data/inst/instancetype.h | 8 ++++-- 3 files changed, 90 insertions(+), 3 deletions(-) (limited to 'data') diff --git a/data/inst/instanceloader.cpp b/data/inst/instanceloader.cpp index 59fed951..bc43c061 100644 --- a/data/inst/instanceloader.cpp +++ b/data/inst/instanceloader.cpp @@ -15,7 +15,67 @@ #include "instanceloader.h" +#include "instancetype.h" + InstanceLoader::InstanceLoader(QObject *parent) : QObject(parent) { } + + +InstanceLoader::InstTypeError InstanceLoader::registerInstanceType(InstanceType *type) +{ + // Check to see if the type ID exists. + if (m_typeMap.contains(type->typeID())) + return TypeIDExists; + + // Set the parent to this. + type->setParent(this); + + // Add it to the map. + m_typeMap.insert(type->typeID(), type); + return NoError; +} + +InstanceLoader::InstTypeError InstanceLoader::createInstance(Instance *inst, + const InstanceType *type, + const QString &instDir) +{ + // Check if the type is registered. + if (!type || findType(type->typeID()) != type) + return TypeNotRegistered; + + // Create the instance. + return type->createInstance(inst, instDir); +} + +InstanceLoader::InstTypeError InstanceLoader::loadInstance(Instance *inst, + const InstanceType *type, + const QString &instDir) +{ + // Check if the type is registered. + if (!type || findType(type->typeID()) != type) + return TypeNotRegistered; + + return type->loadInstance(inst, instDir); +} + +const InstanceType *InstanceLoader::findType(const QString &id) +{ + if (!m_typeMap.contains(id)) + return NULL; + else + return m_typeMap[id]; +} + +InstTypeList InstanceLoader::typeList() +{ + InstTypeList typeList; + + for (auto iter = m_typeMap.begin(); iter != m_typeMap.end(); iter++) + { + typeList.append(*iter); + } + + return typeList; +} diff --git a/data/inst/instanceloader.h b/data/inst/instanceloader.h index 943a9b2c..1a8a598f 100644 --- a/data/inst/instanceloader.h +++ b/data/inst/instanceloader.h @@ -17,10 +17,14 @@ #define INSTANCELOADER_H #include +#include +#include class InstanceType; class Instance; +typedef QList InstTypeList; + /*! * \brief The InstanceLoader is a singleton that manages all of the instance types and handles loading and creating instances. * Instance types are registered with the instance loader through its registerInstType() function. @@ -41,6 +45,7 @@ public: * TypeNotRegistered is returned by createInstance() and loadInstance() when the given type is not registered. * InstExists is returned by createInstance() if the given instance directory is already an instance. * NotAnInstance is returned by loadInstance() if the given instance directory is not a valid instance. + * WrongInstType is returned by loadInstance() if the given instance directory's type doesn't match the given type. */ enum InstTypeError { @@ -51,7 +56,8 @@ public: TypeNotRegistered, InstExists, - NotAnInstance + NotAnInstance, + WrongInstType }; /*! @@ -84,11 +90,28 @@ public: * \return An InstTypeError error code. * TypeNotRegistered if the given type is not registered with the InstanceLoader. * NotAnInstance if the given instance directory isn't a valid instance. + * WrongInstType if the given instance directory's type isn't the same as the given type. */ InstTypeError loadInstance(Instance *inst, const InstanceType *type, const QString &instDir); + /*! + * \brief Finds an instance type with the given ID. + * If one cannot be found, returns NULL. + * \param id The ID of the type to find. + * \return The type with the given ID. NULL if none were found. + */ + const InstanceType *findType(const QString &id); + + /*! + * \brief Gets a list of the registered instance types. + * \return A list of instance types. + */ + InstTypeList typeList(); + private: explicit InstanceLoader(QObject *parent = 0); + + QMap m_typeMap; }; #endif // INSTANCELOADER_H diff --git a/data/inst/instancetype.h b/data/inst/instancetype.h index 1f187310..ec3f5b87 100644 --- a/data/inst/instancetype.h +++ b/data/inst/instancetype.h @@ -34,6 +34,8 @@ class InstanceType : public QObject public: explicit InstanceType(QObject *parent = 0); + friend class InstanceLoader; + /*! * \brief Gets the ID for this instance type. * By default this is the name of the Instance class that this type @@ -57,6 +59,7 @@ public: */ virtual QString description() const = 0; +protected: /*! * \brief Creates an instance and stores it in inst. * \param inst Pointer to store the created instance in. @@ -65,7 +68,7 @@ public: * TypeNotRegistered if the given type is not registered with the InstanceLoader. * InstExists if the given instance directory is already an instance. */ - virtual InstanceLoader::InstTypeError createInstance(Instance *inst, const QString &instDir) = 0; + virtual InstanceLoader::InstTypeError createInstance(Instance *inst, const QString &instDir) const = 0; /*! * \brief Loads an instance from the given directory. @@ -74,8 +77,9 @@ public: * \return An InstTypeError error code. * TypeNotRegistered if the given type is not registered with the InstanceLoader. * NotAnInstance if the given instance directory isn't a valid instance. + * WrongInstType if the given instance directory's type isn't an instance of this type. */ - virtual InstanceLoader::InstTypeError loadInstance(Instance *inst, const QString &instDir) = 0; + virtual InstanceLoader::InstTypeError loadInstance(Instance *inst, const QString &instDir) const = 0; }; #endif // INSTANCETYPE_H -- cgit v1.2.3 From 80cd8b33aa12933392bd2293a5e91ae696dbe6be Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 18 Feb 2013 16:58:53 -0600 Subject: Implemented Instance --- data/inst/instance.cpp | 34 ++++++++++++++++++++++++++++++++++ data/inst/instanceloader.cpp | 5 +++-- data/inst/instanceloader.h | 2 +- 3 files changed, 38 insertions(+), 3 deletions(-) (limited to 'data') diff --git a/data/inst/instance.cpp b/data/inst/instance.cpp index d2240bc5..8561ed9f 100644 --- a/data/inst/instance.cpp +++ b/data/inst/instance.cpp @@ -15,7 +15,41 @@ #include "instance.h" +#include + +#include "util/pathutils.h" + Instance::Instance(const QString &rootDir, QObject *parent) : SettingsBase(parent) { + m_rootDir = rootDir; + config.loadFile(PathCombine(rootDir, "instance.cfg")); +} + +QString Instance::id() +{ + return QFileInfo(rootDir()).baseName(); +} + +QString Instance::rootDir() +{ + return m_rootDir; +} + +InstanceList *Instance::instList() +{ + if (parent()->inherits("InstanceList")) + return (InstanceList *)parent(); + else + return NULL; +} + +QVariant Instance::getField(const QString &name, QVariant defVal) const +{ + return config.get(name, defVal); +} + +void Instance::setField(const QString &name, QVariant val) +{ + config.set(name, val); } diff --git a/data/inst/instanceloader.cpp b/data/inst/instanceloader.cpp index bc43c061..1a53bfa7 100644 --- a/data/inst/instanceloader.cpp +++ b/data/inst/instanceloader.cpp @@ -17,9 +17,10 @@ #include "instancetype.h" -InstanceLoader::InstanceLoader(QObject *parent) : - QObject(parent) +InstanceLoader::InstanceLoader() : + QObject(NULL) { + } diff --git a/data/inst/instanceloader.h b/data/inst/instanceloader.h index 1a8a598f..b7092edc 100644 --- a/data/inst/instanceloader.h +++ b/data/inst/instanceloader.h @@ -109,7 +109,7 @@ public: InstTypeList typeList(); private: - explicit InstanceLoader(QObject *parent = 0); + InstanceLoader(); QMap m_typeMap; }; -- cgit v1.2.3 From 6e5017e48b633e20bbba61ac2e7cd58e693aec4e Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 19 Feb 2013 12:15:22 -0600 Subject: Implemented loadList() stuff. --- data/inst/instance.h | 27 --- data/inst/instancelist.cpp | 59 +++++- data/inst/instancelist.h | 26 ++- data/inst/instanceloader.cpp | 24 +++ data/inst/instanceloader.h | 54 +++-- data/instancemodel.cpp | 457 ------------------------------------------- data/instancemodel.h | 137 ------------- 7 files changed, 138 insertions(+), 646 deletions(-) delete mode 100644 data/instancemodel.cpp delete mode 100644 data/instancemodel.h (limited to 'data') diff --git a/data/inst/instance.h b/data/inst/instance.h index 1b282b03..56e9d0b8 100644 --- a/data/inst/instance.h +++ b/data/inst/instance.h @@ -79,23 +79,6 @@ public: virtual InstanceList *instList(); - /*! - * \brief Gets this instance's group. - * This function is used by the instance grouping system and should - * not be touched by classes deriving from Instance. - * \return The instance's group name. - * \sa setGroup() - */ - QString group() const { return m_group; } - - /*! - * \brief Sets the instance's group. - * \param group The instance's new group name. - * \sa group() - */ - void setGroup(const QString &group) { m_group = group; emit groupChanged(this, group); } - - //////// FIELDS AND SETTINGS //////// // Fields are options stored in the instance's config file that are specific // to instances (not a part of SettingsBase). Settings are things overridden @@ -278,14 +261,6 @@ public: */ virtual void updateCurrentVersion(bool keepCurrent = false) = 0; -signals: - /*! - * \brief Signal emitted when the instance's group changes. - * \param inst Pointer to the instance whose group changed. - * \param newGroup The instance's new group. - */ - void groupChanged(Instance *inst, QString newGroup); - protected: /*! * \brief Gets the value of the given field in the instance's config file. @@ -314,8 +289,6 @@ protected: INIFile config; private: - QString m_group; - QString m_rootDir; }; diff --git a/data/inst/instancelist.cpp b/data/inst/instancelist.cpp index 62367ed6..cbb89f05 100644 --- a/data/inst/instancelist.cpp +++ b/data/inst/instancelist.cpp @@ -17,7 +17,62 @@ #include "data/siglist_impl.h" -InstanceList::InstanceList(QObject *parent) : - QObject(parent) +#include +#include +#include + +#include "instance.h" +#include "instanceloader.h" + +#include "util/pathutils.h" + + +InstanceList::InstanceList(const QString &instDir, QObject *parent) : + QObject(parent), m_instDir(instDir) +{ + +} + +InstanceList::InstListError InstanceList::loadList() { + QDir dir(m_instDir); + QDirIterator iter(dir); + + while (iter.hasNext()) + { + QString subDir = iter.next(); + if (QFileInfo(PathCombine(subDir, "instance.cfg")).exists()) + { + QSharedPointer inst; + InstanceLoader::InstTypeError error = InstanceLoader::loader. + loadInstance(inst.data(), subDir); + + if (inst.data() && error == InstanceLoader::NoError) + { + qDebug(QString("Loaded instance %1").arg(inst->name()).toUtf8()); + inst->setParent(this); + append(QSharedPointer(inst)); + } + else if (error != InstanceLoader::NotAnInstance) + { + QString errorMsg = QString("Failed to load instance %1: "). + arg(QFileInfo(subDir).baseName()).toUtf8(); + + switch (error) + { + case InstanceLoader::TypeNotRegistered: + errorMsg += "Instance type not found."; + break; + } + qDebug(errorMsg.toUtf8()); + } + else if (!inst.data()) + { + qDebug(QString("Error loading instance %1. Instance loader returned null."). + arg(QFileInfo(subDir).baseName()).toUtf8()); + } + } + } + + return NoError; } diff --git a/data/inst/instancelist.h b/data/inst/instancelist.h index 72dec5f0..c43c4cc0 100644 --- a/data/inst/instancelist.h +++ b/data/inst/instancelist.h @@ -24,16 +24,34 @@ class Instance; -class InstanceList : public QObject, SigList> +class InstanceList : public QObject, public SigList> { Q_OBJECT public: - explicit InstanceList(QObject *parent = 0); + explicit InstanceList(const QString &instDir, QObject *parent = 0); -signals: + /*! + * \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 + }; -public slots: + QString instDir() const { return m_instDir; } + /*! + * \brief Loads the instance list. + */ + InstListError loadList(); + + DEFINE_SIGLIST_SIGNALS(QSharedPointer); + SETUP_SIGLIST_SIGNALS(QSharedPointer); +protected: + QString m_instDir; }; #endif // INSTANCELIST_H diff --git a/data/inst/instanceloader.cpp b/data/inst/instanceloader.cpp index 1a53bfa7..e80a0e9e 100644 --- a/data/inst/instanceloader.cpp +++ b/data/inst/instanceloader.cpp @@ -15,8 +15,16 @@ #include "instanceloader.h" +#include + #include "instancetype.h" +#include "data/inifile.h" + +#include "util/pathutils.h" + +InstanceLoader InstanceLoader::loader; + InstanceLoader::InstanceLoader() : QObject(NULL) { @@ -61,6 +69,22 @@ InstanceLoader::InstTypeError InstanceLoader::loadInstance(Instance *inst, return type->loadInstance(inst, instDir); } +InstanceLoader::InstTypeError InstanceLoader::loadInstance(Instance *inst, + const QString &instDir) +{ + QFileInfo instConfig(PathCombine(instDir, "instance.cfg")); + + if (!instConfig.exists()) + return NotAnInstance; + + INIFile ini; + ini.loadFile(instConfig.path()); + QString typeName = ini.get("type", "StdInstance").toString(); + const InstanceType *type = findType(typeName); + + return loadInstance(inst, type, instDir); +} + const InstanceType *InstanceLoader::findType(const QString &id) { if (!m_typeMap.contains(id)) diff --git a/data/inst/instanceloader.h b/data/inst/instanceloader.h index b7092edc..60c4e998 100644 --- a/data/inst/instanceloader.h +++ b/data/inst/instanceloader.h @@ -34,18 +34,18 @@ class InstanceLoader : public QObject { Q_OBJECT public: - static InstanceLoader instLoader; + static InstanceLoader loader; /*! * \brief Error codes returned by functions in the InstanceLoader and InstanceType classes. - * NoError indicates that no error occurred. - * OtherError indicates that an unspecified error occurred. - * TypeIDExists is returned by registerInstanceType() if the ID of the type being registered already exists. - * - * TypeNotRegistered is returned by createInstance() and loadInstance() when the given type is not registered. - * InstExists is returned by createInstance() if the given instance directory is already an instance. - * NotAnInstance is returned by loadInstance() if the given instance directory is not a valid instance. - * WrongInstType is returned by loadInstance() if the given instance directory's type doesn't match the given type. + * + * - NoError indicates that no error occurred. + * - OtherError indicates that an unspecified error occurred. + * - TypeIDExists is returned by registerInstanceType() if the ID of the type being registered already exists. + * - TypeNotRegistered is returned by createInstance() and loadInstance() when the given type is not registered. + * - InstExists is returned by createInstance() if the given instance directory is already an instance. + * - NotAnInstance is returned by loadInstance() if the given instance directory is not a valid instance. + * - WrongInstType is returned by loadInstance() if the given instance directory's type doesn't match the given type. */ enum InstTypeError { @@ -62,41 +62,56 @@ public: /*! * \brief Registers the given InstanceType with the instance loader. - * This causes the instance loader to take ownership of the given - * instance type (meaning the instance type's parent will be set to - * the instance loader). + * This causes the instance loader to take ownership of the given + * instance type (meaning the instance type's parent will be set to + * the instance loader). + * * \param type The InstanceType to register. * \return An InstTypeError error code. - * TypeIDExists if the given type's is already registered to another instance type. + * - TypeIDExists if the given type's is already registered to another instance type. */ InstTypeError registerInstanceType(InstanceType *type); /*! * \brief Creates an instance with the given type and stores it in inst. + * * \param inst Pointer to store the created instance in. * \param type The type of instance to create. * \param instDir The instance's directory. * \return An InstTypeError error code. - * TypeNotRegistered if the given type is not registered with the InstanceLoader. - * InstExists if the given instance directory is already an instance. + * - TypeNotRegistered if the given type is not registered with the InstanceLoader. + * - InstExists if the given instance directory is already an instance. */ InstTypeError createInstance(Instance *inst, const InstanceType *type, const QString &instDir); /*! * \brief Loads an instance from the given directory. + * * \param inst Pointer to store the loaded instance in. * \param type The type of instance to load. * \param instDir The instance's directory. * \return An InstTypeError error code. - * TypeNotRegistered if the given type is not registered with the InstanceLoader. - * NotAnInstance if the given instance directory isn't a valid instance. - * WrongInstType if the given instance directory's type isn't the same as the given type. + * - TypeNotRegistered if the given type is not registered with the InstanceLoader. + * - NotAnInstance if the given instance directory isn't a valid instance. + * - WrongInstType if the given instance directory's type isn't the same as the given type. */ InstTypeError loadInstance(Instance *inst, const InstanceType *type, 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 InstTypeError error code. + * - TypeNotRegistered if the instance's type is not registered with the InstanceLoader. + * - NotAnInstance if the given instance directory isn't a valid instance. + */ + InstTypeError loadInstance(Instance *inst, const QString &instDir); + /*! * \brief Finds an instance type with the given ID. - * If one cannot be found, returns NULL. + * If one cannot be found, returns NULL. + * * \param id The ID of the type to find. * \return The type with the given ID. NULL if none were found. */ @@ -104,6 +119,7 @@ public: /*! * \brief Gets a list of the registered instance types. + * * \return A list of instance types. */ InstTypeList typeList(); diff --git a/data/instancemodel.cpp b/data/instancemodel.cpp deleted file mode 100644 index fbdb8212..00000000 --- a/data/instancemodel.cpp +++ /dev/null @@ -1,457 +0,0 @@ -/* Copyright 2013 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 "instancemodel.h" - -#include - -#include -#include -#include -#include - -#include -#include -#include - -#include "data/stdinstance.h" -#include "util/pathutils.h" - -#define GROUP_FILE_FORMAT_VERSION 1 - -InstanceModel::InstanceModel( QObject* parent ) : - QAbstractItemModel() -{ -} - -InstanceModel::~InstanceModel() -{ - saveGroupInfo(); - for(int i = 0; i < groups.size(); i++) - { - delete groups[i]; - } -} - -void InstanceModel::addInstance( InstanceBase* inst, const QString& groupName ) -{ - auto group = getGroupByName(groupName); - group->addInstance(inst); -} - -void InstanceGroup::addInstance ( InstanceBase* inst ) -{ - instances.append(inst); - inst->setGroup(this); - // TODO: notify model. -} - - -void InstanceModel::initialLoad(QString dir) -{ - groupFileName = dir + "/instgroups.json"; - implicitGroup = new InstanceGroup("Ungrouped", this); - groups.append(implicitGroup); - - // temporary map from instance ID to group name - QMap groupMap; - - if (QFileInfo(groupFileName).exists()) - { - QFile groupFile(groupFileName); - - if (!groupFile.open(QIODevice::ReadOnly)) - { - // An error occurred. Ignore it. - qDebug("Failed to read instance group file."); - goto groupParseFail; - } - - QTextStream in(&groupFile); - QString jsonStr = in.readAll(); - groupFile.close(); - - QJsonParseError error; - QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonStr.toUtf8(), &error); - - if (error.error != QJsonParseError::NoError) - { - qWarning(QString("Failed to parse instance group file: %1 at offset %2"). - arg(error.errorString(), QString::number(error.offset)).toUtf8()); - goto groupParseFail; - } - - if (!jsonDoc.isObject()) - { - qWarning("Invalid group file. Root entry should be an object."); - goto groupParseFail; - } - - QJsonObject rootObj = jsonDoc.object(); - - // Make sure the format version matches. - if (rootObj.value("formatVersion").toVariant().toInt() == GROUP_FILE_FORMAT_VERSION) - { - // Get the group list. - if (!rootObj.value("groups").isObject()) - { - qWarning("Invalid group list JSON: 'groups' should be an object."); - goto groupParseFail; - } - - // Iterate through the list. - QJsonObject groupList = rootObj.value("groups").toObject(); - - for (QJsonObject::iterator iter = groupList.begin(); - iter != groupList.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(); - - // Create the group object. - InstanceGroup *group = new InstanceGroup(groupName, this); - groups.push_back(group); - - // If 'hidden' isn't a bool value, just assume it's false. - if (groupObj.value("hidden").isBool() && groupObj.value("hidden").toBool()) - { - group->setHidden(groupObj.value("hidden").toBool()); - } - - 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; - } - - // 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++) - { - groupMap[(*iter2).toString()] = groupName; - } - } - } - } - -groupParseFail: - - qDebug("Loading instances"); - QDir instDir(dir); - QDirIterator iter(instDir); - - while (iter.hasNext()) - { - QString subDir = iter.next(); - if (QFileInfo(PathCombine(subDir, "instance.cfg")).exists()) - { - // TODO Differentiate between different instance types. - InstanceBase* inst = new StdInstance(subDir); - QString instID = inst->getInstID(); - auto iter = groupMap.find(instID); - if(iter != groupMap.end()) - { - addInstance(inst,iter.value()); - } - else - { - addInstance(inst); - } - } - } -} - -int InstanceModel::columnCount ( const QModelIndex& parent ) const -{ - // for now... - return 1; -} - -QVariant InstanceModel::data ( const QModelIndex& index, int role ) const -{ - if (!index.isValid()) - return QVariant(); - - if (role != Qt::DisplayRole) - return QVariant(); - - InstanceModelItem *item = static_cast(index.internalPointer()); - - return item->data(index.column()); -} - -QModelIndex InstanceModel::index ( int row, int column, const QModelIndex& parent ) const -{ - if (!hasIndex(row, column, parent)) - return QModelIndex(); - - InstanceModelItem *parentItem; - - if (!parent.isValid()) - parentItem = (InstanceModelItem *) this; - else - parentItem = static_cast(parent.internalPointer()); - - InstanceModelItem *childItem = parentItem->getChild(row); - if (childItem) - return createIndex(row, column, childItem); - else - return QModelIndex(); - -} - -QModelIndex InstanceModel::parent ( const QModelIndex& index ) const -{ - if (!index.isValid()) - return QModelIndex(); - - InstanceModelItem *childItem = static_cast(index.internalPointer()); - InstanceModelItem *parentItem = childItem->getParent(); - - if (parentItem == this) - return QModelIndex(); - - return createIndex(parentItem->getRow(), 0, parentItem); -} - -int InstanceModel::rowCount ( const QModelIndex& parent ) const -{ - InstanceModelItem *parentItem; - if (parent.column() > 0) - return 0; - - if (!parent.isValid()) - parentItem = (InstanceModelItem*) this; - else - parentItem = static_cast(parent.internalPointer()); - - return parentItem->numChildren(); -} - -bool InstanceModel::saveGroupInfo() const -{ - /* - using namespace boost::property_tree; - ptree pt; - - pt.put("formatVersion", GROUP_FILE_FORMAT_VERSION); - - try - { - typedef QMap > GroupListMap; - - GroupListMap groupLists; - for (auto iter = instances.begin(); iter != instances.end(); iter++) - { - InstanceGroup *group = getInstanceGroup(*iter); - - if (group != nullptr) - groupLists[group].push_back(*iter); - } - - ptree groupsPtree; - for (auto iter = groupLists.begin(); iter != groupLists.end(); iter++) - { - auto group = iter.key(); - auto & gList = iter.value(); - - ptree groupTree; - - groupTree.put("hidden", group->isHidden()); - - ptree instList; - for (auto iter2 = gList.begin(); iter2 != gList.end(); iter2++) - { - std::string instID((*iter2)->getInstID().toUtf8()); - instList.push_back(std::make_pair("", ptree(instID))); - } - groupTree.put_child("instances", instList); - - groupsPtree.push_back(std::make_pair(std::string(group->getName().toUtf8()), groupTree)); - } - pt.put_child("groups", groupsPtree); - - write_json(groupFile.toStdString(), pt); - } - catch (json_parser_error e) - { -// wxLogError(_("Failed to read group list.\nJSON parser error at line %i: %s"), -// e.line(), wxStr(e.message()).c_str()); - return false; - } - catch (ptree_error e) - { -// wxLogError(_("Failed to save group list. Unknown ptree error.")); - return false; - } - - return true; - */ - return false; -} - -void InstanceModel::setInstanceGroup ( InstanceBase* inst, const QString& groupName ) -{ - /* - InstanceGroup *prevGroup = getInstanceGroup(inst); - - if (prevGroup != nullptr) - { - groupsMap.remove(inst); - } - - if (!groupName.isEmpty()) - { - InstanceGroup *newGroup = nullptr; - - for (auto iter = root->groups.begin(); iter != root->groups.end(); iter++) - { - if ((*iter)->getName() == groupName) - { - newGroup = *iter; - } - } - - if (newGroup == nullptr) - { - newGroup = new InstanceGroup(groupName, this); - root->groups.push_back(newGroup); - } - - groupsMap[inst] = newGroup; - } - - // TODO: propagate change, reflect in model, etc. - //InstanceGroupChanged(inst); - */ -} - -InstanceGroup* InstanceModel::getGroupByName ( const QString& name ) const -{ - for (auto iter = groups.begin(); iter != groups.end(); iter++) - { - if ((*iter)->getName() == name) - return *iter; - } - return nullptr; -} -/* -void InstanceModel::setGroupFile ( QString filename ) -{ - groupFile = filename; -}*/ - -int InstanceModel::numChildren() const -{ - return groups.count(); -} - -InstanceModelItem* InstanceModel::getChild ( int index ) const -{ - return groups[index]; -} - -QVariant InstanceModel::data ( int role ) const -{ - switch(role) - { - case Qt::DisplayRole: - return "name"; - } - return QVariant(); -} - - -InstanceGroup::InstanceGroup(const QString& name, InstanceModel *parent) -{ - this->name = name; - this->model = parent; - this->hidden = false; -} - -InstanceGroup::~InstanceGroup() -{ - for(int i = 0; i < instances.size(); i++) - { - delete instances[i]; - } -} - - -QString InstanceGroup::getName() const -{ - return name; -} - -void InstanceGroup::setName(const QString& name) -{ - this->name = name; - //TODO: propagate change -} - -InstanceModelItem* InstanceGroup::getParent() const -{ - return model; -} - -bool InstanceGroup::isHidden() const -{ - return hidden; -} - -void InstanceGroup::setHidden(bool hidden) -{ - this->hidden = hidden; - //TODO: propagate change -} - -int InstanceGroup::getRow() const -{ - return model->getIndexOf( this); -} - -InstanceModelItem* InstanceGroup::getChild ( int index ) const -{ - return instances[index]; -} - -int InstanceGroup::numChildren() const -{ - return instances.size(); -} - -QVariant InstanceGroup::data ( int role ) const -{ - switch(role) - { - case Qt::DisplayRole: - return name; - default: - return QVariant(); - } -} diff --git a/data/instancemodel.h b/data/instancemodel.h deleted file mode 100644 index 9678fef0..00000000 --- a/data/instancemodel.h +++ /dev/null @@ -1,137 +0,0 @@ -/* Copyright 2013 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. - */ - -#ifndef INSTANCEMODEL_H -#define INSTANCEMODEL_H - -#include -#include -#include -#include - -enum IMI_type -{ - IMI_Root, - IMI_Group, - IMI_Instance -}; - -class InstanceModel; -class InstanceGroup; -class InstanceBase; - -class InstanceModelItem -{ - public: - virtual IMI_type getModelItemType() const = 0; - virtual InstanceModelItem * getParent() const = 0; - virtual int numChildren() const = 0; - virtual InstanceModelItem * getChild(int index) const = 0; - virtual InstanceModel * getModel() const = 0; - virtual QVariant data(int role) const = 0; - virtual int getRow() const = 0; -}; - -class InstanceGroup : public InstanceModelItem -{ -public: - InstanceGroup(const QString& name, InstanceModel * model); - ~InstanceGroup(); - - QString getName() const; - void setName(const QString& name); - - bool isHidden() const; - void setHidden(bool hidden); - - virtual IMI_type getModelItemType() const - { - return IMI_Group; - } - virtual InstanceModelItem* getParent() const; - virtual InstanceModelItem* getChild ( int index ) const; - virtual int numChildren() const; - virtual InstanceModel * getModel() const - { - return model; - }; - virtual QVariant data ( int column ) const; - int getIndexOf(InstanceBase * inst) - { - return instances.indexOf(inst); - }; - virtual int getRow() const; - void addInstance ( InstanceBase* inst ); -protected: - QString name; - InstanceModel * model; - QVector instances; - bool hidden; - int row; -}; - -class InstanceModel : public QAbstractItemModel, public InstanceModelItem -{ -public: - explicit InstanceModel(QObject *parent = 0); - ~InstanceModel(); - - virtual int columnCount ( const QModelIndex& parent = QModelIndex() ) const; - virtual QVariant data ( const QModelIndex& index, int role = Qt::DisplayRole ) const; - virtual QModelIndex index ( int row, int column, const QModelIndex& parent = QModelIndex() ) const; - virtual QModelIndex parent ( const QModelIndex& child ) const; - virtual int rowCount ( const QModelIndex& parent = QModelIndex() ) const; - - void addInstance(InstanceBase *inst, const QString& groupName = "Ungrouped"); - void setInstanceGroup(InstanceBase *inst, const QString & groupName); - InstanceGroup* getGroupByName(const QString & name) const; - - void initialLoad(QString dir); - bool saveGroupInfo() const; - - virtual IMI_type getModelItemType() const - { - return IMI_Root; - } - virtual InstanceModelItem * getParent() const - { - return nullptr; - }; - virtual int numChildren() const; - virtual InstanceModelItem* getChild ( int index ) const; - virtual InstanceModel* getModel() const - { - return nullptr; - }; - virtual QVariant data ( int column ) const; - int getIndexOf(const InstanceGroup * grp) const - { - return groups.indexOf((InstanceGroup *) grp); - }; - virtual int getRow() const - { - return 0; - }; -signals: - -public slots: - -private: - QString groupFileName; - QVector groups; - InstanceGroup * implicitGroup; -}; - -#endif // INSTANCEMODEL_H -- cgit v1.2.3 From f71479ec33562c9a0ebbdb335bef5e2824a12710 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 20 Feb 2013 08:32:26 -0600 Subject: Added stdinstance plugin and a ton of plugin stuff. --- data/inst/instance.cpp | 55 +++++++++++++++++++++++ data/inst/instance.h | 45 +++++++++++++++++++ data/inst/instancetype.h | 4 +- data/plugin/instancetypeplugin.h | 41 +++++++++++++++++ data/plugin/pluginmanager.cpp | 96 ++++++++++++++++++++++++++++++++++++++++ data/plugin/pluginmanager.h | 71 +++++++++++++++++++++++++++++ 6 files changed, 309 insertions(+), 3 deletions(-) create mode 100644 data/plugin/instancetypeplugin.h create mode 100644 data/plugin/pluginmanager.cpp create mode 100644 data/plugin/pluginmanager.h (limited to 'data') diff --git a/data/inst/instance.cpp b/data/inst/instance.cpp index 8561ed9f..5db0be20 100644 --- a/data/inst/instance.cpp +++ b/data/inst/instance.cpp @@ -44,6 +44,61 @@ InstanceList *Instance::instList() return NULL; } +QString Instance::minecraftDir() const +{ + QFileInfo mcDir(PathCombine(rootDir(), "minecraft")); + QFileInfo dotMCDir(PathCombine(rootDir(), ".minecraft")); + + if (dotMCDir.exists() && !mcDir.exists()) + { + return dotMCDir.path(); + } + else + { + return mcDir.path(); + } +} + +QString Instance::binDir() const +{ + return PathCombine(minecraftDir(), "bin"); +} + +QString Instance::savesDir() const +{ + return PathCombine(minecraftDir(), "saves"); +} + +QString Instance::mlModsDir() const +{ + return PathCombine(minecraftDir(), "mods"); +} + +QString Instance::coreModsDir() const +{ + return PathCombine(minecraftDir(), "coremods"); +} + +QString Instance::resourceDir() const +{ + return PathCombine(minecraftDir(), "resources"); +} + +QString Instance::screenshotsDir() const +{ + return PathCombine(minecraftDir(), "screenshots"); +} + +QString Instance::texturePacksDir() const +{ + return PathCombine(minecraftDir(), "texturepacks"); +} + +QString Instance::mcJar() const +{ + return PathCombine(binDir(), "minecraft.jar"); +} + QVariant Instance::getField(const QString &name, QVariant defVal) const { return config.get(name, defVal); diff --git a/data/inst/instance.h b/data/inst/instance.h index 56e9d0b8..035704b9 100644 --- a/data/inst/instance.h +++ b/data/inst/instance.h @@ -214,6 +214,51 @@ public: { setField("lastLaunchTime", val); } + ////// Directories ////// + //! Gets the path to the instance's minecraft folder. + QString minecraftDir() const; + + /*! + * \brief Gets the path to the instance's instance mods folder. + * This is the folder where the jar mods are kept. + */ + QString instModsDir() const; + + //! Gets the path to the instance's bin folder. + QString binDir() const; + + //! Gets the path to the instance's saves folder. + QString savesDir() const; + + //! Gets the path to the instance's mods folder. (.minecraft/mods) + QString mlModsDir() const; + + //! Gets the path to the instance's coremods folder. + QString coreModsDir() const; + + //! Gets the path to the instance's resources folder. + QString resourceDir() const; + + //! Gets the path to the instance's screenshots folder. + QString screenshotsDir() const; + + //! Gets the path to the instance's texture packs folder. + QString texturePacksDir() const; + + + ////// Files ////// + //! Gets the path to the instance's minecraft.jar + QString mcJar() const; + + //! Gets the path to the instance's mcbackup.jar. + QString mcBackup() const; + + //! Gets the path to the instance's config file. + QString configFile() const; + + //! Gets the path to the instance's modlist file. + QString modListFile() const; + ////// Settings ////// //// Java Settings //// diff --git a/data/inst/instancetype.h b/data/inst/instancetype.h index ec3f5b87..bd22a17c 100644 --- a/data/inst/instancetype.h +++ b/data/inst/instancetype.h @@ -37,9 +37,7 @@ public: friend class InstanceLoader; /*! - * \brief Gets the ID for this instance type. - * By default this is the name of the Instance class that this type - * creates, but this can be changed by overriding this function. + * \brief Gets the ID for this instance type. * The type ID should be unique as it is used to identify the type * of instances when they are loaded. * Changing this value at runtime results in undefined behavior. diff --git a/data/plugin/instancetypeplugin.h b/data/plugin/instancetypeplugin.h new file mode 100644 index 00000000..8e3febdb --- /dev/null +++ b/data/plugin/instancetypeplugin.h @@ -0,0 +1,41 @@ +/* Copyright 2013 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. + */ + +#ifndef INSTANCETYPEPLUGIN_H +#define INSTANCETYPEPLUGIN_H + +#include + +#include "data/inst/instancetype.h" + +/*! + * \brief Interface for plugins that want to provide custom instance types. + */ +class InstanceTypePlugin +{ +public: + /*! + * \brief Gets a QList containing the instance types that this plugin provides. + * These instance types are then registered with the InstanceLoader. + * The InstanceType objects should \e not be deleted by the plugin. Once they + * are registered, they belong to the InstanceLoader. + * \return A QList containing this plugin's instance types. + */ + virtual QList getInstanceTypes() = 0; +}; + +Q_DECLARE_INTERFACE(InstanceTypePlugin, "net.forkk.MultiMC.InstanceTypePlugin/0.1") + +#endif // INSTANCETYPEPLUGIN_H diff --git a/data/plugin/pluginmanager.cpp b/data/plugin/pluginmanager.cpp new file mode 100644 index 00000000..cd33b285 --- /dev/null +++ b/data/plugin/pluginmanager.cpp @@ -0,0 +1,96 @@ +/* Copyright 2013 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 "pluginmanager.h" + +#include +#include +#include + +#include + +#include "data/plugin/instancetypeplugin.h" + +PluginManager PluginManager::manager; + +PluginManager::PluginManager() : + QObject(NULL) +{ + +} + +bool PluginManager::loadPlugins(QString pluginDir) +{ + m_plugins.clear(); + + QDir dir(pluginDir); + QDirIterator iter(dir); + + while (iter.hasNext()) + { + QFileInfo pluginFile(dir.absoluteFilePath(iter.next())); + + if (pluginFile.exists() && pluginFile.isFile()) + { + QPluginLoader pluginLoader(pluginFile.absoluteFilePath()); + pluginLoader.load(); + QObject *plugin = pluginLoader.instance(); + if (plugin) + { + qDebug(QString("Loaded plugin %1."). + arg(pluginFile.baseName()).toUtf8()); + m_plugins.push_back(plugin); + } + else + { + qWarning(QString("Error loading plugin %1. Not a valid plugin."). + arg(pluginFile.baseName()).toUtf8()); + } + } + } + + return true; +} + +bool PluginManager::initInstanceTypes() +{ + for (int i = 0; i < m_plugins.count(); i++) + { + InstanceTypePlugin *plugin = qobject_cast(m_plugins[i]); + if (plugin) + { + QList instanceTypes = plugin->getInstanceTypes(); + + for (int i = 0; i < instanceTypes.count(); i++) + { + InstanceLoader::InstTypeError error = + InstanceLoader::loader.registerInstanceType(instanceTypes[i]); + switch (error) + { + case InstanceLoader::TypeIDExists: + qWarning(QString("Instance type %1 already registered."). + arg(instanceTypes[i]->typeID()).toUtf8()); + } + } + } + } + + return true; +} + +QObject *PluginManager::getPlugin(int index) +{ + return m_plugins[index]; +} diff --git a/data/plugin/pluginmanager.h b/data/plugin/pluginmanager.h new file mode 100644 index 00000000..8e2dba0d --- /dev/null +++ b/data/plugin/pluginmanager.h @@ -0,0 +1,71 @@ +/* Copyright 2013 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. + */ + +#ifndef PLUGINMANAGER_H +#define PLUGINMANAGER_H + +#include +#include +#include + +/*! + * \brief This class is a singleton that manages loading plugins. + */ +class PluginManager : public QObject +{ + Q_OBJECT +public: + /*! + * \brief Gets the plugin manager instance. + */ + static PluginManager &get() { return manager; } + + /*! + * \brief Loads plugins from the given directory. + * This function does \e not initialize the plugins. It simply loads their + * classes. Use the init functions to initialize the various plugin types. + * \param The directory to load plugins from. + * \return True if successful. False on failure. + */ + bool loadPlugins(QString pluginDir); + + /*! + * \brief Initializes the instance type plugins. + * \return True if successful. False on failure. + */ + bool initInstanceTypes(); + + /*! + * \brief Checks how many plugins are loaded. + * \return The number of plugins. + */ + int count() { return m_plugins.count(); } + + /*! + * \brief Gets the plugin at the given index. + * \param index The index of the plugin to get. + * \return The plugin at the given index. + */ + QObject *getPlugin(int index); + +private: + PluginManager(); + + QList m_plugins; + + static PluginManager manager; +}; + +#endif // PLUGINMANAGER_H -- cgit v1.2.3 From dd2e836b4cf4cfa043f9ea2911f58f1d22d4e282 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 20 Feb 2013 19:10:09 -0600 Subject: Split MultiMC up into a few separate libraries. Fixed plugin system. Tons of other stuff... --- data/appsettings.cpp | 40 ----- data/appsettings.h | 116 ------------- data/inifile.cpp | 86 ---------- data/inifile.h | 36 ----- data/inst/instance.cpp | 110 ------------- data/inst/instance.h | 340 --------------------------------------- data/inst/instancelist.cpp | 78 --------- data/inst/instancelist.h | 57 ------- data/inst/instanceloader.cpp | 106 ------------ data/inst/instanceloader.h | 133 --------------- data/inst/instancetype.cpp | 21 --- data/inst/instancetype.h | 83 ---------- data/plugin/instancetypeplugin.h | 41 ----- data/plugin/pluginmanager.cpp | 81 +++++----- data/plugin/pluginmanager.h | 17 +- data/siglist.h | 130 --------------- data/siglist_impl.h | 156 ------------------ data/version/instversion.cpp | 32 ---- data/version/instversion.h | 51 ------ data/version/instversionlist.cpp | 21 --- data/version/instversionlist.h | 43 ----- 21 files changed, 54 insertions(+), 1724 deletions(-) delete mode 100644 data/appsettings.cpp delete mode 100644 data/appsettings.h delete mode 100644 data/inifile.cpp delete mode 100644 data/inifile.h delete mode 100644 data/inst/instance.cpp delete mode 100644 data/inst/instance.h delete mode 100644 data/inst/instancelist.cpp delete mode 100644 data/inst/instancelist.h delete mode 100644 data/inst/instanceloader.cpp delete mode 100644 data/inst/instanceloader.h delete mode 100644 data/inst/instancetype.cpp delete mode 100644 data/inst/instancetype.h delete mode 100644 data/plugin/instancetypeplugin.h delete mode 100644 data/siglist.h delete mode 100644 data/siglist_impl.h delete mode 100644 data/version/instversion.cpp delete mode 100644 data/version/instversion.h delete mode 100644 data/version/instversionlist.cpp delete mode 100644 data/version/instversionlist.h (limited to 'data') diff --git a/data/appsettings.cpp b/data/appsettings.cpp deleted file mode 100644 index 1d9c4312..00000000 --- a/data/appsettings.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/* Copyright 2013 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 "appsettings.h" - -AppSettings* settings; - -SettingsBase::SettingsBase(QObject *parent) : - QObject(parent) -{ - -} - -AppSettings::AppSettings(QObject *parent) : - SettingsBase(parent) -{ - -} - -QVariant AppSettings::getValue(const QString& name, QVariant defVal) const -{ - return config.value(name, defVal); -} - -void AppSettings::setValue(const QString& name, QVariant val) -{ - config.setValue(name, val); -} diff --git a/data/appsettings.h b/data/appsettings.h deleted file mode 100644 index 8a786db0..00000000 --- a/data/appsettings.h +++ /dev/null @@ -1,116 +0,0 @@ -/* Copyright 2013 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. - */ - -#ifndef APPSETTINGS_H -#define APPSETTINGS_H - -#include -#include -#include -#include - -#include "util/apputils.h" -#include "util/osutils.h" - -#if WINDOWS -#define JPATHKEY "JavaPathWindows" -#elif OSX -#define JPATHKEY "JavaPathOSX" -#else -#define JPATHKEY "JavaPathLinux" -#endif - -#define DEFINE_SETTING_ADVANCED(funcName, name, valType, defVal) \ - virtual valType get ## funcName() const { return getValue(name, defVal).value(); } \ - virtual void set ## funcName(valType value) { setValue(name, value); } - -#define DEFINE_SETTING(name, valType, defVal) \ - DEFINE_SETTING_ADVANCED(name, STR_VAL(name), valType, defVal) - - -class SettingsBase : public QObject -{ - Q_OBJECT -public: - explicit SettingsBase(QObject *parent = 0); - - // Updates - DEFINE_SETTING(UseDevBuilds, bool, false) - DEFINE_SETTING(AutoUpdate, bool, true) - - // Folders - DEFINE_SETTING(InstanceDir, QString, "instances") - DEFINE_SETTING(CentralModsDir, QString, "mods") - DEFINE_SETTING(LWJGLDir, QString, "lwjgl") - - // Console - DEFINE_SETTING(ShowConsole, bool, true) - DEFINE_SETTING(AutoCloseConsole, bool, true) - - // Toolbar settings - DEFINE_SETTING(InstanceToolbarVisible, bool, true) - DEFINE_SETTING(InstanceToolbarPosition, QPoint, QPoint()) - - // Console Colors - DEFINE_SETTING(SysMessageColor, QColor, QColor(Qt::blue)) - DEFINE_SETTING(StdOutColor, QColor, QColor(Qt::black)) - DEFINE_SETTING(StdErrColor, QColor, QColor(Qt::red)) - - // Window Size - DEFINE_SETTING(LaunchCompatMode, bool, false) - DEFINE_SETTING(LaunchMaximized, bool, false) - DEFINE_SETTING(MinecraftWinWidth, int, 854) - DEFINE_SETTING(MinecraftWinHeight, int, 480) - - // Auto login - DEFINE_SETTING(AutoLogin, bool, false) - - // Memory - DEFINE_SETTING(MinMemAlloc, int, 512) - DEFINE_SETTING(MaxMemAlloc, int, 1024) - - // Java Settings - DEFINE_SETTING_ADVANCED(JavaPath, JPATHKEY, QString, "java") - DEFINE_SETTING(JvmArgs, QString, "") - - // Custom Commands - DEFINE_SETTING(PreLaunchCommand, QString, "") - DEFINE_SETTING(PostExitCommand, QString, "") - - virtual QVariant getValue(const QString& name, QVariant defVal = QVariant()) const = 0; - virtual void setValue(const QString& name, QVariant val) = 0; -}; - -class AppSettings : public SettingsBase -{ - Q_OBJECT -public: - explicit AppSettings(QObject *parent = 0); - - QSettings& getConfig() { return config; } - - virtual QVariant getValue(const QString &name, QVariant defVal = QVariant()) const; - virtual void setValue(const QString& name, QVariant val); - -protected: - QSettings config; -}; - -#undef DEFINE_SETTING_ADVANCED -#undef DEFINE_SETTING - -extern AppSettings* settings; - -#endif // APPSETTINGS_H diff --git a/data/inifile.cpp b/data/inifile.cpp deleted file mode 100644 index 2d68caf6..00000000 --- a/data/inifile.cpp +++ /dev/null @@ -1,86 +0,0 @@ -/* Copyright 2013 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 "inifile.h" - -#include -#include -#include - -INIFile::INIFile() -{ - -} - -bool INIFile::saveFile(QString fileName) -{ - // TODO Handle errors. - QFile file(fileName); - file.open(QIODevice::WriteOnly); - QTextStream out(&file); - - for (Iterator iter = begin(); iter != end(); iter++) - { - out << iter.key() << "=" << iter.value().toString() << "\n"; - } - - return true; -} - -bool INIFile::loadFile(QString fileName) -{ - // TODO Handle errors. - QFile file(fileName); - file.open(QIODevice::ReadOnly); - QTextStream in(&file); - - QStringList lines = in.readAll().split('\n'); - for (int i = 0; i < lines.count(); i++) - { - QString & lineRaw = lines[i]; - // Ignore comments. - QString line = lineRaw.left(lineRaw.indexOf('#')).trimmed(); - - int eqPos = line.indexOf('='); - if(eqPos == -1) - continue; - QString key = line.left(eqPos).trimmed(); - QString valueStr = line.right(line.length() - eqPos - 1).trimmed(); - - QVariant value(valueStr); - /* - QString dbg = key; - dbg += " = "; - dbg += valueStr; - qDebug(dbg.toLocal8Bit()); - */ - this->operator [](key) = value; - } - - return true; -} - -QVariant INIFile::get(QString key, QVariant def) const -{ - if (!this->contains(key)) - return def; - else - return this->operator [](key); -} - -void INIFile::set(QString key, QVariant val) -{ - this->operator [](key) = val; -} diff --git a/data/inifile.h b/data/inifile.h deleted file mode 100644 index 75783859..00000000 --- a/data/inifile.h +++ /dev/null @@ -1,36 +0,0 @@ -/* Copyright 2013 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. - */ - -#ifndef INIFILE_H -#define INIFILE_H - -#include -#include -#include - -// Sectionless INI parser (for instance config files) -class INIFile : public QMap -{ -public: - explicit INIFile(); - - bool loadFile(QString fileName); - bool saveFile(QString fileName); - - QVariant get(QString key, QVariant def) const; - void set(QString key, QVariant val); -}; - -#endif // INIFILE_H diff --git a/data/inst/instance.cpp b/data/inst/instance.cpp deleted file mode 100644 index 5db0be20..00000000 --- a/data/inst/instance.cpp +++ /dev/null @@ -1,110 +0,0 @@ -/* Copyright 2013 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 "instance.h" - -#include - -#include "util/pathutils.h" - -Instance::Instance(const QString &rootDir, QObject *parent) : - SettingsBase(parent) -{ - m_rootDir = rootDir; - config.loadFile(PathCombine(rootDir, "instance.cfg")); -} - -QString Instance::id() -{ - return QFileInfo(rootDir()).baseName(); -} - -QString Instance::rootDir() -{ - return m_rootDir; -} - -InstanceList *Instance::instList() -{ - if (parent()->inherits("InstanceList")) - return (InstanceList *)parent(); - else - return NULL; -} - -QString Instance::minecraftDir() const -{ - QFileInfo mcDir(PathCombine(rootDir(), "minecraft")); - QFileInfo dotMCDir(PathCombine(rootDir(), ".minecraft")); - - if (dotMCDir.exists() && !mcDir.exists()) - { - return dotMCDir.path(); - } - else - { - return mcDir.path(); - } -} - -QString Instance::binDir() const -{ - return PathCombine(minecraftDir(), "bin"); -} - -QString Instance::savesDir() const -{ - return PathCombine(minecraftDir(), "saves"); -} - -QString Instance::mlModsDir() const -{ - return PathCombine(minecraftDir(), "mods"); -} - -QString Instance::coreModsDir() const -{ - return PathCombine(minecraftDir(), "coremods"); -} - -QString Instance::resourceDir() const -{ - return PathCombine(minecraftDir(), "resources"); -} - -QString Instance::screenshotsDir() const -{ - return PathCombine(minecraftDir(), "screenshots"); -} - -QString Instance::texturePacksDir() const -{ - return PathCombine(minecraftDir(), "texturepacks"); -} - -QString Instance::mcJar() const -{ - return PathCombine(binDir(), "minecraft.jar"); -} - -QVariant Instance::getField(const QString &name, QVariant defVal) const -{ - return config.get(name, defVal); -} - -void Instance::setField(const QString &name, QVariant val) -{ - config.set(name, val); -} diff --git a/data/inst/instance.h b/data/inst/instance.h deleted file mode 100644 index 035704b9..00000000 --- a/data/inst/instance.h +++ /dev/null @@ -1,340 +0,0 @@ -/* Copyright 2013 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. - */ - -#ifndef INSTANCE_H -#define INSTANCE_H - -#include -#include - -#include "data/appsettings.h" -#include "data/inifile.h" - -#define DEFINE_OVERRIDDEN_SETTING_ADVANCED(funcName, cfgEntryName, typeName) \ - typeName get ## funcName() const { return getField(cfgEntryName, settings->get ## funcName()).value(); } - -#define DEFINE_OVERRIDDEN_SETTING(funcName, typeName) \ - DEFINE_OVERRIDDEN_SETTING_ADVANCED(funcName, STR_VAL(funcName), typeName) - -class InstanceList; - -/*! - * \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 Instance : public SettingsBase -{ - Q_OBJECT -public: - explicit Instance(const QString &rootDir, QObject *parent = 0); - - // Please, for the sake of my (and everyone else's) sanity, at least keep this shit - // *somewhat* organized. Also, documentation is semi-important here. Please don't - // leave undocumented stuff behind. - - - //////// STUFF //////// - - /*! - * \brief Get the instance's ID. - * This is a unique identifier string that is, by default, set to the - * instance's folder name. It's not always the instance's folder name, - * however, as any class deriving from Instance can override the id() - * method and change how the ID is determined. The instance's ID - * should always remain constant. Undefined behavior results if an - * already loaded instance's ID changes. - * - * \return The instance's ID. - */ - virtual QString id(); - - /*! - * \brief Gets the path to the instance's root directory. - * \return The path to the instance's root directory. - */ - virtual QString rootDir(); - - /*! - * \brief Gets the instance list that this instance is a part of. - * Returns NULL if this instance is not in a list - * (the parent is not an InstanceList). - * \return A pointer to the InstanceList containing this instance. - */ - virtual InstanceList *instList(); - - - //////// FIELDS AND SETTINGS //////// - // Fields are options stored in the instance's config file that are specific - // to instances (not a part of SettingsBase). Settings are things overridden - // from SettingsBase. - - - ////// Fields ////// - - //// General Info //// - - /*! - * \brief Gets this instance's name. - * This is the name that will be displayed to the user. - * \return The instance's name. - * \sa setName - */ - virtual QString name() { return getField("name", "Unnamed Instance").value(); } - - /*! - * \brief Sets the instance's name - * \param val The instance's new name. - */ - virtual void setName(QString val) { setField("name", val); } - - /*! - * \brief Gets the instance's icon key. - * \return The instance's icon key. - * \sa setIconKey() - */ - virtual QString iconKey() const { return getField("iconKey", "default").value(); } - - /*! - * \brief Sets the instance's icon key. - * \param val The new icon key. - */ - virtual void setIconKey(QString val) { setField("iconKey", val); } - - - /*! - * \brief Gets the instance's notes. - * \return The instances notes. - */ - virtual QString notes() const { return getField("notes", "").value(); } - - /*! - * \brief Sets the instance's notes. - * \param val The instance's new notes. - */ - virtual void setNotes(QString val) { setField("notes", val); } - - - /*! - * \brief Checks if the instance's minecraft.jar needs to be rebuilt. - * If this is true, the instance's mods will be reinstalled to its - * minecraft.jar file. This value is automatically set to true when - * the jar mod list changes. - * \return Whether or not the instance's jar file should be rebuilt. - */ - virtual bool shouldRebuild() const { return getField("NeedsRebuild", false).value(); } - - /*! - * \brief Sets whether or not the instance's minecraft.jar needs to be rebuilt. - * \param val Whether the instance's minecraft needs to be rebuilt or not. - */ - virtual void setShouldRebuild(bool val) { setField("NeedsRebuild", val); } - - - //// Version Stuff //// - - /*! - * \brief Sets the instance's current version. - * This value represents the instance's current version. If this value - * is different from intendedVersion(), the instance should be updated. - * This value is updated by the updateCurrentVersion() function. - * \return A string representing the instance's current version. - */ - virtual QString currentVersion() { return getField("JarVersion", "Unknown").value(); } - - /*! - * \brief Sets the instance's current version. - * This is used to keep track of the instance's current version. Don't - * mess with this unless you know what you're doing. - * \param val The new value. - */ - virtual void setCurrentVersion(QString val) { setField("JarVersion", val); } - - - /*! - * \brief Gets the version of LWJGL that this instance should use. - * If no LWJGL version is specified in the instance's config file, - * defaults to "Mojang" - * \return The instance's LWJGL version. - */ - virtual QString lwjglVersion() { return getField("LwjglVersion", "Mojang").value(); } - - /*! - * \brief Sets the version of LWJGL that this instance should use. - * \param val The LWJGL version to use - */ - virtual void setLWJGLVersion(QString val) { setField("LwjglVersion", val); } - - - /*! - * \brief Gets the version that this instance should try to update to. - * If this value differs from currentVersion(), the instance will - * download the intended version when it launches. - * \return The instance's intended version. - */ - virtual QString intendedVersion() { return getField("IntendedJarVersion", currentVersion()).value(); } - - /*! - * \brief Sets the version that this instance should try to update to. - * \param val The instance's new intended version. - */ - virtual void setIntendedVersion(QString val) { setField("IntendedJarVersion", val); } - - - - //// Timestamps //// - - /*! - * \brief Gets the time that the instance was last launched. - * Measured in milliseconds since epoch. QDateTime::currentMSecsSinceEpoch() - * \return The time that the instance was last launched. - */ - virtual qint64 lastLaunch() { return getField("lastLaunchTime", 0).value(); } - - /*! - * \brief Sets the time that the instance was last launched. - * \param val The time to set. Defaults to QDateTime::currentMSecsSinceEpoch() - */ - virtual void setLastLaunch(qint64 val = QDateTime::currentMSecsSinceEpoch()) - { setField("lastLaunchTime", val); } - - - ////// Directories ////// - //! Gets the path to the instance's minecraft folder. - QString minecraftDir() const; - - /*! - * \brief Gets the path to the instance's instance mods folder. - * This is the folder where the jar mods are kept. - */ - QString instModsDir() const; - - //! Gets the path to the instance's bin folder. - QString binDir() const; - - //! Gets the path to the instance's saves folder. - QString savesDir() const; - - //! Gets the path to the instance's mods folder. (.minecraft/mods) - QString mlModsDir() const; - - //! Gets the path to the instance's coremods folder. - QString coreModsDir() const; - - //! Gets the path to the instance's resources folder. - QString resourceDir() const; - - //! Gets the path to the instance's screenshots folder. - QString screenshotsDir() const; - - //! Gets the path to the instance's texture packs folder. - QString texturePacksDir() const; - - - ////// Files ////// - //! Gets the path to the instance's minecraft.jar - QString mcJar() const; - - //! Gets the path to the instance's mcbackup.jar. - QString mcBackup() const; - - //! Gets the path to the instance's config file. - QString configFile() const; - - //! Gets the path to the instance's modlist file. - QString modListFile() const; - - ////// Settings ////// - - //// Java Settings //// - DEFINE_OVERRIDDEN_SETTING_ADVANCED(JavaPath, JPATHKEY, QString) - DEFINE_OVERRIDDEN_SETTING(JvmArgs, QString) - - //// Custom Commands //// - DEFINE_OVERRIDDEN_SETTING(PreLaunchCommand, QString) - DEFINE_OVERRIDDEN_SETTING(PostExitCommand, QString) - - //// Memory //// - DEFINE_OVERRIDDEN_SETTING(MinMemAlloc, int) - DEFINE_OVERRIDDEN_SETTING(MaxMemAlloc, int) - - //// Auto login //// - DEFINE_OVERRIDDEN_SETTING(AutoLogin, bool) - - // This little guy here is to keep Qt Creator from being a dumbass and - // auto-indenting the lines below the macros. Seriously, it's really annoying. - ; - - - //////// OTHER FUNCTIONS //////// - - //// Version System //// - - /*! - * \brief Checks whether or not the currentVersion of the instance needs to be updated. - * If this returns true, updateCurrentVersion is called. In the - * standard instance, this is determined by checking a timestamp - * stored in the instance config file against the last modified time of Minecraft.jar. - * \return True if updateCurrentVersion() should be called. - */ - virtual bool shouldUpdateCurrentVersion() = 0; - - /*! - * \brief Updates the current version. - * This function should first set the current version timestamp - * (setCurrentVersionTimestamp()) to the current time. Next, if - * keepCurrent is false, this function should check what the - * instance's current version is and call setCurrentVersion() to - * update it. This function will automatically be called when the - * instance is loaded if shouldUpdateCurrentVersion returns true. - * \param keepCurrent If true, only the version timestamp will be updated. - */ - virtual void updateCurrentVersion(bool keepCurrent = false) = 0; - -protected: - /*! - * \brief Gets the value of the given field in the instance's config file. - * If the value isn't in the config file, defVal is returned instead. - * \param name The name of the field in the config file. - * \param defVal The default value. - * \return The value of the given field or defVal if the field doesn't exist. - * \sa setField() - */ - virtual QVariant getField(const QString &name, QVariant defVal = QVariant()) const; - - /*! - * \brief Sets the value of the given field in the config file. - * \param name The name of the field in the config file. - * \param val The value to set the field to. - * \sa getField() - */ - virtual void setField(const QString &name, QVariant val); - - // Overrides for SettingBase stuff. - virtual QVariant getValue(const QString &name, QVariant defVal = QVariant()) const - { return settings->getValue(name, defVal); } - virtual void setValue(const QString &name, QVariant val) - { setField(name, val); } - - INIFile config; - -private: - QString m_rootDir; -}; - -#endif // INSTANCE_H diff --git a/data/inst/instancelist.cpp b/data/inst/instancelist.cpp deleted file mode 100644 index cbb89f05..00000000 --- a/data/inst/instancelist.cpp +++ /dev/null @@ -1,78 +0,0 @@ -/* Copyright 2013 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 "instancelist.h" - -#include "data/siglist_impl.h" - -#include -#include -#include - -#include "instance.h" -#include "instanceloader.h" - -#include "util/pathutils.h" - - -InstanceList::InstanceList(const QString &instDir, QObject *parent) : - QObject(parent), m_instDir(instDir) -{ - -} - -InstanceList::InstListError InstanceList::loadList() -{ - QDir dir(m_instDir); - QDirIterator iter(dir); - - while (iter.hasNext()) - { - QString subDir = iter.next(); - if (QFileInfo(PathCombine(subDir, "instance.cfg")).exists()) - { - QSharedPointer inst; - InstanceLoader::InstTypeError error = InstanceLoader::loader. - loadInstance(inst.data(), subDir); - - if (inst.data() && error == InstanceLoader::NoError) - { - qDebug(QString("Loaded instance %1").arg(inst->name()).toUtf8()); - inst->setParent(this); - append(QSharedPointer(inst)); - } - else if (error != InstanceLoader::NotAnInstance) - { - QString errorMsg = QString("Failed to load instance %1: "). - arg(QFileInfo(subDir).baseName()).toUtf8(); - - switch (error) - { - case InstanceLoader::TypeNotRegistered: - errorMsg += "Instance type not found."; - break; - } - qDebug(errorMsg.toUtf8()); - } - else if (!inst.data()) - { - qDebug(QString("Error loading instance %1. Instance loader returned null."). - arg(QFileInfo(subDir).baseName()).toUtf8()); - } - } - } - - return NoError; -} diff --git a/data/inst/instancelist.h b/data/inst/instancelist.h deleted file mode 100644 index c43c4cc0..00000000 --- a/data/inst/instancelist.h +++ /dev/null @@ -1,57 +0,0 @@ -/* Copyright 2013 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. - */ - -#ifndef INSTANCELIST_H -#define INSTANCELIST_H - -#include - -#include - -#include "data/siglist.h" - -class Instance; - -class InstanceList : public QObject, public SigList> -{ - Q_OBJECT -public: - explicit InstanceList(const QString &instDir, QObject *parent = 0); - - /*! - * \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 - }; - - QString instDir() const { return m_instDir; } - - /*! - * \brief Loads the instance list. - */ - InstListError loadList(); - - DEFINE_SIGLIST_SIGNALS(QSharedPointer); - SETUP_SIGLIST_SIGNALS(QSharedPointer); -protected: - QString m_instDir; -}; - -#endif // INSTANCELIST_H diff --git a/data/inst/instanceloader.cpp b/data/inst/instanceloader.cpp deleted file mode 100644 index e80a0e9e..00000000 --- a/data/inst/instanceloader.cpp +++ /dev/null @@ -1,106 +0,0 @@ -/* Copyright 2013 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 "instanceloader.h" - -#include - -#include "instancetype.h" - -#include "data/inifile.h" - -#include "util/pathutils.h" - -InstanceLoader InstanceLoader::loader; - -InstanceLoader::InstanceLoader() : - QObject(NULL) -{ - -} - - -InstanceLoader::InstTypeError InstanceLoader::registerInstanceType(InstanceType *type) -{ - // Check to see if the type ID exists. - if (m_typeMap.contains(type->typeID())) - return TypeIDExists; - - // Set the parent to this. - type->setParent(this); - - // Add it to the map. - m_typeMap.insert(type->typeID(), type); - return NoError; -} - -InstanceLoader::InstTypeError InstanceLoader::createInstance(Instance *inst, - const InstanceType *type, - const QString &instDir) -{ - // Check if the type is registered. - if (!type || findType(type->typeID()) != type) - return TypeNotRegistered; - - // Create the instance. - return type->createInstance(inst, instDir); -} - -InstanceLoader::InstTypeError InstanceLoader::loadInstance(Instance *inst, - const InstanceType *type, - const QString &instDir) -{ - // Check if the type is registered. - if (!type || findType(type->typeID()) != type) - return TypeNotRegistered; - - return type->loadInstance(inst, instDir); -} - -InstanceLoader::InstTypeError InstanceLoader::loadInstance(Instance *inst, - const QString &instDir) -{ - QFileInfo instConfig(PathCombine(instDir, "instance.cfg")); - - if (!instConfig.exists()) - return NotAnInstance; - - INIFile ini; - ini.loadFile(instConfig.path()); - QString typeName = ini.get("type", "StdInstance").toString(); - const InstanceType *type = findType(typeName); - - return loadInstance(inst, type, instDir); -} - -const InstanceType *InstanceLoader::findType(const QString &id) -{ - if (!m_typeMap.contains(id)) - return NULL; - else - return m_typeMap[id]; -} - -InstTypeList InstanceLoader::typeList() -{ - InstTypeList typeList; - - for (auto iter = m_typeMap.begin(); iter != m_typeMap.end(); iter++) - { - typeList.append(*iter); - } - - return typeList; -} diff --git a/data/inst/instanceloader.h b/data/inst/instanceloader.h deleted file mode 100644 index 60c4e998..00000000 --- a/data/inst/instanceloader.h +++ /dev/null @@ -1,133 +0,0 @@ -/* Copyright 2013 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. - */ - -#ifndef INSTANCELOADER_H -#define INSTANCELOADER_H - -#include -#include -#include - -class InstanceType; -class Instance; - -typedef QList InstTypeList; - -/*! - * \brief The InstanceLoader is a singleton that manages all of the instance types and handles loading and creating instances. - * Instance types are registered with the instance loader through its registerInstType() function. - * Creating instances is done through the InstanceLoader's createInstance() function. This function takes - */ -class InstanceLoader : public QObject -{ - Q_OBJECT -public: - static InstanceLoader loader; - - /*! - * \brief Error codes returned by functions in the InstanceLoader and InstanceType classes. - * - * - NoError indicates that no error occurred. - * - OtherError indicates that an unspecified error occurred. - * - TypeIDExists is returned by registerInstanceType() if the ID of the type being registered already exists. - * - TypeNotRegistered is returned by createInstance() and loadInstance() when the given type is not registered. - * - InstExists is returned by createInstance() if the given instance directory is already an instance. - * - NotAnInstance is returned by loadInstance() if the given instance directory is not a valid instance. - * - WrongInstType is returned by loadInstance() if the given instance directory's type doesn't match the given type. - */ - enum InstTypeError - { - NoError = 0, - OtherError, - - TypeIDExists, - - TypeNotRegistered, - InstExists, - NotAnInstance, - WrongInstType - }; - - /*! - * \brief Registers the given InstanceType with the instance loader. - * This causes the instance loader to take ownership of the given - * instance type (meaning the instance type's parent will be set to - * the instance loader). - * - * \param type The InstanceType to register. - * \return An InstTypeError error code. - * - TypeIDExists if the given type's is already registered to another instance type. - */ - InstTypeError registerInstanceType(InstanceType *type); - - /*! - * \brief Creates an instance with the given type and stores it in inst. - * - * \param inst Pointer to store the created instance in. - * \param type The type of instance to create. - * \param instDir The instance's directory. - * \return An InstTypeError error code. - * - TypeNotRegistered if the given type is not registered with the InstanceLoader. - * - InstExists if the given instance directory is already an instance. - */ - InstTypeError createInstance(Instance *inst, const InstanceType *type, const QString &instDir); - - /*! - * \brief Loads an instance from the given directory. - * - * \param inst Pointer to store the loaded instance in. - * \param type The type of instance to load. - * \param instDir The instance's directory. - * \return An InstTypeError error code. - * - TypeNotRegistered if the given type is not registered with the InstanceLoader. - * - NotAnInstance if the given instance directory isn't a valid instance. - * - WrongInstType if the given instance directory's type isn't the same as the given type. - */ - InstTypeError loadInstance(Instance *inst, const InstanceType *type, 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 InstTypeError error code. - * - TypeNotRegistered if the instance's type is not registered with the InstanceLoader. - * - NotAnInstance if the given instance directory isn't a valid instance. - */ - InstTypeError loadInstance(Instance *inst, const QString &instDir); - - /*! - * \brief Finds an instance type with the given ID. - * If one cannot be found, returns NULL. - * - * \param id The ID of the type to find. - * \return The type with the given ID. NULL if none were found. - */ - const InstanceType *findType(const QString &id); - - /*! - * \brief Gets a list of the registered instance types. - * - * \return A list of instance types. - */ - InstTypeList typeList(); - -private: - InstanceLoader(); - - QMap m_typeMap; -}; - -#endif // INSTANCELOADER_H diff --git a/data/inst/instancetype.cpp b/data/inst/instancetype.cpp deleted file mode 100644 index 4f27542d..00000000 --- a/data/inst/instancetype.cpp +++ /dev/null @@ -1,21 +0,0 @@ -/* Copyright 2013 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 "instancetype.h" - -InstanceType::InstanceType(QObject *parent) : - QObject(parent) -{ -} diff --git a/data/inst/instancetype.h b/data/inst/instancetype.h deleted file mode 100644 index bd22a17c..00000000 --- a/data/inst/instancetype.h +++ /dev/null @@ -1,83 +0,0 @@ -/* Copyright 2013 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. - */ - -#ifndef INSTANCETYPE_H -#define INSTANCETYPE_H - -#include - -#include "instanceloader.h" - -/*! - * \brief The InstanceType class is a base class for all instance types. - * It handles loading and creating instances of a certain type. There should be - * one of these for each type of instance and they should be registered with the - * InstanceLoader. - * To create an instance, the InstanceLoader calls the type's createInstance() - * function. Loading is done through the loadInstance() function. - */ -class InstanceType : public QObject -{ - Q_OBJECT -public: - explicit InstanceType(QObject *parent = 0); - - friend class InstanceLoader; - - /*! - * \brief Gets the ID for this instance type. - * The type ID should be unique as it is used to identify the type - * of instances when they are loaded. - * Changing this value at runtime results in undefined behavior. - * \return This instance type's ID string. - */ - virtual QString typeID() const = 0; - - /*! - * \brief Gets the name of this instance type as it is displayed to the user. - * \return The instance type's display name. - */ - virtual QString displayName() const = 0; - - /*! - * \brief Gets a longer, more detailed description of this instance type. - * \return The instance type's description. - */ - virtual QString description() const = 0; - -protected: - /*! - * \brief Creates an instance and stores it in inst. - * \param inst Pointer to store the created instance in. - * \param instDir The instance's directory. - * \return An InstTypeError error code. - * TypeNotRegistered if the given type is not registered with the InstanceLoader. - * InstExists if the given instance directory is already an instance. - */ - virtual InstanceLoader::InstTypeError createInstance(Instance *inst, const QString &instDir) const = 0; - - /*! - * \brief Loads an instance from the given directory. - * \param inst Pointer to store the loaded instance in. - * \param instDir The instance's directory. - * \return An InstTypeError error code. - * TypeNotRegistered if the given type is not registered with the InstanceLoader. - * NotAnInstance if the given instance directory isn't a valid instance. - * WrongInstType if the given instance directory's type isn't an instance of this type. - */ - virtual InstanceLoader::InstTypeError loadInstance(Instance *inst, const QString &instDir) const = 0; -}; - -#endif // INSTANCETYPE_H diff --git a/data/plugin/instancetypeplugin.h b/data/plugin/instancetypeplugin.h deleted file mode 100644 index 8e3febdb..00000000 --- a/data/plugin/instancetypeplugin.h +++ /dev/null @@ -1,41 +0,0 @@ -/* Copyright 2013 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. - */ - -#ifndef INSTANCETYPEPLUGIN_H -#define INSTANCETYPEPLUGIN_H - -#include - -#include "data/inst/instancetype.h" - -/*! - * \brief Interface for plugins that want to provide custom instance types. - */ -class InstanceTypePlugin -{ -public: - /*! - * \brief Gets a QList containing the instance types that this plugin provides. - * These instance types are then registered with the InstanceLoader. - * The InstanceType objects should \e not be deleted by the plugin. Once they - * are registered, they belong to the InstanceLoader. - * \return A QList containing this plugin's instance types. - */ - virtual QList getInstanceTypes() = 0; -}; - -Q_DECLARE_INTERFACE(InstanceTypePlugin, "net.forkk.MultiMC.InstanceTypePlugin/0.1") - -#endif // INSTANCETYPEPLUGIN_H diff --git a/data/plugin/pluginmanager.cpp b/data/plugin/pluginmanager.cpp index cd33b285..2f066293 100644 --- a/data/plugin/pluginmanager.cpp +++ b/data/plugin/pluginmanager.cpp @@ -18,10 +18,17 @@ #include #include #include +#include + +#include #include -#include "data/plugin/instancetypeplugin.h" +#include "instancetypeinterface.h" + +// MultiMC's API version. This must match the "api" field in each plugin's +// metadata or MultiMC won't consider them valid MultiMC plugin. +#define MMC_API_VERSION "MultiMC5-API-1" PluginManager PluginManager::manager; @@ -33,8 +40,16 @@ PluginManager::PluginManager() : bool PluginManager::loadPlugins(QString pluginDir) { + // Delete the loaded plugins and clear the list. + for (int i = 0; i < m_plugins.count(); i++) + { + delete m_plugins[i]; + } m_plugins.clear(); + qDebug(QString("Loading plugins from directory: %1"). + arg(pluginDir).toUtf8()); + QDir dir(pluginDir); QDirIterator iter(dir); @@ -44,53 +59,47 @@ bool PluginManager::loadPlugins(QString pluginDir) if (pluginFile.exists() && pluginFile.isFile()) { - QPluginLoader pluginLoader(pluginFile.absoluteFilePath()); - pluginLoader.load(); - QObject *plugin = pluginLoader.instance(); - if (plugin) - { - qDebug(QString("Loaded plugin %1."). - arg(pluginFile.baseName()).toUtf8()); - m_plugins.push_back(plugin); - } - else + qDebug(QString("Attempting to load plugin: %1"). + arg(pluginFile.canonicalFilePath()).toUtf8()); + + QPluginLoader *pluginLoader = new QPluginLoader(pluginFile.absoluteFilePath()); + + QJsonObject pluginInfo = pluginLoader->metaData(); + QJsonObject pluginMetadata = pluginInfo.value("MetaData").toObject(); + + if (pluginMetadata.value("api").toString("") != MMC_API_VERSION) { - qWarning(QString("Error loading plugin %1. Not a valid plugin."). - arg(pluginFile.baseName()).toUtf8()); + // If "api" is not specified, it's not a MultiMC plugin. + qDebug(QString("Not loading plugin %1. Not a valid MultiMC plugin. " + "API: %2"). + arg(pluginFile.canonicalFilePath(), pluginMetadata.value("api").toString("")).toUtf8()); + continue; } + + qDebug(QString("Loaded plugin: %1"). + arg(pluginInfo.value("IID").toString()).toUtf8()); + m_plugins.push_back(pluginLoader); } } return true; } -bool PluginManager::initInstanceTypes() +QPluginLoader *PluginManager::getPlugin(int index) +{ + return m_plugins[index]; +} + +void PluginManager::initInstanceTypes() { for (int i = 0; i < m_plugins.count(); i++) { - InstanceTypePlugin *plugin = qobject_cast(m_plugins[i]); - if (plugin) + InstanceTypeInterface *instType = qobject_cast(m_plugins[i]->instance()); + + if (instType) { - QList instanceTypes = plugin->getInstanceTypes(); - - for (int i = 0; i < instanceTypes.count(); i++) - { - InstanceLoader::InstTypeError error = - InstanceLoader::loader.registerInstanceType(instanceTypes[i]); - switch (error) - { - case InstanceLoader::TypeIDExists: - qWarning(QString("Instance type %1 already registered."). - arg(instanceTypes[i]->typeID()).toUtf8()); - } - } + // TODO: Handle errors + InstanceLoader::get().registerInstanceType(instType); } } - - return true; -} - -QObject *PluginManager::getPlugin(int index) -{ - return m_plugins[index]; } diff --git a/data/plugin/pluginmanager.h b/data/plugin/pluginmanager.h index 8e2dba0d..b93fd6d2 100644 --- a/data/plugin/pluginmanager.h +++ b/data/plugin/pluginmanager.h @@ -41,12 +41,6 @@ public: */ bool loadPlugins(QString pluginDir); - /*! - * \brief Initializes the instance type plugins. - * \return True if successful. False on failure. - */ - bool initInstanceTypes(); - /*! * \brief Checks how many plugins are loaded. * \return The number of plugins. @@ -58,12 +52,19 @@ public: * \param index The index of the plugin to get. * \return The plugin at the given index. */ - QObject *getPlugin(int index); + QPluginLoader *getPlugin(int index); + + /*! + * \brief Initializes and registers all the instance types. + * This is done by going through the plugin list and registering all of the + * plugins that derive from the InstanceTypeInterface with the InstanceLoader. + */ + void initInstanceTypes(); private: PluginManager(); - QList m_plugins; + QList m_plugins; static PluginManager manager; }; diff --git a/data/siglist.h b/data/siglist.h deleted file mode 100644 index b6432b6e..00000000 --- a/data/siglist.h +++ /dev/null @@ -1,130 +0,0 @@ -/* Copyright 2013 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. - */ - -#ifndef SIGLIST_H -#define SIGLIST_H - -#include -#include - -// A QList that allows emitting signals when the list changes. -// Since QObject doesn't support templates, to use this class with a -// certain type, you should create a class deriving from SigList and then -// call the DEFINE_SIGLIST_SIGNALS(T) and SETUP_SIGLIST_SIGNALS(T) macros. -template -class SigList : public QList -{ - -public: - explicit SigList() : QList() {} - - virtual void append(const T &value); - virtual void append(const QList &other); - - virtual void clear(); - - virtual void erase(iterator pos); - virtual void erase(iterator first, iterator last); - - virtual void insert(int i, const T &t); - virtual void insert(iterator before, const T &t); - - virtual void move(int from, int to); - - virtual void pop_back() { takeLast(); } - virtual void pop_front() { takeFirst(); } - - virtual void push_back(const T &t) { append(t); } - virtual void push_front(const T &t) { prepend(t); } - - virtual void prepend(const T &t); - - virtual int removeAll(const T &t); - virtual bool removeOne(const T &t); - - virtual void removeAt(int i) { takeAt(i); } - virtual void removeFirst() { takeFirst(); } - virtual void removeLast() { takeLast(); } - - virtual void swap(QList &other); - virtual void swap(int i, int j); - - virtual T takeAt(int i); - virtual T takeFirst(); - virtual T takeLast(); - - virtual QList &operator +=(const QList &other) { append(other); return *this; } - virtual QList &operator +=(const T &value) { append(value); return *this; } - virtual QList &operator <<(const QList &other) { append(other); return *this; } - virtual QList &operator <<(const T &value) { append(value); return *this; } - - virtual QList &operator =(const QList &other); - - - // Signal emitted after an item is added to the list. - // Contains a reference to item and the item's new index. - virtual void onItemAdded(const T &item, int index) = 0; - - // Signal emitted after multiple items are added to the list at once. - // The items parameter is a const reference to a QList of the items that - // were added. - // The firstIndex parameter is the new index of the first item added. - virtual void onItemsAdded(const QList &items, int firstIndex) = 0; - - // Signal emitted after an item is removed to the list. - // Contains a reference to the item and the item's old index. - virtual void onItemRemoved(const T &item, int index) = 0; - - // Signal emitted after multiple items are removed from the list at once. - // The items parameter is a const reference to a QList of the items that - // were added. - // The firstIndex parameter is the new index of the first item added. - virtual void onItemsRemoved(const QList &items, int firstIndex) = 0; - - // Signal emitted after an item is moved to another index. - // Contains the item, the old index, and the new index. - virtual void onItemMoved(const T &item, int oldIndex, int newIndex) = 0; - - // Signal emitted after an operation that changes the whole list occurs. - // This signal should be treated as if all data in the entire list was cleared - // and new data added in its place. - virtual void onInvalidated() = 0; -}; - -// Defines the signals for a SigList -#define DEFINE_SIGLIST_SIGNALS(TYPE) \ - Q_SIGNAL void itemAdded(TYPE const &item, int index);\ - Q_SIGNAL void itemsAdded(const QList &items, int firstIndex);\ - Q_SIGNAL void itemRemoved(TYPE const &item, int index);\ - Q_SIGNAL void itemsRemoved(const QList &items, int firstIndex);\ - Q_SIGNAL void itemMoved(TYPE const &item, int oldIndex, int newIndex);\ - Q_SIGNAL void invalidated(); - -// Overrides the onItem* functions and causes them to emit their corresponding -// signals. -#define SETUP_SIGLIST_SIGNALS(TYPE) \ - virtual void onItemAdded(TYPE const &item, int index)\ - { emit itemAdded(item, index); }\ - virtual void onItemsAdded(const QList &items, int firstIndex)\ - { emit itemsAdded(items, firstIndex); }\ - virtual void onItemRemoved(TYPE const &item, int index)\ - { emit itemRemoved(item, index); }\ - virtual void onItemsRemoved(const QList &items, int firstIndex)\ - { emit itemsRemoved(items, firstIndex); }\ - virtual void onItemMoved(TYPE const &item, int oldIndex, int newIndex)\ - { emit itemMoved(item, oldIndex, newIndex); }\ - virtual void onInvalidated() { emit invalidated(); } - -#endif // SIGLIST_H diff --git a/data/siglist_impl.h b/data/siglist_impl.h deleted file mode 100644 index 16ddd9b0..00000000 --- a/data/siglist_impl.h +++ /dev/null @@ -1,156 +0,0 @@ -/* Copyright 2013 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 "siglist.h" - -template -void SigList::append(const T &value) -{ - QList::append(value); - onItemAdded(value, length() - 1); -} - -template -void SigList::prepend(const T &value) -{ - QList::prepend(value); - onItemAdded(value, 0); -} - -template -void SigList::append(const QList &other) -{ - int index = length(); - QList::append(other); - onItemsAdded(other, index); -} - -template -void SigList::clear() -{ - QList::clear(); - onInvalidated(); -} - -template -void SigList::erase(QList::iterator pos) -{ - T value = *pos; - int index = indexOf(*pos); - QList::erase(pos); - onItemRemoved(value, index); -} - -template -void SigList::erase(QList::iterator first, QList::iterator last) -{ - QList removedValues; - int firstIndex = indexOf(*first); - - for (QList::iterator iter = first; iter < last; iter++) - { - removedValues << *iter; - QList::erase(iter); - } - - onItemsRemoved(removedValues, firstIndex); -} - -template -void SigList::insert(int i, const T &t) -{ - QList::insert(i, t); - onItemAdded(t, i); -} - -template -void SigList::insert(QList::iterator before, const T &t) -{ - QList::insert(before, t); - onItemAdded(t, indexOf(t)); -} - -template -void SigList::move(int from, int to) -{ - const T &item = at(from); - QList::move(from, to); - onItemMoved(item, from, to); -} - -template -int SigList::removeAll(const T &t) -{ - int retVal = QList::removeAll(t); - onInvalidated(); - return retVal; -} - -template -bool SigList::removeOne(const T &t) -{ - int index = indexOf(t); - if (QList::removeOne(t)) - { - onItemRemoved(t, index); - return true; - } - return false; -} - -template -void SigList::swap(QList &other) -{ - QList::swap(other); - onInvalidated(); -} - -template -void SigList::swap(int i, int j) -{ - const T &item1 = at(i); - const T &item2 = at(j); - QList::swap(i, j); - onItemMoved(item1, i, j); - onItemMoved(item2, j, i); -} - -template -T SigList::takeAt(int i) -{ - T val = QList::takeAt(i); - onItemRemoved(val, i); - return val; -} - -template -T SigList::takeFirst() -{ - return takeAt(0); -} - -template -T SigList::takeLast() -{ - return takeAt(length() - 1); -} - -template -QList &SigList::operator =(const QList &other) -{ - QList::operator =(other); - onInvalidated(); - return *this; -} diff --git a/data/version/instversion.cpp b/data/version/instversion.cpp deleted file mode 100644 index 1493153a..00000000 --- a/data/version/instversion.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* Copyright 2013 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 "instversion.h" -#include "instversionlist.h" - -InstVersion::InstVersion(InstVersionList *parent) : - QObject(parent) -{ - -} - -InstVersionList *InstVersion::versionList() const -{ - // Parent should *always* be an InstVersionList - if (!parent() || !parent()->inherits("InstVersionList")) - return NULL; - else - return (InstVersionList *)parent(); -} diff --git a/data/version/instversion.h b/data/version/instversion.h deleted file mode 100644 index 361563fd..00000000 --- a/data/version/instversion.h +++ /dev/null @@ -1,51 +0,0 @@ -/* Copyright 2013 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. - */ - -#ifndef INSTVERSION_H -#define INSTVERSION_H - -#include - -class InstVersionList; - -class InstVersion : public QObject -{ - Q_OBJECT -public: - // Constructs a new InstVersion with the given parent. The parent *must* - // be the InstVersionList that contains this InstVersion. The InstVersion - // should be added to the list immediately after being created as any calls - // to id() will likely fail unless the InstVersion is in a list. - explicit InstVersion(InstVersionList *parent = 0); - - // Returns this InstVersion's ID. This is usually just the InstVersion's index - // within its InstVersionList, but not always. - // If this InstVersion is not in an InstVersionList, returns -1. - virtual int id() const = 0; - - // Returns this InstVersion's name. This is displayed to the user in the GUI - // and is usually just the version number ("1.4.7"), for example. - virtual QString name() const = 0; - - // Returns this InstVersion's name. This is usually displayed to the user - // in the GUI and specifies what kind of version this is. For example: it - // could be "Snapshot", "Latest Version", "MCNostalgia", etc. - virtual QString type() const = 0; - - // Returns the version list that this InstVersion is a part of. - virtual InstVersionList *versionList() const; -}; - -#endif // INSTVERSION_H diff --git a/data/version/instversionlist.cpp b/data/version/instversionlist.cpp deleted file mode 100644 index ab57f94c..00000000 --- a/data/version/instversionlist.cpp +++ /dev/null @@ -1,21 +0,0 @@ -/* Copyright 2013 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 "instversionlist.h" - -InstVersionList::InstVersionList() : - QObject(NULL) -{ -} diff --git a/data/version/instversionlist.h b/data/version/instversionlist.h deleted file mode 100644 index f79bc1b0..00000000 --- a/data/version/instversionlist.h +++ /dev/null @@ -1,43 +0,0 @@ -/* Copyright 2013 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. - */ - -#ifndef INSTVERSIONLIST_H -#define INSTVERSIONLIST_H - -#include - -class InstVersion; - -// 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. -class InstVersionList : public QObject -{ - Q_OBJECT -public: - explicit InstVersionList(); - - // Reloads the version list. - virtual void loadVersionList() = 0; - - // Gets the version at the given index. - virtual const InstVersion *at(int i) const = 0; - - // Returns the number of versions in the list. - virtual int count() const = 0; -}; - -#endif // INSTVERSIONLIST_H -- cgit v1.2.3