fix: cleanup and suggested changes

Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
This commit is contained in:
Rachel Powers
2022-12-26 14:29:13 -07:00
parent b2082bfde7
commit 3691f3a296
19 changed files with 187 additions and 174 deletions

View File

@ -25,8 +25,8 @@
#include "Json.h"
#include <quazip/quazip.h>
#include <quazip/quazipfile.h>
#include <quazip/quazipdir.h>
#include <quazip/quazipfile.h>
#include <QCryptographicHash>
@ -40,7 +40,7 @@ bool process(DataPack& pack, ProcessingLevel level)
case ResourceType::ZIPFILE:
return DataPackUtils::processZIP(pack, level);
default:
qWarning() << "Invalid type for resource pack parse task!";
qWarning() << "Invalid type for data pack parse task!";
return false;
}
}
@ -49,11 +49,16 @@ bool processFolder(DataPack& pack, ProcessingLevel level)
{
Q_ASSERT(pack.type() == ResourceType::FOLDER);
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
};
QFileInfo mcmeta_file_info(FS::PathCombine(pack.fileinfo().filePath(), "pack.mcmeta"));
if (mcmeta_file_info.exists() && mcmeta_file_info.isFile()) {
QFile mcmeta_file(mcmeta_file_info.filePath());
if (!mcmeta_file.open(QIODevice::ReadOnly))
return false; // can't open mcmeta file
return mcmeta_invalid(); // can't open mcmeta file
auto data = mcmeta_file.readAll();
@ -61,22 +66,22 @@ bool processFolder(DataPack& pack, ProcessingLevel level)
mcmeta_file.close();
if (!mcmeta_result) {
return false; // mcmeta invalid
return mcmeta_invalid(); // mcmeta invalid
}
} else {
return false; // mcmeta file isn't a valid file
return mcmeta_invalid(); // mcmeta file isn't a valid file
}
QFileInfo data_dir_info(FS::PathCombine(pack.fileinfo().filePath(), "data"));
if (!data_dir_info.exists() || !data_dir_info.isDir()) {
return false; // data dir does not exists or isn't valid
return false; // data dir does not exists or isn't valid
}
if (level == ProcessingLevel::BasicInfoOnly) {
return true; // only need basic info already checked
return true; // only need basic info already checked
}
return true; // all tests passed
return true; // all tests passed
}
bool processZIP(DataPack& pack, ProcessingLevel level)
@ -85,15 +90,20 @@ bool processZIP(DataPack& pack, ProcessingLevel level)
QuaZip zip(pack.fileinfo().filePath());
if (!zip.open(QuaZip::mdUnzip))
return false; // can't open zip file
return false; // can't open zip file
QuaZipFile file(&zip);
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
};
if (zip.setCurrentFile("pack.mcmeta")) {
if (!file.open(QIODevice::ReadOnly)) {
qCritical() << "Failed to open file in zip.";
zip.close();
return false;
return mcmeta_invalid();
}
auto data = file.readAll();
@ -102,20 +112,20 @@ bool processZIP(DataPack& pack, ProcessingLevel level)
file.close();
if (!mcmeta_result) {
return false; // mcmeta invalid
return mcmeta_invalid(); // mcmeta invalid
}
} else {
return false; // could not set pack.mcmeta as current file.
return mcmeta_invalid(); // could not set pack.mcmeta as current file.
}
QuaZipDir zipDir(&zip);
if (!zipDir.exists("/data")) {
return false; // data dir does not exists at zip root
return false; // data dir does not exists at zip root
}
if (level == ProcessingLevel::BasicInfoOnly) {
zip.close();
return true; // only need basic info already checked
return true; // only need basic info already checked
}
zip.close();
@ -123,7 +133,7 @@ bool processZIP(DataPack& pack, ProcessingLevel level)
return true;
}
// https://minecraft.fandom.com/wiki/Tutorials/Creating_a_resource_pack#Formatting_pack.mcmeta
// https://minecraft.fandom.com/wiki/Data_pack#pack.mcmeta
bool processMCMeta(DataPack& pack, QByteArray&& raw_data)
{
try {
@ -147,9 +157,7 @@ bool validate(QFileInfo file)
} // namespace DataPackUtils
LocalDataPackParseTask::LocalDataPackParseTask(int token, DataPack& dp)
: Task(nullptr, false), m_token(token), m_resource_pack(dp)
{}
LocalDataPackParseTask::LocalDataPackParseTask(int token, DataPack& dp) : Task(nullptr, false), m_token(token), m_data_pack(dp) {}
bool LocalDataPackParseTask::abort()
{
@ -159,7 +167,7 @@ bool LocalDataPackParseTask::abort()
void LocalDataPackParseTask::executeTask()
{
if (!DataPackUtils::process(m_resource_pack))
if (!DataPackUtils::process(m_data_pack))
return;
if (m_aborted)

View File

@ -59,7 +59,7 @@ class LocalDataPackParseTask : public Task {
private:
int m_token;
DataPack& m_resource_pack;
DataPack& m_data_pack;
bool m_aborted = false;
};

View File

@ -284,7 +284,8 @@ ModDetails ReadLiteModInfo(QByteArray contents)
return details;
}
bool process(Mod& mod, ProcessingLevel level) {
bool process(Mod& mod, ProcessingLevel level)
{
switch (mod.type()) {
case ResourceType::FOLDER:
return processFolder(mod, level);
@ -293,13 +294,13 @@ bool process(Mod& mod, ProcessingLevel level) {
case ResourceType::LITEMOD:
return processLitemod(mod);
default:
qWarning() << "Invalid type for resource pack parse task!";
qWarning() << "Invalid type for mod parse task!";
return false;
}
}
bool processZIP(Mod& mod, ProcessingLevel level) {
bool processZIP(Mod& mod, ProcessingLevel level)
{
ModDetails details;
QuaZip zip(mod.fileinfo().filePath());
@ -316,7 +317,7 @@ bool processZIP(Mod& mod, ProcessingLevel level) {
details = ReadMCModTOML(file.readAll());
file.close();
// to replace ${file.jarVersion} with the actual version, as needed
if (details.version == "${file.jarVersion}") {
if (zip.setCurrentFile("META-INF/MANIFEST.MF")) {
@ -347,7 +348,6 @@ bool processZIP(Mod& mod, ProcessingLevel level) {
}
}
zip.close();
mod.setDetails(details);
@ -403,11 +403,11 @@ bool processZIP(Mod& mod, ProcessingLevel level) {
}
zip.close();
return false; // no valid mod found in archive
return false; // no valid mod found in archive
}
bool processFolder(Mod& mod, ProcessingLevel level) {
bool processFolder(Mod& mod, ProcessingLevel level)
{
ModDetails details;
QFileInfo mcmod_info(FS::PathCombine(mod.fileinfo().filePath(), "mcmod.info"));
@ -424,13 +424,13 @@ bool processFolder(Mod& mod, ProcessingLevel level) {
return true;
}
return false; // no valid mcmod.info file found
return false; // no valid mcmod.info file found
}
bool processLitemod(Mod& mod, ProcessingLevel level) {
bool processLitemod(Mod& mod, ProcessingLevel level)
{
ModDetails details;
QuaZip zip(mod.fileinfo().filePath());
if (!zip.open(QuaZip::mdUnzip))
return false;
@ -451,24 +451,22 @@ bool processLitemod(Mod& mod, ProcessingLevel level) {
}
zip.close();
return false; // no valid litemod.json found in archive
return false; // no valid litemod.json found in archive
}
/** Checks whether a file is valid as a mod or not. */
bool validate(QFileInfo file) {
bool validate(QFileInfo file)
{
Mod mod{ file };
return ModUtils::process(mod, ProcessingLevel::BasicInfoOnly) && mod.valid();
}
} // namespace ModUtils
LocalModParseTask::LocalModParseTask(int token, ResourceType type, const QFileInfo& modFile)
: Task(nullptr, false), m_token(token), m_type(type), m_modFile(modFile), m_result(new Result())
{}
bool LocalModParseTask::abort()
{
m_aborted.store(true);
@ -476,7 +474,7 @@ bool LocalModParseTask::abort()
}
void LocalModParseTask::executeTask()
{
{
Mod mod{ m_modFile };
ModUtils::process(mod, ModUtils::ProcessingLevel::Full);

View File

@ -27,32 +27,29 @@ bool processLitemod(Mod& mod, ProcessingLevel level = ProcessingLevel::Full);
bool validate(QFileInfo file);
} // namespace ModUtils
class LocalModParseTask : public Task
{
class LocalModParseTask : public Task {
Q_OBJECT
public:
public:
struct Result {
ModDetails details;
};
using ResultPtr = std::shared_ptr<Result>;
ResultPtr result() const {
return m_result;
}
ResultPtr result() const { return m_result; }
[[nodiscard]] bool canAbort() const override { return true; }
bool abort() override;
LocalModParseTask(int token, ResourceType type, const QFileInfo & modFile);
LocalModParseTask(int token, ResourceType type, const QFileInfo& modFile);
void executeTask() override;
[[nodiscard]] int token() const { return m_token; }
private:
private:
void processAsZip();
void processAsFolder();
void processAsLitemod();
private:
private:
int m_token;
ResourceType m_type;
QFileInfo m_modFile;

View File

@ -22,8 +22,8 @@
#include "Json.h"
#include <quazip/quazip.h>
#include <quazip/quazipfile.h>
#include <quazip/quazipdir.h>
#include <quazip/quazipfile.h>
#include <QCryptographicHash>
@ -46,11 +46,16 @@ bool processFolder(ResourcePack& pack, ProcessingLevel level)
{
Q_ASSERT(pack.type() == ResourceType::FOLDER);
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
};
QFileInfo mcmeta_file_info(FS::PathCombine(pack.fileinfo().filePath(), "pack.mcmeta"));
if (mcmeta_file_info.exists() && mcmeta_file_info.isFile()) {
QFile mcmeta_file(mcmeta_file_info.filePath());
if (!mcmeta_file.open(QIODevice::ReadOnly))
return false; // can't open mcmeta file
return mcmeta_invalid(); // can't open mcmeta file
auto data = mcmeta_file.readAll();
@ -58,26 +63,31 @@ bool processFolder(ResourcePack& pack, ProcessingLevel level)
mcmeta_file.close();
if (!mcmeta_result) {
return false; // mcmeta invalid
return mcmeta_invalid(); // mcmeta invalid
}
} else {
return false; // mcmeta file isn't a valid file
return mcmeta_invalid(); // mcmeta file isn't a valid file
}
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
return false; // assets dir does not exists or isn't valid
}
if (level == ProcessingLevel::BasicInfoOnly) {
return true; // only need basic info already checked
return true; // only need basic info already checked
}
auto png_invalid = [&pack]() {
qWarning() << "Resource pack at" << pack.fileinfo().filePath() << "does not have a valid pack.png";
return true; // the png is optional
};
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 false; // can't open pack.png file
return png_invalid(); // can't open pack.png file
auto data = pack_png_file.readAll();
@ -85,13 +95,13 @@ bool processFolder(ResourcePack& pack, ProcessingLevel level)
pack_png_file.close();
if (!pack_png_result) {
return false; // pack.png invalid
return png_invalid(); // pack.png invalid
}
} else {
return false; // pack.png does not exists or is not a valid file.
return png_invalid(); // pack.png does not exists or is not a valid file.
}
return true; // all tests passed
return true; // all tests passed
}
bool processZIP(ResourcePack& pack, ProcessingLevel level)
@ -100,15 +110,20 @@ bool processZIP(ResourcePack& pack, ProcessingLevel level)
QuaZip zip(pack.fileinfo().filePath());
if (!zip.open(QuaZip::mdUnzip))
return false; // can't open zip file
return false; // can't open zip file
QuaZipFile file(&zip);
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
};
if (zip.setCurrentFile("pack.mcmeta")) {
if (!file.open(QIODevice::ReadOnly)) {
qCritical() << "Failed to open file in zip.";
zip.close();
return false;
return mcmeta_invalid();
}
auto data = file.readAll();
@ -117,27 +132,32 @@ bool processZIP(ResourcePack& pack, ProcessingLevel level)
file.close();
if (!mcmeta_result) {
return false; // mcmeta invalid
return mcmeta_invalid(); // mcmeta invalid
}
} else {
return false; // could not set pack.mcmeta as current file.
return mcmeta_invalid(); // 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
return false; // assets dir does not exists at zip root
}
if (level == ProcessingLevel::BasicInfoOnly) {
zip.close();
return true; // only need basic info already checked
return true; // only need basic info already checked
}
auto png_invalid = [&pack]() {
qWarning() << "Resource pack at" << pack.fileinfo().filePath() << "does not have a valid pack.png";
return true; // the png is optional
};
if (zip.setCurrentFile("pack.png")) {
if (!file.open(QIODevice::ReadOnly)) {
qCritical() << "Failed to open file in zip.";
zip.close();
return false;
return png_invalid();
}
auto data = file.readAll();
@ -146,10 +166,10 @@ bool processZIP(ResourcePack& pack, ProcessingLevel level)
file.close();
if (!pack_png_result) {
return false; // pack.png invalid
return png_invalid(); // pack.png invalid
}
} else {
return false; // could not set pack.mcmeta as current file.
return png_invalid(); // could not set pack.mcmeta as current file.
}
zip.close();

View File

@ -24,8 +24,8 @@
#include "FileSystem.h"
#include <quazip/quazip.h>
#include <quazip/quazipfile.h>
#include <quazip/quazipdir.h>
#include <quazip/quazipfile.h>
namespace ShaderPackUtils {
@ -45,18 +45,18 @@ bool process(ShaderPack& pack, ProcessingLevel level)
bool processFolder(ShaderPack& pack, ProcessingLevel level)
{
Q_ASSERT(pack.type() == ResourceType::FOLDER);
QFileInfo shaders_dir_info(FS::PathCombine(pack.fileinfo().filePath(), "shaders"));
if (!shaders_dir_info.exists() || !shaders_dir_info.isDir()) {
return false; // assets dir does not exists or isn't valid
return false; // assets dir does not exists or isn't valid
}
pack.setPackFormat(ShaderPackFormat::VALID);
if (level == ProcessingLevel::BasicInfoOnly) {
return true; // only need basic info already checked
return true; // only need basic info already checked
}
return true; // all tests passed
return true; // all tests passed
}
bool processZIP(ShaderPack& pack, ProcessingLevel level)
@ -65,19 +65,19 @@ bool processZIP(ShaderPack& pack, ProcessingLevel level)
QuaZip zip(pack.fileinfo().filePath());
if (!zip.open(QuaZip::mdUnzip))
return false; // can't open zip file
return false; // can't open zip file
QuaZipFile file(&zip);
QuaZipDir zipDir(&zip);
if (!zipDir.exists("/shaders")) {
return false; // assets dir does not exists at zip root
return false; // assets dir does not exists at zip root
}
pack.setPackFormat(ShaderPackFormat::VALID);
if (level == ProcessingLevel::BasicInfoOnly) {
zip.close();
return true; // only need basic info already checked
return true; // only need basic info already checked
}
zip.close();
@ -85,7 +85,6 @@ bool processZIP(ShaderPack& pack, ProcessingLevel level)
return true;
}
bool validate(QFileInfo file)
{
ShaderPack sp{ file };
@ -94,9 +93,7 @@ bool validate(QFileInfo file)
} // namespace ShaderPackUtils
LocalShaderPackParseTask::LocalShaderPackParseTask(int token, ShaderPack& sp)
: Task(nullptr, false), m_token(token), m_shader_pack(sp)
{}
LocalShaderPackParseTask::LocalShaderPackParseTask(int token, ShaderPack& sp) : Task(nullptr, false), m_token(token), m_shader_pack(sp) {}
bool LocalShaderPackParseTask::abort()
{

View File

@ -19,7 +19,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <QDebug>
@ -38,7 +37,7 @@ bool process(ShaderPack& pack, ProcessingLevel level = ProcessingLevel::Full);
bool processZIP(ShaderPack& pack, ProcessingLevel level = ProcessingLevel::Full);
bool processFolder(ShaderPack& pack, ProcessingLevel level = ProcessingLevel::Full);
/** Checks whether a file is valid as a resource pack or not. */
/** Checks whether a file is valid as a shader pack or not. */
bool validate(QFileInfo file);
} // namespace ShaderPackUtils

View File

@ -24,12 +24,12 @@
#include "FileSystem.h"
#include <qdir.h>
#include <qfileinfo.h>
#include <quazip/quazip.h>
#include <quazip/quazipfile.h>
#include <quazip/quazipdir.h>
#include <utility>
#include <quazip/quazipfile.h>
#include <QDir>
#include <QFileInfo>
namespace WorldSaveUtils {
@ -41,15 +41,22 @@ bool process(WorldSave& pack, ProcessingLevel level)
case ResourceType::ZIPFILE:
return WorldSaveUtils::processZIP(pack, level);
default:
qWarning() << "Invalid type for shader pack parse task!";
qWarning() << "Invalid type for world save parse task!";
return false;
}
}
/// @brief checks a folder structure to see if it contains a level.dat
/// @param dir the path to check
/// @param saves used in recursive call if a "saves" dir was found
/// @return std::tuple of (
/// bool <found level.dat>,
/// QString <name of folder containing level.dat>,
/// bool <saves folder found>
/// )
static std::tuple<bool, QString, bool> contains_level_dat(QDir dir, bool saves = false)
{
for(auto const& entry : dir.entryInfoList()) {
for (auto const& entry : dir.entryInfoList()) {
if (!entry.isDir()) {
continue;
}
@ -64,12 +71,11 @@ static std::tuple<bool, QString, bool> contains_level_dat(QDir dir, bool saves =
return std::make_tuple(false, "", saves);
}
bool processFolder(WorldSave& save, ProcessingLevel level)
{
Q_ASSERT(save.type() == ResourceType::FOLDER);
auto [ found, save_dir_name, found_saves_dir ] = contains_level_dat(QDir(save.fileinfo().filePath()));
auto [found, save_dir_name, found_saves_dir] = contains_level_dat(QDir(save.fileinfo().filePath()));
if (!found) {
return false;
@ -84,14 +90,21 @@ bool processFolder(WorldSave& save, ProcessingLevel level)
}
if (level == ProcessingLevel::BasicInfoOnly) {
return true; // only need basic info already checked
return true; // only need basic info already checked
}
// resurved for more intensive processing
return true; // all tests passed
// reserved for more intensive processing
return true; // all tests passed
}
/// @brief checks a folder structure to see if it contains a level.dat
/// @param zip the zip file to check
/// @return std::tuple of (
/// bool <found level.dat>,
/// QString <name of folder containing level.dat>,
/// bool <saves folder found>
/// )
static std::tuple<bool, QString, bool> contains_level_dat(QuaZip& zip)
{
bool saves = false;
@ -100,7 +113,7 @@ static std::tuple<bool, QString, bool> contains_level_dat(QuaZip& zip)
saves = true;
zipDir.cd("/saves");
}
for (auto const& entry : zipDir.entryList()) {
zipDir.cd(entry);
if (zipDir.exists("level.dat")) {
@ -117,14 +130,14 @@ bool processZIP(WorldSave& save, ProcessingLevel level)
QuaZip zip(save.fileinfo().filePath());
if (!zip.open(QuaZip::mdUnzip))
return false; // can't open zip file
return false; // can't open zip file
auto [ found, save_dir_name, found_saves_dir ] = contains_level_dat(zip);
auto [found, save_dir_name, found_saves_dir] = contains_level_dat(zip);
if (save_dir_name.endsWith("/")) {
save_dir_name.chop(1);
}
if (!found) {
return false;
}
@ -139,17 +152,16 @@ bool processZIP(WorldSave& save, ProcessingLevel level)
if (level == ProcessingLevel::BasicInfoOnly) {
zip.close();
return true; // only need basic info already checked
return true; // only need basic info already checked
}
// resurved for more intensive processing
// reserved for more intensive processing
zip.close();
return true;
}
bool validate(QFileInfo file)
{
WorldSave sp{ file };
@ -158,9 +170,7 @@ bool validate(QFileInfo file)
} // namespace WorldSaveUtils
LocalWorldSaveParseTask::LocalWorldSaveParseTask(int token, WorldSave& save)
: Task(nullptr, false), m_token(token), m_save(save)
{}
LocalWorldSaveParseTask::LocalWorldSaveParseTask(int token, WorldSave& save) : Task(nullptr, false), m_token(token), m_save(save) {}
bool LocalWorldSaveParseTask::abort()
{

View File

@ -59,4 +59,4 @@ class LocalWorldSaveParseTask : public Task {
WorldSave& m_save;
bool m_aborted = false;
};
};