2022-10-08 19:09:53 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-03-07 01:01:28 +00:00
|
|
|
#include "Library.h"
|
2016-10-01 23:26:10 +01:00
|
|
|
#include "MinecraftInstance.h"
|
|
|
|
|
2020-07-18 15:18:02 +01:00
|
|
|
#include <BuildConfig.h>
|
2023-08-02 17:35:35 +01:00
|
|
|
#include <FileSystem.h>
|
|
|
|
#include <net/ChecksumValidator.h>
|
|
|
|
#include <net/Download.h>
|
2014-05-09 16:16:25 +01:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void Library::getApplicableFiles(const RuntimeContext& runtimeContext,
|
|
|
|
QStringList& jar,
|
|
|
|
QStringList& native,
|
|
|
|
QStringList& native32,
|
|
|
|
QStringList& native64,
|
|
|
|
const QString& overridePath) const
|
2014-07-26 22:00:35 +01:00
|
|
|
{
|
2017-04-17 21:51:30 +01:00
|
|
|
bool local = isLocal();
|
2023-08-02 17:35:35 +01:00
|
|
|
auto actualPath = [&](QString relPath) {
|
2016-03-26 15:56:57 +00:00
|
|
|
QFileInfo out(FS::PathCombine(storagePrefix(), relPath));
|
2023-08-02 17:35:35 +01:00
|
|
|
if (local && !overridePath.isEmpty()) {
|
2016-10-01 23:26:10 +01:00
|
|
|
QString fileName = out.fileName();
|
2018-11-11 22:50:36 +00:00
|
|
|
return QFileInfo(FS::PathCombine(overridePath, fileName)).absoluteFilePath();
|
2016-10-01 23:26:10 +01:00
|
|
|
}
|
2016-03-26 15:56:57 +00:00
|
|
|
return out.absoluteFilePath();
|
|
|
|
};
|
2022-07-11 08:01:07 +01:00
|
|
|
QString raw_storage = storageSuffix(runtimeContext);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (isNative()) {
|
|
|
|
if (raw_storage.contains("${arch}")) {
|
2017-04-15 10:40:22 +01:00
|
|
|
auto nat32Storage = raw_storage;
|
|
|
|
nat32Storage.replace("${arch}", "32");
|
|
|
|
auto nat64Storage = raw_storage;
|
|
|
|
nat64Storage.replace("${arch}", "64");
|
|
|
|
native32 += actualPath(nat32Storage);
|
|
|
|
native64 += actualPath(nat64Storage);
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2017-04-15 10:40:22 +01:00
|
|
|
native += actualPath(raw_storage);
|
2016-03-26 15:56:57 +00:00
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2017-04-15 10:40:22 +01:00
|
|
|
jar += actualPath(raw_storage);
|
2014-07-26 22:00:35 +01:00
|
|
|
}
|
|
|
|
}
|
2015-05-31 18:24:39 +01:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
QList<NetAction::Ptr> Library::getDownloads(const RuntimeContext& runtimeContext,
|
|
|
|
class HttpMetaCache* cache,
|
|
|
|
QStringList& failedLocalFiles,
|
|
|
|
const QString& overridePath) const
|
2014-07-26 22:00:35 +01:00
|
|
|
{
|
2021-11-21 22:21:12 +00:00
|
|
|
QList<NetAction::Ptr> out;
|
2018-11-26 02:06:58 +00:00
|
|
|
bool stale = isAlwaysStale();
|
2017-04-17 21:51:30 +01:00
|
|
|
bool local = isLocal();
|
2018-11-26 02:06:58 +00:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
auto check_local_file = [&](QString storage) {
|
2018-11-26 02:06:58 +00:00
|
|
|
QFileInfo fileinfo(storage);
|
|
|
|
QString fileName = fileinfo.fileName();
|
|
|
|
auto fullPath = FS::PathCombine(overridePath, fileName);
|
|
|
|
QFileInfo localFileInfo(fullPath);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!localFileInfo.exists()) {
|
2018-11-26 02:06:58 +00:00
|
|
|
failedLocalFiles.append(localFileInfo.filePath());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2014-07-26 22:00:35 +01:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
auto add_download = [&](QString storage, QString url, QString sha1) {
|
|
|
|
if (local) {
|
2018-11-26 02:06:58 +00:00
|
|
|
return check_local_file(storage);
|
|
|
|
}
|
2016-03-26 15:56:57 +00:00
|
|
|
auto entry = cache->resolveEntry("libraries", storage);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (stale) {
|
2016-06-13 20:53:56 +01:00
|
|
|
entry->setStale(true);
|
|
|
|
}
|
2016-03-26 15:56:57 +00:00
|
|
|
if (!entry->isStale())
|
|
|
|
return true;
|
2016-10-28 01:19:19 +01:00
|
|
|
Net::Download::Options options;
|
2023-08-02 17:35:35 +01:00
|
|
|
if (stale) {
|
2016-10-28 01:19:19 +01:00
|
|
|
options |= Net::Download::Option::AcceptLocalFiles;
|
|
|
|
}
|
2020-05-19 14:13:16 +01:00
|
|
|
|
2022-08-21 17:06:18 +01:00
|
|
|
// Don't add a time limit for the libraries cache entry validity
|
|
|
|
options |= Net::Download::Option::MakeEternal;
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
if (sha1.size()) {
|
2020-05-19 14:13:16 +01:00
|
|
|
auto rawSha1 = QByteArray::fromHex(sha1.toLatin1());
|
|
|
|
auto dl = Net::Download::makeCached(url, entry, options);
|
|
|
|
dl->addValidator(new Net::ChecksumValidator(QCryptographicHash::Sha1, rawSha1));
|
2021-04-01 02:50:28 +01:00
|
|
|
qDebug() << "Checksummed Download for:" << rawName().serialize() << "storage:" << storage << "url:" << url;
|
2020-05-19 14:13:16 +01:00
|
|
|
out.append(dl);
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2020-05-19 14:13:16 +01:00
|
|
|
out.append(Net::Download::makeCached(url, entry, options));
|
2021-04-01 02:50:28 +01:00
|
|
|
qDebug() << "Download for:" << rawName().serialize() << "storage:" << storage << "url:" << url;
|
2016-03-26 15:56:57 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2014-07-26 22:00:35 +01:00
|
|
|
|
2022-07-11 08:01:07 +01:00
|
|
|
QString raw_storage = storageSuffix(runtimeContext);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_mojangDownloads) {
|
|
|
|
if (isNative()) {
|
2022-08-06 23:06:32 +01:00
|
|
|
auto nativeClassifier = getCompatibleNative(runtimeContext);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!nativeClassifier.isNull()) {
|
|
|
|
if (nativeClassifier.contains("${arch}")) {
|
2017-10-28 20:12:12 +01:00
|
|
|
auto nat32Classifier = nativeClassifier;
|
|
|
|
nat32Classifier.replace("${arch}", "32");
|
|
|
|
auto nat64Classifier = nativeClassifier;
|
|
|
|
nat64Classifier.replace("${arch}", "64");
|
|
|
|
auto nat32info = m_mojangDownloads->getDownloadInfo(nat32Classifier);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (nat32info) {
|
2017-10-28 20:12:12 +01:00
|
|
|
auto cooked_storage = raw_storage;
|
|
|
|
cooked_storage.replace("${arch}", "32");
|
|
|
|
add_download(cooked_storage, nat32info->url, nat32info->sha1);
|
|
|
|
}
|
|
|
|
auto nat64info = m_mojangDownloads->getDownloadInfo(nat64Classifier);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (nat64info) {
|
2017-10-28 20:12:12 +01:00
|
|
|
auto cooked_storage = raw_storage;
|
|
|
|
cooked_storage.replace("${arch}", "64");
|
|
|
|
add_download(cooked_storage, nat64info->url, nat64info->sha1);
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2017-10-28 20:12:12 +01:00
|
|
|
auto info = m_mojangDownloads->getDownloadInfo(nativeClassifier);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (info) {
|
2017-10-28 20:12:12 +01:00
|
|
|
add_download(raw_storage, info->url, info->sha1);
|
|
|
|
}
|
2017-04-15 10:40:22 +01:00
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2021-04-01 02:50:28 +01:00
|
|
|
qDebug() << "Ignoring native library" << m_name.serialize() << "because it has no classifier for current OS";
|
2017-10-28 20:12:12 +01:00
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
|
|
|
if (m_mojangDownloads->artifact) {
|
2017-10-28 20:12:12 +01:00
|
|
|
auto artifact = m_mojangDownloads->artifact;
|
|
|
|
add_download(raw_storage, artifact->url, artifact->sha1);
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2021-04-01 02:50:28 +01:00
|
|
|
qDebug() << "Ignoring java library" << m_name.serialize() << "because it has no artifact";
|
2016-03-26 15:56:57 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
|
|
|
auto raw_dl = [&]() {
|
|
|
|
if (!m_absoluteURL.isEmpty()) {
|
2016-03-26 15:56:57 +00:00
|
|
|
return m_absoluteURL;
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_repositoryURL.isEmpty()) {
|
2020-07-18 15:18:02 +01:00
|
|
|
return BuildConfig.LIBRARY_BASE + raw_storage;
|
2016-03-26 15:56:57 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_repositoryURL.endsWith('/')) {
|
2016-03-26 15:56:57 +00:00
|
|
|
return m_repositoryURL + raw_storage;
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2016-03-26 15:56:57 +00:00
|
|
|
return m_repositoryURL + QChar('/') + raw_storage;
|
|
|
|
}
|
|
|
|
}();
|
2023-08-02 17:35:35 +01:00
|
|
|
if (raw_storage.contains("${arch}")) {
|
2016-03-26 15:56:57 +00:00
|
|
|
QString cooked_storage = raw_storage;
|
|
|
|
QString cooked_dl = raw_dl;
|
2018-06-28 22:18:45 +01:00
|
|
|
add_download(cooked_storage.replace("${arch}", "32"), cooked_dl.replace("${arch}", "32"), QString());
|
2016-03-26 15:56:57 +00:00
|
|
|
cooked_storage = raw_storage;
|
|
|
|
cooked_dl = raw_dl;
|
2018-06-28 22:18:45 +01:00
|
|
|
add_download(cooked_storage.replace("${arch}", "64"), cooked_dl.replace("${arch}", "64"), QString());
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2018-06-28 22:18:45 +01:00
|
|
|
add_download(raw_storage, raw_dl, QString());
|
2016-03-26 15:56:57 +00:00
|
|
|
}
|
2015-05-31 16:51:20 +01:00
|
|
|
}
|
2016-03-26 15:56:57 +00:00
|
|
|
return out;
|
2014-07-26 22:00:35 +01:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
bool Library::isActive(const RuntimeContext& runtimeContext) const
|
2014-07-26 22:00:35 +01:00
|
|
|
{
|
|
|
|
bool result = true;
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_rules.empty()) {
|
2014-07-26 22:00:35 +01:00
|
|
|
result = true;
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2014-07-26 22:00:35 +01:00
|
|
|
RuleAction ruleResult = Disallow;
|
2023-08-02 17:35:35 +01:00
|
|
|
for (auto rule : m_rules) {
|
2022-08-06 23:06:32 +01:00
|
|
|
RuleAction temp = rule->apply(this, runtimeContext);
|
2014-07-26 22:00:35 +01:00
|
|
|
if (temp != Defer)
|
|
|
|
ruleResult = temp;
|
|
|
|
}
|
|
|
|
result = result && (ruleResult == Allow);
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
if (isNative()) {
|
2022-08-06 23:06:32 +01:00
|
|
|
result = result && !getCompatibleNative(runtimeContext).isNull();
|
2014-07-26 22:00:35 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-04-17 21:51:30 +01:00
|
|
|
bool Library::isLocal() const
|
|
|
|
{
|
|
|
|
return m_hint == "local";
|
|
|
|
}
|
|
|
|
|
2018-11-26 02:06:58 +00:00
|
|
|
bool Library::isAlwaysStale() const
|
|
|
|
{
|
|
|
|
return m_hint == "always-stale";
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
QString Library::getCompatibleNative(const RuntimeContext& runtimeContext) const
|
|
|
|
{
|
2022-08-06 23:06:32 +01:00
|
|
|
// try to match precise classifier "[os]-[arch]"
|
|
|
|
auto entry = m_nativeClassifiers.constFind(runtimeContext.getClassifier());
|
|
|
|
// try to match imprecise classifier on legacy architectures "[os]"
|
|
|
|
if (entry == m_nativeClassifiers.constEnd() && runtimeContext.isLegacyArch())
|
|
|
|
entry = m_nativeClassifiers.constFind(runtimeContext.system);
|
|
|
|
|
|
|
|
if (entry == m_nativeClassifiers.constEnd())
|
|
|
|
return QString();
|
|
|
|
|
|
|
|
return entry.value();
|
|
|
|
}
|
|
|
|
|
2016-03-07 01:01:28 +00:00
|
|
|
void Library::setStoragePrefix(QString prefix)
|
2015-05-27 00:30:18 +01:00
|
|
|
{
|
|
|
|
m_storagePrefix = prefix;
|
|
|
|
}
|
|
|
|
|
2016-03-07 01:01:28 +00:00
|
|
|
QString Library::defaultStoragePrefix()
|
2015-05-27 00:30:18 +01:00
|
|
|
{
|
|
|
|
return "libraries/";
|
|
|
|
}
|
|
|
|
|
2016-03-07 01:01:28 +00:00
|
|
|
QString Library::storagePrefix() const
|
2015-05-27 00:30:18 +01:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_storagePrefix.isEmpty()) {
|
2015-05-27 00:30:18 +01:00
|
|
|
return defaultStoragePrefix();
|
|
|
|
}
|
|
|
|
return m_storagePrefix;
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
QString Library::filename(const RuntimeContext& runtimeContext) const
|
2017-04-17 21:51:30 +01:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!m_filename.isEmpty()) {
|
2017-04-17 21:51:30 +01:00
|
|
|
return m_filename;
|
|
|
|
}
|
|
|
|
// non-native? use only the gradle specifier
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!isNative()) {
|
2017-04-17 21:51:30 +01:00
|
|
|
return m_name.getFileName();
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise native, override classifiers. Mojang HACK!
|
|
|
|
GradleSpecifier nativeSpec = m_name;
|
2022-08-06 23:06:32 +01:00
|
|
|
QString nativeClassifier = getCompatibleNative(runtimeContext);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!nativeClassifier.isNull()) {
|
2022-08-06 23:06:32 +01:00
|
|
|
nativeSpec.setClassifier(nativeClassifier);
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2017-04-17 21:51:30 +01:00
|
|
|
nativeSpec.setClassifier("INVALID");
|
|
|
|
}
|
|
|
|
return nativeSpec.getFileName();
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
QString Library::displayName(const RuntimeContext& runtimeContext) const
|
2017-04-17 21:51:30 +01:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!m_displayname.isEmpty())
|
2017-04-17 21:51:30 +01:00
|
|
|
return m_displayname;
|
2022-07-11 08:01:07 +01:00
|
|
|
return filename(runtimeContext);
|
2017-04-17 21:51:30 +01:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
QString Library::storageSuffix(const RuntimeContext& runtimeContext) const
|
2014-05-11 11:37:21 +01:00
|
|
|
{
|
2014-07-26 22:00:35 +01:00
|
|
|
// non-native? use only the gradle specifier
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!isNative()) {
|
2017-04-17 21:51:30 +01:00
|
|
|
return m_name.toPath(m_filename);
|
2014-07-26 22:00:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise native, override classifiers. Mojang HACK!
|
|
|
|
GradleSpecifier nativeSpec = m_name;
|
2022-08-06 23:06:32 +01:00
|
|
|
QString nativeClassifier = getCompatibleNative(runtimeContext);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!nativeClassifier.isNull()) {
|
2022-08-06 23:06:32 +01:00
|
|
|
nativeSpec.setClassifier(nativeClassifier);
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2014-07-26 22:00:35 +01:00
|
|
|
nativeSpec.setClassifier("INVALID");
|
|
|
|
}
|
2017-04-17 21:51:30 +01:00
|
|
|
return nativeSpec.toPath(m_filename);
|
2014-05-11 11:37:21 +01:00
|
|
|
}
|