From 10c0d828c1956ce86e0be9b928b97dc70e820ab4 Mon Sep 17 00:00:00 2001 From: EmreTech Date: Mon, 8 Mar 2021 09:53:39 -0800 Subject: [PATCH] Adding the updater (COMPILATION BROKEN) Don't try to compile this yet, there will be a very long linker error that I haven't figured out yet. If you know how to fix it, send me a Pull Request. --- Makefile | 8 ++--- core/include/constants.hpp | 14 ++++----- core/include/utils.hpp | 3 +- core/source/utils.cpp | 9 +++--- gui/include/gui_app.hpp | 2 ++ gui/include/updateActivity.hpp | 7 +++++ gui/include/updaterTab.hpp | 15 +++++++++ gui/source/gui_app.cpp | 23 ++++++++++++-- gui/source/updateActivity.cpp | 1 + gui/source/updaterTab.cpp | 51 +++++++++++++++++++++++++++++++ resources/i18n/en-US/nxgui.json | 10 ++++-- resources/xml/activity/update.xml | 10 ++++++ resources/xml/tabs/updater.xml | 51 +++++++++++++++++++++++++++++++ 13 files changed, 182 insertions(+), 22 deletions(-) create mode 100644 gui/include/updateActivity.hpp create mode 100644 gui/include/updaterTab.hpp create mode 100644 gui/source/updateActivity.cpp create mode 100644 gui/source/updaterTab.cpp create mode 100644 resources/xml/activity/update.xml create mode 100644 resources/xml/tabs/updater.xml diff --git a/Makefile b/Makefile index dab8684..2772a0f 100644 --- a/Makefile +++ b/Makefile @@ -63,16 +63,16 @@ OUT_SHADERS := shaders ARCH := -march=armv8-a+crc+crypto -mtune=cortex-a57 -mtp=soft -fPIE CFLAGS := -g -Wall -O2 -ffunction-sections \ - $(ARCH) $(DEFINES) + $(ARCH) $(DEFINES) `curl-config --cflags` -CFLAGS += $(INCLUDE) -D__SWITCH__ +CFLAGS += $(INCLUDE) -D__SWITCH__ -CXXFLAGS := $(CFLAGS) -std=c++1z -fexceptions -Wmaybe-uninitialized -O2 -Wno-volatile +CXXFLAGS := $(CFLAGS) -std=c++17 -fexceptions -O2 -Wno-volatile ASFLAGS := -g $(ARCH) LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) -LIBS := -lnx +LIBS := -lnx `curl-config --libs` #--------------------------------------------------------------------------------- # list of directories containing libraries, this must be the top level containing diff --git a/core/include/constants.hpp b/core/include/constants.hpp index 185e5d2..158c66c 100644 --- a/core/include/constants.hpp +++ b/core/include/constants.hpp @@ -1,18 +1,18 @@ #define ROOT_PATH "/" -#define NRO_PATH_1 "/switch/Calculator_NX.nro" -#define NRO_PATH_2 "/switch/Calculator_NX/Calculator_NX.nro" +#define NRO_PATH_1 "/switch/Calculator_NX.nro" // This is for if the user manually got this app from my GitHub +#define NRO_PATH_2 "/switch/Calculator_NX/Calculator_NX.nro" // This is for if the user downloaded this app from HB App Store #define CONFIG_PATH "/config/Calculator_NX/" #define GUI_DEFAULT_FILE "gui_default.txt" #define CMD_DEFAULT_FILE "cmd_default.txt" #define DOWNLOAD_PATH "/config/Calculator_NX/download/" -#define API_URL "https://api.github.com/repos/EmreTech/Calculator_NX/releases/latest" - - - - +#define JSON_DOWNLOAD_FILE "latest-tag.json" +#define JSON_DOWNLOAD_PATH "/config/Calculator_NX/download/latest-tag.json" +#define NRO_DOWNLOAD_PATH "/config/Calculator_NX/download/Calculator_NX.nro" +#define API_URL "https://api.github.com/repos/EmreTech/Calculator_NX/releases/latest" +#define APP_VERSION "v1.3.2" \ No newline at end of file diff --git a/core/include/utils.hpp b/core/include/utils.hpp index f8073c5..ea855d2 100644 --- a/core/include/utils.hpp +++ b/core/include/utils.hpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -7,11 +8,11 @@ #include #include +#include "constants.hpp" #define API_AGENT "EmreTech" using json = nlohmann::json; void downloadFile(const char *url, const char *filename); - std::string getLatestTag(std::string url); std::string getLatestDownload(std::string url); \ No newline at end of file diff --git a/core/source/utils.cpp b/core/source/utils.cpp index 7559c0f..ce9ab8d 100644 --- a/core/source/utils.cpp +++ b/core/source/utils.cpp @@ -1,5 +1,4 @@ #include "utils.hpp" -#include "constants.hpp" void downloadFile(const char *url, const char *filename) { CURL *curl; @@ -30,10 +29,10 @@ void downloadFile(const char *url, const char *filename) { } std::string getLatestTag(std::string url) { - downloadFile(url.c_str(), DOWNLOAD_PATH + "latest-tag.json"); + downloadFile(url.c_str(), JSON_DOWNLOAD_PATH); json api_data; - std::ifstream api_file(DOWNLOAD_PATH + "latest-tag.json"); + std::ifstream api_file(JSON_DOWNLOAD_PATH); api_file >> api_data; api_file.close(); @@ -42,11 +41,11 @@ std::string getLatestTag(std::string url) { } std::string getLatestDownload(std::string url) { - const char *full_filename = DOWNLOAD_PATH + "latest-tag.json"; + const char *full_filename = JSON_DOWNLOAD_PATH; downloadFile(url.c_str(), full_filename); json api_data; - std::ifstream api_file(DOWNLOAD_PATH + "latest-tag.json"); + std::ifstream api_file(JSON_DOWNLOAD_PATH); api_file >> api_data; api_file.close(); diff --git a/gui/include/gui_app.hpp b/gui/include/gui_app.hpp index bf295d2..62eacee 100644 --- a/gui/include/gui_app.hpp +++ b/gui/include/gui_app.hpp @@ -16,5 +16,7 @@ #include "switchToGUI.hpp" #include "mainActivity.hpp" #include "aboutTab.hpp" +#include "updateActivity.hpp" +#include "updaterTab.hpp" bool run_gui_app(); \ No newline at end of file diff --git a/gui/include/updateActivity.hpp b/gui/include/updateActivity.hpp new file mode 100644 index 0000000..78e4e21 --- /dev/null +++ b/gui/include/updateActivity.hpp @@ -0,0 +1,7 @@ +#pragma once + +#include + +struct updateActivity : public brls::Activity { + CONTENT_FROM_XML_RES("activity/update.xml"); +}; diff --git a/gui/include/updaterTab.hpp b/gui/include/updaterTab.hpp new file mode 100644 index 0000000..c7ba976 --- /dev/null +++ b/gui/include/updaterTab.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include + +struct UpdaterTab : public brls::Box { + UpdaterTab(); + + static brls::View* create(); + + private: + bool onYesButtonClicked(brls::View* view); + bool onNoButtonClicked(brls::View* view); + + BRLS_BIND(brls::Label, verboseLabel, "verbose_label"); +}; \ No newline at end of file diff --git a/gui/source/gui_app.cpp b/gui/source/gui_app.cpp index 94d8b2f..a344f0b 100644 --- a/gui/source/gui_app.cpp +++ b/gui/source/gui_app.cpp @@ -3,11 +3,14 @@ #include #include #include "constants.hpp" -#include "app.hpp" +#include "utils.hpp" +//#include "app.hpp" using namespace brls::literals; bool run_gui_app() { + bool versionsSame; + std::filesystem::path gui_default{ CONFIG_PATH }; gui_default.append(GUI_DEFAULT_FILE); std::filesystem::path cmd_default{ CONFIG_PATH }; @@ -16,9 +19,15 @@ bool run_gui_app() { if (!std::filesystem::exists(CONFIG_PATH)) std::filesystem::create_directory(CONFIG_PATH); if (std::filesystem::exists(cmd_default)) return true; + socketInitializeDefault(); + // Set up the logger brls::Logger::setLogLevel(brls::LogLevel::INFO); + std::string version_from_api = getLatestTag(std::string(API_URL)); + std::string currentVersion = std::string(APP_VERSION); + versionsSame = version_from_api == currentVersion; + // Init the app and i18n if (!brls::Application::init()) { // If the program fails init process, it reports this and stops @@ -32,11 +41,17 @@ bool run_gui_app() { // Initlize the XML configs brls::Application::registerXMLView("CalculatorTab", CalculatorTab::create); - brls::Application::registerXMLView("AboutTab", AboutTab::create); // + brls::Application::registerXMLView("AboutTab", AboutTab::create); brls::Application::registerXMLView("SwitchToCMD", SwitchToCMD::create); + if (!versionsSame) + brls::Application::registerXMLView("UpdaterTab", UpdaterTab::create); + // If the program is sucessfull with the init process, it pushes the whole GUI - brls::Application::pushActivity(new MainActivity()); + if (versionsSame) + brls::Application::pushActivity(new MainActivity()); + else + brls::Application::pushActivity(new updateActivity()); //brls::Logger::debug("Successfully completed the Push Activity function"); // Main application loop @@ -58,6 +73,8 @@ bool run_gui_app() { } } + socketExit(); + if (std::filesystem::exists(cmd_default)) return true; // When the loop exits, it reports a successful exit diff --git a/gui/source/updateActivity.cpp b/gui/source/updateActivity.cpp new file mode 100644 index 0000000..df447af --- /dev/null +++ b/gui/source/updateActivity.cpp @@ -0,0 +1 @@ +#include "updateActivity.hpp" \ No newline at end of file diff --git a/gui/source/updaterTab.cpp b/gui/source/updaterTab.cpp new file mode 100644 index 0000000..2640d0f --- /dev/null +++ b/gui/source/updaterTab.cpp @@ -0,0 +1,51 @@ +#include "updaterTab.hpp" + +#include "utils.hpp" +#include "constants.hpp" +#include +#include + +UpdaterTab::UpdaterTab() { + this->inflateFromXMLRes("xml/tabs/updater.xml"); + + BRLS_REGISTER_CLICK_BY_ID("yes", this->onYesButtonClicked); + BRLS_REGISTER_CLICK_BY_ID("no", this->onNoButtonClicked); +} + +bool UpdaterTab::onYesButtonClicked(brls::View* view) { + //TODO: Actually update the app + this->verboseLabel->setText("Finding the URL for the app..."); + std::string url_download = getLatestDownload(API_URL); + + this->verboseLabel->setText("URL found! Downloading from URL..."); + downloadFile(url_download.c_str(), NRO_DOWNLOAD_PATH); + + this->verboseLabel->setText("File downloaded! Moving to the /switch folder..."); + if (std::filesystem::exists(NRO_PATH_1)) + std::filesystem::copy_file(NRO_DOWNLOAD_PATH, NRO_PATH_1); + else + std::filesystem::copy_file(NRO_DOWNLOAD_PATH, NRO_PATH_2); + + return true; +} + +bool UpdaterTab::onNoButtonClicked(brls::View* view) { + //TODO: Add a way to ignore an update. Until then, it'll excute the same code from onYesButtonClicked + this->verboseLabel->setText("Finding the URL for the app..."); + std::string url_download = getLatestDownload(API_URL); + + this->verboseLabel->setText("URL found! Downloading from URL..."); + downloadFile(url_download.c_str(), NRO_DOWNLOAD_PATH); + + this->verboseLabel->setText("File downloaded! Moving to the /switch folder..."); + if (std::filesystem::exists(NRO_PATH_1)) + std::filesystem::copy_file(NRO_DOWNLOAD_PATH, NRO_PATH_1); + else + std::filesystem::copy_file(NRO_DOWNLOAD_PATH, NRO_PATH_2); + + return true; +} + +brls::View* UpdaterTab::create() { + return new UpdaterTab(); +} \ No newline at end of file diff --git a/resources/i18n/en-US/nxgui.json b/resources/i18n/en-US/nxgui.json index fb01289..a2d79de 100644 --- a/resources/i18n/en-US/nxgui.json +++ b/resources/i18n/en-US/nxgui.json @@ -7,7 +7,9 @@ "calculator": "Calculator Home", "about": "About", "help": "Help", - "switch_gui_tab": "Switch to CMD Mode" + "switch_gui_tab": "Switch to CMD Mode", + + "update": "Updater" }, "components": { @@ -23,6 +25,10 @@ "labels_credits3": "natinusala and other contributors for making the borealis library", "labels_version": "Version 1.3.2", - "switch_gui": "Switch over to the CMD Version" + "switch_gui": "Switch over to the CMD Version", + + "update_reminder": "There is a new version of Calculator_NX avaiable! Would you like to update?", + "yes": "Yes", + "no": "No" } } \ No newline at end of file diff --git a/resources/xml/activity/update.xml b/resources/xml/activity/update.xml new file mode 100644 index 0000000..6adea49 --- /dev/null +++ b/resources/xml/activity/update.xml @@ -0,0 +1,10 @@ + + + + + + + + diff --git a/resources/xml/tabs/updater.xml b/resources/xml/tabs/updater.xml new file mode 100644 index 0000000..dee737b --- /dev/null +++ b/resources/xml/tabs/updater.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + \ No newline at end of file