mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-03-27 23:37:05 -05:00
feat: Add option to check for updates to the Extras menu
This commit is contained in:
@@ -680,6 +680,12 @@ EXPORT_MODULE namespace hex {
|
||||
*/
|
||||
bool isNightlyBuild();
|
||||
|
||||
/**
|
||||
* @brief Checks if there's an update available for the current version of ImHex
|
||||
* @return Optional string returning the version string of the new version, or std::nullopt if no update is available
|
||||
*/
|
||||
std::optional<std::string> checkForUpdate();
|
||||
|
||||
enum class UpdateType {
|
||||
Stable,
|
||||
Nightly
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user