2022-06-04 05:56:26 +05:30
|
|
|
#include "FlameCheckUpdate.h"
|
|
|
|
#include "FlameAPI.h"
|
|
|
|
#include "FlameModIndex.h"
|
|
|
|
|
|
|
|
#include <MurmurHash2.h>
|
|
|
|
|
|
|
|
#include "FileSystem.h"
|
|
|
|
#include "Json.h"
|
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
#include "ResourceDownloadTask.h"
|
|
|
|
|
|
|
|
#include "minecraft/mod/ModFolderModel.h"
|
|
|
|
#include "minecraft/mod/ResourceFolderModel.h"
|
2022-06-04 05:56:26 +05:30
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
auto response = new QByteArray();
|
|
|
|
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 20:15:29 +05:30
|
|
|
ModPlatform::IndexedVersion getFileInfo(int addonId, int fileId)
|
2022-06-04 05:56:26 +05:30
|
|
|
{
|
2022-06-16 20:15:29 +05:30
|
|
|
ModPlatform::IndexedVersion ver;
|
2022-06-04 05:56:26 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
QEventLoop loop;
|
2022-06-04 05:56:26 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
auto get_file_info_job = new NetJob("Flame::GetFileInfoJob", APPLICATION->network());
|
2022-06-04 05:56:26 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
auto response = new QByteArray();
|
|
|
|
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 05:56:26 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
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 05:56:26 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
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 05:56:26 +05:30
|
|
|
}
|
2022-06-16 20:15:29 +05:30
|
|
|
});
|
2022-06-04 05:56:26 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
QObject::connect(get_file_info_job, &NetJob::finished, [&loop, get_file_info_job] {
|
|
|
|
get_file_info_job->deleteLater();
|
|
|
|
loop.quit();
|
|
|
|
});
|
2022-06-04 05:56:26 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
get_file_info_job->start();
|
|
|
|
loop.exec();
|
2022-06-04 05:56:26 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
return ver;
|
|
|
|
}
|
2022-06-04 05:56:26 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
/* 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 05:56:26 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
int i = 0;
|
2022-06-26 22:47:15 +05:30
|
|
|
for (auto* mod : m_mods) {
|
|
|
|
if (!mod->enabled()) {
|
2022-06-20 17:25:35 +05:30
|
|
|
emit checkFailed(mod, tr("Disabled mods won't be updated, to prevent mod duplication issues!"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:33:45 +05:30
|
|
|
setStatus(tr("Getting API response from CurseForge for '%1'...").arg(mod->name()));
|
2022-06-16 20:15:29 +05:30
|
|
|
setProgress(i++, m_mods.size());
|
|
|
|
|
2022-12-19 00:11:46 +05:30
|
|
|
ModPlatform::IndexedPack pack{ mod->metadata()->project_id.toString() };
|
|
|
|
auto latest_ver = api.getLatestVersion({ pack, m_game_versions, m_loaders });
|
2022-06-04 05:56:26 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
// Check if we were aborted while getting the latest version
|
|
|
|
if (m_was_aborted) {
|
|
|
|
aborted();
|
2022-06-04 05:56:26 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
setStatus(tr("Parsing the API response from CurseForge for '%1'...").arg(mod->name()));
|
2022-06-04 05:56:26 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
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 05:56:26 +05:30
|
|
|
}
|
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
if (latest_ver.downloadUrl.isEmpty() && latest_ver.fileId != mod->metadata()->file_id) {
|
2022-06-16 20:15:29 +05:30
|
|
|
auto pack = getProjectInfo(latest_ver);
|
|
|
|
auto recover_url = QString("%1/download/%2").arg(pack.websiteUrl, latest_ver.fileId.toString());
|
2022-07-19 21:33:45 +05:30
|
|
|
emit checkFailed(mod, tr("Mod has a new update available, but is not downloadable using CurseForge."), recover_url);
|
2022-06-04 05:56:26 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
continue;
|
|
|
|
}
|
2022-06-04 05:56:26 +05:30
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
if (!latest_ver.hash.isEmpty() && (mod->metadata()->hash != latest_ver.hash || mod->status() == ModStatus::NotInstalled)) {
|
2022-06-16 20:15:29 +05:30
|
|
|
// Fake pack with the necessary info to pass to the download task :)
|
|
|
|
ModPlatform::IndexedPack pack;
|
2022-06-26 22:47:15 +05:30
|
|
|
pack.name = mod->name();
|
|
|
|
pack.slug = mod->metadata()->slug;
|
|
|
|
pack.addonId = mod->metadata()->project_id;
|
|
|
|
pack.websiteUrl = mod->homeurl();
|
|
|
|
for (auto& author : mod->authors())
|
2022-06-16 20:15:29 +05:30
|
|
|
pack.authors.append({ author });
|
2022-06-26 22:47:15 +05:30
|
|
|
pack.description = mod->description();
|
2022-11-25 17:53:46 +05:30
|
|
|
pack.provider = ModPlatform::ResourceProvider::FLAME;
|
2022-06-16 20:15:29 +05:30
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
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 20:15:29 +05:30
|
|
|
old_version = current_ver.version;
|
|
|
|
}
|
2022-06-04 05:56:26 +05:30
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
auto download_task = new ResourceDownloadTask(pack, latest_ver, m_mods_folder);
|
2022-06-26 22:47:15 +05:30
|
|
|
m_updatable.emplace_back(pack.name, mod->metadata()->hash, old_version, latest_ver.version,
|
2022-06-16 20:15:29 +05:30
|
|
|
api.getModFileChangelog(latest_ver.addonId.toInt(), latest_ver.fileId.toInt()),
|
2022-11-25 17:53:46 +05:30
|
|
|
ModPlatform::ResourceProvider::FLAME, download_task);
|
2022-06-16 20:15:29 +05:30
|
|
|
}
|
|
|
|
}
|
2022-06-04 05:56:26 +05:30
|
|
|
|
|
|
|
emitSucceeded();
|
|
|
|
}
|