aboutsummaryrefslogtreecommitdiff
path: root/launcher/ui/pages/modplatform/ModModel.cpp
blob: 59399c5956f226aa714613e86da4b96bc58d4399 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "ModModel.h"

#include "Json.h"
#include "ModPage.h"
#include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h"

#include <QMessageBox>

namespace ResourceDownload {

ModModel::ModModel(ModPage* parent, ResourceAPI* api) : ResourceModel(parent, api) {}

/******** Make data requests ********/

ResourceAPI::SearchArgs ModModel::createSearchArguments()
{
    auto profile = static_cast<MinecraftInstance&>(m_associated_page->m_base_instance).getPackProfile();
    return { ModPlatform::ResourceType::MOD, m_next_search_offset,     m_search_term,
             getSorts()[currentSort],        profile->getModLoaders(), getMineVersions() };
}
ResourceAPI::SearchCallbacks ModModel::createSearchCallbacks()
{
    return { [this](auto& doc) {
        if (!s_running_models.constFind(this).value())
            return;
        searchRequestFinished(doc);
    } };
}

ResourceAPI::VersionSearchArgs ModModel::createVersionsArguments(QModelIndex& entry)
{
    auto const& pack = m_packs[entry.row()];
    auto profile = static_cast<MinecraftInstance&>(m_associated_page->m_base_instance).getPackProfile();

    return { pack.addonId.toString(), getMineVersions(), profile->getModLoaders() };
}
ResourceAPI::VersionSearchCallbacks ModModel::createVersionsCallbacks(QModelIndex& entry)
{
    auto const& pack = m_packs[entry.row()];

    return { [this, pack, entry](auto& doc, auto addonId) {
        if (!s_running_models.constFind(this).value())
            return;
        versionRequestSucceeded(doc, addonId, entry);
    } };
}

ResourceAPI::ProjectInfoArgs ModModel::createInfoArguments(QModelIndex& entry)
{
    auto& pack = m_packs[entry.row()];
    return { pack };
}
ResourceAPI::ProjectInfoCallbacks ModModel::createInfoCallbacks(QModelIndex& entry)
{
    return { [this, entry](auto& doc, auto& pack) {
        if (!s_running_models.constFind(this).value())
            return;
        infoRequestFinished(doc, pack, entry);
    } };
}

void ModModel::searchWithTerm(const QString& term, const int sort, const bool filter_changed)
{
    if (m_search_term == term && m_search_term.isNull() == term.isNull() && currentSort == sort && !filter_changed) {
        return;
    }

    setSearchTerm(term);
    currentSort = sort;

    refresh();
}

/******** Request callbacks ********/

void ModModel::searchRequestFinished(QJsonDocument& doc)
{
    QList<ModPlatform::IndexedPack> newList;
    auto packs = documentToArray(doc);

    for (auto packRaw : packs) {
        auto packObj = packRaw.toObject();

        ModPlatform::IndexedPack pack;
        try {
            loadIndexedPack(pack, packObj);
            newList.append(pack);
        } catch (const JSONValidationError& e) {
            qWarning() << "Error while loading mod from " << m_associated_page->debugName() << ": " << e.cause();
            continue;
        }
    }

    if (packs.size() < 25) {
        m_search_state = SearchState::Finished;
    } else {
        m_next_search_offset += 25;
        m_search_state = SearchState::CanFetchMore;
    }

    // When you have a Qt build with assertions turned on, proceeding here will abort the application
    if (newList.size() == 0)
        return;

    beginInsertRows(QModelIndex(), m_packs.size(), m_packs.size() + newList.size() - 1);
    m_packs.append(newList);
    endInsertRows();
}

void ModModel::infoRequestFinished(QJsonDocument& doc, ModPlatform::IndexedPack& pack, const QModelIndex& index)
{
    qDebug() << "Loading mod info";

    try {
        auto obj = Json::requireObject(doc);
        loadExtraPackInfo(pack, obj);
    } catch (const JSONValidationError& e) {
        qDebug() << doc;
        qWarning() << "Error while reading " << debugName() << " mod info: " << e.cause();
    }

    // Check if the index is still valid for this mod or not
    if (pack.addonId == data(index, Qt::UserRole).value<ModPlatform::IndexedPack>().addonId) {
        // Cache info :^)
        QVariant new_pack;
        new_pack.setValue(pack);
        if (!setData(index, new_pack, Qt::UserRole)) {
            qWarning() << "Failed to cache mod info!";
        }
    }

    m_associated_page->updateUi();
}

void ModModel::versionRequestSucceeded(QJsonDocument doc, QString addonId, const QModelIndex& index)
{
    auto current = m_associated_page->getCurrentPack();
    if (addonId != current.addonId) {
        return;
    }

    auto arr = doc.isObject() ? Json::ensureArray(doc.object(), "data") : doc.array();

    try {
        loadIndexedPackVersions(current, arr);
    } catch (const JSONValidationError& e) {
        qDebug() << doc;
        qWarning() << "Error while reading " << debugName() << " mod version: " << e.cause();
    }

    // Cache info :^)
    QVariant new_pack;
    new_pack.setValue(current);
    if (!setData(index, new_pack, Qt::UserRole)) {
        qWarning() << "Failed to cache mod versions!";
    }

    m_associated_page->updateVersionList();
}

/******** Helpers ********/

#define MOD_PAGE(x) static_cast<ModPage*>(x)

auto ModModel::getMineVersions() const -> std::optional<std::list<Version>>
{
    auto versions = MOD_PAGE(m_associated_page)->getFilter()->versions;
    if (!versions.empty())
        return versions;
    return {};
}

}  // namespace ResourceDownload