set.mitm: language emulation (closes #489)

This commit is contained in:
Michael Scire 2019-05-22 12:11:40 -07:00
parent 6cc29185d2
commit 6f25e92892
10 changed files with 452 additions and 68 deletions

View file

@ -78,6 +78,16 @@ void __appInit(void) {
if (R_FAILED(rc)) {
std::abort();
}
rc = pmdmntInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = pminfoInitialize();
if (R_FAILED(rc)) {
std::abort();
}
});
CheckAtmosphereVersion(CURRENT_ATMOSPHERE_VERSION);

View file

@ -0,0 +1,113 @@
/*
* 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 <mutex>
#include <algorithm>
#include <switch.h>
#include "set_mitm_service.hpp"
#include "set_shim.h"
void SetMitmService::PostProcess(IMitmServiceObject *obj, IpcResponseContext *ctx) {
/* No commands need postprocessing. */
}
bool SetMitmService::IsValidLanguageCode(u64 lang_code) {
static constexpr u64 s_valid_language_codes[] = {
LanguageCode_Japanese,
LanguageCode_AmericanEnglish,
LanguageCode_French,
LanguageCode_German,
LanguageCode_Italian,
LanguageCode_Spanish,
LanguageCode_Chinese,
LanguageCode_Korean,
LanguageCode_Dutch,
LanguageCode_Portuguese,
LanguageCode_Russian,
LanguageCode_Taiwanese,
LanguageCode_BritishEnglish,
LanguageCode_CanadianFrench,
LanguageCode_LatinAmericanSpanish,
LanguageCode_SimplifiedChinese,
LanguageCode_TraditionalChinese,
};
size_t num_language_codes = sizeof(s_valid_language_codes) / sizeof(s_valid_language_codes[0]);
if (GetRuntimeFirmwareVersion() < FirmwareVersion_400) {
/* 4.0.0 added simplified and traditional chinese. */
num_language_codes -= 2;
}
for (size_t i = 0; i < num_language_codes; i++) {
if (lang_code == s_valid_language_codes[i]) {
return true;
}
}
return false;
}
bool SetMitmService::IsValidRegionCode(u32 region_code) {
return region_code < RegionCode_Max;
}
void SetMitmService::EnsureLocale() {
std::scoped_lock<HosMutex> lk(this->lock);
if (!this->got_locale) {
std::memset(&this->locale, 0xCC, sizeof(this->locale));
if (this->title_id == TitleId_Ns) {
u64 app_pid = 0;
u64 app_tid = 0;
Result rc;
if (R_FAILED((rc = pmdmntGetApplicationPid(&app_pid)))) {
return;
}
if (R_FAILED((rc = pminfoGetTitleId(&app_tid, app_pid)))) {
return;
}
this->locale = Utils::GetTitleOverrideLocale(app_tid);
} else {
this->locale = Utils::GetTitleOverrideLocale(this->title_id);
this->got_locale = true;
}
}
}
Result SetMitmService::GetLanguageCode(Out<u64> out_lang_code) {
this->EnsureLocale();
if (!IsValidLanguageCode(this->locale.language_code)) {
return ResultAtmosphereMitmShouldForwardToSession;
}
out_lang_code.SetValue(this->locale.language_code);
return ResultSuccess;
}
Result SetMitmService::GetRegionCode(Out<u32> out_region_code) {
this->EnsureLocale();
if (!IsValidRegionCode(this->locale.region_code)) {
return ResultAtmosphereMitmShouldForwardToSession;
}
out_region_code.SetValue(this->locale.region_code);
return ResultSuccess;
}
Result SetMitmService::GetAvailableLanguageCodes(OutPointerWithClientSize<u64> out_language_codes, Out<s32> out_count) {
return setGetAvailableLanguageCodesFwd(this->forward_service.get(), out_count.GetPointer(), out_language_codes.pointer, out_language_codes.num_elements);
}

View file

