From ab124174b026cebdb498f9b3fd41477f8ca495d5 Mon Sep 17 00:00:00 2001 From: William JCM Date: Wed, 28 Jul 2021 14:18:39 +0200 Subject: [PATCH] SaveTool: add rest of response handling. --- src/SaveTool/SaveTool.cpp | 52 ++++++++++++++++++++++++++ src/SaveTool/SaveTool.h | 5 +++ src/SaveTool/SaveTool_drawMainMenu.cpp | 12 ++++++ 3 files changed, 69 insertions(+) diff --git a/src/SaveTool/SaveTool.cpp b/src/SaveTool/SaveTool.cpp index b26036d..3b45801 100644 --- a/src/SaveTool/SaveTool.cpp +++ b/src/SaveTool/SaveTool.cpp @@ -32,6 +32,8 @@ #include +#include + #include #include #include @@ -303,6 +305,56 @@ void SaveTool::updateCheckEvent(SDL_Event& event) { _queue.addToast(Toast::Type::Error, Utility::formatString("The request failed with error code {}: {}", r.status_code, r.reason)); return; } + + using json = nlohmann::json; + + json response = json::parse(r.text); + + struct Version { + explicit Version(const std::string& str) { + std::size_t start_point = 0; + + if(str[0] == 'v') { + start_point++; + } + + major = std::atoi(str.c_str() + start_point); + start_point = str.find('.', start_point) + 1; + minor = std::atoi(str.c_str() + start_point); + start_point = str.find('.', start_point) + 1; + patch = std::atoi(str.c_str() + start_point); + } + Int major; + Int minor; + Int patch; + + bool operator==(const Version& other) const { + return (major == other.major) && (minor == other.minor) && (patch == other.patch); + } + bool operator>(const Version& other) const { + return ( major * 10000 + minor * 100 + patch) > + (other.major * 10000 + other.minor * 100 + other.patch); + } + }; + + static const Version current_ver{"0.9.9"}; + Version latest_ver{response[0]["tag_name"]}; + + if(latest_ver == current_ver) { + _queue.addToast(Toast::Type::Success, "The application is already up to date."); + } + else if(latest_ver > current_ver) { + _queue.addToast(Toast::Type::Warning, "Your version is out of date.\nCheck the settings for more information.", + std::chrono::milliseconds{5000}); + _updateAvailable = true; + _latestVersion = Utility::formatString("{}.{}.{}", latest_ver.major, latest_ver.minor, latest_ver.patch); + _releaseLink = response[0]["html_url"]; + _downloadLink = response[0]["assets"][0]["browser_download_url"]; + Utility::Debug{} << _downloadLink.c_str(); + } + else if(current_ver > latest_ver) { + _queue.addToast(Toast::Type::Warning, "Your version is more recent than the latest one in the repo. How???"); + } } void SaveTool::initialiseConfiguration() { diff --git a/src/SaveTool/SaveTool.h b/src/SaveTool/SaveTool.h index 9da41bf..da0266a 100644 --- a/src/SaveTool/SaveTool.h +++ b/src/SaveTool/SaveTool.h @@ -199,4 +199,9 @@ class SaveTool: public Platform::Sdl2Application, public efsw::FileWatchListener bool _checkUpdatesOnStartup{true}; bool _unsafeMode{false}; + + bool _updateAvailable{false}; + std::string _latestVersion; + std::string _releaseLink; + std::string _downloadLink; }; diff --git a/src/SaveTool/SaveTool_drawMainMenu.cpp b/src/SaveTool/SaveTool_drawMainMenu.cpp index 2eadc9c..3094079 100644 --- a/src/SaveTool/SaveTool_drawMainMenu.cpp +++ b/src/SaveTool/SaveTool_drawMainMenu.cpp @@ -67,6 +67,18 @@ void SaveTool::drawMainMenu() { _thread = std::thread{[this]{ checkForUpdates(); }}; } + if(_updateAvailable) { + ImGui::AlignTextToFramePadding(); + ImGui::Text("Version %s is available.", _latestVersion.c_str()); + if(ImGui::Button(ICON_FA_FILE_SIGNATURE " Release notes")) { + openUri(_releaseLink); + } + ImGui::SameLine(); + if(ImGui::Button(ICON_FA_DOWNLOAD " Download now")) { + openUri(_downloadLink); + } + } + ImGui::EndMenu(); }