Checkpoint/3ds/source/configuration.cpp

213 lines
7.1 KiB
C++
Raw Normal View History

2018-05-26 22:25:48 +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.
*/
2018-05-26 22:25:48 +02:00
2018-05-28 15:36:27 +02:00
#include "configuration.hpp"
2018-05-26 22:25:48 +02:00
Configuration::Configuration(void)
{
// check for existing config.json files on the sd card, BASEPATH
2019-07-09 22:08:48 +02:00
if (!io::fileExists(Archive::sdmc(), StringUtils::UTF8toUTF16(BASEPATH.c_str()))) {
2018-05-28 15:36:27 +02:00
store();
2018-05-27 22:59:33 +02:00
}
2018-05-26 22:25:48 +02:00
2019-04-25 00:13:46 +02:00
mJson = loadJson(BASEPATH);
2018-05-26 22:25:48 +02:00
2019-07-09 22:08:48 +02:00
// check if json is valid
if (!mJson.is_object()) {
store();
}
bool updateJson = false;
2019-05-06 22:51:09 +02:00
if (mJson.find("version") == mJson.end()) {
// if config is present but is < 3.4.2, override it
2018-05-28 15:36:27 +02:00
store();
}
2019-05-06 22:51:09 +02:00
else {
2019-07-08 22:22:38 +02:00
if (mJson["version"] < CONFIG_VERSION) {
mJson["version"] = CONFIG_VERSION;
updateJson = true;
}
if (!(mJson.contains("nand_saves") && mJson["nand_saves"].is_boolean())) {
mJson["nand_saves"] = false;
2019-07-09 22:08:48 +02:00
updateJson = true;
2019-07-08 22:22:38 +02:00
}
if (!(mJson.contains("scan_cart") && mJson["scan_cart"].is_boolean())) {
mJson["scan_cart"] = false;
2019-07-09 22:08:48 +02:00
updateJson = true;
2019-07-08 22:22:38 +02:00
}
if (!(mJson.contains("filter") && mJson["filter"].is_array())) {
mJson["filter"] = nlohmann::json::array();
updateJson = true;
}
if (!(mJson.contains("favorites") && mJson["favorites"].is_array())) {
mJson["favorites"] = nlohmann::json::array();
2019-05-06 22:51:09 +02:00
updateJson = true;
}
2019-07-08 22:22:38 +02:00
if (!(mJson.contains("additional_save_folders") && mJson["additional_save_folders"].is_array())) {
mJson["additional_save_folders"] = nlohmann::json::array();
updateJson = true;
}
if (!(mJson.contains("additional_extdata_folders") && mJson["additional_extdata_folders"].is_array())) {
mJson["additional_extdata_folders"] = nlohmann::json::array();
2019-07-09 22:08:48 +02:00
updateJson = true;
2019-07-08 22:22:38 +02:00
}
// check every single entry in the arrays...
for (auto& obj : mJson["filter"]) {
if (!obj.is_string()) {
mJson["filter"] = nlohmann::json::array();
updateJson = true;
break;
}
}
for (auto& obj : mJson["favorites"]) {
if (!obj.is_string()) {
mJson["favorites"] = nlohmann::json::array();
updateJson = true;
break;
}
}
for (auto& obj : mJson["additional_save_folders"]) {
if (!obj.is_string()) {
mJson["additional_save_folders"] = nlohmann::json::array();
updateJson = true;
break;
}
}
for (auto& obj : mJson["additional_extdata_folders"]) {
if (!obj.is_string()) {
mJson["additional_extdata_folders"] = nlohmann::json::array();
2019-07-09 22:08:48 +02:00
updateJson = true;
2019-07-08 22:22:38 +02:00
break;
}
}
}
2019-05-06 22:51:09 +02:00
if (updateJson) {
mJson["version"] = CONFIG_VERSION;
2019-04-25 00:13:46 +02:00
storeJson(mJson, BASEPATH);
}
2018-05-27 22:59:33 +02:00
// parse filters
std::vector<std::string> filter = mJson["filter"];
2019-05-06 22:51:09 +02:00
for (auto& id : filter) {
2018-05-27 22:59:33 +02:00
mFilterIds.emplace(strtoull(id.c_str(), NULL, 16));
2018-05-26 22:25:48 +02:00
}
2018-05-27 22:59:33 +02:00
2018-09-16 16:34:12 +02:00
// parse favorites
std::vector<std::string> favorites = mJson["favorites"];
2019-05-06 22:51:09 +02:00
for (auto& id : favorites) {
2018-09-16 16:34:12 +02:00
mFavoriteIds.emplace(strtoull(id.c_str(), NULL, 16));
}
2018-05-28 20:24:11 +02:00
mNandSaves = mJson["nand_saves"];
2019-07-09 22:08:48 +02:00
mScanCard = mJson["scan_cart"];
2018-05-28 20:24:11 +02:00
// parse additional save folders
auto js = mJson["additional_save_folders"];
2019-05-06 22:51:09 +02:00
for (auto it = js.begin(); it != js.end(); ++it) {
2018-05-28 20:24:11 +02:00
std::vector<std::string> folders = it.value()["folders"];
std::vector<std::u16string> u16folders;
2019-05-06 22:51:09 +02:00
for (auto& folder : folders) {
2018-05-28 20:24:11 +02:00
u16folders.push_back(StringUtils::UTF8toUTF16(folder.c_str()));
}
mAdditionalSaveFolders.emplace(strtoull(it.key().c_str(), NULL, 16), u16folders);
}
// parse additional extdata folders
auto je = mJson["additional_extdata_folders"];
2019-05-06 22:51:09 +02:00
for (auto it = je.begin(); it != je.end(); ++it) {
2018-05-28 20:24:11 +02:00
std::vector<std::string> folders = it.value()["folders"];
std::vector<std::u16string> u16folders;
2019-05-06 22:51:09 +02:00
for (auto& folder : folders) {
2018-05-28 20:24:11 +02:00
u16folders.push_back(StringUtils::UTF8toUTF16(folder.c_str()));
}
mAdditionalExtdataFolders.emplace(strtoull(it.key().c_str(), NULL, 16), u16folders);
}
2018-05-26 22:25:48 +02:00
}
2019-04-25 00:13:46 +02:00
nlohmann::json Configuration::loadJson(const std::string& path)
{
nlohmann::json json;
2019-04-25 00:13:46 +02:00
FILE* in = fopen(path.c_str(), "rt");
2019-05-06 22:51:09 +02:00
if (in != NULL) {
json = nlohmann::json::parse(in, nullptr, false);
fclose(in);
}
2019-04-25 00:13:46 +02:00
return json;
}
void Configuration::storeJson(nlohmann::json& json, const std::string& path)
{
std::string writeData = json.dump(2);
writeData.shrink_to_fit();
size_t size = writeData.size();
FILE* out = fopen(path.c_str(), "wt");
2019-05-06 22:51:09 +02:00
if (out != NULL) {
fwrite(writeData.c_str(), 1, size, out);
fclose(out);
}
2019-04-25 00:13:46 +02:00
}
2018-05-28 15:36:27 +02:00
void Configuration::store(void)
{
2019-04-25 00:13:46 +02:00
nlohmann::json src = loadJson("romfs:/config.json");
storeJson(src, BASEPATH);
2018-05-28 15:36:27 +02:00
}
2018-05-26 22:25:48 +02:00
bool Configuration::filter(u64 id)
{
return mFilterIds.find(id) != mFilterIds.end();
2018-05-27 22:59:33 +02:00
}
2018-09-16 16:34:12 +02:00
bool Configuration::favorite(u64 id)
{
return mFavoriteIds.find(id) != mFavoriteIds.end();
}
2018-05-27 22:59:33 +02:00
bool Configuration::nandSaves(void)
{
return mNandSaves;
2018-05-28 20:24:11 +02:00
}
std::vector<std::u16string> Configuration::additionalSaveFolders(u64 id)
{
std::vector<std::u16string> emptyvec;
auto folders = mAdditionalSaveFolders.find(id);
return folders == mAdditionalSaveFolders.end() ? emptyvec : folders->second;
}
std::vector<std::u16string> Configuration::additionalExtdataFolders(u64 id)
{
std::vector<std::u16string> emptyvec;
auto folders = mAdditionalExtdataFolders.find(id);
return folders == mAdditionalExtdataFolders.end() ? emptyvec : folders->second;
2019-07-08 22:22:38 +02:00
}
bool Configuration::shouldScanCard(void)
{
return mScanCard;
2018-05-26 22:25:48 +02:00
}