From 57cdaca0b53839b0568c3a0e3fca76df3252ab47 Mon Sep 17 00:00:00 2001 From: Spoiled Unknown <58999771+SpoiledUnknown@users.noreply.github.com> Date: Thu, 25 Jun 2026 18:20:59 +0530 Subject: [PATCH] Added the check for update functionality. --- assets/svg/download.svg | 5 ++ include/utils/update_manager.h | 8 ++++ include/version.h | 3 ++ src/core/application.cpp | 6 +++ src/core/utils/image_manager.cpp | 2 + src/core/utils/update_manager.cpp | 79 +++++++++++++++++++++++++++++++ src/ui/panels/left_panel.cpp | 22 +++++++-- src/ui/panels/settings.cpp | 7 ++- 8 files changed, 125 insertions(+), 7 deletions(-) create mode 100644 assets/svg/download.svg create mode 100644 include/utils/update_manager.h create mode 100644 include/version.h create mode 100644 src/core/utils/update_manager.cpp diff --git a/assets/svg/download.svg b/assets/svg/download.svg new file mode 100644 index 00000000..156485c5 --- /dev/null +++ b/assets/svg/download.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/include/utils/update_manager.h b/include/utils/update_manager.h new file mode 100644 index 00000000..4b04944f --- /dev/null +++ b/include/utils/update_manager.h @@ -0,0 +1,8 @@ +#pragma once +#include + +namespace Updater { +void checkForUpdates(); +inline bool hasUpdate; +inline std::string url; +} // namespace Updater \ No newline at end of file diff --git a/include/version.h b/include/version.h new file mode 100644 index 00000000..ff639828 --- /dev/null +++ b/include/version.h @@ -0,0 +1,3 @@ +#pragma once + +#define APP_VERSION "0.1.0" diff --git a/src/core/application.cpp b/src/core/application.cpp index e2fad968..08cdc284 100644 --- a/src/core/application.cpp +++ b/src/core/application.cpp @@ -15,6 +15,7 @@ #include "storage/storage.h" #include "utils/banner_manager.h" #include "utils/tray_manager.h" +#include "utils/update_manager.h" #ifdef _WIN32 #define _WIN32_WINNT 0x0A00 // Windows 10 @@ -28,6 +29,7 @@ GLFWcursor *WindowCallbacks::cursor_nwse = nullptr; GLFWcursor *WindowCallbacks::cursor_nesw = nullptr; GLFWcursor *WindowCallbacks::cursor_h = nullptr; + GLFWcursor *WindowCallbacks::cursor_v = nullptr; bool WindowCallbacks::hovered_; #endif @@ -147,12 +149,14 @@ void Application::run() { storage.initTOML(); bool discordRpc = true; + bool checkForUpdates = true; bool systemTray = false; auto toml = storage.loadTOML(); auto *settingsTable = toml["settings"].as_table(); if (settingsTable) { discordRpc = settingsTable->get("discord_rpc")->value_or(false); systemTray = settingsTable->get("quit_tray_min")->value_or(false); + checkForUpdates = settingsTable->get("check_updates")->value_or(false); } std::vector> clients; @@ -193,6 +197,8 @@ void Application::run() { discord::RichPresence::UpdatePresence("Lazap", "In Main Menu"); } + if (checkForUpdates) Updater::checkForUpdates(); + printf("Startup took: %ld ms\n", std::chrono::duration_cast( std::chrono::high_resolution_clock::now() - startupBegin) diff --git a/src/core/utils/image_manager.cpp b/src/core/utils/image_manager.cpp index 73df7a8d..0e1730b4 100644 --- a/src/core/utils/image_manager.cpp +++ b/src/core/utils/image_manager.cpp @@ -7,6 +7,8 @@ #include "battery/embed.hpp" #define STB_IMAGE_IMPLEMENTATION +#include + #include "stb_image.h" std::unordered_map ImageManager::cache; diff --git a/src/core/utils/update_manager.cpp b/src/core/utils/update_manager.cpp new file mode 100644 index 00000000..c617f36e --- /dev/null +++ b/src/core/utils/update_manager.cpp @@ -0,0 +1,79 @@ +#include "utils/update_manager.h" + +#include + +#include +#include + +#include "string.h" +#include "version.h" + +struct UpdateRes { + std::string tag_name; + std::string html_url; +}; + +template <> +struct glz::meta { + static constexpr auto value = object("tag_name", &UpdateRes::tag_name, + "html_url", &UpdateRes::html_url); +}; + +struct Version { + int major; + int minor; + int patch; +}; + +Version parseVersion(std::string version) { + Version result{}; + + if (!version.empty() && version[0] == 'v') version.erase(0, 1); + + sscanf(version.data(), "%d.%d.%d", &result.major, &result.minor, + &result.patch); + + return result; +} + +bool isNewer(const Version& remote, const Version& local) { + if (remote.major != local.major) return remote.major > local.major; + + if (remote.minor != local.minor) return remote.minor > local.minor; + + return remote.patch > local.patch; +} + +namespace Updater { +void checkForUpdates() { + cpr::Response res = cpr::Get(cpr::Url{ + "https://api.github.com/repos/Lazap-Development/Lazap/releases/latest"}); + + if (res.status_code != 200) { + std::cerr << "HTTP Error: " << res.status_code << std::endl; + return; + } + + UpdateRes obj; + + if (auto ec = + glz::read(obj, res.text); + ec) { + std::cerr << glz::format_error(ec, res.text) << std::endl; + return; + } + + glz::read_json(obj, res.text); + + auto remoteVersion = parseVersion(obj.tag_name); + auto localVersion = parseVersion(APP_VERSION); + + if (isNewer(remoteVersion, localVersion)) { + std::cout << "Remote Version: " << obj.tag_name << std::endl; + std::cout << "Local Version: " << APP_VERSION << std::endl; + + hasUpdate = true; + url = obj.html_url; + } +} +} // namespace Updater \ No newline at end of file diff --git a/src/ui/panels/left_panel.cpp b/src/ui/panels/left_panel.cpp index 11a43dd7..26afd8e2 100644 --- a/src/ui/panels/left_panel.cpp +++ b/src/ui/panels/left_panel.cpp @@ -6,6 +6,7 @@ #endif #include "ui/theme.h" #include "utils/image_manager.h" +#include "utils/update_manager.h" using namespace ui; @@ -32,6 +33,8 @@ void LeftPanel::init() { 0xFF000000); ImageManager::loadSVG(b::embed<"assets/svg/github.svg">(), "github", 0xFFFFFFFF); + ImageManager::loadSVG(b::embed<"assets/svg/download.svg">(), "download", + 0x99FF66B2); ImageManager::loadSVG(b::embed<"assets/svg/settings.svg">(), "settings", 0xFFFFFFFF); ImageManager::loadSVG(b::embed<"assets/svg/settings.svg">(), "settings-black", @@ -112,10 +115,23 @@ void LeftPanel::render() { view_->view == ViewType::Library)) if (view_->view != ViewType::Library) view_->Library(); - ImGui::Dummy( - ImVec2(0, ImGui::GetContentRegionAvail().y - (125.0f * scale_.y))); - // Bottom icons + if (Updater::hasUpdate) { + ImGui::Dummy( + ImVec2(0, ImGui::GetContentRegionAvail().y - (180.0f * scale_.y))); + + if (drawIconButton("Update", ImageManager::get("download"), + ImageManager::get("download"), + ImVec2(30 * scale_.x, 30 * scale_.x), false)) + openURL(Updater::url); + + ImGui::Dummy(ImVec2(0, 30.0f * scale_.y)); + } + else { + ImGui::Dummy( + ImVec2(0, ImGui::GetContentRegionAvail().y - (125.0f * scale_.y))); + } + if (drawIconButton("GitHub", ImageManager::get("github"), ImageManager::get("github"), ImVec2(30 * scale_.x, 30 * scale_.x), false)) diff --git a/src/ui/panels/settings.cpp b/src/ui/panels/settings.cpp index e486f56f..8241c48e 100755 --- a/src/ui/panels/settings.cpp +++ b/src/ui/panels/settings.cpp @@ -122,10 +122,9 @@ void SettingsPanel::render() { else Autostart::deleteShortcut(); } - if (addOption("Check for Updates", InputType::Toggle, &checkUpdates_, - true)) { - // saveSettings(); - ImGui::OpenPopup("Coming Soon"); + if (addOption("Check for Updates", InputType::Toggle, &checkUpdates_)) { + saveSettings(); + ImGui::OpenPopup("Restart"); } ImGui::EndGroup();