Skip to content

Commit

Permalink
Adding the updater (COMPILATION BROKEN)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
EmmmaTech committed Mar 8, 2021
1 parent 6e6fd19 commit 10c0d82
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 22 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions core/include/constants.hpp
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 2 additions & 1 deletion core/include/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <json.hpp>
#include <sys/select.h>
#include <curl/curl.h>

#include <iostream>
Expand All @@ -7,11 +8,11 @@
#include <string>

#include <stdio.h>
#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);
9 changes: 4 additions & 5 deletions core/source/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "utils.hpp"
#include "constants.hpp"

void downloadFile(const char *url, const char *filename) {
CURL *curl;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions gui/include/gui_app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@
#include "switchToGUI.hpp"
#include "mainActivity.hpp"
#include "aboutTab.hpp"
#include "updateActivity.hpp"
#include "updaterTab.hpp"

bool run_gui_app();
7 changes: 7 additions & 0 deletions gui/include/updateActivity.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <borealis.hpp>

struct updateActivity : public brls::Activity {
CONTENT_FROM_XML_RES("activity/update.xml");
};
15 changes: 15 additions & 0 deletions gui/include/updaterTab.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <borealis.hpp>

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");
};
23 changes: 20 additions & 3 deletions gui/source/gui_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
#include <filesystem>
#include <fstream>
#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 };
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions gui/source/updateActivity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "updateActivity.hpp"
51 changes: 51 additions & 0 deletions gui/source/updaterTab.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "updaterTab.hpp"

#include "utils.hpp"
#include "constants.hpp"
#include <string>
#include <filesystem>

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();
}
10 changes: 8 additions & 2 deletions resources/i18n/en-US/nxgui.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
}
}
10 changes: 10 additions & 0 deletions resources/xml/activity/update.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<brls:TabFrame
title="@i18n/nxgui/title"
icon="@res/icon/Calculator_NX_Icon_Alt.jpg">

<!-- Main tab for the update activity -->
<brls:Tab label="@i18n/nxgui/tabs/update" >
<UpdaterTab/>
</brls:Tab>

</brls:TabFrame>
51 changes: 51 additions & 0 deletions resources/xml/tabs/updater.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<brls:Box
width="auto"
height="auto"
axis="column"
justifyContent="center">

<brls:Box
width="auto"
height="auto"
axis="column"
paddingLeft="@style/brls/tab_frame/content_padding_sides"
paddingRight="@style/brls/tab_frame/content_padding_sides"
paddingTop="@style/brls/tab_frame/content_padding_top_bottom"
paddingBottom="@style/brls/tab_frame/content_padding_top_bottom">

<brls:Label
width="auto"
height="auto"
textAlign="center"
fontSize="20"
marginBottom="25px"
text="@i18n/nxgui/components/update_reminder"/>

<brls:Button
id="yes"
width="auto"
height="auto"
shrink="1.0"
style="primary"
text="@i18n/nxgui/components/yes"/>

<brls:Button
id="no"
width="auto"
height="auto"
shrink="1.0"
style="primary"
text="@i18n/nxgui/components/no"/>

<brls:Label
id="verbose_label"
width="auto"
height="auto"
textAlign="center"
fontSize="20"
marginBottom="25px"
text="Waiting for response from user..."/>

</brls:Box>

</brls:Box>

0 comments on commit 10c0d82

Please sign in to comment.