1
0
Fork 0
mirror of https://github.com/HamletDuFromage/aio-switch-updater.git synced 2024-11-09 12:01:44 +00:00
AIO-switch-updater/source/app_page.cpp

266 lines
8.1 KiB
C++
Raw Normal View History

2020-09-20 01:21:28 +01:00
#include "app_page.hpp"
2021-02-10 16:28:47 +00:00
#include "current_cfw.hpp"
#include "worker_page.hpp"
#include "confirm_page.hpp"
#include "download_cheats_page.hpp"
2021-03-16 14:56:46 +00:00
#include "utils.hpp"
#include "extract.hpp"
2021-03-16 14:56:46 +00:00
#include "fs.hpp"
#include <switch.h>
#include <filesystem>
#include <fstream>
2021-03-16 14:56:46 +00:00
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
NsApplicationRecord *records = new NsApplicationRecord[MaxTitleCount];
NsApplicationControlData *controlData = NULL;
std::string name;
2020-09-20 01:21:28 +01:00
s32 recordCount = 0;
u64 controlSize = 0;
u64 tid;
//titles = fs::readLineByLine(UPDATED_TITLES_PATH);
if (!util::isApplet()) {
if (R_SUCCEEDED(nsListApplicationRecord(records, MaxTitleCount, 0, &recordCount))){
for (s32 i = 0; i < recordCount; i++){
controlSize = 0;
if(R_FAILED(InitControlData(&controlData))) break;
tid = records[i].application_id;
if R_FAILED(GetControlData(tid, controlData, controlSize, name)) continue;
this->CreateGameListItem(name, tid, &controlData);
list->addView(listItem);
2020-09-20 01:21:28 +01:00
free(controlData);
}
delete[] records;
}
}
else {
tid = GetCurrentApplicationId();
if (R_SUCCEEDED(InitControlData(&controlData)) && R_SUCCEEDED(GetControlData(tid, controlData, controlSize, name))) {
this->CreateGameListItem(name, tid, &controlData);
2020-09-20 01:21:28 +01:00
list->addView(listItem);
}
label = new brls::Label(brls::LabelStyle::SMALL, "menus/cheats/applet_mode_not_supported"_i18n, true);
list->addView(label);
2020-09-20 01:21:28 +01:00
}
this->CreateDownloadAllButton();
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 = "";
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);
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(
new ConfirmPage(stagedFrame, text)
);
stagedFrame->addStage(
new WorkerPage(stagedFrame, "menus/common/downloading"_i18n, [url](){util::downloadArchive(url, archiveType::cheats);})
2020-09-20 01:21:28 +01:00
);
stagedFrame->addStage(
new WorkerPage(stagedFrame, "menus/common/extracting"_i18n, [](){util::extractArchive(archiveType::cheats);})
2020-09-20 01:21:28 +01:00
);
stagedFrame->addStage(
2021-03-10 20:54:17 +00:00
new ConfirmPage(stagedFrame, "menus/common/all_done"_i18n, true)
2020-09-20 01:21:28 +01:00
);
brls::Application::pushView(stagedFrame);
});
list->addView(download);
}
u32 AppPage::InitControlData(NsApplicationControlData** controlData) {
*controlData = (NsApplicationControlData*)malloc(sizeof(NsApplicationControlData));
if(*controlData == NULL) {
free(*controlData);
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);
if(R_FAILED(rc)) return rc;
if(controlSize < sizeof(controlData->nacp)) return 100;
rc = nacpGetLanguageEntry(&controlData->nacp, &langEntry);
if(R_FAILED(rc)) return rc;
if (!langEntry->name) return 200;
name = langEntry->name;
return 0;
}
void AppPage::CreateGameListItem(const std::string& name, u64 tid, NsApplicationControlData **controlData)
{
listItem = new brls::ListItem(name, "", util::formatApplicationId(tid));
listItem->setThumbnail((*controlData)->icon, sizeof((*controlData)->icon));
}
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();
}
void AppPage_CheatSlips::CreateLabel()
{
this->setTitle("menus/cheats/cheastlips_title"_i18n);
label = new brls::Label(brls::LabelStyle::DESCRIPTION, "menus/cheats/cheatslips_select"_i18n, true);
list->addView(label);
}
void AppPage_CheatSlips::CreateGameListItem(const std::string& name, u64 tid, NsApplicationControlData **controlData)
{
AppPage::CreateGameListItem(name, tid, controlData);
listItem->getClickEvent()->subscribe([&, tid, name](brls::View* view) { brls::Application::pushView(new DownloadCheatsPage_CheatSlips(tid, name)); });
}
AppPage_Gbatemp::AppPage_Gbatemp() : AppPage()
{
this->PopulatePage();
this->setIcon("romfs:/gbatemp_icon.png");
}
void AppPage_Gbatemp::CreateLabel()
{
this->setTitle("menus/cheats/gbatemp_title"_i18n);
label = new brls::Label( brls::LabelStyle::DESCRIPTION,"menus/cheats/cheatslips_select"_i18n,true);
list->addView(label);
}
void AppPage_Gbatemp::CreateGameListItem(const std::string& name, u64 tid, NsApplicationControlData **controlData)
{
AppPage::CreateGameListItem(name, tid, controlData);
listItem->getClickEvent()->subscribe([&, tid, name](brls::View* view) { brls::Application::pushView(new DownloadCheatsPage_GbaTemp(tid, name)); });
}
AppPage_Exclude::AppPage_Exclude() : AppPage()
{
this->PopulatePage();
}
void AppPage_Exclude::CreateLabel()
{
this->setTitle("menus/cheats/exclude_titles"_i18n);
label = new brls::Label( brls::LabelStyle::DESCRIPTION, "menus/cheats/exclude_titles_desc"_i18n, true);
list->addView(label);
}
void AppPage_Exclude::PopulatePage()
{
this->CreateLabel();
NsApplicationRecord *records = new NsApplicationRecord[MaxTitleCount];
NsApplicationControlData *controlData = NULL;
std::string name;
s32 recordCount = 0;
u64 controlSize = 0;
u64 tid;
auto titles = fs::readLineByLine(CHEATS_EXCLUDE);
if (!util::isApplet()) {
if (R_SUCCEEDED(nsListApplicationRecord(records, MaxTitleCount, 0, &recordCount))){
for (s32 i = 0; i < recordCount; i++){
controlSize = 0;
if(R_FAILED(InitControlData(&controlData))) break;
tid = records[i].application_id;
if R_FAILED(GetControlData(tid, controlData, controlSize, name)) continue;
brls::ToggleListItem *listItem;
listItem = new brls::ToggleListItem(util::formatListItemTitle(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);
}
delete[] records;
}
}
else {
label = new brls::Label(brls::LabelStyle::SMALL, "menus/common/applet_mode_not_supported"_i18n, true);
list->addView(label);
}
list->registerAction("menus/cheats/exclude_titles_save"_i18n, brls::Key::B, [this] {
std::set<std::string> exclude;
for (const auto& item : items) {
if(!item.first->getToggleState()) {
exclude.insert(item.second);
}
}
extract::writeTitlesToFile(exclude, CHEATS_EXCLUDE);
brls::Application::popView();
return true;
});
this->CreateDownloadAllButton();
2020-09-20 01:21:28 +01:00
this->setContentView(list);
}