1
0
Fork 0
mirror of https://github.com/HamletDuFromage/aio-switch-updater.git synced 2024-11-08 19:41:49 +00:00
AIO-switch-updater/source/download_cheats_page.cpp

388 lines
14 KiB
C++
Raw Normal View History

#include "download_cheats_page.hpp"
2021-09-11 14:48:13 +01:00
#include <filesystem>
#include <fstream>
#include "constants.hpp"
2021-09-11 14:48:13 +01:00
#include "current_cfw.hpp"
2021-02-10 16:28:47 +00:00
#include "download.hpp"
#include "extract.hpp"
2021-09-11 14:48:13 +01:00
#include "fs.hpp"
#include "utils.hpp"
2021-03-16 14:56:46 +00:00
namespace i18n = brls::i18n;
using namespace i18n::literals;
using json = nlohmann::json;
namespace show_cheats {
void ShowCheatSheet(u64 tid, const std::string& bid, const std::string& name)
{
std::string path = fmt::format("{}{}/cheats/{}.txt", util::getContentsPath(), util::formatApplicationId(tid), bid);
brls::AppletFrame* appView = new brls::AppletFrame(true, true);
brls::List* cheatsList = new brls::List();
if (std::filesystem::exists(path) && CreateCheatList(path, &cheatsList)) {
appView->setContentView(cheatsList);
appView->registerAction("menus/cheats/delete_file"_i18n, brls::Key::X, [tid, bid] {
DeleteCheats(tid, bid);
brls::Dialog* dialog = new brls::Dialog("menus/common/all_done"_i18n);
brls::GenericEvent::Callback callback = [dialog](brls::View* view) {
dialog->close();
};
dialog->addButton("menus/common/ok"_i18n, callback);
dialog->setCancelable(true);
dialog->open();
return true;
});
brls::PopupFrame::open(name, appView, "");
}
else {
NoCheatsFoundPopup();
}
}
2021-09-11 14:48:13 +01:00
void ShowCheatFiles(u64 tid, const std::string& name)
{
std::string path = util::getContentsPath();
path += util::formatApplicationId(tid) + "/cheats/";
2021-09-11 14:48:13 +01:00
brls::TabFrame* appView = new brls::TabFrame();
bool is_populated = false;
if (std::filesystem::exists(path)) {
for (const auto& cheatFile : std::filesystem::directory_iterator(path)) {
brls::List* cheatsList = new brls::List();
is_populated |= CreateCheatList(cheatFile.path(), &cheatsList);
if (is_populated) {
appView->addTab(util::upperCase(cheatFile.path().stem()), cheatsList);
}
2021-09-11 14:48:13 +01:00
}
}
if (is_populated) {
brls::PopupFrame::open(name, appView, "");
}
else {
NoCheatsFoundPopup();
}
}
void NoCheatsFoundPopup()
{
brls::Dialog* dialog = new brls::Dialog("menus/cheats/not_found"_i18n);
brls::GenericEvent::Callback callback = [dialog](brls::View* view) {
dialog->close();
};
dialog->addButton("menus/common/ok"_i18n, callback);
dialog->setCancelable(true);
dialog->open();
}
bool CreateCheatList(const std::filesystem::path& path, brls::List** cheatsList)
2021-09-11 14:48:13 +01:00
{
bool res = false;
if (extract::isBID(path.filename().stem())) {
(*cheatsList)->addView(new brls::Label(brls::LabelStyle::DESCRIPTION, fmt::format("menus/cheats/cheatfile_label"_i18n, path.filename().string()), true));
2021-09-11 14:48:13 +01:00
std::string str;
std::regex cheats_expr(R"(\[.+\]|\{.+\})");
2021-09-11 14:48:13 +01:00
std::ifstream in(path);
if (in) {
while (std::getline(in, str)) {
if (str.size() > 0) {
if (std::regex_search(str, cheats_expr)) {
(*cheatsList)->addView(new brls::ListItem(str));
2021-09-11 14:48:13 +01:00
res = true;
}
}
}
}
}
2021-09-11 14:48:13 +01:00
return res;
}
void DeleteCheats(u64 tid, const std::string& bid)
{
std::filesystem::remove(fmt::format("{}{:016X}/cheats/{}.txt", util::getContentsPath(), tid, bid));
}
2022-02-20 15:33:54 +00:00
2021-09-11 14:48:13 +01:00
} // namespace show_cheats
2021-10-26 14:48:24 +01:00
DownloadCheatsPage::DownloadCheatsPage(uint64_t tid, const std::string& name) : AppletFrame(true, true), tid(tid), name(name)
{
this->list = new brls::List();
this->GetVersion();
this->GetBuildID();
2021-10-26 14:48:24 +01:00
this->setTitle(this->name);
this->setFooterText("v" + std::to_string(this->version / 0x10000));
2021-10-26 14:48:24 +01:00
this->brls::AppletFrame::registerAction("menus/cheats/show_existing"_i18n, brls::Key::X, [this] {
show_cheats::ShowCheatSheet(this->tid, this->bid, this->name);
2021-09-11 14:48:13 +01:00
return true;
});
this->rebuildHints();
}
2021-09-11 14:48:13 +01:00
void DownloadCheatsPage::GetBuildID()
{
if (CurrentCfw::running_cfw == CFW::ams)
this->GetBuildIDFromDmnt();
2021-09-11 14:48:13 +01:00
if (this->bid == "")
this->GetBuildIDFromFile();
}
2021-09-11 14:48:13 +01:00
void DownloadCheatsPage::GetBuildIDFromDmnt()
{
static Service g_dmntchtSrv;
DmntCheatProcessMetadata metadata;
smGetService(&g_dmntchtSrv, "dmnt:cht");
serviceDispatch(&g_dmntchtSrv, 65003);
serviceDispatchOut(&g_dmntchtSrv, 65002, metadata);
serviceClose(&g_dmntchtSrv);
2021-09-11 14:48:13 +01:00
if (metadata.title_id == this->tid) {
u64 buildID = 0;
memcpy(&buildID, metadata.main_nso_build_id, sizeof(u64));
this->bid = fmt::format("{:016X}", __builtin_bswap64(buildID));
}
}
2021-02-10 17:41:13 +00:00
2021-09-11 14:48:13 +01:00
void DownloadCheatsPage::GetVersion()
{
NsApplicationContentMetaStatus* MetaSatus = new NsApplicationContentMetaStatus[100U];
s32 out;
nsListApplicationContentMetaStatus(this->tid, 0, MetaSatus, 100, &out);
2021-09-11 14:48:13 +01:00
for (int i = 0; i < out; i++) {
if (version < MetaSatus[i].version) this->version = MetaSatus[i].version;
}
}
2021-09-11 14:48:13 +01:00
void DownloadCheatsPage::GetBuildIDFromFile()
{
nlohmann::ordered_json versions_json;
download::getRequest(VERSIONS_DIRECTORY + util::formatApplicationId(this->tid) + ".json", versions_json);
std::string version_str = std::to_string(this->version);
2021-09-11 14:48:13 +01:00
if (versions_json.find(version_str) != versions_json.end()) {
this->bid = versions_json[version_str];
}
else {
this->bid = "";
}
}
2021-09-11 14:48:13 +01:00
void DownloadCheatsPage::WriteCheats(const std::string& cheatContent)
{
2021-10-26 14:48:24 +01:00
std::string path = util::getContentsPath() + util::formatApplicationId(this->tid) + "/cheats/";
fs::createTree(path);
2021-10-26 14:48:24 +01:00
std::ofstream cheatFile(path + this->bid + ".txt", std::ios::app);
2021-09-11 14:48:13 +01:00
cheatFile << "\n\n"
<< cheatContent;
}
2021-10-26 14:48:24 +01:00
void DownloadCheatsPage::AddCheatsfileListItem()
{
brls::ListItem* item = new brls::ListItem("menus/cheats/show_cheat_files"_i18n);
item->getClickEvent()->subscribe([this](brls::View* view) {
show_cheats::ShowCheatFiles(this->tid, this->name);
});
this->list->addView(item);
}
2022-02-20 15:33:54 +00:00
void DownloadCheatsPage::ShowBidNotFound()
{
brls::Label* label = new brls::Label(
brls::LabelStyle::REGULAR,
"menus/cheats/bid_not_found"_i18n,
true);
this->list->addView(label);
}
void DownloadCheatsPage::ShowCheatsNotFound()
{
brls::Label* label = new brls::Label(
brls::LabelStyle::REGULAR,
"menus/cheats/cheats_not_found"_i18n,
true);
this->list->addView(label);
}
DownloadCheatsPage_CheatSlips::DownloadCheatsPage_CheatSlips(uint64_t tid, const std::string& name) : DownloadCheatsPage(tid, name)
{
2021-10-26 14:48:24 +01:00
this->label = new brls::Label(
brls::LabelStyle::DESCRIPTION,
"menus/cheats/cheatslips_dl"_i18n + "\n\uE016 Build ID: " + this->bid,
2021-09-11 14:48:13 +01:00
true);
2021-10-26 14:48:24 +01:00
this->list->addView(this->label);
2021-09-11 14:48:13 +01:00
if (this->bid != "") {
std::vector<std::string> headers = {"accept: application/json"};
nlohmann::ordered_json cheatsInfo;
download::getRequest(CHEATSLIPS_CHEATS_URL + util::formatApplicationId(this->tid) + "/" + this->bid, cheatsInfo, headers);
2021-09-11 14:48:13 +01:00
if (cheatsInfo.find("cheats") != cheatsInfo.end()) {
for (const auto& p : cheatsInfo.at("cheats").items()) {
json cheat = p.value();
2021-10-26 14:48:24 +01:00
listItem = new brls::ToggleListItem(GetCheatsTitle(cheat), 0, "", "\uE016", "o");
this->listItem->registerAction("menus/cheats/cheatslips_see_more"_i18n, brls::Key::Y, [this, cheat] {
2021-09-11 14:48:13 +01:00
if (cheat.find("titles") != cheat.end()) {
ShowCheatsContent(cheat.at("titles"));
}
return true;
});
2021-10-26 14:48:24 +01:00
this->toggles.push_back(std::make_pair(listItem, cheat.at("id")));
list->addView(listItem);
}
2021-10-26 14:48:24 +01:00
if (this->list->getViewsCount() > 1)
this->list->addView(new brls::ListItemGroupSpacing(true));
}
2022-02-20 15:33:54 +00:00
else {
ShowCheatsNotFound();
}
}
2021-02-10 17:41:13 +00:00
else {
2022-02-20 15:33:54 +00:00
ShowBidNotFound();
2021-02-10 17:41:13 +00:00
}
2021-10-26 14:48:24 +01:00
this->list->registerAction((this->bid != "") ? "menus/cheats/cheatslips_dl_cheats"_i18n : "brls/hints/back"_i18n, brls::Key::B, [this] {
std::vector<int> ids;
2021-09-11 14:48:13 +01:00
for (auto& e : toggles) {
if (e.first->getToggleState()) {
ids.push_back(e.second);
}
}
2021-02-06 18:42:14 +00:00
int error = 0;
2021-09-11 14:48:13 +01:00
if (!ids.empty()) {
json token;
std::ifstream tokenFile(TOKEN_PATH);
tokenFile >> token;
tokenFile.close();
std::vector<std::string> headers = {"accept: application/json"};
2021-09-11 14:48:13 +01:00
if (token.find("token") != token.end()) {
headers.push_back("X-API-TOKEN: " + token.at("token").get<std::string>());
}
nlohmann::ordered_json cheatsInfo;
download::getRequest("https://www.cheatslips.com/api/v1/cheats/" + util::formatApplicationId(this->tid) + "/" + this->bid, cheatsInfo, headers);
2021-09-11 14:48:13 +01:00
if (cheatsInfo.find("cheats") != cheatsInfo.end()) {
for (const auto& p : cheatsInfo.at("cheats").items()) {
2021-09-11 14:48:13 +01:00
if (std::find(ids.begin(), ids.end(), p.value().at("id")) != ids.end()) {
if (p.value().at("content").get<std::string>() == "Quota exceeded for today !") {
2021-02-06 18:42:14 +00:00
error = 1;
}
else {
WriteCheats(p.value().at("content"));
2021-02-06 18:42:14 +00:00
}
}
}
}
else {
2021-02-06 18:42:14 +00:00
error = 2;
}
2021-09-11 14:48:13 +01:00
if (error != 0) {
2021-02-06 18:42:14 +00:00
brls::Dialog* dialog;
2021-09-11 14:48:13 +01:00
switch (error) {
2021-02-06 18:42:14 +00:00
case 1:
2021-03-01 18:19:17 +00:00
dialog = new brls::Dialog("menus/cheats/quota"_i18n);
2021-02-06 18:42:14 +00:00
break;
case 2:
2021-03-01 18:19:17 +00:00
dialog = new brls::Dialog("menus/cheats/cheatslips_error"_i18n);
2021-02-06 18:42:14 +00:00
break;
}
brls::GenericEvent::Callback callback = [dialog](brls::View* view) {
dialog->close();
};
2021-03-11 01:46:22 +00:00
dialog->addButton("menus/common/ok"_i18n, callback);
dialog->setCancelable(true);
dialog->open();
}
}
2021-09-11 14:48:13 +01:00
if (error == 0) {
/* brls::Dialog* dialog = new brls::Dialog("menus/cheatslips_success"_i18n);
bool dialogResult = false;
bool result = false;
brls::GenericEvent::Callback callback = [dialog, &dialogResult](brls::View* view) {
dialogResult = true;
dialog->close();
};
2021-03-11 01:46:22 +00:00
dialog->addButton("menus/common/ok"_i18n, callback);
dialog->setCancelable(true);
dialog->open();
while(result == false){
usleep(1);
result = dialogResult;
}
dialogResult = false; */
brls::Application::popView();
}
return true;
});
2021-10-26 14:48:24 +01:00
this->AddCheatsfileListItem();
this->setContentView(list);
}
2021-09-11 14:48:13 +01:00
std::string DownloadCheatsPage_CheatSlips::GetCheatsTitle(json cheat)
{
std::string res = "";
2021-09-11 14:48:13 +01:00
if (cheat.find("titles") != cheat.end()) {
for (auto& p : cheat.at("titles")) {
2021-10-26 14:48:24 +01:00
res += "[" + p.get<std::string>() + "]" + " - ";
}
}
return res;
}
2021-09-11 14:48:13 +01:00
void DownloadCheatsPage_CheatSlips::ShowCheatsContent(nlohmann::ordered_json titles)
{
2021-02-09 15:27:18 +00:00
brls::AppletFrame* appView = new brls::AppletFrame(true, true);
brls::List* list = new brls::List();
2021-10-26 14:48:24 +01:00
brls::ListItem* titlesItem;
2021-09-11 14:48:13 +01:00
for (auto& p : titles) {
2021-10-26 14:48:24 +01:00
titlesItem = new brls::ListItem(p.get<std::string>());
titlesItem->registerAction("", brls::Key::A, [this] {
2021-02-09 15:43:03 +00:00
return true;
});
2021-10-26 14:48:24 +01:00
list->addView(titlesItem);
2021-02-09 15:27:18 +00:00
}
appView->setContentView(list);
2021-03-01 18:19:17 +00:00
brls::PopupFrame::open("menus/cheats/sheet_content"_i18n, appView, "", "");
}
DownloadCheatsPage_GbaTemp::DownloadCheatsPage_GbaTemp(uint64_t tid, const std::string& name) : DownloadCheatsPage(tid, name)
{
2021-10-26 14:48:24 +01:00
this->label = new brls::Label(
brls::LabelStyle::DESCRIPTION,
fmt::format("menus/cheats/gbatemp_dl"_i18n, this->bid),
2021-09-11 14:48:13 +01:00
true);
2021-10-26 14:48:24 +01:00
this->list->addView(label);
2021-09-11 14:48:13 +01:00
if (this->bid != "") {
nlohmann::ordered_json cheatsJson;
download::getRequest(CHEATS_DIRECTORY + util::formatApplicationId(this->tid) + ".json", cheatsJson);
2021-09-11 14:48:13 +01:00
if (cheatsJson.find(this->bid) != cheatsJson.end()) {
for (const auto& p : cheatsJson[this->bid].items()) {
json cheat = p.value();
2021-10-26 14:48:24 +01:00
this->listItem = new brls::ListItem(cheat.at("title"));
2021-10-03 18:54:09 +01:00
listItem->registerAction("menus/cheats/gbatemp_dl_cheatcode"_i18n, brls::Key::A, [this, cheat] {
WriteCheats(cheat.at("content"));
brls::Dialog* dialog = new brls::Dialog(fmt::format("menus/cheats/gbatemp_dl_successful_dl"_i18n, cheat.at("title")));
brls::GenericEvent::Callback callback = [dialog](brls::View* view) {
dialog->close();
};
dialog->addButton("menus/common/ok"_i18n, callback);
dialog->setCancelable(true);
dialog->open();
return true;
});
2021-10-26 14:48:24 +01:00
this->list->addView(listItem);
}
2021-10-26 14:48:24 +01:00
this->list->addView(new brls::ListItemGroupSpacing(true));
}
2022-02-20 15:33:54 +00:00
else {
ShowCheatsNotFound();
}
}
else {
2022-02-20 15:33:54 +00:00
ShowBidNotFound();
}
2021-10-26 14:48:24 +01:00
this->AddCheatsfileListItem();
this->setContentView(this->list);
}