Added Dependency API

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2023-04-14 18:59:31 +03:00
parent 11f8d25d94
commit 5655a33515
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
5 changed files with 93 additions and 32 deletions

View File

@ -111,6 +111,24 @@ class ResourceAPI {
std::function<void(QJsonDocument&, ModPlatform::IndexedPack)> on_succeed; std::function<void(QJsonDocument&, ModPlatform::IndexedPack)> on_succeed;
}; };
struct DependencySearchArgs {
ModPlatform::Dependency dependency;
Version mcVersion;
ModLoaderTypes loader;
DependencySearchArgs(DependencySearchArgs const&) = default;
void operator=(DependencySearchArgs other)
{
dependency = other.dependency;
mcVersion = other.mcVersion;
loader = other.loader;
}
};
struct DependencySearchCallbacks {
std::function<void(QJsonDocument&, ModPlatform::Dependency)> on_succeed;
};
public: public:
/** Gets a list of available sorting methods for this API. */ /** Gets a list of available sorting methods for this API. */
[[nodiscard]] virtual auto getSortingMethods() const -> QList<SortingMethod> = 0; [[nodiscard]] virtual auto getSortingMethods() const -> QList<SortingMethod> = 0;
@ -143,6 +161,12 @@ class ResourceAPI {
return nullptr; return nullptr;
} }
[[nodiscard]] virtual Task::Ptr getDependencyVersion(DependencySearchArgs&&, DependencySearchCallbacks&&) const
{
qWarning() << "TODO";
return nullptr;
}
static auto getModLoaderString(ModLoaderType type) -> const QString static auto getModLoaderString(ModLoaderType type) -> const QString
{ {
switch (type) { switch (type) {

View File

@ -48,7 +48,8 @@ class FlameAPI : public NetworkResourceAPI {
private: private:
[[nodiscard]] std::optional<QString> getSearchURL(SearchArgs const& args) const override [[nodiscard]] std::optional<QString> getSearchURL(SearchArgs const& args) const override
{ {
auto gameVersionStr = args.versions.has_value() ? QString("gameVersion=%1").arg(args.versions.value().front().toString()) : QString(); auto gameVersionStr =
args.versions.has_value() ? QString("gameVersion=%1").arg(args.versions.value().front().toString()) : QString();
QStringList get_arguments; QStringList get_arguments;
get_arguments.append(QString("classId=%1").arg(getClassId(args.type))); get_arguments.append(QString("classId=%1").arg(getClassId(args.type)));
@ -83,4 +84,12 @@ class FlameAPI : public NetworkResourceAPI {
return url + get_parameters.join('&'); return url + get_parameters.join('&');
}; };
[[nodiscard]] std::optional<QString> getDependecyURL(DependencySearchArgs const& args) const override
{
return QString("https://api.curseforge.com/v1/mods/%1/files?pageSize=10000&gameVersion=%2&modLoaderType=%")
.arg(args.dependency.addonId.toString())
.arg(args.mcVersion.toString())
.arg(getMappedModLoader(args.loader));
};
}; };

View File

@ -47,9 +47,7 @@ Task::Ptr NetworkResourceAPI::searchProjects(SearchArgs&& args, SearchCallbacks&
callbacks.on_fail(reason, network_error_code); callbacks.on_fail(reason, network_error_code);
}); });
QObject::connect(netJob.get(), &NetJob::aborted, [=]{ QObject::connect(netJob.get(), &NetJob::aborted, [=] { callbacks.on_abort(); });
callbacks.on_abort();
});
return netJob; return netJob;
} }
@ -101,9 +99,7 @@ Task::Ptr NetworkResourceAPI::getProjectVersions(VersionSearchArgs&& args, Versi
callbacks.on_succeed(doc, args.pack); callbacks.on_succeed(doc, args.pack);
}); });
QObject::connect(netJob.get(), &NetJob::finished, [response] { QObject::connect(netJob.get(), &NetJob::finished, [response] { delete response; });
delete response;
});
return netJob; return netJob;
} }
@ -120,9 +116,38 @@ Task::Ptr NetworkResourceAPI::getProject(QString addonId, QByteArray* response)
netJob->addNetAction(Net::Download::makeByteArray(QUrl(project_url), response)); netJob->addNetAction(Net::Download::makeByteArray(QUrl(project_url), response));
QObject::connect(netJob.get(), &NetJob::finished, [response] { QObject::connect(netJob.get(), &NetJob::finished, [response] { delete response; });
delete response;
});
return netJob; return netJob;
} }
Task::Ptr NetworkResourceAPI::getDependencyVersion(DependencySearchArgs&& args, DependencySearchCallbacks&& callbacks) const
{
auto versions_url_optional = getDependecyURL(args);
if (!versions_url_optional.has_value())
return nullptr;
auto versions_url = versions_url_optional.value();
auto netJob = makeShared<NetJob>(QString("%1::Dependecy").arg(args.dependency.addonId.toString()), APPLICATION->network());
auto response = new QByteArray();
netJob->addNetAction(Net::Download::makeByteArray(versions_url, response));
QObject::connect(netJob.get(), &NetJob::succeeded, [=] {
QJsonParseError parse_error{};
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
if (parse_error.error != QJsonParseError::NoError) {
qWarning() << "Error while parsing JSON response for getting versions at " << parse_error.offset
<< " reason: " << parse_error.errorString();
qWarning() << *response;
return;
}
callbacks.on_succeed(doc, args.dependency);
});
QObject::connect(netJob.get(), &NetJob::finished, [response] { delete response; });
return netJob;
};

