Skip to content

Commit

Permalink
curseforge api update
Browse files Browse the repository at this point in the history
Signed-off-by: kaniol-lck <375520360@qq.com>
  • Loading branch information
kaniol-lck committed Jun 5, 2024
1 parent a8c6af4 commit 6898b17
Show file tree
Hide file tree
Showing 16 changed files with 39 additions and 114 deletions.
5 changes: 4 additions & 1 deletion src/curseforge/curseforgeapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ CurseforgeAPI *CurseforgeAPI::api()
return &api;
}

Reply<QList<CurseforgeModInfo> > CurseforgeAPI::searchMods(int sectionId, const GameVersion &version, int index, const QString &searchFilter, int category, int sort)
Reply<QList<CurseforgeModInfo> > CurseforgeAPI::searchMods(int sectionId, const GameVersion &version, const ModLoaderType::Type &loaderType, int index, const QString &searchFilter, int category, int sort)
{
QUrl url = PREFIX + "/v1/mods/search";

Expand All @@ -40,6 +40,9 @@ Reply<QList<CurseforgeModInfo> > CurseforgeAPI::searchMods(int sectionId, const
//game version
if(version != GameVersion::Any)
urlQuery.addQueryItem("gameVersion", version);
//loader type
if(loaderType != ModLoaderType::Any)
urlQuery.addQueryItem("modLoaderType", QString::number(ModLoaderType::curseforge.indexOf(loaderType)));
//index
urlQuery.addQueryItem("index", QString::number(index));
//search page size
Expand Down
3 changes: 2 additions & 1 deletion src/curseforge/curseforgeapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <functional>
#include <QNetworkDiskCache>

#include "modloadertype.h"
#include "gameversion.h"
#include "curseforgemodinfo.h"
#include "curseforgecategoryinfo.h"
Expand All @@ -22,7 +23,7 @@ class CurseforgeAPI : public QObject
explicit CurseforgeAPI(QObject *parent = nullptr);
static CurseforgeAPI *api();

Reply<QList<CurseforgeModInfo> > searchMods(int sectionId, const GameVersion &version, int index, const QString &searchFilter, int category, int sort);
Reply<QList<CurseforgeModInfo> > searchMods(int sectionId, const GameVersion &version, const ModLoaderType::Type &loaderType, int index, const QString &searchFilter, int category, int sort);
Reply<int, CurseforgeFileInfo, QList<CurseforgeFileInfo> > getIdByFingerprint(const QString &fingerprint);
Reply<QString> getDescription(int id);
Reply<QString> getChangelog(int id, int FileID);
Expand Down
35 changes: 5 additions & 30 deletions src/curseforge/curseforgemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void CurseforgeManager::setSectionId(CurseforgeAPI::Section newSectionId)
void CurseforgeManager::getModList()
{
emit searchStarted();
searchModsGetter_ = api_.searchMods(sectionId_, currentGameVersion_, currentIndex_, currentName_, currentCategoryId_, currentSort_).asUnique();
searchModsGetter_ = api_.searchMods(sectionId_, currentGameVersion_, currentLoaderType_, currentIndex_, currentName_, currentCategoryId_, currentSort_).asUnique();
searchModsGetter_->setOnFinished(this, [=](const QList<CurseforgeModInfo> &infoList){
//new search
if(currentIndex_ == 0){
Expand Down Expand Up @@ -113,12 +113,10 @@ QVariant CurseforgeManagerModel::headerData(int section, Qt::Orientation orienta
return tr("Date Created");
case ReleaseDateColumn:
return tr("Date Released");
case LoaderTypesColumn:
return tr("Loader Types");
case DownloadCountColumn:
return tr("Download Count");
case PopularityScoreColumn:
return tr("Popularity Score");
case PopularityRankColumn:
return tr("Popularity Rank");
case SummaryColumn:
return tr("Summary");
break;
Expand Down Expand Up @@ -179,16 +177,10 @@ QVariant CurseforgeManagerModel::data(const QModelIndex &index, int role) const
return mod->modInfo().dateCreated().toString(QLocale().dateTimeFormat());
case ReleaseDateColumn:
return mod->modInfo().dateReleased().toString(QLocale().dateTimeFormat());
case LoaderTypesColumn:{
QStringList stringList;
for(auto &&loaderType : mod->modInfo().loaderTypes())
stringList << ModLoaderType::toString(loaderType);
return stringList.join(", ");
}
case DownloadCountColumn:
return mod->modInfo().downloadCount();
case PopularityScoreColumn:
return mod->modInfo().popularityScore();
case PopularityRankColumn:
return mod->modInfo().gamePopularityRank();
case SummaryColumn:
return mod->modInfo().summary();
break;
Expand Down Expand Up @@ -220,20 +212,3 @@ void CurseforgeManagerModel::setItemHeight(int newItemHeight)
{
itemHeight_ = newItemHeight;
}

CurseforgeManagerProxyModel::CurseforgeManagerProxyModel(QObject *parent) :
QSortFilterProxyModel(parent)
{}

void CurseforgeManagerProxyModel::setLoaderType(ModLoaderType::Type newLoaderType)
{
loaderType_ = newLoaderType;
}

bool CurseforgeManagerProxyModel::filterAcceptsRow(int source_row, const QModelIndex &) const
{
if(loaderType_ == ModLoaderType::Any) return true;
auto mod = sourceModel()->index(source_row, CurseforgeManagerModel::ModColumn).data(Qt::UserRole + 1).value<CurseforgeMod *>();
if(!mod) return false;
return mod->modInfo().loaderTypes().contains(loaderType_) || mod->modInfo().loaderTypes().isEmpty();
}
17 changes: 1 addition & 16 deletions src/curseforge/curseforgemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CurseforgeManagerModel : public QAbstractTableModel
Q_OBJECT
friend class CurseforgeManager;
public:
enum Column { ModColumn, NameColumn, ProjectIDColumn, SlugColumn, AuthorsColumn, CategoryColumn, WebsiteColumn, UpdateDateColumn, CreateDateColumn, ReleaseDateColumn, LoaderTypesColumn, DownloadCountColumn, PopularityScoreColumn, SummaryColumn };
enum Column { ModColumn, NameColumn, ProjectIDColumn, SlugColumn, AuthorsColumn, CategoryColumn, WebsiteColumn, UpdateDateColumn, CreateDateColumn, ReleaseDateColumn, DownloadCountColumn, PopularityRankColumn, SummaryColumn };
CurseforgeManagerModel(CurseforgeManager *manager);

QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
Expand Down Expand Up @@ -60,19 +60,4 @@ class CurseforgeManager : public ExploreManager
void getModList() override;
};

class CurseforgeManagerProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
CurseforgeManagerProxyModel(QObject *parent = nullptr);

void setLoaderType(ModLoaderType::Type newLoaderType);

protected:
virtual bool filterAcceptsRow(int source_row, const QModelIndex &) const override;

private:
ModLoaderType::Type loaderType_ = ModLoaderType::Any;
};

#endif // CURSEFORGEMANAGER_H
8 changes: 4 additions & 4 deletions src/curseforge/curseforgemodcacheinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ CurseforgeModCacheInfo CurseforgeModCacheInfo::fromVariant(const QVariant &varia
if(variant.toMap().contains("slug"))
info.slug_ = value(variant, "slug").toString();
info.iconUrl_ = value(variant, "iconUrl").toUrl();
info.popularityScore_ = value(variant, "popularityScore").toDouble();
info.gamePopularityRank_ = value(variant, "gamePopularityRank").toDouble();
return info;
}

Expand All @@ -32,7 +32,7 @@ QJsonObject CurseforgeModCacheInfo::toJsonObject() const
object.insert("summary", summary_);
object.insert("slug", slug_);
object.insert("iconUrl", iconUrl_.toString());
object.insert("popularityScore", popularityScore_);
object.insert("gamePopularityRank", gamePopularityRank_);
return object;
}

Expand Down Expand Up @@ -89,7 +89,7 @@ const QPixmap &CurseforgeModCacheInfo::icon() const
return icon_;
}

double CurseforgeModCacheInfo::popularityScore() const
double CurseforgeModCacheInfo::gamePopularityRank() const
{
return popularityScore_;
return gamePopularityRank_;
}
4 changes: 2 additions & 2 deletions src/curseforge/curseforgemodcacheinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CurseforgeModCacheInfo
const QString &slug() const;
const QUrl &iconUrl() const;
const QPixmap &icon() const;
double popularityScore() const;
double gamePopularityRank() const;

protected:
int id_;
Expand All @@ -34,7 +34,7 @@ class CurseforgeModCacheInfo
QString slug_;
QUrl iconUrl_;
QPixmap icon_;
double popularityScore_;
double gamePopularityRank_;
};


Expand Down
25 changes: 1 addition & 24 deletions src/curseforge/curseforgemodinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ CurseforgeModInfo CurseforgeModInfo::fromVariant(const QVariant &variant)
modInfo.dateModified_ = value(variant, "dateModified").toDateTime();
modInfo.dateCreated_ = value(variant, "dateCreated").toDateTime();
modInfo.dateReleased_ = value(variant, "dateReleased").toDateTime();
modInfo.popularityScore_ = value(variant, "popularityScore").toDouble();

for(auto &&str : value(variant, "modLoaders").toStringList())
modInfo.loaderTypes_ << ModLoaderType::fromString(str);
modInfo.gamePopularityRank_ = value(variant, "gamePopularityRank").toDouble();

//authors
for(auto &&author : value(variant, "authors").toList())
Expand Down Expand Up @@ -93,26 +90,6 @@ int CurseforgeModInfo::downloadCount() const
return downloadCount_;
}

const QList<ModLoaderType::Type> &CurseforgeModInfo::loaderTypes() const
{
return loaderTypes_;
}

bool CurseforgeModInfo::isFabricMod() const
{
return loaderTypes_.contains(ModLoaderType::Fabric);
}

bool CurseforgeModInfo::isForgeMod() const
{
return loaderTypes_.contains(ModLoaderType::Forge);
}

bool CurseforgeModInfo::isRiftMod() const
{
return loaderTypes_.contains(ModLoaderType::Rift);
}

const QList<CurseforgeFileInfo> &CurseforgeModInfo::latestFiles() const
{
return latestFileList_;
Expand Down
5 changes: 0 additions & 5 deletions src/curseforge/curseforgemodinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ class CurseforgeModInfo : public CurseforgeModCacheInfo, Tagable
const QUrl &iconUrl() const;
const QString &description() const;
int downloadCount() const;
const QList<ModLoaderType::Type> &loaderTypes() const;
bool isFabricMod() const;
bool isForgeMod() const;
bool isRiftMod() const;
const QList<CurseforgeFileInfo> &latestFiles() const;
std::optional<CurseforgeFileInfo> latestFileInfo(const GameVersion &version, ModLoaderType::Type &loaderType) const;
const QList<CurseforgeFileInfo> &allFileList() const;
Expand All @@ -65,7 +61,6 @@ class CurseforgeModInfo : public CurseforgeModCacheInfo, Tagable
QStringList authors_;
QString description_;
int downloadCount_;
QList<ModLoaderType::Type> loaderTypes_;
QList<CurseforgeFileInfo> latestFileList_;
QList<CurseforgeFileInfo> allFileList_;
int totalFileCount_ = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/local/localmodpath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ void LocalModPath::loadMods(bool autoLoaderType)

auto future = QtConcurrent::run([=]{
int count = 0;
QVector<int> modCount(3, 0);
QMap<ModLoaderType::Type, int> modCount;
emit loadStarted();
for(auto &file : modFileList){
modCount[file->loadInfo()]++;
emit loadProgress(++count, modFileList.size());
}
if(autoLoaderType){
if(auto iter = std::max_element(modCount.cbegin(), modCount.cend()); iter != modCount.cend()){
auto loaderType = ModLoaderType::local.at(iter - modCount.cbegin());
auto loaderType = iter.key();
info_.setLoaderType(loaderType);
emit infoUpdated();
}
Expand Down
12 changes: 8 additions & 4 deletions src/modloadertype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

ModLoaderType::Type ModLoaderType::fromString(const QString &str)
{
if(str.toLower() == "fabric")
if(str.toLower() == "cauldron")
return Type::Cauldron;
else if(str.toLower() == "fabric")
return Type::Fabric;
else if(str.toLower() == "forge")
return Type::Forge;
else if(str.toLower() == "liteloader")
return Type::LiteLoader;
else if(str.toLower() == "neoforge")
return Type::Neoforge;
return Type::NeoForge;
else if(str.toLower() == "quilt")
return Type::Quilt;
else if(str.toLower() == "rift")
Expand All @@ -25,13 +27,15 @@ QString ModLoaderType::toString(Type loaderType)
switch (loaderType) {
case Type::Any:
return QObject::tr("Any");
case Type::Cauldron:
return "Cauldron";
case Type::Fabric:
return "Fabric";
case Type::Forge:
return "Forge";
case Type::LiteLoader:
return "LiteLoader";
case Type::Neoforge:
case Type::NeoForge:
return "NeoForge";
case Type::Quilt:
return "Quilt";
Expand All @@ -50,7 +54,7 @@ QIcon ModLoaderType::icon(Type type)
return QIcon(":/image/forge.svg");
if(type == LiteLoader)
return QIcon(":/image/liteloader.png");
if(type == Neoforge)
if(type == NeoForge)
return QIcon(":/image/neoforge.png");
if(type == Quilt)
return QIcon(":/image/quilt.svg");
Expand Down
8 changes: 5 additions & 3 deletions src/modloadertype.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ namespace ModLoaderType
{
enum Type{
Any,
Cauldron, //is it alive?
Fabric,
Forge,
LiteLoader,
Neoforge,
NeoForge,
Quilt,
Rift
};
Expand All @@ -22,8 +23,9 @@ QString toString(Type loaderType);

QIcon icon(Type type);

const QList<Type> curseforge{ Any, Fabric, Forge/*, Rift*/ };
const QList<Type> modrinth{ Any, Fabric, Forge, LiteLoader, Neoforge, Quilt, Rift };
const QList<Type> curseforge{ Any, Forge, Cauldron, LiteLoader, Fabric, Quilt, NeoForge };
const QList<Type> modrinth{ Any, Fabric, Forge, LiteLoader, NeoForge, Quilt, Rift };
const QList<Type> optifine{ Any, Fabric, Forge };
const QList<Type> replay{ Any, Fabric, Forge };
const QList<Type> local{ Any, Fabric, Forge };

Expand Down
8 changes: 3 additions & 5 deletions src/ui/curseforge/curseforgemodbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ CurseforgeModBrowser::CurseforgeModBrowser(QWidget *parent, LocalMod *mod, Curse
ExploreBrowser(parent, QIcon(":/image/curseforge.svg"), "Curseforge", QUrl("https://www.curseforge.com/minecraft/mc-mods")),
ui(new Ui::CurseforgeModBrowser),
manager_(new CurseforgeManager(this, sectionId)),
proxyModel_(new CurseforgeManagerProxyModel(this)),
sectionId_(sectionId),
infoWidget_(new CurseforgeModInfoWidget(this)),
fileListWidget_(new CurseforgeFileListWidget(this)),
Expand All @@ -46,8 +45,7 @@ CurseforgeModBrowser::CurseforgeModBrowser(QWidget *parent, LocalMod *mod, Curse
fileListWidget_->hide();
ui->setupUi(this);
ui->menu_Curseforge->insertActions(ui->menu_Curseforge->actions().first(), menu_->actions());
proxyModel_->setSourceModel(manager_->model());
initUi(manager_, proxyModel_);
initUi(manager_);
treeViewIndexWidgetColumn_ = CurseforgeManagerModel::CategoryColumn;

for(auto &&toolBar : findChildren<QToolBar *>())
Expand Down Expand Up @@ -298,8 +296,8 @@ QDialog *CurseforgeModBrowser::getDialog(const QModelIndex &index)

void CurseforgeModBrowser::on_loaderSelect_currentIndexChanged(int index)
{
proxyModel_->setLoaderType(ModLoaderType::curseforge.at(index));
proxyModel_->invalidate();
currentLoaderType_ = ModLoaderType::curseforge.at(index);
search();
updateStatusText();
}

Expand Down
1 change: 0 additions & 1 deletion src/ui/curseforge/curseforgemodbrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ private slots:
private:
Ui::CurseforgeModBrowser *ui;
CurseforgeManager *manager_;
CurseforgeManagerProxyModel *proxyModel_;
CurseforgeAPI::Section sectionId_;
CurseforgeModInfoWidget *infoWidget_;
CurseforgeFileListWidget *fileListWidget_;
Expand Down
14 changes: 0 additions & 14 deletions src/ui/curseforge/curseforgemoditemwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,6 @@ CurseforgeModItemWidget::CurseforgeModItemWidget(CurseforgeModBrowser *parent, C
ui->createDateTime->setText(tr("Created"));
ui->createDateTime->setDateTime(mod->modInfo().dateCreated());

//loader type
for(auto &&loaderType : mod_->modInfo().loaderTypes()){
auto label = new QLabel(this);
label->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
if(loaderType == ModLoaderType::Fabric)
label->setText(QString(R"(<img src=":/image/fabric.png" height="22" width="22"/>)"));
else if(loaderType == ModLoaderType::Forge)
label->setText(QString(R"(<img src=":/image/forge.svg" height="22" width="22"/>)"));
else
label->setText(ModLoaderType::toString(loaderType));
label->setToolTip(ModLoaderType::toString(loaderType));
ui->loadersLayout->addWidget(label);
}

if(defaultFileInfo_.has_value())
ui->downloadSpeedText->setText(sizeConvert(defaultDownload.value().size()) + "\n"
+ numberConvert(mod->modInfo().downloadCount(), "", 3, 1000) + tr(" Downloads"));
Expand Down
2 changes: 1 addition & 1 deletion src/ui/modrinth/modrinthmoditemwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ ModrinthModItemWidget::ModrinthModItemWidget(ModrinthModBrowser *parent, Modrint
label->setText(QString(R"(<img src=":/image/forge.svg" height="22" width="22"/>)"));
else if(loaderType == ModLoaderType::LiteLoader)
label->setText(QString(R"(<img src=":/image/liteloader.png" height="22" width="22"/>)"));
else if(loaderType == ModLoaderType::Neoforge)
else if(loaderType == ModLoaderType::NeoForge)
label->setText(QString(R"(<img src=":/image/neoforge.png" height="22" width="22"/>)"));
else if(loaderType == ModLoaderType::Quilt)
label->setText(QString(R"(<img src=":/image/quilt.svg" height="22" width="22"/>)"));
Expand Down
2 changes: 1 addition & 1 deletion src/ui/replay/replaymodbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void ReplayModBrowser::refresh()
void ReplayModBrowser::searchModByPathInfo(LocalModPath *path)
{
ui->versionSelect->setCurrentText(path->info().gameVersion());
ui->loaderSelect->setCurrentIndex(ModLoaderType::curseforge.indexOf(path->info().loaderType()));
ui->loaderSelect->setCurrentIndex(ModLoaderType::optifine.indexOf(path->info().loaderType()));
downloadPathSelectMenu_->setDownloadPath(path);
}

Expand Down

0 comments on commit 6898b17

Please sign in to comment.