Skip to content

Commit

Permalink
fix: cleanup and suggested changes
Browse files Browse the repository at this point in the history
Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
  • Loading branch information
Ryex committed Dec 26, 2022
1 parent b2082bf commit bc0da56
Show file tree
Hide file tree
Showing 19 changed files with 187 additions and 174 deletions.
6 changes: 3 additions & 3 deletions launcher/minecraft/mod/DataPack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
// Values taken from:
// https://minecraft.fandom.com/wiki/Tutorials/Creating_a_data_pack#%22pack_format%22
static const QMap<int, std::pair<Version, Version>> s_pack_format_versions = {
{ 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.1") } }, { 9, { Version("1.18.2"), Version("1.18.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.1") } }, { 9, { Version("1.18.2"), Version("1.18.2") } },
{ 10, { Version("1.19"), Version("1.19.3") } },
};

Expand Down
8 changes: 4 additions & 4 deletions launcher/minecraft/mod/DataPack.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class DataPack : public Resource {
/** 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. */
/** Gets the description of the data pack. */
[[nodiscard]] QString description() const { return m_description; }

/** Thread-safe. */
Expand All @@ -62,12 +62,12 @@ class DataPack : public Resource {
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
/* The 'version' of a data pack, as defined in the pack.mcmeta file.
* See https://minecraft.fandom.com/wiki/Data_pack#pack.mcmeta
*/
int m_pack_format = 0;

/** The resource pack's description, as defined in the pack.mcmeta file.
/** The data pack's description, as defined in the pack.mcmeta file.
*/
QString m_description;
};
2 changes: 1 addition & 1 deletion launcher/minecraft/mod/Mod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,4 @@ void Mod::finishResolvingWithDetails(ModDetails&& details)
bool Mod::valid() const
{
return !m_local_details.mod_id.isEmpty();
}
}
11 changes: 5 additions & 6 deletions launcher/minecraft/mod/ResourcePack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
// 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") } },
// { 11, { Version("22w42a"), Version("22w44a") } }
{ 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") } }, { 11, { Version("22w42a"), Version("22w44a") } },
{ 12, { Version("1.19.3"), Version("1.19.3") } },
};

Expand Down
2 changes: 0 additions & 2 deletions launcher/minecraft/mod/ShaderPack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@

#include "minecraft/mod/tasks/LocalShaderPackParseTask.h"


void ShaderPack::setPackFormat(ShaderPackFormat new_format)
{
QMutexLocker locker(&m_data_lock);


m_pack_format = new_format;
}

Expand Down
24 changes: 10 additions & 14 deletions launcher/minecraft/mod/ShaderPack.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,27 @@
#include "Resource.h"

/* Info:
* Currently For Optifine / Iris shader packs,
* could be expanded to support others should they exsist?
* Currently For Optifine / Iris shader packs,
* could be expanded to support others should they exist?
*
* This class and enum are mostly here as placeholders for validating
* that a shaderpack exsists and is in the right format,
* This class and enum are mostly here as placeholders for validating
* that a shaderpack exists and is in the right format,
* namely that they contain a folder named 'shaders'.
*
* In the technical sense it would be possible to parse files like `shaders/shaders.properties`
* to get information like the availble profiles but this is not all that usefull without more knoledge of the
* shader mod used to be able to change settings
*
* In the technical sense it would be possible to parse files like `shaders/shaders.properties`
* to get information like the available profiles but this is not all that useful without more knowledge of the
* shader mod used to be able to change settings.
*/

#include <QMutex>

enum class ShaderPackFormat {
VALID,
INVALID
};
enum class ShaderPackFormat { VALID, INVALID };

class ShaderPack : public Resource {
Q_OBJECT
public:
using Ptr = shared_qobject_ptr<Resource>;

[[nodiscard]] ShaderPackFormat packFormat() const { return m_pack_format; }

ShaderPack(QObject* parent = nullptr) : Resource(parent) {}
Expand All @@ -62,5 +58,5 @@ class ShaderPack : public Resource {
protected:
mutable QMutex m_data_lock;

ShaderPackFormat m_pack_format = ShaderPackFormat::INVALID;
ShaderPackFormat m_pack_format = ShaderPackFormat::INVALID;
};
6 changes: 2 additions & 4 deletions launcher/minecraft/mod/WorldSave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,17 @@ void WorldSave::setSaveFormat(WorldSaveFormat new_save_format)
{
QMutexLocker locker(&m_data_lock);


m_save_format = new_save_format;
}

void WorldSave::setSaveDirName(QString dir_name)
{
QMutexLocker locker(&m_data_lock);


m_save_dir_name = dir_name;
}

bool WorldSave::valid() const
{
return m_save_format != WorldSaveFormat::INVALID;
}
return m_save_format != WorldSaveFormat::INVALID;
}
14 changes: 4 additions & 10 deletions launcher/minecraft/mod/WorldSave.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@

class Version;

enum class WorldSaveFormat {
SINGLE,
MULTI,
INVALID
};
enum class WorldSaveFormat { SINGLE, MULTI, INVALID };

class WorldSave : public Resource {
Q_OBJECT
Expand All @@ -53,15 +49,13 @@ class WorldSave : public Resource {

bool valid() const override;


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
/** The format in which the save file is in.
* Since saves can be distributed in various slightly different ways, this allows us to treat them separately.
*/
WorldSaveFormat m_save_format = WorldSaveFormat::INVALID;

QString m_save_dir_name;

QString m_save_dir_name;
};
46 changes: 27 additions & 19 deletions launcher/minecraft/mod/tasks/LocalDataPackParseTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand All @@ -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;
}
}
Expand All @@ -49,34 +49,39 @@ 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();

bool mcmeta_result = DataPackUtils::processMCMeta(pack, std::move(data));

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)
Expand All @@ -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();
Expand All @@ -102,28 +112,28 @@ 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();

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 {
Expand All @@ -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()
{
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion launcher/minecraft/mod/tasks/LocalDataPackParseTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class LocalDataPackParseTask : public Task {
private:
int m_token;

DataPack& m_resource_pack;
DataPack& m_data_pack;

bool m_aborted = false;
};

0 comments on commit bc0da56

Please sign in to comment.