2019-09-16 09:22:08 +01:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-09-16 09:22:08 +01: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 23:02:10 +01:00
|
|
|
#include <stratosphere.hpp>
|
2019-09-16 09:22:08 +01:00
|
|
|
#include "dmnt_cheat_api.hpp"
|
|
|
|
#include "dmnt_cheat_vm.hpp"
|
|
|
|
#include "dmnt_cheat_debug_events_manager.hpp"
|
|
|
|
|
2019-10-24 10:30:10 +01:00
|
|
|
namespace ams::dmnt::cheat::impl {
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/* Helper definitions. */
|
|
|
|
constexpr size_t MaxCheatCount = 0x80;
|
|
|
|
constexpr size_t MaxFrozenAddressCount = 0x80;
|
|
|
|
|
2021-01-19 02:43:36 +00:00
|
|
|
class FrozenAddressMapEntry : public util::IntrusiveRedBlackTreeBaseNode<FrozenAddressMapEntry> {
|
|
|
|
public:
|
2021-04-27 04:05:56 +01:00
|
|
|
using RedBlackKeyType = u64;
|
2021-01-19 02:43:36 +00:00
|
|
|
private:
|
|
|
|
u64 m_address;
|
|
|
|
FrozenAddressValue m_value;
|
|
|
|
public:
|
2021-10-23 23:25:20 +01:00
|
|
|
FrozenAddressMapEntry(u64 address, FrozenAddressValue value) : m_address(address), m_value(value) { /* ... */ }
|
2021-01-19 02:43:36 +00:00
|
|
|
|
|
|
|
constexpr u64 GetAddress() const { return m_address; }
|
|
|
|
|
|
|
|
constexpr const FrozenAddressValue &GetValue() const { return m_value; }
|
|
|
|
constexpr FrozenAddressValue &GetValue() { return m_value; }
|
|
|
|
|
2021-04-27 04:05:56 +01:00
|
|
|
static constexpr ALWAYS_INLINE int Compare(const RedBlackKeyType &lval, const FrozenAddressMapEntry &rhs) {
|
2021-01-19 02:43:36 +00:00
|
|
|
const auto rval = rhs.GetAddress();
|
|
|
|
|
|
|
|
if (lval < rval) {
|
|
|
|
return -1;
|
|
|
|
} else if (lval == rval) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr ALWAYS_INLINE int Compare(const FrozenAddressMapEntry &lhs, const FrozenAddressMapEntry &rhs) {
|
|
|
|
return Compare(lhs.GetAddress(), rhs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
constinit os::SdkMutex g_text_file_buffer_lock;
|
|
|
|
constinit char g_text_file_buffer[64_KB];
|
|
|
|
|
|
|
|
constinit u8 g_frozen_address_map_memory[sizeof(FrozenAddressMapEntry) * MaxFrozenAddressCount];
|
|
|
|
constinit lmem::HeapHandle g_frozen_address_map_heap;
|
|
|
|
|
|
|
|
FrozenAddressMapEntry *AllocateFrozenAddress(u64 address, FrozenAddressValue value) {
|
|
|
|
FrozenAddressMapEntry *entry = static_cast<FrozenAddressMapEntry *>(lmem::AllocateFromUnitHeap(g_frozen_address_map_heap));
|
|
|
|
if (entry != nullptr) {
|
2021-03-22 03:30:40 +00:00
|
|
|
std::construct_at(entry, address, value);
|
2021-01-19 02:43:36 +00:00
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeallocateFrozenAddress(FrozenAddressMapEntry *entry) {
|
2021-03-22 03:30:40 +00:00
|
|
|
std::destroy_at(entry);
|
2021-01-19 02:43:36 +00:00
|
|
|
lmem::FreeToUnitHeap(g_frozen_address_map_heap, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
using FrozenAddressMap = typename util::IntrusiveRedBlackTreeBaseTraits<FrozenAddressMapEntry>::TreeType<FrozenAddressMapEntry>;
|
|
|
|
|
2019-09-16 09:22:08 +01:00
|
|
|
/* Manager class. */
|
|
|
|
class CheatProcessManager {
|
2019-10-19 04:31:15 +01:00
|
|
|
private:
|
|
|
|
static constexpr size_t ThreadStackSize = 0x4000;
|
2019-09-16 09:22:08 +01:00
|
|
|
private:
|
2021-10-10 08:14:06 +01:00
|
|
|
os::SdkMutex m_cheat_lock;
|
|
|
|
os::Event m_unsafe_break_event;
|
|
|
|
os::Event m_debug_events_event; /* Autoclear. */
|
|
|
|
os::ThreadType m_detect_thread, m_debug_events_thread;
|
|
|
|
os::SystemEvent m_cheat_process_event;
|
|
|
|
os::NativeHandle m_cheat_process_debug_handle = os::InvalidNativeHandle;
|
|
|
|
CheatProcessMetadata m_cheat_process_metadata = {};
|
|
|
|
|
|
|
|
os::ThreadType m_vm_thread;
|
|
|
|
bool m_broken_unsafe = false;
|
|
|
|
bool m_needs_reload_vm = false;
|
|
|
|
CheatVirtualMachine m_cheat_vm;
|
|
|
|
|
|
|
|
bool m_enable_cheats_by_default = true;
|
|
|
|
bool m_always_save_cheat_toggles = false;
|
|
|
|
bool m_should_save_cheat_toggles = false;
|
|
|
|
CheatEntry m_cheat_entries[MaxCheatCount] = {};
|
|
|
|
FrozenAddressMap m_frozen_addresses_map = {};
|
|
|
|
|
|
|
|
alignas(os::MemoryPageSize) u8 m_detect_thread_stack[ThreadStackSize] = {};
|
|
|
|
alignas(os::MemoryPageSize) u8 m_debug_events_thread_stack[ThreadStackSize] = {};
|
|
|
|
alignas(os::MemoryPageSize) u8 m_vm_thread_stack[ThreadStackSize] = {};
|
2019-09-16 09:22:08 +01:00
|
|
|
private:
|
|
|
|
static void DetectLaunchThread(void *_this);
|
|
|
|
static void VirtualMachineThread(void *_this);
|
|
|
|
static void DebugEventsThread(void *_this);
|
|
|
|
|
|
|
|
Result AttachToApplicationProcess(bool on_process_launch);
|
|
|
|
|
|
|
|
bool ParseCheats(const char *s, size_t len);
|
2021-10-11 02:22:32 +01:00
|
|
|
bool LoadCheats(const ncm::ProgramId program_id, const u8 *module_id);
|
2019-09-16 09:22:08 +01:00
|
|
|
bool ParseCheatToggles(const char *s, size_t len);
|
2019-10-28 04:43:01 +00:00
|
|
|
bool LoadCheatToggles(const ncm::ProgramId program_id);
|
|
|
|
void SaveCheatToggles(const ncm::ProgramId program_id);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
bool GetNeedsReloadVm() const {
|
2021-10-10 08:14:06 +01:00
|
|
|
return m_needs_reload_vm;
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetNeedsReloadVm(bool reload) {
|
2021-10-10 08:14:06 +01:00
|
|
|
m_needs_reload_vm = reload;
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ResetCheatEntry(size_t i) {
|
|
|
|
if (i < MaxCheatCount) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::memset(m_cheat_entries + i, 0, sizeof(m_cheat_entries[i]));
|
|
|
|
m_cheat_entries[i].cheat_id = i;
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
this->SetNeedsReloadVm(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResetAllCheatEntries() {
|
|
|
|
for (size_t i = 0; i < MaxCheatCount; i++) {
|
|
|
|
this->ResetCheatEntry(i);
|
|
|
|
}
|
2020-04-25 01:00:43 +01:00
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
m_cheat_vm.ResetStaticRegisters();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CheatEntry *GetCheatEntryById(size_t i) {
|
|
|
|
if (i < MaxCheatCount) {
|
2021-10-10 08:14:06 +01:00
|
|
|
return m_cheat_entries + i;
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheatEntry *GetCheatEntryByReadableName(const char *readable_name) {
|
|
|
|
/* Check all non-master cheats for match. */
|
|
|
|
for (size_t i = 1; i < MaxCheatCount; i++) {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (std::strncmp(m_cheat_entries[i].definition.readable_name, readable_name, sizeof(m_cheat_entries[i].definition.readable_name)) == 0) {
|
|
|
|
return m_cheat_entries + i;
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheatEntry *GetFreeCheatEntry() {
|
|
|
|
/* Check all non-master cheats for availability. */
|
|
|
|
for (size_t i = 1; i < MaxCheatCount; i++) {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (m_cheat_entries[i].definition.num_opcodes == 0) {
|
|
|
|
return m_cheat_entries + i;
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloseActiveCheatProcess() {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (m_cheat_process_debug_handle != os::InvalidNativeHandle) {
|
2020-04-25 01:00:43 +01:00
|
|
|
/* We don't need to do any unsafe brekaing. */
|
2021-10-10 08:14:06 +01:00
|
|
|
m_broken_unsafe = false;
|
|
|
|
m_unsafe_break_event.Signal();
|
2020-04-25 01:00:43 +01:00
|
|
|
|
2019-09-16 10:14:23 +01:00
|
|
|
/* Knock out the debug events thread. */
|
2021-10-10 08:14:06 +01:00
|
|
|
os::CancelThreadSynchronization(std::addressof(m_debug_events_thread));
|
2019-09-16 10:14:23 +01:00
|
|
|
|
2019-09-16 09:22:08 +01:00
|
|
|
/* Close resources. */
|
2021-10-10 08:14:06 +01:00
|
|
|
R_ABORT_UNLESS(svc::CloseHandle(m_cheat_process_debug_handle));
|
|
|
|
m_cheat_process_debug_handle = os::InvalidNativeHandle;
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
/* Save cheat toggles. */
|
2021-10-10 08:14:06 +01:00
|
|
|
if (m_always_save_cheat_toggles || m_should_save_cheat_toggles) {
|
|
|
|
this->SaveCheatToggles(m_cheat_process_metadata.program_id);
|
|
|
|
m_should_save_cheat_toggles = false;
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear metadata. */
|
2021-10-10 08:14:06 +01:00
|
|
|
static_assert(util::is_pod<decltype(m_cheat_process_metadata)>::value, "CheatProcessMetadata definition!");
|
|
|
|
std::memset(std::addressof(m_cheat_process_metadata), 0, sizeof(m_cheat_process_metadata));
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
/* Clear cheat list. */
|
|
|
|
this->ResetAllCheatEntries();
|
|
|
|
|
|
|
|
/* Clear frozen addresses. */
|
2021-01-19 02:43:36 +00:00
|
|
|
{
|
2021-10-10 08:14:06 +01:00
|
|
|
auto it = m_frozen_addresses_map.begin();
|
|
|
|
while (it != m_frozen_addresses_map.end()) {
|
2021-01-19 02:43:36 +00:00
|
|
|
FrozenAddressMapEntry *entry = std::addressof(*it);
|
2021-10-10 08:14:06 +01:00
|
|
|
it = m_frozen_addresses_map.erase(it);
|
2021-01-19 02:43:36 +00:00
|
|
|
DeallocateFrozenAddress(entry);
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
/* Signal to our fans. */
|
2021-10-10 08:14:06 +01:00
|
|
|
m_cheat_process_event.Signal();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasActiveCheatProcess() {
|
|
|
|
/* Note: This function *MUST* be called only with the cheat lock held. */
|
2019-10-18 05:18:27 +01:00
|
|
|
os::ProcessId pid;
|
2021-10-10 08:14:06 +01:00
|
|
|
bool has_cheat_process = m_cheat_process_debug_handle != os::InvalidNativeHandle;
|
|
|
|
has_cheat_process &= R_SUCCEEDED(os::GetProcessId(std::addressof(pid), m_cheat_process_debug_handle));
|
2021-10-09 22:49:53 +01:00
|
|
|
has_cheat_process &= R_SUCCEEDED(pm::dmnt::GetApplicationProcessId(std::addressof(pid)));
|
2021-10-10 08:14:06 +01:00
|
|
|
has_cheat_process &= (pid == m_cheat_process_metadata.process_id);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
if (!has_cheat_process) {
|
|
|
|
this->CloseActiveCheatProcess();
|
|
|
|
}
|
|
|
|
|
|
|
|
return has_cheat_process;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result EnsureCheatProcess() {
|
2021-10-09 18:36:21 +01:00
|
|
|
R_UNLESS(this->HasActiveCheatProcess(), dmnt::cheat::ResultCheatNotAttached());
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
2021-10-05 01:12:32 +01:00
|
|
|
os::NativeHandle GetCheatProcessHandle() const {
|
2021-10-10 08:14:06 +01:00
|
|
|
return m_cheat_process_debug_handle;
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
2021-10-05 01:12:32 +01:00
|
|
|
os::NativeHandle HookToCreateApplicationProcess() const {
|
|
|
|
os::NativeHandle h;
|
2021-10-09 22:49:53 +01:00
|
|
|
R_ABORT_UNLESS(pm::dmnt::HookToCreateApplicationProcess(std::addressof(h)));
|
2019-09-16 09:22:08 +01:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2019-10-18 05:18:27 +01:00
|
|
|
void StartProcess(os::ProcessId process_id) const {
|
2020-02-23 07:05:14 +00:00
|
|
|
R_ABORT_UNLESS(pm::dmnt::StartProcess(process_id));
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2021-10-10 08:14:06 +01:00
|
|
|
CheatProcessManager() : m_cheat_lock(), m_unsafe_break_event(os::EventClearMode_ManualClear), m_debug_events_event(os::EventClearMode_AutoClear), m_cheat_process_event(os::EventClearMode_AutoClear, true) {
|
2019-09-16 09:22:08 +01:00
|
|
|
/* Learn whether we should enable cheats by default. */
|
|
|
|
{
|
2019-10-18 18:37:03 +01:00
|
|
|
u8 en = 0;
|
2021-10-09 22:49:53 +01:00
|
|
|
if (settings::fwdbg::GetSettingsItemValue(std::addressof(en), sizeof(en), "atmosphere", "dmnt_cheats_enabled_by_default") == sizeof(en)) {
|
2021-10-10 08:14:06 +01:00
|
|
|
m_enable_cheats_by_default = (en != 0);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
2019-10-18 18:37:03 +01:00
|
|
|
en = 0;
|
2021-10-09 22:49:53 +01:00
|
|
|
if (settings::fwdbg::GetSettingsItemValue( std::addressof(en), sizeof(en), "atmosphere", "dmnt_always_save_cheat_toggles") == sizeof(en)) {
|
2021-10-10 08:14:06 +01:00
|
|
|
m_always_save_cheat_toggles = (en != 0);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Spawn application detection thread, spawn cheat vm thread. */
|
2021-10-10 08:14:06 +01:00
|
|
|
R_ABORT_UNLESS(os::CreateThread(std::addressof(m_detect_thread), DetectLaunchThread, this, m_detect_thread_stack, ThreadStackSize, AMS_GET_SYSTEM_THREAD_PRIORITY(dmnt, CheatDetect)));
|
|
|
|
os::SetThreadNamePointer(std::addressof(m_detect_thread), AMS_GET_SYSTEM_THREAD_NAME(dmnt, CheatDetect));
|
|
|
|
R_ABORT_UNLESS(os::CreateThread(std::addressof(m_vm_thread), VirtualMachineThread, this, m_vm_thread_stack, ThreadStackSize, AMS_GET_SYSTEM_THREAD_PRIORITY(dmnt, CheatVirtualMachine)));
|
|
|
|
os::SetThreadNamePointer(std::addressof(m_vm_thread), AMS_GET_SYSTEM_THREAD_NAME(dmnt, CheatVirtualMachine));
|
|
|
|
R_ABORT_UNLESS(os::CreateThread(std::addressof(m_debug_events_thread), DebugEventsThread, this, m_debug_events_thread_stack, ThreadStackSize, AMS_GET_SYSTEM_THREAD_PRIORITY(dmnt, CheatDebugEvents)));
|
|
|
|
os::SetThreadNamePointer(std::addressof(m_debug_events_thread), AMS_GET_SYSTEM_THREAD_NAME(dmnt, CheatDebugEvents));
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
/* Start threads. */
|
2021-10-10 08:14:06 +01:00
|
|
|
os::StartThread(std::addressof(m_detect_thread));
|
|
|
|
os::StartThread(std::addressof(m_vm_thread));
|
|
|
|
os::StartThread(std::addressof(m_debug_events_thread));
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GetHasActiveCheatProcess() {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
return this->HasActiveCheatProcess();
|
|
|
|
}
|
|
|
|
|
2021-10-05 01:12:32 +01:00
|
|
|
os::NativeHandle GetCheatProcessEventHandle() const {
|
2021-10-10 08:14:06 +01:00
|
|
|
return m_cheat_process_event.GetReadableHandle();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetCheatProcessMetadata(CheatProcessMetadata *out) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
std::memcpy(out, std::addressof(m_cheat_process_metadata), sizeof(*out));
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ForceOpenCheatProcess() {
|
|
|
|
return this->AttachToApplicationProcess(false);
|
|
|
|
}
|
|
|
|
|
2021-07-22 03:21:58 +01:00
|
|
|
Result ForceCloseCheatProcess() {
|
|
|
|
this->CloseActiveCheatProcess();
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2019-09-16 09:22:08 +01:00
|
|
|
Result ReadCheatProcessMemoryUnsafe(u64 proc_addr, void *out_data, size_t size) {
|
2021-10-04 22:54:13 +01:00
|
|
|
return svc::ReadDebugProcessMemory(reinterpret_cast<uintptr_t>(out_data), this->GetCheatProcessHandle(), proc_addr, size);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result WriteCheatProcessMemoryUnsafe(u64 proc_addr, const void *data, size_t size) {
|
2021-10-04 22:54:13 +01:00
|
|
|
R_TRY(svc::WriteDebugProcessMemory(this->GetCheatProcessHandle(), reinterpret_cast<uintptr_t>(data), proc_addr, size));
|
2019-09-16 09:22:08 +01:00
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
for (auto &entry : m_frozen_addresses_map) {
|
2021-01-19 02:43:36 +00:00
|
|
|
/* Get address/value. */
|
|
|
|
const u64 address = entry.GetAddress();
|
|
|
|
auto &value = entry.GetValue();
|
|
|
|
|
2019-09-16 09:22:08 +01:00
|
|
|
/* Map is ordered, so break when we can. */
|
|
|
|
if (address >= proc_addr + size) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we need to write. */
|
|
|
|
if (proc_addr <= address && address < proc_addr + size) {
|
|
|
|
const size_t offset = (address - proc_addr);
|
|
|
|
const size_t copy_size = std::min(sizeof(value.value), size - offset);
|
2021-10-09 22:49:53 +01:00
|
|
|
std::memcpy(std::addressof(value.value), reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(data) + offset), copy_size);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
2020-04-25 01:24:15 +01:00
|
|
|
Result PauseCheatProcessUnsafe() {
|
2021-10-10 08:14:06 +01:00
|
|
|
m_broken_unsafe = true;
|
|
|
|
m_unsafe_break_event.Clear();
|
2021-10-04 22:54:13 +01:00
|
|
|
return svc::BreakDebugProcess(this->GetCheatProcessHandle());
|
2020-04-25 01:00:43 +01:00
|
|
|
}
|
|
|
|
|
2020-04-25 01:24:15 +01:00
|
|
|
Result ResumeCheatProcessUnsafe() {
|
2021-10-10 08:14:06 +01:00
|
|
|
m_broken_unsafe = false;
|
|
|
|
m_unsafe_break_event.Signal();
|
2020-04-25 01:00:43 +01:00
|
|
|
dmnt::cheat::impl::ContinueCheatProcess(this->GetCheatProcessHandle());
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2019-09-16 09:22:08 +01:00
|
|
|
Result GetCheatProcessMappingCount(u64 *out_count) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
|
|
|
|
2021-10-04 22:54:13 +01:00
|
|
|
svc::MemoryInfo mem_info;
|
|
|
|
svc::PageInfo page_info;
|
2019-09-16 09:22:08 +01:00
|
|
|
u64 address = 0, count = 0;
|
|
|
|
do {
|
2021-10-09 22:49:53 +01:00
|
|
|
if (R_FAILED(svc::QueryDebugProcessMemory(std::addressof(mem_info), std::addressof(page_info), this->GetCheatProcessHandle(), address))) {
|
2019-09-16 09:22:08 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-10-05 23:16:54 +01:00
|
|
|
if (mem_info.permission != svc::MemoryPermission_None) {
|
2019-09-16 09:22:08 +01:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
2021-10-05 23:16:54 +01:00
|
|
|
address = mem_info.base_address + mem_info.size;
|
2019-09-16 09:22:08 +01:00
|
|
|
} while (address != 0);
|
|
|
|
|
|
|
|
*out_count = count;
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
2021-10-04 22:54:13 +01:00
|
|
|
Result GetCheatProcessMappings(svc::MemoryInfo *mappings, size_t max_count, u64 *out_count, u64 offset) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
|
|
|
|
2021-10-04 22:54:13 +01:00
|
|
|
svc::MemoryInfo mem_info;
|
|
|
|
svc::PageInfo page_info;
|
2019-09-16 09:22:08 +01:00
|
|
|
u64 address = 0, total_count = 0, written_count = 0;
|
|
|
|
do {
|
2021-10-09 22:49:53 +01:00
|
|
|
if (R_FAILED(svc::QueryDebugProcessMemory(std::addressof(mem_info), std::addressof(page_info), this->GetCheatProcessHandle(), address))) {
|
2019-09-16 09:22:08 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-10-05 23:16:54 +01:00
|
|
|
if (mem_info.permission != svc::MemoryPermission_None) {
|
2019-09-16 09:22:08 +01:00
|
|
|
if (offset <= total_count && written_count < max_count) {
|
|
|
|
mappings[written_count++] = mem_info;
|
|
|
|
}
|
|
|
|
total_count++;
|
|
|
|
}
|
|
|
|
|
2021-10-05 23:16:54 +01:00
|
|
|
address = mem_info.base_address + mem_info.size;
|
2019-09-16 09:22:08 +01:00
|
|
|
} while (address != 0 && written_count < max_count);
|
|
|
|
|
|
|
|
*out_count = written_count;
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ReadCheatProcessMemory(u64 proc_addr, void *out_data, size_t size) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
|
|
|
|
|
|
|
return this->ReadCheatProcessMemoryUnsafe(proc_addr, out_data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result WriteCheatProcessMemory(u64 proc_addr, const void *data, size_t size) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
|
|
|
|
|
|
|
return this->WriteCheatProcessMemoryUnsafe(proc_addr, data, size);
|
|
|
|
}
|
|
|
|
|
2021-10-04 22:54:13 +01:00
|
|
|
Result QueryCheatProcessMemory(svc::MemoryInfo *mapping, u64 address) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
|
|
|
|
2021-10-04 22:54:13 +01:00
|
|
|
svc::PageInfo page_info;
|
2021-10-09 22:49:53 +01:00
|
|
|
return svc::QueryDebugProcessMemory(mapping, std::addressof(page_info), this->GetCheatProcessHandle(), address);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
2020-04-25 01:24:15 +01:00
|
|
|
Result PauseCheatProcess() {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2020-04-25 01:00:43 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
|
|
|
|
2020-04-25 01:24:15 +01:00
|
|
|
return this->PauseCheatProcessUnsafe();
|
2020-04-25 01:00:43 +01:00
|
|
|
}
|
|
|
|
|
2020-04-25 01:24:15 +01:00
|
|
|
Result ResumeCheatProcess() {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2020-04-25 01:00:43 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
|
|
|
|
2020-04-25 01:24:15 +01:00
|
|
|
return this->ResumeCheatProcessUnsafe();
|
2020-04-25 01:00:43 +01:00
|
|
|
}
|
|
|
|
|
2019-09-16 09:22:08 +01:00
|
|
|
Result GetCheatCount(u64 *out_count) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
|
|
|
|
|
|
|
size_t count = 0;
|
|
|
|
for (size_t i = 0; i < MaxCheatCount; i++) {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (m_cheat_entries[i].definition.num_opcodes) {
|
2019-09-16 09:22:08 +01:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_count = count;
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetCheats(CheatEntry *out_cheats, size_t max_count, u64 *out_count, u64 offset) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
|
|
|
|
|
|
|
size_t count = 0, total_count = 0;
|
|
|
|
for (size_t i = 0; i < MaxCheatCount && count < max_count; i++) {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (m_cheat_entries[i].definition.num_opcodes) {
|
2019-09-16 09:22:08 +01:00
|
|
|
total_count++;
|
|
|
|
if (total_count > offset) {
|
2021-10-10 08:14:06 +01:00
|
|
|
out_cheats[count++] = m_cheat_entries[i];
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_count = count;
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetCheatById(CheatEntry *out_cheat, u32 cheat_id) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
|
|
|
|
|
|
|
const CheatEntry *entry = this->GetCheatEntryById(cheat_id);
|
2021-10-09 18:36:21 +01:00
|
|
|
R_UNLESS(entry != nullptr, dmnt::cheat::ResultCheatUnknownId());
|
|
|
|
R_UNLESS(entry->definition.num_opcodes != 0, dmnt::cheat::ResultCheatUnknownId());
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
*out_cheat = *entry;
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ToggleCheat(u32 cheat_id) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
|
|
|
|
|
|
|
CheatEntry *entry = this->GetCheatEntryById(cheat_id);
|
2021-10-09 18:36:21 +01:00
|
|
|
R_UNLESS(entry != nullptr, dmnt::cheat::ResultCheatUnknownId());
|
|
|
|
R_UNLESS(entry->definition.num_opcodes != 0, dmnt::cheat::ResultCheatUnknownId());
|
2019-09-16 09:22:08 +01:00
|
|
|
|
2021-10-09 18:36:21 +01:00
|
|
|
R_UNLESS(cheat_id != 0, dmnt::cheat::ResultCheatCannotDisable());
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
entry->enabled = !entry->enabled;
|
|
|
|
|
|
|
|
/* Trigger a VM reload. */
|
|
|
|
this->SetNeedsReloadVm(true);
|
|
|
|
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
2019-10-18 05:18:27 +01:00
|
|
|
Result AddCheat(u32 *out_id, const CheatDefinition &def, bool enabled) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
|
|
|
|
2021-10-09 18:36:21 +01:00
|
|
|
R_UNLESS(def.num_opcodes != 0, dmnt::cheat::ResultCheatInvalid());
|
|
|
|
R_UNLESS(def.num_opcodes <= util::size(def.opcodes), dmnt::cheat::ResultCheatInvalid());
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
CheatEntry *new_entry = this->GetFreeCheatEntry();
|
2021-10-09 18:36:21 +01:00
|
|
|
R_UNLESS(new_entry != nullptr, dmnt::cheat::ResultCheatOutOfResource());
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
new_entry->enabled = enabled;
|
2019-10-18 05:18:27 +01:00
|
|
|
new_entry->definition = def;
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
/* Trigger a VM reload. */
|
|
|
|
this->SetNeedsReloadVm(true);
|
|
|
|
|
2021-07-22 03:21:58 +01:00
|
|
|
/* Set output id. */
|
|
|
|
*out_id = new_entry->cheat_id;
|
|
|
|
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result RemoveCheat(u32 cheat_id) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
2021-10-09 18:36:21 +01:00
|
|
|
R_UNLESS(cheat_id < MaxCheatCount, dmnt::cheat::ResultCheatUnknownId());
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
this->ResetCheatEntry(cheat_id);
|
|
|
|
|
|
|
|
/* Trigger a VM reload. */
|
|
|
|
this->SetNeedsReloadVm(true);
|
|
|
|
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
2021-07-22 03:21:58 +01:00
|
|
|
Result SetMasterCheat(const CheatDefinition &def) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2021-07-22 03:21:58 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
|
|
|
|
2021-10-09 18:36:21 +01:00
|
|
|
R_UNLESS(def.num_opcodes != 0, dmnt::cheat::ResultCheatInvalid());
|
|
|
|
R_UNLESS(def.num_opcodes <= util::size(def.opcodes), dmnt::cheat::ResultCheatInvalid());
|
2021-07-22 03:21:58 +01:00
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
CheatEntry *master_entry = m_cheat_entries + 0;
|
2021-07-22 03:21:58 +01:00
|
|
|
|
|
|
|
master_entry->enabled = true;
|
|
|
|
master_entry->definition = def;
|
|
|
|
|
|
|
|
/* Trigger a VM reload. */
|
|
|
|
this->SetNeedsReloadVm(true);
|
|
|
|
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-04-25 01:00:43 +01:00
|
|
|
Result ReadStaticRegister(u64 *out, size_t which) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2020-04-25 01:00:43 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
2021-10-09 18:36:21 +01:00
|
|
|
R_UNLESS(which < CheatVirtualMachine::NumStaticRegisters, dmnt::cheat::ResultCheatInvalid());
|
2020-04-25 01:00:43 +01:00
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
*out = m_cheat_vm.GetStaticRegister(which);
|
2020-04-25 01:00:43 +01:00
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result WriteStaticRegister(size_t which, u64 value) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2020-04-25 01:00:43 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
2021-10-09 18:36:21 +01:00
|
|
|
R_UNLESS(which < CheatVirtualMachine::NumStaticRegisters, dmnt::cheat::ResultCheatInvalid());
|
2020-04-25 01:00:43 +01:00
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
m_cheat_vm.SetStaticRegister(which, value);
|
2020-04-25 01:00:43 +01:00
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result ResetStaticRegisters() {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2020-04-25 01:00:43 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
m_cheat_vm.ResetStaticRegisters();
|
2020-04-25 01:00:43 +01:00
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2019-09-16 09:22:08 +01:00
|
|
|
Result GetFrozenAddressCount(u64 *out_count) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
*out_count = std::distance(m_frozen_addresses_map.begin(), m_frozen_addresses_map.end());
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetFrozenAddresses(FrozenAddressEntry *frz_addrs, size_t max_count, u64 *out_count, u64 offset) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
|
|
|
|
|
|
|
u64 total_count = 0, written_count = 0;
|
2021-10-10 08:14:06 +01:00
|
|
|
for (const auto &entry : m_frozen_addresses_map) {
|
2019-09-16 09:22:08 +01:00
|
|
|
if (written_count >= max_count) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset <= total_count) {
|
2021-01-19 02:43:36 +00:00
|
|
|
frz_addrs[written_count].address = entry.GetAddress();
|
|
|
|
frz_addrs[written_count].value = entry.GetValue();
|
2019-09-16 09:22:08 +01:00
|
|
|
written_count++;
|
|
|
|
}
|
|
|
|
total_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_count = written_count;
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetFrozenAddress(FrozenAddressEntry *frz_addr, u64 address) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
const auto it = m_frozen_addresses_map.find_key(address);
|
|
|
|
R_UNLESS(it != m_frozen_addresses_map.end(), dmnt::cheat::ResultFrozenAddressNotFound());
|
2019-09-16 09:22:08 +01:00
|
|
|
|
2021-01-19 02:43:36 +00:00
|
|
|
frz_addr->address = it->GetAddress();
|
|
|
|
frz_addr->value = it->GetValue();
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result EnableFrozenAddress(u64 *out_value, u64 address, u64 width) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
const auto it = m_frozen_addresses_map.find_key(address);
|
|
|
|
R_UNLESS(it == m_frozen_addresses_map.end(), dmnt::cheat::ResultFrozenAddressAlreadyExists());
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
FrozenAddressValue value = {};
|
|
|
|
value.width = width;
|
2021-10-09 22:49:53 +01:00
|
|
|
R_TRY(this->ReadCheatProcessMemoryUnsafe(address, std::addressof(value.value), width));
|
2019-09-16 09:22:08 +01:00
|
|
|
|
2021-01-19 02:43:36 +00:00
|
|
|
FrozenAddressMapEntry *entry = AllocateFrozenAddress(address, value);
|
2021-10-09 18:36:21 +01:00
|
|
|
R_UNLESS(entry != nullptr, dmnt::cheat::ResultFrozenAddressOutOfResource());
|
2021-01-19 02:43:36 +00:00
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
m_frozen_addresses_map.insert(*entry);
|
2019-09-16 09:22:08 +01:00
|
|
|
*out_value = value.value;
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result DisableFrozenAddress(u64 address) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
R_TRY(this->EnsureCheatProcess());
|
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
const auto it = m_frozen_addresses_map.find_key(address);
|
|
|
|
R_UNLESS(it != m_frozen_addresses_map.end(), dmnt::cheat::ResultFrozenAddressNotFound());
|
2019-09-16 09:22:08 +01:00
|
|
|
|
2021-01-19 02:43:36 +00:00
|
|
|
FrozenAddressMapEntry *entry = std::addressof(*it);
|
2021-10-10 08:14:06 +01:00
|
|
|
m_frozen_addresses_map.erase(it);
|
2021-01-19 02:43:36 +00:00
|
|
|
DeallocateFrozenAddress(entry);
|
|
|
|
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
void CheatProcessManager::DetectLaunchThread(void *_this) {
|
2021-10-10 08:14:06 +01:00
|
|
|
CheatProcessManager *manager = reinterpret_cast<CheatProcessManager *>(_this);
|
2019-09-16 09:22:08 +01:00
|
|
|
Event hook;
|
|
|
|
while (true) {
|
2021-10-10 08:14:06 +01:00
|
|
|
eventLoadRemote(std::addressof(hook), manager->HookToCreateApplicationProcess(), true);
|
2021-10-09 22:49:53 +01:00
|
|
|
if (R_SUCCEEDED(eventWait(std::addressof(hook), std::numeric_limits<u64>::max()))) {
|
2021-10-10 08:14:06 +01:00
|
|
|
manager->AttachToApplicationProcess(true);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
2021-10-09 22:49:53 +01:00
|
|
|
eventClose(std::addressof(hook));
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheatProcessManager::DebugEventsThread(void *_this) {
|
2021-10-10 08:14:06 +01:00
|
|
|
CheatProcessManager *manager = reinterpret_cast<CheatProcessManager *>(_this);
|
2019-09-16 09:22:08 +01:00
|
|
|
while (true) {
|
|
|
|
/* Atomically wait (and clear) signal for new process. */
|
2021-10-10 08:14:06 +01:00
|
|
|
manager->m_debug_events_event.Wait();
|
2019-09-17 06:47:00 +01:00
|
|
|
while (true) {
|
2021-10-10 08:14:06 +01:00
|
|
|
os::NativeHandle cheat_process_handle = manager->GetCheatProcessHandle();
|
2021-10-04 22:54:13 +01:00
|
|
|
s32 dummy;
|
|
|
|
while (cheat_process_handle != os::InvalidNativeHandle && R_SUCCEEDED(svc::WaitSynchronization(std::addressof(dummy), std::addressof(cheat_process_handle), 1, std::numeric_limits<u64>::max()))) {
|
2021-10-10 08:14:06 +01:00
|
|
|
manager->m_cheat_lock.Lock();
|
|
|
|
ON_SCOPE_EXIT { manager->m_cheat_lock.Unlock(); };
|
2020-05-16 23:05:36 +01:00
|
|
|
{
|
2021-10-10 08:14:06 +01:00
|
|
|
ON_SCOPE_EXIT { cheat_process_handle = manager->GetCheatProcessHandle(); };
|
2020-05-16 23:05:36 +01:00
|
|
|
|
|
|
|
/* If we did an unsafe break, wait until we're not broken. */
|
2021-10-10 08:14:06 +01:00
|
|
|
if (manager->m_broken_unsafe) {
|
|
|
|
manager->m_cheat_lock.Unlock();
|
|
|
|
manager->m_unsafe_break_event.Wait();
|
|
|
|
manager->m_cheat_lock.Lock();
|
|
|
|
if (manager->GetCheatProcessHandle() != os::InvalidNativeHandle) {
|
2020-05-16 23:05:36 +01:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2020-04-25 01:00:43 +01:00
|
|
|
}
|
2019-09-16 09:22:08 +01:00
|
|
|
|
2020-05-16 23:05:36 +01:00
|
|
|
/* Handle any pending debug events. */
|
2021-10-10 08:14:06 +01:00
|
|
|
if (manager->HasActiveCheatProcess()) {
|
|
|
|
R_TRY_CATCH(dmnt::cheat::impl::ContinueCheatProcess(manager->GetCheatProcessHandle())) {
|
2020-05-16 23:05:36 +01:00
|
|
|
R_CATCH(svc::ResultProcessTerminated) {
|
2021-10-10 08:14:06 +01:00
|
|
|
manager->CloseActiveCheatProcess();
|
2020-05-16 23:05:36 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} R_END_TRY_CATCH_WITH_ABORT_UNLESS;
|
|
|
|
}
|
2019-09-17 06:47:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* WaitSynchronization failed. This means someone canceled our synchronization, possibly us. */
|
|
|
|
/* Let's check if we should quit! */
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(manager->m_cheat_lock);
|
|
|
|
if (!manager->HasActiveCheatProcess()) {
|
2019-09-17 06:47:00 +01:00
|
|
|
break;
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheatProcessManager::VirtualMachineThread(void *_this) {
|
2021-10-10 08:14:06 +01:00
|
|
|
CheatProcessManager *manager = reinterpret_cast<CheatProcessManager *>(_this);
|
2019-09-16 09:22:08 +01:00
|
|
|
while (true) {
|
|
|
|
/* Apply cheats. */
|
|
|
|
{
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(manager->m_cheat_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
if (manager->HasActiveCheatProcess()) {
|
2019-09-16 09:22:08 +01:00
|
|
|
/* Execute VM. */
|
2021-10-10 08:14:06 +01:00
|
|
|
if (!manager->GetNeedsReloadVm() || manager->m_cheat_vm.LoadProgram(manager->m_cheat_entries, util::size(manager->m_cheat_entries))) {
|
|
|
|
manager->SetNeedsReloadVm(false);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
/* Execute program only if it has opcodes. */
|
2021-10-10 08:14:06 +01:00
|
|
|
if (manager->m_cheat_vm.GetProgramSize()) {
|
|
|
|
manager->m_cheat_vm.Execute(std::addressof(manager->m_cheat_process_metadata));
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Apply frozen addresses. */
|
2021-10-10 08:14:06 +01:00
|
|
|
for (const auto &entry : manager->m_frozen_addresses_map) {
|
2021-01-19 02:43:36 +00:00
|
|
|
const auto address = entry.GetAddress();
|
|
|
|
const auto &value = entry.GetValue();
|
|
|
|
|
2019-09-16 09:22:08 +01:00
|
|
|
/* Use Write SVC directly, to avoid the usual frozen address update logic. */
|
2021-10-10 08:14:06 +01:00
|
|
|
svc::WriteDebugProcessMemory(manager->GetCheatProcessHandle(), reinterpret_cast<uintptr_t>(std::addressof(value.value)), address, value.width);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sleep until next potential execution. */
|
2021-10-04 22:54:13 +01:00
|
|
|
constexpr s64 TimesPerSecond = 12;
|
|
|
|
constexpr s64 DelayNanoSeconds = TimeSpan::FromSeconds(1).GetNanoSeconds() / TimesPerSecond;
|
|
|
|
constexpr TimeSpan Delay = TimeSpan::FromNanoSeconds(DelayNanoSeconds);
|
|
|
|
os::SleepThread(Delay);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 07:05:14 +00:00
|
|
|
#define R_ABORT_UNLESS_IF_NEW_PROCESS(res) \
|
2019-09-16 09:22:08 +01:00
|
|
|
if (on_process_launch) { \
|
2020-02-23 07:05:14 +00:00
|
|
|
R_ABORT_UNLESS(res); \
|
2019-09-16 09:22:08 +01:00
|
|
|
} else { \
|
|
|
|
R_TRY(res); \
|
|
|
|
}
|
|
|
|
|
|
|
|
Result CheatProcessManager::AttachToApplicationProcess(bool on_process_launch) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::scoped_lock lk(m_cheat_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
/* Close the active process, if needed. */
|
|
|
|
{
|
|
|
|
if (this->HasActiveCheatProcess()) {
|
|
|
|
/* When forcing attach, we're done. */
|
2020-03-08 08:06:23 +00:00
|
|
|
R_SUCCEED_IF(!on_process_launch);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Detach from the current process, if it's open. */
|
|
|
|
this->CloseActiveCheatProcess();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the application process's ID. */
|
2021-10-10 08:14:06 +01:00
|
|
|
R_ABORT_UNLESS_IF_NEW_PROCESS(pm::dmnt::GetApplicationProcessId(std::addressof(m_cheat_process_metadata.process_id)));
|
2019-09-16 09:22:08 +01:00
|
|
|
auto proc_guard = SCOPE_GUARD {
|
|
|
|
if (on_process_launch) {
|
2021-10-10 08:14:06 +01:00
|
|
|
this->StartProcess(m_cheat_process_metadata.process_id);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
m_cheat_process_metadata.process_id = os::ProcessId{};
|
2019-09-16 09:22:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Get process handle, use it to learn memory extents. */
|
|
|
|
{
|
2021-10-04 22:54:13 +01:00
|
|
|
os::NativeHandle proc_h = os::InvalidNativeHandle;
|
2019-10-28 04:43:01 +00:00
|
|
|
ncm::ProgramLocation loc = {};
|
2019-11-21 12:03:19 +00:00
|
|
|
cfg::OverrideStatus status = {};
|
2019-09-16 09:22:08 +01:00
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
R_ABORT_UNLESS_IF_NEW_PROCESS(pm::dmnt::AtmosphereGetProcessInfo(std::addressof(proc_h), std::addressof(loc), std::addressof(status), m_cheat_process_metadata.process_id));
|
2021-10-05 20:34:09 +01:00
|
|
|
ON_SCOPE_EXIT { os::CloseNativeHandle(proc_h); };
|
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
m_cheat_process_metadata.program_id = loc.program_id;
|
2019-09-16 09:22:08 +01:00
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
R_ABORT_UNLESS(svc::GetInfo(std::addressof(m_cheat_process_metadata.heap_extents.base), svc::InfoType_HeapRegionAddress, proc_h, 0));
|
|
|
|
R_ABORT_UNLESS(svc::GetInfo(std::addressof(m_cheat_process_metadata.heap_extents.size), svc::InfoType_HeapRegionSize, proc_h, 0));
|
|
|
|
R_ABORT_UNLESS(svc::GetInfo(std::addressof(m_cheat_process_metadata.alias_extents.base), svc::InfoType_AliasRegionAddress, proc_h, 0));
|
|
|
|
R_ABORT_UNLESS(svc::GetInfo(std::addressof(m_cheat_process_metadata.alias_extents.size), svc::InfoType_AliasRegionSize, proc_h, 0));
|
|
|
|
R_ABORT_UNLESS(svc::GetInfo(std::addressof(m_cheat_process_metadata.aslr_extents.base), svc::InfoType_AslrRegionAddress, proc_h, 0));
|
|
|
|
R_ABORT_UNLESS(svc::GetInfo(std::addressof(m_cheat_process_metadata.aslr_extents.size), svc::InfoType_AslrRegionSize, proc_h, 0));
|
2019-09-16 09:22:08 +01:00
|
|
|
|
2019-11-21 12:03:19 +00:00
|
|
|
/* If new process launch, we may not want to actually attach. */
|
|
|
|
if (on_process_launch) {
|
2021-10-09 18:36:21 +01:00
|
|
|
R_UNLESS(status.IsCheatEnabled(), dmnt::cheat::ResultCheatNotAttached());
|
2019-11-21 12:03:19 +00:00
|
|
|
}
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get module information from loader. */
|
|
|
|
{
|
2021-10-11 02:22:32 +01:00
|
|
|
ldr::ModuleInfo proc_modules[2];
|
2019-10-28 04:43:01 +00:00
|
|
|
s32 num_modules;
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
/* TODO: ldr::dmnt:: */
|
2021-10-11 02:22:32 +01:00
|
|
|
R_ABORT_UNLESS_IF_NEW_PROCESS(ldrDmntGetProcessModuleInfo(static_cast<u64>(m_cheat_process_metadata.process_id), reinterpret_cast<LoaderModuleInfo *>(proc_modules), util::size(proc_modules), std::addressof(num_modules)));
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
/* All applications must have two modules. */
|
|
|
|
/* Only accept one (which means we're attaching to HBL) */
|
|
|
|
/* if we aren't auto-attaching. */
|
2021-10-11 02:22:32 +01:00
|
|
|
const ldr::ModuleInfo *proc_module = nullptr;
|
2019-09-16 09:22:08 +01:00
|
|
|
if (num_modules == 2) {
|
2021-10-09 20:45:31 +01:00
|
|
|
proc_module = std::addressof(proc_modules[1]);
|
2019-09-16 09:22:08 +01:00
|
|
|
} else if (num_modules == 1 && !on_process_launch) {
|
2021-10-09 20:45:31 +01:00
|
|
|
proc_module = std::addressof(proc_modules[0]);
|
2019-09-16 09:22:08 +01:00
|
|
|
} else {
|
2021-10-09 18:36:21 +01:00
|
|
|
return dmnt::cheat::ResultCheatNotAttached();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
2021-10-11 02:22:32 +01:00
|
|
|
m_cheat_process_metadata.main_nso_extents.base = proc_module->address;
|
2021-10-10 08:14:06 +01:00
|
|
|
m_cheat_process_metadata.main_nso_extents.size = proc_module->size;
|
2021-10-11 02:22:32 +01:00
|
|
|
std::memcpy(m_cheat_process_metadata.main_nso_module_id, proc_module->module_id, sizeof(m_cheat_process_metadata.main_nso_module_id));
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read cheats off the SD. */
|
2021-10-11 02:22:32 +01:00
|
|
|
if (!this->LoadCheats(m_cheat_process_metadata.program_id, m_cheat_process_metadata.main_nso_module_id) ||
|
2021-10-10 08:14:06 +01:00
|
|
|
!this->LoadCheatToggles(m_cheat_process_metadata.program_id)) {
|
2019-09-16 09:22:08 +01:00
|
|
|
/* If new process launch, require success. */
|
2021-10-09 18:36:21 +01:00
|
|
|
R_UNLESS(!on_process_launch, dmnt::cheat::ResultCheatNotAttached());
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Open a debug handle. */
|
2021-02-21 01:37:59 +00:00
|
|
|
svc::Handle debug_handle = svc::InvalidHandle;
|
2021-10-10 08:14:06 +01:00
|
|
|
R_ABORT_UNLESS_IF_NEW_PROCESS(svc::DebugActiveProcess(std::addressof(debug_handle), m_cheat_process_metadata.process_id.value));
|
2021-02-21 01:37:59 +00:00
|
|
|
|
|
|
|
/* Set our debug handle. */
|
2021-10-10 08:14:06 +01:00
|
|
|
m_cheat_process_debug_handle = debug_handle;
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
/* Cancel process guard. */
|
|
|
|
proc_guard.Cancel();
|
|
|
|
|
2020-04-25 01:00:43 +01:00
|
|
|
/* Reset broken state. */
|
2021-10-10 08:14:06 +01:00
|
|
|
m_broken_unsafe = false;
|
|
|
|
m_unsafe_break_event.Signal();
|
2020-04-25 01:00:43 +01:00
|
|
|
|
2019-09-16 09:22:08 +01:00
|
|
|
/* If new process, start the process. */
|
|
|
|
if (on_process_launch) {
|
2021-10-10 08:14:06 +01:00
|
|
|
this->StartProcess(m_cheat_process_metadata.process_id);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Signal to the debug events thread. */
|
2021-10-10 08:14:06 +01:00
|
|
|
m_debug_events_event.Signal();
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
/* Signal to our fans. */
|
2021-10-10 08:14:06 +01:00
|
|
|
m_cheat_process_event.Signal();
|
2019-09-16 09:22:08 +01:00
|
|
|
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
2020-02-23 07:05:14 +00:00
|
|
|
#undef R_ABORT_UNLESS_IF_NEW_PROCESS
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
bool CheatProcessManager::ParseCheats(const char *s, size_t len) {
|
|
|
|
/* Trigger a VM reload. */
|
|
|
|
this->SetNeedsReloadVm(true);
|
|
|
|
|
|
|
|
/* Parse the input string. */
|
|
|
|
size_t i = 0;
|
|
|
|
CheatEntry *cur_entry = nullptr;
|
|
|
|
while (i < len) {
|
|
|
|
if (std::isspace(static_cast<unsigned char>(s[i]))) {
|
|
|
|
/* Just ignore whitespace. */
|
|
|
|
i++;
|
|
|
|
} else if (s[i] == '[') {
|
|
|
|
/* Parse a readable cheat name. */
|
|
|
|
cur_entry = this->GetFreeCheatEntry();
|
|
|
|
if (cur_entry == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract name bounds. */
|
|
|
|
size_t j = i + 1;
|
|
|
|
while (s[j] != ']') {
|
|
|
|
j++;
|
2019-09-17 06:49:58 +01:00
|
|
|
if (j >= len) {
|
2019-09-16 09:22:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* s[i+1:j] is cheat name. */
|
2019-09-17 06:49:58 +01:00
|
|
|
const size_t cheat_name_len = std::min(j - i - 1, sizeof(cur_entry->definition.readable_name));
|
2021-10-09 20:45:31 +01:00
|
|
|
std::memcpy(cur_entry->definition.readable_name, s + (i + 1), cheat_name_len);
|
2019-09-16 09:22:08 +01:00
|
|
|
cur_entry->definition.readable_name[cheat_name_len] = 0;
|
|
|
|
|
|
|
|
/* Skip onwards. */
|
|
|
|
i = j + 1;
|
|
|
|
} else if (s[i] == '{') {
|
|
|
|
/* We're parsing a master cheat. */
|
2021-10-10 08:14:06 +01:00
|
|
|
cur_entry = std::addressof(m_cheat_entries[0]);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
/* There can only be one master cheat. */
|
|
|
|
if (cur_entry->definition.num_opcodes > 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract name bounds */
|
|
|
|
size_t j = i + 1;
|
|
|
|
while (s[j] != '}') {
|
|
|
|
j++;
|
2019-09-17 06:49:58 +01:00
|
|
|
if (j >= len) {
|
2019-09-16 09:22:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* s[i+1:j] is cheat name. */
|
2019-09-17 06:49:58 +01:00
|
|
|
const size_t cheat_name_len = std::min(j - i - 1, sizeof(cur_entry->definition.readable_name));
|
2021-10-09 20:45:31 +01:00
|
|
|
memcpy(cur_entry->definition.readable_name, s + (i + 1), cheat_name_len);
|
2019-09-16 09:22:08 +01:00
|
|
|
cur_entry->definition.readable_name[cheat_name_len] = 0;
|
|
|
|
|
|
|
|
/* Skip onwards. */
|
|
|
|
i = j + 1;
|
|
|
|
} else if (std::isxdigit(static_cast<unsigned char>(s[i]))) {
|
|
|
|
/* Make sure that we have a cheat open. */
|
|
|
|
if (cur_entry == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bounds check the opcode count. */
|
|
|
|
if (cur_entry->definition.num_opcodes >= util::size(cur_entry->definition.opcodes)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 || !std::isxdigit(static_cast<unsigned char>(s[i+j]))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the new opcode. */
|
|
|
|
char hex_str[9] = {0};
|
2021-10-09 20:45:31 +01:00
|
|
|
std::memcpy(hex_str, s + i, 8);
|
2019-09-16 09:22:08 +01:00
|
|
|
cur_entry->definition.opcodes[cur_entry->definition.num_opcodes++] = std::strtoul(hex_str, NULL, 16);
|
|
|
|
|
|
|
|
/* Skip onwards. */
|
|
|
|
i += 8;
|
|
|
|
} else {
|
|
|
|
/* Unexpected character encountered. */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Master cheat can't be disabled. */
|
2021-10-10 08:14:06 +01:00
|
|
|
if (m_cheat_entries[0].definition.num_opcodes > 0) {
|
|
|
|
m_cheat_entries[0].enabled = true;
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable all entries we parsed. */
|
|
|
|
for (size_t i = 1; i < MaxCheatCount; i++) {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (m_cheat_entries[i].definition.num_opcodes > 0) {
|
|
|
|
m_cheat_entries[i].enabled = m_enable_cheats_by_default;
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CheatProcessManager::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 (std::isspace(static_cast<unsigned char>(s[i]))) {
|
|
|
|
/* Just ignore whitespace. */
|
|
|
|
i++;
|
|
|
|
} else if (s[i] == '[') {
|
|
|
|
/* Extract name bounds. */
|
|
|
|
size_t j = i + 1;
|
|
|
|
while (s[j] != ']') {
|
|
|
|
j++;
|
2019-09-17 06:49:58 +01:00
|
|
|
if (j >= len) {
|
2019-09-16 09:22:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* s[i+1:j] is cheat name. */
|
2019-09-17 06:49:58 +01:00
|
|
|
const size_t cheat_name_len = std::min(j - i - 1, sizeof(cur_cheat_name));
|
2021-10-09 20:45:31 +01:00
|
|
|
std::memcpy(cur_cheat_name, s + (i + 1), cheat_name_len);
|
2019-09-16 09:22:08 +01:00
|
|
|
cur_cheat_name[cheat_name_len] = 0;
|
|
|
|
|
|
|
|
/* Skip onwards. */
|
|
|
|
i = j + 1;
|
|
|
|
|
|
|
|
/* Skip whitespace. */
|
|
|
|
while (std::isspace(static_cast<unsigned char>(s[i]))) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse whether to toggle. */
|
|
|
|
j = i + 1;
|
|
|
|
while (!std::isspace(static_cast<unsigned char>(s[j]))) {
|
|
|
|
j++;
|
|
|
|
if (j >= len || (j - i) >= sizeof(toggle)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* s[i:j] is toggle. */
|
|
|
|
const size_t toggle_len = (j - i);
|
2021-10-09 20:45:31 +01:00
|
|
|
std::memcpy(toggle, s + i, toggle_len);
|
2019-09-16 09:22:08 +01:00
|
|
|
toggle[toggle_len] = 0;
|
|
|
|
|
|
|
|
/* Allow specifying toggle for not present cheat. */
|
|
|
|
CheatEntry *entry = this->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;
|
|
|
|
}
|
|
|
|
|
2021-10-11 02:22:32 +01:00
|
|
|
bool CheatProcessManager::LoadCheats(const ncm::ProgramId program_id, const u8 *module_id) {
|
2019-09-16 09:22:08 +01:00
|
|
|
/* Reset existing entries. */
|
|
|
|
this->ResetAllCheatEntries();
|
|
|
|
|
2021-10-11 02:22:32 +01:00
|
|
|
/* Open the file for program/module_id. */
|
2020-03-08 23:33:49 +00:00
|
|
|
fs::FileHandle file;
|
2019-09-16 09:22:08 +01:00
|
|
|
{
|
2020-03-08 23:33:49 +00:00
|
|
|
char path[fs::EntryNameLengthMax + 1];
|
2021-01-12 10:59:41 +00:00
|
|
|
util::SNPrintf(path, sizeof(path), "sdmc:/atmosphere/contents/%016lx/cheats/%02x%02x%02x%02x%02x%02x%02x%02x.txt", program_id.value,
|
2021-10-11 02:22:32 +01:00
|
|
|
module_id[0], module_id[1], module_id[2], module_id[3], module_id[4], module_id[5], module_id[6], module_id[7]);
|
2020-03-08 23:33:49 +00:00
|
|
|
if (R_FAILED(fs::OpenFile(std::addressof(file), path, fs::OpenMode_Read))) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
2020-03-08 23:33:49 +00:00
|
|
|
ON_SCOPE_EXIT { fs::CloseFile(file); };
|
2019-09-16 09:22:08 +01:00
|
|
|
|
2020-03-08 23:33:49 +00:00
|
|
|
/* Get file size. */
|
|
|
|
s64 file_size;
|
|
|
|
if (R_FAILED(fs::GetFileSize(std::addressof(file_size), file))) {
|
2019-09-16 09:22:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-01-19 02:43:36 +00:00
|
|
|
if (file_size < 0 || file_size >= static_cast<s64>(sizeof(g_text_file_buffer))) {
|
2019-09-16 09:22:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-01-19 02:43:36 +00:00
|
|
|
|
|
|
|
std::scoped_lock lk(g_text_file_buffer_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
/* Read cheats into buffer. */
|
2021-01-19 02:43:36 +00:00
|
|
|
if (R_FAILED(fs::ReadFile(file, 0, g_text_file_buffer, file_size))) {
|
2019-09-16 09:22:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-01-19 02:43:36 +00:00
|
|
|
g_text_file_buffer[file_size] = '\x00';
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
/* Parse cheat buffer. */
|
2021-01-19 02:43:36 +00:00
|
|
|
return this->ParseCheats(g_text_file_buffer, std::strlen(g_text_file_buffer));
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
bool CheatProcessManager::LoadCheatToggles(const ncm::ProgramId program_id) {
|
2019-09-16 09:22:08 +01:00
|
|
|
/* Unless we successfully parse, don't save toggles on close. */
|
2021-10-10 08:14:06 +01:00
|
|
|
m_should_save_cheat_toggles = false;
|
2019-09-16 09:22:08 +01:00
|
|
|
|
2020-03-08 23:33:49 +00:00
|
|
|
/* Open the file for program_id. */
|
|
|
|
fs::FileHandle file;
|
|
|
|
{
|
|
|
|
char path[fs::EntryNameLengthMax + 1];
|
2021-01-12 10:59:41 +00:00
|
|
|
util::SNPrintf(path, sizeof(path), "sdmc:/atmosphere/contents/%016lx/cheats/toggles.txt", program_id.value);
|
2020-03-08 23:33:49 +00:00
|
|
|
if (R_FAILED(fs::OpenFile(std::addressof(file), path, fs::OpenMode_Read))) {
|
|
|
|
/* No file presence is allowed. */
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
2020-03-08 23:33:49 +00:00
|
|
|
ON_SCOPE_EXIT { fs::CloseFile(file); };
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
/* Get file size. */
|
2020-03-08 23:33:49 +00:00
|
|
|
s64 file_size;
|
|
|
|
if (R_FAILED(fs::GetFileSize(std::addressof(file_size), file))) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-19 02:43:36 +00:00
|
|
|
if (file_size < 0 || file_size >= static_cast<s64>(sizeof(g_text_file_buffer))) {
|
2019-09-16 09:22:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-01-19 02:43:36 +00:00
|
|
|
|
|
|
|
std::scoped_lock lk(g_text_file_buffer_lock);
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
/* Read cheats into buffer. */
|
2021-01-19 02:43:36 +00:00
|
|
|
if (R_FAILED(fs::ReadFile(file, 0, g_text_file_buffer, file_size))) {
|
2019-09-16 09:22:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-01-19 02:43:36 +00:00
|
|
|
g_text_file_buffer[file_size] = '\x00';
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
/* Parse toggle buffer. */
|
2021-10-10 08:14:06 +01:00
|
|
|
m_should_save_cheat_toggles = this->ParseCheatToggles(g_text_file_buffer, std::strlen(g_text_file_buffer));
|
|
|
|
return m_should_save_cheat_toggles;
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
void CheatProcessManager::SaveCheatToggles(const ncm::ProgramId program_id) {
|
|
|
|
/* Open the file for program_id. */
|
2020-03-08 23:33:49 +00:00
|
|
|
fs::FileHandle file;
|
2019-09-16 09:22:08 +01:00
|
|
|
{
|
2020-03-08 23:33:49 +00:00
|
|
|
char path[fs::EntryNameLengthMax + 1];
|
2021-01-12 10:59:41 +00:00
|
|
|
util::SNPrintf(path, sizeof(path), "sdmc:/atmosphere/contents/%016lx/cheats/toggles.txt", program_id.value);
|
2020-03-08 23:33:49 +00:00
|
|
|
fs::DeleteFile(path);
|
|
|
|
fs::CreateFile(path, 0);
|
|
|
|
if (R_FAILED(fs::OpenFile(std::addressof(file), path, fs::OpenMode_Write | fs::OpenMode_AllowAppend))) {
|
2019-09-16 09:22:08 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 23:33:49 +00:00
|
|
|
ON_SCOPE_EXIT { fs::CloseFile(file); };
|
|
|
|
|
|
|
|
s64 offset = 0;
|
|
|
|
char buf[0x100];
|
2019-09-16 09:22:08 +01:00
|
|
|
|
|
|
|
/* Save all non-master cheats. */
|
|
|
|
for (size_t i = 1; i < MaxCheatCount; i++) {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (m_cheat_entries[i].definition.num_opcodes != 0) {
|
|
|
|
util::SNPrintf(buf, sizeof(buf), "[%s]\n", m_cheat_entries[i].definition.readable_name);
|
2020-03-08 23:33:49 +00:00
|
|
|
const size_t name_len = std::strlen(buf);
|
|
|
|
if (R_SUCCEEDED(fs::WriteFile(file, offset, buf, name_len, fs::WriteOption::Flush))) {
|
|
|
|
offset += name_len;
|
|
|
|
}
|
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
const char *entry = m_cheat_entries[i].enabled ? "true\n" : "false\n";
|
2020-03-08 23:33:49 +00:00
|
|
|
const size_t entry_len = std::strlen(entry);
|
|
|
|
if (R_SUCCEEDED(fs::WriteFile(file, offset, entry, entry_len, fs::WriteOption::Flush))) {
|
|
|
|
offset += entry_len;
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Manager global. */
|
2021-03-22 01:47:30 +00:00
|
|
|
util::TypedStorage<CheatProcessManager> g_cheat_process_manager;
|
2020-04-08 10:21:35 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitializeCheatManager() {
|
|
|
|
/* Initialize the debug events manager (spawning its threads). */
|
|
|
|
InitializeDebugEventsManager();
|
2019-09-16 09:22:08 +01:00
|
|
|
|
2021-01-19 02:43:36 +00:00
|
|
|
/* Initialize the frozen address map heap. */
|
|
|
|
g_frozen_address_map_heap = lmem::CreateUnitHeap(g_frozen_address_map_memory, sizeof(g_frozen_address_map_memory), sizeof(FrozenAddressMapEntry), lmem::CreateOption_ThreadSafe);
|
|
|
|
|
2020-04-08 10:21:35 +01:00
|
|
|
/* Create the cheat process manager (spawning its threads). */
|
2021-03-22 03:30:40 +00:00
|
|
|
util::ConstructAt(g_cheat_process_manager);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GetHasActiveCheatProcess() {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).GetHasActiveCheatProcess();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
2021-10-05 01:12:32 +01:00
|
|
|
os::NativeHandle GetCheatProcessEventHandle() {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).GetCheatProcessEventHandle();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetCheatProcessMetadata(CheatProcessMetadata *out) {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).GetCheatProcessMetadata(out);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ForceOpenCheatProcess() {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).ForceOpenCheatProcess();
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
2020-04-25 01:24:15 +01:00
|
|
|
Result PauseCheatProcess() {
|
|
|
|
return GetReference(g_cheat_process_manager).PauseCheatProcess();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result ResumeCheatProcess() {
|
|
|
|
return GetReference(g_cheat_process_manager).ResumeCheatProcess();
|
|
|
|
}
|
|
|
|
|
2021-07-22 03:21:58 +01:00
|
|
|
Result ForceCloseCheatProcess() {
|
|
|
|
return GetReference(g_cheat_process_manager).ForceCloseCheatProcess();
|
|
|
|
}
|
|
|
|
|
2019-09-16 09:22:08 +01:00
|
|
|
Result ReadCheatProcessMemoryUnsafe(u64 process_addr, void *out_data, size_t size) {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).ReadCheatProcessMemoryUnsafe(process_addr, out_data, size);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result WriteCheatProcessMemoryUnsafe(u64 process_addr, void *data, size_t size) {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).WriteCheatProcessMemoryUnsafe(process_addr, data, size);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
2020-04-25 01:24:15 +01:00
|
|
|
Result PauseCheatProcessUnsafe() {
|
|
|
|
return GetReference(g_cheat_process_manager).PauseCheatProcessUnsafe();
|
2020-04-25 01:00:43 +01:00
|
|
|
}
|
|
|
|
|
2020-04-25 01:24:15 +01:00
|
|
|
Result ResumeCheatProcessUnsafe() {
|
|
|
|
return GetReference(g_cheat_process_manager).ResumeCheatProcessUnsafe();
|
2020-04-25 01:00:43 +01:00
|
|
|
}
|
|
|
|
|
2019-09-16 09:22:08 +01:00
|
|
|
Result GetCheatProcessMappingCount(u64 *out_count) {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).GetCheatProcessMappingCount(out_count);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
2021-10-04 22:54:13 +01:00
|
|
|
Result GetCheatProcessMappings(svc::MemoryInfo *mappings, size_t max_count, u64 *out_count, u64 offset) {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).GetCheatProcessMappings(mappings, max_count, out_count, offset);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ReadCheatProcessMemory(u64 proc_addr, void *out_data, size_t size) {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).ReadCheatProcessMemory(proc_addr, out_data, size);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result WriteCheatProcessMemory(u64 proc_addr, const void *data, size_t size) {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).WriteCheatProcessMemory(proc_addr, data, size);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
2021-10-04 22:54:13 +01:00
|
|
|
Result QueryCheatProcessMemory(svc::MemoryInfo *mapping, u64 address) {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).QueryCheatProcessMemory(mapping, address);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetCheatCount(u64 *out_count) {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).GetCheatCount(out_count);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetCheats(CheatEntry *cheats, size_t max_count, u64 *out_count, u64 offset) {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).GetCheats(cheats, max_count, out_count, offset);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetCheatById(CheatEntry *out_cheat, u32 cheat_id) {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).GetCheatById(out_cheat, cheat_id);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ToggleCheat(u32 cheat_id) {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).ToggleCheat(cheat_id);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
2019-10-18 05:18:27 +01:00
|
|
|
Result AddCheat(u32 *out_id, const CheatDefinition &def, bool enabled) {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).AddCheat(out_id, def, enabled);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result RemoveCheat(u32 cheat_id) {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).RemoveCheat(cheat_id);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
2021-07-22 03:21:58 +01:00
|
|
|
Result SetMasterCheat(const CheatDefinition &def) {
|
|
|
|
return GetReference(g_cheat_process_manager).SetMasterCheat(def);
|
|
|
|
}
|
|
|
|
|
2020-04-25 01:00:43 +01:00
|
|
|
Result ReadStaticRegister(u64 *out, size_t which) {
|
|
|
|
return GetReference(g_cheat_process_manager).ReadStaticRegister(out, which);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result WriteStaticRegister(size_t which, u64 value) {
|
|
|
|
return GetReference(g_cheat_process_manager).WriteStaticRegister(which, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result ResetStaticRegisters() {
|
|
|
|
return GetReference(g_cheat_process_manager).ResetStaticRegisters();
|
|
|
|
}
|
|
|
|
|
2019-09-16 09:22:08 +01:00
|
|
|
Result GetFrozenAddressCount(u64 *out_count) {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).GetFrozenAddressCount(out_count);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetFrozenAddresses(FrozenAddressEntry *frz_addrs, size_t max_count, u64 *out_count, u64 offset) {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).GetFrozenAddresses(frz_addrs, max_count, out_count, offset);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetFrozenAddress(FrozenAddressEntry *frz_addr, u64 address) {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).GetFrozenAddress(frz_addr, address);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result EnableFrozenAddress(u64 *out_value, u64 address, u64 width) {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).EnableFrozenAddress(out_value, address, width);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result DisableFrozenAddress(u64 address) {
|
2020-04-08 10:21:35 +01:00
|
|
|
return GetReference(g_cheat_process_manager).DisableFrozenAddress(address);
|
2019-09-16 09:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|