mirror of
https://github.com/azahar-emu/azahar
synced 2025-11-07 07:29:58 +01:00
qt: Added new update checker logic which correctly handles prereleases
This commit is contained in:
parent
4a3d4de9c2
commit
31908f732f
@ -394,9 +394,12 @@ GMainWindow::GMainWindow(Core::System& system_)
|
|||||||
#ifdef ENABLE_QT_UPDATE_CHECKER
|
#ifdef ENABLE_QT_UPDATE_CHECKER
|
||||||
if (UISettings::values.check_for_update_on_start) {
|
if (UISettings::values.check_for_update_on_start) {
|
||||||
update_future = QtConcurrent::run([]() -> QString {
|
update_future = QtConcurrent::run([]() -> QString {
|
||||||
const std::optional<std::string> latest_release = UpdateChecker::CheckForUpdate();
|
const bool is_prerelease = // TODO: This can be done better -OS
|
||||||
if (latest_release && latest_release.value() != Common::g_build_fullname) {
|
(strstr(Common::g_build_fullname, "rc") != NULL);
|
||||||
return QString::fromStdString(latest_release.value());
|
const std::optional<std::string> latest_release_tag =
|
||||||
|
UpdateChecker::GetLatestRelease(is_prerelease);
|
||||||
|
if (latest_release_tag && latest_release_tag.value() != Common::g_build_fullname) {
|
||||||
|
return QString::fromStdString(latest_release_tag.value());
|
||||||
}
|
}
|
||||||
return QString{};
|
return QString{};
|
||||||
});
|
});
|
||||||
|
|||||||
@ -9,47 +9,74 @@
|
|||||||
#include "common/logging/log.h"
|
#include "common/logging/log.h"
|
||||||
#include "update_checker.h"
|
#include "update_checker.h"
|
||||||
|
|
||||||
std::optional<std::string> UpdateChecker::CheckForUpdate() {
|
std::optional<std::string> GetResponse(std::string url, std::string path) {
|
||||||
constexpr auto UPDATE_CHECK_URL = "http://api.github.com";
|
constexpr std::size_t timeout_seconds = 15;
|
||||||
constexpr auto UPDATE_CHECK_PATH = "/repos/azahar-emu/azahar/releases/latest";
|
|
||||||
constexpr std::size_t UPDATE_CHECK_TIMEOUT_SECONDS = 15;
|
|
||||||
|
|
||||||
std::unique_ptr<httplib::Client> client = std::make_unique<httplib::Client>(UPDATE_CHECK_URL);
|
std::unique_ptr<httplib::Client> client = std::make_unique<httplib::Client>(url);
|
||||||
client->set_connection_timeout(UPDATE_CHECK_TIMEOUT_SECONDS);
|
client->set_connection_timeout(timeout_seconds);
|
||||||
client->set_read_timeout(UPDATE_CHECK_TIMEOUT_SECONDS);
|
client->set_read_timeout(timeout_seconds);
|
||||||
client->set_write_timeout(UPDATE_CHECK_TIMEOUT_SECONDS);
|
client->set_write_timeout(timeout_seconds);
|
||||||
|
|
||||||
if (client == nullptr) {
|
if (client == nullptr) {
|
||||||
LOG_ERROR(Frontend, "Invalid URL {}{}", UPDATE_CHECK_URL, UPDATE_CHECK_PATH);
|
LOG_ERROR(Frontend, "Invalid URL {}{}", url, path);
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
httplib::Request request{
|
httplib::Request request{
|
||||||
.method = "GET",
|
.method = "GET",
|
||||||
.path = UPDATE_CHECK_PATH,
|
.path = path,
|
||||||
};
|
};
|
||||||
|
|
||||||
client->set_follow_location(true);
|
client->set_follow_location(true);
|
||||||
const auto result = client->send(request);
|
const auto result = client->send(request);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
LOG_ERROR(Frontend, "GET to {}{} returned null", UPDATE_CHECK_URL, UPDATE_CHECK_PATH);
|
LOG_ERROR(Frontend, "GET to {}{} returned null", url, path);
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto& response = result.value();
|
const auto& response = result.value();
|
||||||
if (response.status >= 400) {
|
if (response.status >= 400) {
|
||||||
LOG_ERROR(Frontend, "GET to {}{} returned error status code: {}", UPDATE_CHECK_URL,
|
LOG_ERROR(Frontend, "GET to {}{} returned error status code: {}", url, path,
|
||||||
UPDATE_CHECK_PATH, response.status);
|
response.status);
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
if (!response.headers.contains("content-type")) {
|
if (!response.headers.contains("content-type")) {
|
||||||
LOG_ERROR(Frontend, "GET to {}{} returned no content", UPDATE_CHECK_URL, UPDATE_CHECK_PATH);
|
LOG_ERROR(Frontend, "GET to {}{} returned no content", url, path);
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return response.body;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<std::string> UpdateChecker::GetLatestRelease(bool include_prereleases) {
|
||||||
|
constexpr auto update_check_url = "http://api.github.com";
|
||||||
|
std::string update_check_path = "/repos/azahar-emu/azahar/releases";
|
||||||
try {
|
try {
|
||||||
return nlohmann::json::parse(response.body).at("tag_name");
|
if (include_prereleases) {
|
||||||
|
// This can return either a prerelease or a normal release, whichever is more recent
|
||||||
|
const auto response = GetResponse(update_check_url, update_check_path);
|
||||||
|
if (!response)
|
||||||
|
return {};
|
||||||
|
return nlohmann::json::parse(response.value()).at(0).at("tag_name");
|
||||||
|
} else {
|
||||||
|
update_check_path += "/latest";
|
||||||
|
const auto response = GetResponse(update_check_url, update_check_path);
|
||||||
|
if (!response)
|
||||||
|
return {};
|
||||||
|
return nlohmann::json::parse(response.value()).at("tag_name");
|
||||||
|
}
|
||||||
|
|
||||||
} catch (nlohmann::detail::out_of_range&) {
|
} catch (nlohmann::detail::out_of_range&) {
|
||||||
|
LOG_ERROR(Frontend,
|
||||||
|
"Parsing JSON response from {}{} failed during update check: "
|
||||||
|
"nlohmann::detail::out_of_range",
|
||||||
|
update_check_url, update_check_path);
|
||||||
|
return {};
|
||||||
|
} catch (nlohmann::detail::type_error&) {
|
||||||
|
LOG_ERROR(Frontend,
|
||||||
|
"Parsing JSON response from {}{} failed during update check: "
|
||||||
|
"nlohmann::detail::type_error",
|
||||||
|
update_check_url, update_check_path);
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,5 +8,5 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace UpdateChecker {
|
namespace UpdateChecker {
|
||||||
std::optional<std::string> CheckForUpdate();
|
std::optional<std::string> GetLatestRelease(bool);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user