2021-02-06 17:24:47 +00:00
|
|
|
#include "download_cheats_page.hpp"
|
|
|
|
#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-06-28 15:58:04 +01:00
|
|
|
#include "extract.hpp"
|
2021-02-10 16:28:47 +00:00
|
|
|
#include "current_cfw.hpp"
|
2021-06-27 23:46:00 +01:00
|
|
|
#include <fstream>
|
|
|
|
#include <filesystem>
|
2021-03-16 14:56:46 +00:00
|
|
|
|
2021-02-06 17:24:47 +00:00
|
|
|
namespace i18n = brls::i18n;
|
|
|
|
using namespace i18n::literals;
|
|
|
|
using json = nlohmann::json;
|
|
|
|
|
2021-07-16 16:33:57 +01:00
|
|
|
namespace show_cheats {
|
|
|
|
|
|
|
|
void ShowCheatFiles(u64 tid, const std::string& name) {
|
|
|
|
std::string path = util::getContentsPath();
|
|
|
|
path += util::formatApplicationId(tid) + "/cheats/";
|
|
|
|
|
|
|
|
brls::TabFrame* appView = new brls::TabFrame();
|
|
|
|
bool is_populated = false;
|
|
|
|
if(std::filesystem::exists(path)) {
|
|
|
|
for(const auto& cheatFile : std::filesystem::directory_iterator(path)){
|
|
|
|
is_populated |= CreateCheatList(cheatFile.path(), &appView);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(is_populated) {
|
|
|
|
brls::PopupFrame::open(name, appView, "");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
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::TabFrame** appView) {
|
|
|
|
bool res = false;
|
|
|
|
brls::List* cheatsList = new brls::List();
|
|
|
|
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));
|
|
|
|
|
|
|
|
std::string str;
|
|
|
|
std::regex cheats_expr(R"(\[.+\])");
|
|
|
|
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));
|
|
|
|
res = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(res) {
|
|
|
|
(*appView)->addTab(util::upperCase(path.filename().stem()), cheatsList);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-27 23:46:00 +01:00
|
|
|
DownloadCheatsPage::DownloadCheatsPage(uint64_t tid, const std::string& name) : AppletFrame(true, true), tid(tid)
|
2021-02-06 17:24:47 +00:00
|
|
|
{
|
2021-06-27 23:46:00 +01:00
|
|
|
list = new brls::List();
|
2021-06-05 18:10:51 +01:00
|
|
|
GetVersion();
|
|
|
|
GetBuildID();
|
2021-07-15 20:16:56 +01:00
|
|
|
this->setTitle(name);
|
2021-07-16 16:33:57 +01:00
|
|
|
this->setFooterText("v" + std::to_string(this->version / 0x10000));
|
|
|
|
this->registerAction("menus/cheats/show_existing"_i18n , brls::Key::X, [this, tid, name] {
|
|
|
|
show_cheats::ShowCheatFiles(tid, name);
|
|
|
|
return true;
|
|
|
|
});
|
2021-06-05 18:10:51 +01:00
|
|
|
}
|
2021-02-06 17:24:47 +00:00
|
|
|
|
2021-06-05 18:10:51 +01:00
|
|
|
void DownloadCheatsPage::GetBuildID() {
|
2021-03-16 02:04:21 +00:00
|
|
|
if(CurrentCfw::running_cfw == CFW::ams)
|
2021-06-05 18:10:51 +01:00
|
|
|
GetBuildIDFromDmnt();
|
|
|
|
if(this->bid == "")
|
|
|
|
GetBuildIDFromFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
if(metadata.title_id == this->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];
|
|
|
|
this->bid = ss.str();
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
this->bid = "";
|
|
|
|
}
|
|
|
|
}
|
2021-02-10 17:41:13 +00:00
|
|
|
|
2021-06-05 18:10:51 +01:00
|
|
|
void DownloadCheatsPage::GetVersion() {
|
|
|
|
NsApplicationContentMetaStatus *MetaSatus = new NsApplicationContentMetaStatus[100U];
|
|
|
|
s32 out;
|
|
|
|
nsListApplicationContentMetaStatus(this->tid, 0, MetaSatus, 100, &out);
|
|
|
|
for(int i = 0; i < out ; i++) {
|
|
|
|
if(version < MetaSatus[i].version) this->version = MetaSatus[i].version;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadCheatsPage::GetBuildIDFromFile() {
|
2021-06-27 23:46:00 +01:00
|
|
|
nlohmann::ordered_json versions_json;
|
|
|
|
download::getRequest(VERSIONS_DIRECTORY + util::formatApplicationId(this->tid) + ".json", versions_json);
|
2021-06-05 18:10:51 +01:00
|
|
|
|
|
|
|
std::string version_str = std::to_string(this->version);
|
|
|
|
if(versions_json.find(version_str) != versions_json.end()) {
|
|
|
|
this->bid = versions_json[version_str];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this->bid = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 13:29:55 +01:00
|
|
|
void DownloadCheatsPage::WriteCheats(const std::string& cheatContent) {
|
2021-07-15 18:56:59 +01:00
|
|
|
std::string path = util::getContentsPath();
|
2021-06-05 18:10:51 +01:00
|
|
|
std::string tidstr = util::formatApplicationId(this->tid);
|
|
|
|
path += tidstr + "/cheats/";
|
|
|
|
fs::createTree(path);
|
|
|
|
path += this->bid + ".txt";
|
|
|
|
std::ofstream cheatFile;
|
|
|
|
cheatFile.open(path, std::ios::app);
|
|
|
|
cheatFile << "\n\n" << cheatContent;
|
2021-06-27 23:46:00 +01:00
|
|
|
/* std::ofstream updated;
|
2021-06-05 18:10:51 +01:00
|
|
|
updated.open(UPDATED_TITLES_PATH, std::ios::app);
|
2021-06-27 23:46:00 +01:00
|
|
|
updated << "\n" << tidstr; */
|
2021-06-05 18:10:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadCheatsPage::DeleteCheats() {
|
2021-07-15 18:56:59 +01:00
|
|
|
std::filesystem::remove(util::getContentsPath() + util::formatApplicationId(this->tid) + "/cheats/" + this->bid + ".txt");
|
2021-06-05 18:10:51 +01:00
|
|
|
}
|
|
|
|
|
2021-06-27 23:46:00 +01:00
|
|
|
DownloadCheatsPage_CheatSlips::DownloadCheatsPage_CheatSlips(uint64_t tid, const std::string& name) : DownloadCheatsPage(tid, name)
|
2021-06-05 18:10:51 +01:00
|
|
|
{
|
2021-02-06 17:24:47 +00:00
|
|
|
label = new brls::Label(
|
|
|
|
brls::LabelStyle::DESCRIPTION,
|
2021-06-05 18:10:51 +01:00
|
|
|
"menus/cheats/cheatslips_dl"_i18n + "\n\uE016 Build ID: " + this->bid,
|
2021-02-06 17:24:47 +00:00
|
|
|
true
|
|
|
|
);
|
|
|
|
list->addView(label);
|
|
|
|
|
2021-06-05 18:10:51 +01:00
|
|
|
if(this->bid != "") {
|
2021-02-06 17:24:47 +00:00
|
|
|
std::vector<std::string> headers = {"accept: application/json"};
|
2021-06-27 23:46:00 +01:00
|
|
|
nlohmann::ordered_json cheatsInfo;
|
|
|
|
download::getRequest(CHEATSLIPS_CHEATS_URL + util::formatApplicationId(this->tid) + "/" + this->bid, cheatsInfo, headers);
|
2021-02-06 17:24:47 +00:00
|
|
|
if(cheatsInfo.find("cheats") != cheatsInfo.end()) {
|
|
|
|
for (const auto& p : cheatsInfo["cheats"].items()) {
|
|
|
|
json cheat = p.value();
|
2021-06-20 17:44:37 +01:00
|
|
|
try {
|
|
|
|
listItem = new::brls::ToggleListItem(GetCheatsTitle(cheat), 0, "", "\uE016", "o");
|
|
|
|
} catch (const std::out_of_range& e) {
|
|
|
|
//Empty titles
|
|
|
|
continue;
|
|
|
|
} catch (...) {
|
|
|
|
//Something else went wrong
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-10 20:54:17 +00:00
|
|
|
listItem->registerAction("menus/cheats/cheatslips_see_more"_i18n, brls::Key::Y, [this, cheat] {
|
2021-02-06 17:24:47 +00:00
|
|
|
if(cheat.find("titles") != cheat.end()) {
|
2021-02-09 15:27:18 +00:00
|
|
|
ShowCheatsContent(cheat["titles"]);
|
2021-02-06 17:24:47 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
toggles.push_back(std::make_pair(listItem, cheat["id"]));
|
|
|
|
list->addView(listItem);
|
|
|
|
}
|
2021-06-20 17:44:37 +01:00
|
|
|
if(list->getViewsCount() > 1)
|
|
|
|
list->addView(new brls::ListItemGroupSpacing(true));
|
2021-02-06 17:24:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-05 18:10:51 +01:00
|
|
|
list->registerAction((this->bid != "") ? "menus/cheats/cheatslips_dl_cheats"_i18n : "brls/hints/back"_i18n, brls::Key::B, [this] {
|
2021-02-06 17:24:47 +00:00
|
|
|
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;
|
2021-02-06 17:24:47 +00:00
|
|
|
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>());
|
|
|
|
}
|
2021-06-27 23:46:00 +01:00
|
|
|
nlohmann::ordered_json cheatsInfo;
|
|
|
|
download::getRequest("https://www.cheatslips.com/api/v1/cheats/" + util::formatApplicationId(this->tid) + "/" + this->bid, cheatsInfo, headers);
|
2021-02-06 17:24:47 +00:00
|
|
|
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 {
|
2021-06-05 18:10:51 +01:00
|
|
|
WriteCheats(p.value()["content"]);
|
2021-02-06 18:42:14 +00:00
|
|
|
}
|
2021-02-06 17:24:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2021-02-06 17:24:47 +00:00
|
|
|
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);
|
2021-02-06 17:24:47 +00:00
|
|
|
dialog->setCancelable(true);
|
|
|
|
dialog->open();
|
|
|
|
}
|
|
|
|
}
|
2021-02-15 14:19:49 +00: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);
|
2021-02-15 14:19:49 +00:00
|
|
|
dialog->setCancelable(true);
|
|
|
|
dialog->open();
|
|
|
|
while(result == false){
|
|
|
|
usleep(1);
|
|
|
|
result = dialogResult;
|
|
|
|
}
|
|
|
|
dialogResult = false; */
|
|
|
|
brls::Application::popView();
|
|
|
|
}
|
2021-02-06 17:24:47 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2021-03-01 18:19:17 +00:00
|
|
|
del = new brls::ListItem("menus/cheats/delete_file"_i18n);
|
2021-06-05 18:10:51 +01:00
|
|
|
del->getClickEvent()->subscribe([this](brls::View* view) {
|
|
|
|
DeleteCheats();
|
2021-03-01 18:19:17 +00:00
|
|
|
brls::Dialog* dialog = new brls::Dialog("menus/common/all_done"_i18n);
|
2021-02-06 17:24:47 +00:00
|
|
|
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);
|
2021-02-06 17:24:47 +00:00
|
|
|
dialog->setCancelable(true);
|
|
|
|
dialog->open();
|
|
|
|
});
|
|
|
|
list->addView(del);
|
|
|
|
this->setContentView(list);
|
|
|
|
}
|
|
|
|
|
2021-06-05 18:10:51 +01:00
|
|
|
std::string DownloadCheatsPage_CheatSlips::GetCheatsTitle(json cheat) {
|
2021-02-06 17:24:47 +00:00
|
|
|
std::string res = "";
|
|
|
|
if(cheat.find("titles") != cheat.end()) {
|
|
|
|
for(auto& p : cheat["titles"]){
|
|
|
|
res += "[" + p.get<std::string>() + "]" + " - ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-06-05 18:10:51 +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();
|
|
|
|
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, "", "");
|
2021-06-05 18:10:51 +01:00
|
|
|
}
|
|
|
|
|
2021-06-27 23:46:00 +01:00
|
|
|
DownloadCheatsPage_GbaTemp::DownloadCheatsPage_GbaTemp(uint64_t tid, const std::string& name) : DownloadCheatsPage(tid, name)
|
2021-06-05 18:10:51 +01:00
|
|
|
{
|
|
|
|
label = new brls::Label(
|
|
|
|
brls::LabelStyle::DESCRIPTION,
|
|
|
|
fmt::format("menus/cheats/gbatemp_dl"_i18n, this->bid),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
list->addView(label);
|
|
|
|
|
|
|
|
if(this->bid != "") {
|
2021-06-27 23:46:00 +01:00
|
|
|
nlohmann::ordered_json cheatsJson;
|
|
|
|
download::getRequest(CHEATS_DIRECTORY + util::formatApplicationId(this->tid) + ".json", cheatsJson);
|
2021-06-05 18:10:51 +01:00
|
|
|
if(cheatsJson.find(this->bid) != cheatsJson.end()) {
|
|
|
|
for (const auto& p : cheatsJson[this->bid].items()) {
|
|
|
|
json cheat = p.value();
|
|
|
|
listItem = new::brls::ListItem(cheat["title"]);
|
|
|
|
listItem->registerAction("menus/cheats/gbatemp_dl_cheatcode"_i18n, brls::Key::A, [this, cheat] {
|
|
|
|
WriteCheats(cheat["content"]);
|
|
|
|
brls::Dialog* dialog = new brls::Dialog(fmt::format("menus/cheats/gbatemp_dl_successful_dl"_i18n, cheat["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;
|
|
|
|
});
|
|
|
|
list->addView(listItem);
|
|
|
|
}
|
|
|
|
list->addView(new brls::ListItemGroupSpacing(true));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
label = new brls::Label(
|
|
|
|
brls::LabelStyle::REGULAR,
|
|
|
|
"menus/cheats/bid_not_found"_i18n,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
list->addView(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
del = new brls::ListItem("menus/cheats/delete_file"_i18n);
|
|
|
|
del->getClickEvent()->subscribe([this](brls::View* view) {
|
|
|
|
DeleteCheats();
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
list->addView(del);
|
|
|
|
this->setContentView(list);
|
|
|
|
}
|
|
|
|
|