View File

@ -14,9 +14,11 @@ class NetworkResourceAPI : public ResourceAPI {
Task::Ptr getProjectInfo(ProjectInfoArgs&&, ProjectInfoCallbacks&&) const override; Task::Ptr getProjectInfo(ProjectInfoArgs&&, ProjectInfoCallbacks&&) const override;
Task::Ptr getProjectVersions(VersionSearchArgs&&, VersionSearchCallbacks&&) const override; Task::Ptr getProjectVersions(VersionSearchArgs&&, VersionSearchCallbacks&&) const override;
Task::Ptr getDependencyVersion(DependencySearchArgs&&, DependencySearchCallbacks&&) const override;
protected: protected:
[[nodiscard]] virtual auto getSearchURL(SearchArgs const& args) const -> std::optional<QString> = 0; [[nodiscard]] virtual auto getSearchURL(SearchArgs const& args) const -> std::optional<QString> = 0;
[[nodiscard]] virtual auto getInfoURL(QString const& id) const -> std::optional<QString> = 0; [[nodiscard]] virtual auto getInfoURL(QString const& id) const -> std::optional<QString> = 0;
[[nodiscard]] virtual auto getVersionsURL(VersionSearchArgs const& args) const -> std::optional<QString> = 0; [[nodiscard]] virtual auto getVersionsURL(VersionSearchArgs const& args) const -> std::optional<QString> = 0;
[[nodiscard]] virtual auto getDependecyURL(DependencySearchArgs const& args) const -> std::optional<QString> = 0;
}; };

View File

@ -12,13 +12,9 @@
class ModrinthAPI : public NetworkResourceAPI { class ModrinthAPI : public NetworkResourceAPI {
public: public:
auto currentVersion(QString hash, auto currentVersion(QString hash, QString hash_format, QByteArray* response) -> Task::Ptr;
QString hash_format,
QByteArray* response) -> Task::Ptr;
auto currentVersions(const QStringList& hashes, auto currentVersions(const QStringList& hashes, QString hash_format, QByteArray* response) -> Task::Ptr;
QString hash_format,
QByteArray* response) -> Task::Ptr;
auto latestVersion(QString hash, auto latestVersion(QString hash,
QString hash_format, QString hash_format,
@ -55,8 +51,7 @@ class ModrinthAPI : public NetworkResourceAPI {
static auto getModLoaderFilters(ModLoaderTypes types) -> const QString static auto getModLoaderFilters(ModLoaderTypes types) -> const QString
{ {
QStringList l; QStringList l;
for (auto loader : getModLoaderStrings(types)) for (auto loader : getModLoaderStrings(types)) {
{
l << QString("\"categories:%1\"").arg(loader); l << QString("\"categories:%1\"").arg(loader);
} }
return l.join(','); return l.join(',');
@ -146,9 +141,15 @@ class ModrinthAPI : public NetworkResourceAPI {
return s.isEmpty() ? QString() : s; return s.isEmpty() ? QString() : s;
} }
inline auto validateModLoaders(ModLoaderTypes loaders) const -> bool inline auto validateModLoaders(ModLoaderTypes loaders) const -> bool { return loaders & (Forge | Fabric | Quilt); }
{
return loaders & (Forge | Fabric | Quilt);
}
[[nodiscard]] std::optional<QString> getDependecyURL(DependencySearchArgs const& args) const override
{
return args.dependency.version.length() != 0 ? QString("%1/version/%2").arg(BuildConfig.MODRINTH_PROD_URL, args.dependency.version)
: QString("%1/project/%2/version?game_versions=[\"%1\"]&loaders=[\"%1\"]")
.arg(BuildConfig.MODRINTH_PROD_URL)
.arg(args.dependency.addonId.toString())
.arg(args.mcVersion.toString())
.arg(getModLoaderStrings(args.loader).join("\",\""));
};
}; };