diff --git a/lib/libimhex/include/hex/helpers/utils.hpp b/lib/libimhex/include/hex/helpers/utils.hpp index 88a1e2b56..34665bd28 100644 --- a/lib/libimhex/include/hex/helpers/utils.hpp +++ b/lib/libimhex/include/hex/helpers/utils.hpp @@ -386,5 +386,6 @@ namespace hex { [[nodiscard]] void* getContainingModule(void* symbol); [[nodiscard]] std::optional blendColors(const std::optional &a, const std::optional &b); + std::optional parseTime(std::string_view format, const std::string &timeString); } diff --git a/lib/libimhex/source/helpers/utils.cpp b/lib/libimhex/source/helpers/utils.cpp index eaff41d08..49458c597 100644 --- a/lib/libimhex/source/helpers/utils.cpp +++ b/lib/libimhex/source/helpers/utils.cpp @@ -784,6 +784,19 @@ namespace hex { return ImAlphaBlendColors(a.value(), b.value()); } + std::optional parseTime(std::string_view format, const std::string &timeString) { + std::istringstream input(timeString); + input.imbue(std::locale(std::setlocale(LC_ALL, nullptr))); + + tm time = {}; + input >> std::get_time(&time, format.data()); + if (input.fail()) { + return std::nullopt; + } + + return std::chrono::system_clock::from_time_t(std::mktime(&time)); + } + extern "C" void macOSCloseButtonPressed() { EventCloseButtonPressed::post(); } diff --git a/plugins/builtin/source/content/init_tasks.cpp b/plugins/builtin/source/content/init_tasks.cpp index 09ef49785..804b9bf1e 100644 --- a/plugins/builtin/source/content/init_tasks.cpp +++ b/plugins/builtin/source/content/init_tasks.cpp @@ -28,69 +28,64 @@ namespace hex::plugin::builtin { bool checkForUpdatesSync() { int checkForUpdates = ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.server_contact", 2); + if (checkForUpdates != 1) + return true; // Check if we should check for updates - TaskManager::createBackgroundTask("Update Check", [checkForUpdates] { + TaskManager::createBackgroundTask("Update Check", [] { std::string updateString; - if (checkForUpdates == 1) { - if (ImHexApi::System::isNightlyBuild()) { - HttpRequest request("GET", GitHubApiURL + "/releases/tags/nightly"s); - request.setTimeout(10000); + if (ImHexApi::System::isNightlyBuild()) { + HttpRequest request("GET", GitHubApiURL + "/releases/tags/nightly"s); + request.setTimeout(10000); - // Query the GitHub API for the latest release version - auto response = request.execute().get(); - if (response.getStatusCode() != 200) - return; + // Query the GitHub API for the latest release version + auto response = request.execute().get(); + if (response.getStatusCode() != 200) + return; - nlohmann::json releases; - try { - releases = nlohmann::json::parse(response.getData()); - } catch (const std::exception &) { - return; - } - - // Check if the response is valid - if (!releases.contains("published_at") || !releases["published_at"].is_string()) - return; - - std::chrono::system_clock::time_point nightlyUpdateTime; - { - std::istringstream iss(releases["published_at"].get()); - iss >> std::chrono::parse("%FT%TZ", nightlyUpdateTime); - } - - if (nightlyUpdateTime > std::chrono::system_clock::now()) { - updateString = "Nightly"; - } - } else { - HttpRequest request("GET", GitHubApiURL + "/releases/latest"s); - - // Query the GitHub API for the latest release version - auto response = request.execute().get(); - if (response.getStatusCode() != 200) - return; - - nlohmann::json releases; - try { - releases = nlohmann::json::parse(response.getData()); - } catch (const std::exception &) { - return; - } - - // Check if the response is valid - if (!releases.contains("tag_name") || !releases["tag_name"].is_string()) - return; - - // 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(); - - // Check if the latest release is different from the current version - if (latestVersion != currVersion) - updateString = latestVersion; + nlohmann::json releases; + try { + releases = nlohmann::json::parse(response.getData()); + } catch (const std::exception &) { + return; } + + // Check if the response is valid + if (!releases.contains("published_at") || !releases["published_at"].is_string()) + return; + + const auto nightlyUpdateTime = hex::parseTime("%FT%TZ", releases["published_at"].get()); + if (nightlyUpdateTime.has_value() && *nightlyUpdateTime > std::chrono::system_clock::now()) { + updateString = "Nightly"; + } + } else { + HttpRequest request("GET", GitHubApiURL + "/releases/latest"s); + + // Query the GitHub API for the latest release version + auto response = request.execute().get(); + if (response.getStatusCode() != 200) + return; + + nlohmann::json releases; + try { + releases = nlohmann::json::parse(response.getData()); + } catch (const std::exception &) { + return; + } + + // Check if the response is valid + if (!releases.contains("tag_name") || !releases["tag_name"].is_string()) + return; + + // 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(); + + // Check if the latest release is different from the current version + if (latestVersion != currVersion) + updateString = latestVersion; } if (updateString.empty())