2022-08-29 13:25:39 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-08-29 02:33:44 +01:00
|
|
|
#include "LocalResourcePackParseTask.h"
|
|
|
|
|
|
|
|
#include "FileSystem.h"
|
|
|
|
#include "Json.h"
|
|
|
|
|
|
|
|
#include <quazip/quazip.h>
|
2022-12-10 03:26:05 +00:00
|
|
|
#include <quazip/quazipdir.h>
|
2022-12-26 21:29:13 +00:00
|
|
|
#include <quazip/quazipfile.h>
|
2022-08-29 02:33:44 +01:00
|
|
|
|
2022-08-30 22:06:13 +01:00
|
|
|
#include <QCryptographicHash>
|
|
|
|
|
2022-08-29 13:25:39 +01:00
|
|
|
namespace ResourcePackUtils {
|
2022-08-29 02:33:44 +01:00
|
|
|
|
2022-11-20 14:04:10 +00:00
|
|
|
bool process(ResourcePack& pack, ProcessingLevel level)
|
2022-08-29 02:33:44 +01:00
|
|
|
{
|
2022-08-29 13:25:39 +01:00
|
|
|
switch (pack.type()) {
|
2022-08-29 02:33:44 +01:00
|
|
|
case ResourceType::FOLDER:
|
2022-12-10 03:26:05 +00:00
|
|
|
return ResourcePackUtils::processFolder(pack, level);
|
2022-08-29 02:33:44 +01:00
|
|
|
case ResourceType::ZIPFILE:
|
2022-12-10 03:26:05 +00:00
|
|
|
return ResourcePackUtils::processZIP(pack, level);
|
2022-08-29 02:33:44 +01:00
|
|
|
default:
|
|
|
|
qWarning() << "Invalid type for resource pack parse task!";
|
2022-08-29 13:25:39 +01:00
|
|
|
return false;
|
2022-08-29 02:33:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-10 03:26:05 +00:00
|
|
|
bool processFolder(ResourcePack& pack, ProcessingLevel level)
|
2022-08-29 02:33:44 +01:00
|
|
|
{
|
2022-08-29 13:25:39 +01:00
|
|
|
Q_ASSERT(pack.type() == ResourceType::FOLDER);
|
2022-08-29 02:33:44 +01:00
|
|
|
|
2022-12-26 21:29:13 +00:00
|
|
|
auto mcmeta_invalid = [&pack]() {
|
|
|
|
qWarning() << "Resource pack at" << pack.fileinfo().filePath() << "does not have a valid pack.mcmeta";
|
|
|
|
return false; // the mcmeta is not optional
|
|
|
|
};
|
|
|
|
|
2022-08-29 13:25:39 +01:00
|
|
|
QFileInfo mcmeta_file_info(FS::PathCombine(pack.fileinfo().filePath(), "pack.mcmeta"));
|
2022-12-10 03:26:05 +00:00
|
|
|
if (mcmeta_file_info.exists() && mcmeta_file_info.isFile()) {
|
2022-08-29 02:33:44 +01:00
|
|
|
QFile mcmeta_file(mcmeta_file_info.filePath());
|
|
|
|
if (!mcmeta_file.open(QIODevice::ReadOnly))
|
2022-12-26 21:29:13 +00:00
|
|
|
return mcmeta_invalid(); // can't open mcmeta file
|
2022-08-29 02:33:44 +01:00
|
|
|
|
|
|
|
auto data = mcmeta_file.readAll();
|
|
|
|
|
2022-12-10 03:26:05 +00:00
|
|
|
bool mcmeta_result = ResourcePackUtils::processMCMeta(pack, std::move(data));
|
2022-08-29 13:15:06 +01:00
|
|
|
|
|
|
|
mcmeta_file.close();
|
2022-12-10 03:26:05 +00:00
|
|
|
if (!mcmeta_result) {
|
2022-12-26 21:29:13 +00:00
|
|
|
return mcmeta_invalid(); // mcmeta invalid
|
2022-12-10 03:26:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-12-26 21:29:13 +00:00
|
|
|
return mcmeta_invalid(); // mcmeta file isn't a valid file
|
2022-08-29 13:15:06 +01:00
|
|
|
}
|
|
|
|
|
2022-12-10 03:26:05 +00:00
|
|
|
QFileInfo assets_dir_info(FS::PathCombine(pack.fileinfo().filePath(), "assets"));
|
|
|
|
if (!assets_dir_info.exists() || !assets_dir_info.isDir()) {
|
2022-12-26 21:29:13 +00:00
|
|
|
return false; // assets dir does not exists or isn't valid
|
2022-12-10 03:26:05 +00:00
|
|
|
}
|
2022-11-20 14:04:10 +00:00
|
|
|
|
2022-12-10 03:26:05 +00:00
|
|
|
if (level == ProcessingLevel::BasicInfoOnly) {
|
2022-12-26 21:29:13 +00:00
|
|
|
return true; // only need basic info already checked
|
2022-12-10 03:26:05 +00:00
|
|
|
}
|
2022-12-26 21:29:13 +00:00
|
|
|
|
|
|
|
auto png_invalid = [&pack]() {
|
|
|
|
qWarning() << "Resource pack at" << pack.fileinfo().filePath() << "does not have a valid pack.png";
|
|
|
|
return true; // the png is optional
|
|
|
|
};
|
|
|
|
|
2022-08-29 13:25:39 +01:00
|
|
|
QFileInfo image_file_info(FS::PathCombine(pack.fileinfo().filePath(), "pack.png"));
|
2022-12-10 03:26:05 +00:00
|
|
|
if (image_file_info.exists() && image_file_info.isFile()) {
|
2022-12-24 18:22:06 +00:00
|
|
|
QFile pack_png_file(image_file_info.filePath());
|
|
|
|
if (!pack_png_file.open(QIODevice::ReadOnly))
|
2022-12-26 21:29:13 +00:00
|
|
|
return png_invalid(); // can't open pack.png file
|
2022-08-29 13:15:06 +01:00
|
|
|
|
2022-12-24 18:22:06 +00:00
|
|
|
auto data = pack_png_file.readAll();
|
2022-08-29 13:15:06 +01:00
|
|
|
|
2022-12-10 03:26:05 +00:00
|
|
|
bool pack_png_result = ResourcePackUtils::processPackPNG(pack, std::move(data));
|
2022-08-29 13:15:06 +01:00
|
|
|
|
2022-12-24 18:22:06 +00:00
|
|
|
pack_png_file.close();
|
2022-12-10 03:26:05 +00:00
|
|
|
if (!pack_png_result) {
|
2022-12-26 21:29:13 +00:00
|
|
|
return png_invalid(); // pack.png invalid
|
2022-12-10 03:26:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-12-26 21:29:13 +00:00
|
|
|
return png_invalid(); // pack.png does not exists or is not a valid file.
|
2022-08-29 02:33:44 +01:00
|
|
|
}
|
2022-12-10 03:26:05 +00:00
|
|
|
|
2022-12-26 21:29:13 +00:00
|
|
|
return true; // all tests passed
|
2022-08-29 02:33:44 +01:00
|
|
|
}
|
|
|
|
|
2022-12-10 03:26:05 +00:00
|
|
|
bool processZIP(ResourcePack& pack, ProcessingLevel level)
|
2022-08-29 02:33:44 +01:00
|
|
|
{
|
2022-08-29 13:25:39 +01:00
|
|
|
Q_ASSERT(pack.type() == ResourceType::ZIPFILE);
|
|
|
|
|
|
|
|
QuaZip zip(pack.fileinfo().filePath());
|
2022-08-29 02:33:44 +01:00
|
|
|
if (!zip.open(QuaZip::mdUnzip))
|
2022-12-26 21:29:13 +00:00
|
|
|
return false; // can't open zip file
|
2022-08-29 02:33:44 +01:00
|
|
|
|
|
|
|
QuaZipFile file(&zip);
|
|
|
|
|
2022-12-26 21:29:13 +00:00
|
|
|
auto mcmeta_invalid = [&pack]() {
|
|
|
|
qWarning() << "Resource pack at" << pack.fileinfo().filePath() << "does not have a valid pack.mcmeta";
|
|
|
|
return false; // the mcmeta is not optional
|
|
|
|
};
|
|
|
|
|
2022-08-29 02:33:44 +01:00
|
|
|
if (zip.setCurrentFile("pack.mcmeta")) {
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
qCritical() << "Failed to open file in zip.";
|
|
|
|
zip.close();
|
2022-12-26 21:29:13 +00:00
|
|
|
return mcmeta_invalid();
|
2022-08-29 02:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto data = file.readAll();
|
|
|
|
|
2022-12-10 03:26:05 +00:00
|
|
|
bool mcmeta_result = ResourcePackUtils::processMCMeta(pack, std::move(data));
|
2022-08-29 02:33:44 +01:00
|
|
|
|
|
|
|
file.close();
|
2022-12-10 03:26:05 +00:00
|
|
|
if (!mcmeta_result) {
|
2022-12-26 21:29:13 +00:00
|
|
|
return mcmeta_invalid(); // mcmeta invalid
|
2022-12-10 03:26:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-12-26 21:29:13 +00:00
|
|
|
return mcmeta_invalid(); // could not set pack.mcmeta as current file.
|
2022-12-10 03:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QuaZipDir zipDir(&zip);
|
|
|
|
if (!zipDir.exists("/assets")) {
|
2022-12-26 21:29:13 +00:00
|
|
|
return false; // assets dir does not exists at zip root
|
2022-08-29 02:33:44 +01:00
|
|
|
}
|
2022-08-29 13:15:06 +01:00
|
|
|
|
2022-11-20 14:04:10 +00:00
|
|
|
if (level == ProcessingLevel::BasicInfoOnly) {
|
|
|
|
zip.close();
|
2022-12-26 21:29:13 +00:00
|
|
|
return true; // only need basic info already checked
|
2022-11-20 14:04:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-26 21:29:13 +00:00
|
|
|
auto png_invalid = [&pack]() {
|
|
|
|
qWarning() << "Resource pack at" << pack.fileinfo().filePath() << "does not have a valid pack.png";
|
|
|
|
return true; // the png is optional
|
|
|
|
};
|
|
|
|
|
2022-08-29 13:15:06 +01:00
|
|
|
if (zip.setCurrentFile("pack.png")) {
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
qCritical() << "Failed to open file in zip.";
|
|
|
|
zip.close();
|
2022-12-26 21:29:13 +00:00
|
|
|
return png_invalid();
|
2022-08-29 13:15:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto data = file.readAll();
|
|
|
|
|
2022-12-10 03:26:05 +00:00
|
|
|
bool pack_png_result = ResourcePackUtils::processPackPNG(pack, std::move(data));
|
2022-08-29 13:15:06 +01:00
|
|
|
|
|
|
|
file.close();
|
2023-05-05 07:46:00 +01:00
|
|
|
zip.close();
|
2022-12-10 03:26:05 +00:00
|
|
|
if (!pack_png_result) {
|
2022-12-26 21:29:13 +00:00
|
|
|
return png_invalid(); // pack.png invalid
|
2022-12-10 03:26:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-05-05 07:46:00 +01:00
|
|
|
zip.close();
|
2022-12-26 21:29:13 +00:00
|
|
|
return png_invalid(); // could not set pack.mcmeta as current file.
|
2022-08-29 13:15:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
zip.close();
|
2022-12-10 03:26:05 +00:00
|
|
|
return true;
|
2022-08-29 02:33:44 +01:00
|
|
|
}
|
2022-08-29 13:25:39 +01:00
|
|
|
|
|
|
|
// https://minecraft.fandom.com/wiki/Tutorials/Creating_a_resource_pack#Formatting_pack.mcmeta
|
2022-12-10 03:26:05 +00:00
|
|
|
bool processMCMeta(ResourcePack& pack, QByteArray&& raw_data)
|
2022-08-29 13:25:39 +01:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
auto json_doc = QJsonDocument::fromJson(raw_data);
|
|
|
|
auto pack_obj = Json::requireObject(json_doc.object(), "pack", {});
|
|
|
|
|
|
|
|
pack.setPackFormat(Json::ensureInteger(pack_obj, "pack_format", 0));
|
|
|
|
pack.setDescription(Json::ensureString(pack_obj, "description", ""));
|
|
|
|
} catch (Json::JsonException& e) {
|
|
|
|
qWarning() << "JsonException: " << e.what() << e.cause();
|
2022-12-10 03:26:05 +00:00
|
|
|
return false;
|
2022-08-29 13:25:39 +01:00
|
|
|
}
|
2022-12-10 03:26:05 +00:00
|
|
|
return true;
|
2022-08-29 13:25:39 +01:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:46:00 +01:00
|
|
|
bool processPackPNG(const ResourcePack& pack, QByteArray&& raw_data)
|
2022-08-29 13:25:39 +01:00
|
|
|
{
|
|
|
|
auto img = QImage::fromData(raw_data);
|
|
|
|
if (!img.isNull()) {
|
|
|
|
pack.setImage(img);
|
|
|
|
} else {
|
|
|
|
qWarning() << "Failed to parse pack.png.";
|
2022-12-10 03:26:05 +00:00
|
|
|
return false;
|
2022-08-29 13:25:39 +01:00
|
|
|
}
|
2022-12-10 03:26:05 +00:00
|
|
|
return true;
|
2022-08-29 13:25:39 +01:00
|
|
|
}
|
2022-11-20 14:04:10 +00:00
|
|
|
|
2023-05-05 07:46:00 +01:00
|
|
|
bool processPackPNG(const ResourcePack& pack)
|
2023-08-02 17:35:35 +01:00
|
|
|
{
|
2023-05-05 07:46:00 +01:00
|
|
|
auto png_invalid = [&pack]() {
|
|
|
|
qWarning() << "Resource pack at" << pack.fileinfo().filePath() << "does not have a valid pack.png";
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (pack.type()) {
|
2023-08-02 17:35:35 +01:00
|
|
|
case ResourceType::FOLDER: {
|
2023-05-05 07:46:00 +01:00
|
|
|
QFileInfo image_file_info(FS::PathCombine(pack.fileinfo().filePath(), "pack.png"));
|
|
|
|
if (image_file_info.exists() && image_file_info.isFile()) {
|
|
|
|
QFile pack_png_file(image_file_info.filePath());
|
|
|
|
if (!pack_png_file.open(QIODevice::ReadOnly))
|
|
|
|
return png_invalid(); // can't open pack.png file
|
|
|
|
|
|
|
|
auto data = pack_png_file.readAll();
|
|
|
|
|
|
|
|
bool pack_png_result = ResourcePackUtils::processPackPNG(pack, std::move(data));
|
|
|
|
|
|
|
|
pack_png_file.close();
|
|
|
|
if (!pack_png_result) {
|
|
|
|
return png_invalid(); // pack.png invalid
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return png_invalid(); // pack.png does not exists or is not a valid file.
|
|
|
|
}
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
case ResourceType::ZIPFILE: {
|
2023-05-05 07:46:00 +01:00
|
|
|
Q_ASSERT(pack.type() == ResourceType::ZIPFILE);
|
|
|
|
|
|
|
|
QuaZip zip(pack.fileinfo().filePath());
|
|
|
|
if (!zip.open(QuaZip::mdUnzip))
|
|
|
|
return false; // can't open zip file
|
|
|
|
|
|
|
|
QuaZipFile file(&zip);
|
|
|
|
if (zip.setCurrentFile("pack.png")) {
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
qCritical() << "Failed to open file in zip.";
|
|
|
|
zip.close();
|
|
|
|
return png_invalid();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto data = file.readAll();
|
|
|
|
|
|
|
|
bool pack_png_result = ResourcePackUtils::processPackPNG(pack, std::move(data));
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
if (!pack_png_result) {
|
|
|
|
return png_invalid(); // pack.png invalid
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return png_invalid(); // could not set pack.mcmeta as current file.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
qWarning() << "Invalid type for resource pack parse task!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 14:04:10 +00:00
|
|
|
bool validate(QFileInfo file)
|
|
|
|
{
|
|
|
|
ResourcePack rp{ file };
|
|
|
|
return ResourcePackUtils::process(rp, ProcessingLevel::BasicInfoOnly) && rp.valid();
|
|
|
|
}
|
|
|
|
|
2022-08-29 13:25:39 +01:00
|
|
|
} // namespace ResourcePackUtils
|
|
|
|
|
|
|
|
LocalResourcePackParseTask::LocalResourcePackParseTask(int token, ResourcePack& rp)
|
|
|
|
: Task(nullptr, false), m_token(token), m_resource_pack(rp)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool LocalResourcePackParseTask::abort()
|
|
|
|
{
|
|
|
|
m_aborted = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalResourcePackParseTask::executeTask()
|
|
|
|
{
|
|
|
|
if (!ResourcePackUtils::process(m_resource_pack))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_aborted)
|
|
|
|
emitAborted();
|
|
|
|
else
|
|
|
|
emitSucceeded();
|
|
|
|
}
|