2019-05-30 23:35:03 +00:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <locale>
|
|
|
|
#include "common/hex_util.h"
|
|
|
|
#include "common/microprofile.h"
|
|
|
|
#include "common/swap.h"
|
|
|
|
#include "core/core.h"
|
|
|
|
#include "core/core_timing.h"
|
|
|
|
#include "core/core_timing_util.h"
|
2020-02-11 23:56:24 +00:00
|
|
|
#include "core/hardware_properties.h"
|
2021-02-13 01:58:31 +00:00
|
|
|
#include "core/hle/kernel/k_page_table.h"
|
2021-04-24 05:04:28 +00:00
|
|
|
#include "core/hle/kernel/k_process.h"
|
2019-05-30 23:35:03 +00:00
|
|
|
#include "core/hle/service/hid/controllers/npad.h"
|
|
|
|
#include "core/hle/service/hid/hid.h"
|
|
|
|
#include "core/hle/service/sm/sm.h"
|
2020-04-09 02:21:21 +00:00
|
|
|
#include "core/memory.h"
|
2019-05-30 23:35:03 +00:00
|
|
|
#include "core/memory/cheat_engine.h"
|
|
|
|
|
2020-03-31 19:10:44 +00:00
|
|
|
namespace Core::Memory {
|
2020-09-15 07:24:42 +00:00
|
|
|
namespace {
|
2020-07-15 22:30:06 +00:00
|
|
|
constexpr auto CHEAT_ENGINE_NS = std::chrono::nanoseconds{1000000000 / 12};
|
2019-05-30 23:35:03 +00:00
|
|
|
constexpr u32 KEYPAD_BITMASK = 0x3FFFFFF;
|
|
|
|
|
2020-09-15 07:24:42 +00:00
|
|
|
std::string_view ExtractName(std::string_view data, std::size_t start_index, char match) {
|
|
|
|
auto end_index = start_index;
|
|
|
|
while (data[end_index] != match) {
|
|
|
|
++end_index;
|
|
|
|
if (end_index > data.size() ||
|
|
|
|
(end_index - start_index - 1) > sizeof(CheatDefinition::readable_name)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data.substr(start_index, end_index - start_index);
|
|
|
|
}
|
|
|
|
} // Anonymous namespace
|
|
|
|
|
2021-05-16 05:46:30 +00:00
|
|
|
StandardVmCallbacks::StandardVmCallbacks(System& system_, const CheatProcessMetadata& metadata_)
|
|
|
|
: metadata{metadata_}, system{system_} {}
|
2019-05-30 23:35:03 +00:00
|
|
|
|
|
|
|
StandardVmCallbacks::~StandardVmCallbacks() = default;
|
|
|
|
|
|
|
|
void StandardVmCallbacks::MemoryRead(VAddr address, void* data, u64 size) {
|
2019-11-26 21:29:34 +00:00
|
|
|
system.Memory().ReadBlock(SanitizeAddress(address), data, size);
|
2019-05-30 23:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StandardVmCallbacks::MemoryWrite(VAddr address, const void* data, u64 size) {
|
2019-11-26 22:39:57 +00:00
|
|
|
system.Memory().WriteBlock(SanitizeAddress(address), data, size);
|
2019-05-30 23:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 StandardVmCallbacks::HidKeysDown() {
|
|
|
|
const auto applet_resource =
|
|
|
|
system.ServiceManager().GetService<Service::HID::Hid>("hid")->GetAppletResource();
|
|
|
|
if (applet_resource == nullptr) {
|
|
|
|
LOG_WARNING(CheatEngine,
|
|
|
|
"Attempted to read input state, but applet resource is not initialized!");
|
2020-08-06 06:55:45 +00:00
|
|
|
return 0;
|
2019-05-30 23:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto press_state =
|
|
|
|
applet_resource
|
|
|
|
->GetController<Service::HID::Controller_NPad>(Service::HID::HidController::NPad)
|
|
|
|
.GetAndResetPressState();
|
|
|
|
return press_state & KEYPAD_BITMASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StandardVmCallbacks::DebugLog(u8 id, u64 value) {
|
|
|
|
LOG_INFO(CheatEngine, "Cheat triggered DebugLog: ID '{:01X}' Value '{:016X}'", id, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StandardVmCallbacks::CommandLog(std::string_view data) {
|
|
|
|
LOG_DEBUG(CheatEngine, "[DmntCheatVm]: {}",
|
|
|
|
data.back() == '\n' ? data.substr(0, data.size() - 1) : data);
|
|
|
|
}
|
|
|
|
|
|
|
|
VAddr StandardVmCallbacks::SanitizeAddress(VAddr in) const {
|
|
|
|
if ((in < metadata.main_nso_extents.base ||
|
|
|
|
in >= metadata.main_nso_extents.base + metadata.main_nso_extents.size) &&
|
|
|
|
(in < metadata.heap_extents.base ||
|
|
|
|
in >= metadata.heap_extents.base + metadata.heap_extents.size)) {
|
|
|
|
LOG_ERROR(CheatEngine,
|
|
|
|
"Cheat attempting to access memory at invalid address={:016X}, if this "
|
|
|
|
"persists, "
|
|
|
|
"the cheat may be incorrect. However, this may be normal early in execution if "
|
|
|
|
"the game has not properly set up yet.",
|
|
|
|
in);
|
|
|
|
return 0; ///< Invalid addresses will hard crash
|
|
|
|
}
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheatParser::~CheatParser() = default;
|
|
|
|
|
|
|
|
TextCheatParser::~TextCheatParser() = default;
|
|
|
|
|
2020-09-15 07:13:22 +00:00
|
|
|
std::vector<CheatEntry> TextCheatParser::Parse(std::string_view data) const {
|
2019-05-30 23:35:03 +00:00
|
|
|
std::vector<CheatEntry> out(1);
|
2020-09-15 07:13:22 +00:00
|
|
|
std::optional<u64> current_entry;
|
2019-05-30 23:35:03 +00:00
|
|
|
|
|
|
|
for (std::size_t i = 0; i < data.size(); ++i) {
|
2019-09-21 22:13:10 +00:00
|
|
|
if (::isspace(data[i])) {
|
2019-05-30 23:35:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data[i] == '{') {
|
|
|
|
current_entry = 0;
|
|
|
|
|
|
|
|
if (out[*current_entry].definition.num_opcodes > 0) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-09-15 07:24:42 +00:00
|
|
|
const auto name = ExtractName(data, i + 1, '}');
|
2019-05-30 23:35:03 +00:00
|
|
|
if (name.empty()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::memcpy(out[*current_entry].definition.readable_name.data(), name.data(),
|
|
|
|
std::min<std::size_t>(out[*current_entry].definition.readable_name.size(),
|
|
|
|
name.size()));
|
|
|
|
out[*current_entry]
|
|
|
|
.definition.readable_name[out[*current_entry].definition.readable_name.size() - 1] =
|
|
|
|
'\0';
|
|
|
|
|
|
|
|
i += name.length() + 1;
|
|
|
|
} else if (data[i] == '[') {
|
|
|
|
current_entry = out.size();
|
|
|
|
out.emplace_back();
|
|
|
|
|
2020-09-15 07:24:42 +00:00
|
|
|
const auto name = ExtractName(data, i + 1, ']');
|
2019-05-30 23:35:03 +00:00
|
|
|
if (name.empty()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::memcpy(out[*current_entry].definition.readable_name.data(), name.data(),
|
|
|
|
std::min<std::size_t>(out[*current_entry].definition.readable_name.size(),
|
|
|
|
name.size()));
|
|
|
|
out[*current_entry]
|
|
|
|
.definition.readable_name[out[*current_entry].definition.readable_name.size() - 1] =
|
|
|
|
'\0';
|
|
|
|
|
|
|
|
i += name.length() + 1;
|
2019-09-21 22:13:10 +00:00
|
|
|
} else if (::isxdigit(data[i])) {
|
2019-05-30 23:35:03 +00:00
|
|
|
if (!current_entry || out[*current_entry].definition.num_opcodes >=
|
|
|
|
out[*current_entry].definition.opcodes.size()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto hex = std::string(data.substr(i, 8));
|
|
|
|
if (!std::all_of(hex.begin(), hex.end(), ::isxdigit)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-10-13 12:10:50 +00:00
|
|
|
const auto value = static_cast<u32>(std::stoul(hex, nullptr, 0x10));
|
2019-05-30 23:35:03 +00:00
|
|
|
out[*current_entry].definition.opcodes[out[*current_entry].definition.num_opcodes++] =
|
2020-10-13 12:10:50 +00:00
|
|
|
value;
|
2019-05-30 23:35:03 +00:00
|
|
|
|
|
|
|
i += 8;
|
|
|
|
} else {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out[0].enabled = out[0].definition.num_opcodes > 0;
|
|
|
|
out[0].cheat_id = 0;
|
|
|
|
|
|
|
|
for (u32 i = 1; i < out.size(); ++i) {
|
|
|
|
out[i].enabled = out[i].definition.num_opcodes > 0;
|
|
|
|
out[i].cheat_id = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-05-16 05:46:30 +00:00
|
|
|
CheatEngine::CheatEngine(System& system_, std::vector<CheatEntry> cheats_,
|
|
|
|
const std::array<u8, 0x20>& build_id_)
|
|
|
|
: vm{std::make_unique<StandardVmCallbacks>(system_, metadata)},
|
|
|
|
cheats(std::move(cheats_)), core_timing{system_.CoreTiming()}, system{system_} {
|
|
|
|
metadata.main_nso_build_id = build_id_;
|
2019-05-30 23:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CheatEngine::~CheatEngine() {
|
|
|
|
core_timing.UnscheduleEvent(event, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheatEngine::Initialize() {
|
2020-07-27 23:00:41 +00:00
|
|
|
event = Core::Timing::CreateEvent(
|
|
|
|
"CheatEngine::FrameCallback::" + Common::HexToString(metadata.main_nso_build_id),
|
|
|
|
[this](std::uintptr_t user_data, std::chrono::nanoseconds ns_late) {
|
|
|
|
FrameCallback(user_data, ns_late);
|
|
|
|
});
|
2020-07-15 22:30:06 +00:00
|
|
|
core_timing.ScheduleEvent(CHEAT_ENGINE_NS, event);
|
2019-05-30 23:35:03 +00:00
|
|
|
|
|
|
|
metadata.process_id = system.CurrentProcess()->GetProcessID();
|
|
|
|
metadata.title_id = system.CurrentProcess()->GetTitleID();
|
|
|
|
|
2020-04-09 02:21:21 +00:00
|
|
|
const auto& page_table = system.CurrentProcess()->PageTable();
|
2020-08-06 06:48:11 +00:00
|
|
|
metadata.heap_extents = {
|
|
|
|
.base = page_table.GetHeapRegionStart(),
|
|
|
|
.size = page_table.GetHeapRegionSize(),
|
|
|
|
};
|
|
|
|
|
|
|
|
metadata.address_space_extents = {
|
|
|
|
.base = page_table.GetAddressSpaceStart(),
|
|
|
|
.size = page_table.GetAddressSpaceSize(),
|
|
|
|
};
|
|
|
|
|
|
|
|
metadata.alias_extents = {
|
|
|
|
.base = page_table.GetAliasCodeRegionStart(),
|
|
|
|
.size = page_table.GetAliasCodeRegionSize(),
|
|
|
|
};
|
2019-05-30 23:35:03 +00:00
|
|
|
|
|
|
|
is_pending_reload.exchange(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheatEngine::SetMainMemoryParameters(VAddr main_region_begin, u64 main_region_size) {
|
2020-08-06 06:48:11 +00:00
|
|
|
metadata.main_nso_extents = {
|
|
|
|
.base = main_region_begin,
|
|
|
|
.size = main_region_size,
|
|
|
|
};
|
2019-05-30 23:35:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 02:14:15 +00:00
|
|
|
void CheatEngine::Reload(std::vector<CheatEntry> reload_cheats) {
|
|
|
|
cheats = std::move(reload_cheats);
|
2019-05-30 23:35:03 +00:00
|
|
|
is_pending_reload.exchange(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
MICROPROFILE_DEFINE(Cheat_Engine, "Add-Ons", "Cheat Engine", MP_RGB(70, 200, 70));
|
|
|
|
|
2020-07-27 23:00:41 +00:00
|
|
|
void CheatEngine::FrameCallback(std::uintptr_t, std::chrono::nanoseconds ns_late) {
|
2019-05-30 23:35:03 +00:00
|
|
|
if (is_pending_reload.exchange(false)) {
|
|
|
|
vm.LoadProgram(cheats);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vm.GetProgramSize() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MICROPROFILE_SCOPE(Cheat_Engine);
|
|
|
|
|
|
|
|
vm.Execute(metadata);
|
|
|
|
|
2020-07-15 23:14:21 +00:00
|
|
|
core_timing.ScheduleEvent(CHEAT_ENGINE_NS - ns_late, event);
|
2019-05-30 23:35:03 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 19:10:44 +00:00
|
|
|
} // namespace Core::Memory
|