aboutsummaryrefslogtreecommitdiff
path: root/launcher/minecraft/mod/tasks/LocalShaderPackParseTask.cpp
blob: a9949735b0e97566055c02042fca84c139499740 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 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>
#include <quazip/quazipfile.h>

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);

    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
    }
    pack.setPackFormat(ShaderPackFormat::VALID);

    if (level == ProcessingLevel::BasicInfoOnly) {
        return true;  // only need basic info already checked
    }

    return true;  // all tests passed
}

bool processZIP(ShaderPack& pack, ProcessingLevel level)
{
    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);

    QuaZipDir zipDir(&zip);
    if (!zipDir.exists("/shaders")) {
        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
    }

    zip.close();

    return true;
}

bool validate(QFileInfo file)
{
    ShaderPack sp{ file };
    return ShaderPackUtils::process(sp, ProcessingLevel::BasicInfoOnly) && sp.valid();
}

}  // namespace ShaderPackUtils

LocalShaderPackParseTask::LocalShaderPackParseTask(int token, ShaderPack& sp) : Task(nullptr, false), m_token(token), m_shader_pack(sp) {}

bool LocalShaderPackParseTask::abort()
{
    m_aborted = true;
    return true;
}

void LocalShaderPackParseTask::executeTask()
{
    if (!ShaderPackUtils::process(m_shader_pack))
        return;

    if (m_aborted)
        emitAborted();
    else
        emitSucceeded();
}