2022-12-24 23:52:06 +05:30
|
|
|
// SPDX-FileCopyrightText: 2022 Rachel Powers <508861+Ryex@users.noreply.github.com>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prism Launcher - Minecraft Launcher
|
|
|
|
* Copyright (C) 2022 Rachel Powers <508861+Ryex@users.noreply.github.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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "LocalShaderPackParseTask.h"
|
|
|
|
|
|
|
|
#include "FileSystem.h"
|
|
|
|
|
|
|
|
#include <quazip/quazip.h>
|
|
|
|
#include <quazip/quazipdir.h>
|
2022-12-27 02:59:13 +05:30
|
|
|
#include <quazip/quazipfile.h>
|
2022-12-24 23:52:06 +05:30
|
|
|
|
|
|
|
namespace ShaderPackUtils {
|
|
|
|
|
|
|
|
bool process(ShaderPack& pack, ProcessingLevel level)
|
|
|
|
{
|
|
|
|
switch (pack.type()) {
|
|
|
|
case ResourceType::FOLDER:
|
|
|
|
return ShaderPackUtils::processFolder(pack, level);
|
|
|
|
case ResourceType::ZIPFILE:
|
|
|
|
return ShaderPackUtils::processZIP(pack, level);
|
|
|
|
default:
|
|
|
|
qWarning() << "Invalid type for shader pack parse task!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool processFolder(ShaderPack& pack, ProcessingLevel level)
|
|
|
|
{
|
|
|
|
Q_ASSERT(pack.type() == ResourceType::FOLDER);
|
2022-12-27 02:59:13 +05:30
|
|
|
|
2022-12-24 23:52:06 +05:30
|
|
|
QFileInfo shaders_dir_info(FS::PathCombine(pack.fileinfo().filePath(), "shaders"));
|
|
|
|
if (!shaders_dir_info.exists() || !shaders_dir_info.isDir()) {
|
2022-12-27 02:59:13 +05:30
|
|
|
return false; // assets dir does not exists or isn't valid
|
2022-12-24 23:52:06 +05:30
|
|
|
}
|
|
|
|
pack.setPackFormat(ShaderPackFormat::VALID);
|
|
|
|
|
|
|
|
if (level == ProcessingLevel::BasicInfoOnly) {
|
2022-12-27 02:59:13 +05:30
|
|
|
return true; // only need basic info already checked
|
2022-12-24 23:52:06 +05:30
|
|
|
}
|
2022-12-27 02:59:13 +05:30
|
|
|
|
|
|
|
return true; // all tests passed
|
2022-12-24 23:52:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool processZIP(ShaderPack& pack, ProcessingLevel level)
|
|
|
|
{
|
|
|
|
Q_ASSERT(pack.type() == ResourceType::ZIPFILE);
|
|
|
|
|
|
|
|
QuaZip zip(pack.fileinfo().filePath());
|
|
|
|
if (!zip.open(QuaZip::mdUnzip))
|
2022-12-27 02:59:13 +05:30
|
|
|
return false; // can't open zip file
|
2022-12-24 23:52:06 +05:30
|
|
|
|
|
|
|
QuaZipFile file(&zip);
|
|
|
|
|
|
|
|
QuaZipDir zipDir(&zip);
|
|
|
|
if (!zipDir.exists("/shaders")) {
|
2022-12-27 02:59:13 +05:30
|
|
|
return false; // assets dir does not exists at zip root
|
2022-12-24 23:52:06 +05:30
|
|
|
}
|
|
|
|
pack.setPackFormat(ShaderPackFormat::VALID);
|
|
|
|
|
|
|
|
if (level == ProcessingLevel::BasicInfoOnly) {
|
|
|
|
zip.close();
|
2022-12-27 02:59:13 +05:30
|
|
|
return true; // only need basic info already checked
|
2022-12-24 23:52:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
zip.close();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool validate(QFileInfo file)
|
|
|
|
{
|
|
|
|
ShaderPack sp{ file };
|
|
|
|
return ShaderPackUtils::process(sp, ProcessingLevel::BasicInfoOnly) && sp.valid();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ShaderPackUtils
|
|
|
|
|
2022-12-27 02:59:13 +05:30
|
|
|
LocalShaderPackParseTask::LocalShaderPackParseTask(int token, ShaderPack& sp) : Task(nullptr, false), m_token(token), m_shader_pack(sp) {}
|
2022-12-24 23:52:06 +05:30
|
|
|
|
|
|
|
bool LocalShaderPackParseTask::abort()
|
|
|
|
{
|
|
|
|
m_aborted = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalShaderPackParseTask::executeTask()
|
|
|
|
{
|
|
|
|
if (!ShaderPackUtils::process(m_shader_pack))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_aborted)
|
|
|
|
emitAborted();
|
|
|
|
else
|
|
|
|
emitSucceeded();
|
|
|
|
}
|