2022-06-04 01:26:26 +01:00
|
|
|
#include "FlameCheckUpdate.h"
|
|
|
|
#include "FlameAPI.h"
|
|
|
|
#include "FlameModIndex.h"
|
|
|
|
|
|
|
|
#include <MurmurHash2.h>
|
2023-06-02 22:04:06 +01:00
|
|
|
#include <memory>
|
2022-06-04 01:26:26 +01:00
|
|
|
|
|
|
|
#include "FileSystem.h"
|
|
|
|
#include "Json.h"
|
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
#include "ResourceDownloadTask.h"
|
|
|
|
|
|
|
|
#include "minecraft/mod/ModFolderModel.h"
|
|
|
|
#include "minecraft/mod/ResourceFolderModel.h"
|
2022-06-04 01:26:26 +01:00
|
|
|
|
|
|
|
static FlameAPI api;
|
|
|
|
|
|
|
|
bool FlameCheckUpdate::abort()
|
|
|
|
{
|
|
|
|
m_was_aborted = true;
|
|
|
|
if (m_net_job)
|
|
|
|
return m_net_job->abort();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ModPlatform::IndexedPack getProjectInfo(ModPlatform::IndexedVersion& ver_info)
|
|
|
|
{
|
|
|
|
ModPlatform::IndexedPack pack;
|
|
|
|
|
|
|
|
QEventLoop loop;
|
|
|
|
|
|
|
|
auto get_project_job = new NetJob("Flame::GetProjectJob", APPLICATION->network());
|
|
|
|
|
2023-06-23 17:13:52 +01:00
|
|
|
auto response = std::make_shared<QByteArray>();
|
2022-06-04 01:26:26 +01:00
|
|
|
auto url = QString("https://api.curseforge.com/v1/mods/%1").arg(ver_info.addonId.toString());
|
|
|
|
auto dl = Net::Download::makeByteArray(url, response);
|
|
|
|
get_project_job->addNetAction(dl);
|
|
|
|
|
|
|
|
QObject::connect(get_project_job, &NetJob::succeeded, [response, &pack]() {
|
|
|
|
QJsonParseError parse_error{};
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response from FlameCheckUpdate at " << parse_error.offset
|
|
|
|
<< " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
auto doc_obj = Json::requireObject(doc);
|
|
|
|
auto data_obj = Json::requireObject(doc_obj, "data");
|
|
|
|
FlameMod::loadIndexedPack(pack, data_obj);
|
|
|
|
} catch (Json::JsonException& e) {
|
|
|
|
qWarning() << e.cause();
|
|
|
|
qDebug() << doc;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
QObject::connect(get_project_job, &NetJob::finished, [&loop, get_project_job] {
|
|
|
|
get_project_job->deleteLater();
|
|
|
|
loop.quit();
|
|
|
|
});
|
|
|
|
|
|
|
|
get_project_job->start();
|
|
|
|
loop.exec();
|
|
|
|
|
|
|
|
return pack;
|
|
|
|
}
|
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
ModPlatform::IndexedVersion getFileInfo(int addonId, int fileId)
|
2022-06-04 01:26:26 +01:00
|
|
|
{
|
2022-06-16 15:45:29 +01:00
|
|
|
ModPlatform::IndexedVersion ver;
|
2022-06-04 01:26:26 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
QEventLoop loop;
|
2022-06-04 01:26:26 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
auto get_file_info_job = new NetJob("Flame::GetFileInfoJob", APPLICATION->network());
|
2022-06-04 01:26:26 +01:00
|
|
|
|
2023-06-23 17:13:52 +01:00
|
|
|
auto response = std::make_shared<QByteArray>();
|
2022-06-16 15:45:29 +01:00
|
|
|
auto url = QString("https://api.curseforge.com/v1/mods/%1/files/%2").arg(QString::number(addonId), QString::number(fileId));
|
|
|
|
auto dl = Net::Download::makeByteArray(url, response);
|
|
|
|
get_file_info_job->addNetAction(dl);
|
2022-06-04 01:26:26 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
QObject::connect(get_file_info_job, &NetJob::succeeded, [response, &ver]() {
|
|
|
|
QJsonParseError parse_error{};
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response from FlameCheckUpdate at " << parse_error.offset
|
|
|
|
<< " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
2022-06-04 01:26:26 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
try {
|
|
|
|
auto doc_obj = Json::requireObject(doc);
|
|
|
|
auto data_obj = Json::requireObject(doc_obj, "data");
|
|
|
|
ver = FlameMod::loadIndexedPackVersion(data_obj);
|
|
|
|
} catch (Json::JsonException& e) {
|
|
|
|
qWarning() << e.cause();
|
|
|
|
qDebug() << doc;
|
2022-06-04 01:26:26 +01:00
|
|
|
}
|
2022-06-16 15:45:29 +01:00
|
|
|
});
|
2022-06-04 01:26:26 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
QObject::connect(get_file_info_job, &NetJob::finished, [&loop, get_file_info_job] {
|
|
|
|
get_file_info_job->deleteLater();
|
|
|
|
loop.quit();
|
|
|
|
});
|
2022-06-04 01:26:26 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
get_file_info_job->start();
|
|
|
|
loop.exec();
|
2022-06-04 01:26:26 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
return ver;
|
|
|
|
}
|
2022-06-04 01:26:26 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
/* Check for update:
|
|
|
|
* - Get latest version available
|
|
|
|
* - Compare hash of the latest version with the current hash
|
|
|
|
* - If equal, no updates, else, there's updates, so add to the list
|
|
|
|
* */
|
|
|
|
void FlameCheckUpdate::executeTask()
|
|
|
|
{
|
|
|
|
setStatus(tr("Preparing mods for CurseForge..."));
|
2022-06-04 01:26:26 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
int i = 0;
|
2022-06-26 18:17:15 +01:00
|
|
|
for (auto* mod : m_mods) {
|
|
|
|
if (!mod->enabled()) {
|
2022-06-20 12:55:35 +01:00
|
|
|
emit checkFailed(mod, tr("Disabled mods won't be updated, to prevent mod duplication issues!"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-07-19 17:03:45 +01:00
|
|
|
setStatus(tr("Getting API response from CurseForge for '%1'...").arg(mod->name()));
|
2022-06-16 15:45:29 +01:00
|
|
|
setProgress(i++, m_mods.size());
|
|
|
|
|
2023-06-02 22:04:06 +01:00
|
|
|
auto latest_ver = api.getLatestVersion({ { mod->metadata()->project_id.toString() }, m_game_versions, m_loaders });
|
2022-06-04 01:26:26 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
// Check if we were aborted while getting the latest version
|
|
|
|
if (m_was_aborted) {
|
|
|
|
aborted();
|
2022-06-04 01:26:26 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-26 18:17:15 +01:00
|
|
|
setStatus(tr("Parsing the API response from CurseForge for '%1'...").arg(mod->name()));
|
2022-06-04 01:26:26 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
if (!latest_ver.addonId.isValid()) {
|
|
|
|
emit checkFailed(mod, tr("No valid version found for this mod. It's probably unavailable for the current game "
|
|
|
|
"version / mod loader."));
|
|
|
|
continue;
|
2022-06-04 01:26:26 +01:00
|
|
|
}
|
|
|
|
|
2022-06-26 18:17:15 +01:00
|
|
|
if (latest_ver.downloadUrl.isEmpty() && latest_ver.fileId != mod->metadata()->file_id) {
|
2022-06-16 15:45:29 +01:00
|
|
|
auto pack = getProjectInfo(latest_ver);
|
|
|
|
auto recover_url = QString("%1/download/%2").arg(pack.websiteUrl, latest_ver.fileId.toString());
|
2022-07-19 17:03:45 +01:00
|
|
|
emit checkFailed(mod, tr("Mod has a new update available, but is not downloadable using CurseForge."), recover_url);
|
2022-06-04 01:26:26 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-06-04 01:26:26 +01:00
|
|
|
|
2022-06-26 18:17:15 +01:00
|
|
|
if (!latest_ver.hash.isEmpty() && (mod->metadata()->hash != latest_ver.hash || mod->status() == ModStatus::NotInstalled)) {
|
2022-06-16 15:45:29 +01:00
|
|
|
// Fake pack with the necessary info to pass to the download task :)
|
2023-06-02 22:04:06 +01:00
|
|
|
auto pack = std::make_shared<ModPlatform::IndexedPack>();
|
|
|
|
pack->name = mod->name();
|
|
|
|
pack->slug = mod->metadata()->slug;
|
|
|
|
pack->addonId = mod->metadata()->project_id;
|
|
|
|
pack->websiteUrl = mod->homeurl();
|
2022-06-26 18:17:15 +01:00
|
|
|
for (auto& author : mod->authors())
|
2023-06-02 22:04:06 +01:00
|
|
|
pack->authors.append({ author });
|
|
|
|
pack->description = mod->description();
|
|
|
|
pack->provider = ModPlatform::ResourceProvider::FLAME;
|
2022-06-16 15:45:29 +01:00
|
|
|
|
2022-06-26 18:17:15 +01:00
|
|
|
auto old_version = mod->version();
|
|
|
|
if (old_version.isEmpty() && mod->status() != ModStatus::NotInstalled) {
|
|
|
|
auto current_ver = getFileInfo(latest_ver.addonId.toInt(), mod->metadata()->file_id.toInt());
|
2022-06-16 15:45:29 +01:00
|
|
|
old_version = current_ver.version;
|
|
|
|
}
|
2022-06-04 01:26:26 +01:00
|
|
|
|
2023-01-24 19:52:09 +00:00
|
|
|
auto download_task = makeShared<ResourceDownloadTask>(pack, latest_ver, m_mods_folder);
|
2023-06-02 22:04:06 +01:00
|
|
|
m_updatable.emplace_back(pack->name, mod->metadata()->hash, old_version, latest_ver.version,
|
2022-06-16 15:45:29 +01:00
|
|
|
api.getModFileChangelog(latest_ver.addonId.toInt(), latest_ver.fileId.toInt()),
|
2022-11-25 12:23:46 +00:00
|
|
|
ModPlatform::ResourceProvider::FLAME, download_task);
|
2022-06-16 15:45:29 +01:00
|
|
|
}
|
|
|
|
}
|
2022-06-04 01:26:26 +01:00
|
|
|
|
|
|
|
emitSucceeded();
|
|
|
|
}
|