Checkpoint/switch/source/cheatmanager.cpp

113 lines
4.3 KiB
C++
Raw Normal View History

2019-05-01 10:23:03 +00:00
/*
2019-05-06 20:51:09 +00:00
* This file is part of Checkpoint
* Copyright (C) 2017-2019 Bernardo Giordano, FlagBrew
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
2019-05-01 10:23:03 +00:00
#include "cheatmanager.hpp"
CheatManager::CheatManager(void)
2019-05-01 10:23:03 +00:00
{
mCheats = nullptr;
2019-05-06 20:51:09 +00:00
if (io::fileExists("/switch/Checkpoint/cheats.json")) {
2019-05-01 10:23:03 +00:00
const std::string path = "/switch/Checkpoint/cheats.json";
2019-05-06 20:51:09 +00:00
FILE* in = fopen(path.c_str(), "rt");
if (in != NULL) {
mCheats = std::make_shared<nlohmann::json>(nlohmann::json::parse(in, nullptr, false));
fclose(in);
}
2019-07-24 18:23:20 +00:00
else {
Logger::getInstance().log(Logger::WARN, "Failed to open " + path + " with errno %d.", errno);
2019-07-24 18:23:20 +00:00
}
2019-05-01 10:23:03 +00:00
}
2019-05-06 20:51:09 +00:00
else {
2019-05-01 10:23:03 +00:00
const std::string path = "romfs:/cheats/cheats.json.bz2";
// load compressed archive in memory
FILE* f = fopen(path.c_str(), "rb");
2019-05-06 20:51:09 +00:00
if (f != NULL) {
2019-05-01 10:23:03 +00:00
fseek(f, 0, SEEK_END);
2019-05-06 20:51:09 +00:00
u32 size = ftell(f);
2019-07-24 17:35:27 +00:00
unsigned int destLen = CHEAT_SIZE_DECOMPRESSED;
2019-05-06 20:51:09 +00:00
char* s = new char[size];
char* d = new char[destLen]();
2019-05-01 10:23:03 +00:00
rewind(f);
fread(s, 1, size, f);
2019-05-06 20:51:09 +00:00
2019-05-01 10:23:03 +00:00
int r = BZ2_bzBuffToBuffDecompress(d, &destLen, s, size, 0, 0);
2019-05-06 20:51:09 +00:00
if (r == BZ_OK) {
mCheats = std::make_shared<nlohmann::json>(nlohmann::json::parse(d));
2019-05-01 10:23:03 +00:00
}
delete[] s;
delete[] d;
fclose(f);
2019-05-01 10:23:03 +00:00
}
2019-07-24 18:23:20 +00:00
else {
Logger::getInstance().log(Logger::WARN, "Failed to open " + path + " with errno %d.", errno);
2019-07-24 18:23:20 +00:00
}
2019-05-01 10:23:03 +00:00
}
}
bool CheatManager::areCheatsAvailable(const std::string& key)
2019-05-01 10:23:03 +00:00
{
return mCheats->find(key) != mCheats->end();
2019-05-01 10:23:03 +00:00
}
void CheatManager::save(const std::string& key, const std::vector<std::string>& s)
2019-05-01 10:23:03 +00:00
{
static size_t MAGIC_LEN = strlen(SELECTED_MAGIC);
2019-05-01 10:23:03 +00:00
2019-05-06 20:51:09 +00:00
std::string idfolder = StringUtils::format("/atmosphere/titles/%s", key.c_str());
2019-05-01 10:23:03 +00:00
std::string rootfolder = idfolder + "/cheats";
2019-05-03 20:49:01 +00:00
mkdir(idfolder.c_str(), 777);
mkdir(rootfolder.c_str(), 777);
2019-05-01 10:23:03 +00:00
auto cheats = *CheatManager::getInstance().cheats().get();
for (auto it = cheats[key].begin(); it != cheats[key].end(); ++it) {
2019-05-06 20:51:09 +00:00
std::string buildid = it.key();
2019-05-01 10:23:03 +00:00
std::string cheatFile = "";
for (size_t i = 0; i < s.size(); i++) {
std::string cellName = s.at(i);
2019-05-18 20:51:58 +00:00
if (cellName.compare(0, MAGIC_LEN, SELECTED_MAGIC) == 0) {
2019-05-01 10:23:03 +00:00
cellName = cellName.substr(strlen(SELECTED_MAGIC), cellName.length());
if (cheats[key][buildid].find(cellName) != cheats[key][buildid].end()) {
2019-07-06 05:55:31 +00:00
cheatFile += "[" + cellName + "]\n";
for (auto& it2 : cheats[key][buildid][cellName]) {
2019-05-01 15:20:40 +00:00
cheatFile += it2.get<std::string>() + "\n";
2019-05-01 10:23:03 +00:00
}
cheatFile += "\n";
}
}
}
2019-07-24 18:23:20 +00:00
std::string outPath = rootfolder + "/" + buildid + ".txt";
FILE* f = fopen(outPath.c_str(), "w");
2019-05-06 20:51:09 +00:00
if (f != NULL) {
fwrite(cheatFile.c_str(), 1, cheatFile.length(), f);
fclose(f);
}
2019-07-24 18:23:20 +00:00
else {
Logger::getInstance().log(Logger::ERROR, "Failed to write " + outPath + " with errno %d.", errno);
2019-07-24 18:23:20 +00:00
}
2019-05-01 10:23:03 +00:00
}
}