diff options
Diffstat (limited to 'data')
-rw-r--r-- | data/appsettings.cpp | 40 | ||||
-rw-r--r-- | data/appsettings.h | 119 | ||||
-rw-r--r-- | data/inifile.cpp | 86 | ||||
-rw-r--r-- | data/inifile.h | 36 | ||||
-rw-r--r-- | data/instancebase.cpp | 109 | ||||
-rw-r--r-- | data/instancebase.h | 58 | ||||
-rw-r--r-- | data/instancemodel.cpp | 454 | ||||
-rw-r--r-- | data/instancemodel.h | 137 | ||||
-rw-r--r-- | data/plugin/pluginmanager.cpp | 105 | ||||
-rw-r--r-- | data/plugin/pluginmanager.h | 72 | ||||
-rw-r--r-- | data/siglist.h | 130 | ||||
-rw-r--r-- | data/siglist_imp.h | 156 | ||||
-rw-r--r-- | data/stdinstance.cpp | 22 | ||||
-rw-r--r-- | data/stdinstance.h | 28 |
14 files changed, 177 insertions, 1375 deletions
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 a9068bfd..00000000 --- a/data/appsettings.h +++ /dev/null @@ -1,119 +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 <QObject> -#include <QSettings> -#include <QColor> -#include <QPoint> - -#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<valType>(); } \ - virtual void set ## funcName(valType value) { setValue(name, value); } - -#define DEFINE_SETTING(name, valType, defVal) \ - DEFINE_SETTING_ADVANCED(name, STR_VAL(name), valType, defVal) - -#define DEFINE_OVERRIDE_SETTING(overrideName) \ - - -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, "") - -protected: - 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; } - -protected: - virtual QVariant getValue(const QString &name, QVariant defVal = QVariant()) const; - virtual void setValue(const QString& name, QVariant val); - - 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 <QFile> -#include <QTextStream> -#include <QStringList> - -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 <QMap> -#include <QString> -#include <QVariant> - -// Sectionless INI parser (for instance config files) -class INIFile : public QMap<QString, QVariant> -{ -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/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 <QFileInfo> -#include <QDir> - -#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 <QObject> -#include <QString> - -#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.cpp b/data/instancemodel.cpp deleted file mode 100644 index 0f1e59f6..00000000 --- a/data/instancemodel.cpp +++ /dev/null @@ -1,454 +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 <QString> - -#include <QDir> -#include <QFile> -#include <QDirIterator> -#include <QTextStream> - -#include <QJsonDocument> -#include <QJsonObject> -#include <QJsonArray> - -#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<QString, QString> 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(); - - InstanceModelItem *item = static_cast<InstanceModelItem*>(index.internalPointer()); - - return item->data(role); -} - -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<InstanceModelItem*>(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<InstanceModelItem*>(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<InstanceModelItem*>(parent.internalPointer()); - - return parentItem->numChildren(); -} - -bool InstanceModel::saveGroupInfo() const -{ - /* - using namespace boost::property_tree; - ptree pt; - - pt.put<int>("formatVersion", GROUP_FILE_FORMAT_VERSION); - - try - { - typedef QMap<InstanceGroup *, QVector<InstanceBase*> > 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<bool>("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 4bd34831..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 INSTANCELIST_H -#define INSTANCELIST_H - -#include <QList> -#include <QMap> -#include <QSet> -#include <qabstractitemmodel.h> - -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<InstanceBase*> 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<InstanceGroup*> groups; - InstanceGroup * implicitGroup; -}; - -#endif // INSTANCELIST_H diff --git a/data/plugin/pluginmanager.cpp b/data/plugin/pluginmanager.cpp new file mode 100644 index 00000000..2f066293 --- /dev/null +++ b/data/plugin/pluginmanager.cpp @@ -0,0 +1,105 @@ +/* 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 <QDir> +#include <QDirIterator> +#include <QFileInfo> +#include <QVariant> + +#include <QJsonObject> + +#include <QtPlugin> + +#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; + +PluginManager::PluginManager() : + QObject(NULL) +{ + +} + +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); + + while (iter.hasNext()) + { + QFileInfo pluginFile(dir.absoluteFilePath(iter.next())); + + if (pluginFile.exists() && pluginFile.isFile()) + { + 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) + { + // 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; +} + +QPluginLoader *PluginManager::getPlugin(int index) +{ + return m_plugins[index]; +} + +void PluginManager::initInstanceTypes() +{ + for (int i = 0; i < m_plugins.count(); i++) + { + InstanceTypeInterface *instType = qobject_cast<InstanceTypeInterface *>(m_plugins[i]->instance()); + + if (instType) + { + // TODO: Handle errors + InstanceLoader::get().registerInstanceType(instType); + } + } +} diff --git a/data/plugin/pluginmanager.h b/data/plugin/pluginmanager.h new file mode 100644 index 00000000..b93fd6d2 --- /dev/null +++ b/data/plugin/pluginmanager.h @@ -0,0 +1,72 @@ +/* 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 <QObject> +#include <QList> +#include <QPluginLoader> + +/*! + * \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 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. + */ + 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<QPluginLoader *> m_plugins; + + static PluginManager manager; +}; + +#endif // PLUGINMANAGER_H 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 <QObject> -#include <QList> - -// 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<T> and then -// call the DEFINE_SIGLIST_SIGNALS(T) and SETUP_SIGLIST_SIGNALS(T) macros. -template <typename T> -class SigList : public QList<T> -{ - -public: - explicit SigList() : QList<T>() {} - - virtual void append(const T &value); - virtual void append(const QList<T> &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<T> &other); - virtual void swap(int i, int j); - - virtual T takeAt(int i); - virtual T takeFirst(); - virtual T takeLast(); - - virtual QList<T> &operator +=(const QList<T> &other) { append(other); return *this; } - virtual QList<T> &operator +=(const T &value) { append(value); return *this; } - virtual QList<T> &operator <<(const QList<T> &other) { append(other); return *this; } - virtual QList<T> &operator <<(const T &value) { append(value); return *this; } - - virtual QList<T> &operator =(const QList<T> &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<T> &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<T> &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<TYPE> &items, int firstIndex);\ - Q_SIGNAL void itemRemoved(TYPE const &item, int index);\ - Q_SIGNAL void itemsRemoved(const QList<TYPE> &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<TYPE> &items, int firstIndex)\ - { emit itemsAdded(items, firstIndex); }\ - virtual void onItemRemoved(TYPE const &item, int index)\ - { emit itemRemoved(item, index); }\ - virtual void onItemsRemoved(const QList<TYPE> &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_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 <typename T> -void SigList<T>::append(const T &value) -{ - QList<T>::append(value); - onItemAdded(value, length() - 1); -} - -template <typename T> -void SigList<T>::prepend(const T &value) -{ - QList<T>::prepend(value); - onItemAdded(value, 0); -} - -template <typename T> -void SigList<T>::append(const QList<T> &other) -{ - int index = length(); - QList<T>::append(other); - onItemsAdded(other, index); -} - -template <typename T> -void SigList<T>::clear() -{ - QList<T>::clear(); - onInvalidated(); -} - -template <typename T> -void SigList<T>::erase(QList<T>::iterator pos) -{ - T value = *pos; - int index = indexOf(*pos); - QList<T>::erase(pos); - onItemRemoved(value, index); -} - -template <typename T> -void SigList<T>::erase(QList<T>::iterator first, QList<T>::iterator last) -{ - QList<T> removedValues; - int firstIndex = indexOf(*first); - - for (QList<T>::iterator iter = first; iter < last; iter++) - { - removedValues << *iter; - QList<T>::erase(iter); - } - - onItemsRemoved(removedValues, firstIndex); -} - -template <typename T> -void SigList<T>::insert(int i, const T &t) -{ - QList<T>::insert(i, t); - onItemAdded(t, i); -} - -template <typename T> -void SigList<T>::insert(QList<T>::iterator before, const T &t) -{ - QList<T>::insert(before, t); - onItemAdded(t, indexOf(t)); -} - -template <typename T> -void SigList<T>::move(int from, int to) -{ - const T &item = at(from); - QList<T>::move(from, to); - onItemMoved(item, from, to); -} - -template <typename T> -int SigList<T>::removeAll(const T &t) -{ - int retVal = QList<T>::removeAll(t); - onInvalidated(); - return retVal; -} - -template <typename T> -bool SigList<T>::removeOne(const T &t) -{ - int index = indexOf(t); - if (QList<T>::removeOne(t)) - { - onItemRemoved(t, index); - return true; - } - return false; -} - -template <typename T> -void SigList<T>::swap(QList<T> &other) -{ - QList<T>::swap(other); - onInvalidated(); -} - -template <typename T> -void SigList<T>::swap(int i, int j) -{ - const T &item1 = at(i); - const T &item2 = at(j); - QList<T>::swap(i, j); - onItemMoved(item1, i, j); - onItemMoved(item2, j, i); -} - -template <typename T> -T SigList<T>::takeAt(int i) -{ - T val = QList<T>::takeAt(i); - onItemRemoved(val, i); - return val; -} - -template <typename T> -T SigList<T>::takeFirst() -{ - return takeAt(0); -} - -template <typename T> -T SigList<T>::takeLast() -{ - return takeAt(length() - 1); -} - -template <typename T> -QList<T> &SigList<T>::operator =(const QList<T> &other) -{ - QList<T>::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 |