2019-07-18 04:04:00 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
|
2019-10-24 10:30:10 +01:00
|
|
|
namespace ams::cfg {
|
2019-07-18 04:04:00 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/* Types. */
|
|
|
|
struct OverrideKey {
|
|
|
|
u64 key_combination;
|
|
|
|
bool override_by_default;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct HblOverrideConfig {
|
|
|
|
OverrideKey override_key;
|
2019-11-21 12:21:23 +00:00
|
|
|
OverrideKey override_any_app_key;
|
2019-10-28 04:43:01 +00:00
|
|
|
ncm::ProgramId program_id;
|
2019-07-18 04:04:00 +01:00
|
|
|
bool override_any_app;
|
|
|
|
};
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
struct ContentSpecificOverrideConfig {
|
2019-07-18 04:04:00 +01:00
|
|
|
OverrideKey override_key;
|
|
|
|
OverrideKey cheat_enable_key;
|
2019-11-22 03:32:41 +00:00
|
|
|
OverrideLocale locale;
|
2019-07-18 04:04:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Override globals. */
|
|
|
|
OverrideKey g_default_override_key = {
|
|
|
|
.key_combination = KEY_L,
|
|
|
|
.override_by_default = true,
|
|
|
|
};
|
|
|
|
|
|
|
|
OverrideKey g_default_cheat_enable_key = {
|
|
|
|
.key_combination = KEY_L,
|
|
|
|
.override_by_default = true,
|
|
|
|
};
|
|
|
|
|
|
|
|
HblOverrideConfig g_hbl_override_config = {
|
|
|
|
.override_key = {
|
2019-11-21 12:21:23 +00:00
|
|
|
.key_combination = KEY_R,
|
|
|
|
.override_by_default = true,
|
|
|
|
},
|
|
|
|
.override_any_app_key = {
|
2019-07-18 04:04:00 +01:00
|
|
|
.key_combination = KEY_R,
|
|
|
|
.override_by_default = false,
|
|
|
|
},
|
2019-10-28 04:43:01 +00:00
|
|
|
.program_id = ncm::ProgramId::AppletPhotoViewer,
|
2019-07-18 04:04:00 +01:00
|
|
|
.override_any_app = true,
|
|
|
|
};
|
|
|
|
|
|
|
|
char g_hbl_sd_path[0x100] = "/atmosphere/hbl.nsp";
|
|
|
|
|
|
|
|
/* Helpers. */
|
|
|
|
OverrideKey ParseOverrideKey(const char *value) {
|
|
|
|
OverrideKey cfg = {};
|
|
|
|
|
|
|
|
/* Parse on by default. */
|
|
|
|
if (value[0] == '!') {
|
|
|
|
cfg.override_by_default = true;
|
|
|
|
value++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse key combination. */
|
|
|
|
if (strcasecmp(value, "A") == 0) {
|
|
|
|
cfg.key_combination = KEY_A;
|
|
|
|
} else if (strcasecmp(value, "B") == 0) {
|
|
|
|
cfg.key_combination = KEY_B;
|
|
|
|
} else if (strcasecmp(value, "X") == 0) {
|
|
|
|
cfg.key_combination = KEY_X;
|
|
|
|
} else if (strcasecmp(value, "Y") == 0) {
|
|
|
|
cfg.key_combination = KEY_Y;
|
|
|
|
} else if (strcasecmp(value, "LS") == 0) {
|
|
|
|
cfg.key_combination = KEY_LSTICK;
|
|
|
|
} else if (strcasecmp(value, "RS") == 0) {
|
|
|
|
cfg.key_combination = KEY_RSTICK;
|
|
|
|
} else if (strcasecmp(value, "L") == 0) {
|
|
|
|
cfg.key_combination = KEY_L;
|
|
|
|
} else if (strcasecmp(value, "R") == 0) {
|
|
|
|
cfg.key_combination = KEY_R;
|
|
|
|
} else if (strcasecmp(value, "ZL") == 0) {
|
|
|
|
cfg.key_combination = KEY_ZL;
|
|
|
|
} else if (strcasecmp(value, "ZR") == 0) {
|
|
|
|
cfg.key_combination = KEY_ZR;
|
|
|
|
} else if (strcasecmp(value, "PLUS") == 0) {
|
|
|
|
cfg.key_combination = KEY_PLUS;
|
|
|
|
} else if (strcasecmp(value, "MINUS") == 0) {
|
|
|
|
cfg.key_combination = KEY_MINUS;
|
|
|
|
} else if (strcasecmp(value, "DLEFT") == 0) {
|
|
|
|
cfg.key_combination = KEY_DLEFT;
|
|
|
|
} else if (strcasecmp(value, "DUP") == 0) {
|
|
|
|
cfg.key_combination = KEY_DUP;
|
|
|
|
} else if (strcasecmp(value, "DRIGHT") == 0) {
|
|
|
|
cfg.key_combination = KEY_DRIGHT;
|
|
|
|
} else if (strcasecmp(value, "DDOWN") == 0) {
|
|
|
|
cfg.key_combination = KEY_DDOWN;
|
|
|
|
} else if (strcasecmp(value, "SL") == 0) {
|
|
|
|
cfg.key_combination = KEY_SL;
|
|
|
|
} else if (strcasecmp(value, "SR") == 0) {
|
|
|
|
cfg.key_combination = KEY_SR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg;
|
|
|
|
}
|
|
|
|
|
2019-11-27 21:44:09 +00:00
|
|
|
int OverrideConfigIniHandler(void *user, const char *section, const char *name, const char *value) {
|
2019-07-18 04:04:00 +01:00
|
|
|
/* Taken and modified, with love, from Rajkosto's implementation. */
|
|
|
|
if (strcasecmp(section, "hbl_config") == 0) {
|
2019-10-28 04:43:01 +00:00
|
|
|
/* TODO: Consider deprecating "title_id" string in the future." */
|
|
|
|
if (strcasecmp(name, "program_id") == 0 || strcasecmp(name, "title_id") == 0) {
|
|
|
|
u64 override_program_id = strtoul(value, NULL, 16);
|
|
|
|
if (override_program_id != 0) {
|
|
|
|
g_hbl_override_config.program_id = {override_program_id};
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
} else if (strcasecmp(name, "path") == 0) {
|
|
|
|
while (*value == '/' || *value == '\\') {
|
|
|
|
value++;
|
|
|
|
}
|
|
|
|
std::snprintf(g_hbl_sd_path, sizeof(g_hbl_sd_path) - 1, "/%s", value);
|
|
|
|
g_hbl_sd_path[sizeof(g_hbl_sd_path) - 1] = '\0';
|
2019-11-27 21:44:09 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < sizeof(g_hbl_sd_path); i++) {
|
|
|
|
if (g_hbl_sd_path[i] == '\\') {
|
|
|
|
g_hbl_sd_path[i] = '/';
|
|
|
|
}
|
|
|
|
}
|
2019-07-18 04:04:00 +01:00
|
|
|
} else if (strcasecmp(name, "override_key") == 0) {
|
|
|
|
g_hbl_override_config.override_key = ParseOverrideKey(value);
|
|
|
|
} else if (strcasecmp(name, "override_any_app") == 0) {
|
|
|
|
if (strcasecmp(value, "true") == 0 || strcasecmp(value, "1") == 0) {
|
|
|
|
g_hbl_override_config.override_any_app = true;
|
|
|
|
} else if (strcasecmp(value, "false") == 0 || strcasecmp(value, "0") == 0) {
|
|
|
|
g_hbl_override_config.override_any_app = false;
|
|
|
|
} else {
|
|
|
|
/* I guess we default to not changing the value? */
|
|
|
|
}
|
2019-11-21 12:21:23 +00:00
|
|
|
} else if (strcasecmp(name, "override_any_app_key") == 0) {
|
|
|
|
g_hbl_override_config.override_any_app_key = ParseOverrideKey(value);
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
} else if (strcasecmp(section, "default_config") == 0) {
|
|
|
|
if (strcasecmp(name, "override_key") == 0) {
|
|
|
|
g_default_override_key = ParseOverrideKey(value);
|
|
|
|
} else if (strcasecmp(name, "cheat_enable_key") == 0) {
|
|
|
|
g_default_cheat_enable_key = ParseOverrideKey(value);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
int ContentSpecificIniHandler(void *user, const char *section, const char *name, const char *value) {
|
|
|
|
ContentSpecificOverrideConfig *config = reinterpret_cast<ContentSpecificOverrideConfig *>(user);
|
2019-07-18 04:04:00 +01:00
|
|
|
|
|
|
|
if (strcasecmp(section, "override_config") == 0) {
|
|
|
|
if (strcasecmp(name, "override_key") == 0) {
|
|
|
|
config->override_key = ParseOverrideKey(value);
|
|
|
|
} else if (strcasecmp(name, "cheat_enable_key") == 0) {
|
|
|
|
config->cheat_enable_key = ParseOverrideKey(value);
|
2019-11-22 03:32:41 +00:00
|
|
|
} else if (strcasecmp(name, "override_language") == 0) {
|
|
|
|
config->locale.language_code = settings::LanguageCode::Encode(value);
|
|
|
|
} else if (strcasecmp(name, "override_region") == 0) {
|
|
|
|
if (strcasecmp(value, "jpn") == 0) {
|
|
|
|
config->locale.region_code = settings::RegionCode_Japan;
|
|
|
|
} else if (strcasecmp(value, "usa") == 0) {
|
|
|
|
config->locale.region_code = settings::RegionCode_America;
|
|
|
|
} else if (strcasecmp(value, "eur") == 0) {
|
|
|
|
config->locale.region_code = settings::RegionCode_Europe;
|
|
|
|
} else if (strcasecmp(value, "aus") == 0) {
|
|
|
|
config->locale.region_code = settings::RegionCode_Australia;
|
|
|
|
} else if (strcasecmp(value, "chn") == 0) {
|
|
|
|
config->locale.region_code = settings::RegionCode_China;
|
|
|
|
} else if (strcasecmp(value, "kor") == 0) {
|
|
|
|
config->locale.region_code = settings::RegionCode_Korea;
|
|
|
|
} else if (strcasecmp(value, "twn") == 0) {
|
|
|
|
config->locale.region_code = settings::RegionCode_Taiwan;
|
|
|
|
}
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-11-22 03:32:41 +00:00
|
|
|
|
2019-07-18 04:04:00 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-11-21 12:03:19 +00:00
|
|
|
constexpr inline bool IsOverrideMatch(const OverrideStatus &status, const OverrideKey &cfg) {
|
|
|
|
bool keys_triggered = ((status.keys_held & cfg.key_combination) != 0);
|
|
|
|
return (cfg.override_by_default ^ keys_triggered);
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
2019-11-21 12:21:23 +00:00
|
|
|
inline bool IsApplicationHblProgramId(ncm::ProgramId program_id) {
|
|
|
|
return g_hbl_override_config.override_any_app && ncm::IsApplicationProgramId(program_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool IsSpecificHblProgramId(ncm::ProgramId program_id) {
|
|
|
|
return program_id == g_hbl_override_config.program_id;
|
|
|
|
}
|
|
|
|
|
2019-07-18 04:04:00 +01:00
|
|
|
void ParseIniFile(util::ini::Handler handler, const char *path, void *user_ctx) {
|
|
|
|
/* Mount the SD card. */
|
|
|
|
FsFileSystem sd_fs = {};
|
2019-10-23 08:07:20 +01:00
|
|
|
if (R_FAILED(fsOpenSdCardFileSystem(&sd_fs))) {
|
2019-07-18 04:04:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ON_SCOPE_EXIT { serviceClose(&sd_fs.s); };
|
|
|
|
|
|
|
|
/* Open the file. */
|
|
|
|
FsFile config_file;
|
2019-09-30 10:52:32 +01:00
|
|
|
if (R_FAILED(fsFsOpenFile(&sd_fs, path, FsOpenMode_Read, &config_file))) {
|
2019-07-18 04:04:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ON_SCOPE_EXIT { fsFileClose(&config_file); };
|
|
|
|
|
|
|
|
/* Parse the config. */
|
|
|
|
util::ini::ParseFile(&config_file, user_ctx, handler);
|
|
|
|
}
|
|
|
|
|
2019-11-27 21:44:09 +00:00
|
|
|
void RefreshOverrideConfiguration() {
|
2019-12-06 11:29:14 +00:00
|
|
|
ParseIniFile(OverrideConfigIniHandler, "/atmosphere/config/override_config.ini", nullptr);
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
ContentSpecificOverrideConfig GetContentOverrideConfig(ncm::ProgramId program_id) {
|
2019-07-18 04:04:00 +01:00
|
|
|
char path[FS_MAX_PATH];
|
2019-10-28 04:43:01 +00:00
|
|
|
std::snprintf(path, sizeof(path) - 1, "/atmosphere/contents/%016lx/config.ini", static_cast<u64>(program_id));
|
2019-07-18 04:04:00 +01:00
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
ContentSpecificOverrideConfig config = {
|
2019-07-18 04:04:00 +01:00
|
|
|
.override_key = g_default_override_key,
|
|
|
|
.cheat_enable_key = g_default_cheat_enable_key,
|
|
|
|
};
|
2019-11-22 03:32:41 +00:00
|
|
|
std::memset(&config.locale, 0xCC, sizeof(config.locale));
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
ParseIniFile(ContentSpecificIniHandler, path, &config);
|
2019-07-18 04:04:00 +01:00
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-21 12:03:19 +00:00
|
|
|
OverrideStatus CaptureOverrideStatus(ncm::ProgramId program_id) {
|
|
|
|
OverrideStatus status = {};
|
2019-07-18 04:04:00 +01:00
|
|
|
|
|
|
|
/* If the SD card isn't initialized, we can't override. */
|
|
|
|
if (!IsSdCardInitialized()) {
|
2019-11-21 12:03:19 +00:00
|
|
|
return status;
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* For system modules and anything launched before the home menu, always override. */
|
2019-10-28 04:43:01 +00:00
|
|
|
if (program_id < ncm::ProgramId::AppletStart || !pm::info::HasLaunchedProgram(ncm::ProgramId::AppletQlaunch)) {
|
2019-11-21 12:03:19 +00:00
|
|
|
status.SetProgramSpecific();
|
|
|
|
return status;
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
2019-11-27 21:44:09 +00:00
|
|
|
/* Unconditionally refresh override_config.ini contents. */
|
|
|
|
RefreshOverrideConfiguration();
|
2019-07-18 04:04:00 +01:00
|
|
|
|
2019-11-21 12:03:19 +00:00
|
|
|
/* If we can't read the key state, don't override anything. */
|
|
|
|
if (R_FAILED(hid::GetKeysHeld(&status.keys_held))) {
|
|
|
|
return status;
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
2019-11-21 12:03:19 +00:00
|
|
|
/* Detect Hbl. */
|
2019-11-21 12:21:23 +00:00
|
|
|
if ((IsApplicationHblProgramId(program_id) && IsOverrideMatch(status, g_hbl_override_config.override_any_app_key)) ||
|
|
|
|
(IsSpecificHblProgramId(program_id) && IsOverrideMatch(status, g_hbl_override_config.override_key)))
|
|
|
|
{
|
2019-11-21 12:03:19 +00:00
|
|
|
status.SetHbl();
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
2019-11-21 12:03:19 +00:00
|
|
|
/* Detect content specific keys. */
|
2019-10-28 04:43:01 +00:00
|
|
|
const auto content_cfg = GetContentOverrideConfig(program_id);
|
2019-11-21 12:03:19 +00:00
|
|
|
if (IsOverrideMatch(status, content_cfg.override_key)) {
|
|
|
|
status.SetProgramSpecific();
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
2019-11-21 12:03:19 +00:00
|
|
|
/* Only allow cheat enable if not HBL. */
|
|
|
|
if (!status.IsHbl() && IsOverrideMatch(status, content_cfg.cheat_enable_key)) {
|
|
|
|
status.SetCheatEnabled();
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
2019-11-21 12:03:19 +00:00
|
|
|
return status;
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
2019-11-22 03:32:41 +00:00
|
|
|
OverrideLocale GetOverrideLocale(ncm::ProgramId program_id) {
|
|
|
|
return GetContentOverrideConfig(program_id).locale;
|
|
|
|
}
|
|
|
|
|
2019-07-18 04:04:00 +01:00
|
|
|
/* HBL Configuration utilities. */
|
2019-10-28 04:43:01 +00:00
|
|
|
bool IsHblProgramId(ncm::ProgramId program_id) {
|
2019-11-21 12:21:23 +00:00
|
|
|
return IsApplicationHblProgramId(program_id) || IsSpecificHblProgramId(program_id);
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *GetHblPath() {
|
|
|
|
return g_hbl_sd_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|