aboutsummaryrefslogtreecommitdiff
path: root/launcher/modplatform/flame/FlameCheckUpdate.cpp
blob: 12d990296b3e3982ee103e38eb5a887a7198b45a (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "FlameCheckUpdate.h"
#include "FlameAPI.h"
#include "FlameModIndex.h"

#include <MurmurHash2.h>

#include "FileSystem.h"
#include "Json.h"

#include "ModDownloadTask.h"

static FlameAPI api;
static ModPlatform::ProviderCapabilities ProviderCaps;

bool FlameCheckUpdate::abort()
{
    m_was_aborted = true;
    if (m_net_job)
        return m_net_job->abort();
    return true;
}

ModPlatform::IndexedPack getProjectInfo(ModPlatform::IndexedVersion& ver_info)
{
    ModPlatform::IndexedPack pack;

    QEventLoop loop;

    auto get_project_job = new NetJob("Flame::GetProjectJob", APPLICATION->network());

    auto response = new QByteArray();
    auto url = QString("https://api.curseforge.com/v1/mods/%1").arg(ver_info.addonId.toString());
    auto dl = Net::Download::makeByteArray(url, response);
    get_project_job->addNetAction(dl);

    QObject::connect(get_project_job, &NetJob::succeeded, [response, &pack]() {
        QJsonParseError parse_error{};
        QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
        if (parse_error.error != QJsonParseError::NoError) {
            qWarning() << "Error while parsing JSON response from FlameCheckUpdate at " << parse_error.offset
                       << " reason: " << parse_error.errorString();
            qWarning() << *response;
            return;
        }

        try {
            auto doc_obj = Json::requireObject(doc);
            auto data_obj = Json::requireObject(doc_obj, "data");
            FlameMod::loadIndexedPack(pack, data_obj);
        } catch (Json::JsonException& e) {
            qWarning() << e.cause();
            qDebug() << doc;
        }
    });

    QObject::connect(get_project_job, &NetJob::finished, [&loop, get_project_job] {
        get_project_job->deleteLater();
        loop.quit();
    });

    get_project_job->start();
    loop.exec();

    return pack;
}

/* Check for update:
 * - Get latest version available
 * - Compare hash of the latest version with the current hash
 * - If equal, no updates, else, there's updates, so add to the list
 * */
void FlameCheckUpdate::executeTask()
{
    setStatus(tr("Preparing mods for CurseForge..."));
    setProgress(0, 5);

    QHash<int, Mod> mappings;

    // Create all hashes
    std::list<uint> murmur_hashes;

    auto best_hash_type = ProviderCaps.hashType(ModPlatform::Provider::FLAME).first();
    for (auto mod : m_mods) {
        QByteArray jar_data;

        try {
            jar_data = FS::read(mod.fileinfo().absoluteFilePath());
        } catch (FS::FileSystemException& e) {
            qCritical() << QString("Failed to open / read JAR file of %1").arg(mod.name());
            qCritical() << QString("Reason: ") << e.cause();

            failed(e.what());
            return;
        }

        QByteArray jar_data_treated;
        for (char c : jar_data) {
            // CF-specific
            if (!(c == 9 || c == 10 || c == 13 || c == 32))
                jar_data_treated.push_back(c);
        }

        auto murmur_hash = MurmurHash2(jar_data_treated, jar_data_treated.length());
        murmur_hashes.emplace_back(murmur_hash);

        mappings.insert(mod.metadata()->mod_id().toInt(), mod);
    }

    auto* response = new QByteArray();
    auto job = api.matchFingerprints(murmur_hashes, response);

    QEventLoop lock;

    connect(job.get(), &Task::succeeded, this, [this, response, &mappings] {
        QJsonParseError parse_error{};
        QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
        if (parse_error.error != QJsonParseError::NoError) {
            qWarning() << "Error while parsing JSON response from FlameCheckUpdate at " << parse_error.offset
                       << " reason: " << parse_error.errorString();
            qWarning() << *response;

            failed(parse_error.errorString());
            return;
        }

        setStatus(tr("Parsing the first API response from CurseForge..."));
        setProgress(2, 5);

        try {
            auto doc_obj = Json::requireObject(doc);
            auto data_obj = Json::ensureObject(doc_obj, "data");
            auto match_arr = Json::ensureArray(data_obj, "exactMatches");
            for (auto match : match_arr) {
                auto match_obj = Json::ensureObject(match);

                ModPlatform::IndexedVersion current_ver;
                try {
                    auto file_obj = Json::requireObject(match_obj, "file");
                    current_ver = FlameMod::loadIndexedPackVersion(file_obj);
                } catch (Json::JsonException& e) {
                    qCritical() << "Error while parsing Flame indexed version";
                    qCritical() << e.what();
                    failed(tr("An error occured while parsing a CurseForge indexed version!"));
                    return;
                }

                auto mod_iter = mappings.find(current_ver.addonId.toInt());
                if (mod_iter == mappings.end()) {
                    qCritical() << "Failed to remap mod from Flame!";
                    qDebug() << match_obj;
                    continue;
                }

                auto mod = mod_iter.value();

                setStatus(tr("Waiting for the API response from CurseForge for '%1'...").arg(mod.name()));
                setProgress(3, 5);

                auto latest_ver = api.getLatestVersion({ current_ver.addonId.toString(), m_game_versions, m_loaders });

                // Check if we were aborted while getting the latest version
                if (m_was_aborted) {
                    aborted();
                    return;
                }

                setStatus(tr("Parsing the API response from CurseForge for '%1'...").arg(mod.name()));
                setProgress(4, 5);

                if (!latest_ver.addonId.isValid()) {
                    emit checkFailed(
                        mod,
                        tr("No valid version found for this mod. It's probably unavailable for the current game version / mod loader."));
                    continue;
                }

                if (latest_ver.downloadUrl.isEmpty() && latest_ver.fileId != current_ver.fileId) {
                    auto pack = getProjectInfo(latest_ver);
                    auto recover_url = QString("%1/download/%2").arg(pack.websiteUrl, latest_ver.fileId.toString());
                    emit checkFailed(mod, tr("Mod has a new update available, but is opted-out on CurseForge"), recover_url);

                    continue;
                }

                if (!latest_ver.hash.isEmpty() && (current_ver.hash != latest_ver.hash || mod.status() == ModStatus::NotInstalled)) {
                    // Fake pack with the necessary info to pass to the download task :)
                    ModPlatform::IndexedPack pack;
                    pack.name = mod.name();
                    pack.addonId = mod.metadata()->project_id;
                    pack.websiteUrl = mod.homeurl();
                    for (auto& author : mod.authors())
                        pack.authors.append({ author });
                    pack.description = mod.description();
                    pack.provider = ModPlatform::Provider::FLAME;

                    auto download_task = new ModDownloadTask(pack, latest_ver, m_mods_folder);
                    m_updatable.emplace_back(mod.name(), current_ver.hash, current_ver.version, latest_ver.version,
                                             api.getModFileChangelog(latest_ver.addonId.toInt(), latest_ver.fileId.toInt()),
                                             ModPlatform::Provider::FLAME, download_task);
                }
            }

        } catch (Json::JsonException& e) {
            failed(e.cause() + " : " + e.what());
        }
    });

    connect(job.get(), &Task::finished, &lock, &QEventLoop::quit);

    setStatus(tr("Waiting for the first API response from CurseForge..."));
    setProgress(1, 5);

    m_net_job = job.get();
    job->start();

    lock.exec();

    emitSucceeded();
}