feat: add more resource pack info

Adds pack format id and description to ResourcePack, that'll be parsed
from pack.mcmeta.

Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
flow 2022-08-28 22:29:52 -03:00
parent cda2bfc240
commit 050768c266
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
4 changed files with 87 additions and 1 deletions

View File

@ -322,6 +322,8 @@ set(MINECRAFT_SOURCES
minecraft/mod/Resource.cpp minecraft/mod/Resource.cpp
minecraft/mod/ResourceFolderModel.h minecraft/mod/ResourceFolderModel.h
minecraft/mod/ResourceFolderModel.cpp minecraft/mod/ResourceFolderModel.cpp
minecraft/mod/ResourcePack.h
minecraft/mod/ResourcePack.cpp
minecraft/mod/ResourcePackFolderModel.h minecraft/mod/ResourcePackFolderModel.h
minecraft/mod/ResourcePackFolderModel.cpp minecraft/mod/ResourcePackFolderModel.cpp
minecraft/mod/TexturePackFolderModel.h minecraft/mod/TexturePackFolderModel.h

View File

@ -80,6 +80,7 @@ class Resource : public QObject {
[[nodiscard]] auto shouldResolve() const -> bool { return !m_is_resolving && !m_is_resolved; } [[nodiscard]] auto shouldResolve() const -> bool { return !m_is_resolving && !m_is_resolved; }
[[nodiscard]] auto isResolving() const -> bool { return m_is_resolving; } [[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; } [[nodiscard]] auto resolutionTicket() const -> int { return m_resolution_ticket; }
void setResolving(bool resolving, int resolutionTicket) void setResolving(bool resolving, int resolutionTicket)

View File

@ -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();
}

View File

@ -2,12 +2,47 @@
#include "Resource.h" #include "Resource.h"
#include <QMutex>
class Version;
/* TODO:
*
* Store pack.png
* Store localized descriptions
* */
class ResourcePack : public Resource { class ResourcePack : public Resource {
Q_OBJECT Q_OBJECT
public: public:
using Ptr = shared_qobject_ptr<Resource>; using Ptr = shared_qobject_ptr<Resource>;
ResourcePack(QObject* parent = nullptr) : Resource(parent) {} ResourcePack(QObject* parent = nullptr) : Resource(parent) {}
ResourcePack(QFileInfo file_info) : Resource(file_info) {} 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;
}; };