refactor: move RP/TP validation to their respective utils
This makes it easier to validate individual resources, and allows the logic to be used in other places in the future, if we need to. Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
parent
d92ae530d7
commit
df0f9259c0
@ -28,14 +28,14 @@
|
|||||||
|
|
||||||
namespace ResourcePackUtils {
|
namespace ResourcePackUtils {
|
||||||
|
|
||||||
bool process(ResourcePack& pack)
|
bool process(ResourcePack& pack, ProcessingLevel level)
|
||||||
{
|
{
|
||||||
switch (pack.type()) {
|
switch (pack.type()) {
|
||||||
case ResourceType::FOLDER:
|
case ResourceType::FOLDER:
|
||||||
ResourcePackUtils::processFolder(pack);
|
ResourcePackUtils::processFolder(pack, level);
|
||||||
return true;
|
return true;
|
||||||
case ResourceType::ZIPFILE:
|
case ResourceType::ZIPFILE:
|
||||||
ResourcePackUtils::processZIP(pack);
|
ResourcePackUtils::processZIP(pack, level);
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
qWarning() << "Invalid type for resource pack parse task!";
|
qWarning() << "Invalid type for resource pack parse task!";
|
||||||
@ -43,7 +43,7 @@ bool process(ResourcePack& pack)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void processFolder(ResourcePack& pack)
|
void processFolder(ResourcePack& pack, ProcessingLevel level)
|
||||||
{
|
{
|
||||||
Q_ASSERT(pack.type() == ResourceType::FOLDER);
|
Q_ASSERT(pack.type() == ResourceType::FOLDER);
|
||||||
|
|
||||||
@ -60,6 +60,9 @@ void processFolder(ResourcePack& pack)
|
|||||||
mcmeta_file.close();
|
mcmeta_file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (level == ProcessingLevel::BasicInfoOnly)
|
||||||
|
return;
|
||||||
|
|
||||||
QFileInfo image_file_info(FS::PathCombine(pack.fileinfo().filePath(), "pack.png"));
|
QFileInfo image_file_info(FS::PathCombine(pack.fileinfo().filePath(), "pack.png"));
|
||||||
if (image_file_info.isFile()) {
|
if (image_file_info.isFile()) {
|
||||||
QFile mcmeta_file(image_file_info.filePath());
|
QFile mcmeta_file(image_file_info.filePath());
|
||||||
@ -74,7 +77,7 @@ void processFolder(ResourcePack& pack)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void processZIP(ResourcePack& pack)
|
void processZIP(ResourcePack& pack, ProcessingLevel level)
|
||||||
{
|
{
|
||||||
Q_ASSERT(pack.type() == ResourceType::ZIPFILE);
|
Q_ASSERT(pack.type() == ResourceType::ZIPFILE);
|
||||||
|
|
||||||
@ -98,6 +101,11 @@ void processZIP(ResourcePack& pack)
|
|||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (level == ProcessingLevel::BasicInfoOnly) {
|
||||||
|
zip.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (zip.setCurrentFile("pack.png")) {
|
if (zip.setCurrentFile("pack.png")) {
|
||||||
if (!file.open(QIODevice::ReadOnly)) {
|
if (!file.open(QIODevice::ReadOnly)) {
|
||||||
qCritical() << "Failed to open file in zip.";
|
qCritical() << "Failed to open file in zip.";
|
||||||
@ -138,6 +146,13 @@ void processPackPNG(ResourcePack& pack, QByteArray&& raw_data)
|
|||||||
qWarning() << "Failed to parse pack.png.";
|
qWarning() << "Failed to parse pack.png.";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool validate(QFileInfo file)
|
||||||
|
{
|
||||||
|
ResourcePack rp{ file };
|
||||||
|
return ResourcePackUtils::process(rp, ProcessingLevel::BasicInfoOnly) && rp.valid();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace ResourcePackUtils
|
} // namespace ResourcePackUtils
|
||||||
|
|
||||||
LocalResourcePackParseTask::LocalResourcePackParseTask(int token, ResourcePack& rp)
|
LocalResourcePackParseTask::LocalResourcePackParseTask(int token, ResourcePack& rp)
|
||||||
|
@ -26,13 +26,19 @@
|
|||||||
#include "tasks/Task.h"
|
#include "tasks/Task.h"
|
||||||
|
|
||||||
namespace ResourcePackUtils {
|
namespace ResourcePackUtils {
|
||||||
bool process(ResourcePack& pack);
|
|
||||||
|
|
||||||
void processZIP(ResourcePack& pack);
|
enum class ProcessingLevel { Full, BasicInfoOnly };
|
||||||
void processFolder(ResourcePack& pack);
|
|
||||||
|
bool process(ResourcePack& pack, ProcessingLevel level = ProcessingLevel::Full);
|
||||||
|
|
||||||
|
void processZIP(ResourcePack& pack, ProcessingLevel level = ProcessingLevel::Full);
|
||||||
|
void processFolder(ResourcePack& pack, ProcessingLevel level = ProcessingLevel::Full);
|
||||||
|
|
||||||
void processMCMeta(ResourcePack& pack, QByteArray&& raw_data);
|
void processMCMeta(ResourcePack& pack, QByteArray&& raw_data);
|
||||||
void processPackPNG(ResourcePack& pack, QByteArray&& raw_data);
|
void processPackPNG(ResourcePack& pack, QByteArray&& raw_data);
|
||||||
|
|
||||||
|
/** Checks whether a file is valid as a resource pack or not. */
|
||||||
|
bool validate(QFileInfo file);
|
||||||
} // namespace ResourcePackUtils
|
} // namespace ResourcePackUtils
|
||||||
|
|
||||||
class LocalResourcePackParseTask : public Task {
|
class LocalResourcePackParseTask : public Task {
|
||||||
|
@ -28,14 +28,14 @@
|
|||||||
|
|
||||||
namespace TexturePackUtils {
|
namespace TexturePackUtils {
|
||||||
|
|
||||||
bool process(TexturePack& pack)
|
bool process(TexturePack& pack, ProcessingLevel level)
|
||||||
{
|
{
|
||||||
switch (pack.type()) {
|
switch (pack.type()) {
|
||||||
case ResourceType::FOLDER:
|
case ResourceType::FOLDER:
|
||||||
TexturePackUtils::processFolder(pack);
|
TexturePackUtils::processFolder(pack, level);
|
||||||
return true;
|
return true;
|
||||||
case ResourceType::ZIPFILE:
|
case ResourceType::ZIPFILE:
|
||||||
TexturePackUtils::processZIP(pack);
|
TexturePackUtils::processZIP(pack, level);
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
qWarning() << "Invalid type for resource pack parse task!";
|
qWarning() << "Invalid type for resource pack parse task!";
|
||||||
@ -43,7 +43,7 @@ bool process(TexturePack& pack)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void processFolder(TexturePack& pack)
|
void processFolder(TexturePack& pack, ProcessingLevel level)
|
||||||
{
|
{
|
||||||
Q_ASSERT(pack.type() == ResourceType::FOLDER);
|
Q_ASSERT(pack.type() == ResourceType::FOLDER);
|
||||||
|
|
||||||
@ -60,6 +60,9 @@ void processFolder(TexturePack& pack)
|
|||||||
mcmeta_file.close();
|
mcmeta_file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (level == ProcessingLevel::BasicInfoOnly)
|
||||||
|
return;
|
||||||
|
|
||||||
QFileInfo image_file_info(FS::PathCombine(pack.fileinfo().filePath(), "pack.png"));
|
QFileInfo image_file_info(FS::PathCombine(pack.fileinfo().filePath(), "pack.png"));
|
||||||
if (image_file_info.isFile()) {
|
if (image_file_info.isFile()) {
|
||||||
QFile mcmeta_file(image_file_info.filePath());
|
QFile mcmeta_file(image_file_info.filePath());
|
||||||
@ -74,7 +77,7 @@ void processFolder(TexturePack& pack)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void processZIP(TexturePack& pack)
|
void processZIP(TexturePack& pack, ProcessingLevel level)
|
||||||
{
|
{
|
||||||
Q_ASSERT(pack.type() == ResourceType::ZIPFILE);
|
Q_ASSERT(pack.type() == ResourceType::ZIPFILE);
|
||||||
|
|
||||||
@ -98,6 +101,11 @@ void processZIP(TexturePack& pack)
|
|||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (level == ProcessingLevel::BasicInfoOnly) {
|
||||||
|
zip.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (zip.setCurrentFile("pack.png")) {
|
if (zip.setCurrentFile("pack.png")) {
|
||||||
if (!file.open(QIODevice::ReadOnly)) {
|
if (!file.open(QIODevice::ReadOnly)) {
|
||||||
qCritical() << "Failed to open file in zip.";
|
qCritical() << "Failed to open file in zip.";
|
||||||
@ -129,6 +137,13 @@ void processPackPNG(TexturePack& pack, QByteArray&& raw_data)
|
|||||||
qWarning() << "Failed to parse pack.png.";
|
qWarning() << "Failed to parse pack.png.";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool validate(QFileInfo file)
|
||||||
|
{
|
||||||
|
TexturePack rp{ file };
|
||||||
|
return TexturePackUtils::process(rp, ProcessingLevel::BasicInfoOnly) && rp.valid();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace TexturePackUtils
|
} // namespace TexturePackUtils
|
||||||
|
|
||||||
LocalTexturePackParseTask::LocalTexturePackParseTask(int token, TexturePack& rp)
|
LocalTexturePackParseTask::LocalTexturePackParseTask(int token, TexturePack& rp)
|
||||||
|
@ -27,13 +27,19 @@
|
|||||||
#include "tasks/Task.h"
|
#include "tasks/Task.h"
|
||||||
|
|
||||||
namespace TexturePackUtils {
|
namespace TexturePackUtils {
|
||||||
bool process(TexturePack& pack);
|
|
||||||
|
|
||||||
void processZIP(TexturePack& pack);
|
enum class ProcessingLevel { Full, BasicInfoOnly };
|
||||||
void processFolder(TexturePack& pack);
|
|
||||||
|
bool process(TexturePack& pack, ProcessingLevel level = ProcessingLevel::Full);
|
||||||
|
|
||||||
|
void processZIP(TexturePack& pack, ProcessingLevel level = ProcessingLevel::Full);
|
||||||
|
void processFolder(TexturePack& pack, ProcessingLevel level = ProcessingLevel::Full);
|
||||||
|
|
||||||
void processPackTXT(TexturePack& pack, QByteArray&& raw_data);
|
void processPackTXT(TexturePack& pack, QByteArray&& raw_data);
|
||||||
void processPackPNG(TexturePack& pack, QByteArray&& raw_data);
|
void processPackPNG(TexturePack& pack, QByteArray&& raw_data);
|
||||||
|
|
||||||
|
/** Checks whether a file is valid as a texture pack or not. */
|
||||||
|
bool validate(QFileInfo file);
|
||||||
} // namespace TexturePackUtils
|
} // namespace TexturePackUtils
|
||||||
|
|
||||||
class LocalTexturePackParseTask : public Task {
|
class LocalTexturePackParseTask : public Task {
|
||||||
|
@ -110,10 +110,8 @@
|
|||||||
#include "ui/dialogs/ImportResourcePackDialog.h"
|
#include "ui/dialogs/ImportResourcePackDialog.h"
|
||||||
#include "ui/themes/ITheme.h"
|
#include "ui/themes/ITheme.h"
|
||||||
|
|
||||||
#include <minecraft/mod/ResourcePack.h>
|
|
||||||
#include <minecraft/mod/ResourcePackFolderModel.h>
|
#include <minecraft/mod/ResourcePackFolderModel.h>
|
||||||
#include <minecraft/mod/tasks/LocalResourcePackParseTask.h>
|
#include <minecraft/mod/tasks/LocalResourcePackParseTask.h>
|
||||||
#include <minecraft/mod/TexturePack.h>
|
|
||||||
#include <minecraft/mod/TexturePackFolderModel.h>
|
#include <minecraft/mod/TexturePackFolderModel.h>
|
||||||
#include <minecraft/mod/tasks/LocalTexturePackParseTask.h>
|
#include <minecraft/mod/tasks/LocalTexturePackParseTask.h>
|
||||||
|
|
||||||
@ -1806,32 +1804,30 @@ void MainWindow::droppedURLs(QList<QUrl> urls)
|
|||||||
for (auto& url : urls) {
|
for (auto& url : urls) {
|
||||||
if (url.isLocalFile()) {
|
if (url.isLocalFile()) {
|
||||||
auto localFileName = url.toLocalFile();
|
auto localFileName = url.toLocalFile();
|
||||||
|
QFileInfo localFileInfo(localFileName);
|
||||||
ResourcePack rp{ QFileInfo(localFileName) };
|
|
||||||
TexturePack tp{ QFileInfo(localFileName) };
|
|
||||||
|
|
||||||
ImportResourcePackDialog dlg(this);
|
ImportResourcePackDialog dlg(this);
|
||||||
|
|
||||||
if (ResourcePackUtils::process(rp) && rp.valid()) {
|
if (ResourcePackUtils::validate(localFileInfo)) {
|
||||||
dlg.exec();
|
dlg.exec();
|
||||||
|
|
||||||
if (dlg.result() == QDialog::Accepted) {
|
if (dlg.result() == QDialog::Accepted) {
|
||||||
qDebug() << "Selected instance to import resource pack into: " << dlg.selectedInstanceKey;
|
qDebug() << "Selected instance to import resource pack into: " << dlg.selectedInstanceKey;
|
||||||
auto instance = APPLICATION->instances()->getInstanceById(dlg.selectedInstanceKey);
|
auto instance = APPLICATION->instances()->getInstanceById(dlg.selectedInstanceKey);
|
||||||
auto instanceButBuffed = std::dynamic_pointer_cast<MinecraftInstance>(instance);
|
auto instanceButBuffed = std::dynamic_pointer_cast<MinecraftInstance>(instance);
|
||||||
instanceButBuffed->resourcePackList()->installResource(localFileName);
|
instanceButBuffed->resourcePackList()->installResource(localFileName);
|
||||||
}
|
}
|
||||||
} else if (TexturePackUtils::process(tp) && tp.valid()) {
|
} else if (TexturePackUtils::validate(localFileInfo)) {
|
||||||
dlg.exec();
|
dlg.exec();
|
||||||
|
|
||||||
if (dlg.result() == QDialog::Accepted) {
|
if (dlg.result() == QDialog::Accepted) {
|
||||||
qDebug() << "Selected instance to import texture pack into: " << dlg.selectedInstanceKey;
|
qDebug() << "Selected instance to import texture pack into: " << dlg.selectedInstanceKey;
|
||||||
auto instance = APPLICATION->instances()->getInstanceById(dlg.selectedInstanceKey);
|
auto instance = APPLICATION->instances()->getInstanceById(dlg.selectedInstanceKey);
|
||||||
auto instanceButBuffed = std::dynamic_pointer_cast<MinecraftInstance>(instance);
|
auto instanceButBuffed = std::dynamic_pointer_cast<MinecraftInstance>(instance);
|
||||||
instanceButBuffed->texturePackList()->installResource(localFileName);
|
instanceButBuffed->texturePackList()->installResource(localFileName);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
addInstance(localFileName);
|
addInstance(localFileName);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
addInstance(url.toString());
|
addInstance(url.toString());
|
||||||
|
Loading…
Reference in New Issue
Block a user