diff options
| author | Petr Mrázek <peterix@gmail.com> | 2013-08-17 13:40:51 +0200 |
|---|---|---|
| committer | Petr Mrázek <peterix@gmail.com> | 2013-08-17 13:40:51 +0200 |
| commit | 253067c782955380bbf66ac0475dc954375b1ff4 (patch) | |
| tree | ca97e231fd3a764256d95b5fc8d08fc25ff72161 /logic | |
| parent | 77e80665422c4e97e2286418ab55e20c4030023b (diff) | |
| download | PrismLauncher-253067c782955380bbf66ac0475dc954375b1ff4.tar.gz PrismLauncher-253067c782955380bbf66ac0475dc954375b1ff4.tar.bz2 PrismLauncher-253067c782955380bbf66ac0475dc954375b1ff4.zip | |
Move all the things (YES. Move them.)
Also, implemented some basic modlist logic, to be wired up.
Diffstat (limited to 'logic')
55 files changed, 6254 insertions, 0 deletions
diff --git a/logic/BaseInstance.cpp b/logic/BaseInstance.cpp new file mode 100644 index 00000000..c2df34e1 --- /dev/null +++ b/logic/BaseInstance.cpp @@ -0,0 +1,181 @@ +/* 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 "BaseInstance.h" +#include "BaseInstance_p.h" + +#include <QFileInfo> + +#include "inisettingsobject.h" +#include "setting.h" +#include "overridesetting.h" + +#include "pathutils.h" +#include "lists/MinecraftVersionList.h" + + +BaseInstance::BaseInstance( BaseInstancePrivate* d_in, + const QString& rootDir, + SettingsObject* settings_obj, + QObject* parent + ) +:inst_d(d_in), QObject(parent) +{ + I_D(BaseInstance); + d->m_settings = settings_obj; + d->m_rootDir = rootDir; + + settings().registerSetting(new Setting("name", "Unnamed Instance")); + settings().registerSetting(new Setting("iconKey", "default")); + settings().registerSetting(new Setting("notes", "")); + settings().registerSetting(new Setting("lastLaunchTime", 0)); + + // Java Settings + settings().registerSetting(new Setting("OverrideJava", false)); + settings().registerSetting(new OverrideSetting("JavaPath", globalSettings->getSetting("JavaPath"))); + settings().registerSetting(new OverrideSetting("JvmArgs", globalSettings->getSetting("JvmArgs"))); + + // Custom Commands + settings().registerSetting(new Setting("OverrideCommands", false)); + settings().registerSetting(new OverrideSetting("PreLaunchCommand", globalSettings->getSetting("PreLaunchCommand"))); + settings().registerSetting(new OverrideSetting("PostExitCommand", globalSettings->getSetting("PostExitCommand"))); + + // Window Size + settings().registerSetting(new Setting("OverrideWindow", false)); + settings().registerSetting(new OverrideSetting("LaunchMaximized", globalSettings->getSetting("LaunchMaximized"))); + settings().registerSetting(new OverrideSetting("MinecraftWinWidth", globalSettings->getSetting("MinecraftWinWidth"))); + settings().registerSetting(new OverrideSetting("MinecraftWinHeight", globalSettings->getSetting("MinecraftWinHeight"))); + + // Memory + settings().registerSetting(new Setting("OverrideMemory", false)); + settings().registerSetting(new OverrideSetting("MinMemAlloc", globalSettings->getSetting("MinMemAlloc"))); + settings().registerSetting(new OverrideSetting("MaxMemAlloc", globalSettings->getSetting("MaxMemAlloc"))); + + // Auto login + settings().registerSetting(new Setting("OverrideLogin", false)); + settings().registerSetting(new OverrideSetting("AutoLogin", globalSettings->getSetting("AutoLogin"))); + + // Console + settings().registerSetting(new Setting("OverrideConsole", false)); + settings().registerSetting(new OverrideSetting("ShowConsole", globalSettings->getSetting("ShowConsole"))); + settings().registerSetting(new OverrideSetting("AutoCloseConsole", globalSettings->getSetting("AutoCloseConsole"))); +} + +QString BaseInstance::id() const +{ + return QFileInfo(instanceRoot()).fileName(); +} + +QString BaseInstance::instanceType() const +{ + I_D(BaseInstance); + return d->m_settings->get("InstanceType").toString(); +} + + +QString BaseInstance::instanceRoot() const +{ + I_D(BaseInstance); + return d->m_rootDir; +} + +QString BaseInstance::minecraftRoot() const +{ + QFileInfo mcDir(PathCombine(instanceRoot(), "minecraft")); + QFileInfo dotMCDir(PathCombine(instanceRoot(), ".minecraft")); + + if (dotMCDir.exists() && !mcDir.exists()) + return dotMCDir.filePath(); + else + return mcDir.filePath(); +} + +InstanceList *BaseInstance::instList() const +{ + if (parent()->inherits("InstanceList")) + return (InstanceList *)parent(); + else + return NULL; +} + +InstVersionList *BaseInstance::versionList() const +{ + return &MinecraftVersionList::getMainList(); +} + +SettingsObject &BaseInstance::settings() const +{ + I_D(BaseInstance); + return *d->m_settings; +} + +qint64 BaseInstance::lastLaunch() const +{ + I_D(BaseInstance); + return d->m_settings->get ( "lastLaunchTime" ).value<qint64>(); +} +void BaseInstance::setLastLaunch ( qint64 val ) +{ + I_D(BaseInstance); + d->m_settings->set ( "lastLaunchTime", val ); + emit propertiesChanged ( this ); +} + +void BaseInstance::setGroup ( QString val ) +{ + I_D(BaseInstance); + d->m_group = val; + emit propertiesChanged ( this ); +} +QString BaseInstance::group() const +{ + I_D(BaseInstance); + return d->m_group; +} + +void BaseInstance::setNotes ( QString val ) +{ + I_D(BaseInstance); + d->m_settings->set ( "notes", val ); +} +QString BaseInstance::notes() const +{ + I_D(BaseInstance); + return d->m_settings->get ( "notes" ).toString(); +} + +void BaseInstance::setIconKey ( QString val ) +{ + I_D(BaseInstance); + d->m_settings->set ( "iconKey", val ); + emit propertiesChanged ( this ); +} +QString BaseInstance::iconKey() const +{ + I_D(BaseInstance); + return d->m_settings->get ( "iconKey" ).toString(); +} + +void BaseInstance::setName ( QString val ) +{ + I_D(BaseInstance); + d->m_settings->set ( "name", val ); + emit propertiesChanged ( this ); +} +QString BaseInstance::name() const +{ + I_D(BaseInstance); + return d->m_settings->get ( "name" ).toString(); +} diff --git a/logic/BaseInstance.h b/logic/BaseInstance.h new file mode 100644 index 00000000..8b5de6f5 --- /dev/null +++ b/logic/BaseInstance.h @@ -0,0 +1,145 @@ +/* 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. + */ + +#pragma once + +#include <QObject> +#include <QDateTime> + +#include <settingsobject.h> + +#include "inifile.h" +#include "lists/InstVersionList.h" + +class QDialog; +class BaseUpdate; +class MinecraftProcess; +class OneSixUpdate; +class InstanceList; +class BaseInstancePrivate; + +/*! + * \brief Base class for instances. + * This class implements many functions that are common between instances and + * provides a standard interface for all instances. + * + * To create a new instance type, create a new class inheriting from this class + * and implement the pure virtual functions. + */ +class BaseInstance : public QObject +{ + Q_OBJECT +protected: + /// no-touchy! + BaseInstance(BaseInstancePrivate * d, const QString &rootDir, SettingsObject * settings, QObject *parent = 0); +public: + /// virtual destructor to make sure the destruction is COMPLETE + virtual ~BaseInstance() {}; + + /// The instance's ID. The ID SHALL be determined by MMC internally. The ID IS guaranteed to be unique. + QString id() const; + + /// get the type of this instance + QString instanceType() const; + + /// Path to the instance's root directory. + QString instanceRoot() const; + + /// Path to the instance's minecraft directory. + QString minecraftRoot() const; + + QString name() const; + void setName(QString val); + + QString iconKey() const; + void setIconKey(QString val); + + QString notes() const; + void setNotes(QString val); + + QString group() const; + void setGroup(QString val); + + virtual QString intendedVersionId() const = 0; + virtual bool setIntendedVersionId(QString version) = 0; + + /*! + * The instance's current version. + * This value represents the instance's current version. If this value is + * different from the intendedVersion, the instance should be updated. + * \warning Don't change this value unless you know what you're doing. + */ + virtual QString currentVersionId() const = 0; + //virtual void setCurrentVersionId(QString val) = 0; + + /*! + * Whether or not Minecraft should be downloaded when the instance is launched. + */ + virtual bool shouldUpdate() const = 0; + virtual void setShouldUpdate(bool val) = 0; + + /** + * Gets the time that the instance was last launched. + * Stored in milliseconds since epoch. + */ + qint64 lastLaunch() const; + /// Sets the last launched time to 'val' milliseconds since epoch + void setLastLaunch(qint64 val = QDateTime::currentMSecsSinceEpoch()); + + /*! + * \brief Gets 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. + */ + InstanceList *instList() const; + + /*! + * \brief Gets a pointer to this instance's version list. + * \return A pointer to the available version list for this instance. + */ + virtual InstVersionList *versionList() const; + + /*! + * \brief Gets this instance's settings object. + * This settings object stores instance-specific settings. + * \return A pointer to this instance's settings object. + */ + virtual SettingsObject &settings() const; + + /// returns a valid update task if update is needed, NULL otherwise + virtual BaseUpdate* doUpdate() = 0; + + /// returns a valid minecraft process, ready for launch + virtual MinecraftProcess* prepareForLaunch(QString user, QString session) = 0; + + /// do any necessary cleanups after the instance finishes. also runs before 'prepareForLaunch' + virtual void cleanupAfterRun() = 0; + + /// create a mod edit dialog for the instance + virtual QSharedPointer<QDialog> createModEditDialog ( QWidget* parent ) = 0; +signals: + /*! + * \brief Signal emitted when properties relevant to the instance view change + */ + void propertiesChanged(BaseInstance * inst); + +protected: + QSharedPointer<BaseInstancePrivate> inst_d; +}; + +// pointer for lazy people +typedef QSharedPointer<BaseInstance> InstancePtr; + diff --git a/logic/BaseInstance_p.h b/logic/BaseInstance_p.h new file mode 100644 index 00000000..a30916a4 --- /dev/null +++ b/logic/BaseInstance_p.h @@ -0,0 +1,14 @@ +#pragma once +#include <QString> +#include <settingsobject.h> + +class BaseInstance; + +#define I_D(Class) Class##Private * const d = (Class##Private * const) inst_d.data() + +struct BaseInstancePrivate +{ + QString m_rootDir; + QString m_group; + SettingsObject *m_settings; +};
\ No newline at end of file diff --git a/logic/BaseUpdate.cpp b/logic/BaseUpdate.cpp new file mode 100644 index 00000000..b086ab14 --- /dev/null +++ b/logic/BaseUpdate.cpp @@ -0,0 +1,13 @@ +#include "BaseUpdate.h" + +BaseUpdate::BaseUpdate ( BaseInstance* inst, QObject* parent ) : Task ( parent ) +{ + m_inst = inst; +} + +void BaseUpdate::updateDownloadProgress(qint64 current, qint64 total) +{ + // The progress on the current file is current / total + float currentDLProgress = (float) current / (float) total; + setProgress((int)(currentDLProgress * 100)); // convert to percentage +}
\ No newline at end of file diff --git a/logic/BaseUpdate.h b/logic/BaseUpdate.h new file mode 100644 index 00000000..d1e7b735 --- /dev/null +++ b/logic/BaseUpdate.h @@ -0,0 +1,49 @@ +/* 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. + */ + +#pragma once + +#include <QObject> +#include <QList> +#include <QUrl> + +#include "net/DownloadJob.h" + +#include "tasks/Task.h" + +class MinecraftVersion; +class BaseInstance; + +/*! + * The game update task is the task that handles downloading instances' files. + */ +class BaseUpdate : public Task +{ + Q_OBJECT +public: + explicit BaseUpdate(BaseInstance *inst, QObject *parent = 0); + + virtual void executeTask() = 0; + +protected slots: + //virtual void error(const QString &msg); + void updateDownloadProgress(qint64 current, qint64 total); + +protected: + JobListQueue download_queue; + BaseInstance *m_inst; +}; + + diff --git a/logic/CMakeLists.txt b/logic/CMakeLists.txt new file mode 100644 index 00000000..b1eacced --- /dev/null +++ b/logic/CMakeLists.txt @@ -0,0 +1,24 @@ +project(libMultiMC) + +set(CMAKE_AUTOMOC ON) + +# Find Qt +find_package(Qt5Core REQUIRED) +find_package(Qt5Network REQUIRED) +find_package(Qt5Xml REQUIRED) + +# Include Qt headers. +include_directories(${Qt5Base_INCLUDE_DIRS}) +include_directories(${Qt5Network_INCLUDE_DIRS}) + +# Include utility library. +include_directories(${CMAKE_SOURCE_DIR}/libutil/include) + +# Include settings library. +include_directories(${CMAKE_SOURCE_DIR}/libsettings/include) + +SET(LIBINST_HEADERS + +) + + diff --git a/logic/IconListModel.cpp b/logic/IconListModel.cpp new file mode 100644 index 00000000..2d2fb6cf --- /dev/null +++ b/logic/IconListModel.cpp @@ -0,0 +1,163 @@ +#include "IconListModel.h" +#include <pathutils.h> +#include <QMap> +#include <QEventLoop> +#include <QDir> + +#define MAX_SIZE 1024 +IconList* IconList::m_Instance = 0; +QMutex IconList::mutex; + +struct entry +{ + QString key; + QString name; + QIcon icon; + bool is_builtin; +}; + +class Private : public QObject +{ + Q_OBJECT +public: + QMap<QString, int> index; + QVector<entry> icons; + Private() + { + } +}; + + +IconList::IconList() : QAbstractListModel(), d(new Private()) +{ + QDir instance_icons(":/icons/instances/"); + auto file_info_list = instance_icons.entryInfoList(QDir::Files, QDir::Name); + for(auto file_info: file_info_list) + { + QString key = file_info.baseName(); + addIcon(key, key, file_info.absoluteFilePath(), true); + } + + // FIXME: get from settings + ensurePathExists("icons/"); + QDir user_icons("icons/"); + file_info_list = user_icons.entryInfoList(QDir::Files, QDir::Name); + for(auto file_info: file_info_list) + { + QString filename = file_info.absoluteFilePath(); + QString key = file_info.baseName(); + addIcon(key, key, filename); + } +} + +IconList::~IconList() +{ + delete d; + d = nullptr; +} + +QVariant IconList::data ( const QModelIndex& index, int role ) const +{ + if(!index.isValid()) + return QVariant(); + + int row = index.row(); + + if(row < 0 || row >= d->icons.size()) + return QVariant(); + + switch(role) + { + case Qt::DecorationRole: + return d->icons[row].icon; + case Qt::DisplayRole: + return d->icons[row].name; + case Qt::UserRole: + return d->icons[row].key; + default: + return QVariant(); + } +} + +int IconList::rowCount ( const QModelIndex& parent ) const +{ + return d->icons.size(); +} + +bool IconList::addIcon ( QString key, QString name, QString path, bool is_builtin ) +{ + auto iter = d->index.find(key); + if(iter != d->index.end()) + { + if(d->icons[*iter].is_builtin) return false; + + QIcon icon(path); + if(icon.isNull()) return false; + + // replace the icon + d->icons[*iter] = {key, name, icon, is_builtin}; + return true; + } + else + { + QIcon icon(path); + if(icon.isNull()) return false; + + // add a new icon + d->icons.push_back({key, name, icon, is_builtin}); + d->index[key] = d->icons.size() - 1; + return true; + } +} + + +QIcon IconList::getIcon ( QString key ) +{ + int icon_index = getIconIndex(key); + + if(icon_index != -1) + return d->icons[icon_index].icon; + + // Fallback for icons that don't exist. + icon_index = getIconIndex("infinity"); + + if(icon_index != -1) + return d->icons[icon_index].icon; + return QIcon(); +} + +int IconList::getIconIndex ( QString key ) +{ + if(key == "default") + key = "infinity"; + + auto iter = d->index.find(key); + if(iter != d->index.end()) + return *iter; + + + return -1; +} + + +void IconList::drop() +{ + mutex.lock(); + delete m_Instance; + m_Instance = 0; + mutex.unlock(); +} + +IconList* IconList::instance() +{ + if ( !m_Instance ) + { + mutex.lock(); + if ( !m_Instance ) + m_Instance = new IconList; + mutex.unlock(); + } + return m_Instance; +} + +#include "IconListModel.moc"
\ No newline at end of file diff --git a/logic/IconListModel.h b/logic/IconListModel.h new file mode 100644 index 00000000..31b05e64 --- /dev/null +++ b/logic/IconListModel.h @@ -0,0 +1,33 @@ +#pragma once + +#include <QMutex> +#include <QAbstractListModel> +#include <QtGui/QIcon> + +class Private; + +class IconList : public QAbstractListModel +{ +public: + static IconList* instance(); + static void drop(); + QIcon getIcon ( QString key ); + int getIconIndex ( QString key ); + + virtual QVariant data ( const QModelIndex& index, int role = Qt::DisplayRole ) const; + virtual int rowCount ( const QModelIndex& parent = QModelIndex() ) const; + + bool addIcon(QString key, QString name, QString path, bool is_builtin = false); + + +private: + virtual ~IconList(); + IconList(); + // hide copy constructor + IconList ( const IconList & ) = delete; + // hide assign op + IconList& operator= ( const IconList & ) = delete; + static IconList* m_Instance; + static QMutex mutex; + Private* d; +}; diff --git a/logic/InstanceFactory.cpp b/logic/InstanceFactory.cpp new file mode 100644 index 00000000..f3511157 --- /dev/null +++ b/logic/InstanceFactory.cpp @@ -0,0 +1,113 @@ +/* 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 "InstanceFactory.h" + +#include <QDir> +#include <QFileInfo> + +#include "BaseInstance.h" +#include "LegacyInstance.h" +#include "OneSixInstance.h" +#include "NostalgiaInstance.h" +#include "InstanceVersion.h" +#include "MinecraftVersion.h" + +#include "inifile.h" +#include <inisettingsobject.h> +#include <setting.h> + +#include "pathutils.h" + +InstanceFactory InstanceFactory::loader; + +InstanceFactory::InstanceFactory() : + QObject(NULL) +{ + +} + +InstanceFactory::InstLoadError InstanceFactory::loadInstance(BaseInstance *&inst, c |
