Checkpoint/3ds/source/cheatmanager.cpp

103 lines
3.8 KiB
C++
Raw Normal View History

2019-04-22 16:24:45 +02:00
/*
2019-05-06 22:51:09 +02: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-04-22 16:24:45 +02:00
#include "cheatmanager.hpp"
2019-07-07 21:48:23 +02:00
CheatManager::CheatManager(void)
2019-04-22 16:24:45 +02:00
{
2019-07-07 21:48:23 +02:00
mCheats = nullptr;
2019-05-06 22:51:09 +02:00
if (io::fileExists("/3ds/Checkpoint/cheats.json")) {
const std::string path = "/3ds/Checkpoint/cheats.json";
2019-05-06 22:51:09 +02:00
FILE* in = fopen(path.c_str(), "rt");
if (in != NULL) {
2019-07-07 21:48:23 +02:00
mCheats = std::make_shared<nlohmann::json>(nlohmann::json::parse(in, nullptr, false));
fclose(in);
}
2019-07-24 19:35:27 +02:00
else {
Logger::getInstance().log(Logger::WARN, "Failed to open " + path + " with errno %d.", errno);
2019-07-24 19:35:27 +02:00
}
}
2019-05-06 22:51:09 +02:00
else {
const std::string path = "romfs:/cheats/cheats.json.bz2";
// load compressed archive in memory
FILE* f = fopen(path.c_str(), "rb");
2019-05-06 22:51:09 +02:00
if (f != NULL) {
fseek(f, 0, SEEK_END);
2019-05-06 22:51:09 +02:00
u32 size = ftell(f);
2019-07-24 19:35:27 +02:00
unsigned int destLen = CHEAT_SIZE_DECOMPRESSED;
2019-05-06 22:51:09 +02:00
char* s = new char[size];
char* d = new char[destLen]();
rewind(f);
fread(s, 1, size, f);
2019-05-06 22:51:09 +02:00
int r = BZ2_bzBuffToBuffDecompress(d, &destLen, s, size, 0, 0);
2019-05-06 22:51:09 +02:00
if (r == BZ_OK) {
2019-07-07 21:48:23 +02:00
mCheats = std::make_shared<nlohmann::json>(nlohmann::json::parse(d));
}
delete[] s;
delete[] d;
fclose(f);
}
2019-07-24 19:35:27 +02:00
else {
Logger::getInstance().log(Logger::WARN, "Failed to open " + path + " with errno %d.", errno);
2019-07-24 19:35:27 +02:00
}
}
2019-04-22 16:24:45 +02:00
}
2019-07-07 21:48:23 +02:00
bool CheatManager::areCheatsAvailable(const std::string& key)
2019-04-22 16:24:45 +02:00
{
2019-07-07 21:48:23 +02:00
return mCheats->find(key) != mCheats->end();
2019-04-22 16:24:45 +02:00
}
2019-07-07 21:48:23 +02:00
void CheatManager::save(const std::string& key, const std::vector<std::string>& s)
2019-04-22 16:24:45 +02:00
{
2019-07-07 21:48:23 +02:00
static size_t MAGIC_LEN = strlen(SELECTED_MAGIC);
2019-04-22 17:37:45 +02:00
2019-04-22 22:02:33 +02:00
std::string cheatFile = "";
2019-07-07 21:48:23 +02:00
auto cheats = *CheatManager::getInstance().cheats().get();
for (size_t i = 0; i < s.size(); i++) {
std::string cellName = s.at(i);
2019-05-18 22:51:58 +02:00
if (cellName.compare(0, MAGIC_LEN, SELECTED_MAGIC) == 0) {
cellName = cellName.substr(MAGIC_LEN, cellName.length());
2019-07-07 21:48:23 +02:00
cheatFile += "[" + cellName + "]\n";
for (auto& it : cheats[key][cellName]) {
2019-04-22 22:02:33 +02:00
cheatFile += it.get<std::string>() + "\n";
}
cheatFile += "\n";
}
}
const std::string outPath = "/cheats/" + key + ".txt";
FILE* f = fopen(outPath.c_str(), "w");
2019-05-06 22:51:09 +02:00
if (f != NULL) {
fwrite(cheatFile.c_str(), 1, cheatFile.length(), f);
fclose(f);
}
else {
Logger::getInstance().log(Logger::ERROR, "Failed to write " + outPath + " with errno %d.", errno);
}
2019-04-22 16:24:45 +02:00
}