From 2feb6901d634eb0b1e25cad5a931fc439a0bf623 Mon Sep 17 00:00:00 2001 From: flb Date: Mon, 21 Sep 2020 20:36:46 +0200 Subject: [PATCH] Made ini avoidance more robust, added a download payload option and cleaned stuff up --- Makefile | 2 +- include/JC_page.hpp | 2 -- include/confirm_page.hpp | 3 +- include/constants.hpp | 2 ++ include/download_payload_page.hpp | 17 +++++++++++ include/extract.hpp | 2 +- include/list_download_tab.hpp | 3 -- include/main_frame.hpp | 1 - include/tools_tab.hpp | 2 ++ include/utils.hpp | 1 + source/confirm_page.cpp | 1 - source/download.cpp | 1 - source/download_payload_page.cpp | 51 +++++++++++++++++++++++++++++++ source/extract.cpp | 43 ++++++++++++++++++++------ source/list_download_tab.cpp | 15 ++++----- source/tools_tab.cpp | 19 +++++++----- source/utils.cpp | 46 +++++++++++++--------------- 17 files changed, 150 insertions(+), 61 deletions(-) create mode 100644 include/download_payload_page.hpp create mode 100644 source/download_payload_page.cpp diff --git a/Makefile b/Makefile index a1f1a38..4343c65 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,7 @@ DATA := data INCLUDES := include lib/minizip/include APP_TITLE := AIO Switch Updater APP_AUTHOR := HamletDuFromage -APP_VERSION := 1.0.3 +APP_VERSION := 1.1.0 #ROMFS := $(BUILD)/romfs BOREALIS_PATH := lib/borealis diff --git a/include/JC_page.hpp b/include/JC_page.hpp index f2b7e95..ffc716a 100644 --- a/include/JC_page.hpp +++ b/include/JC_page.hpp @@ -1,8 +1,6 @@ #pragma once -#include "utils.hpp" #include "JC_color_swapper.hpp" -#include "constants.hpp" #include "confirm_page.hpp" #include "worker_page.hpp" diff --git a/include/confirm_page.hpp b/include/confirm_page.hpp index 7c78a7e..b1e461f 100644 --- a/include/confirm_page.hpp +++ b/include/confirm_page.hpp @@ -2,10 +2,9 @@ #include #include +#include #include "utils.hpp" -#include "download.hpp" -#include "constants.hpp" #include "main_frame.hpp" class ConfirmPage : public brls::View diff --git a/include/constants.hpp b/include/constants.hpp index e620eaa..a7af3b3 100644 --- a/include/constants.hpp +++ b/include/constants.hpp @@ -19,6 +19,8 @@ #define CFW_URL "https://hamletdufromage.github.io/switch-cfw/cfw.html" #define CFW_FILENAME "/config/aio-switch-updater/cfw.zip" +#define PAYLOAD_URL "https://hamletdufromage.github.io/switch-payloads/payloads.html" + #define CHEATS_RELEASE_URL "https://github.com/HamletDuFromage/switch-cheats-db/releases/tag/v1.0" #define CHEATS_URL_TITLES "https://github.com/HamletDuFromage/switch-cheats-db/releases/download/v1.0/titles.zip" #define CHEATS_URL_CONTENTS "https://github.com/HamletDuFromage/switch-cheats-db/releases/download/v1.0/contents.zip" diff --git a/include/download_payload_page.hpp b/include/download_payload_page.hpp new file mode 100644 index 0000000..2e4909a --- /dev/null +++ b/include/download_payload_page.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include "utils.hpp" +#include "confirm_page.hpp" +#include "worker_page.hpp" + +class DownloadPayloadPage : public brls::AppletFrame +{ + private: + brls::Label* label; + brls::Label* notFound; + brls::List* list; + std::vector items; + + public: + DownloadPayloadPage(); +}; \ No newline at end of file diff --git a/include/extract.hpp b/include/extract.hpp index 45370a1..18ce862 100644 --- a/include/extract.hpp +++ b/include/extract.hpp @@ -22,7 +22,6 @@ #include "progress_event.hpp" #include "constants.hpp" -#include "utils.hpp" static constexpr u32 MaxTitleCount = 64000; @@ -39,6 +38,7 @@ typedef struct Title { } Title; void extract(const char* filename, const char* workingPath = ROOT_PATH, int overwriteInis = 1); +void extract(const char * filename, const char* workingPath, const char* toExclude); std::string formatApplicationId(u64 ApplicationId); std::vector getInstalledTitlesNs(); std::vector<Title> excludeTitles(const char* path, std::vector<Title> listedTitles); diff --git a/include/list_download_tab.hpp b/include/list_download_tab.hpp index f9c4453..a570d61 100644 --- a/include/list_download_tab.hpp +++ b/include/list_download_tab.hpp @@ -4,15 +4,12 @@ #include <string> #include "download.hpp" #include "extract.hpp" -#include "constants.hpp" #include "confirm_page.hpp" #include "worker_page.hpp" -#include "utils.hpp" class ListDownloadTab : public brls::List { private: - std::tuple<std::vector<std::string>, std::vector<std::string>> links; std::vector<brls::ListItem*> linkItems; brls::Label *notFound; brls::Label *description; diff --git a/include/main_frame.hpp b/include/main_frame.hpp index 9137ad1..76324e7 100644 --- a/include/main_frame.hpp +++ b/include/main_frame.hpp @@ -2,7 +2,6 @@ #include <borealis.hpp> -#include "utils.hpp" #include "about_tab.hpp" #include "list_download_tab.hpp" #include "tools_tab.hpp" diff --git a/include/tools_tab.hpp b/include/tools_tab.hpp index 952a2ab..695a20b 100644 --- a/include/tools_tab.hpp +++ b/include/tools_tab.hpp @@ -5,6 +5,7 @@ #include "worker_page.hpp" #include "app_page.hpp" #include "payload_page.hpp" +#include "download_payload_page.hpp" #include "JC_page.hpp" #include "extract.hpp" #include "utils.hpp" @@ -18,6 +19,7 @@ class ToolsTab : public brls::List brls::ListItem* JCcolor; brls::ListItem* updateApp; brls::ListItem* rebootPayload; + brls::ListItem* downloadPaysload; brls::StagedAppletFrame* stagedFrame; public: diff --git a/include/utils.hpp b/include/utils.hpp index 6449861..2ad3d0a 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -23,4 +23,5 @@ std::string formatApplicationId(u64 ApplicationId); std::set<std::string> readLineByLine(const char * path); std::vector<std::string> fetchPayloads(); void shut_down(bool reboot = false); +int showDialogBox(std::string text, std::string opt1, std::string opt2); std::string getLatestTag(const char *url); \ No newline at end of file diff --git a/source/confirm_page.cpp b/source/confirm_page.cpp index 66fbcc2..accefc1 100644 --- a/source/confirm_page.cpp +++ b/source/confirm_page.cpp @@ -1,5 +1,4 @@ #include "confirm_page.hpp" -#include <algorithm> ConfirmPage::ConfirmPage(brls::StagedAppletFrame* frame, std::string text, bool done): done(done) { diff --git a/source/download.cpp b/source/download.cpp index 463bb0f..860289d 100644 --- a/source/download.cpp +++ b/source/download.cpp @@ -174,7 +174,6 @@ std::string fetchTitle(const char *url){ /* check for errors */ std::string ver = "-1"; std::string s = std::string(chunk.memory); - //std::cout << "start html " << s.substr(0, 199) << std::endl; std::regex rgx("<title>.+"); std::smatch match; if (std::regex_search(s, match, rgx)){ diff --git a/source/download_payload_page.cpp b/source/download_payload_page.cpp new file mode 100644 index 0000000..9231d3a --- /dev/null +++ b/source/download_payload_page.cpp @@ -0,0 +1,51 @@ +#include "download_payload_page.hpp" + +DownloadPayloadPage::DownloadPayloadPage() : AppletFrame(true, true) +{ + this->setTitle("Download payloads"); + list = new brls::List(); + label = new brls::Label( + brls::LabelStyle::DESCRIPTION, + "Select a payload to download to " + std::string(BOOTLOADER_PL_PATH) + ".", + true + ); + list->addView(label); + std::tuple, std::vector> links = fetchLinks(PAYLOAD_URL); + int nbLinks = std::get<0>(links).size(); + if(nbLinks){ + items.reserve(nbLinks); + for (int i = 0; i(links)[i]; + std::string path = std::string(BOOTLOADER_PL_PATH) + std::get<0>(links)[i]; + std::string text("Downloading:\n" + std::get<0>(links)[i] + "\n\nFrom:\n" + url); + items[i] = new brls::ListItem(std::get<0>(links)[i]); + items[i]->getClickEvent()->subscribe([&, text, url, path](brls::View* view) { + createTree(BOOTLOADER_PL_PATH); + brls::StagedAppletFrame* stagedFrame = new brls::StagedAppletFrame(); + stagedFrame->setTitle("Getting payload"); + stagedFrame->addStage( + new ConfirmPage(stagedFrame, text) + ); + stagedFrame->addStage( + new WorkerPage(stagedFrame, "Downloading...", [url, path](){downloadFile(url.c_str(), path.c_str(), OFF);}) + ); + stagedFrame->addStage( + new ConfirmPage(stagedFrame, "All done!", true) + ); + brls::Application::pushView(stagedFrame); + }); + list->addView(items[i]); + } + } + else{ + notFound = new brls::Label( + brls::LabelStyle::DESCRIPTION, + "Could not find a download link, make sure the Switch has access to the internet.\n"\ + "If this problem persists, please open an issue on Github", + true + ); + notFound->setHorizontalAlign(NVG_ALIGN_CENTER); + list->addView(notFound); + } + this->setContentView(list); +} \ No newline at end of file diff --git a/source/extract.cpp b/source/extract.cpp index 40fbc20..23b7098 100644 --- a/source/extract.cpp +++ b/source/extract.cpp @@ -9,8 +9,12 @@ void extract(const char * filename, const char* workingPath, int overwriteInis){ ProgressEvent::instance().setTotalSteps(entries.size() + 1); for (int i = 0; i < (int) entries.size(); i++){ if(overwriteInis == 0){ - if(entries[i].name.substr(entries[i].name.length() - 4) != ".ini") + if(entries[i].name.substr(entries[i].name.length() - 4) == ".ini"){ + if(!std::filesystem::exists("/" + entries[i].name)) unzipper.extractEntry(entries[i].name); + } + else{ unzipper.extractEntry(entries[i].name); + } } else unzipper.extractEntry(entries[i].name); @@ -21,6 +25,28 @@ void extract(const char * filename, const char* workingPath, int overwriteInis){ ProgressEvent::instance().setStep(ProgressEvent::instance().getMax()); } +void extract(const char * filename, const char* workingPath, const char* toExclude){ + ProgressEvent::instance().reset(); + ProgressEvent::instance().setStep(1); + chdir(workingPath); + zipper::Unzipper unzipper(filename); + std::vector entries = unzipper.entries(); + ProgressEvent::instance().setTotalSteps(entries.size() + 1); + for (int i = 0; i < (int) entries.size(); i++){ + if("/" + entries[i].name == toExclude){ + if(!std::filesystem::exists(toExclude)) { + unzipper.extractEntry(entries[i].name); + } + } + else { + unzipper.extractEntry(entries[i].name); + } + + ProgressEvent::instance().setStep(i); + } + unzipper.close(); + ProgressEvent::instance().setStep(ProgressEvent::instance().getMax()); +} std::vector getInstalledTitlesNs(){ // This function has been cobbled together from the "app_controldata" example in devkitpro. @@ -76,7 +102,6 @@ std::vector<Title> getInstalledTitlesNs(){ if (R_FAILED(rc)) { totalFailed++; - std::cout << "nsGetApplicationControlData() failed: 0x" << std::hex << rc << " for Title ID: " << formatApplicationId(recs[i].application_id) << std::endl; } if (outsize < sizeof(buf->nacp)) { @@ -165,17 +190,20 @@ void extractCheats(const char * zipPath, std::vector<Title> titles, CFW cfw, boo switch(cfw){ case ams: offset = std::string(CONTENTS_PATH).length(); - createTree(AMS_CONTENTS); + std::filesystem::create_directory(AMS_PATH); + std::filesystem::create_directory(AMS_CONTENTS); chdir(AMS_PATH); break; case rnx: offset = std::string(CONTENTS_PATH).length(); - createTree(REINX_CONTENTS); + std::filesystem::create_directory(REINX_PATH); + std::filesystem::create_directory(REINX_CONTENTS); chdir(REINX_PATH); break; case sxos: offset = std::string(TITLES_PATH).length(); - createTree(SXOS_TITLES); + std::filesystem::create_directory(SXOS_PATH); + std::filesystem::create_directory(SXOS_TITLES); chdir(SXOS_PATH); break; } @@ -217,11 +245,6 @@ void extractCheats(const char * zipPath, std::vector<Title> titles, CFW cfw, boo k++; } } - brls::Logger::debug("Titles size:"); - std::cout << titles.size() << std::endl; - - brls::Logger::debug("Parents size:"); - std::cout << parents.size() << std::endl; size_t lastL = 0; std::string name; diff --git a/source/list_download_tab.cpp b/source/list_download_tab.cpp index 59b9376..150d7b0 100644 --- a/source/list_download_tab.cpp +++ b/source/list_download_tab.cpp @@ -3,6 +3,7 @@ ListDownloadTab::ListDownloadTab(archiveType type) : brls::List() { + std::tuple<std::vector<std::string>, std::vector<std::string>> links; std::string operation = "Getting "; this->description = new brls::Label(brls::LabelStyle::DESCRIPTION, "", true); switch(type){ @@ -89,13 +90,13 @@ ListDownloadTab::ListDownloadTab(archiveType type) : } else{ notFound = new brls::Label( - brls::LabelStyle::DESCRIPTION, - "Could not find a download link, make sure the Switch has access to the internet.\n"\ - "If this problem persists, please open an issue on Github", - true - ); - notFound->setHorizontalAlign(NVG_ALIGN_CENTER); - this->addView(notFound); + brls::LabelStyle::DESCRIPTION, + "Could not find a download link, make sure the Switch has access to the internet.\n"\ + "If this problem persists, please open an issue on Github", + true + ); + notFound->setHorizontalAlign(NVG_ALIGN_CENTER); + this->addView(notFound); } } diff --git a/source/tools_tab.cpp b/source/tools_tab.cpp index f8fb9ac..38cd8cb 100644 --- a/source/tools_tab.cpp +++ b/source/tools_tab.cpp @@ -31,6 +31,18 @@ ToolsTab::ToolsTab() : brls::List() }); this->addView(JCcolor); + downloadPaysload = new brls::ListItem("Dowload payloads to " + std::string(BOOTLOADER_PL_PATH)); + downloadPaysload->getClickEvent()->subscribe([&](brls::View* view){ + brls::Application::pushView(new DownloadPayloadPage()); + }); + this->addView(downloadPaysload); + + rebootPayload = new brls::ListItem("Shut down / Inject payload"); + rebootPayload->getClickEvent()->subscribe([&](brls::View* view){ + brls::Application::pushView(new PayloadPage()); + }); + this->addView(rebootPayload); + std::string tag = getLatestTag(TAGS_INFO); if(!tag.empty()){ updateApp = new brls::ListItem("Update the app (v" + tag +")"); @@ -54,11 +66,4 @@ ToolsTab::ToolsTab() : brls::List() }); this->addView(updateApp); } - - rebootPayload = new brls::ListItem("Shut down / Inject payload"); - rebootPayload->getClickEvent()->subscribe([&](brls::View* view){ - brls::Application::pushView(new PayloadPage()); - }); - this->addView(rebootPayload); - } \ No newline at end of file diff --git a/source/utils.cpp b/source/utils.cpp index 3bd99ce..2c5ca12 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -75,40 +75,41 @@ void downloadArchive(std::string url, archiveType type){ int dialogResult = -1; -void extractArchive(archiveType type){ - int overwriteInis = -1; - std::vector<Title> titles; - brls::Dialog* dialog = new brls::Dialog("Do you want to overwrite existing .ini files?"); - brls::GenericEvent::Callback noCallback = [dialog](brls::View* view) { +int showDialogBox(std::string text, std::string opt1, std::string opt2){ + int result = -1; + brls::Dialog* dialog = new brls::Dialog(text); + brls::GenericEvent::Callback callback1 = [dialog](brls::View* view) { dialogResult = 0; dialog->close(); }; - brls::GenericEvent::Callback yesCallback = [dialog](brls::View* view) { + brls::GenericEvent::Callback callback2 = [dialog](brls::View* view) { dialogResult = 1; dialog->close(); }; - dialog->addButton("No", noCallback); - dialog->addButton("Yes", yesCallback); + dialog->addButton(opt1, callback1); + dialog->addButton(opt2, callback2); dialog->setCancelable(false); + dialog->open(); + while(result == -1){ + usleep(1); + result = dialogResult; + } + dialogResult = -1; + return result; +} +void extractArchive(archiveType type){ + int overwriteInis = 0; + std::vector<Title> titles; switch(type){ case sigpatches: if(isArchive(SIGPATCHES_FILENAME)) { std::string backup(HEKATE_IPL_PATH); backup += ".old"; if(std::filesystem::exists(HEKATE_IPL_PATH)){ - dialog->open(); - while(overwriteInis == -1){ - usleep(1); - overwriteInis = dialogResult; - } - dialogResult = -1; + overwriteInis = showDialogBox("Do you want to overwrite existing " + std::string(HEKATE_IPL_PATH) +"?", "No", "Yes"); if(overwriteInis == 0){ - std::filesystem::remove(backup); - std::filesystem::rename(HEKATE_IPL_PATH, backup); - extract(SIGPATCHES_FILENAME); - std::filesystem::remove(HEKATE_IPL_PATH); - std::filesystem::rename(backup, HEKATE_IPL_PATH); + extract(SIGPATCHES_FILENAME, ROOT_PATH, HEKATE_IPL_PATH); } else{ extract(SIGPATCHES_FILENAME); @@ -133,12 +134,7 @@ void extractArchive(archiveType type){ extract(APP_FILENAME); break; case cfw: - dialog->open(); - while(overwriteInis == -1){ - usleep(1); - overwriteInis = dialogResult; - } - dialogResult = -1; + overwriteInis = showDialogBox("Do you want to overwrite existing .ini config files?", "No", "Yes"); extract(CFW_FILENAME, ROOT_PATH, overwriteInis); break; }