feat: add initial support for parseing datapacks
Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
This commit is contained in:
@ -23,6 +23,7 @@
|
||||
|
||||
#include <quazip/quazip.h>
|
||||
#include <quazip/quazipfile.h>
|
||||
#include <quazip/quazipdir.h>
|
||||
|
||||
#include <QCryptographicHash>
|
||||
|
||||
@ -32,58 +33,74 @@ bool process(ResourcePack& pack, ProcessingLevel level)
|
||||
{
|
||||
switch (pack.type()) {
|
||||
case ResourceType::FOLDER:
|
||||
ResourcePackUtils::processFolder(pack, level);
|
||||
return true;
|
||||
return ResourcePackUtils::processFolder(pack, level);
|
||||
case ResourceType::ZIPFILE:
|
||||
ResourcePackUtils::processZIP(pack, level);
|
||||
return true;
|
||||
return ResourcePackUtils::processZIP(pack, level);
|
||||
default:
|
||||
qWarning() << "Invalid type for resource pack parse task!";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void processFolder(ResourcePack& pack, ProcessingLevel level)
|
||||
bool processFolder(ResourcePack& pack, ProcessingLevel level)
|
||||
{
|
||||
Q_ASSERT(pack.type() == ResourceType::FOLDER);
|
||||
|
||||
QFileInfo mcmeta_file_info(FS::PathCombine(pack.fileinfo().filePath(), "pack.mcmeta"));
|
||||
if (mcmeta_file_info.isFile()) {
|
||||
if (mcmeta_file_info.exists() && mcmeta_file_info.isFile()) {
|
||||
QFile mcmeta_file(mcmeta_file_info.filePath());
|
||||
if (!mcmeta_file.open(QIODevice::ReadOnly))
|
||||
return;
|
||||
return false; // can't open mcmeta file
|
||||
|
||||
auto data = mcmeta_file.readAll();
|
||||
|
||||
ResourcePackUtils::processMCMeta(pack, std::move(data));
|
||||
bool mcmeta_result = ResourcePackUtils::processMCMeta(pack, std::move(data));
|
||||
|
||||
mcmeta_file.close();
|
||||
if (!mcmeta_result) {
|
||||
return false; // mcmeta invalid
|
||||
}
|
||||
} else {
|
||||
return false; // mcmeta file isn't a valid file
|
||||
}
|
||||
|
||||
if (level == ProcessingLevel::BasicInfoOnly)
|
||||
return;
|
||||
QFileInfo assets_dir_info(FS::PathCombine(pack.fileinfo().filePath(), "assets"));
|
||||
if (!assets_dir_info.exists() || !assets_dir_info.isDir()) {
|
||||
return false; // assets dir does not exists or isn't valid
|
||||
}
|
||||
|
||||
if (level == ProcessingLevel::BasicInfoOnly) {
|
||||
return true; // only need basic info already checked
|
||||
}
|
||||
|
||||
QFileInfo image_file_info(FS::PathCombine(pack.fileinfo().filePath(), "pack.png"));
|
||||
if (image_file_info.isFile()) {
|
||||
if (image_file_info.exists() && image_file_info.isFile()) {
|
||||
QFile mcmeta_file(image_file_info.filePath());
|
||||
if (!mcmeta_file.open(QIODevice::ReadOnly))
|
||||
return;
|
||||
return false; // can't open pack.png file
|
||||
|
||||
auto data = mcmeta_file.readAll();
|
||||
|
||||
ResourcePackUtils::processPackPNG(pack, std::move(data));
|
||||
bool pack_png_result = ResourcePackUtils::processPackPNG(pack, std::move(data));
|
||||
|
||||
mcmeta_file.close();
|
||||
if (!pack_png_result) {
|
||||
return false; // pack.png invalid
|
||||
}
|
||||
} else {
|
||||
return false; // pack.png does not exists or is not a valid file.
|
||||
}
|
||||
|
||||
return true; // all tests passed
|
||||
}
|
||||
|
||||
void processZIP(ResourcePack& pack, ProcessingLevel level)
|
||||
bool processZIP(ResourcePack& pack, ProcessingLevel level)
|
||||
{
|
||||
Q_ASSERT(pack.type() == ResourceType::ZIPFILE);
|
||||
|
||||
QuaZip zip(pack.fileinfo().filePath());
|
||||
if (!zip.open(QuaZip::mdUnzip))
|
||||
return;
|
||||
return false; // can't open zip file
|
||||
|
||||
QuaZipFile file(&zip);
|
||||
|
||||
@ -91,40 +108,57 @@ void processZIP(ResourcePack& pack, ProcessingLevel level)
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
qCritical() << "Failed to open file in zip.";
|
||||
zip.close();
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
auto data = file.readAll();
|
||||
|
||||
ResourcePackUtils::processMCMeta(pack, std::move(data));
|
||||
bool mcmeta_result = ResourcePackUtils::processMCMeta(pack, std::move(data));
|
||||
|
||||
file.close();
|
||||
if (!mcmeta_result) {
|
||||
return false; // mcmeta invalid
|
||||
}
|
||||
} else {
|
||||
return false; // could not set pack.mcmeta as current file.
|
||||
}
|
||||
|
||||
QuaZipDir zipDir(&zip);
|
||||
if (!zipDir.exists("/assets")) {
|
||||
return false; // assets dir does not exists at zip root
|
||||
}
|
||||
|
||||
if (level == ProcessingLevel::BasicInfoOnly) {
|
||||
zip.close();
|
||||
return;
|
||||
return true; // only need basic info already checked
|
||||
}
|
||||
|
||||
if (zip.setCurrentFile("pack.png")) {
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
qCritical() << "Failed to open file in zip.";
|
||||
zip.close();
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
auto data = file.readAll();
|
||||
|
||||
ResourcePackUtils::processPackPNG(pack, std::move(data));
|
||||
bool pack_png_result = ResourcePackUtils::processPackPNG(pack, std::move(data));
|
||||
|
||||
file.close();
|
||||
if (!pack_png_result) {
|
||||
return false; // pack.png invalid
|
||||
}
|
||||
} else {
|
||||
return false; // could not set pack.mcmeta as current file.
|
||||
}
|
||||
|
||||
zip.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// https://minecraft.fandom.com/wiki/Tutorials/Creating_a_resource_pack#Formatting_pack.mcmeta
|
||||
void processMCMeta(ResourcePack& pack, QByteArray&& raw_data)
|
||||
bool processMCMeta(ResourcePack& pack, QByteArray&& raw_data)
|
||||
{
|
||||
try {
|
||||
auto json_doc = QJsonDocument::fromJson(raw_data);
|
||||
@ -134,17 +168,21 @@ void processMCMeta(ResourcePack& pack, QByteArray&& raw_data)
|
||||
pack.setDescription(Json::ensureString(pack_obj, "description", ""));
|
||||
} catch (Json::JsonException& e) {
|
||||
qWarning() << "JsonException: " << e.what() << e.cause();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void processPackPNG(ResourcePack& pack, QByteArray&& raw_data)
|
||||
bool processPackPNG(ResourcePack& pack, QByteArray&& raw_data)
|
||||
{
|
||||
auto img = QImage::fromData(raw_data);
|
||||
if (!img.isNull()) {
|
||||
pack.setImage(img);
|
||||
} else {
|
||||
qWarning() << "Failed to parse pack.png.";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool validate(QFileInfo file)
|
||||
|
Reference in New Issue
Block a user