Merge pull request #597 from Scrumplex/refactor-modloader-modapi

This commit is contained in:
Sefa Eyeoglu 2022-05-22 13:17:53 +02:00 committed by GitHub
commit 29ef1e2c4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 68 additions and 54 deletions

View File

@ -36,6 +36,13 @@
#include "ComponentUpdateTask.h" #include "ComponentUpdateTask.h"
#include "Application.h" #include "Application.h"
#include "modplatform/ModAPI.h"
static const QMap<QString, ModAPI::ModLoaderType> modloaderMapping{
{"net.minecraftforge", ModAPI::Forge},
{"net.fabricmc.fabric-loader", ModAPI::Fabric},
{"org.quiltmc.quilt-loader", ModAPI::Quilt}
};
PackProfile::PackProfile(MinecraftInstance * instance) PackProfile::PackProfile(MinecraftInstance * instance)
: QAbstractListModel() : QAbstractListModel()
@ -971,19 +978,19 @@ void PackProfile::disableInteraction(bool disable)
} }
} }
ModAPI::ModLoaderType PackProfile::getModLoader() ModAPI::ModLoaderTypes PackProfile::getModLoaders()
{ {
if (!getComponentVersion("net.minecraftforge").isEmpty()) ModAPI::ModLoaderTypes result = ModAPI::Unspecified;
QMapIterator<QString, ModAPI::ModLoaderType> i(modloaderMapping);
while (i.hasNext())
{ {
return ModAPI::Forge; i.next();
Component* c = getComponent(i.key());
if (c != nullptr && c->isEnabled()) {
result |= i.value();
} }
else if (!getComponentVersion("net.fabricmc.fabric-loader").isEmpty())
{
return ModAPI::Fabric;
} }
else if (!getComponentVersion("org.quiltmc.quilt-loader").isEmpty()) return result;
{
return ModAPI::Quilt;
}
return ModAPI::Unspecified;
} }

View File

@ -118,7 +118,7 @@ public:
// todo(merged): is this the best approach // todo(merged): is this the best approach
void appendComponent(ComponentPtr component); void appendComponent(ComponentPtr component);
ModAPI::ModLoaderType getModLoader(); ModAPI::ModLoaderTypes getModLoaders();
private: private:
void scheduleSave(); void scheduleSave();

View File

@ -16,14 +16,21 @@ class ModAPI {
public: public:
virtual ~ModAPI() = default; virtual ~ModAPI() = default;
// https://docs.curseforge.com/?http#tocS_ModLoaderType enum ModLoaderType {
enum ModLoaderType { Unspecified = 0, Forge = 1, Cauldron = 2, LiteLoader = 3, Fabric = 4, Quilt = 5 }; Unspecified = 0,
Forge = 1 << 0,
Cauldron = 1 << 1,
LiteLoader = 1 << 2,
Fabric = 1 << 3,
Quilt = 1 << 4
};
Q_DECLARE_FLAGS(ModLoaderTypes, ModLoaderType)
struct SearchArgs { struct SearchArgs {
int offset; int offset;
QString search; QString search;
QString sorting; QString sorting;
ModLoaderType mod_loader; ModLoaderTypes loaders;
std::list<Version> versions; std::list<Version> versions;
}; };
@ -33,7 +40,7 @@ class ModAPI {
struct VersionSearchArgs { struct VersionSearchArgs {
QString addonId; QString addonId;
std::list<Version> mcVersions; std::list<Version> mcVersions;
ModLoaderType loader; ModLoaderTypes loaders;
}; };
virtual void getVersions(CallerType* caller, VersionSearchArgs&& args) const = 0; virtual void getVersions(CallerType* caller, VersionSearchArgs&& args) const = 0;

View File

@ -37,14 +37,14 @@ class FlameAPI : public NetworkModAPI {
.arg(args.offset) .arg(args.offset)
.arg(args.search) .arg(args.search)
.arg(getSortFieldInt(args.sorting)) .arg(getSortFieldInt(args.sorting))
.arg(getMappedModLoader(args.mod_loader)) .arg(getMappedModLoader(args.loaders))
.arg(gameVersionStr); .arg(gameVersionStr);
}; };
inline auto getVersionsURL(VersionSearchArgs& args) const -> QString override inline auto getVersionsURL(VersionSearchArgs& args) const -> QString override
{ {
QString gameVersionQuery = args.mcVersions.size() == 1 ? QString("gameVersion=%1&").arg(args.mcVersions.front().toString()) : ""; QString gameVersionQuery = args.mcVersions.size() == 1 ? QString("gameVersion=%1&").arg(args.mcVersions.front().toString()) : "";
QString modLoaderQuery = QString("modLoaderType=%1&").arg(getMappedModLoader(args.loader)); QString modLoaderQuery = QString("modLoaderType=%1&").arg(getMappedModLoader(args.loaders));
return QString("https://api.curseforge.com/v1/mods/%1/files?pageSize=10000&%2%3") return QString("https://api.curseforge.com/v1/mods/%1/files?pageSize=10000&%2%3")
.arg(args.addonId) .arg(args.addonId)
@ -53,11 +53,16 @@ class FlameAPI : public NetworkModAPI {
}; };
public: public:
static auto getMappedModLoader(const ModLoaderType type) -> const ModLoaderType static auto getMappedModLoader(const ModLoaderTypes loaders) -> const int
{ {
// https://docs.curseforge.com/?http#tocS_ModLoaderType
if (loaders & Forge)
return 1;
if (loaders & Fabric)
return 4;
// TODO: remove this once Quilt drops official Fabric support // TODO: remove this once Quilt drops official Fabric support
if (type == Quilt) // NOTE: Most if not all Fabric mods should work *currently* if (loaders & Quilt) // NOTE: Most if not all Fabric mods should work *currently*
return Fabric; return 4; // Quilt would probably be 5
return type; return 0;
} }
}; };

