1
0
Fork 0
mirror of https://github.com/HamletDuFromage/aio-switch-updater.git synced 2024-11-25 02:52:05 +00:00
AIO-switch-updater/source/app_page.cpp

358 lines
12 KiB
C++
Raw Normal View History

2020-09-20 01:21:28 +01:00
#include "app_page.hpp"
2021-09-11 14:48:13 +01:00
#include <switch.h>
2021-09-11 14:48:13 +01:00
#include <filesystem>
#include <fstream>
#include <regex>
2021-03-16 14:56:46 +00:00
2021-09-11 14:48:13 +01:00
#include "confirm_page.hpp"
#include "current_cfw.hpp"
2022-05-16 14:39:50 +01:00
#include "download.hpp"
2021-09-11 14:48:13 +01:00
#include "download_cheats_page.hpp"
#include "extract.hpp"
#include "fs.hpp"
#include "utils.hpp"
#include "worker_page.hpp"
namespace i18n = brls::i18n;
using namespace i18n::literals;
AppPage::AppPage() : AppletFrame(true, true)
2020-09-20 01:21:28 +01:00
{
list = new brls::List();
}
2020-09-20 01:21:28 +01:00
void AppPage::PopulatePage()
{
this->CreateLabel();
2020-09-20 01:21:28 +01:00
2021-09-11 14:48:13 +01:00
NsApplicationRecord* records = new NsApplicationRecord[MaxTitleCount];
NsApplicationControlData* controlData = NULL;
std::string name;
2020-09-20 01:21:28 +01:00
2021-09-11 14:48:13 +01:00
s32 recordCount = 0;
u64 controlSize = 0;
u64 tid;
if (!util::isApplet()) {
2021-09-11 14:48:13 +01:00
if (R_SUCCEEDED(nsListApplicationRecord(records, MaxTitleCount, 0, &recordCount))) {
for (s32 i = 0; i < recordCount; i++) {
controlSize = 0;
2021-09-11 14:48:13 +01:00
if (R_FAILED(InitControlData(&controlData))) break;
tid = records[i].application_id;
2022-05-16 14:39:50 +01:00
if (R_FAILED(GetControlData(tid, controlData, controlSize, name)))
continue;
listItem = new brls::ListItem(name, "", util::formatApplicationId(tid));
2022-05-16 14:39:50 +01:00
listItem->setThumbnail(controlData->icon, sizeof(controlData->icon));
this->AddListItem(name, tid);
}
2021-08-23 14:07:09 +01:00
free(controlData);
}
}
else {
tid = GetCurrentApplicationId();
if (R_SUCCEEDED(InitControlData(&controlData)) && R_SUCCEEDED(GetControlData(tid & 0xFFFFFFFFFFFFF000, controlData, controlSize, name))) {
2022-05-22 14:39:26 +01:00
listItem = new brls::ListItem(name, "", util::formatApplicationId(tid));
listItem->setThumbnail(controlData->icon, sizeof(controlData->icon));
2022-05-16 14:39:50 +01:00
this->AddListItem(name, tid);
2020-09-20 01:21:28 +01:00
}
label = new brls::Label(brls::LabelStyle::SMALL, "menus/common/applet_mode_not_supported"_i18n, true);
list->addView(label);
2020-09-20 01:21:28 +01:00
}
2021-09-07 14:44:12 +01:00
delete[] records;
2022-05-16 14:55:12 +01:00
brls::Logger::debug("count {}", list->getViewsCount());
if (!list->getViewsCount()) {
2022-05-16 14:55:12 +01:00
list->addView(new brls::Label(brls::LabelStyle::DESCRIPTION, "menus/common/nothing_to_see"_i18n, true));
}
this->setContentView(list);
}
void AppPage::CreateDownloadAllButton()
{
2021-03-01 18:19:17 +00:00
std::string text("menus/cheats/downloading"_i18n);
2020-09-20 01:21:28 +01:00
std::string url = "";
2021-09-11 14:48:13 +01:00
switch (CurrentCfw::running_cfw) {
case CFW::ams:
2020-09-20 01:21:28 +01:00
url += CHEATS_URL_CONTENTS;
break;
case CFW::rnx:
2020-09-20 01:21:28 +01:00
url += CHEATS_URL_CONTENTS;
break;
case CFW::sxos:
2020-09-20 01:21:28 +01:00
url += CHEATS_URL_CONTENTS;
break;
}
text += url;
download = new brls::ListItem("menus/cheats/dl_latest"_i18n);
2021-10-03 18:54:09 +01:00
download->getClickEvent()->subscribe([url, text](brls::View* view) {
2020-09-20 01:21:28 +01:00
brls::StagedAppletFrame* stagedFrame = new brls::StagedAppletFrame();
2021-03-10 20:54:17 +00:00
stagedFrame->setTitle("menus/cheats/getting_cheats"_i18n);
2020-09-20 01:21:28 +01:00
stagedFrame->addStage(
2021-09-11 14:48:13 +01:00
new ConfirmPage(stagedFrame, text));
2020-09-20 01:21:28 +01:00
stagedFrame->addStage(
new WorkerPage(stagedFrame, "menus/common/downloading"_i18n, [url]() { util::downloadArchive(url, contentType::cheats); }));
2020-09-20 01:21:28 +01:00
stagedFrame->addStage(
new WorkerPage(stagedFrame, "menus/common/extracting"_i18n, []() { util::extractArchive(contentType::cheats); }));
2020-09-20 01:21:28 +01:00
stagedFrame->addStage(
new ConfirmPage_Done(stagedFrame, "menus/common/all_done"_i18n));
2020-09-20 01:21:28 +01:00
brls::Application::pushView(stagedFrame);
});
list->addView(download);
}
2021-09-11 14:48:13 +01:00
u32 AppPage::InitControlData(NsApplicationControlData** controlData)
{
2021-09-07 14:44:12 +01:00
free(*controlData);
*controlData = (NsApplicationControlData*)malloc(sizeof(NsApplicationControlData));
2021-09-11 14:48:13 +01:00
if (*controlData == NULL) {
return 300;
}
else {
memset(*controlData, 0, sizeof(NsApplicationControlData));
return 0;
}
}
u32 AppPage::GetControlData(u64 tid, NsApplicationControlData* controlData, u64& controlSize, std::string& name)
{
Result rc;
NacpLanguageEntry* langEntry = NULL;
rc = nsGetApplicationControlData(NsApplicationControlSource_Storage, tid, controlData, sizeof(NsApplicationControlData), &controlSize);
2021-09-11 14:48:13 +01:00
if (R_FAILED(rc)) return rc;
2021-09-11 14:48:13 +01:00
if (controlSize < sizeof(controlData->nacp)) return 100;
rc = nacpGetLanguageEntry(&controlData->nacp, &langEntry);
2021-09-11 14:48:13 +01:00
if (R_FAILED(rc)) return rc;
if (!langEntry->name) return 200;
name = langEntry->name;
return 0;
}
2022-05-16 14:39:50 +01:00
void AppPage::AddListItem(const std::string& name, u64 tid)
{
list->addView(listItem);
}
uint64_t AppPage::GetCurrentApplicationId()
{
Result rc = 0;
uint64_t pid = 0;
uint64_t tid = 0;
rc = pmdmntGetApplicationProcessId(&pid);
if (rc == 0x20f || R_FAILED(rc)) return 0;
rc = pminfoGetProgramId(&tid, pid);
if (rc == 0x20f || R_FAILED(rc)) return 0;
return tid;
}
AppPage_CheatSlips::AppPage_CheatSlips() : AppPage()
{
this->PopulatePage();
this->registerAction("menus/cheats/cheatslips_logout"_i18n, brls::Key::X, [this] {
std::filesystem::remove(TOKEN_PATH);
return true;
});
}
void AppPage_CheatSlips::CreateLabel()
{
this->setTitle("menus/cheats/cheatslips_title"_i18n);
label = new brls::Label(brls::LabelStyle::DESCRIPTION, "menus/cheats/cheatslips_select"_i18n, true);
list->addView(label);
}
2022-05-16 14:39:50 +01:00
void AppPage_CheatSlips::AddListItem(const std::string& name, u64 tid)
{
2021-10-03 18:54:09 +01:00
listItem->getClickEvent()->subscribe([tid, name](brls::View* view) { brls::Application::pushView(new DownloadCheatsPage_CheatSlips(tid, name)); });
2022-05-16 14:39:50 +01:00
list->addView(listItem);
}
AppPage_Gbatemp::AppPage_Gbatemp() : AppPage()
{
this->PopulatePage();
this->setIcon("romfs:/gbatemp_icon.png");
}
void AppPage_Gbatemp::CreateLabel()
{
this->setTitle("menus/cheats/gbatemp_title"_i18n);
2021-09-11 14:48:13 +01:00
label = new brls::Label(brls::LabelStyle::DESCRIPTION, "menus/cheats/cheatslips_select"_i18n, true);
list->addView(label);
}
2022-05-16 14:39:50 +01:00
void AppPage_Gbatemp::AddListItem(const std::string& name, u64 tid)
{
2021-10-03 18:54:09 +01:00
listItem->getClickEvent()->subscribe([tid, name](brls::View* view) { brls::Application::pushView(new DownloadCheatsPage_GbaTemp(tid, name)); });
2022-05-16 14:39:50 +01:00
list->addView(listItem);
}
AppPage_Exclude::AppPage_Exclude() : AppPage()
{
this->PopulatePage();
}
void AppPage_Exclude::CreateLabel()
{
this->setTitle("menus/cheats/exclude_titles"_i18n);
2021-09-11 14:48:13 +01:00
label = new brls::Label(brls::LabelStyle::DESCRIPTION, "menus/cheats/exclude_titles_desc"_i18n, true);
list->addView(label);
}
void AppPage_Exclude::PopulatePage()
{
this->CreateLabel();
2021-09-11 14:48:13 +01:00
NsApplicationRecord* records = new NsApplicationRecord[MaxTitleCount];
NsApplicationControlData* controlData = NULL;
std::string name;
2021-09-11 14:48:13 +01:00
s32 recordCount = 0;
u64 controlSize = 0;
u64 tid;
auto titles = fs::readLineByLine(CHEATS_EXCLUDE);
if (!util::isApplet()) {
2021-09-11 14:48:13 +01:00
if (R_SUCCEEDED(nsListApplicationRecord(records, MaxTitleCount, 0, &recordCount))) {
for (s32 i = 0; i < recordCount; i++) {
controlSize = 0;
2021-09-11 14:48:13 +01:00
if (R_FAILED(InitControlData(&controlData))) break;
tid = records[i].application_id;
2022-05-16 14:39:50 +01:00
if (R_FAILED(GetControlData(tid, controlData, controlSize, name)))
continue;
2021-09-11 14:48:13 +01:00
brls::ToggleListItem* listItem;
2021-07-09 12:49:17 +01:00
listItem = new brls::ToggleListItem(std::string(name), titles.find(util::formatApplicationId(tid)) != titles.end() ? 0 : 1);
listItem->setThumbnail(controlData->icon, sizeof(controlData->icon));
items.insert(std::make_pair(listItem, util::formatApplicationId(tid)));
list->addView(listItem);
}
free(controlData);
}
}
else {
label = new brls::Label(brls::LabelStyle::SMALL, "menus/common/applet_mode_not_supported"_i18n, true);
list->addView(label);
}
delete[] records;
2021-09-11 14:48:13 +01:00
list->registerAction("menus/cheats/exclude_titles_save"_i18n, brls::Key::B, [this] {
std::set<std::string> exclude;
for (const auto& item : items) {
2021-09-11 14:48:13 +01:00
if (!item.first->getToggleState()) {
exclude.insert(item.second);
}
}
extract::writeTitlesToFile(exclude, CHEATS_EXCLUDE);
brls::Application::popView();
return true;
});
this->registerAction("menus/cheats/exclude_all"_i18n, brls::Key::X, [this] {
std::set<std::string> exclude;
2022-10-31 23:17:12 +00:00
for (const auto& item : items) {
exclude.insert(item.second);
}
extract::writeTitlesToFile(exclude, CHEATS_EXCLUDE);
brls::Application::popView();
return true;
});
this->registerAction("menus/cheats/exclude_none"_i18n, brls::Key::Y, [this] {
extract::writeTitlesToFile({}, CHEATS_EXCLUDE);
brls::Application::popView();
return true;
});
2020-09-20 01:21:28 +01:00
this->setContentView(list);
}
AppPage_DownloadedCheats::AppPage_DownloadedCheats() : AppPage()
{
GetExistingCheatsTids();
this->PopulatePage();
}
void AppPage_DownloadedCheats::CreateLabel()
{
this->setTitle("menus/cheats/installed"_i18n);
label = new brls::Label(brls::LabelStyle::DESCRIPTION, "menus/cheats/label"_i18n, true);
list->addView(label);
}
2022-05-16 14:39:50 +01:00
void AppPage_DownloadedCheats::AddListItem(const std::string& name, u64 tid)
{
auto tid_str = util::formatApplicationId(tid);
if (titles.find(tid_str) != titles.end()) {
2022-05-16 14:39:50 +01:00
listItem->getClickEvent()->subscribe([tid, name](brls::View* view) { cheats_util::ShowCheatFiles(tid, name); });
listItem->registerAction("menus/cheats/delete_cheats"_i18n, brls::Key::Y, [tid_str] {
2022-03-30 21:23:15 +01:00
util::showDialogBoxInfo(extract::removeCheatsDirectory(fmt::format("{}{}", util::getContentsPath(), tid_str)) ? "menus/common/all_done"_i18n : fmt::format("menus/cheats/deletion_error"_i18n, tid_str));
return true;
});
2022-05-16 14:39:50 +01:00
list->addView(listItem);
}
}
2021-09-11 14:48:13 +01:00
void AppPage_DownloadedCheats::GetExistingCheatsTids()
{
std::string path = util::getContentsPath();
2021-09-11 14:48:13 +01:00
for (const auto& entry : std::filesystem::directory_iterator(path)) {
std::string cheatsPath = entry.path().string() + "/cheats";
if (std::filesystem::exists(cheatsPath) && !std::filesystem::is_empty(cheatsPath)) {
for (const auto& cheatFile : std::filesystem::directory_iterator(cheatsPath)) {
if (extract::isBID(cheatFile.path().filename().stem())) {
titles.insert(util::upperCase(cheatsPath.substr(cheatsPath.length() - 7 - 16, 16)));
break;
}
}
}
}
}
2022-05-16 14:39:50 +01:00
AppPage_OutdatedTitles::AppPage_OutdatedTitles() : AppPage()
{
download::getRequest(LOOKUP_TABLE_URL, versions);
if (versions.empty()) {
2022-05-16 15:35:42 +01:00
list->addView(new brls::Label(brls::LabelStyle::DESCRIPTION, "menus/main/links_not_found"_i18n, true));
this->setContentView(list);
}
else {
this->PopulatePage();
}
2022-05-16 14:39:50 +01:00
}
void AppPage_OutdatedTitles::AddListItem(const std::string& name, u64 tid)
{
u32 version = cheats_util::GetVersion(tid);
std::string tid_string = util::formatApplicationId(tid);
if (versions.find(tid_string) != versions.end()) {
u32 latest = versions.at(tid_string).at("latest");
if (version < latest) {
listItem->setSubLabel(fmt::format("{}\t|\t v{} (local) → v{} (latest)", tid_string, version, latest));
list->addView(listItem);
}
}
/* else {
listItem->setSubLabel(fmt::format("{}\t|\t {}", tid_string, "menus/tools/latest_version_not_found"_i18n));
list->addView(listItem);
} */
}