From 6a79f2b490d7643af57ad217796c8f45e256a4d2 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sun, 14 Nov 2021 15:27:09 +0100 Subject: [PATCH] feat: add version to update menu --- include/misc/Updater.h | 6 ++++++ src/misc/Updater.cpp | 20 +++++++++++++------- src/render/gui/UpdateMenu.cpp | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/include/misc/Updater.h b/include/misc/Updater.h index 5d3eb07..7e210e0 100644 --- a/include/misc/Updater.h +++ b/include/misc/Updater.h @@ -16,6 +16,7 @@ private: bool m_FileWrited; bool m_CancelDownload; DataBuffer m_FileBuffer; + std::string m_LastVersion; public: Updater() : m_Progress(0), m_DownloadComplete(false), m_FileWrited(false), m_CancelDownload(false) {} @@ -32,6 +33,11 @@ public: static std::string getLocalFilePath(); static void removeOldFile(); + + static std::string getCurrentVersion(){ return TD_VERSION; } + std::string getLastVersion() { return m_LastVersion; } + + bool canUpdate(); private: std::string getDownloadFileURL(); }; diff --git a/src/misc/Updater.cpp b/src/misc/Updater.cpp index 2efcbdd..049220b 100644 --- a/src/misc/Updater.cpp +++ b/src/misc/Updater.cpp @@ -13,18 +13,20 @@ namespace td { namespace utils { bool Updater::checkUpdate() { - std::string newFileUrl = getDownloadFileURL(); - - if (newFileUrl.empty()) return false; - httplib::Client client("thesims.freeboxos.fr", 30000); if (auto res = client.Get("/Tower%20Defense/version")) { - std::string currentVersion = TD_VERSION; - std::string lastVersion = res.value().body; + std::string currentVersion = getCurrentVersion(); + m_LastVersion = res.value().body; - if (currentVersion != lastVersion) { + // we only look at the first line + m_LastVersion = m_LastVersion.substr(0, m_LastVersion.find('\n')); + + std::cout << "Current version : [" << getCurrentVersion() << "]\n"; + std::cout << "Last version : [" << m_LastVersion << "]\n"; + + if (currentVersion != m_LastVersion) { return true; } return false; @@ -36,6 +38,10 @@ bool Updater::checkUpdate() { return false; } +bool Updater::canUpdate() { + return !getDownloadFileURL().empty(); +} + std::string Updater::getDownloadFileURL() { Os systemOs = getSystemOs(); Architecture systemArch = getSystemArchitecture(); diff --git a/src/render/gui/UpdateMenu.cpp b/src/render/gui/UpdateMenu.cpp index 112747b..c5b5390 100644 --- a/src/render/gui/UpdateMenu.cpp +++ b/src/render/gui/UpdateMenu.cpp @@ -44,9 +44,20 @@ void UpdateMenu::render() { } } else { ImGui::Text("An update is available!"); + ImGui::Separator(); + ImGui::Text("Current version : %s", m_Updater.getCurrentVersion().c_str()); + ImGui::Text("Last version : %s", m_Updater.getLastVersion().c_str()); + + bool canDownloadFile = m_Updater.canUpdate(); + + if(!canDownloadFile) ImGui::BeginDisabled(); + if (ImGui::Button("Download")) { m_Updater.downloadUpdate(); } + + if(!canDownloadFile) ImGui::EndDisabled(); + ImGui::SameLine(); if (ImGui::Button("Cancel")) { m_Opened = false; @@ -55,6 +66,9 @@ void UpdateMenu::render() { } } else { ImGui::Text("No update available!"); + ImGui::Separator(); + ImGui::Text("Current version : %s", m_Updater.getCurrentVersion().c_str()); + ImGui::Text("Last version : %s", m_Updater.getLastVersion().c_str()); } } else { ImGui::Text("Checking updates ...");