diff options
| -rw-r--r-- | launcher/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | launcher/minecraft/mod/Resource.h | 1 | ||||
| -rw-r--r-- | launcher/minecraft/mod/ResourcePack.cpp | 48 | ||||
| -rw-r--r-- | launcher/minecraft/mod/ResourcePack.h | 37 | 
4 files changed, 87 insertions, 1 deletions
| diff --git a/launcher/CMakeLists.txt b/launcher/CMakeLists.txt index 490202cf..7c3c2f28 100644 --- a/launcher/CMakeLists.txt +++ b/launcher/CMakeLists.txt @@ -322,6 +322,8 @@ set(MINECRAFT_SOURCES      minecraft/mod/Resource.cpp      minecraft/mod/ResourceFolderModel.h      minecraft/mod/ResourceFolderModel.cpp +    minecraft/mod/ResourcePack.h +    minecraft/mod/ResourcePack.cpp      minecraft/mod/ResourcePackFolderModel.h      minecraft/mod/ResourcePackFolderModel.cpp      minecraft/mod/TexturePackFolderModel.h diff --git a/launcher/minecraft/mod/Resource.h b/launcher/minecraft/mod/Resource.h index cee1f172..96ff9f4b 100644 --- a/launcher/minecraft/mod/Resource.h +++ b/launcher/minecraft/mod/Resource.h @@ -80,6 +80,7 @@ class Resource : public QObject {      [[nodiscard]] auto shouldResolve() const -> bool { return !m_is_resolving && !m_is_resolved; }      [[nodiscard]] auto isResolving() const -> bool { return m_is_resolving; } +    [[nodiscard]] auto isResolved() const -> bool { return m_is_resolved; }      [[nodiscard]] auto resolutionTicket() const -> int { return m_resolution_ticket; }      void setResolving(bool resolving, int resolutionTicket) diff --git a/launcher/minecraft/mod/ResourcePack.cpp b/launcher/minecraft/mod/ResourcePack.cpp new file mode 100644 index 00000000..68d86aed --- /dev/null +++ b/launcher/minecraft/mod/ResourcePack.cpp @@ -0,0 +1,48 @@ +#include "ResourcePack.h" + +#include <QDebug> +#include <QMap> + +#include "Version.h" + +// Values taken from: +// https://minecraft.fandom.com/wiki/Tutorials/Creating_a_resource_pack#Formatting_pack.mcmeta +static const QMap<int, std::pair<Version, Version>> s_pack_format_versions = { +    { 1, { Version("1.6.1"), Version("1.8.9") } }, { 2, { Version("1.9"), Version("1.10.2") } }, +    { 3, { Version("1.11"), Version("1.12.2") } }, { 4, { Version("1.13"), Version("1.14.4") } }, +    { 5, { Version("1.15"), Version("1.16.1") } }, { 6, { Version("1.16.2"), Version("1.16.5") } }, +    { 7, { Version("1.17"), Version("1.17.1") } }, { 8, { Version("1.18"), Version("1.18.2") } }, +    { 9, { Version("1.19"), Version("1.19.2") } }, +}; + +void ResourcePack::setPackFormat(int new_format_id) +{ +    QMutexLocker locker(&m_data_lock); + +    if (!s_pack_format_versions.contains(new_format_id)) { +        qCritical() << "Error: Pack format '%1' is not a recognized resource pack id."; +        return; +    } + +    m_pack_format = new_format_id; +} + +void ResourcePack::setDescription(QString new_description) +{ +    QMutexLocker locker(&m_data_lock); + +    m_description = new_description; +} + +std::pair<Version, Version> ResourcePack::compatibleVersions() const +{ +    if (!s_pack_format_versions.contains(m_pack_format)) { +        // Not having a valid pack format is fine if we didn't yet parse the .mcmeta file, +        // but if we did and we still don't have a valid pack format, that's a bit concerning. +        Q_ASSERT(!isResolved()); + +        return {{}, {}}; +    } + +    return s_pack_format_versions.constFind(m_pack_format).value(); +} diff --git a/launcher/minecraft/mod/ResourcePack.h b/launcher/minecraft/mod/ResourcePack.h index c2cc8690..ab84ad37 100644 --- a/launcher/minecraft/mod/ResourcePack.h +++ b/launcher/minecraft/mod/ResourcePack.h @@ -2,12 +2,47 @@  #include "Resource.h" +#include <QMutex> + +class Version; + +/* TODO: + * + * Store pack.png + * Store localized descriptions + * */ +  class ResourcePack : public Resource {      Q_OBJECT -    public: +   public:      using Ptr = shared_qobject_ptr<Resource>;      ResourcePack(QObject* parent = nullptr) : Resource(parent) {}      ResourcePack(QFileInfo file_info) : Resource(file_info) {} +    /** Gets the numerical ID of the pack format. */ +    [[nodiscard]] int packFormat() const { return m_pack_format; } +    /** Gets, respectively, the lower and upper versions supported by the set pack format. */ +    [[nodiscard]] std::pair<Version, Version> compatibleVersions() const; + +    /** Gets the description of the resource pack. */ +    [[nodiscard]] QString description() const { return m_description; } + +    /** Thread-safe. */ +    void setPackFormat(int new_format_id); + +    /** Thread-safe. */ +    void setDescription(QString new_description); + +   protected: +    mutable QMutex m_data_lock; + +    /* The 'version' of a resource pack, as defined in the pack.mcmeta file. +     * See https://minecraft.fandom.com/wiki/Tutorials/Creating_a_resource_pack#Formatting_pack.mcmeta +     */ +    int m_pack_format = 0; + +    /** The resource pack's description, as defined in the pack.mcmeta file. +     */ +    QString m_description;  }; | 
