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... --- libinstance/include/instance.h | 342 ++++++++++++++++++++++++++++ libinstance/include/instancelist.h | 59 +++++ libinstance/include/instanceloader.h | 137 +++++++++++ libinstance/include/instancetypeinterface.h | 86 +++++++ libinstance/include/instversion.h | 53 +++++ libinstance/include/instversionlist.h | 45 ++++ libinstance/include/libinstance_config.h | 27 +++ 7 files changed, 749 insertions(+) create mode 100644 libinstance/include/instance.h create mode 100644 libinstance/include/instancelist.h create mode 100644 libinstance/include/instanceloader.h create mode 100644 libinstance/include/instancetypeinterface.h create mode 100644 libinstance/include/instversion.h create mode 100644 libinstance/include/instversionlist.h create mode 100644 libinstance/include/libinstance_config.h (limited to 'libinstance/include') diff --git a/libinstance/include/instance.h b/libinstance/include/instance.h new file mode 100644 index 00000000..7b3c001d --- /dev/null +++ b/libinstance/include/instance.h @@ -0,0 +1,342 @@ +/* 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 "appsettings.h" +#include "inifile.h" + +#include "libinstance_config.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 LIBMMCINST_EXPORT 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() const; + + /*! + * \brief Gets the path to the instance's root directory. + * \return The path to the instance's root directory. + */ + virtual QString rootDir() const; + + /*! + * \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() const; + + + //////// 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/libinstance/include/instancelist.h b/libinstance/include/instancelist.h new file mode 100644 index 00000000..f6be815c --- /dev/null +++ b/libinstance/include/instancelist.h @@ -0,0 +1,59 @@ +/* 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 "siglist.h" + +#include "libinstance_config.h" + +class Instance; + +class LIBMMCINST_EXPORT 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/libinstance/include/instanceloader.h b/libinstance/include/instanceloader.h new file mode 100644 index 00000000..39696639 --- /dev/null +++ b/libinstance/include/instanceloader.h @@ -0,0 +1,137 @@ +/* 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 + +#include "libinstance_config.h" + +class InstanceTypeInterface; +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 LIBMMCINST_EXPORT InstanceLoader : public QObject +{ + Q_OBJECT +public: + /*! + * \brief Gets a reference to the instance loader. + */ + static InstanceLoader &get() { return 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. + * + * \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(InstanceTypeInterface *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 InstanceTypeInterface *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 InstanceTypeInterface *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 InstanceTypeInterface *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; + + static InstanceLoader loader; +}; + +#endif // INSTANCELOADER_H diff --git a/libinstance/include/instancetypeinterface.h b/libinstance/include/instancetypeinterface.h new file mode 100644 index 00000000..a061b9d2 --- /dev/null +++ b/libinstance/include/instancetypeinterface.h @@ -0,0 +1,86 @@ +/* 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" + +//! The InstanceTypeInterface's interface ID. +#define InstanceTypeInterface_IID "net.forkk.MultiMC.InstanceTypeInterface/0.1" + +/*! + * \brief The InstanceType class is an interface for all instance types. + * InstanceTypes are usually provided by plugins. + * 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 InstanceTypeInterface +{ +public: + 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; +}; + +Q_DECLARE_INTERFACE(InstanceTypeInterface, InstanceTypeInterface_IID) + +#endif // INSTANCETYPE_H diff --git a/libinstance/include/instversion.h b/libinstance/include/instversion.h new file mode 100644 index 00000000..c505c5a3 --- /dev/null +++ b/libinstance/include/instversion.h @@ -0,0 +1,53 @@ +/* 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 + +#include "libinstance_config.h" + +class InstVersionList; + +class LIBMMCINST_EXPORT 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/libinstance/include/instversionlist.h b/libinstance/include/instversionlist.h new file mode 100644 index 00000000..2cd9ed1e --- /dev/null +++ b/libinstance/include/instversionlist.h @@ -0,0 +1,45 @@ +/* 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 + +#include "libinstance_config.h" + +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 LIBMMCINST_EXPORT 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 diff --git a/libinstance/include/libinstance_config.h b/libinstance/include/libinstance_config.h new file mode 100644 index 00000000..2e6dc884 --- /dev/null +++ b/libinstance/include/libinstance_config.h @@ -0,0 +1,27 @@ +/* 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 LIBINSTANCE_CONFIG_H +//#define LIBINSTANCE_CONFIG_H + +#include + +#ifdef LIBMMCINST_LIBRARY +# define LIBMMCINST_EXPORT Q_DECL_EXPORT +#else +# define LIBMMCINST_EXPORT Q_DECL_IMPORT +#endif + +//#endif // LIBINSTANCE_CONFIG_H -- cgit From f3b6eeeac4e1606a288c7f12fec271fbb7f120f6 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 20 Feb 2013 19:45:00 -0600 Subject: Got instance loading working. --- libinstance/include/instanceloader.h | 11 +++++++---- libinstance/include/instancetypeinterface.h | 4 ++-- libinstance/src/instancelist.cpp | 9 ++++++--- libinstance/src/instanceloader.cpp | 6 +++--- plugins/stdinstance/stdinstancetype.cpp | 28 ++++++++++++++++++++++++++-- plugins/stdinstance/stdinstancetype.h | 4 ++-- 6 files changed, 46 insertions(+), 16 deletions(-) (limited to 'libinstance/include') diff --git a/libinstance/include/instanceloader.h b/libinstance/include/instanceloader.h index 39696639..96be36fb 100644 --- a/libinstance/include/instanceloader.h +++ b/libinstance/include/instanceloader.h @@ -51,6 +51,7 @@ public: * - 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. + * - CantCreateDir is returned by createInstance( if the given instance directory can't be created.) */ enum InstTypeError { @@ -62,7 +63,8 @@ public: TypeNotRegistered, InstExists, NotAnInstance, - WrongInstType + WrongInstType, + CantCreateDir }; /*! @@ -83,8 +85,9 @@ public: * \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. + * - CantCreateDir if the given instance directory cannot be created. */ - InstTypeError createInstance(Instance *inst, const InstanceTypeInterface *type, const QString &instDir); + InstTypeError createInstance(Instance *&inst, const InstanceTypeInterface *type, const QString &instDir); /*! * \brief Loads an instance from the given directory. @@ -97,7 +100,7 @@ public: * - 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 InstanceTypeInterface *type, const QString &instDir); + InstTypeError loadInstance(Instance *&inst, const InstanceTypeInterface *type, const QString &instDir); /*! * \brief Loads an instance from the given directory. @@ -108,7 +111,7 @@ public: * - 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); + InstTypeError loadInstance(Instance *&inst, const QString &instDir); /*! * \brief Finds an instance type with the given ID. diff --git a/libinstance/include/instancetypeinterface.h b/libinstance/include/instancetypeinterface.h index a061b9d2..30a12d99 100644 --- a/libinstance/include/instancetypeinterface.h +++ b/libinstance/include/instancetypeinterface.h @@ -67,7 +67,7 @@ protected: * 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; + virtual InstanceLoader::InstTypeError createInstance(Instance *&inst, const QString &instDir) const = 0; /*! * \brief Loads an instance from the given directory. @@ -78,7 +78,7 @@ protected: * 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; + virtual InstanceLoader::InstTypeError loadInstance(Instance *&inst, const QString &instDir) const = 0; }; Q_DECLARE_INTERFACE(InstanceTypeInterface, InstanceTypeInterface_IID) diff --git a/libinstance/src/instancelist.cpp b/libinstance/src/instancelist.cpp index 15f79d05..3b0b668f 100644 --- a/libinstance/src/instancelist.cpp +++ b/libinstance/src/instancelist.cpp @@ -43,9 +43,10 @@ InstanceList::InstListError InstanceList::loadList() QString subDir = iter.next(); if (QFileInfo(PathCombine(subDir, "instance.cfg")).exists()) { - QSharedPointer inst; + Instance *instPtr = NULL; + InstanceLoader::InstTypeError error = InstanceLoader::get(). - loadInstance(inst.data(), subDir); + loadInstance(instPtr, subDir); if (error != InstanceLoader::NoError && error != InstanceLoader::NotAnInstance) @@ -66,13 +67,15 @@ InstanceList::InstListError InstanceList::loadList() } qDebug(errorMsg.toUtf8()); } - else if (!inst.data()) + else if (!instPtr) { qDebug(QString("Error loading instance %1. Instance loader returned null."). arg(QFileInfo(subDir).baseName()).toUtf8()); } else { + QSharedPointer inst(instPtr); + qDebug(QString("Loaded instance %1").arg(inst->name()).toUtf8()); inst->setParent(this); append(QSharedPointer(inst)); diff --git a/libinstance/src/instanceloader.cpp b/libinstance/src/instanceloader.cpp index eff9d56e..620562b6 100644 --- a/libinstance/src/instanceloader.cpp +++ b/libinstance/src/instanceloader.cpp @@ -49,7 +49,7 @@ InstanceLoader::InstTypeError InstanceLoader::registerInstanceType(InstanceTypeI return NoError; } -InstanceLoader::InstTypeError InstanceLoader::createInstance(Instance *inst, +InstanceLoader::InstTypeError InstanceLoader::createInstance(Instance *&inst, const InstanceTypeInterface *type, const QString &instDir) { @@ -61,7 +61,7 @@ InstanceLoader::InstTypeError InstanceLoader::createInstance(Instance *inst, return type->createInstance(inst, instDir); } -InstanceLoader::InstTypeError InstanceLoader::loadInstance(Instance *inst, +InstanceLoader::InstTypeError InstanceLoader::loadInstance(Instance *&inst, const InstanceTypeInterface *type, const QString &instDir) { @@ -72,7 +72,7 @@ InstanceLoader::InstTypeError InstanceLoader::loadInstance(Instance *inst, return type->loadInstance(inst, instDir); } -InstanceLoader::InstTypeError InstanceLoader::loadInstance(Instance *inst, +InstanceLoader::InstTypeError InstanceLoader::loadInstance(Instance *&inst, const QString &instDir) { QFileInfo instConfig(PathCombine(instDir, "instance.cfg")); diff --git a/plugins/stdinstance/stdinstancetype.cpp b/plugins/stdinstance/stdinstancetype.cpp index 9b7aa994..5a3a6649 100644 --- a/plugins/stdinstance/stdinstancetype.cpp +++ b/plugins/stdinstance/stdinstancetype.cpp @@ -15,20 +15,44 @@ #include "stdinstancetype.h" +#include +#include + +#include "stdinstance.h" + StdInstanceType::StdInstanceType(QObject *parent) : QObject(parent) { } -InstanceLoader::InstTypeError StdInstanceType::createInstance(Instance *inst, +InstanceLoader::InstTypeError StdInstanceType::createInstance(Instance *&inst, const QString &instDir) const { + QFileInfo rootDir(instDir); + + if (!rootDir.exists() && !QDir().mkdir(rootDir.path())) + { + return InstanceLoader::CantCreateDir; + } + + StdInstance *stdInst = new StdInstance(instDir); + + // TODO: Verify that the instance is valid. + + inst = stdInst; + return InstanceLoader::NoError; } -InstanceLoader::InstTypeError StdInstanceType::loadInstance(Instance *inst, +InstanceLoader::InstTypeError StdInstanceType::loadInstance(Instance *&inst, const QString &instDir) const { + StdInstance *stdInst = new StdInstance(instDir); + + // TODO: Verify that the instance is valid. + + inst = stdInst; + return InstanceLoader::NoError; } diff --git a/plugins/stdinstance/stdinstancetype.h b/plugins/stdinstance/stdinstancetype.h index 4f659ba7..b8382a97 100644 --- a/plugins/stdinstance/stdinstancetype.h +++ b/plugins/stdinstance/stdinstancetype.h @@ -34,9 +34,9 @@ public: virtual QString description() const { return "A standard Minecraft instance."; } - virtual InstanceLoader::InstTypeError createInstance(Instance *inst, const QString &instDir) const; + virtual InstanceLoader::InstTypeError createInstance(Instance *&inst, const QString &instDir) const; - virtual InstanceLoader::InstTypeError loadInstance(Instance *inst, const QString &instDir) const; + virtual InstanceLoader::InstTypeError loadInstance(Instance *&inst, const QString &instDir) const; }; #endif // STDINSTANCETYPE_H -- cgit From e9ef332ec409209626716da43ab8bb14651723b4 Mon Sep 17 00:00:00 2001 From: Andrew Okin Date: Thu, 21 Feb 2013 12:10:10 -0600 Subject: Fixed some issues with GCC. --- libinstance/include/instancelist.h | 2 +- libinstance/src/instanceloader.cpp | 2 +- libutil/include/siglist.h | 2 ++ libutil/include/siglist_impl.h | 20 ++++++++++---------- 4 files changed, 14 insertions(+), 12 deletions(-) (limited to 'libinstance/include') diff --git a/libinstance/include/instancelist.h b/libinstance/include/instancelist.h index f6be815c..d5b8c196 100644 --- a/libinstance/include/instancelist.h +++ b/libinstance/include/instancelist.h @@ -26,7 +26,7 @@ class Instance; -class LIBMMCINST_EXPORT InstanceList : public QObject, public SigList> +class LIBMMCINST_EXPORT InstanceList : public QObject, public SigList< QSharedPointer > { Q_OBJECT public: diff --git a/libinstance/src/instanceloader.cpp b/libinstance/src/instanceloader.cpp index 620562b6..9d98230f 100644 --- a/libinstance/src/instanceloader.cpp +++ b/libinstance/src/instanceloader.cpp @@ -100,7 +100,7 @@ InstTypeList InstanceLoader::typeList() { InstTypeList typeList; - for (auto iter = m_typeMap.begin(); iter != m_typeMap.end(); iter++) + for (QMap::iterator iter = m_typeMap.begin(); iter != m_typeMap.end(); iter++) { typeList.append(*iter); } diff --git a/libutil/include/siglist.h b/libutil/include/siglist.h index b6432b6e..347cf752 100644 --- a/libutil/include/siglist.h +++ b/libutil/include/siglist.h @@ -26,6 +26,8 @@ template class SigList : public QList { +private: + typedef typename QList::iterator iterator; public: explicit SigList() : QList() {} diff --git a/libutil/include/siglist_impl.h b/libutil/include/siglist_impl.h index 16ddd9b0..cb37ea4b 100644 --- a/libutil/include/siglist_impl.h +++ b/libutil/include/siglist_impl.h @@ -19,7 +19,7 @@ template void SigList::append(const T &value) { QList::append(value); - onItemAdded(value, length() - 1); + onItemAdded(value, QList::length() - 1); } template @@ -32,7 +32,7 @@ void SigList::prepend(const T &value) template void SigList::append(const QList &other) { - int index = length(); + int index = QList::length(); QList::append(other); onItemsAdded(other, index); } @@ -45,7 +45,7 @@ void SigList::clear() } template -void SigList::erase(QList::iterator pos) +void SigList::erase(SigList::iterator pos) { T value = *pos; int index = indexOf(*pos); @@ -54,12 +54,12 @@ void SigList::erase(QList::iterator pos) } template -void SigList::erase(QList::iterator first, QList::iterator last) +void SigList::erase(SigList::iterator first, SigList::iterator last) { QList removedValues; int firstIndex = indexOf(*first); - for (QList::iterator iter = first; iter < last; iter++) + for (SigList::iterator iter = first; iter < last; iter++) { removedValues << *iter; QList::erase(iter); @@ -76,7 +76,7 @@ void SigList::insert(int i, const T &t) } template -void SigList::insert(QList::iterator before, const T &t) +void SigList::insert(SigList::iterator before, const T &t) { QList::insert(before, t); onItemAdded(t, indexOf(t)); @@ -85,7 +85,7 @@ void SigList::insert(QList::iterator before, const T &t) template void SigList::move(int from, int to) { - const T &item = at(from); + const T &item = QList::at(from); QList::move(from, to); onItemMoved(item, from, to); } @@ -120,8 +120,8 @@ void SigList::swap(QList &other) template void SigList::swap(int i, int j) { - const T &item1 = at(i); - const T &item2 = at(j); + const T &item1 = QList::at(i); + const T &item2 = QList::at(j); QList::swap(i, j); onItemMoved(item1, i, j); onItemMoved(item2, j, i); @@ -144,7 +144,7 @@ T SigList::takeFirst() template T SigList::takeLast() { - return takeAt(length() - 1); + return takeAt(QList::length() - 1); } template -- cgit