aboutsummaryrefslogtreecommitdiff
path: root/launcher/modplatform/modrinth/ModrinthPackIndex.cpp
blob: 6c8659dc3243fa963ec11a81248520b932475061 (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
// SPDX-License-Identifier: GPL-3.0-only
/*
 *  PolyMC - Minecraft Launcher
 *
 *  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 "ModrinthPackIndex.h"
#include "ModrinthAPI.h"

#include "Json.h"
#include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h"
#include "net/NetJob.h"

static ModrinthAPI api;

void Modrinth::loadIndexedPack(ModPlatform::IndexedPack& pack, QJsonObject& obj)
{
    pack.addonId = Json::requireString(obj, "project_id");
    pack.provider = ModPlatform::Provider::MODRINTH;
    pack.name = Json::requireString(obj, "title");
    
    QString slug = Json::ensureString(obj, "slug", "");
    if (!slug.isEmpty())
        pack.websiteUrl = "https://modrinth.com/mod/" + Json::ensureString(obj, "slug", "");
    else
        pack.websiteUrl = "";

    pack.description = Json::ensureString(obj, "description", "");

    pack.logoUrl = Json::requireString(obj, "icon_url");
    pack.logoName = pack.addonId.toString();

    ModPlatform::ModpackAuthor modAuthor;
    modAuthor.name = Json::requireString(obj, "author");
    modAuthor.url = api.getAuthorURL(modAuthor.name);
    pack.authors.append(modAuthor);
}

void Modrinth::loadIndexedPackVersions(ModPlatform::IndexedPack& pack,
                                       QJsonArray& arr,
                                       const shared_qobject_ptr<QNetworkAccessManager>& network,
                                       BaseInstance* inst)
{
    QVector<ModPlatform::IndexedVersion> unsortedVersions;
    QString mcVersion = (static_cast<MinecraftInstance*>(inst))->getPackProfile()->getComponentVersion("net.minecraft");

    for (auto versionIter : arr) {
        auto obj = versionIter.toObject();
        ModPlatform::IndexedVersion file;
        file.addonId = Json::requireString(obj, "project_id");
        file.fileId = Json::requireString(obj, "id");
        file.date = Json::requireString(obj, "date_published");
        auto versionArray = Json::requireArray(obj, "game_versions");
        if (versionArray.empty()) { continue; }
        for (auto mcVer : versionArray) {
            file.mcVersion.append(mcVer.toString());
        }
        auto loaders = Json::requireArray(obj, "loaders");
        for (auto loader : loaders) {
            file.loaders.append(loader.toString());
        }
        file.version = Json::requireString(obj, "name");

        auto files = Json::requireArray(obj, "files");
        int i = 0;
        
        // Find correct file (needed in cases where one version may have multiple files)
        // Will default to the last one if there's no primary (though I think Modrinth requires that
        // at least one file is primary, idk)
        // NOTE: files.count() is 1-indexed, so we need to subtract 1 to become 0-indexed
        while (i < files.count() - 1){ 
            auto parent = files[i].toObject();
            auto fileName = Json::requireString(parent, "filename");

            // Grab the primary file, if available
            if(Json::requireBoolean(parent, "primary"))
                break;

            i++;
        }

        auto parent = files[i].toObject();
        if (parent.contains("url")) {
            file.downloadUrl = Json::requireString(parent, "url");
            file.fileName = Json::requireString(parent, "filename");

            unsortedVersions.append(file);
        }
    }
    auto orderSortPredicate = [](const ModPlatform::IndexedVersion& a, const ModPlatform::IndexedVersion& b) -> bool {
        // dates are in RFC 3339 format
        return a.date > b.date;
    };
    std::sort(unsortedVersions.begin(), unsortedVersions.end(), orderSortPredicate);
    pack.versions = unsortedVersions;
    pack.versionsLoaded = true;
}