View File

@ -28,30 +28,25 @@ class ModrinthAPI : public NetworkModAPI {
public: public:
inline auto getAuthorURL(const QString& name) const -> QString { return "https://modrinth.com/user/" + name; }; inline auto getAuthorURL(const QString& name) const -> QString { return "https://modrinth.com/user/" + name; };
static auto getModLoaderStrings(ModLoaderType type) -> const QStringList static auto getModLoaderStrings(const ModLoaderTypes types) -> const QStringList
{ {
QStringList l; QStringList l;
switch (type)
{
case Unspecified:
for (auto loader : {Forge, Fabric, Quilt}) for (auto loader : {Forge, Fabric, Quilt})
{
if ((types & loader) || types == Unspecified)
{ {
l << ModAPI::getModLoaderString(loader); l << ModAPI::getModLoaderString(loader);
} }
break;
case Quilt:
l << ModAPI::getModLoaderString(Fabric);
default:
l << ModAPI::getModLoaderString(type);
} }
if ((types & Quilt) && (~types & Fabric)) // Add Fabric if Quilt is in use, if Fabric isn't already there
l << ModAPI::getModLoaderString(Fabric);
return l; return l;
} }
static auto getModLoaderFilters(ModLoaderType type) -> const QString static auto getModLoaderFilters(ModLoaderTypes types) -> const QString
{ {
QStringList l; QStringList l;
for (auto loader : getModLoaderStrings(type)) for (auto loader : getModLoaderStrings(types))
{ {
l << QString("\"categories:%1\"").arg(loader); l << QString("\"categories:%1\"").arg(loader);
} }
@ -61,7 +56,7 @@ class ModrinthAPI : public NetworkModAPI {
private: private:
inline auto getModSearchURL(SearchArgs& args) const -> QString override inline auto getModSearchURL(SearchArgs& args) const -> QString override
{ {
if (!validateModLoader(args.mod_loader)) { if (!validateModLoaders(args.loaders)) {
qWarning() << "Modrinth only have Forge and Fabric-compatible mods!"; qWarning() << "Modrinth only have Forge and Fabric-compatible mods!";
return ""; return "";
} }
@ -76,7 +71,7 @@ class ModrinthAPI : public NetworkModAPI {
.arg(args.offset) .arg(args.offset)
.arg(args.search) .arg(args.search)
.arg(args.sorting) .arg(args.sorting)
.arg(getModLoaderFilters(args.mod_loader)) .arg(getModLoaderFilters(args.loaders))
.arg(getGameVersionsArray(args.versions)); .arg(getGameVersionsArray(args.versions));
}; };
@ -88,7 +83,7 @@ class ModrinthAPI : public NetworkModAPI {
"loaders=[\"%3\"]") "loaders=[\"%3\"]")
.arg(args.addonId) .arg(args.addonId)
.arg(getGameVersionsString(args.mcVersions)) .arg(getGameVersionsString(args.mcVersions))
.arg(getModLoaderStrings(args.loader).join("\",\"")); .arg(getModLoaderStrings(args.loaders).join("\",\""));
}; };
auto getGameVersionsArray(std::list<Version> mcVersions) const -> QString auto getGameVersionsArray(std::list<Version> mcVersions) const -> QString
@ -101,9 +96,9 @@ class ModrinthAPI : public NetworkModAPI {
return s.isEmpty() ? QString() : QString("[%1],").arg(s); return s.isEmpty() ? QString() : QString("[%1],").arg(s);
} }
inline auto validateModLoader(ModLoaderType modLoader) const -> bool inline auto validateModLoaders(ModLoaderTypes loaders) const -> bool
{ {
return modLoader == Unspecified || modLoader == Forge || modLoader == Fabric || modLoader == Quilt; return (loaders == Unspecified) || (loaders & (Forge | Fabric | Quilt));
} }
}; };

