aboutsummaryrefslogtreecommitdiff
path: root/launcher/ui/pages/modplatform/TexturePackModel.cpp
blob: fa63695140449691ad17941353727863e7e1b25e (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
// SPDX-FileCopyrightText: 2023 flowln <flowlnlnln@gmail.com>
//
// SPDX-License-Identifier: GPL-3.0-only

#include "TexturePackModel.h"

#include "Application.h"

#include "meta/Index.h"
#include "meta/Version.h"

static std::list<Version> s_availableVersions = {};

namespace ResourceDownload {
TexturePackResourceModel::TexturePackResourceModel(BaseInstance const& inst, ResourceAPI* api)
    : ResourcePackResourceModel(inst, api), m_version_list(APPLICATION->metadataIndex()->get("net.minecraft"))
{
    if (!m_version_list->isLoaded()) {
        qDebug() << "Loading version list...";
        auto task = m_version_list->getLoadTask();
        if (!task->isRunning())
            task->start();
    }
}

void waitOnVersionListLoad(Meta::VersionList::Ptr version_list)
{
    QEventLoop load_version_list_loop;

    QTimer time_limit_for_list_load;
    time_limit_for_list_load.setTimerType(Qt::TimerType::CoarseTimer);
    time_limit_for_list_load.setSingleShot(true);
    time_limit_for_list_load.callOnTimeout(&load_version_list_loop, &QEventLoop::quit);
    time_limit_for_list_load.start(4000);

    auto task = version_list->getLoadTask();
    QObject::connect(task.get(), &Task::finished, &load_version_list_loop, &QEventLoop::quit);

    load_version_list_loop.exec();
    if (time_limit_for_list_load.isActive())
        time_limit_for_list_load.stop();
}

ResourceAPI::SearchArgs TexturePackResourceModel::createSearchArguments()
{
    if (s_availableVersions.empty())
        waitOnVersionListLoad(m_version_list);

    auto args = ResourcePackResourceModel::createSearchArguments();

    if (!m_version_list->isLoaded()) {
        qCritical() << "The version list could not be loaded. Falling back to showing all entries.";
        return args;
    }

    if (s_availableVersions.empty()) {
        for (auto&& version : m_version_list->versions()) {
            // FIXME: This duplicates the logic in meta for the 'texturepacks' trait. However, we don't have access to that
            //        information from the index file alone. Also, downloading every version's file isn't a very good idea.
            if (auto ver = version->toComparableVersion(); ver <= maximumTexturePackVersion())
                s_availableVersions.push_back(ver);
        }
    }

    Q_ASSERT(!s_availableVersions.empty());

    args.versions = s_availableVersions;

    return args;
}

ResourceAPI::VersionSearchArgs TexturePackResourceModel::createVersionsArguments(QModelIndex& entry)
{
    auto args = ResourcePackResourceModel::createVersionsArguments(entry);
    if (!m_version_list->isLoaded()) {
        qCritical() << "The version list could not be loaded. Falling back to showing all entries.";
        return args;
    }

    args.mcVersions = s_availableVersions;
    return args;
}

}  // namespace ResourceDownload