summaryrefslogtreecommitdiff
path: root/archived/projt-launcher/launcher/meta/BaseEntity.cpp
blob: 785653a6e48d486494eb52898d76ae3b30a03547 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// 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 "BaseEntity.hpp"

#include <QDir>
#include <QFile>

#include "Application.h"
#include "BuildConfig.h"
#include "Exception.h"
#include "FileSystem.h"
#include "Json.h"
#include "modplatform/helpers/HashUtils.h"
#include "net/ApiDownload.h"
#include "net/ChecksumValidator.h"
#include "net/HttpMetaCache.h"

namespace projt::meta
{

	namespace
	{

		/**
		 * @brief Validator that parses downloaded JSON into the target entity.
		 */
		class JsonParseValidator : public Net::Validator
		{
		  public:
			explicit JsonParseValidator(MetaEntity* entity) : m_entity(entity)
			{}

			bool init(QNetworkRequest&) override
			{
				m_buffer.clear();
				return true;
			}

			bool write(QByteArray& chunk) override
			{
				m_buffer.append(chunk);
				return true;
			}

			bool abort() override
			{
				m_buffer.clear();
				return true;
			}

			bool validate(QNetworkReply&) override
			{
				QString filename = m_entity->cacheFilePath();
				try
				{
					QJsonDocument doc = Json::requireDocument(m_buffer, filename);
					QJsonObject root  = Json::requireObject(doc, filename);
					m_entity->loadFromJson(root);
					return true;
				}
				catch (const Exception& ex)
				{
					qWarning() << "Failed to parse metadata:" << ex.cause();
					return false;
				}
			}

		  private:
			QByteArray m_buffer;
			MetaEntity* m_entity;
		};

	} // anonymous namespace

	QUrl MetaEntity::remoteUrl() const
	{
		auto settings	 = APPLICATION->settings();
		QString override = settings->get("MetaURLOverride").toString();

		QString baseUrl = override.isEmpty() ? BuildConfig.META_URL : override;
		return QUrl(baseUrl).resolved(cacheFilePath());
	}

	Task::Ptr MetaEntity::createLoadTask(Net::Mode mode)
	{
		if (m_activeTask && m_activeTask->isRunning())
			return m_activeTask;

		m_activeTask = Task::Ptr(new EntityLoader(this, mode));
		return m_activeTask;
	}

	// EntityLoader implementation

	EntityLoader::EntityLoader(MetaEntity* target, Net::Mode mode) : m_target(target), m_mode(mode)
	{}

	void EntityLoader::executeTask()
	{
		attemptLocalLoad();
	}

	void EntityLoader::attemptLocalLoad()
	{
		QString cachePath = QDir("meta").absoluteFilePath(m_target->cacheFilePath());

		if (!QFile::exists(cachePath))
		{
			// No local cache, need remote fetch
			if (m_mode == Net::Mode::Offline)
			{
				emitFailed(tr("Metadata not available offline: %1").arg(m_target->cacheFilePath()));
				return;
			}
			initiateRemoteFetch();
			return;
		}

		QString cacheError;

		if (m_mode == Net::Mode::Online)
		{
			// In online mode, always revalidate metadata against the remote cache entry on first load.
			// Relying only on the cached file plus the last known expected checksum can leave us stuck
			// on stale package manifests across launcher restarts.
			if (loadCachedMetadata(cachePath, &cacheError))
			{
				m_target->m_state	   = MetaEntity::State::Cached;
				m_canUseLocalFallback = true;
			}
			else
			{
				qDebug() << "Cache parse failed for" << cachePath << ":" << cacheError;
				FS::deletePath(cachePath);
				m_target->m_state	   = MetaEntity::State::Pending;
				m_canUseLocalFallback = false;
			}
			initiateRemoteFetch();
			return;
		}

		if (loadCachedMetadata(cachePath, &cacheError))
		{
			m_target->m_state = MetaEntity::State::Cached;
			emitSucceeded();
			return;
		}

		qDebug() << "Cache parse failed for" << cachePath << ":" << cacheError;
		FS::deletePath(cachePath);
		m_target->m_state = MetaEntity::State::Pending;

		if (m_mode == Net::Mode::Offline)
		{
			emitFailed(tr("Cached metadata corrupted and offline mode active"));
			return;
		}

		initiateRemoteFetch();
	}

