Skip to content

Commit

Permalink
Implement requirements for extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
Holt59 committed Aug 9, 2024
1 parent fc5f6b9 commit dcc89b1
Show file tree
Hide file tree
Showing 14 changed files with 372 additions and 125 deletions.
2 changes: 2 additions & 0 deletions include/uibase/exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace MOBase
class QDLLEXPORT Exception : public std::exception
{
public:
Exception(const char* text) : m_Message(text) {}
Exception(const std::string& text) : m_Message(QByteArray::fromStdString(text)) {}
Exception(const QString& text) : m_Message(text.toUtf8()) {}

virtual const char* what() const noexcept override { return m_Message.constData(); }
Expand Down
72 changes: 37 additions & 35 deletions include/uibase/extensions/extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@
#include <QJsonObject>
#include <QTranslator>

#include "dllimport.h"
#include "iplugingame.h"
#include "../dllimport.h"
#include "../iplugingame.h"
#include "../versioning.h"
#include "requirements.h"
#include "theme.h"
#include "translation.h"
#include "versioninfo.h"

namespace MOBase
{
class IExtension;

class InvalidExtensionMetaDataException : public Exception
{
public:
using Exception::Exception;
};

enum class ExtensionType
{
INVALID,
THEME,
TRANSLATION,
PLUGIN,
Expand All @@ -48,10 +53,6 @@ class QDLLEXPORT ExtensionContributor
class QDLLEXPORT ExtensionMetaData
{
public:
// check if that metadata object is valid
//
bool isValid() const;

// retrieve the identifier of the extension
//
const auto& identifier() const { return m_Identifier; }
Expand All @@ -72,18 +73,22 @@ class QDLLEXPORT ExtensionMetaData
//
auto type() const { return m_Type; }

// retrieve the description of the extension.
// retrieve the description of the extension
//
auto description() const { return localized(m_Description); }

// retrieve the icon for the extension (might be an empty icon)
//
const auto& icon() const { return m_Icon; }

// retrieve the version of the extension.
// retrieve the version of the extension
//
const auto& version() const { return m_Version; }

// retrieve the requirements of the extension
//
const auto& requirements() const { return m_Requirements; }

// retrieve the raw JSON metadata, this is mostly useful for specific extension type
// to extract custom parts
//
Expand All @@ -92,18 +97,16 @@ class QDLLEXPORT ExtensionMetaData
// retrieve the content objects of the extension
QJsonObject content() const;

private:
QString localized(QString const& value) const;
protected:
ExtensionMetaData(std::filesystem::path const& path, const QJsonObject& jsonData);

private:
friend class ExtensionFactory;

constexpr static const char* DEFAULT_TRANSLATIONS_FOLDER = "translations";
constexpr static const char* DEFAULT_STYLESHEET_PATH = "stylesheets";

ExtensionType parseType(QString const& value) const;

ExtensionMetaData(std::filesystem::path const& path, const QJsonObject& jsonData);
std::optional<ExtensionType> parseType(QString const& value) const;

private:
QJsonObject m_JsonData;
Expand All @@ -116,10 +119,13 @@ class QDLLEXPORT ExtensionMetaData
ExtensionType m_Type;
QString m_Description;
QIcon m_Icon;
VersionInfo m_Version;
Version m_Version;
std::vector<ExtensionRequirement> m_Requirements;

std::filesystem::path m_TranslationFilesPrefix;
std::filesystem::path m_StyleSheetFilePath;

QString localized(QString const& value) const;
};

class QDLLEXPORT IExtension
Expand All @@ -133,24 +139,19 @@ class QDLLEXPORT IExtension
//
const auto& metadata() const { return m_MetaData; }

// retrieve the requirements of the extension
//
const auto& requirements() const { return m_Requirements; }

public:
virtual ~IExtension() {}
IExtension& operator=(const IExtension&) = delete;

protected:
IExtension(std::filesystem::path path, ExtensionMetaData metadata);
IExtension(std::filesystem::path const& path, ExtensionMetaData&& metadata);

public:
IExtension(const IExtension&) = default;

private:
std::filesystem::path m_Path;
ExtensionMetaData m_MetaData;
std::vector<ExtensionRequirement> m_Requirements;
};

// factory for extensions
Expand All @@ -161,13 +162,14 @@ class QDLLEXPORT ExtensionFactory
// load an extension from the given directory, return a null-pointer if the extension
// could not be load
//
static std::unique_ptr<IExtension> loadExtension(std::filesystem::path directory);
static std::unique_ptr<IExtension>
loadExtension(std::filesystem::path const& directory);

private:
// load an extension from the given directory
//
static std::unique_ptr<IExtension> loadExtension(std::filesystem::path directory,
ExtensionMetaData metadata);
static std::unique_ptr<IExtension>
loadExtension(std::filesystem::path const& directory, ExtensionMetaData&& metadata);
};

// theme extension that provides one or more base themes for MO2
Expand All @@ -180,12 +182,12 @@ class QDLLEXPORT ThemeExtension : public IExtension
const auto& themes() const { return m_Themes; }

private:
ThemeExtension(std::filesystem::path path, ExtensionMetaData metadata,
ThemeExtension(std::filesystem::path const& path, ExtensionMetaData&& metadata,
std::vector<std::shared_ptr<const Theme>> themes);

friend class ExtensionFactory;
static std::unique_ptr<ThemeExtension> loadExtension(std::filesystem::path path,
ExtensionMetaData metadata);
static std::unique_ptr<ThemeExtension>
loadExtension(std::filesystem::path const& path, ExtensionMetaData&& metadata);

static std::shared_ptr<const Theme>
parseTheme(std::filesystem::path const& extensionFolder, const QString& identifier,
Expand All @@ -205,12 +207,12 @@ class QDLLEXPORT TranslationExtension : public IExtension
const auto& translations() const { return m_Translations; }

private:
TranslationExtension(std::filesystem::path path, ExtensionMetaData metadata,
TranslationExtension(std::filesystem::path const& path, ExtensionMetaData&& metadata,
std::vector<std::shared_ptr<const Translation>> translations);

friend class ExtensionFactory;
static std::unique_ptr<TranslationExtension>
loadExtension(std::filesystem::path path, ExtensionMetaData metadata);
loadExtension(std::filesystem::path const& path, ExtensionMetaData&& metadata);

static std::shared_ptr<const Translation>
parseTranslation(std::filesystem::path const& extensionFolder,
Expand Down Expand Up @@ -241,14 +243,14 @@ class QDLLEXPORT PluginExtension : public IExtension

protected:
PluginExtension(
std::filesystem::path path, ExtensionMetaData metadata, bool autodetect,
std::filesystem::path const& path, ExtensionMetaData&& metadata, bool autodetect,
std::map<std::string, std::filesystem::path> plugins,
std::vector<std::shared_ptr<const ThemeAddition>> themeAdditions,
std::vector<std::shared_ptr<const TranslationAddition>> translationAdditions);

friend class ExtensionFactory;
static std::unique_ptr<PluginExtension> loadExtension(std::filesystem::path path,
ExtensionMetaData metadata);
static std::unique_ptr<PluginExtension>
loadExtension(std::filesystem::path const& path, ExtensionMetaData&& metadata);

private:
// auto-detect plugins
Expand All @@ -271,8 +273,8 @@ class QDLLEXPORT GameExtension : public PluginExtension
GameExtension(PluginExtension&& pluginExtension);

friend class ExtensionFactory;
static std::unique_ptr<GameExtension> loadExtension(std::filesystem::path path,
ExtensionMetaData metadata);
static std::unique_ptr<GameExtension> loadExtension(std::filesystem::path const& path,
ExtensionMetaData&& metadata);
};

} // namespace MOBase
Expand Down
28 changes: 25 additions & 3 deletions include/uibase/extensions/requirements.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,26 @@
#include <QJsonValue>
#include <QString>

#include "dllimport.h"
#include "../dllimport.h"
#include "../exceptions.h"

namespace MOBase
{
class IOrganizer;
class ExtensionMetaData;

class InvalidRequirementException : public Exception
{
public:
using Exception::Exception;
};

class InvalidRequirementsException : public Exception
{
public:
using Exception::Exception;
};

class ExtensionRequirementImpl;

// extension requirements
Expand Down Expand Up @@ -44,13 +57,22 @@ class QDLLEXPORT ExtensionRequirement
//
bool check(IOrganizer* organizer) const;

// retrieve the type of this extension
//
Type type() const;

// retrieve a textual representation of this requirement, e.g. "ModOrganizer 2.5.4"
// for a requirement that requires MO2 2.5.4
//
QString string() const;

public:
~ExtensionRequirement();

private:
friend class ExtensionRequirementFactory;

ExtensionRequirement(std::shared_ptr<ExtensionRequirementImpl> impl);

std::shared_ptr<ExtensionRequirementImpl> m_Impl;
};

Expand All @@ -62,7 +84,7 @@ class QDLLEXPORT ExtensionRequirementFactory
// extract requirements from the given metadata
//
static std::vector<ExtensionRequirement>
parseRequirements(const ExtensionMetaData& metadata);
parseRequirements(const QJsonValue& json_requirements);

private:
};
Expand Down
2 changes: 1 addition & 1 deletion include/uibase/extensions/theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <QRegularExpression>

#include "dllimport.h"
#include "../dllimport.h"

namespace MOBase
{
Expand Down
2 changes: 1 addition & 1 deletion include/uibase/extensions/translation.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <filesystem>
#include <string>

#include "dllimport.h"
#include "../dllimport.h"

namespace MOBase
{
Expand Down
7 changes: 6 additions & 1 deletion include/uibase/extensions/versionconstraints.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,18 @@ class QDLLEXPORT VersionConstraints
public:
// construct a set of constraints
//
VersionConstraints(std::vector<VersionConstraint> constraints);
VersionConstraints(QString const& repr, std::vector<VersionConstraint> constraints);

// check if the given version matches the set of constraints
//
bool matches(Version const& version) const;

// retrieve a string representation of this set of constraints
//
auto string() const { return m_Repr; }

private:
QString m_Repr;
std::vector<VersionConstraint> m_Constraints;
};

Expand Down
3 changes: 2 additions & 1 deletion include/uibase/versioning.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ class QDLLEXPORT Version

// do not add metadata even if present
//
NoMetadata = 0b1000
NoMetadata = 0b1000,

};
Q_DECLARE_FLAGS(FormatModes, FormatMode);

Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ mo2_target_sources(uibase
errorcodes.cpp
eventfilter.cpp
executableinfo.cpp
extension.cpp
filesystemutilities.cpp
guessedvalue.cpp
json.cpp
Expand Down
Loading

0 comments on commit dcc89b1

Please sign in to comment.