1
0
Fork 0
mirror of https://github.com/HamletDuFromage/aio-switch-updater.git synced 2024-11-28 20:32:06 +00:00
AIO-switch-updater/source/download_cheats_page.cpp

259 lines
9.2 KiB
C++
Raw Normal View History

#include "download_cheats_page.hpp"
2021-03-16 14:56:46 +00:00
#include <fstream>
#include <filesystem>
#include "constants.hpp"
2021-02-10 16:28:47 +00:00
#include "download.hpp"
#include "utils.hpp"
2021-03-16 14:56:46 +00:00
#include "fs.hpp"
2021-02-10 16:28:47 +00:00
#include "current_cfw.hpp"
2021-03-16 14:56:46 +00:00
namespace i18n = brls::i18n;
using namespace i18n::literals;
using json = nlohmann::json;
DownloadCheatsPage::DownloadCheatsPage(uint64_t tid) : AppletFrame(true, true)
{
2021-03-10 20:54:17 +00:00
this->setTitle("menus/cheats/menu"_i18n);
2021-02-10 17:41:13 +00:00
std::string bid = "";
if(CurrentCfw::running_cfw == CFW::ams)
2021-02-10 17:41:13 +00:00
bid = GetBuilID(tid);
if(bid == "")
bid = GetBuilIDFromFile(tid);
list = new brls::List();
label = new brls::Label(
brls::LabelStyle::DESCRIPTION,
2021-03-01 18:19:17 +00:00
"menus/cheats/cheatslips_dl"_i18n + "\n\uE016 Build ID: " + bid,
true
);
list->addView(label);
if(bid != "") {
std::vector<std::string> headers = {"accept: application/json"};
json cheatsInfo = download::getRequest(CHEATSLIPS_CHEATS_URL + util::formatApplicationId(tid) + "/" + bid, headers);
if(cheatsInfo.find("cheats") != cheatsInfo.end()) {
for (const auto& p : cheatsInfo["cheats"].items()) {
json cheat = p.value();
2021-02-16 17:03:14 +00:00
listItem = new::brls::ToggleListItem(GetCheatsTitle(cheat), 0, "", "\uE016", "o");
2021-03-10 20:54:17 +00:00
listItem->registerAction("menus/cheats/cheatslips_see_more"_i18n, brls::Key::Y, [this, cheat] {
if(cheat.find("titles") != cheat.end()) {
2021-02-09 15:27:18 +00:00
ShowCheatsContent(cheat["titles"]);
}
return true;
});
toggles.push_back(std::make_pair(listItem, cheat["id"]));
list->addView(listItem);
}
list->addView(new brls::ListItemGroupSpacing(true));
}
}
2021-02-10 17:41:13 +00:00
else {
label = new brls::Label(
brls::LabelStyle::REGULAR,
2021-03-01 18:19:17 +00:00
"menus/cheats/bid_not_found"_i18n,
2021-02-10 17:41:13 +00:00
true
);
list->addView(label);
}
2021-03-01 18:19:17 +00:00
list->registerAction((bid != "") ? "menus/cheats/cheatslips_dl_cheats"_i18n : "brls/hints/back"_i18n, brls::Key::B, [this, bid, tid] {
std::vector<int> ids;
for(auto& e : toggles){
if(e.first->getToggleState()){
ids.push_back(e.second);
}
}
2021-02-06 18:42:14 +00:00
int error = 0;
if(!ids.empty()) {
json token;
std::ifstream tokenFile(TOKEN_PATH);
tokenFile >> token;
tokenFile.close();
std::vector<std::string> headers = {"accept: application/json"};
if(token.find("token") != token.end()) {
headers.push_back("X-API-TOKEN: " + token["token"].get<std::string>());
}
json cheatsInfo = download::getRequest("https://www.cheatslips.com/api/v1/cheats/" + util::formatApplicationId(tid) + "/" + bid, headers);
if(cheatsInfo.find("cheats") != cheatsInfo.end()) {
for (const auto& p : cheatsInfo["cheats"].items()) {
if(std::find(ids.begin(), ids.end(), p.value()["id"]) != ids.end()) {
2021-02-06 18:42:14 +00:00
if(p.value()["content"].get<std::string>() == "Quota exceeded for today !"){
error = 1;
}
else {
WriteCheats(tid, bid, p.value()["content"]);
}
}
}
}
else {
2021-02-06 18:42:14 +00:00
error = 2;
}
if(error != 0){
brls::Dialog* dialog;
switch(error){
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();
}
}
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-03-01 18:19:17 +00:00
del = new brls::ListItem("menus/cheats/delete_file"_i18n);
del->getClickEvent()->subscribe([this, tid, bid](brls::View* view) {
DeleteCheats(tid, bid);
2021-03-01 18:19:17 +00:00
brls::Dialog* dialog = new brls::Dialog("menus/common/all_done"_i18n);
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();
});
list->addView(del);
this->setContentView(list);
}
std::string DownloadCheatsPage::GetBuilID(uint64_t tid) {
2021-02-10 17:41:13 +00:00
static Service g_dmntchtSrv;
DmntCheatProcessMetadata metadata;
smGetService(&g_dmntchtSrv, "dmnt:cht");
serviceDispatch(&g_dmntchtSrv, 65003);
serviceDispatchOut(&g_dmntchtSrv, 65002, metadata);
serviceClose(&g_dmntchtSrv);
if(metadata.title_id == tid){
u8 buildID[0x20];
memcpy(buildID, metadata.main_nso_build_id, 0x20);
std::stringstream ss;
for (u8 i = 0; i < 8; i++)
ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << (u16)buildID[i];
return ss.str();
}
else{
return "";
}
}
std::string DownloadCheatsPage::GetBuilIDFromFile(uint64_t tid) {
NsApplicationContentMetaStatus *MetaSatus = new NsApplicationContentMetaStatus[100U];
s32 out;
nsListApplicationContentMetaStatus(tid, 0, MetaSatus, 100, &out);
u32 v = 0;
for(int i = 0; i < out ; i++) {
if(v < MetaSatus[i].version) v = MetaSatus[i].version;
}
this->setFooterText("Game version: v" + std::to_string(v / 0x10000));
std::string version = std::to_string(v);
std::string versions_str = download::downloadPage(VERSIONS_DIRECTORY + util::formatApplicationId(tid) + ".json");
json versions;
if (json::accept(versions_str)) versions = json::parse(versions_str);
else versions = json::object();
if(versions.find(version) != versions.end()) {
return versions[version];
}
return "";
}
std::string DownloadCheatsPage::GetCheatsTitle(json cheat) {
std::string res = "";
if(cheat.find("titles") != cheat.end()) {
for(auto& p : cheat["titles"]){
res += "[" + p.get<std::string>() + "]" + " - ";
}
}
res.erase(res.length() - 3);
if(res.size() > 80){
res = res.substr(0, 79) + "\u2026";
}
return res;
}
void DownloadCheatsPage::WriteCheats(uint64_t tid, std::string bid, std::string cheatContent) {
std::string path;
std::string tidstr = util::formatApplicationId(tid);
switch(CurrentCfw::running_cfw){
case CFW::ams:
path = std::string(AMS_PATH) + std::string(CONTENTS_PATH);
break;
case CFW::rnx:
path = std::string(REINX_PATH) + std::string(CONTENTS_PATH);
break;
case CFW::sxos:
path = std::string(SXOS_PATH) + std::string(TITLES_PATH);
break;
}
path += tidstr + "/cheats/";
2021-03-16 14:56:46 +00:00
fs::createTree(path);
path += bid + ".txt";
std::ofstream cheatFile;
cheatFile.open(path, std::ios::app);
2021-02-24 19:44:15 +00:00
cheatFile << "\n\n" << cheatContent;
std::ofstream updated;
updated.open(UPDATED_TITLES_PATH, std::ios::app);
updated << "\n" << tidstr;
}
void DownloadCheatsPage::DeleteCheats(uint64_t tid, std::string bid) {
std::string path;
switch(CurrentCfw::running_cfw){
case CFW::ams:
path = std::string(AMS_PATH) + std::string(CONTENTS_PATH);
break;
case CFW::rnx:
path = std::string(REINX_PATH) + std::string(CONTENTS_PATH);
break;
case CFW::sxos:
path = std::string(SXOS_PATH) + std::string(TITLES_PATH);
break;
}
std::filesystem::remove(path + util::formatApplicationId(tid) + "/cheats/" + bid + ".txt");
2021-02-09 15:27:18 +00:00
}
void DownloadCheatsPage::ShowCheatsContent(nlohmann::ordered_json titles) {
brls::AppletFrame* appView = new brls::AppletFrame(true, true);
brls::List* list = new brls::List();
brls::ListItem* listItem;
for(auto& p : titles){
listItem = new brls::ListItem(p.get<std::string>());
2021-02-09 15:43:03 +00:00
listItem->registerAction("", brls::Key::A, [this] {
return true;
});
2021-02-09 15:27:18 +00:00
list->addView(listItem);
}
appView->setContentView(list);
2021-03-01 18:19:17 +00:00
brls::PopupFrame::open("menus/cheats/sheet_content"_i18n, appView, "", "");
}