	void EntityLoader::initiateRemoteFetch()
	{
		setStatus(tr("Downloading metadata: %1").arg(m_target->cacheFilePath()));

		m_netTask = NetJob::Ptr(new NetJob(tr("Fetch %1").arg(m_target->cacheFilePath()), APPLICATION->network()));

		auto cacheEntry = APPLICATION->metacache()->resolveEntry("meta", m_target->cacheFilePath());
		cacheEntry->setStale(true);

		auto download = Net::ApiDownload::makeCached(m_target->remoteUrl(), cacheEntry);

		// Add checksum validator if available
		if (!m_target->m_expectedSha256.isEmpty())
		{
			download->addValidator(new Net::ChecksumValidator(QCryptographicHash::Sha256, m_target->m_expectedSha256));
		}

		// Add JSON parsing validator
		download->addValidator(new JsonParseValidator(m_target));

		m_netTask->addNetAction(download);
		m_netTask->setAskRetry(false);

		// Connect signals
		connect(m_netTask.get(), &Task::succeeded, this, [this]() { finalizeLoad(MetaEntity::State::Synchronized); });
		connect(m_netTask.get(), &Task::failed, this, &EntityLoader::handleRemoteFailure);
		connect(m_netTask.get(), &Task::progress, this, &Task::setProgress);
		connect(m_netTask.get(), &Task::stepProgress, this, &EntityLoader::propagateStepProgress);
		connect(m_netTask.get(), &Task::status, this, &Task::setStatus);
		connect(m_netTask.get(), &Task::details, this, &Task::setDetails);

		m_netTask->start();
	}

	bool EntityLoader::loadCachedMetadata(const QString& cachePath, QString* errorOut)
	{
		try
		{
			setStatus(tr("Loading cached metadata"));

			QByteArray content		 = FS::read(cachePath);
			m_target->m_actualSha256 = Hashing::hash(content, Hashing::Algorithm::Sha256);

			if (m_target->m_state == MetaEntity::State::Pending)
			{
				QJsonDocument doc = Json::requireDocument(content, cachePath);
				QJsonObject root  = Json::requireObject(doc, cachePath);
				m_target->loadFromJson(root);
			}

			return true;
		}
		catch (const Exception& ex)
		{
			if (errorOut)
				*errorOut = ex.cause();
			return false;
		}
	}

	void EntityLoader::handleRemoteFailure(const QString& reason)
	{
		if (!m_canUseLocalFallback)
		{
			emitFailed(reason);
			return;
		}

		QString cachePath = QDir("meta").absoluteFilePath(m_target->cacheFilePath());
		QString cacheError;
		if (QFile::exists(cachePath) && loadCachedMetadata(cachePath, &cacheError))
		{
			m_target->m_state = MetaEntity::State::Cached;
			emitSucceeded();
			qWarning() << "Remote metadata refresh failed for" << m_target->cacheFilePath()
					   << "- falling back to cached metadata:" << reason;
			return;
		}

		if (!cacheError.isEmpty())
			qWarning() << "Cached fallback also failed for" << cachePath << ":" << cacheError;

		emitFailed(reason);
	}

	void EntityLoader::finalizeLoad(MetaEntity::State newState)
	{
		m_target->m_state = newState;
		if (newState == MetaEntity::State::Synchronized)
		{
			m_target->m_actualSha256 = m_target->m_expectedSha256;
		}
		emitSucceeded();
	}

	bool EntityLoader::canAbort() const
	{
		return m_netTask ? m_netTask->canAbort() : false;
	}

	bool EntityLoader::abort()
	{
		if (m_netTask)
		{
			Task::abort();
			return m_netTask->abort();
		}
		return Task::abort();
	}

} // namespace projt::meta