@ -0,0 +1,68 @@
/*
* 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/>.
*/
#pragma once
#include <switch.h>
#include <stratosphere.hpp>
#include "../utils.hpp"
enum SetCmd : u32 {
SetCmd_GetLanguageCode = 0,
SetCmd_GetRegionCode = 4,
/* Commands for which set:sys *must* act as a passthrough. */
/* TODO: Solve the relevant IPC detection problem. */
SetCmd_GetAvailableLanguageCodes = 1,
};
class SetMitmService : public IMitmServiceObject {
private:
HosMutex lock;
OverrideLocale locale;
bool got_locale;
public:
SetMitmService(std::shared_ptr<Service> s, u64 pid) : IMitmServiceObject(s, pid) {
this->got_locale = false;
}
static bool ShouldMitm(u64 pid, u64 tid) {
/* Mitm all applications. */
return tid == TitleId_Ns || TitleIdIsApplication(tid);
}
static void PostProcess(IMitmServiceObject *obj, IpcResponseContext *ctx);
protected:
static bool IsValidLanguageCode(u64 lang_code);
static bool IsValidRegionCode(u32 region_code);
void EnsureLocale();
protected:
/* Overridden commands. */
Result GetLanguageCode(Out<u64> out_lang_code);
Result GetRegionCode(Out<u32> out_region_code);
/* Forced passthrough commands. */
Result GetAvailableLanguageCodes(OutPointerWithClientSize<u64> out_language_codes, Out<s32> out_count);
public:
DEFINE_SERVICE_DISPATCH_TABLE {
MakeServiceCommandMeta<SetCmd_GetLanguageCode, &SetMitmService::GetLanguageCode>(),
MakeServiceCommandMeta<SetCmd_GetRegionCode, &SetMitmService::GetRegionCode>(),
MakeServiceCommandMeta<SetCmd_GetAvailableLanguageCodes, &SetMitmService::GetAvailableLanguageCodes>(),
};
};

View file

@ -0,0 +1,59 @@
/*
* 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 <string.h>
#include <switch.h>
#include "setsys_shim.h"
/* Command forwarders. */
Result setGetAvailableLanguageCodesFwd(Service* s, s32 *total_entries, u64 *language_codes, size_t max_entries) {
IpcCommand c;
ipcInitialize(&c);
ipcAddRecvStatic(&c, language_codes, max_entries * sizeof(*language_codes), 0);
struct {
u64 magic;
u64 cmd_id;
} *raw;
raw = serviceIpcPrepareHeader(s, &c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 1;
Result rc = serviceIpcDispatch(s);
if (R_SUCCEEDED(rc)) {
IpcParsedCommand r;
struct {
u64 magic;
u64 result;
s32 total_entries;
} *resp;
serviceIpcParse(s, &r, sizeof(*resp));
resp = r.Raw;
rc = resp->result;
if (R_SUCCEEDED(rc)) {
*total_entries = resp->total_entries;
}
}
return rc;
}

View file

@ -0,0 +1,19 @@
/**
* @file set_shim.h
* @brief Settings Services (set) IPC wrapper. To be merged into libnx, eventually.
* @author SciresM
* @copyright libnx Authors
*/
#pragma once
#include <switch.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Command forwarders. */
Result setGetAvailableLanguageCodesFwd(Service* s, s32 *total_entries, u64 *language_codes, size_t max_entries);
#ifdef __cplusplus
}
#endif

View file

