Added Dependency API
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
parent
11f8d25d94
commit
5655a33515
@ -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) {
|
||||||
|
@ -41,14 +41,15 @@ class FlameAPI : public NetworkResourceAPI {
|
|||||||
return 4;
|
return 4;
|
||||||
// TODO: remove this once Quilt drops official Fabric support
|
// TODO: remove this once Quilt drops official Fabric support
|
||||||
if (loaders & 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 4; // Quilt would probably be 5
|
return 4; // Quilt would probably be 5
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
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)));
|
||||||
@ -73,7 +74,7 @@ class FlameAPI : public NetworkResourceAPI {
|
|||||||
|
|
||||||
[[nodiscard]] std::optional<QString> getVersionsURL(VersionSearchArgs const& args) const override
|
[[nodiscard]] std::optional<QString> getVersionsURL(VersionSearchArgs const& args) const override
|
||||||
{
|
{
|
||||||
QString url{QString("https://api.curseforge.com/v1/mods/%1/files?pageSize=10000&").arg(args.pack.addonId.toString())};
|
QString url{ QString("https://api.curseforge.com/v1/mods/%1/files?pageSize=10000&").arg(args.pack.addonId.toString()) };
|
||||||
|
|
||||||
QStringList get_parameters;
|
QStringList get_parameters;
|
||||||
if (args.mcVersions.has_value())
|
if (args.mcVersions.has_value())
|
||||||
@ -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));
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
@ -24,7 +24,7 @@ Task::Ptr NetworkResourceAPI::searchProjects(SearchArgs&& args, SearchCallbacks&
|
|||||||
|
|
||||||
netJob->addNetAction(Net::Download::makeByteArray(QUrl(search_url), response));
|
netJob->addNetAction(Net::Download::makeByteArray(QUrl(search_url), response));
|
||||||
|
|
||||||
QObject::connect(netJob.get(), &NetJob::succeeded, [=]{
|
QObject::connect(netJob.get(), &NetJob::succeeded, [=] {
|
||||||
QJsonParseError parse_error{};
|
QJsonParseError parse_error{};
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
||||||
if (parse_error.error != QJsonParseError::NoError) {
|
if (parse_error.error != QJsonParseError::NoError) {
|
||||||
@ -40,16 +40,14 @@ Task::Ptr NetworkResourceAPI::searchProjects(SearchArgs&& args, SearchCallbacks&
|
|||||||
callbacks.on_succeed(doc);
|
callbacks.on_succeed(doc);
|
||||||
});
|
});
|
||||||
|
|
||||||
QObject::connect(netJob.get(), &NetJob::failed, [=](QString reason){
|
QObject::connect(netJob.get(), &NetJob::failed, [=](QString reason) {
|
||||||
int network_error_code = -1;
|
int network_error_code = -1;
|
||||||
if (auto* failed_action = netJob->getFailedActions().at(0); failed_action && failed_action->m_reply)
|
if (auto* failed_action = netJob->getFailedActions().at(0); failed_action && failed_action->m_reply)
|
||||||
network_error_code = failed_action->m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
network_error_code = failed_action->m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
|
|
||||||
callbacks.on_fail(reason, network_error_code);
|
callbacks.on_fail(reason, network_error_code);
|
||||||
});
|
|
||||||
QObject::connect(netJob.get(), &NetJob::aborted, [=]{
|
|
||||||
callbacks.on_abort();
|
|
||||||
});
|
});
|
||||||
|
QObject::connect(netJob.get(), &NetJob::aborted, [=] { 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;
|
||||||
|
};
|
||||||
|
@ -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;
|
||||||
};
|
};
|
||||||
|
@ -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,
|
||||||
@ -28,8 +24,8 @@ class ModrinthAPI : public NetworkResourceAPI {
|
|||||||
|
|
||||||
auto latestVersions(const QStringList& hashes,
|
auto latestVersions(const QStringList& hashes,
|
||||||
QString hash_format,
|
QString hash_format,
|
||||||
std::optional<std::list<Version>> mcVersions,
|
std::optional<std::list<Version>> mcVersions,
|
||||||
std::optional<ModLoaderTypes> loaders,
|
std::optional<ModLoaderTypes> loaders,
|
||||||
QByteArray* response) -> Task::Ptr;
|
QByteArray* response) -> Task::Ptr;
|
||||||
|
|
||||||
Task::Ptr getProjects(QStringList addonIds, QByteArray* response) const override;
|
Task::Ptr getProjects(QStringList addonIds, QByteArray* response) const override;
|
||||||
@ -42,7 +38,7 @@ class ModrinthAPI : public NetworkResourceAPI {
|
|||||||
static auto getModLoaderStrings(const ModLoaderTypes types) -> const QStringList
|
static auto getModLoaderStrings(const ModLoaderTypes types) -> const QStringList
|
||||||
{
|
{
|
||||||
QStringList l;
|
QStringList l;
|
||||||
for (auto loader : {Forge, Fabric, Quilt}) {
|
for (auto loader : { Forge, Fabric, Quilt }) {
|
||||||
if (types & loader) {
|
if (types & loader) {
|
||||||
l << getModLoaderString(loader);
|
l << getModLoaderString(loader);
|
||||||
}
|
}
|
||||||
@ -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(',');
|
||||||
@ -139,16 +134,22 @@ class ModrinthAPI : public NetworkResourceAPI {
|
|||||||
auto getGameVersionsArray(std::list<Version> mcVersions) const -> QString
|
auto getGameVersionsArray(std::list<Version> mcVersions) const -> QString
|
||||||
{
|
{
|
||||||
QString s;
|
QString s;
|
||||||
for(auto& ver : mcVersions){
|
for (auto& ver : mcVersions) {
|
||||||
s += QString("\"versions:%1\",").arg(ver.toString());
|
s += QString("\"versions:%1\",").arg(ver.toString());
|
||||||
}
|
}
|
||||||
s.remove(s.length() - 1, 1); //remove last comma
|
s.remove(s.length() - 1, 1); // remove last comma
|
||||||
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("\",\""));
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user