feat: Add option to check for updates to the Extras menu

This commit is contained in:
WerWolv
2025-08-09 23:46:15 +02:00
parent 6be0eeff72
commit 0870ab4d3c
5 changed files with 99 additions and 61 deletions

View File

@@ -26,8 +26,11 @@
#include <set>
#include <algorithm>
#include <GLFW/glfw3.h>
#include <hex/api_urls.hpp>
#include <hex/helpers/http_requests.hpp>
#include <hex/helpers/utils_macos.hpp>
#include <nlohmann/json.hpp>
#if defined(OS_WINDOWS)
#include <windows.h>
@@ -941,6 +944,73 @@ namespace hex {
return getImHexVersion().nightly();
}
std::optional<std::string> checkForUpdate() {
#if defined(OS_WEB)
return std::nullopt;
#else
if (ImHexApi::System::isNightlyBuild()) {
HttpRequest request("GET", GitHubApiURL + std::string("/releases/tags/nightly"));
request.setTimeout(10000);
// Query the GitHub API for the latest release version
auto response = request.execute().get();
if (response.getStatusCode() != 200)
return std::nullopt;
nlohmann::json releases;
try {
releases = nlohmann::json::parse(response.getData());
} catch (const std::exception &) {
return std::nullopt;
}
// Check if the response is valid
if (!releases.contains("assets") || !releases["assets"].is_array())
return std::nullopt;
const auto firstAsset = releases["assets"].front();
if (!firstAsset.is_object() || !firstAsset.contains("updated_at"))
return std::nullopt;
const auto nightlyUpdateTime = hex::parseTime("%Y-%m-%dT%H:%M:%SZ", firstAsset["updated_at"].get<std::string>());
const auto imhexBuildTime = ImHexApi::System::getBuildTime();
if (nightlyUpdateTime.has_value() && imhexBuildTime.has_value() && *nightlyUpdateTime > *imhexBuildTime) {
return "Nightly";
}
} else {
HttpRequest request("GET", GitHubApiURL + std::string("/releases/latest"));
// Query the GitHub API for the latest release version
auto response = request.execute().get();
if (response.getStatusCode() != 200)
return std::nullopt;
nlohmann::json releases;
try {
releases = nlohmann::json::parse(response.getData());
} catch (const std::exception &) {
return std::nullopt;
}
// Check if the response is valid
if (!releases.contains("tag_name") || !releases["tag_name"].is_string())
return std::nullopt;
// Convert the current version string to a format that can be compared to the latest release
auto currVersion = "v" + ImHexApi::System::getImHexVersion().get(false);
// Get the latest release version string
auto latestVersion = releases["tag_name"].get<std::string>();
// Check if the latest release is different from the current version
if (latestVersion != currVersion)
return latestVersion;
}
return std::nullopt;
#endif
}
bool updateImHex(UpdateType updateType) {
#if defined(OS_WEB)
switch (updateType) {