From 2d74f465f8b0dd4b3ab43b49a3f054a915545231 Mon Sep 17 00:00:00 2001 From: flb Date: Wed, 22 Sep 2021 21:01:45 +0200 Subject: [PATCH] added option to cancel downloads/extraction https://github.com/HamletDuFromage/aio-switch-updater/issues/140 https://github.com/HamletDuFromage/aio-switch-updater/issues/125 --- include/progress_event.hpp | 4 ++++ source/color_swapper.cpp | 4 ---- source/download.cpp | 4 +++- source/extract.cpp | 22 +++++++++++++++++----- source/worker_page.cpp | 10 ++++++++-- 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/include/progress_event.hpp b/include/progress_event.hpp index 0f3d4ff..21a4ac9 100644 --- a/include/progress_event.hpp +++ b/include/progress_event.hpp @@ -10,6 +10,7 @@ private: double _total = 0; double _speed = 0; long _status_code = 0; + bool _interupt = false; public: ProgressEvent(const ProgressEvent&) = delete; @@ -31,6 +32,7 @@ public: _total = 0; _speed = 0; _status_code = 0; + _interupt = false; } inline void setTotalSteps(int steps) { _max = steps; } @@ -47,4 +49,6 @@ public: inline double getTotal() { return _total; } inline double getSpeed() { return _speed; } inline double getStatusCode() { return _status_code; } + inline void setInterupt(bool interupt) { _interupt = interupt; } + inline bool getInterupt() { return _interupt; } }; diff --git a/source/color_swapper.cpp b/source/color_swapper.cpp index 6bfb0d6..9774e36 100644 --- a/source/color_swapper.cpp +++ b/source/color_swapper.cpp @@ -155,7 +155,6 @@ namespace JC { { hiddbgInitialize(); hidsysInitialize(); - ProgressEvent::instance().reset(); ProgressEvent::instance().setStep(1); int res = setColor(values); if (res != 0) { @@ -170,7 +169,6 @@ namespace JC { { hiddbgInitialize(); hidsysInitialize(); - ProgressEvent::instance().reset(); ProgressEvent::instance().setStep(1); json backup; json profiles; @@ -306,7 +304,6 @@ namespace PC { { hiddbgInitialize(); hidsysInitialize(); - ProgressEvent::instance().reset(); ProgressEvent::instance().setStep(1); int res = setColor(values); if (res != 0) { @@ -321,7 +318,6 @@ namespace PC { { hiddbgInitialize(); hidsysInitialize(); - ProgressEvent::instance().reset(); ProgressEvent::instance().setStep(1); json backup; json profiles; diff --git a/source/download.cpp b/source/download.cpp index c2a2142..dd6d578 100644 --- a/source/download.cpp +++ b/source/download.cpp @@ -45,6 +45,9 @@ namespace download { static size_t WriteMemoryCallback(void* contents, size_t size, size_t num_files, void* userp) { + if (ProgressEvent::instance().getInterupt()) { + return 0; + } ntwrk_struct_t* data_struct = (ntwrk_struct_t*)userp; size_t realsize = size * num_files; @@ -132,7 +135,6 @@ namespace download { long downloadFile(const std::string& url, std::vector& res, const char* output, int api) { - ProgressEvent::instance().reset(); CURL* curl = curl_easy_init(); ntwrk_struct_t chunk = {0}; long status_code; diff --git a/source/extract.cpp b/source/extract.cpp index fa5f6a9..c0f3094 100644 --- a/source/extract.cpp +++ b/source/extract.cpp @@ -46,7 +46,6 @@ namespace extract { brls::Application::quit(); } } - ProgressEvent::instance().reset(); ProgressEvent::instance().setStep(1); ProgressEvent::instance().setTotalSteps(entries.size() + 1); } @@ -56,9 +55,14 @@ namespace extract { { zipper::Unzipper unzipper(filename); std::vector entries; + preWork(unzipper, workingPath, entries); std::set ignoreList = fs::readLineByLine(FILES_IGNORE); + for (const auto& entry : entries) { + if (ProgressEvent::instance().getInterupt()) { + break; + } if ((overwriteInis == 0 && entry.name.substr(entry.name.length() - 4) == ".ini") || find_if(ignoreList.begin(), ignoreList.end(), [&entry](std::string ignored) { u8 res = ("/" + entry.name).find(ignored); return (res == 0 || res == 1); }) != ignoreList.end()) { @@ -87,9 +91,14 @@ namespace extract { zipper::Unzipper unzipper(filename); std::vector entries; preWork(unzipper, workingPath, entries); + std::set ignoreList = fs::readLineByLine(FILES_IGNORE); ignoreList.insert(toExclude); + for (const auto& entry : entries) { + if (ProgressEvent::instance().getInterupt()) { + break; + } if (find_if(ignoreList.begin(), ignoreList.end(), [&entry](std::string ignored) { u8 res = ("/" + entry.name).find(ignored); return (res == 0 || res == 1); }) != ignoreList.end()) { @@ -172,7 +181,6 @@ namespace extract { void extractCheats(const std::string& zipPath, std::vector titles, CFW cfw, bool credits) { - ProgressEvent::instance().reset(); zipper::Unzipper unzipper(zipPath); std::vector entries = unzipper.entries(); //std::set extractedTitles; @@ -238,6 +246,9 @@ namespace extract { std::string id; ProgressEvent::instance().setTotalSteps(titles.size()); for (size_t j = 0; j < titles.size(); j++) { + if (ProgressEvent::instance().getInterupt()) { + break; + } for (size_t l = lastL; l < parents.size(); l++) { if (strcasecmp((titles[j]).c_str(), parents[l].substr(offset, 16).c_str()) == 0) { unzipper.extractEntry(parents[l]); @@ -261,10 +272,9 @@ namespace extract { void extractAllCheats(const std::string& zipPath, CFW cfw) { - ProgressEvent::instance().reset(); zipper::Unzipper unzipper(zipPath); std::vector entries = unzipper.entries(); - //std::set extractedTitles; + int offset = 0; switch (cfw) { case CFW::ams: @@ -288,6 +298,9 @@ namespace extract { } ProgressEvent::instance().setTotalSteps(entries.size()); for (const auto& entry : entries) { + if (ProgressEvent::instance().getInterupt()) { + break; + } if (((int)entry.name.size() == offset + 16 + 4) && (isBID(entry.name.substr(offset, 16)))) { //extractedTitles.insert(util::upperCase(entry.name.substr(offset - 24, 16))); unzipper.extractEntry(entry.name); @@ -325,7 +338,6 @@ namespace extract { void removeCheats() { std::string path = util::getContentsPath(); - ProgressEvent::instance().reset(); ProgressEvent::instance().setTotalSteps(std::distance(std::filesystem::directory_iterator(path), std::filesystem::directory_iterator())); for (const auto& entry : std::filesystem::directory_iterator(path)) { removeCheatsDirectory(entry.path().string()); diff --git a/source/worker_page.cpp b/source/worker_page.cpp index 187bbc4..ce6c73d 100644 --- a/source/worker_page.cpp +++ b/source/worker_page.cpp @@ -6,6 +6,7 @@ #include "constants.hpp" #include "download.hpp" #include "extract.hpp" +#include "main_frame.hpp" #include "progress_event.hpp" #include "utils.hpp" @@ -24,7 +25,10 @@ WorkerPage::WorkerPage(brls::StagedAppletFrame* frame, const std::string& text, this->button = new brls::Button(brls::ButtonStyle::REGULAR); this->button->setParent(this); - this->registerAction("", brls::Key::B, [this] { return true; }); + this->registerAction("menus/common/cancel"_i18n, brls::Key::B, [this] { + ProgressEvent::instance().setInterupt(true); + return true; + }); this->registerAction("", brls::Key::A, [this] { return true; }); } @@ -44,6 +48,9 @@ void WorkerPage::draw(NVGcontext* vg, int x, int y, unsigned width, unsigned hei this->draw_page = false; brls::Application::crash(fmt::format("menus/errors/error_message"_i18n, util::getErrorMessage(ProgressEvent::instance().getStatusCode()))); } + if (ProgressEvent::instance().getInterupt()) { + brls::Application::pushView(new MainFrame()); + } else { ProgressEvent::instance().setStatusCode(0); frame->nextStage(); @@ -54,7 +61,6 @@ void WorkerPage::draw(NVGcontext* vg, int x, int y, unsigned width, unsigned hei this->progressDisp->frame(ctx); if (ProgressEvent::instance().getTotal()) { this->label->setText(fmt::format("{0} ({1:.1f} MB of {2:.1f} MB - {3:.1f} MB/s)", text, ProgressEvent::instance().getNow() / 0x100000, ProgressEvent::instance().getTotal() / 0x100000, ProgressEvent::instance().getSpeed() / 0x100000)); - //this->label->setText(fmt::format("{0} ({1:.1f}MB of {2:.1f}MB - MB/s)", text, ProgressEvent::instance().getNow() / 0x100000, ProgressEvent::instance().getTotal() / 0x100000)); } this->label->frame(ctx); }