View File

@ -391,7 +391,7 @@ void ModFolderPage::on_actionInstall_mods_triggered()
return; //this is a null instance or a legacy instance return; //this is a null instance or a legacy instance
} }
auto profile = ((MinecraftInstance *)m_inst)->getPackProfile(); auto profile = ((MinecraftInstance *)m_inst)->getPackProfile();
if (profile->getModLoader() == ModAPI::Unspecified) { if (profile->getModLoaders() == ModAPI::Unspecified) {
QMessageBox::critical(this,tr("Error"),tr("Please install a mod loader first!")); QMessageBox::critical(this,tr("Error"),tr("Please install a mod loader first!"));
return; return;
} }

View File

@ -68,7 +68,7 @@ void ListModel::requestModVersions(ModPlatform::IndexedPack const& current)
{ {
auto profile = (dynamic_cast<MinecraftInstance*>((dynamic_cast<ModPage*>(parent()))->m_instance))->getPackProfile(); auto profile = (dynamic_cast<MinecraftInstance*>((dynamic_cast<ModPage*>(parent()))->m_instance))->getPackProfile();
m_parent->apiProvider()->getVersions(this, { current.addonId.toString(), getMineVersions(), profile->getModLoader() }); m_parent->apiProvider()->getVersions(this, { current.addonId.toString(), getMineVersions(), profile->getModLoaders() });
} }
void ListModel::performPaginatedSearch() void ListModel::performPaginatedSearch()
@ -76,7 +76,7 @@ void ListModel::performPaginatedSearch()
auto profile = (dynamic_cast<MinecraftInstance*>((dynamic_cast<ModPage*>(parent()))->m_instance))->getPackProfile(); auto profile = (dynamic_cast<MinecraftInstance*>((dynamic_cast<ModPage*>(parent()))->m_instance))->getPackProfile();
m_parent->apiProvider()->searchMods( m_parent->apiProvider()->searchMods(
this, { nextSearchOffset, currentSearchTerm, getSorts()[currentSort], profile->getModLoader(), getMineVersions() }); this, { nextSearchOffset, currentSearchTerm, getSorts()[currentSort], profile->getModLoaders(), getMineVersions() });
} }
void ListModel::refresh() void ListModel::refresh()

View File

