2019-02-27 11:30:08 +00:00
|
|
|
/*
|
2019-04-08 02:00:49 +00:00
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
2019-02-27 11:30:08 +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/>.
|
|
|
|
*/
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
#include <map>
|
2019-02-27 11:30:08 +00:00
|
|
|
#include <switch.h>
|
|
|
|
#include "dmnt_cheat_manager.hpp"
|
2019-02-28 02:33:07 +00:00
|
|
|
#include "dmnt_cheat_vm.hpp"
|
2019-03-04 14:55:37 +00:00
|
|
|
#include "dmnt_config.hpp"
|
2019-03-15 10:30:51 +00:00
|
|
|
#include "dmnt_cheat_debug_events_manager.hpp"
|
2019-02-27 12:09:14 +00:00
|
|
|
#include "pm_shim.h"
|
2019-02-27 11:30:08 +00:00
|
|
|
|
|
|
|
static HosMutex g_cheat_lock;
|
2019-03-06 02:57:50 +00:00
|
|
|
static HosThread g_detect_thread, g_vm_thread, g_debug_events_thread;
|
2019-02-27 11:30:08 +00:00
|
|
|
|
2019-02-27 12:37:02 +00:00
|
|
|
static IEvent *g_cheat_process_event;
|
2019-02-28 02:33:07 +00:00
|
|
|
static DmntCheatVm *g_cheat_vm;
|
2019-02-27 12:37:02 +00:00
|
|
|
|
2019-02-27 12:09:14 +00:00
|
|
|
static CheatProcessMetadata g_cheat_process_metadata = {0};
|
|
|
|
static Handle g_cheat_process_debug_hnd = 0;
|
|
|
|
|
2019-03-06 08:20:17 +00:00
|
|
|
/* Should we enable cheats by default? */
|
|
|
|
static bool g_enable_cheats_by_default = true;
|
2019-03-25 17:35:08 +00:00
|
|
|
static bool g_always_save_cheat_toggles = false;
|
|
|
|
static bool g_should_save_cheat_toggles = false;
|
2019-03-06 08:20:17 +00:00
|
|
|
|
2019-03-06 07:58:36 +00:00
|
|
|
/* For debug event thread management. */
|
2019-03-14 16:15:10 +00:00
|
|
|
static HosMutex g_debug_event_thread_lock, g_attach_lock;
|
2019-03-06 07:58:36 +00:00
|
|
|
static bool g_has_debug_events_thread = false;
|
2019-03-06 02:57:50 +00:00
|
|
|
|
|
|
|
/* To save some copying. */
|
|
|
|
static bool g_needs_reload_vm_program = false;
|
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* Global cheat entry storage. */
|
|
|
|
static CheatEntry g_cheat_entries[DmntCheatManager::MaxCheatCount];
|
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
/* Global frozen address storage. */
|
|
|
|
static std::map<u64, FrozenAddressValue> g_frozen_addresses_map;
|
|
|
|
|
2019-03-06 07:58:36 +00:00
|
|
|
void DmntCheatManager::StartDebugEventsThread() {
|
|
|
|
std::scoped_lock<HosMutex> lk(g_debug_event_thread_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-06 07:58:36 +00:00
|
|
|
/* Spawn the debug events thread. */
|
|
|
|
if (!g_has_debug_events_thread) {
|
|
|
|
Result rc;
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-06 07:58:36 +00:00
|
|
|
if (R_FAILED((rc = g_debug_events_thread.Initialize(&DmntCheatManager::DebugEventsThread, nullptr, 0x4000, 48)))) {
|
|
|
|
return fatalSimple(rc);
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-06 07:58:36 +00:00
|
|
|
if (R_FAILED((rc = g_debug_events_thread.Start()))) {
|
|
|
|
return fatalSimple(rc);
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-06 07:58:36 +00:00
|
|
|
g_has_debug_events_thread = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DmntCheatManager::WaitDebugEventsThread() {
|
|
|
|
std::scoped_lock<HosMutex> lk(g_debug_event_thread_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-06 07:58:36 +00:00
|
|
|
/* Wait for the thread to exit. */
|
|
|
|
if (g_has_debug_events_thread) {
|
|
|
|
g_debug_events_thread.CancelSynchronization();
|
|
|
|
g_debug_events_thread.Join();
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-06 07:58:36 +00:00
|
|
|
g_has_debug_events_thread = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DmntCheatManager::CloseActiveCheatProcess() {
|
2019-03-06 08:20:17 +00:00
|
|
|
if (g_cheat_process_debug_hnd != 0) {
|
2019-03-05 04:13:52 +00:00
|
|
|
/* Close process resources. */
|
2019-02-27 12:27:49 +00:00
|
|
|
svcCloseHandle(g_cheat_process_debug_hnd);
|
|
|
|
g_cheat_process_debug_hnd = 0;
|
2019-03-25 17:35:08 +00:00
|
|
|
|
|
|
|
/* Save cheat toggles. */
|
|
|
|
if (g_always_save_cheat_toggles || g_should_save_cheat_toggles) {
|
|
|
|
SaveCheatToggles(g_cheat_process_metadata.title_id);
|
|
|
|
g_should_save_cheat_toggles = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear metadata. */
|
2019-02-27 12:27:49 +00:00
|
|
|
g_cheat_process_metadata = (CheatProcessMetadata){0};
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* Clear cheat list. */
|
|
|
|
ResetAllCheatEntries();
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
/* Clear frozen addresses. */
|
|
|
|
ResetFrozenAddresses();
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* Signal to our fans. */
|
2019-02-27 12:37:02 +00:00
|
|
|
g_cheat_process_event->Signal();
|
2019-02-27 12:27:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DmntCheatManager::HasActiveCheatProcess() {
|
|
|
|
u64 tmp;
|
|
|
|
bool has_cheat_process = g_cheat_process_debug_hnd != 0;
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 12:27:49 +00:00
|
|
|
if (has_cheat_process) {
|
|
|
|
has_cheat_process &= R_SUCCEEDED(svcGetProcessId(&tmp, g_cheat_process_debug_hnd));
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-06 07:58:36 +00:00
|
|
|
if (has_cheat_process) {
|
|
|
|
has_cheat_process &= R_SUCCEEDED(pmdmntGetApplicationPid(&tmp));
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 12:27:49 +00:00
|
|
|
if (has_cheat_process) {
|
|
|
|
has_cheat_process &= tmp == g_cheat_process_metadata.process_id;
|
|
|
|
}
|
2019-03-06 08:20:17 +00:00
|
|
|
|
2019-02-27 12:27:49 +00:00
|
|
|
if (!has_cheat_process) {
|
|
|
|
CloseActiveCheatProcess();
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 12:27:49 +00:00
|
|
|
return has_cheat_process;
|
|
|
|
}
|
|
|
|
|
2019-03-03 14:29:48 +00:00
|
|
|
Result DmntCheatManager::ReadCheatProcessMemoryForVm(u64 proc_addr, void *out_data, size_t size) {
|
|
|
|
if (HasActiveCheatProcess()) {
|
|
|
|
return svcReadDebugProcessMemory(out_data, g_cheat_process_debug_hnd, proc_addr, size);
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 13:56:44 +00:00
|
|
|
return ResultDmntCheatNotAttached;
|
2019-03-03 14:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result DmntCheatManager::WriteCheatProcessMemoryForVm(u64 proc_addr, const void *data, size_t size) {
|
|
|
|
if (HasActiveCheatProcess()) {
|
2019-03-05 19:44:24 +00:00
|
|
|
Result rc = svcWriteDebugProcessMemory(g_cheat_process_debug_hnd, data, proc_addr, size);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 19:44:24 +00:00
|
|
|
/* We might have a frozen address. Update it if we do! */
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
2019-03-05 19:56:17 +00:00
|
|
|
for (auto & [address, value] : g_frozen_addresses_map) {
|
|
|
|
/* Map is in order, so break here. */
|
|
|
|
if (address >= proc_addr + size) {
|
|
|
|
break;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 19:56:17 +00:00
|
|
|
/* Check if we need to write. */
|
|
|
|
if (proc_addr <= address) {
|
|
|
|
const size_t offset = (address - proc_addr);
|
|
|
|
const size_t size_to_copy = size - offset;
|
|
|
|
memcpy(&value.value, (void *)((uintptr_t)data + offset), size_to_copy < sizeof(value.value) ? size_to_copy : sizeof(value.value));
|
|
|
|
}
|
2019-03-05 19:44:24 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 19:44:24 +00:00
|
|
|
return rc;
|
2019-03-03 14:29:48 +00:00
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 13:56:44 +00:00
|
|
|
return ResultDmntCheatNotAttached;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Result DmntCheatManager::GetCheatProcessMappingCount(u64 *out_count) {
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 13:56:44 +00:00
|
|
|
if (!HasActiveCheatProcess()) {
|
|
|
|
return ResultDmntCheatNotAttached;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 13:56:44 +00:00
|
|
|
MemoryInfo mem_info;
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 13:56:44 +00:00
|
|
|
u64 address = 0;
|
|
|
|
*out_count = 0;
|
|
|
|
do {
|
|
|
|
mem_info.perm = 0;
|
|
|
|
u32 tmp;
|
|
|
|
if (R_FAILED(svcQueryDebugProcessMemory(&mem_info, &tmp, g_cheat_process_debug_hnd, address))) {
|
|
|
|
break;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 13:56:44 +00:00
|
|
|
if (mem_info.perm != 0) {
|
|
|
|
*out_count += 1;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 13:56:44 +00:00
|
|
|
address = mem_info.addr + mem_info.size;
|
|
|
|
} while (address != 0);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2019-03-04 13:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result DmntCheatManager::GetCheatProcessMappings(MemoryInfo *mappings, size_t max_count, u64 *out_count, u64 offset) {
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 13:56:44 +00:00
|
|
|
if (!HasActiveCheatProcess()) {
|
|
|
|
return ResultDmntCheatNotAttached;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 13:56:44 +00:00
|
|
|
MemoryInfo mem_info;
|
|
|
|
u64 address = 0;
|
|
|
|
u64 count = 0;
|
|
|
|
*out_count = 0;
|
|
|
|
do {
|
|
|
|
mem_info.perm = 0;
|
|
|
|
u32 tmp;
|
|
|
|
if (R_FAILED(svcQueryDebugProcessMemory(&mem_info, &tmp, g_cheat_process_debug_hnd, address))) {
|
|
|
|
break;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 13:56:44 +00:00
|
|
|
if (mem_info.perm != 0) {
|
|
|
|
count++;
|
|
|
|
if (count > offset) {
|
|
|
|
mappings[(*out_count)++] = mem_info;
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 13:56:44 +00:00
|
|
|
address = mem_info.addr + mem_info.size;
|
|
|
|
} while (address != 0 && *out_count < max_count);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2019-03-03 14:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result DmntCheatManager::ReadCheatProcessMemory(u64 proc_addr, void *out_data, size_t size) {
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-03 14:29:48 +00:00
|
|
|
return ReadCheatProcessMemoryForVm(proc_addr, out_data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result DmntCheatManager::WriteCheatProcessMemory(u64 proc_addr, const void *data, size_t size) {
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-03 14:29:48 +00:00
|
|
|
return WriteCheatProcessMemoryForVm(proc_addr, data, size);
|
|
|
|
}
|
|
|
|
|
2019-03-04 13:56:44 +00:00
|
|
|
Result DmntCheatManager::QueryCheatProcessMemory(MemoryInfo *mapping, u64 address) {
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 13:56:44 +00:00
|
|
|
if (HasActiveCheatProcess()) {
|
|
|
|
u32 tmp;
|
|
|
|
return svcQueryDebugProcessMemory(mapping, &tmp, g_cheat_process_debug_hnd, address);
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 13:56:44 +00:00
|
|
|
return ResultDmntCheatNotAttached;
|
|
|
|
}
|
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
void DmntCheatManager::ResetFrozenAddresses() {
|
|
|
|
/* Just clear the map. */
|
|
|
|
g_frozen_addresses_map.clear();
|
|
|
|
}
|
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
void DmntCheatManager::ResetCheatEntry(size_t i) {
|
|
|
|
if (i < DmntCheatManager::MaxCheatCount) {
|
|
|
|
g_cheat_entries[i].enabled = false;
|
|
|
|
g_cheat_entries[i].cheat_id = i;
|
|
|
|
g_cheat_entries[i].definition = {0};
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-06 02:57:50 +00:00
|
|
|
/* Trigger a VM reload. */
|
|
|
|
g_needs_reload_vm_program = true;
|
2019-03-05 04:13:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DmntCheatManager::ResetAllCheatEntries() {
|
|
|
|
for (size_t i = 0; i < DmntCheatManager::MaxCheatCount; i++) {
|
|
|
|
ResetCheatEntry(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CheatEntry *DmntCheatManager::GetFreeCheatEntry() {
|
|
|
|
/* Check all non-master cheats for availability. */
|
|
|
|
for (size_t i = 1; i < DmntCheatManager::MaxCheatCount; i++) {
|
|
|
|
if (g_cheat_entries[i].definition.num_opcodes == 0) {
|
|
|
|
return &g_cheat_entries[i];
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-03-05 05:11:12 +00:00
|
|
|
CheatEntry *DmntCheatManager::GetCheatEntryById(size_t i) {
|
|
|
|
if (i < DmntCheatManager::MaxCheatCount) {
|
|
|
|
return &g_cheat_entries[i];
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheatEntry *DmntCheatManager::GetCheatEntryByReadableName(const char *readable_name) {
|
|
|
|
/* Check all non-master cheats for match. */
|
|
|
|
for (size_t i = 1; i < DmntCheatManager::MaxCheatCount; i++) {
|
|
|
|
if (strcmp(g_cheat_entries[i].definition.readable_name, readable_name) == 0) {
|
|
|
|
return &g_cheat_entries[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 05:11:12 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
bool DmntCheatManager::ParseCheats(const char *s, size_t len) {
|
|
|
|
size_t i = 0;
|
|
|
|
CheatEntry *cur_entry = NULL;
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-06 02:57:50 +00:00
|
|
|
/* Trigger a VM reload. */
|
|
|
|
g_needs_reload_vm_program = true;
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
while (i < len) {
|
|
|
|
if (isspace(s[i])) {
|
|
|
|
/* Just ignore space. */
|
|
|
|
i++;
|
|
|
|
} else if (s[i] == '[') {
|
|
|
|
/* Parse a readable cheat name. */
|
|
|
|
cur_entry = GetFreeCheatEntry();
|
|
|
|
if (cur_entry == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* Extract name bounds. */
|
|
|
|
size_t j = i + 1;
|
|
|
|
while (s[j] != ']') {
|
|
|
|
j++;
|
|
|
|
if (j >= len || (j - i - 1) >= sizeof(cur_entry->definition.readable_name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* s[i+1:j] is cheat name. */
|
|
|
|
const size_t cheat_name_len = (j - i - 1);
|
|
|
|
memcpy(cur_entry->definition.readable_name, &s[i+1], cheat_name_len);
|
|
|
|
cur_entry->definition.readable_name[cheat_name_len] = 0;
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* Skip onwards. */
|
|
|
|
i = j + 1;
|
|
|
|
} else if (s[i] == '{') {
|
|
|
|
/* We're parsing a master cheat. */
|
|
|
|
cur_entry = &g_cheat_entries[0];
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* There can only be one master cheat. */
|
|
|
|
if (cur_entry->definition.num_opcodes > 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* Extract name bounds */
|
|
|
|
size_t j = i + 1;
|
|
|
|
while (s[j] != '}') {
|
|
|
|
j++;
|
|
|
|
if (j >= len || (j - i - 1) >= sizeof(cur_entry->definition.readable_name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* s[i+1:j] is cheat name. */
|
|
|
|
const size_t cheat_name_len = (j - i - 1);
|
|
|
|
memcpy(cur_entry->definition.readable_name, &s[i+1], cheat_name_len);
|
|
|
|
cur_entry->definition.readable_name[cheat_name_len] = 0;
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* Skip onwards. */
|
|
|
|
i = j + 1;
|
|
|
|
} else if (isxdigit(s[i])) {
|
|
|
|
/* Make sure that we have a cheat open. */
|
|
|
|
if (cur_entry == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:24:49 +00:00
|
|
|
/* Bounds check the opcode count. */
|
|
|
|
if (cur_entry->definition.num_opcodes >= sizeof(cur_entry->definition.opcodes)/sizeof(cur_entry->definition.opcodes[0])) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* We're parsing an instruction, so validate it's 8 hex digits. */
|
|
|
|
for (size_t j = 1; j < 8; j++) {
|
|
|
|
/* Validate 8 hex chars. */
|
|
|
|
if (i + j >= len || !isxdigit(s[i+j])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* Parse the new opcode. */
|
|
|
|
char hex_str[9] = {0};
|
|
|
|
memcpy(hex_str, &s[i], 8);
|
|
|
|
cur_entry->definition.opcodes[cur_entry->definition.num_opcodes++] = strtoul(hex_str, NULL, 16);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* Skip onwards. */
|
|
|
|
i += 8;
|
|
|
|
} else {
|
|
|
|
/* Unexpected character encountered. */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-06 08:20:17 +00:00
|
|
|
/* Master cheat can't be disabled. */
|
|
|
|
if (g_cheat_entries[0].definition.num_opcodes > 0) {
|
|
|
|
g_cheat_entries[0].enabled = true;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* Enable all entries we parsed. */
|
2019-03-06 08:20:17 +00:00
|
|
|
for (size_t i = 1; i < DmntCheatManager::MaxCheatCount; i++) {
|
2019-03-05 04:13:52 +00:00
|
|
|
if (g_cheat_entries[i].definition.num_opcodes > 0) {
|
2019-03-06 08:20:17 +00:00
|
|
|
g_cheat_entries[i].enabled = g_enable_cheats_by_default;
|
2019-03-05 04:13:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DmntCheatManager::LoadCheats(u64 title_id, const u8 *build_id) {
|
|
|
|
/* Reset existing entries. */
|
|
|
|
ResetAllCheatEntries();
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
FILE *f_cht = NULL;
|
|
|
|
/* Open the file for title/build_id. */
|
|
|
|
{
|
|
|
|
char path[FS_MAX_PATH+1] = {0};
|
|
|
|
snprintf(path, FS_MAX_PATH, "sdmc:/atmosphere/titles/%016lx/cheats/%02x%02x%02x%02x%02x%02x%02x%02x.txt", title_id,
|
2019-03-25 17:35:08 +00:00
|
|
|
build_id[0], build_id[1], build_id[2], build_id[3], build_id[4], build_id[5], build_id[6], build_id[7]);
|
2019-03-05 04:13:52 +00:00
|
|
|
|
|
|
|
f_cht = fopen(path, "rb");
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* Check for NULL */
|
|
|
|
if (f_cht == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ON_SCOPE_EXIT { fclose(f_cht); };
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* Get file size. */
|
|
|
|
fseek(f_cht, 0L, SEEK_END);
|
|
|
|
const size_t cht_sz = ftell(f_cht);
|
|
|
|
fseek(f_cht, 0L, SEEK_SET);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* Allocate cheat txt buffer. */
|
|
|
|
char *cht_txt = (char *)malloc(cht_sz + 1);
|
|
|
|
if (cht_txt == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ON_SCOPE_EXIT { free(cht_txt); };
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* Read cheats into buffer. */
|
|
|
|
if (fread(cht_txt, 1, cht_sz, f_cht) != cht_sz) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
cht_txt[cht_sz] = 0;
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* Parse cheat buffer. */
|
|
|
|
return ParseCheats(cht_txt, strlen(cht_txt));
|
|
|
|
}
|
|
|
|
|
2019-03-25 17:35:08 +00:00
|
|
|
bool DmntCheatManager::ParseCheatToggles(const char *s, size_t len) {
|
|
|
|
size_t i = 0;
|
|
|
|
char cur_cheat_name[sizeof(CheatEntry::definition.readable_name)];
|
|
|
|
char toggle[8];
|
|
|
|
|
|
|
|
while (i < len) {
|
|
|
|
if (isspace(s[i])) {
|
|
|
|
/* Just ignore space. */
|
|
|
|
i++;
|
|
|
|
} else if (s[i] == '[') {
|
|
|
|
|
|
|
|
/* Extract name bounds. */
|
|
|
|
size_t j = i + 1;
|
|
|
|
while (s[j] != ']') {
|
|
|
|
j++;
|
|
|
|
if (j >= len || (j - i - 1) >= sizeof(cur_cheat_name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* s[i+1:j] is cheat name. */
|
|
|
|
const size_t cheat_name_len = (j - i - 1);
|
|
|
|
memcpy(cur_cheat_name, &s[i+1], cheat_name_len);
|
|
|
|
cur_cheat_name[cheat_name_len] = 0;
|
|
|
|
|
|
|
|
/* Skip onwards. */
|
|
|
|
i = j + 1;
|
|
|
|
|
|
|
|
/* Skip whitespace. */
|
|
|
|
while (isspace(s[i])) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse whether to toggle. */
|
|
|
|
j = i + 1;
|
|
|
|
while (!isspace(s[j])) {
|
|
|
|
j++;
|
|
|
|
if (j >= len || (j - i) >= sizeof(toggle)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* s[i:j] is toggle. */
|
|
|
|
const size_t toggle_len = (j - i);
|
|
|
|
memcpy(toggle, &s[i], toggle_len);
|
|
|
|
toggle[toggle_len] = 0;
|
|
|
|
|
|
|
|
/* Allow specifying toggle for not present cheat. */
|
|
|
|
CheatEntry *entry = GetCheatEntryByReadableName(cur_cheat_name);
|
|
|
|
if (entry != nullptr) {
|
|
|
|
if (strcasecmp(toggle, "1") == 0 || strcasecmp(toggle, "true") == 0 || strcasecmp(toggle, "on") == 0) {
|
|
|
|
entry->enabled = true;
|
|
|
|
} else if (strcasecmp(toggle, "0") == 0 || strcasecmp(toggle, "false") == 0 || strcasecmp(toggle, "off") == 0) {
|
|
|
|
entry->enabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip onwards. */
|
|
|
|
i = j + 1;
|
|
|
|
} else {
|
|
|
|
/* Unexpected character encountered. */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DmntCheatManager::LoadCheatToggles(u64 title_id) {
|
|
|
|
FILE *f_tg = NULL;
|
|
|
|
/* Open the file for title id. */
|
|
|
|
{
|
|
|
|
char path[FS_MAX_PATH+1] = {0};
|
|
|
|
snprintf(path, FS_MAX_PATH, "sdmc:/atmosphere/titles/%016lx/cheats/toggles.txt", title_id);
|
|
|
|
|
|
|
|
f_tg = fopen(path, "rb");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unless we successfully parse, don't save toggles on close. */
|
|
|
|
g_should_save_cheat_toggles = false;
|
|
|
|
|
|
|
|
/* Check for NULL, which is allowed. */
|
|
|
|
if (f_tg == NULL) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
ON_SCOPE_EXIT { fclose(f_tg); };
|
|
|
|
|
|
|
|
/* Get file size. */
|
|
|
|
fseek(f_tg, 0L, SEEK_END);
|
|
|
|
const size_t tg_sz = ftell(f_tg);
|
|
|
|
fseek(f_tg, 0L, SEEK_SET);
|
|
|
|
|
|
|
|
/* Allocate toggle txt buffer. */
|
|
|
|
char *tg_txt = (char *)malloc(tg_sz + 1);
|
|
|
|
if (tg_txt == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ON_SCOPE_EXIT { free(tg_txt); };
|
|
|
|
|
|
|
|
/* Read toggles into buffer. */
|
|
|
|
if (fread(tg_txt, 1, tg_sz, f_tg) != tg_sz) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
tg_txt[tg_sz] = 0;
|
|
|
|
|
|
|
|
/* Parse toggle buffer. */
|
|
|
|
g_should_save_cheat_toggles = ParseCheatToggles(tg_txt, strlen(tg_txt));
|
|
|
|
return g_should_save_cheat_toggles;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DmntCheatManager::SaveCheatToggles(u64 title_id) {
|
|
|
|
FILE *f_tg = NULL;
|
|
|
|
/* Open the file for title id. */
|
|
|
|
{
|
|
|
|
char path[FS_MAX_PATH+1] = {0};
|
|
|
|
snprintf(path, FS_MAX_PATH, "sdmc:/atmosphere/titles/%016lx/cheats/toggles.txt", title_id);
|
|
|
|
|
|
|
|
f_tg = fopen(path, "wb");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f_tg == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ON_SCOPE_EXIT { fclose(f_tg); };
|
|
|
|
|
|
|
|
/* Save all non-master cheats. */
|
|
|
|
for (size_t i = 1; i < DmntCheatManager::MaxCheatCount; i++) {
|
|
|
|
if (g_cheat_entries[i].definition.num_opcodes != 0) {
|
|
|
|
fprintf(f_tg, "[%s]\n", g_cheat_entries[i].definition.readable_name);
|
|
|
|
if (g_cheat_entries[i].enabled) {
|
|
|
|
fprintf(f_tg, "true\n");
|
|
|
|
} else {
|
|
|
|
fprintf(f_tg, "false\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 05:11:12 +00:00
|
|
|
Result DmntCheatManager::GetCheatCount(u64 *out_count) {
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 05:11:12 +00:00
|
|
|
if (!HasActiveCheatProcess()) {
|
|
|
|
return ResultDmntCheatNotAttached;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 05:11:12 +00:00
|
|
|
*out_count = 0;
|
|
|
|
for (size_t i = 0; i < DmntCheatManager::MaxCheatCount; i++) {
|
|
|
|
if (g_cheat_entries[i].definition.num_opcodes > 0) {
|
|
|
|
*out_count += 1;
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2019-03-05 05:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result DmntCheatManager::GetCheats(CheatEntry *cheats, size_t max_count, u64 *out_count, u64 offset) {
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 05:11:12 +00:00
|
|
|
if (!HasActiveCheatProcess()) {
|
|
|
|
return ResultDmntCheatNotAttached;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 05:11:12 +00:00
|
|
|
u64 count = 0;
|
|
|
|
*out_count = 0;
|
|
|
|
for (size_t i = 0; i < DmntCheatManager::MaxCheatCount && (*out_count) < max_count; i++) {
|
|
|
|
if (g_cheat_entries[i].definition.num_opcodes > 0) {
|
|
|
|
count++;
|
|
|
|
if (count > offset) {
|
|
|
|
cheats[(*out_count)++] = g_cheat_entries[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2019-03-05 05:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result DmntCheatManager::GetCheatById(CheatEntry *out_cheat, u32 cheat_id) {
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 05:11:12 +00:00
|
|
|
if (!HasActiveCheatProcess()) {
|
|
|
|
return ResultDmntCheatNotAttached;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 05:11:12 +00:00
|
|
|
const CheatEntry *entry = GetCheatEntryById(cheat_id);
|
|
|
|
if (entry == nullptr || entry->definition.num_opcodes == 0) {
|
|
|
|
return ResultDmntCheatUnknownChtId;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 05:11:12 +00:00
|
|
|
*out_cheat = *entry;
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2019-03-05 05:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result DmntCheatManager::ToggleCheat(u32 cheat_id) {
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 05:11:12 +00:00
|
|
|
if (!HasActiveCheatProcess()) {
|
|
|
|
return ResultDmntCheatNotAttached;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 05:11:12 +00:00
|
|
|
CheatEntry *entry = GetCheatEntryById(cheat_id);
|
|
|
|
if (entry == nullptr || entry->definition.num_opcodes == 0) {
|
|
|
|
return ResultDmntCheatUnknownChtId;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-06 08:20:17 +00:00
|
|
|
if (cheat_id == 0) {
|
|
|
|
return ResultDmntCheatCannotDisableMasterCheat;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 05:11:12 +00:00
|
|
|
entry->enabled = !entry->enabled;
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-06 02:57:50 +00:00
|
|
|
/* Trigger a VM reload. */
|
|
|
|
g_needs_reload_vm_program = true;
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2019-03-05 05:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result DmntCheatManager::AddCheat(u32 *out_id, CheatDefinition *def, bool enabled) {
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 05:11:12 +00:00
|
|
|
if (!HasActiveCheatProcess()) {
|
|
|
|
return ResultDmntCheatNotAttached;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 05:11:12 +00:00
|
|
|
if (def->num_opcodes == 0 || def->num_opcodes > sizeof(def->opcodes)/sizeof(def->opcodes[0])) {
|
|
|
|
return ResultDmntCheatInvalidCheat;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 05:11:12 +00:00
|
|
|
CheatEntry *new_entry = GetFreeCheatEntry();
|
|
|
|
if (new_entry == nullptr) {
|
|
|
|
return ResultDmntCheatOutOfCheats;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 05:11:12 +00:00
|
|
|
new_entry->enabled = enabled;
|
|
|
|
new_entry->definition = *def;
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-06 02:57:50 +00:00
|
|
|
/* Trigger a VM reload. */
|
|
|
|
g_needs_reload_vm_program = true;
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2019-03-05 05:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result DmntCheatManager::RemoveCheat(u32 cheat_id) {
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 05:11:12 +00:00
|
|
|
if (!HasActiveCheatProcess()) {
|
|
|
|
return ResultDmntCheatNotAttached;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 05:11:12 +00:00
|
|
|
if (cheat_id >= DmntCheatManager::MaxCheatCount) {
|
|
|
|
return ResultDmntCheatUnknownChtId;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 05:11:12 +00:00
|
|
|
ResetCheatEntry(cheat_id);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-06 02:57:50 +00:00
|
|
|
/* Trigger a VM reload. */
|
|
|
|
g_needs_reload_vm_program = true;
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2019-03-05 05:11:12 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
Result DmntCheatManager::GetFrozenAddressCount(u64 *out_count) {
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
if (!HasActiveCheatProcess()) {
|
|
|
|
return ResultDmntCheatNotAttached;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
*out_count = g_frozen_addresses_map.size();
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2019-03-05 06:13:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result DmntCheatManager::GetFrozenAddresses(FrozenAddressEntry *frz_addrs, size_t max_count, u64 *out_count, u64 offset) {
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
if (!HasActiveCheatProcess()) {
|
|
|
|
return ResultDmntCheatNotAttached;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
u64 count = 0;
|
|
|
|
*out_count = 0;
|
|
|
|
for (auto const& [address, value] : g_frozen_addresses_map) {
|
|
|
|
if ((*out_count) >= max_count) {
|
|
|
|
break;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
count++;
|
|
|
|
if (count > offset) {
|
|
|
|
const u64 cur_ind = (*out_count)++;
|
|
|
|
frz_addrs[cur_ind].address = address;
|
|
|
|
frz_addrs[cur_ind].value = value;
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2019-03-05 06:13:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result DmntCheatManager::GetFrozenAddress(FrozenAddressEntry *frz_addr, u64 address) {
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
if (!HasActiveCheatProcess()) {
|
|
|
|
return ResultDmntCheatNotAttached;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
const auto it = g_frozen_addresses_map.find(address);
|
|
|
|
if (it == g_frozen_addresses_map.end()) {
|
|
|
|
return ResultDmntCheatAddressNotFrozen;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
frz_addr->address = it->first;
|
|
|
|
frz_addr->value = it->second;
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2019-03-05 06:13:43 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 06:17:18 +00:00
|
|
|
Result DmntCheatManager::EnableFrozenAddress(u64 *out_value, u64 address, u64 width) {
|
2019-03-05 06:13:43 +00:00
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
if (!HasActiveCheatProcess()) {
|
|
|
|
return ResultDmntCheatNotAttached;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
if (g_frozen_addresses_map.size() >= DmntCheatManager::MaxFrozenAddressCount) {
|
|
|
|
return ResultDmntCheatTooManyFrozenAddresses;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
const auto it = g_frozen_addresses_map.find(address);
|
|
|
|
if (it != g_frozen_addresses_map.end()) {
|
|
|
|
return ResultDmntCheatAddressAlreadyFrozen;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
Result rc;
|
|
|
|
FrozenAddressValue value = {0};
|
|
|
|
value.width = width;
|
|
|
|
if (R_FAILED((rc = ReadCheatProcessMemoryForVm(address, &value.value, width)))) {
|
|
|
|
return rc;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
g_frozen_addresses_map[address] = value;
|
2019-03-05 06:17:18 +00:00
|
|
|
*out_value = value.value;
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2019-03-05 06:13:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result DmntCheatManager::DisableFrozenAddress(u64 address) {
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
if (!HasActiveCheatProcess()) {
|
|
|
|
return ResultDmntCheatNotAttached;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
const auto it = g_frozen_addresses_map.find(address);
|
|
|
|
if (it == g_frozen_addresses_map.end()) {
|
|
|
|
return ResultDmntCheatAddressNotFrozen;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
g_frozen_addresses_map.erase(address);
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2019-03-05 06:13:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 11:30:08 +00:00
|
|
|
Handle DmntCheatManager::PrepareDebugNextApplication() {
|
|
|
|
Result rc;
|
|
|
|
Handle event_h;
|
|
|
|
if (R_FAILED((rc = pmdmntEnableDebugForApplication(&event_h)))) {
|
|
|
|
fatalSimple(rc);
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 11:30:08 +00:00
|
|
|
return event_h;
|
|
|
|
}
|
|
|
|
|
2019-02-27 12:09:14 +00:00
|
|
|
static void PopulateMemoryExtents(MemoryRegionExtents *extents, Handle p_h, u64 id_base, u64 id_size) {
|
|
|
|
Result rc;
|
|
|
|
/* Get base extent. */
|
|
|
|
if (R_FAILED((rc = svcGetInfo(&extents->base, id_base, p_h, 0)))) {
|
|
|
|
fatalSimple(rc);
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 12:09:14 +00:00
|
|
|
/* Get size extent. */
|
|
|
|
if (R_FAILED((rc = svcGetInfo(&extents->size, id_size, p_h, 0)))) {
|
|
|
|
fatalSimple(rc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:42:52 +00:00
|
|
|
static void StartDebugProcess(u64 pid) {
|
|
|
|
Result rc = pmdmntStartProcess(pid);
|
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
fatalSimple(rc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 14:55:37 +00:00
|
|
|
Result DmntCheatManager::ForceOpenCheatProcess() {
|
2019-03-14 16:15:10 +00:00
|
|
|
std::scoped_lock<HosMutex> attach_lk(g_attach_lock);
|
2019-03-04 14:55:37 +00:00
|
|
|
Result rc;
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-14 16:15:10 +00:00
|
|
|
/* Acquire the cheat lock for long enough to close the process if needed. */
|
|
|
|
{
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-14 16:15:10 +00:00
|
|
|
if (HasActiveCheatProcess()) {
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2019-03-14 16:15:10 +00:00
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-14 16:15:10 +00:00
|
|
|
/* Close the current application, if it's open. */
|
|
|
|
CloseActiveCheatProcess();
|
2019-03-04 14:55:37 +00:00
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-14 16:15:10 +00:00
|
|
|
/* Intentionally yield the cheat lock to the debug events thread. */
|
2019-03-06 07:58:36 +00:00
|
|
|
/* Wait to not have debug events thread. */
|
|
|
|
WaitDebugEventsThread();
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-14 16:15:10 +00:00
|
|
|
/* At this point, we can re-acquire the lock for the rest of the function. */
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 14:55:37 +00:00
|
|
|
/* Get the current application process ID. */
|
|
|
|
if (R_FAILED((rc = pmdmntGetApplicationPid(&g_cheat_process_metadata.process_id)))) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
ON_SCOPE_EXIT { if (R_FAILED(rc)) { g_cheat_process_metadata.process_id = 0; } };
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 14:55:37 +00:00
|
|
|
/* Get process handle, use it to learn memory extents. */
|
|
|
|
{
|
|
|
|
Handle proc_h = 0;
|
|
|
|
ON_SCOPE_EXIT { if (proc_h != 0) { svcCloseHandle(proc_h); } };
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 14:55:37 +00:00
|
|
|
if (R_FAILED((rc = pmdmntAtmosphereGetProcessInfo(&proc_h, &g_cheat_process_metadata.title_id, nullptr, g_cheat_process_metadata.process_id)))) {
|
|
|
|
return rc;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 14:55:37 +00:00
|
|
|
/* Get memory extents. */
|
|
|
|
PopulateMemoryExtents(&g_cheat_process_metadata.heap_extents, proc_h, 4, 5);
|
|
|
|
PopulateMemoryExtents(&g_cheat_process_metadata.alias_extents, proc_h, 2, 3);
|
|
|
|
if (kernelAbove200()) {
|
|
|
|
PopulateMemoryExtents(&g_cheat_process_metadata.address_space_extents, proc_h, 12, 13);
|
|
|
|
} else {
|
|
|
|
g_cheat_process_metadata.address_space_extents.base = 0x08000000UL;
|
|
|
|
g_cheat_process_metadata.address_space_extents.size = 0x78000000UL;
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 14:55:37 +00:00
|
|
|
/* Get module information from Loader. */
|
|
|
|
{
|
|
|
|
LoaderModuleInfo proc_modules[2];
|
|
|
|
u32 num_modules;
|
|
|
|
if (R_FAILED((rc = ldrDmntGetModuleInfos(g_cheat_process_metadata.process_id, proc_modules, sizeof(proc_modules), &num_modules)))) {
|
|
|
|
return rc;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 14:55:37 +00:00
|
|
|
/* All applications must have two modules. */
|
|
|
|
/* However, this is a force-open, so we will accept one module. */
|
|
|
|
/* Poor HBL, I guess... */
|
|
|
|
LoaderModuleInfo *proc_module;
|
|
|
|
if (num_modules == 2) {
|
|
|
|
proc_module = &proc_modules[1];
|
|
|
|
} else if (num_modules == 1) {
|
|
|
|
proc_module = &proc_modules[0];
|
|
|
|
} else {
|
|
|
|
rc = ResultDmntCheatNotAttached;
|
|
|
|
return rc;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 14:55:37 +00:00
|
|
|
g_cheat_process_metadata.main_nso_extents.base = proc_module->base_address;
|
|
|
|
g_cheat_process_metadata.main_nso_extents.size = proc_module->size;
|
|
|
|
memcpy(g_cheat_process_metadata.main_nso_build_id, proc_module->build_id, sizeof(g_cheat_process_metadata.main_nso_build_id));
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* Read cheats off the SD. */
|
|
|
|
/* This is allowed to fail. We may not have any cheats. */
|
|
|
|
LoadCheats(g_cheat_process_metadata.title_id, g_cheat_process_metadata.main_nso_build_id);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
|
|
|
/* Load saved toggles, if present. */
|
|
|
|
LoadCheatToggles(g_cheat_process_metadata.title_id);
|
|
|
|
|
2019-03-04 14:55:37 +00:00
|
|
|
/* Open a debug handle. */
|
|
|
|
if (R_FAILED((rc = svcDebugActiveProcess(&g_cheat_process_debug_hnd, g_cheat_process_metadata.process_id)))) {
|
|
|
|
return rc;
|
|
|
|
}
|
2019-03-06 07:58:36 +00:00
|
|
|
/* Start debug events thread. */
|
|
|
|
StartDebugEventsThread();
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 14:55:37 +00:00
|
|
|
/* Signal to our fans. */
|
|
|
|
g_cheat_process_event->Signal();
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 14:55:37 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-02-27 11:30:08 +00:00
|
|
|
void DmntCheatManager::OnNewApplicationLaunch() {
|
2019-03-14 16:15:10 +00:00
|
|
|
std::scoped_lock<HosMutex> attach_lk(g_attach_lock);
|
2019-02-27 12:09:14 +00:00
|
|
|
Result rc;
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-14 16:15:10 +00:00
|
|
|
{
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
|
|
|
/* Close the current application, if it's open. */
|
|
|
|
CloseActiveCheatProcess();
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-14 16:15:10 +00:00
|
|
|
/* Intentionally yield the cheat lock to the debug events thread. */
|
2019-03-06 07:58:36 +00:00
|
|
|
/* Wait to not have debug events thread. */
|
|
|
|
WaitDebugEventsThread();
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-14 16:15:10 +00:00
|
|
|
/* At this point, we can re-acquire the lock for the rest of the function. */
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 12:09:14 +00:00
|
|
|
/* Get the new application's process ID. */
|
|
|
|
if (R_FAILED((rc = pmdmntGetApplicationPid(&g_cheat_process_metadata.process_id)))) {
|
|
|
|
fatalSimple(rc);
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 12:09:14 +00:00
|
|
|
/* Get process handle, use it to learn memory extents. */
|
|
|
|
{
|
|
|
|
Handle proc_h = 0;
|
|
|
|
ON_SCOPE_EXIT { if (proc_h != 0) { svcCloseHandle(proc_h); } };
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 14:55:37 +00:00
|
|
|
if (R_FAILED((rc = pmdmntAtmosphereGetProcessInfo(&proc_h, &g_cheat_process_metadata.title_id, nullptr, g_cheat_process_metadata.process_id)))) {
|
2019-02-27 12:09:14 +00:00
|
|
|
fatalSimple(rc);
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 12:09:14 +00:00
|
|
|
/* Get memory extents. */
|
|
|
|
PopulateMemoryExtents(&g_cheat_process_metadata.heap_extents, proc_h, 4, 5);
|
|
|
|
PopulateMemoryExtents(&g_cheat_process_metadata.alias_extents, proc_h, 2, 3);
|
|
|
|
if (kernelAbove200()) {
|
|
|
|
PopulateMemoryExtents(&g_cheat_process_metadata.address_space_extents, proc_h, 12, 13);
|
|
|
|
} else {
|
|
|
|
g_cheat_process_metadata.address_space_extents.base = 0x08000000UL;
|
|
|
|
g_cheat_process_metadata.address_space_extents.size = 0x78000000UL;
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 14:55:37 +00:00
|
|
|
/* Check if we should skip based on keys down. */
|
|
|
|
if (!DmntConfigManager::HasCheatEnableButton(g_cheat_process_metadata.title_id)) {
|
|
|
|
StartDebugProcess(g_cheat_process_metadata.process_id);
|
|
|
|
g_cheat_process_metadata.process_id = 0;
|
|
|
|
return;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 12:09:14 +00:00
|
|
|
/* Get module information from Loader. */
|
|
|
|
{
|
|
|
|
LoaderModuleInfo proc_modules[2];
|
|
|
|
u32 num_modules;
|
2019-03-04 07:42:52 +00:00
|
|
|
if (R_FAILED((rc = ldrDmntGetModuleInfos(g_cheat_process_metadata.process_id, proc_modules, sizeof(proc_modules), &num_modules)))) {
|
2019-02-27 12:09:14 +00:00
|
|
|
fatalSimple(rc);
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 12:09:14 +00:00
|
|
|
/* All applications must have two modules. */
|
|
|
|
/* If we only have one, we must be e.g. mitming HBL. */
|
|
|
|
/* We don't want to fuck with HBL. */
|
|
|
|
if (num_modules != 2) {
|
2019-03-04 07:42:52 +00:00
|
|
|
StartDebugProcess(g_cheat_process_metadata.process_id);
|
2019-02-27 12:09:14 +00:00
|
|
|
g_cheat_process_metadata.process_id = 0;
|
|
|
|
return;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 12:09:14 +00:00
|
|
|
g_cheat_process_metadata.main_nso_extents.base = proc_modules[1].base_address;
|
|
|
|
g_cheat_process_metadata.main_nso_extents.size = proc_modules[1].size;
|
2019-02-27 12:27:49 +00:00
|
|
|
memcpy(g_cheat_process_metadata.main_nso_build_id, proc_modules[1].build_id, sizeof(g_cheat_process_metadata.main_nso_build_id));
|
2019-02-27 12:09:14 +00:00
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
/* Read cheats off the SD. */
|
2019-03-25 17:35:08 +00:00
|
|
|
if (!LoadCheats(g_cheat_process_metadata.title_id, g_cheat_process_metadata.main_nso_build_id) || !LoadCheatToggles(g_cheat_process_metadata.title_id)) {
|
2019-03-05 04:13:52 +00:00
|
|
|
/* If we don't have cheats, or cheats are malformed, don't attach. */
|
|
|
|
StartDebugProcess(g_cheat_process_metadata.process_id);
|
|
|
|
g_cheat_process_metadata.process_id = 0;
|
|
|
|
return;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 12:09:14 +00:00
|
|
|
/* Open a debug handle. */
|
|
|
|
if (R_FAILED((rc = svcDebugActiveProcess(&g_cheat_process_debug_hnd, g_cheat_process_metadata.process_id)))) {
|
|
|
|
fatalSimple(rc);
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 07:42:52 +00:00
|
|
|
/* Start the process. */
|
|
|
|
StartDebugProcess(g_cheat_process_metadata.process_id);
|
2019-03-06 07:58:36 +00:00
|
|
|
|
|
|
|
/* Start debug events thread. */
|
|
|
|
StartDebugEventsThread();
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 12:37:02 +00:00
|
|
|
/* Signal to our fans. */
|
|
|
|
g_cheat_process_event->Signal();
|
2019-02-27 11:30:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DmntCheatManager::DetectThread(void *arg) {
|
|
|
|
auto waiter = new WaitableManager(1);
|
|
|
|
waiter->AddWaitable(LoadReadOnlySystemEvent(PrepareDebugNextApplication(), [](u64 timeout) {
|
|
|
|
/* Process stuff for new application. */
|
|
|
|
DmntCheatManager::OnNewApplicationLaunch();
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 11:30:08 +00:00
|
|
|
/* Setup detection for the next application, and close the duplicate handle. */
|
|
|
|
svcCloseHandle(PrepareDebugNextApplication());
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2019-02-27 11:30:08 +00:00
|
|
|
}, true));
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 11:30:08 +00:00
|
|
|
waiter->Process();
|
|
|
|
delete waiter;
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 11:30:08 +00:00
|
|
|
void DmntCheatManager::VmThread(void *arg) {
|
|
|
|
while (true) {
|
2019-02-27 12:27:49 +00:00
|
|
|
/* Execute Cheat VM. */
|
|
|
|
{
|
|
|
|
/* Acquire lock. */
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 12:27:49 +00:00
|
|
|
if (HasActiveCheatProcess()) {
|
2019-02-28 02:33:07 +00:00
|
|
|
/* Execute VM. */
|
2019-03-06 02:57:50 +00:00
|
|
|
if (!g_needs_reload_vm_program || (g_cheat_vm->LoadProgram(g_cheat_entries, DmntCheatManager::MaxCheatCount))) {
|
|
|
|
/* Program: reloaded. */
|
|
|
|
g_needs_reload_vm_program = false;
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-06 02:57:50 +00:00
|
|
|
/* Execute program if it's present. */
|
2019-03-05 04:13:52 +00:00
|
|
|
if (g_cheat_vm->GetProgramSize() != 0) {
|
|
|
|
g_cheat_vm->Execute(&g_cheat_process_metadata);
|
|
|
|
}
|
2019-02-28 02:33:07 +00:00
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-05 06:13:43 +00:00
|
|
|
/* Apply frozen addresses. */
|
|
|
|
for (auto const& [address, value] : g_frozen_addresses_map) {
|
|
|
|
WriteCheatProcessMemoryForVm(address, &value.value, value.width);
|
|
|
|
}
|
2019-02-27 12:27:49 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-06 02:57:50 +00:00
|
|
|
constexpr u64 ONE_SECOND = 1000000000ul;
|
|
|
|
constexpr u64 NUM_TIMES = 12;
|
|
|
|
constexpr u64 DELAY = ONE_SECOND / NUM_TIMES;
|
|
|
|
svcSleepThread(DELAY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DmntCheatManager::DebugEventsThread(void *arg) {
|
2019-03-25 17:35:08 +00:00
|
|
|
|
|
|
|
while (R_SUCCEEDED(svcWaitSynchronizationSingle(g_cheat_process_debug_hnd, U64_MAX))) {
|
2019-03-06 07:58:36 +00:00
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-06 07:58:36 +00:00
|
|
|
/* Handle any pending debug events. */
|
|
|
|
if (HasActiveCheatProcess()) {
|
2019-03-15 10:30:51 +00:00
|
|
|
DmntCheatDebugEventsManager::ContinueCheatProcess(g_cheat_process_debug_hnd);
|
2019-03-06 02:57:50 +00:00
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 11:30:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 12:27:49 +00:00
|
|
|
bool DmntCheatManager::GetHasActiveCheatProcess() {
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 12:27:49 +00:00
|
|
|
return HasActiveCheatProcess();
|
|
|
|
}
|
|
|
|
|
2019-02-27 12:37:02 +00:00
|
|
|
Handle DmntCheatManager::GetCheatProcessEventHandle() {
|
|
|
|
return g_cheat_process_event->GetHandle();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result DmntCheatManager::GetCheatProcessMetadata(CheatProcessMetadata *out) {
|
|
|
|
std::scoped_lock<HosMutex> lk(g_cheat_lock);
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 12:37:02 +00:00
|
|
|
if (HasActiveCheatProcess()) {
|
|
|
|
*out = g_cheat_process_metadata;
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2019-02-27 12:37:02 +00:00
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-04 13:56:44 +00:00
|
|
|
return ResultDmntCheatNotAttached;
|
2019-02-27 12:37:02 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 11:30:08 +00:00
|
|
|
void DmntCheatManager::InitializeCheatManager() {
|
2019-02-27 12:37:02 +00:00
|
|
|
/* Create cheat process detection event. */
|
|
|
|
g_cheat_process_event = CreateWriteOnlySystemEvent();
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-28 02:33:07 +00:00
|
|
|
/* Create cheat vm. */
|
|
|
|
g_cheat_vm = new DmntCheatVm();
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-06 08:20:17 +00:00
|
|
|
/* Learn whether we should enable cheats by default. */
|
|
|
|
{
|
|
|
|
u8 en;
|
|
|
|
if (R_SUCCEEDED(setsysGetSettingsItemValue("atmosphere", "dmnt_cheats_enabled_by_default", &en, sizeof(en)))) {
|
|
|
|
g_enable_cheats_by_default = (en != 0);
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
|
|
|
if (R_SUCCEEDED(setsysGetSettingsItemValue("atmosphere", "dmnt_always_save_cheat_toggles", &en, sizeof(en)))) {
|
|
|
|
g_always_save_cheat_toggles = (en != 0);
|
|
|
|
}
|
2019-03-06 08:20:17 +00:00
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-15 10:30:51 +00:00
|
|
|
/* Initialize debug events manager. */
|
|
|
|
DmntCheatDebugEventsManager::Initialize();
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 11:30:08 +00:00
|
|
|
/* Spawn application detection thread, spawn cheat vm thread. */
|
2019-03-06 02:57:50 +00:00
|
|
|
if (R_FAILED(g_detect_thread.Initialize(&DmntCheatManager::DetectThread, nullptr, 0x4000, 39))) {
|
2019-02-27 11:30:08 +00:00
|
|
|
std::abort();
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-03-06 02:57:50 +00:00
|
|
|
if (R_FAILED(g_vm_thread.Initialize(&DmntCheatManager::VmThread, nullptr, 0x4000, 48))) {
|
|
|
|
std::abort();
|
|
|
|
}
|
2019-03-25 17:35:08 +00:00
|
|
|
|
2019-02-27 11:30:08 +00:00
|
|
|
/* Start threads. */
|
2019-03-06 07:58:36 +00:00
|
|
|
if (R_FAILED(g_detect_thread.Start()) || R_FAILED(g_vm_thread.Start())) {
|
2019-02-27 11:30:08 +00:00
|
|
|
std::abort();
|
|
|
|
}
|
|
|
|
}
|