diff --git a/Makefile b/Makefile index 740111a..bccc7a1 100644 --- a/Makefile +++ b/Makefile @@ -72,7 +72,7 @@ CXXFLAGS := $(CFLAGS) -std=c++17 -O2 -Wno-volatile ASFLAGS := -g $(ARCH) LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) -LIBS := -lnx +LIBS := -lnx -lcurl #--------------------------------------------------------------------------------- # list of directories containing libraries, this must be the top level containing diff --git a/include/constants.hpp b/include/constants.hpp new file mode 100644 index 0000000..3aec2de --- /dev/null +++ b/include/constants.hpp @@ -0,0 +1,11 @@ +#pragma once + +#define CALCULATOR_NX_PATH "/switch/Calculator_NX" + +#define CONFIG_PATH "/config/Calculator_NX" +#define DOWNLOAD_PATH "/config/Calculator_NX/download" + +#define TAGS_API_LINK "https://api.github.com/repos/EmreTech/Calculator_NX/tags" +#define LATEST_RELEASE_API_LINK "https://api.github.com/repos/EmreTech/Calculator_NX/releases/latest" + +#define BASE_DOWNLOAD_URL "https://github.com/EmreTech/Calculator_NX/releases/download/{}/Calculator_NX.nro" \ No newline at end of file diff --git a/include/download.hpp b/include/download.hpp new file mode 100644 index 0000000..4225d67 --- /dev/null +++ b/include/download.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include + +#include +#include +#include +#include + +#define API_AGENT "EmreTech" + +CURLcode downloadFile(const std::string &url, const std::string &filename); + +std::string getLatestTag(bool nightly); +std::string getLatestDownload(bool nightly); \ No newline at end of file diff --git a/resources/i18n/en-US/text.json b/resources/i18n/en-US/text.json index ba1f80c..b25827c 100644 --- a/resources/i18n/en-US/text.json +++ b/resources/i18n/en-US/text.json @@ -1,5 +1,6 @@ { "title": "Calculator_NX Rewrite", + "update_title": "Calculator_NX Updater", "tabs": { "calculator": "Calculator", @@ -14,5 +15,11 @@ "about": { "appname": "Calculator_NX", "developer": "Developed by EmreTech" + }, + + "updater": { + "starting_update": "Starting update...", + "downloading_update": "Downloading the latest version...", + "restarting_to_finish": "About to restart to finish the update." } } \ No newline at end of file diff --git a/resources/xml/activity/updater.xml b/resources/xml/activity/updater.xml new file mode 100644 index 0000000..aa98a36 --- /dev/null +++ b/resources/xml/activity/updater.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/resources/xml/tabs/updater.xml b/resources/xml/tabs/updater.xml new file mode 100644 index 0000000..048f1e4 --- /dev/null +++ b/resources/xml/tabs/updater.xml @@ -0,0 +1,14 @@ + + + + + \ No newline at end of file diff --git a/source/calculatorTab.cpp b/source/calculatorTab.cpp index 6acd6c1..a9fafe2 100644 --- a/source/calculatorTab.cpp +++ b/source/calculatorTab.cpp @@ -92,4 +92,5 @@ void CalculatorTab::updateScreenBufferFromExpStr() screenBuffer = expressionStr; if (expressionStr.length() >= 30) screenBuffer = expressionStr.substr(29); + updateScreen(); } \ No newline at end of file diff --git a/source/download.cpp b/source/download.cpp new file mode 100644 index 0000000..927d95d --- /dev/null +++ b/source/download.cpp @@ -0,0 +1,80 @@ +#include + +#include +#include + +#include + +size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + size_t written = fwrite(ptr, size, nmemb, stream); + return written; +} +CURLcode downloadFile(const std::string &url, const std::string &filename) +{ + CURL *curl; + FILE *fp; + CURLcode res; + + curl = curl_easy_init(); + if (curl) + { + fp = fopen(filename.c_str(), "wb"); + + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); + + curl_easy_setopt(curl, CURLOPT_USERAGENT, API_AGENT); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); + + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); + + res = curl_easy_perform(curl); + curl_easy_cleanup(curl); + + fclose(fp); + } + + return res; +} + +std::string getLatestTag(bool nightly) +{ + downloadFile( + (nightly ? TAGS_API_LINK : LATEST_RELEASE_API_LINK), DOWNLOAD_PATH + std::string("/github_api.json") + ); + + nlohmann::json api_data; + std::ifstream api_file(DOWNLOAD_PATH + std::string("/github_api.json")); + + api_file >> api_data; + api_file.close(); + + if (nightly) + return api_data[0]["name"].get(); + + return api_data["tag_name"]; +} + +std::string getLatestDownload(bool nightly) +{ + if (nightly) + { + std::string latestTag = getLatestTag(nightly); + return fmt::format(BASE_DOWNLOAD_URL, latestTag); + } + + downloadFile( + LATEST_RELEASE_API_LINK, DOWNLOAD_PATH + std::string("/github_api_two.json") + ); + + nlohmann::json api_data; + std::ifstream api_file(DOWNLOAD_PATH + std::string("/github_api_two.json")); + + api_file >> api_data; + api_file.close(); + + return api_data["assets"][12/*"browser_download_url"*/]; +} \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index c7a27b5..4073607 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,5 +1,4 @@ #include -#include #include #include