feat: support multiarch system classifiers

Signed-off-by: Sefa Eyeoglu <contact@scrumplex.net>
This commit is contained in:
Sefa Eyeoglu 2022-08-07 00:06:32 +02:00
parent 09e85e948c
commit 7bd8bd13fe
No known key found for this signature in database
GPG Key ID: C10411294912A422
5 changed files with 106 additions and 39 deletions

View File

@ -1,5 +1,6 @@
#pragma once
#include <QSet>
#include <QString>
#include "settings/SettingsObject.h"
@ -7,6 +8,7 @@ struct RuntimeContext {
QString javaArchitecture;
QString javaRealArchitecture;
QString javaPath;
QString system;
QString mappedJavaRealArchitecture() const {
if (javaRealArchitecture == "aarch64") {
@ -19,6 +21,27 @@ struct RuntimeContext {
javaArchitecture = instanceSettings->get("JavaArchitecture").toString();
javaRealArchitecture = instanceSettings->get("JavaRealArchitecture").toString();
javaPath = instanceSettings->get("JavaPath").toString();
system = currentSystem();
}
QString getClassifier() const {
return system + "-" + mappedJavaRealArchitecture();
}
// "Legacy" refers to the fact that Mojang assumed that these are the only two architectures
bool isLegacyArch() const {
QSet<QString> legacyArchitectures{"amd64", "x86_64", "i686"};
return legacyArchitectures.contains(mappedJavaRealArchitecture());
}
bool classifierMatches(QString target) const {
// try to match precise classifier "[os]-[arch]"
bool x = target == getClassifier();
// try to match imprecise classifier on legacy architectures "[os]"
if (!x && isLegacyArch())
x = target == system;
return x;
}
static QString currentSystem() {

View File

@ -112,9 +112,9 @@ QList<NetAction::Ptr> Library::getDownloads(
{
if(isNative())
{
if(m_nativeClassifiers.contains(runtimeContext.currentSystem()))
auto nativeClassifier = getCompatibleNative(runtimeContext);
if(!nativeClassifier.isNull())
{
auto nativeClassifier = m_nativeClassifiers[runtimeContext.currentSystem()];
if(nativeClassifier.contains("${arch}"))
{
auto nat32Classifier = nativeClassifier;
@ -215,7 +215,7 @@ bool Library::isActive(const RuntimeContext & runtimeContext) const
RuleAction ruleResult = Disallow;
for (auto rule : m_rules)
{
RuleAction temp = rule->apply(this);
RuleAction temp = rule->apply(this, runtimeContext);
if (temp != Defer)
ruleResult = temp;
}
@ -223,7 +223,7 @@ bool Library::isActive(const RuntimeContext & runtimeContext) const
}
if (isNative())
{
result = result && m_nativeClassifiers.contains(runtimeContext.currentSystem());
result = result && !getCompatibleNative(runtimeContext).isNull();
}
return result;
}
@ -238,6 +238,19 @@ bool Library::isAlwaysStale() const
return m_hint == "always-stale";
}
QString Library::getCompatibleNative(const RuntimeContext & runtimeContext) const {
// 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();
}
void Library::setStoragePrefix(QString prefix)
{
m_storagePrefix = prefix;
@ -271,9 +284,10 @@ QString Library::filename(const RuntimeContext & runtimeContext) const
// otherwise native, override classifiers. Mojang HACK!
GradleSpecifier nativeSpec = m_name;
if (m_nativeClassifiers.contains(runtimeContext.currentSystem()))
QString nativeClassifier = getCompatibleNative(runtimeContext);
if (!nativeClassifier.isNull())
{
nativeSpec.setClassifier(m_nativeClassifiers[runtimeContext.currentSystem()]);
nativeSpec.setClassifier(nativeClassifier);
}
else
{
@ -299,9 +313,10 @@ QString Library::storageSuffix(const RuntimeContext & runtimeContext) const
// otherwise native, override classifiers. Mojang HACK!
GradleSpecifier nativeSpec = m_name;
if (m_nativeClassifiers.contains(runtimeContext.currentSystem()))
QString nativeClassifier = getCompatibleNative(runtimeContext);
if (!nativeClassifier.isNull())
{
nativeSpec.setClassifier(m_nativeClassifiers[runtimeContext.currentSystem()]);
nativeSpec.setClassifier(nativeClassifier);
}
else
{

View File

@ -3,6 +3,7 @@
#include <net/NetAction.h>
#include <QPair>
#include <QList>
#include <QString>
#include <QStringList>
#include <QMap>
#include <QDir>
@ -155,6 +156,8 @@ public: /* methods */
QList<NetAction::Ptr> getDownloads(const RuntimeContext & runtimeContext, class HttpMetaCache * cache,
QStringList & failedLocalFiles, const QString & overridePath) const;
QString getCompatibleNative(const RuntimeContext & runtimeContext) const;
private: /* methods */
/// the default storage prefix used by PolyMC
static QString defaultStoragePrefix();

View File

@ -37,7 +37,7 @@ class Rule
{
protected:
RuleAction m_result;
virtual bool applies(const Library *parent) = 0;
virtual bool applies(const Library *parent, const RuntimeContext & runtimeContext) = 0;
public:
Rule(RuleAction result) : m_result(result)
@ -45,9 +45,9 @@ public:
}
virtual ~Rule() {};
virtual QJsonObject toJson() = 0;
RuleAction apply(const Library *parent)
RuleAction apply(const Library *parent, const RuntimeContext & runtimeContext)
{
if (applies(parent))
if (applies(parent, runtimeContext))
return m_result;
else
return Defer;
@ -63,9 +63,9 @@ private:
QString m_version_regexp;
protected:
virtual bool applies(const Library *)
virtual bool applies(const Library *, const RuntimeContext & runtimeContext)
{
return (m_system == RuntimeContext::currentSystem());
return runtimeContext.classifierMatches(m_system);
}
OsRule(RuleAction result, QString system, QString version_regexp)
: Rule(result), m_system(system), m_version_regexp(version_regexp)
@ -84,7 +84,7 @@ public:
class ImplicitRule : public Rule
{
protected:
virtual bool applies(const Library *)
virtual bool applies(const Library *, const RuntimeContext & runtimeContext)
{
return true;
}

View File

@ -5,6 +5,7 @@
#include <minecraft/Library.h>
#include <net/HttpMetaCache.h>
#include <FileSystem.h>
#include <RuntimeContext.h>
class LibraryTest : public QObject
{
@ -24,6 +25,14 @@ private:
{
return {FS::PathCombine(cache->getBasePath("libraries"), relative)};
}
RuntimeContext dummyContext(QString system = "linux", QString arch = "64", QString realArch = "amd64") {
RuntimeContext r;
r.javaArchitecture = arch;
r.javaRealArchitecture = realArch;
r.system = system;
return r;
}
private
slots:
void initTestCase()
@ -34,12 +43,13 @@ slots:
}
void test_legacy()
{
RuntimeContext r = dummyContext();
Library test("test.package:testname:testversion");
QCOMPARE(test.artifactPrefix(), QString("test.package:testname"));
QCOMPARE(test.isNative(), false);
QStringList jar, native, native32, native64;
test.getApplicableFiles(currentSystem, jar, native, native32, native64, QString());
test.getApplicableFiles(r, jar, native, native32, native64, QString());
QCOMPARE(jar, getStorage("test/package/testname/testversion/testname-testversion.jar"));
QCOMPARE(native, {});
QCOMPARE(native32, {});
@ -47,10 +57,11 @@ slots:
}
void test_legacy_url()
{
RuntimeContext r = dummyContext();
QStringList failedFiles;
Library test("test.package:testname:testversion");
test.setRepositoryURL("file://foo/bar");
auto downloads = test.getDownloads(currentSystem, cache.get(), failedFiles, QString());
auto downloads = test.getDownloads(r, cache.get(), failedFiles, QString());
QCOMPARE(downloads.size(), 1);
QCOMPARE(failedFiles, {});
NetAction::Ptr dl = downloads[0];
@ -58,27 +69,29 @@ slots:
}
void test_legacy_url_local_broken()
{
RuntimeContext r = dummyContext();
Library test("test.package:testname:testversion");
QCOMPARE(test.isNative(), false);
QStringList failedFiles;
test.setHint("local");
auto downloads = test.getDownloads(currentSystem, cache.get(), failedFiles, QString());
auto downloads = test.getDownloads(r, cache.get(), failedFiles, QString());
QCOMPARE(downloads.size(), 0);
QCOMPARE(failedFiles, {"testname-testversion.jar"});
}
void test_legacy_url_local_override()
{
RuntimeContext r = dummyContext();
Library test("com.paulscode:codecwav:20101023");
QCOMPARE(test.isNative(), false);
QStringList failedFiles;
test.setHint("local");
auto downloads = test.getDownloads(currentSystem, cache.get(), failedFiles, QFINDTESTDATA("testdata/Library"));
auto downloads = test.getDownloads(r, cache.get(), failedFiles, QFINDTESTDATA("testdata/Library"));
QCOMPARE(downloads.size(), 0);
qDebug() << failedFiles;
QCOMPARE(failedFiles.size(), 0);
QStringList jar, native, native32, native64;
test.getApplicableFiles(currentSystem, jar, native, native32, native64, QFINDTESTDATA("testdata/Library"));
test.getApplicableFiles(r, jar, native, native32, native64, QFINDTESTDATA("testdata/Library"));
QCOMPARE(jar, {QFileInfo(QFINDTESTDATA("testdata/Library/codecwav-20101023.jar")).absoluteFilePath()});
QCOMPARE(native, {});
QCOMPARE(native32, {});
@ -86,19 +99,20 @@ slots:
}
void test_legacy_native()
{
RuntimeContext r = dummyContext();
Library test("test.package:testname:testversion");
test.m_nativeClassifiers["linux"] = "linux";
QCOMPARE(test.isNative(), true);
test.setRepositoryURL("file://foo/bar");
{
QStringList jar, native, native32, native64;
test.getApplicableFiles(Os_Linux, jar, native, native32, native64, QString());
test.getApplicableFiles(r, jar, native, native32, native64, QString());
QCOMPARE(jar, {});
QCOMPARE(native, getStorage("test/package/testname/testversion/testname-testversion-linux.jar"));
QCOMPARE(native32, {});
QCOMPARE(native64, {});
QStringList failedFiles;
auto dls = test.getDownloads(Os_Linux, cache.get(), failedFiles, QString());
auto dls = test.getDownloads(r, cache.get(), failedFiles, QString());
QCOMPARE(dls.size(), 1);
QCOMPARE(failedFiles, {});
auto dl = dls[0];
@ -107,6 +121,7 @@ slots:
}
void test_legacy_native_arch()
{
RuntimeContext r = dummyContext();
Library test("test.package:testname:testversion");
test.m_nativeClassifiers["linux"]="linux-${arch}";
test.m_nativeClassifiers["osx"]="osx-${arch}";
@ -115,41 +130,43 @@ slots:
test.setRepositoryURL("file://foo/bar");
{
QStringList jar, native, native32, native64;
test.getApplicableFiles(Os_Linux, jar, native, native32, native64, QString());
test.getApplicableFiles(r, jar, native, native32, native64, QString());
QCOMPARE(jar, {});
QCOMPARE(native, {});
QCOMPARE(native32, getStorage("test/package/testname/testversion/testname-testversion-linux-32.jar"));
QCOMPARE(native64, getStorage("test/package/testname/testversion/testname-testversion-linux-64.jar"));
QStringList failedFiles;
auto dls = test.getDownloads(Os_Linux, cache.get(), failedFiles, QString());
auto dls = test.getDownloads(r, cache.get(), failedFiles, QString());
QCOMPARE(dls.size(), 2);
QCOMPARE(failedFiles, {});
QCOMPARE(dls[0]->m_url, QUrl("file://foo/bar/test/package/testname/testversion/testname-testversion-linux-32.jar"));
QCOMPARE(dls[1]->m_url, QUrl("file://foo/bar/test/package/testname/testversion/testname-testversion-linux-64.jar"));
}
r.system = "windows";
{
QStringList jar, native, native32, native64;
test.getApplicableFiles(Os_Windows, jar, native, native32, native64, QString());
test.getApplicableFiles(r, jar, native, native32, native64, QString());
QCOMPARE(jar, {});
QCOMPARE(native, {});
QCOMPARE(native32, getStorage("test/package/testname/testversion/testname-testversion-windows-32.jar"));
QCOMPARE(native64, getStorage("test/package/testname/testversion/testname-testversion-windows-64.jar"));
QStringList failedFiles;
auto dls = test.getDownloads(Os_Windows, cache.get(), failedFiles, QString());
auto dls = test.getDownloads(r, cache.get(), failedFiles, QString());
QCOMPARE(dls.size(), 2);
QCOMPARE(failedFiles, {});
QCOMPARE(dls[0]->m_url, QUrl("file://foo/bar/test/package/testname/testversion/testname-testversion-windows-32.jar"));
QCOMPARE(dls[1]->m_url, QUrl("file://foo/bar/test/package/testname/testversion/testname-testversion-windows-64.jar"));
}
r.system = "osx";
{
QStringList jar, native, native32, native64;
test.getApplicableFiles(Os_OSX, jar, native, native32, native64, QString());
test.getApplicableFiles(r, jar, native, native32, native64, QString());
QCOMPARE(jar, {});
QCOMPARE(native, {});
QCOMPARE(native32, getStorage("test/package/testname/testversion/testname-testversion-osx-32.jar"));
QCOMPARE(native64, getStorage("test/package/testname/testversion/testname-testversion-osx-64.jar"));
QStringList failedFiles;
auto dls = test.getDownloads(Os_OSX, cache.get(), failedFiles, QString());
auto dls = test.getDownloads(r, cache.get(), failedFiles, QString());
QCOMPARE(dls.size(), 2);
QCOMPARE(failedFiles, {});
QCOMPARE(dls[0]->m_url, QUrl("file://foo/bar/test/package/testname/testversion/testname-testversion-osx-32.jar"));
@ -158,6 +175,7 @@ slots:
}
void test_legacy_native_arch_local_override()
{
RuntimeContext r = dummyContext();
Library test("test.package:testname:testversion");
test.m_nativeClassifiers["linux"]="linux-${arch}";
test.setHint("local");
@ -165,96 +183,104 @@ slots:
test.setRepositoryURL("file://foo/bar");
{
QStringList jar, native, native32, native64;
test.getApplicableFiles(Os_Linux, jar, native, native32, native64, QFINDTESTDATA("testdata/Library"));
test.getApplicableFiles(r, jar, native, native32, native64, QFINDTESTDATA("testdata/Library"));
QCOMPARE(jar, {});
QCOMPARE(native, {});
QCOMPARE(native32, {QFileInfo(QFINDTESTDATA("testdata/Library/testname-testversion-linux-32.jar")).absoluteFilePath()});
QCOMPARE(native64, {QFileInfo(QFINDTESTDATA("testdata/Library") + "/testname-testversion-linux-64.jar").absoluteFilePath()});
QStringList failedFiles;
auto dls = test.getDownloads(Os_Linux, cache.get(), failedFiles, QFINDTESTDATA("testdata/Library"));
auto dls = test.getDownloads(r, cache.get(), failedFiles, QFINDTESTDATA("testdata/Library"));
QCOMPARE(dls.size(), 0);
QCOMPARE(failedFiles, {QFileInfo(QFINDTESTDATA("testdata/Library") + "/testname-testversion-linux-64.jar").absoluteFilePath()});
}
}
void test_onenine()
{
RuntimeContext r = dummyContext("osx");
auto test = readMojangJson(QFINDTESTDATA("testdata/Library/lib-simple.json"));
{
QStringList jar, native, native32, native64;
test->getApplicableFiles(Os_OSX, jar, native, native32, native64, QString());
test->getApplicableFiles(r, jar, native, native32, native64, QString());
QCOMPARE(jar, getStorage("com/paulscode/codecwav/20101023/codecwav-20101023.jar"));
QCOMPARE(native, {});
QCOMPARE(native32, {});
QCOMPARE(native64, {});
}
r.system = "linux";
{
QStringList failedFiles;
auto dls = test->getDownloads(Os_Linux, cache.get(), failedFiles, QString());
auto dls = test->getDownloads(r, cache.get(), failedFiles, QString());
QCOMPARE(dls.size(), 1);
QCOMPARE(failedFiles, {});
QCOMPARE(dls[0]->m_url, QUrl("https://libraries.minecraft.net/com/paulscode/codecwav/20101023/codecwav-20101023.jar"));
}
r.system = "osx";
test->setHint("local");
{
QStringList jar, native, native32, native64;
test->getApplicableFiles(Os_OSX, jar, native, native32, native64, QFINDTESTDATA("testdata/Library"));
test->getApplicableFiles(r, jar, native, native32, native64, QFINDTESTDATA("testdata/Library"));
QCOMPARE(jar, {QFileInfo(QFINDTESTDATA("testdata/Library/codecwav-20101023.jar")).absoluteFilePath()});
QCOMPARE(native, {});
QCOMPARE(native32, {});
QCOMPARE(native64, {});
}
r.system = "linux";
{
QStringList failedFiles;
auto dls = test->getDownloads(Os_Linux, cache.get(), failedFiles, QFINDTESTDATA("testdata/Library"));
auto dls = test->getDownloads(r, cache.get(), failedFiles, QFINDTESTDATA("testdata/Library"));
QCOMPARE(dls.size(), 0);
QCOMPARE(failedFiles, {});
}
}
void test_onenine_local_override()
{
RuntimeContext r = dummyContext("osx");
auto test = readMojangJson(QFINDTESTDATA("testdata/Library/lib-simple.json"));
test->setHint("local");
{
QStringList jar, native, native32, native64;
test->getApplicableFiles(Os_OSX, jar, native, native32, native64, QFINDTESTDATA("testdata/Library"));
test->getApplicableFiles(r, jar, native, native32, native64, QFINDTESTDATA("testdata/Library"));
QCOMPARE(jar, {QFileInfo(QFINDTESTDATA("testdata/Library/codecwav-20101023.jar")).absoluteFilePath()});
QCOMPARE(native, {});
QCOMPARE(native32, {});
QCOMPARE(native64, {});
}
r.system = "linux";
{
QStringList failedFiles;
auto dls = test->getDownloads(Os_Linux, cache.get(), failedFiles, QFINDTESTDATA("testdata/Library"));
auto dls = test->getDownloads(r, cache.get(), failedFiles, QFINDTESTDATA("testdata/Library"));
QCOMPARE(dls.size(), 0);
QCOMPARE(failedFiles, {});
}
}
void test_onenine_native()
{
RuntimeContext r = dummyContext("osx");
auto test = readMojangJson(QFINDTESTDATA("testdata/Library/lib-native.json"));
QStringList jar, native, native32, native64;
test->getApplicableFiles(Os_OSX, jar, native, native32, native64, QString());
test->getApplicableFiles(r, jar, native, native32, native64, QString());
QCOMPARE(jar, QStringList());
QCOMPARE(native, getStorage("org/lwjgl/lwjgl/lwjgl-platform/2.9.4-nightly-20150209/lwjgl-platform-2.9.4-nightly-20150209-natives-osx.jar"));
QCOMPARE(native32, {});
QCOMPARE(native64, {});
QStringList failedFiles;
auto dls = test->getDownloads(Os_OSX, cache.get(), failedFiles, QString());
auto dls = test->getDownloads(r, cache.get(), failedFiles, QString());
QCOMPARE(dls.size(), 1);
QCOMPARE(failedFiles, {});
QCOMPARE(dls[0]->m_url, QUrl("https://libraries.minecraft.net/org/lwjgl/lwjgl/lwjgl-platform/2.9.4-nightly-20150209/lwjgl-platform-2.9.4-nightly-20150209-natives-osx.jar"));
}
void test_onenine_native_arch()
{
RuntimeContext r = dummyContext("windows");
auto test = readMojangJson(QFINDTESTDATA("testdata/Library/lib-native-arch.json"));
QStringList jar, native, native32, native64;
test->getApplicableFiles(Os_Windows, jar, native, native32, native64, QString());
test->getApplicableFiles(r, jar, native, native32, native64, QString());
QCOMPARE(jar, {});
QCOMPARE(native, {});
QCOMPARE(native32, getStorage("tv/twitch/twitch-platform/5.16/twitch-platform-5.16-natives-windows-32.jar"));
QCOMPARE(native64, getStorage("tv/twitch/twitch-platform/5.16/twitch-platform-5.16-natives-windows-64.jar"));
QStringList failedFiles;
auto dls = test->getDownloads(Os_Windows, cache.get(), failedFiles, QString());
auto dls = test->getDownloads(r, cache.get(), failedFiles, QString());
QCOMPARE(dls.size(), 2);
QCOMPARE(failedFiles, {});
QCOMPARE(dls[0]->m_url, QUrl("https://libraries.minecraft.net/tv/twitch/twitch-platform/5.16/twitch-platform-5.16-natives-windows-32.jar"));