2019-11-22 03:32:41 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-11-22 03:32:41 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2020-05-11 22:02:10 +00:00
|
|
|
#include <stratosphere.hpp>
|
2019-11-22 03:32:41 +00:00
|
|
|
#include "set_mitm_service.hpp"
|
2020-09-23 09:01:07 +00:00
|
|
|
#include "set_shim.h"
|
2019-11-22 03:32:41 +00:00
|
|
|
|
|
|
|
namespace ams::mitm::settings {
|
|
|
|
|
|
|
|
using namespace ams::settings;
|
|
|
|
|
2020-09-23 09:01:07 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constinit os::ProcessId g_application_process_id = os::InvalidProcessId;
|
|
|
|
constinit cfg::OverrideLocale g_application_locale;
|
|
|
|
constinit bool g_valid_language;
|
|
|
|
constinit bool g_valid_region;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetMitmService::SetMitmService(std::shared_ptr<::Service> &&s, const sm::MitmProcessInfo &c) : sf::MitmServiceImplBase(std::forward<std::shared_ptr<::Service>>(s), c) {
|
2021-10-10 07:14:06 +00:00
|
|
|
if (m_client_info.program_id == ncm::SystemProgramId::Ns) {
|
2020-09-23 09:01:07 +00:00
|
|
|
os::ProcessId application_process_id;
|
2023-05-10 05:12:00 +00:00
|
|
|
if (R_SUCCEEDED(ams::pm::dmnt::GetApplicationProcessId(std::addressof(application_process_id))) && g_application_process_id == application_process_id) {
|
2021-10-10 07:14:06 +00:00
|
|
|
m_locale = g_application_locale;
|
|
|
|
m_is_valid_language = g_valid_language;
|
|
|
|
m_is_valid_region = g_valid_region;
|
|
|
|
m_got_locale = true;
|
2020-09-23 09:01:07 +00:00
|
|
|
} else {
|
|
|
|
this->InvalidateLocale();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this->InvalidateLocale();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 03:32:41 +00:00
|
|
|
Result SetMitmService::EnsureLocale() {
|
2020-09-23 09:01:07 +00:00
|
|
|
/* Optimization: if locale has already been gotten, we can stop. */
|
2021-10-10 07:14:06 +00:00
|
|
|
if (AMS_LIKELY(m_got_locale)) {
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-09-23 09:01:07 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
std::scoped_lock lk(m_lock);
|
2019-11-22 03:32:41 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
const bool is_ns = m_client_info.program_id == ncm::SystemProgramId::Ns;
|
2019-11-22 03:32:41 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
if (!m_got_locale) {
|
|
|
|
ncm::ProgramId program_id = m_client_info.program_id;
|
2020-09-23 09:01:07 +00:00
|
|
|
os::ProcessId application_process_id = os::InvalidProcessId;
|
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
if (is_ns) {
|
2019-11-22 03:32:41 +00:00
|
|
|
/* When NS asks for a locale, refresh to get the current application locale. */
|
2023-05-10 05:12:00 +00:00
|
|
|
R_TRY(ams::pm::dmnt::GetApplicationProcessId(std::addressof(application_process_id)));
|
|
|
|
R_TRY(ams::pm::info::GetProgramId(std::addressof(program_id), application_process_id));
|
2020-09-23 09:01:07 +00:00
|
|
|
}
|
2021-10-10 07:14:06 +00:00
|
|
|
m_locale = cfg::GetOverrideLocale(program_id);
|
|
|
|
m_is_valid_language = settings::IsValidLanguageCode(m_locale.language_code);
|
|
|
|
m_is_valid_region = settings::IsValidRegionCode(m_locale.region_code);
|
|
|
|
m_got_locale = true;
|
2020-09-23 09:01:07 +00:00
|
|
|
|
|
|
|
if (is_ns) {
|
2021-10-10 07:14:06 +00:00
|
|
|
g_application_locale = m_locale;
|
|
|
|
g_valid_language = m_is_valid_language;
|
|
|
|
g_valid_region = m_is_valid_region;
|
2020-09-23 09:01:07 +00:00
|
|
|
g_application_process_id = application_process_id;
|
2019-11-22 03:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2019-11-22 03:32:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 09:01:07 +00:00
|
|
|
void SetMitmService::InvalidateLocale() {
|
2021-10-10 07:14:06 +00:00
|
|
|
std::scoped_lock lk(m_lock);
|
2020-09-23 09:01:07 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
std::memset(std::addressof(m_locale), 0xCC, sizeof(m_locale));
|
|
|
|
m_is_valid_language = false;
|
|
|
|
m_is_valid_region = false;
|
|
|
|
m_got_locale = false;
|
2020-09-23 09:01:07 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 03:32:41 +00:00
|
|
|
Result SetMitmService::GetLanguageCode(sf::Out<settings::LanguageCode> out) {
|
|
|
|
this->EnsureLocale();
|
|
|
|
|
|
|
|
/* If there's no override locale, just use the actual one. */
|
2021-10-10 07:14:06 +00:00
|
|
|
if (AMS_UNLIKELY(!m_is_valid_language)) {
|
2020-09-23 09:01:07 +00:00
|
|
|
static_assert(sizeof(u64) == sizeof(settings::LanguageCode));
|
2021-10-10 07:14:06 +00:00
|
|
|
R_TRY(setGetLanguageCodeFwd(m_forward_service.get(), reinterpret_cast<u64 *>(std::addressof(m_locale.language_code))));
|
2020-09-23 09:01:07 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
m_is_valid_language = true;
|
|
|
|
if (m_client_info.program_id == ncm::SystemProgramId::Ns) {
|
|
|
|
g_application_locale.language_code = m_locale.language_code;
|
2020-09-23 09:01:07 +00:00
|
|
|
g_valid_language = true;
|
|
|
|
}
|
|
|
|
}
|
2019-11-22 03:32:41 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
out.SetValue(m_locale.language_code);
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2019-11-22 03:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result SetMitmService::GetRegionCode(sf::Out<settings::RegionCode> out) {
|
|
|
|
this->EnsureLocale();
|
|
|
|
|
|
|
|
/* If there's no override locale, just use the actual one. */
|
2021-10-10 07:14:06 +00:00
|
|
|
if (AMS_UNLIKELY(!m_is_valid_region)) {
|
2020-09-23 09:01:07 +00:00
|
|
|
static_assert(sizeof(::SetRegion) == sizeof(settings::RegionCode));
|
2021-10-10 07:14:06 +00:00
|
|
|
R_TRY(setGetRegionCodeFwd(m_forward_service.get(), reinterpret_cast<::SetRegion *>(std::addressof(m_locale.region_code))));
|
2020-09-23 09:01:07 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
m_is_valid_region = true;
|
|
|
|
if (m_client_info.program_id == ncm::SystemProgramId::Ns) {
|
|
|
|
g_application_locale.region_code = m_locale.region_code;
|
2020-09-23 09:01:07 +00:00
|
|
|
g_valid_region = true;
|
|
|
|
}
|
|
|
|
}
|
2019-11-22 03:32:41 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
out.SetValue(m_locale.region_code);
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2019-11-22 03:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|