@ -28,6 +28,8 @@
#include "setsys_settings_items.hpp"
#include "setsys_firmware_version.hpp"
#include "set_mitm_service.hpp"
#include "../utils.hpp"
struct SetSysManagerOptions {
@ -46,11 +48,14 @@ void SetMitmMain(void *arg) {
VersionManager::Initialize();
/* Create server manager */
auto server_manager = new SetMitmManager(3);
auto server_manager = new SetMitmManager(4);
/* Create set:sys mitm. */
AddMitmServerToManager<SetSysMitmService>(server_manager, "set:sys", 60);
/* Create set mitm. */
AddMitmServerToManager<SetMitmService>(server_manager, "set", 60);
/* Loop forever, servicing our services. */
server_manager->Process();

View file

@ -624,6 +624,64 @@ OverrideKey Utils::GetTitleOverrideKey(u64 tid) {
return cfg;
}
static int FsMitmTitleSpecificLocaleIniHandler(void *user, const char *section, const char *name, const char *value) {
/* We'll output an override locale when relevant. */
OverrideLocale *user_locale = reinterpret_cast<OverrideLocale *>(user);
if (strcasecmp(section, "override_config") == 0) {
if (strcasecmp(name, "override_language") == 0) {
user_locale->language_code = EncodeLanguageCode(value);
} else if (strcasecmp(name, "override_region") == 0) {
if (strcasecmp(value, "jpn") == 0) {
user_locale->region_code = RegionCode_Japan;
} else if (strcasecmp(value, "usa") == 0) {
user_locale->region_code = RegionCode_America;
} else if (strcasecmp(value, "eur") == 0) {
user_locale->region_code = RegionCode_Europe;
} else if (strcasecmp(value, "aus") == 0) {
user_locale->region_code = RegionCode_Australia;
} else if (strcasecmp(value, "chn") == 0) {
user_locale->region_code = RegionCode_China;
} else if (strcasecmp(value, "kor") == 0) {
user_locale->region_code = RegionCode_Korea;
} else if (strcasecmp(value, "twn") == 0) {
user_locale->region_code = RegionCode_Taiwan;
}
}
} else {
return 0;
}
return 1;
}
OverrideLocale Utils::GetTitleOverrideLocale(u64 tid) {
OverrideLocale locale;
std::memset(&locale, 0xCC, sizeof(locale));
char path[FS_MAX_PATH+1] = {0};
snprintf(path, FS_MAX_PATH, "/atmosphere/titles/%016lx/config.ini", tid);
FsFile cfg_file;
if (R_SUCCEEDED(fsFsOpenFile(&g_sd_filesystem, path, FS_OPEN_READ, &cfg_file))) {
ON_SCOPE_EXIT { fsFileClose(&cfg_file); };
size_t config_file_size = 0x20000;
fsFileGetSize(&cfg_file, &config_file_size);
char *config_buf = reinterpret_cast<char *>(calloc(1, config_file_size + 1));
if (config_buf != NULL) {
ON_SCOPE_EXIT { free(config_buf); };
/* Read title ini contents. */
fsFileRead(&cfg_file, 0, config_buf, config_file_size, &config_file_size);
/* Parse title ini. */
ini_parse_string(config_buf, FsMitmTitleSpecificLocaleIniHandler, &locale);
}
}
return locale;
}
void Utils::RefreshConfiguration() {
FsFile config_file;
if (R_FAILED(fsFsOpenFile(&g_sd_filesystem, "/atmosphere/loader.ini", FS_OPEN_READ, &config_file))) {

View file

@ -42,6 +42,56 @@ struct OverrideKey {
bool override_by_default;
};
struct OverrideLocale {
u64 language_code;
u32 region_code;
};
enum RegionCode : u32 {
RegionCode_Japan = 0,
RegionCode_America = 1,
RegionCode_Europe = 2,
RegionCode_Australia = 3,
RegionCode_China = 4,
RegionCode_Korea = 5,
RegionCode_Taiwan = 6,
RegionCode_Max,
};
static constexpr inline u64 EncodeLanguageCode(const char *code) {
u64 lang_code = 0;
for (size_t i = 0; i < sizeof(lang_code); i++) {
if (code[i] == '\x00') {
break;
}
lang_code |= static_cast<u64>(code[i]) << (8ul * i);
}
return lang_code;
}
enum LanguageCode : u64 {
LanguageCode_Japanese = EncodeLanguageCode("ja"),
LanguageCode_AmericanEnglish = EncodeLanguageCode("en-US"),
LanguageCode_French = EncodeLanguageCode("fr"),
LanguageCode_German = EncodeLanguageCode("de"),
LanguageCode_Italian = EncodeLanguageCode("it"),
LanguageCode_Spanish = EncodeLanguageCode("es"),
LanguageCode_Chinese = EncodeLanguageCode("zh-CN"),
LanguageCode_Korean = EncodeLanguageCode("ko"),
LanguageCode_Dutch = EncodeLanguageCode("nl"),
LanguageCode_Portuguese = EncodeLanguageCode("pt"),
LanguageCode_Russian = EncodeLanguageCode("ru"),
LanguageCode_Taiwanese = EncodeLanguageCode("zh-TW"),
LanguageCode_BritishEnglish = EncodeLanguageCode("en-GB"),
LanguageCode_CanadianFrench = EncodeLanguageCode("fr-CA"),
LanguageCode_LatinAmericanSpanish = EncodeLanguageCode("es-419"),
/* 4.0.0+ */
LanguageCode_SimplifiedChinese = EncodeLanguageCode("zh-Hans"),
LanguageCode_TraditionalChinese = EncodeLanguageCode("zh-Hant"),
};
class Utils {
public:
static bool IsSdInitialized();
@ -82,6 +132,8 @@ class Utils {
static OverrideKey GetTitleOverrideKey(u64 tid);
static bool HasOverrideButton(u64 tid);
static OverrideLocale GetTitleOverrideLocale(u64 tid);
/* Settings! */
static Result GetSettingsItemValueSize(const char *name, const char *key, u64 *out_size);
static Result GetSettingsItemValue(const char *name, const char *key, void *out, size_t max_size, u64 *out_size);

View file

@ -175,9 +175,9 @@ int main(int argc, char **argv)
/* TODO: Create services. */
s_server_manager.AddWaitable(new ServiceServer<ShellService>("pm:shell", 3));
s_server_manager.AddWaitable(new ServiceServer<DebugMonitorService>("pm:dmnt", 2));
s_server_manager.AddWaitable(new ServiceServer<DebugMonitorService>("pm:dmnt", 3));
s_server_manager.AddWaitable(new ServiceServer<BootModeService>("pm:bm", 6));
s_server_manager.AddWaitable(new ServiceServer<InformationService>("pm:info", 2));
s_server_manager.AddWaitable(new ServiceServer<InformationService>("pm:info", 3));
/* Loop forever, servicing our services. */
s_server_manager.Process();