@ -175,7 +175,7 @@ void ModPage::updateModVersions(int prev_count)
bool valid = false; bool valid = false;
for(auto& mcVer : m_filter->versions){ for(auto& mcVer : m_filter->versions){
//NOTE: Flame doesn't care about loader, so passing it changes nothing. //NOTE: Flame doesn't care about loader, so passing it changes nothing.
if (validateVersion(version, mcVer.toString(), packProfile->getModLoader())) { if (validateVersion(version, mcVer.toString(), packProfile->getModLoaders())) {
valid = true; valid = true;
break; break;
} }

View File

@ -37,7 +37,7 @@ class ModPage : public QWidget, public BasePage {
void retranslate() override; void retranslate() override;
auto shouldDisplay() const -> bool override = 0; auto shouldDisplay() const -> bool override = 0;
virtual auto validateVersion(ModPlatform::IndexedVersion& ver, QString mineVer, ModAPI::ModLoaderType loader = ModAPI::Unspecified) const -> bool = 0; virtual auto validateVersion(ModPlatform::IndexedVersion& ver, QString mineVer, ModAPI::ModLoaderTypes loaders = ModAPI::Unspecified) const -> bool = 0;
auto apiProvider() const -> const ModAPI* { return api.get(); }; auto apiProvider() const -> const ModAPI* { return api.get(); };
auto getFilter() const -> const std::shared_ptr<ModFilterWidget::Filter> { return m_filter; } auto getFilter() const -> const std::shared_ptr<ModFilterWidget::Filter> { return m_filter; }

View File

@ -61,9 +61,9 @@ FlameModPage::FlameModPage(ModDownloadDialog* dialog, BaseInstance* instance)
connect(ui->modSelectionButton, &QPushButton::clicked, this, &FlameModPage::onModSelected); connect(ui->modSelectionButton, &QPushButton::clicked, this, &FlameModPage::onModSelected);
} }
auto FlameModPage::validateVersion(ModPlatform::IndexedVersion& ver, QString mineVer, ModAPI::ModLoaderType loader) const -> bool auto FlameModPage::validateVersion(ModPlatform::IndexedVersion& ver, QString mineVer, ModAPI::ModLoaderTypes loaders) const -> bool
{ {
Q_UNUSED(loader); Q_UNUSED(loaders);
return ver.mcVersion.contains(mineVer); return ver.mcVersion.contains(mineVer);
} }

View File

@ -55,7 +55,7 @@ class FlameModPage : public ModPage {
inline auto debugName() const -> QString override { return "Flame"; } inline auto debugName() const -> QString override { return "Flame"; }
inline auto metaEntryBase() const -> QString override { return "FlameMods"; }; inline auto metaEntryBase() const -> QString override { return "FlameMods"; };
auto validateVersion(ModPlatform::IndexedVersion& ver, QString mineVer, ModAPI::ModLoaderType loader = ModAPI::Unspecified) const -> bool override; auto validateVersion(ModPlatform::IndexedVersion& ver, QString mineVer, ModAPI::ModLoaderTypes loaders = ModAPI::Unspecified) const -> bool override;
auto shouldDisplay() const -> bool override; auto shouldDisplay() const -> bool override;
}; };

View File

@ -61,9 +61,9 @@ ModrinthModPage::ModrinthModPage(ModDownloadDialog* dialog, BaseInstance* instan
connect(ui->modSelectionButton, &QPushButton::clicked, this, &ModrinthModPage::onModSelected); connect(ui->modSelectionButton, &QPushButton::clicked, this, &ModrinthModPage::onModSelected);
} }
auto ModrinthModPage::validateVersion(ModPlatform::IndexedVersion& ver, QString mineVer, ModAPI::ModLoaderType loader) const -> bool auto ModrinthModPage::validateVersion(ModPlatform::IndexedVersion& ver, QString mineVer, ModAPI::ModLoaderTypes loaders) const -> bool
{ {
auto loaderStrings = ModrinthAPI::getModLoaderStrings(loader); auto loaderStrings = ModrinthAPI::getModLoaderStrings(loaders);
auto loaderCompatible = false; auto loaderCompatible = false;
for (auto remoteLoader : ver.loaders) for (auto remoteLoader : ver.loaders)

View File

@ -55,7 +55,7 @@ class ModrinthModPage : public ModPage {
inline auto debugName() const -> QString override { return "Modrinth"; } inline auto debugName() const -> QString override { return "Modrinth"; }
inline auto metaEntryBase() const -> QString override { return "ModrinthPacks"; }; inline auto metaEntryBase() const -> QString override { return "ModrinthPacks"; };
auto validateVersion(ModPlatform::IndexedVersion& ver, QString mineVer, ModAPI::ModLoaderType loader = ModAPI::Unspecified) const -> bool override; auto validateVersion(ModPlatform::IndexedVersion& ver, QString mineVer, ModAPI::ModLoaderTypes loaders = ModAPI::Unspecified) const -> bool override;
auto shouldDisplay() const -> bool override; auto shouldDisplay() const -> bool override;
}; };