From 4656b860e81f89d6aba26a59e5f90778dcc96ea5 Mon Sep 17 00:00:00 2001 From: flb Date: Thu, 14 Oct 2021 21:50:18 +0200 Subject: [PATCH] added option to delete orphaned cheats --- include/cheats_page.hpp | 5 +---- include/extract.hpp | 1 + resources/i18n/en-US/menus.json | 2 +- source/cheats_page.cpp | 38 ++++++++++++++++++++++----------- source/extract.cpp | 20 +++++++++++++++++ 5 files changed, 48 insertions(+), 18 deletions(-) diff --git a/include/cheats_page.hpp b/include/cheats_page.hpp index 82ab7ec..ace050a 100644 --- a/include/cheats_page.hpp +++ b/include/cheats_page.hpp @@ -6,10 +6,7 @@ class CheatsPage : public brls::AppletFrame { private: brls::List* list; - brls::ListItem* view; - brls::ListItem* deleteCheats; - brls::ListItem* exclude; - brls::ListItem* dlAll; + brls::ListItem* item; brls::StagedAppletFrame* stagedFrame; public: diff --git a/include/extract.hpp b/include/extract.hpp index 5a827eb..6559605 100644 --- a/include/extract.hpp +++ b/include/extract.hpp @@ -33,6 +33,7 @@ namespace extract { void extractCheats(const std::string& zipPath, std::vector titles, CFW cfw, bool credits = false); void extractAllCheats(const std::string& zipPath, CFW cfw); void removeCheats(); + void removeOrphanedCheats(); bool removeCheatsDirectory(const std::string& entry); bool isBID(const std::string& bid); } // namespace extract \ No newline at end of file diff --git a/resources/i18n/en-US/menus.json b/resources/i18n/en-US/menus.json index 6af69e4..052cf0e 100644 --- a/resources/i18n/en-US/menus.json +++ b/resources/i18n/en-US/menus.json @@ -15,7 +15,7 @@ "view": "View installed cheats", "exclude": "Exclude games from receiving cheat updates", "delete_existing": "Delete all existing cheat codes", - "delete_all": "Delete all cheats", + "delete_orphaned": "Delete orphaned cheat chodes", "deleting": "Deleting…", "cheastlips_title": "Cheatslips cheats", "gbatemp_title": "GBAtemp cheats", diff --git a/source/cheats_page.cpp b/source/cheats_page.cpp index 96142d7..4a27746 100644 --- a/source/cheats_page.cpp +++ b/source/cheats_page.cpp @@ -17,34 +17,46 @@ CheatsPage::CheatsPage() : AppletFrame(true, true) this->setTitle("menus/cheats/menu"_i18n); list = new brls::List(); - view = new brls::ListItem("menus/cheats/view"_i18n); - view->getClickEvent()->subscribe([](brls::View* view) { + item = new brls::ListItem("menus/cheats/view"_i18n); + item->getClickEvent()->subscribe([](brls::View* view) { brls::Application::pushView(new AppPage_DownloadedCheats()); }); - list->addView(view); + list->addView(item); - exclude = new brls::ListItem("menus/cheats/exclude"_i18n); - exclude->getClickEvent()->subscribe([](brls::View* view) { + item = new brls::ListItem("menus/cheats/exclude"_i18n); + item->getClickEvent()->subscribe([](brls::View* view) { brls::Application::pushView(new AppPage_Exclude()); }); - list->addView(exclude); + list->addView(item); - deleteCheats = new brls::ListItem("menus/cheats/delete_existing"_i18n); - deleteCheats->getClickEvent()->subscribe([](brls::View* view) { + item = new brls::ListItem("menus/cheats/delete_existing"_i18n); + item->getClickEvent()->subscribe([](brls::View* view) { brls::StagedAppletFrame* stagedFrame = new brls::StagedAppletFrame(); - stagedFrame->setTitle("menus/cheats/delete_all"_i18n); + stagedFrame->setTitle("menus/cheats/delete_existing"_i18n); stagedFrame->addStage( new WorkerPage(stagedFrame, "menus/cheats/deleting"_i18n, []() { extract::removeCheats(); })); stagedFrame->addStage( new ConfirmPage(stagedFrame, "menus/common/all_done"_i18n, true)); brls::Application::pushView(stagedFrame); }); - list->addView(deleteCheats); + list->addView(item); + + item = new brls::ListItem("menus/cheats/delete_orphaned"_i18n); + item->getClickEvent()->subscribe([](brls::View* view) { + brls::StagedAppletFrame* stagedFrame = new brls::StagedAppletFrame(); + stagedFrame->setTitle("menus/cheats/delete_orphaned"_i18n); + stagedFrame->addStage( + new WorkerPage(stagedFrame, "menus/cheats/deleting"_i18n, []() { extract::removeOrphanedCheats(); })); + stagedFrame->addStage( + new ConfirmPage(stagedFrame, "menus/common/all_done"_i18n, true)); + brls::Application::pushView(stagedFrame); + }); + list->addView(item); std::string cheatsVer = util::downloadFileToString(CHEATS_URL_VERSION); if (cheatsVer != "") { - dlAll = new brls::ListItem("menus/cheats/dl_all"_i18n); - dlAll->getClickEvent()->subscribe([cheatsVer](brls::View* view) { + item = new brls::ListItem("menus/cheats/dl_all"_i18n); + item->getClickEvent()->subscribe([cheatsVer](brls::View* view) { std::string url; switch (CurrentCfw::running_cfw) { case CFW::sxos: @@ -70,7 +82,7 @@ CheatsPage::CheatsPage() : AppletFrame(true, true) new ConfirmPage(stagedFrame, "menus/common/all_done"_i18n, true)); brls::Application::pushView(stagedFrame); }); - list->addView(dlAll); + list->addView(item); } this->setContentView(list); } \ No newline at end of file diff --git a/source/extract.cpp b/source/extract.cpp index 5a50f95..5239a7f 100644 --- a/source/extract.cpp +++ b/source/extract.cpp @@ -288,6 +288,26 @@ namespace extract { ProgressEvent::instance().setStep(ProgressEvent::instance().getMax()); } + void removeOrphanedCheats() + { + auto path = util::getContentsPath(); + std::vector titles = getInstalledTitlesNs(); + ProgressEvent::instance().setTotalSteps(std::distance(std::filesystem::directory_iterator(path), std::filesystem::directory_iterator()) + 1); + for (const auto& entry : std::filesystem::directory_iterator(path)) { + if (ProgressEvent::instance().getInterupt()) { + break; + } + if (std::find_if(titles.begin(), titles.end(), [&entry](std::string title) { + return caselessCompare(entry.path().filename(), title); + }) == titles.end()) { + removeCheatsDirectory(entry.path().string()); + } + ProgressEvent::instance().incrementStep(1); + } + std::filesystem::remove(CHEATS_VERSION); + ProgressEvent::instance().setStep(ProgressEvent::instance().getMax()); + } + bool removeCheatsDirectory(const std::string& entry) { bool res = true;