summaryrefslogtreecommitdiff
path: root/archived/projt-launcher/launcher/meta/JsonFormat.cpp
blob: 8c2d27102de381a55bdc93d5175921e2e61ee79f (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
220
221
222
223
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: 2026 Project Tick
// SPDX-FileContributor: Project Tick Team
/*
 *  ProjT Launcher - Minecraft Launcher
 *  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, 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, write to the Free Software Foundation,
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "JsonFormat.hpp"

#include <QJsonArray>
#include <QDateTime>

#include "Index.hpp"
#include "Json.h"
#include "Version.hpp"
#include "VersionList.hpp"
#include "minecraft/OneSixVersionFormat.h"

namespace projt::meta
{

	SchemaVersion currentSchema()
	{
		return SchemaVersion::V1;
	}

	SchemaVersion detectSchemaVersion(const QJsonObject& root, bool required)
	{
		if (!root.contains("formatVersion"))
			return required ? SchemaVersion::Unknown : SchemaVersion::V1;

		QJsonValue val = root.value("formatVersion");
		if (!val.isDouble())
			return SchemaVersion::Unknown;

		int ver = val.toInt();
		return (ver == 0 || ver == 1) ? SchemaVersion::V1 : SchemaVersion::Unknown;
	}

	void writeSchemaVersion(QJsonObject& target, SchemaVersion version)
	{
		if (version != SchemaVersion::Unknown)
			target.insert("formatVersion", static_cast<int>(version));
	}

	DependencySet parseDependencies(const QJsonObject& source, const QString& fieldName)
	{
		DependencySet result;

		if (!source.contains(fieldName))
			return result;

		QJsonArray arr = Json::requireArray(source, fieldName);
		for (const QJsonValue& item : arr)
		{
			QJsonObject depObj = Json::requireObject(item);

			ComponentDependency dep;
			dep.uid			  = Json::requireString(depObj, "uid");
			dep.equalsVersion = Json::ensureString(depObj, "equals", QString());
			dep.suggests	  = Json::ensureString(depObj, "suggests", QString());

			result.insert(dep);
		}

		return result;
	}

	void writeDependencies(QJsonObject& target, const DependencySet& deps, const QString& fieldName)
	{
		if (deps.empty())
			return;

		QJsonArray arr;
		for (const auto& dep : deps)
		{
			QJsonObject obj;
			obj.insert("uid", dep.uid);

			if (!dep.equalsVersion.isEmpty())
				obj.insert("equals", dep.equalsVersion);

			if (!dep.suggests.isEmpty())
				obj.insert("suggests", dep.suggests);

			arr.append(obj);
		}

		target.insert(fieldName, arr);
	}

	namespace
	{

		MetaVersionList::Ptr buildVersionListFromPackage(const QJsonObject& pkg)
		{
			QString uid = Json::requireString(pkg, "uid");
			auto list	= std::make_shared<MetaVersionList>(uid);

			list->setDisplayName(Json::ensureString(pkg, "name", QString()));
			list->setExpectedChecksum(Json::ensureString(pkg, "sha256", QString()));

			return list;
		}

		MetaVersion::Ptr buildVersionFromJson(const QString& componentUid, const QJsonObject& obj, bool markStability)
		{
			QString verId = Json::requireString(obj, "version");
			auto ver	  = std::make_shared<MetaVersion>(componentUid, verId);

			QString timeStr = Json::requireString(obj, "releaseTime");
			QDateTime dt	= QDateTime::fromString(timeStr, Qt::ISODate);
			ver->setReleaseTimestamp(dt.toMSecsSinceEpoch() / 1000);

			ver->setReleaseType(Json::ensureString(obj, "type", QString()));
			ver->setStable(Json::ensureBoolean(obj, QStringLiteral("recommended"), false));
			ver->setVolatile(Json::ensureBoolean(obj, QStringLiteral("volatile"), false));

			DependencySet deps		= parseDependencies(obj, "requires");
			DependencySet conflicts = parseDependencies(obj, "conflicts");
			ver->setDependencies(deps, conflicts);

			QString sha = Json::ensureString(obj, "sha256", QString());
			if (!sha.isEmpty())
				ver->setExpectedChecksum(sha);

			if (markStability)
				ver->markAsStableCandidate();

			return ver;
		}

		MetaVersion::Ptr buildFullVersionFromJson(const QJsonObject& obj)
		{
			QString uid = Json::requireString(obj, "uid");
			auto ver	= buildVersionFromJson(uid, obj, false);

			VersionFilePtr data =
				OneSixVersionFormat::versionFileFromJson(QJsonDocument(obj),
														 QString("%1/%2.json").arg(uid, ver->versionId()),
														 obj.contains("order"));
			ver->setDetailedData(data);

			return ver;
		}

		MetaVersionList::Ptr buildVersionListFromPackageJson(const QJsonObject& obj)
		{
			QString uid = Json::requireString(obj, "uid");

			QList<QJsonObject> versionObjects = Json::requireIsArrayOf<QJsonObject>(obj, "versions");
			QList<MetaVersion::Ptr> versions;
			versions.reserve(versionObjects.size());

			for (const QJsonObject& vObj : versionObjects)
				versions.append(buildVersionFromJson(uid, vObj, true));

			auto list = std::make_shared<MetaVersionList>(uid);
			list->setDisplayName(Json::ensureString(obj, "name", QString()));
			list->setVersionEntries(versions);

			return list;
		}

		std::shared_ptr<MetaIndex> buildIndexFromJson(const QJsonObject& obj)
		{
			QList<QJsonObject> packages = Json::requireIsArrayOf<QJsonObject>(obj, "packages");
			QList<MetaVersionList::Ptr> components;
			components.reserve(packages.size());

			for (const QJsonObject& pkg : packages)
				components.append(buildVersionListFromPackage(pkg));

			return std::make_shared<MetaIndex>(components);
		}

	} // anonymous namespace

	void loadIndexFromJson(const QJsonObject& json, MetaIndex* index)
	{
		SchemaVersion schema = detectSchemaVersion(json);

		if (schema == SchemaVersion::V1)
			index->mergeWith(buildIndexFromJson(json));
		else
			throw MetaParseError(QObject::tr("Unsupported metadata schema version"));
	}

	void loadVersionListFromJson(const QJsonObject& json, MetaVersionList* list)
	{
		SchemaVersion schema = detectSchemaVersion(json);

		if (schema == SchemaVersion::V1)
			list->mergeWith(buildVersionListFromPackageJson(json));
		else
			throw MetaParseError(QObject::tr("Unsupported metadata schema version"));
	}

	void loadVersionFromJson(const QJsonObject& json, MetaVersion* version)
	{
		SchemaVersion schema = detectSchemaVersion(json);

		if (schema == SchemaVersion::V1)
			version->updateFrom(buildFullVersionFromJson(json));
		else
			throw MetaParseError(QObject::tr("Unsupported metadata schema version"));
	}

} // namespace projt::meta