summaryrefslogtreecommitdiff
path: root/meshmc/launcher/modplatform/flame/FlamePackIndex.cpp
blob: 6808d84f9126af8576dcee47e28cd209218e174e (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
/* SPDX-FileCopyrightText: 2026 Project Tick
 * SPDX-FileContributor: Project Tick
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 *   MeshMC - A Custom Launcher for Minecraft
 *   Copyright (C) 2026 Project Tick
 *
 *   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, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   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 "FlamePackIndex.h"

#include "Json.h"

void Flame::loadIndexedPack(Flame::IndexedPack& pack, QJsonObject& obj)
{
	pack.addonId = Json::requireInteger(obj, "id");
	pack.name = Json::requireString(obj, "name");
	pack.description = Json::ensureString(obj, "summary", "");

	// API v1: links.websiteUrl, API v2: websiteUrl at root
	if (obj.contains("links") && obj.value("links").isObject()) {
		pack.websiteUrl =
			Json::ensureString(obj.value("links").toObject(), "websiteUrl", "");
	} else {
		pack.websiteUrl = Json::ensureString(obj, "websiteUrl", "");
	}

	// API v1: logo is a single object, API v2: attachments is an array
	bool thumbnailFound = false;
	if (obj.contains("logo") && obj.value("logo").isObject()) {
		auto logoObj = obj.value("logo").toObject();
		pack.logoName = Json::ensureString(logoObj, "title", pack.name);
		pack.logoUrl = Json::ensureString(logoObj, "thumbnailUrl", "");
		thumbnailFound = !pack.logoUrl.isEmpty();
	}
	if (!thumbnailFound && obj.contains("attachments")) {
		auto attachments = Json::requireArray(obj, "attachments");
		for (auto attachmentRaw : attachments) {
			auto attachmentObj = Json::requireObject(attachmentRaw);
			bool isDefault = attachmentObj.value("isDefault").toBool(false);
			if (isDefault) {
				thumbnailFound = true;
				pack.logoName = Json::requireString(attachmentObj, "title");
				pack.logoUrl =
					Json::requireString(attachmentObj, "thumbnailUrl");
				break;
			}
		}
	}
	if (!thumbnailFound) {
		pack.logoName = pack.name;
		pack.logoUrl = "";
	}

	auto authors = Json::requireArray(obj, "authors");
	for (auto authorIter : authors) {
		auto author = Json::requireObject(authorIter);
		Flame::ModpackAuthor packAuthor;
		packAuthor.name = Json::requireString(author, "name");
		packAuthor.url = Json::ensureString(author, "url", "");
		pack.authors.append(packAuthor);
	}

	// API v1: mainFileId, API v2: defaultFileId
	int defaultFileId = 0;
	if (obj.contains("mainFileId")) {
		defaultFileId = Json::requireInteger(obj, "mainFileId");
	} else {
		defaultFileId = Json::requireInteger(obj, "defaultFileId");
	}

	bool found = false;
	// check if there are some files before adding the pack
	auto files = Json::ensureArray(obj, "latestFiles");
	for (auto fileIter : files) {
		auto file = Json::requireObject(fileIter);
		int id = Json::requireInteger(file, "id");

		// NOTE: for now, ignore everything that's not the default...
		if (id != defaultFileId) {
			continue;
		}

		// API v1: gameVersions, API v2: gameVersion
		QJsonArray versionArray;
		if (file.contains("gameVersions")) {
			versionArray = Json::requireArray(file, "gameVersions");
		} else {
			versionArray = Json::requireArray(file, "gameVersion");
		}
		if (versionArray.size() < 1) {
			continue;
		}

		found = true;
		break;
	}
	if (!found) {
		throw JSONValidationError(
			QString("Pack with no good file, skipping: %1").arg(pack.name));
	}
}

void Flame::loadIndexedPackVersions(Flame::IndexedPack& pack, QJsonArray& arr)
{
	QVector<Flame::IndexedVersion> unsortedVersions;
	for (auto versionIter : arr) {
		auto version = Json::requireObject(versionIter);
		Flame::IndexedVersion file;

		file.addonId = pack.addonId;
		file.fileId = Json::requireInteger(version, "id");
		QJsonArray versionArray;
		if (version.contains("gameVersions")) {
			versionArray = Json::requireArray(version, "gameVersions");
		} else {
			versionArray = Json::requireArray(version, "gameVersion");
		}
		if (versionArray.size() < 1) {
			continue;
		}

		// pick the latest version supported
		file.mcVersion = versionArray[0].toString();
		file.version = Json::requireString(version, "displayName");
		file.downloadUrl = Json::ensureString(version, "downloadUrl", "");
		file.fileName = Json::ensureString(version, "fileName", "");
		unsortedVersions.append(file);
	}

	auto orderSortPredicate = [](const IndexedVersion& a,
								 const IndexedVersion& b) -> bool {
		return a.fileId > b.fileId;
	};
	std::sort(unsortedVersions.begin(), unsortedVersions.end(),
			  orderSortPredicate);
	pack.versions = unsortedVersions;
	pack.versionsLoaded = true;
}