mirror of
https://github.com/a2x/cs2-dumper.git
synced 2025-10-07 16:30:01 +08:00
Refactored code
This commit is contained in:
300
src/main.cpp
300
src/main.cpp
@@ -1,15 +1,22 @@
|
||||
#include "builder/builder.hpp"
|
||||
#include "process.hpp"
|
||||
#include "sdk/sdk.hpp"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include "utility/string.hpp"
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <regex>
|
||||
|
||||
using Entries = std::map<std::string, std::vector<std::pair<std::string, std::uint64_t>>>;
|
||||
using Entries = std::map<std::string, std::vector<std::pair<std::string, std::uint64_t>>, std::less<>>;
|
||||
|
||||
struct Signature {
|
||||
std::string name;
|
||||
std::string module;
|
||||
std::string pattern;
|
||||
bool relative;
|
||||
std::uint32_t levels;
|
||||
std::ptrdiff_t offset;
|
||||
};
|
||||
|
||||
static const std::array<std::pair<std::string_view, std::unique_ptr<builder::IFileBuilder>>, 4> builders = {
|
||||
{
|
||||
@@ -28,264 +35,199 @@ std::string sanitize_module_name(const std::string& name) {
|
||||
|
||||
template <class IFileBuilder>
|
||||
void generate_file(const std::string_view file_name, const Entries& entries, IFileBuilder& builder) {
|
||||
const std::string output_file_path = std::format("generated/{}.{}", file_name, builder.get_extension());
|
||||
const std::string output_file_path = std::format("generated/{}.{}", file_name, builder.extension());
|
||||
|
||||
std::ofstream output(output_file_path);
|
||||
std::ofstream file(output_file_path);
|
||||
|
||||
if (!output.good()) {
|
||||
spdlog::error("failed to open {}.", file_name);
|
||||
if (!file.good()) {
|
||||
spdlog::error("Failed to open {}.", file_name);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const auto sanitize_namespace_name = [](const std::string& namespace_name) -> std::string {
|
||||
const auto sanitize_namespace_name = [](const std::string& namespace_name) {
|
||||
static std::regex double_colon_pattern("\\::");
|
||||
|
||||
return std::regex_replace(namespace_name, double_colon_pattern, "_");
|
||||
};
|
||||
|
||||
builder.write_top_level(output);
|
||||
builder.write_top_level(file);
|
||||
|
||||
for (auto it = entries.begin(); it != entries.end(); ++it) {
|
||||
const auto& [namespace_name, variables] = *it;
|
||||
|
||||
const std::string sanitized_namespace = sanitize_namespace_name(namespace_name);
|
||||
|
||||
builder.write_namespace(output, sanitized_namespace);
|
||||
builder.write_namespace(file, sanitized_namespace);
|
||||
|
||||
for (const auto& [variable_name, variable_value] : variables)
|
||||
builder.write_variable(output, variable_name, variable_value);
|
||||
builder.write_variable(file, variable_name, variable_value);
|
||||
|
||||
builder.write_closure(output, it == std::prev(entries.end()));
|
||||
builder.write_closure(file, it == std::prev(entries.end()));
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::uint64_t> get_entity_list() noexcept {
|
||||
const std::optional<std::uint64_t> address = process::find_pattern("client.dll", "48 8B 0D ? ? ? ? 48 89 7C 24 ? 8B FA C1 EB");
|
||||
|
||||
if (!address.has_value())
|
||||
return std::nullopt;
|
||||
|
||||
return process::resolve_rip_relative_address(address.value()).value_or(0);
|
||||
}
|
||||
|
||||
std::optional<std::uint64_t> get_global_vars() noexcept {
|
||||
std::optional<std::uint64_t> address = process::find_pattern("client.dll", "48 89 0D ? ? ? ? 48 89 41");
|
||||
|
||||
if (!address.has_value())
|
||||
return std::nullopt;
|
||||
|
||||
address = process::resolve_rip_relative_address(address.value());
|
||||
|
||||
if (!address.has_value())
|
||||
return std::nullopt;
|
||||
|
||||
return address.value();
|
||||
}
|
||||
|
||||
std::optional<std::uint64_t> get_local_player() noexcept {
|
||||
std::optional<std::uint64_t> address = process::find_pattern("client.dll", "48 8B 05 ? ? ? ? 48 85 C0 74 4F");
|
||||
|
||||
if (!address.has_value())
|
||||
return std::nullopt;
|
||||
|
||||
address = process::resolve_rip_relative_address(address.value());
|
||||
|
||||
if (!address.has_value())
|
||||
return std::nullopt;
|
||||
|
||||
return address.value();
|
||||
}
|
||||
|
||||
std::optional<std::uint64_t> get_view_angles() noexcept {
|
||||
std::optional<std::uint64_t> address = process::find_pattern("client.dll", "48 8B 0D ? ? ? ? 48 8B 01 48 FF 60 30");
|
||||
|
||||
if (!address.has_value())
|
||||
return std::nullopt;
|
||||
|
||||
address = process::resolve_rip_relative_address(address.value());
|
||||
|
||||
if (!address.has_value())
|
||||
return std::nullopt;
|
||||
|
||||
return process::read_memory<std::uint64_t>(address.value()) + 0x4510;
|
||||
}
|
||||
|
||||
std::optional<std::uint64_t> get_view_matrix() noexcept {
|
||||
const std::optional<std::uint64_t> address = process::find_pattern("client.dll", "48 8D 0D ? ? ? ? 48 C1 E0 06");
|
||||
|
||||
if (!address.has_value())
|
||||
return std::nullopt;
|
||||
|
||||
return process::resolve_rip_relative_address(address.value()).value_or(0);
|
||||
}
|
||||
|
||||
void dump_schema_classes() {
|
||||
const auto schema_system = sdk::CSchemaSystem::get();
|
||||
|
||||
if (schema_system == nullptr) {
|
||||
spdlog::error("failed to get schema system.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
spdlog::info("schema system: {:#x}", reinterpret_cast<std::uint64_t>(schema_system));
|
||||
|
||||
for (const sdk::CSchemaSystemTypeScope* type_scope : schema_system->get_type_scopes()) {
|
||||
if (type_scope == nullptr)
|
||||
continue;
|
||||
|
||||
const std::string module_name = type_scope->get_module_name();
|
||||
|
||||
spdlog::info("generating files for {}...", module_name);
|
||||
|
||||
Entries entries;
|
||||
|
||||
for (const sdk::CSchemaType_DeclaredClass* declared_class : type_scope->get_declared_classes()) {
|
||||
if (declared_class == nullptr)
|
||||
continue;
|
||||
|
||||
spdlog::info("[{}] @ {:#x}", declared_class->get_class_name(), reinterpret_cast<std::uint64_t>(declared_class));
|
||||
|
||||
const sdk::CSchemaClassInfo* class_info = type_scope->find_declared_class(declared_class->get_class_name());
|
||||
|
||||
if (class_info == nullptr)
|
||||
continue;
|
||||
|
||||
for (const sdk::SchemaClassFieldData_t* field : class_info->get_fields()) {
|
||||
if (field == nullptr)
|
||||
continue;
|
||||
|
||||
spdlog::info(" [{}] = {:#x}", field->get_name(), field->get_offset());
|
||||
|
||||
entries[declared_class->get_class_name()].emplace_back(field->get_name(), field->get_offset());
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& [extension, builder] : builders) {
|
||||
generate_file(module_name, entries, *builder);
|
||||
|
||||
spdlog::info(" > generated {}.{}!", module_name, extension);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dump_interfaces() noexcept {
|
||||
const std::optional<std::vector<std::string>> loaded_modules = process::get_loaded_modules();
|
||||
void dump_interfaces() {
|
||||
const auto loaded_modules = process::loaded_modules();
|
||||
|
||||
if (!loaded_modules.has_value()) {
|
||||
spdlog::error("failed to get loaded modules.");
|
||||
spdlog::critical("Failed to get loaded modules.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
spdlog::info("generating interface files...");
|
||||
spdlog::info("Generating interface files...");
|
||||
|
||||
Entries entries;
|
||||
|
||||
for (const auto& module_name : loaded_modules.value()) {
|
||||
const std::optional<std::uint64_t> module_base = process::get_module_base(module_name);
|
||||
const auto module_base = process::get_module_base_by_name(module_name);
|
||||
|
||||
if (!module_base.has_value())
|
||||
continue;
|
||||
|
||||
const std::optional<std::uint64_t> create_interface_address = process::get_export(module_base.value(), "CreateInterface");
|
||||
const auto create_interface_address = process::get_module_export_by_name(module_base.value(), "CreateInterface");
|
||||
|
||||
if (!create_interface_address.has_value())
|
||||
continue;
|
||||
|
||||
std::optional<std::uint64_t> interface_registry = process::resolve_rip_relative_address(create_interface_address.value());
|
||||
auto interface_registry = utility::Address(create_interface_address.value()).rip().get();
|
||||
|
||||
if (!interface_registry.has_value())
|
||||
if (!interface_registry.is_valid())
|
||||
continue;
|
||||
|
||||
interface_registry = process::read_memory<std::uint64_t>(interface_registry.value());
|
||||
while (interface_registry.is_valid()) {
|
||||
const std::uint64_t interface_ptr = interface_registry.get().address();
|
||||
|
||||
if (!interface_registry.has_value())
|
||||
continue;
|
||||
|
||||
while (interface_registry.value() != 0) {
|
||||
const auto interface_ptr = process::read_memory<std::uint64_t>(interface_registry.value());
|
||||
|
||||
const std::string interface_version_name = process::read_string(process::read_memory<std::uint64_t>(interface_registry.value() + 0x8), 64);
|
||||
const std::string interface_version_name = process::read_string(interface_registry.add(0x8).get().address(), 64);
|
||||
|
||||
entries[sanitize_module_name(module_name)].emplace_back(interface_version_name, interface_ptr - module_base.value());
|
||||
|
||||
interface_registry = process::read_memory<std::uint64_t>(interface_registry.value() + 0x10);
|
||||
interface_registry = interface_registry.add(0x10).get();
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& [extension, builder] : builders) {
|
||||
generate_file("interfaces", entries, *builder);
|
||||
|
||||
spdlog::info(" > generated {}.{}!", "interfaces", extension);
|
||||
spdlog::info(" > Generated {}.{}", "interfaces", extension);
|
||||
}
|
||||
}
|
||||
|
||||
void dump_offsets() noexcept {
|
||||
const std::optional<std::uint64_t> client_base = process::get_module_base("client.dll");
|
||||
void dump_offsets() {
|
||||
std::ifstream file("config.json");
|
||||
|
||||
if (!client_base.has_value()) {
|
||||
spdlog::error("failed to get client.dll base.");
|
||||
if (!file.good()) {
|
||||
spdlog::critical("Failed to open config.json.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const auto get_client_rva = [&client_base](const std::uint64_t address) -> std::uint64_t {
|
||||
return address - client_base.value();
|
||||
};
|
||||
try {
|
||||
const auto json = nlohmann::json::parse(file);
|
||||
|
||||
const std::uint64_t entity_list_rva = get_client_rva(get_entity_list().value_or(0));
|
||||
const std::uint64_t global_vars_rva = get_client_rva(get_global_vars().value_or(0));
|
||||
const std::uint64_t local_player_controller_rva = get_client_rva(get_local_player().value_or(0));
|
||||
const std::uint64_t view_angles_rva = get_client_rva(get_view_angles().value_or(0));
|
||||
const std::uint64_t view_matrix_rva = get_client_rva(get_view_matrix().value_or(0));
|
||||
Entries entries;
|
||||
|
||||
spdlog::info("found offsets!");
|
||||
spdlog::info(" > entity list: {:#x}", entity_list_rva);
|
||||
spdlog::info(" > global vars: {:#x}", global_vars_rva);
|
||||
spdlog::info(" > local player controller: {:#x}", local_player_controller_rva);
|
||||
spdlog::info(" > view angles: {:#x}", view_angles_rva);
|
||||
spdlog::info(" > view matrix: {:#x}", view_matrix_rva);
|
||||
for (const auto& element : json["signatures"]) {
|
||||
const Signature signature = {
|
||||
.name = element["name"],
|
||||
.module = element["module"],
|
||||
.pattern = element["pattern"],
|
||||
.relative = element["relative"],
|
||||
.levels = element["levels"],
|
||||
.offset = element["offset"]
|
||||
};
|
||||
|
||||
const Entries entries = {
|
||||
{ "client_dll", {
|
||||
{ "entity_list", entity_list_rva },
|
||||
{ "global_vars", global_vars_rva },
|
||||
{ "local_player_controller", local_player_controller_rva },
|
||||
{ "view_angles", view_angles_rva },
|
||||
{ "view_matrix", view_matrix_rva }
|
||||
} }
|
||||
};
|
||||
const auto module_base = process::get_module_base_by_name(signature.module);
|
||||
|
||||
spdlog::info("generating offset files...");
|
||||
if (!module_base.has_value())
|
||||
continue;
|
||||
|
||||
for (const auto& [extension, builder] : builders) {
|
||||
generate_file("offsets", entries, *builder);
|
||||
auto address = process::find_pattern(signature.module, signature.pattern);
|
||||
|
||||
spdlog::info(" > generated {}.{}!", "offsets", extension);
|
||||
if (!address.has_value())
|
||||
continue;
|
||||
|
||||
if (signature.relative)
|
||||
address = address->rip();
|
||||
|
||||
if (signature.levels > 0)
|
||||
address = address->get(signature.levels);
|
||||
|
||||
address = address->add(signature.offset);
|
||||
|
||||
spdlog::info("Found '{}' @ {:#x} (RVA: {:#x})", signature.name, address->address(), address->address() - module_base.value());
|
||||
|
||||
entries[sanitize_module_name(signature.module)].emplace_back(signature.name, address->address() - module_base.value());
|
||||
}
|
||||
|
||||
spdlog::info("Generating offset files...");
|
||||
|
||||
for (const auto& [extension, builder] : builders) {
|
||||
generate_file("offsets", entries, *builder);
|
||||
|
||||
spdlog::info(" > Generated file: {}.{}", "offsets", extension);
|
||||
}
|
||||
} catch (const nlohmann::json::parse_error& ex) {
|
||||
spdlog::critical("Failed to parse config.json: {}", ex.what());
|
||||
}
|
||||
}
|
||||
|
||||
void dump_schemas() {
|
||||
const auto schema_system = sdk::SchemaSystem::get();
|
||||
|
||||
if (schema_system == nullptr) {
|
||||
spdlog::critical("Failed to get schema system.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
spdlog::info("Schema system: {:#x}", reinterpret_cast<std::uint64_t>(schema_system));
|
||||
|
||||
for (const auto& type_scope : schema_system->type_scopes()) {
|
||||
const std::string module_name = type_scope->module_name();
|
||||
|
||||
spdlog::info("Generating files for {}...", module_name);
|
||||
|
||||
Entries entries;
|
||||
|
||||
type_scope->for_each_class([&entries](const std::pair<std::string, sdk::SchemaClassInfo*>& pair) {
|
||||
spdlog::info(" [{}] @ {:#x}", pair.first, reinterpret_cast<std::uint64_t>(pair.second));
|
||||
|
||||
pair.second->for_each_field([&entries, &pair](const sdk::SchemaClassFieldData* field) {
|
||||
spdlog::info(" [{}] = {:#x}", field->name(), field->offset());
|
||||
|
||||
entries[pair.first].emplace_back(field->name(), field->offset());
|
||||
});
|
||||
});
|
||||
|
||||
for (const auto& [extension, builder] : builders) {
|
||||
generate_file(module_name, entries, *builder);
|
||||
|
||||
spdlog::info(" > Generated file: {}.{}", module_name, extension);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
const auto start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
if (!std::filesystem::exists("generated"))
|
||||
std::filesystem::create_directory("generated");
|
||||
|
||||
if (!process::attach("cs2.exe")) {
|
||||
spdlog::error("failed to attach to process.");
|
||||
spdlog::critical("Failed to attach to cs2.exe.");
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
spdlog::info("attached to process!");
|
||||
|
||||
dump_schema_classes();
|
||||
|
||||
dump_interfaces();
|
||||
|
||||
dump_offsets();
|
||||
|
||||
spdlog::info("finished!");
|
||||
dump_schemas();
|
||||
|
||||
spdlog::info("Done! Took {}ms.", std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start).count());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
160
src/process.cpp
160
src/process.cpp
@@ -1,8 +1,8 @@
|
||||
#include "process.hpp"
|
||||
#include "base/safe_handle.hpp"
|
||||
#include "utility/safe_handle.hpp"
|
||||
#include "utility/string.hpp"
|
||||
|
||||
#include <charconv>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
#include <Windows.h>
|
||||
@@ -11,67 +11,72 @@
|
||||
namespace process {
|
||||
std::uint32_t process_id = 0;
|
||||
|
||||
base::SafeHandle process_handle;
|
||||
utility::SafeHandle process_handle;
|
||||
|
||||
namespace detail {
|
||||
std::optional<std::uint32_t> get_process_id_by_name(const std::string_view process_name) noexcept {
|
||||
const base::SafeHandle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
|
||||
|
||||
if (snapshot.get() == INVALID_HANDLE_VALUE)
|
||||
return std::nullopt;
|
||||
|
||||
PROCESSENTRY32 process_entry = {};
|
||||
|
||||
process_entry.dwSize = sizeof(PROCESSENTRY32);
|
||||
|
||||
for (Process32First(snapshot.get(), &process_entry); Process32Next(snapshot.get(), &process_entry);) {
|
||||
if (std::string_view(process_entry.szExeFile) == process_name)
|
||||
return process_entry.th32ProcessID;
|
||||
}
|
||||
std::optional<std::uint32_t> get_process_id_by_name(const std::string_view process_name) noexcept {
|
||||
const utility::SafeHandle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
|
||||
|
||||
if (snapshot.get() == INVALID_HANDLE_VALUE)
|
||||
return std::nullopt;
|
||||
|
||||
PROCESSENTRY32 process_entry = {
|
||||
.dwSize = sizeof(PROCESSENTRY32)
|
||||
};
|
||||
|
||||
for (Process32First(snapshot.get(), &process_entry); Process32Next(snapshot.get(), &process_entry);) {
|
||||
if (std::string_view(process_entry.szExeFile) == process_name)
|
||||
return process_entry.th32ProcessID;
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
bool attach(const std::string_view process_name) {
|
||||
process_id = detail::get_process_id_by_name(process_name).value_or(0);
|
||||
bool attach(const std::string_view process_name) noexcept {
|
||||
const auto id = get_process_id_by_name(process_name);
|
||||
|
||||
if (process_id == 0)
|
||||
if (!id.has_value())
|
||||
return false;
|
||||
|
||||
process_handle = base::SafeHandle(OpenProcess(PROCESS_ALL_ACCESS, 0, process_id));
|
||||
process_id = id.value();
|
||||
|
||||
process_handle = utility::SafeHandle(OpenProcess(PROCESS_ALL_ACCESS, 0, process_id));
|
||||
|
||||
return process_handle.get() != nullptr;
|
||||
}
|
||||
|
||||
std::optional<std::uintptr_t> find_pattern(const std::string_view module_name, const std::string_view pattern) noexcept {
|
||||
constexpr auto pattern_to_bytes = [](const std::string_view pattern) -> std::vector<std::int32_t> {
|
||||
std::optional<utility::Address> find_pattern(const std::string_view module_name, const std::string_view pattern) noexcept {
|
||||
constexpr auto pattern_to_bytes = [](const std::string_view pattern) {
|
||||
std::vector<std::int32_t> bytes;
|
||||
|
||||
for (std::size_t i = 0; i < pattern.size(); ++i) {
|
||||
if (pattern[i] == ' ')
|
||||
continue;
|
||||
switch (pattern[i]) {
|
||||
case '?':
|
||||
bytes.push_back(-1);
|
||||
break;
|
||||
|
||||
if (pattern[i] == '?') {
|
||||
bytes.push_back(-1);
|
||||
case ' ':
|
||||
break;
|
||||
|
||||
continue;
|
||||
}
|
||||
default: {
|
||||
if (i + 1 < pattern.size()) {
|
||||
std::int32_t value = 0;
|
||||
|
||||
if (i + 1 < pattern.size()) {
|
||||
std::int32_t value = 0;
|
||||
if (const auto [ptr, ec] = std::from_chars(pattern.data() + i, pattern.data() + i + 2, value, 16); ec == std::errc()) {
|
||||
bytes.push_back(value);
|
||||
|
||||
if (const auto [ptr, ec] = std::from_chars(pattern.data() + i, pattern.data() + i + 2, value, 16); ec == std::errc())
|
||||
bytes.push_back(value);
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
++i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return bytes;
|
||||
};
|
||||
|
||||
const std::optional<std::uintptr_t> module_base = get_module_base(module_name);
|
||||
const auto module_base = get_module_base_by_name(module_name);
|
||||
|
||||
if (!module_base.has_value())
|
||||
return std::nullopt;
|
||||
@@ -98,7 +103,7 @@ namespace process {
|
||||
if (!read_memory(module_base.value(), module_data.get(), module_size))
|
||||
return std::nullopt;
|
||||
|
||||
const std::vector<std::int32_t> pattern_bytes = pattern_to_bytes(pattern);
|
||||
const auto pattern_bytes = pattern_to_bytes(pattern);
|
||||
|
||||
for (std::size_t i = 0; i < module_size - pattern.size(); ++i) {
|
||||
bool found = true;
|
||||
@@ -112,13 +117,31 @@ namespace process {
|
||||
}
|
||||
|
||||
if (found)
|
||||
return module_base.value() + i;
|
||||
return utility::Address(module_base.value() + i);
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<std::uintptr_t> get_export(const std::uintptr_t module_base, const std::string_view function_name) noexcept {
|
||||
std::optional<std::uintptr_t> get_module_base_by_name(const std::string_view module_name) noexcept {
|
||||
const utility::SafeHandle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, process_id));
|
||||
|
||||
if (snapshot.get() == INVALID_HANDLE_VALUE)
|
||||
return std::nullopt;
|
||||
|
||||
MODULEENTRY32 module_entry = {
|
||||
.dwSize = sizeof(MODULEENTRY32)
|
||||
};
|
||||
|
||||
for (Module32First(snapshot.get(), &module_entry); Module32Next(snapshot.get(), &module_entry);) {
|
||||
if (utility::string::equals_ignore_case(module_entry.szModule, module_name))
|
||||
return reinterpret_cast<std::uintptr_t>(module_entry.modBaseAddr);
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<std::uintptr_t> get_module_export_by_name(const std::uintptr_t module_base, const std::string_view function_name) noexcept {
|
||||
const auto headers = std::make_unique<std::uint8_t[]>(0x1000);
|
||||
|
||||
if (!read_memory(module_base, headers.get(), 0x1000))
|
||||
@@ -175,24 +198,15 @@ namespace process {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<std::uintptr_t> get_export(const std::string_view module_name, const std::string_view function_name) noexcept {
|
||||
const std::optional<std::uintptr_t> module_base = get_module_base(module_name);
|
||||
|
||||
if (!module_base.has_value())
|
||||
return std::nullopt;
|
||||
|
||||
return get_export(module_base.value(), function_name);
|
||||
}
|
||||
|
||||
std::optional<std::vector<std::string>> get_loaded_modules() noexcept {
|
||||
const base::SafeHandle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, process_id));
|
||||
std::optional<std::vector<std::string>> loaded_modules() noexcept {
|
||||
const utility::SafeHandle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, process_id));
|
||||
|
||||
if (snapshot.get() == INVALID_HANDLE_VALUE)
|
||||
return std::nullopt;
|
||||
|
||||
MODULEENTRY32 module_entry = {};
|
||||
|
||||
module_entry.dwSize = sizeof(MODULEENTRY32);
|
||||
MODULEENTRY32 module_entry = {
|
||||
.dwSize = sizeof(MODULEENTRY32)
|
||||
};
|
||||
|
||||
std::vector<std::string> loaded_modules;
|
||||
|
||||
@@ -202,53 +216,23 @@ namespace process {
|
||||
return loaded_modules;
|
||||
}
|
||||
|
||||
std::optional<std::uintptr_t> get_module_base(const std::string_view module_name) noexcept {
|
||||
const base::SafeHandle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, process_id));
|
||||
|
||||
if (snapshot.get() == INVALID_HANDLE_VALUE)
|
||||
return std::nullopt;
|
||||
|
||||
MODULEENTRY32 module_entry = {};
|
||||
|
||||
module_entry.dwSize = sizeof(MODULEENTRY32);
|
||||
|
||||
for (Module32First(snapshot.get(), &module_entry); Module32Next(snapshot.get(), &module_entry);) {
|
||||
if (_stricmp(module_entry.szModule, module_name.data()) == 0)
|
||||
return reinterpret_cast<std::uintptr_t>(module_entry.modBaseAddr);
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<std::uintptr_t> resolve_jmp(const std::uintptr_t address) noexcept {
|
||||
const auto displacement = read_memory<std::int32_t>(address + 0x1);
|
||||
|
||||
return address + displacement + 0x5;
|
||||
}
|
||||
|
||||
std::optional<std::uintptr_t> resolve_rip_relative_address(const std::uintptr_t address) noexcept {
|
||||
const auto displacement = read_memory<std::int32_t>(address + 0x3);
|
||||
|
||||
return address + displacement + 0x7;
|
||||
}
|
||||
|
||||
bool read_memory(const std::uintptr_t address, void* buffer, const std::size_t size) noexcept {
|
||||
return ReadProcessMemory(process_handle.get(), reinterpret_cast<LPCVOID>(address), buffer, size, nullptr);
|
||||
return ReadProcessMemory(process_handle.get(), reinterpret_cast<void*>(address), buffer, size, nullptr);
|
||||
}
|
||||
|
||||
bool write_memory(const std::uintptr_t address, const void* buffer, const std::size_t size) noexcept {
|
||||
return WriteProcessMemory(process_handle.get(), reinterpret_cast<LPVOID>(address), buffer, size, nullptr);
|
||||
return WriteProcessMemory(process_handle.get(), reinterpret_cast<void*>(address), buffer, size, nullptr);
|
||||
}
|
||||
|
||||
std::string read_string(const std::uintptr_t address, const std::size_t length) noexcept {
|
||||
std::vector<char> buffer(length);
|
||||
std::string buffer(length, '\0');
|
||||
|
||||
if (!read_memory(address, buffer.data(), length))
|
||||
return {};
|
||||
|
||||
if (const auto it = std::ranges::find(buffer, '\0'); it != buffer.end())
|
||||
buffer.resize(std::distance(buffer.begin(), it));
|
||||
buffer.erase(it, buffer.end());
|
||||
|
||||
return { buffer.begin(), buffer.end() };
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
|
@@ -1,22 +0,0 @@
|
||||
#include "sdk/sdk.hpp"
|
||||
|
||||
namespace sdk {
|
||||
std::uint16_t CSchemaClassInfo::get_fields_count() const noexcept {
|
||||
return process::read_memory<std::uint16_t>(reinterpret_cast<std::uint64_t>(this) + 0x1C);
|
||||
}
|
||||
|
||||
std::vector<SchemaClassFieldData_t*> CSchemaClassInfo::get_fields() const noexcept {
|
||||
std::vector<SchemaClassFieldData_t*> fields;
|
||||
|
||||
for (std::size_t i = 0; i < get_fields_count(); ++i) {
|
||||
const auto field = reinterpret_cast<SchemaClassFieldData_t*>(process::read_memory<std::uint64_t>(reinterpret_cast<std::uint64_t>(this) + 0x28) + (i * 0x20));
|
||||
|
||||
if (field == nullptr)
|
||||
continue;
|
||||
|
||||
fields.push_back(field);
|
||||
}
|
||||
|
||||
return fields;
|
||||
}
|
||||
}
|
@@ -1,28 +0,0 @@
|
||||
#include "sdk/c_schema_system.hpp"
|
||||
#include "process.hpp"
|
||||
|
||||
namespace sdk {
|
||||
CSchemaSystem* CSchemaSystem::get() noexcept {
|
||||
std::optional<std::uintptr_t> address = process::find_pattern("schemasystem.dll", "48 8D 0D ? ? ? ? E9 ? ? ? ? CC CC CC CC 48 8D 0D ? ? ? ? E9 ? ? ? ? CC CC CC CC 48 83 EC 28");
|
||||
|
||||
if (!address.has_value())
|
||||
return nullptr;
|
||||
|
||||
address = process::resolve_rip_relative_address(address.value());
|
||||
|
||||
if (!address.has_value())
|
||||
return nullptr;
|
||||
|
||||
return reinterpret_cast<CSchemaSystem*>(address.value());
|
||||
}
|
||||
|
||||
std::vector<CSchemaSystemTypeScope*> CSchemaSystem::get_type_scopes() const noexcept {
|
||||
std::vector<CSchemaSystemTypeScope*> type_scopes;
|
||||
|
||||
type_scopes.resize(process::read_memory<std::uint32_t>(reinterpret_cast<std::uint64_t>(this) + 0x190));
|
||||
|
||||
process::read_memory(process::read_memory<std::uint64_t>(reinterpret_cast<std::uint64_t>(this) + 0x198), type_scopes.data(), type_scopes.size() * sizeof(std::uint64_t));
|
||||
|
||||
return type_scopes;
|
||||
}
|
||||
}
|
@@ -1,97 +0,0 @@
|
||||
#include "sdk/sdk.hpp"
|
||||
#include "utility/murmur_hash.hpp"
|
||||
|
||||
namespace sdk {
|
||||
CSchemaClassInfo* CSchemaSystemTypeScope::find_declared_class(const std::string_view class_name) const noexcept {
|
||||
const std::uint32_t hash = utility::murmur_hash2(class_name.data(), static_cast<std::uint32_t>(class_name.length()), 0xBAADFEED);
|
||||
|
||||
const std::int32_t hash_transform1 = static_cast<std::uint8_t>(hash >> 0x10) - 0x2D6 + 0x21 * (0x21 * static_cast<std::uint8_t>(hash) + static_cast<std::uint8_t>(hash >> 0x8));
|
||||
const std::int32_t hash_transform2 = 0x21 * hash_transform1 + static_cast<std::uint8_t>(hash >> 0x18);
|
||||
|
||||
const auto hash_part = static_cast<std::uint32_t>(static_cast<std::uint8_t>(hash >> 0x18));
|
||||
|
||||
const std::int32_t hash_transform4 = (hash_transform2 ^ (hash_transform2 >> 0x10)) ^ (static_cast<std::uint16_t>(hash_transform2 ^ (hash_part >> 0x10)) >> 0x8);
|
||||
|
||||
const auto get_class_info = [&](const std::uint64_t address) -> CSchemaClassInfo* {
|
||||
const std::uint64_t class_info_address = address + 0x588 + static_cast<std::uint64_t>(0x28) * static_cast<std::uint8_t>(hash_transform4);
|
||||
|
||||
const auto initial_address = process::read_memory<std::uint64_t>(class_info_address + 0x58);
|
||||
|
||||
std::uint64_t class_address = initial_address;
|
||||
|
||||
if (initial_address != 0) {
|
||||
while (process::read_memory<std::uint32_t>(class_address) != hash) {
|
||||
class_address = process::read_memory<std::uint64_t>(class_address + 0x8);
|
||||
|
||||
if (class_address == 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (class_address == 0) {
|
||||
const auto secondary_address = process::read_memory<std::uint64_t>(class_info_address + 0x60);
|
||||
|
||||
std::uint64_t final_address = 0;
|
||||
|
||||
if (secondary_address != 0 && secondary_address != initial_address) {
|
||||
auto current_address = process::read_memory<std::uint64_t>(class_info_address + 0x60);
|
||||
|
||||
const auto starting_address = process::read_memory<std::uint64_t>(class_info_address + 0x58);
|
||||
|
||||
if (current_address != starting_address) {
|
||||
while (process::read_memory<std::uint32_t>(current_address) != hash) {
|
||||
current_address = process::read_memory<std::uint64_t>(current_address + 0x8);
|
||||
|
||||
if (current_address == starting_address)
|
||||
break;
|
||||
}
|
||||
|
||||
final_address = current_address;
|
||||
}
|
||||
}
|
||||
|
||||
class_address = final_address;
|
||||
}
|
||||
|
||||
return reinterpret_cast<CSchemaClassInfo*>(class_address);
|
||||
};
|
||||
|
||||
const CSchemaClassInfo* class_info = get_class_info(reinterpret_cast<std::uint64_t>(this));
|
||||
|
||||
if (class_info != nullptr)
|
||||
return reinterpret_cast<CSchemaClassInfo*>(process::read_memory<std::uint64_t>(reinterpret_cast<std::uint64_t>(class_info) + 0x10));
|
||||
|
||||
const auto secondary_class_info = process::read_memory<std::uint64_t>(reinterpret_cast<std::uint64_t>(this) + 0x108);
|
||||
|
||||
return secondary_class_info != 0 ? reinterpret_cast<CSchemaClassInfo*>(process::read_memory<std::uint64_t>(reinterpret_cast<std::uint64_t>(get_class_info(secondary_class_info)) + 0x10)) : nullptr;
|
||||
}
|
||||
|
||||
std::vector<CSchemaType_DeclaredClass*> CSchemaSystemTypeScope::get_declared_classes() const noexcept {
|
||||
std::vector<CSchemaType_DeclaredClass*> classes;
|
||||
|
||||
const std::uint64_t base = reinterpret_cast<std::uint64_t>(this) + 0x588;
|
||||
|
||||
const auto block_size = process::read_memory<std::uint32_t>(base + 0x4);
|
||||
const auto count = process::read_memory<std::uint32_t>(base + 0x10);
|
||||
const auto unallocated_data = process::read_memory<std::uint64_t>(base + 0x18 + 0x18);
|
||||
|
||||
std::uint32_t index = 0;
|
||||
|
||||
for (std::uint64_t element = unallocated_data; element != 0; element = process::read_memory<std::uint64_t>(element)) {
|
||||
for (std::size_t i = 0; i < block_size && i != count; ++i) {
|
||||
classes.push_back(reinterpret_cast<CSchemaType_DeclaredClass*>(process::read_memory<std::uint64_t>(element + 0x20 + (i * 0x18))));
|
||||
|
||||
++index;
|
||||
|
||||
if (index >= count)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return classes;
|
||||
}
|
||||
|
||||
std::string CSchemaSystemTypeScope::get_module_name() const noexcept {
|
||||
return process::read_string(reinterpret_cast<std::uint64_t>(this) + 0x8, 256);
|
||||
}
|
||||
}
|
@@ -1,7 +0,0 @@
|
||||
#include "sdk/sdk.hpp"
|
||||
|
||||
namespace sdk {
|
||||
std::string CSchemaType_DeclaredClass::get_class_name() const noexcept {
|
||||
return process::read_string(process::read_memory<std::uint64_t>(reinterpret_cast<std::uint64_t>(this) + 0x8), 64);
|
||||
}
|
||||
}
|
@@ -1,11 +1,11 @@
|
||||
#include "sdk/sdk.hpp"
|
||||
|
||||
namespace sdk {
|
||||
std::string SchemaClassFieldData_t::get_name() const noexcept {
|
||||
std::string SchemaClassFieldData::name() const noexcept {
|
||||
return process::read_string(process::read_memory<std::uint64_t>(reinterpret_cast<std::uint64_t>(this)), 64);
|
||||
}
|
||||
|
||||
std::uint16_t SchemaClassFieldData_t::get_offset() const noexcept {
|
||||
std::uint16_t SchemaClassFieldData::offset() const noexcept {
|
||||
return process::read_memory<std::uint16_t>(reinterpret_cast<std::uint64_t>(this) + 0x10);
|
||||
}
|
||||
}
|
18
src/sdk/schema_class_info.cpp
Normal file
18
src/sdk/schema_class_info.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "sdk/sdk.hpp"
|
||||
|
||||
namespace sdk {
|
||||
std::uint16_t SchemaClassInfo::fields_count() const noexcept {
|
||||
return process::read_memory<std::uint16_t>(reinterpret_cast<std::uint64_t>(this) + 0x1C);
|
||||
}
|
||||
|
||||
void SchemaClassInfo::for_each_field(const std::function<void(SchemaClassFieldData*)>& callback) const noexcept {
|
||||
for (std::size_t i = 0; i < fields_count(); ++i) {
|
||||
const auto field = reinterpret_cast<SchemaClassFieldData*>(process::read_memory<std::uint64_t>(reinterpret_cast<std::uint64_t>(this) + 0x28) + (i * 0x20));
|
||||
|
||||
if (field == nullptr)
|
||||
continue;
|
||||
|
||||
callback(field);
|
||||
}
|
||||
}
|
||||
}
|
28
src/sdk/schema_system.cpp
Normal file
28
src/sdk/schema_system.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "sdk/sdk.hpp"
|
||||
|
||||
namespace sdk {
|
||||
SchemaSystem* SchemaSystem::get() noexcept {
|
||||
const auto address = process::find_pattern("schemasystem.dll", "48 8D 0D ? ? ? ? E9 ? ? ? ? CC CC CC CC 48 8D 0D ? ? ? ? E9 ? ? ? ? CC CC CC CC 48 83 EC 28");
|
||||
|
||||
if (!address.has_value())
|
||||
return nullptr;
|
||||
|
||||
return address->rip().as<SchemaSystem*>();
|
||||
}
|
||||
|
||||
std::vector<SchemaSystemTypeScope*> SchemaSystem::type_scopes() const noexcept {
|
||||
const auto count = process::read_memory<std::uint32_t>(reinterpret_cast<std::uint64_t>(this) + 0x190);
|
||||
const auto data = process::read_memory<std::uint64_t>(reinterpret_cast<std::uint64_t>(this) + 0x198);
|
||||
|
||||
if (count == 0 || data == 0)
|
||||
return {};
|
||||
|
||||
std::vector<sdk::SchemaSystemTypeScope*> type_scopes;
|
||||
|
||||
type_scopes.resize(count);
|
||||
|
||||
process::read_memory(data, type_scopes.data(), count * sizeof(std::uint64_t));
|
||||
|
||||
return type_scopes;
|
||||
}
|
||||
}
|
14
src/sdk/schema_system_type_scope.cpp
Normal file
14
src/sdk/schema_system_type_scope.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include "sdk/sdk.hpp"
|
||||
|
||||
namespace sdk {
|
||||
void SchemaSystemTypeScope::for_each_class(const std::function<void(std::pair<std::string, SchemaClassInfo*>)>& callback) const noexcept {
|
||||
const auto classes = process::read_memory<UtlTsHash<SchemaTypeDeclaredClass*>>(reinterpret_cast<std::uint64_t>(this) + 0x588);
|
||||
|
||||
for (const auto& element : classes.elements())
|
||||
callback({ element->binary_name(), reinterpret_cast<SchemaClassInfo*>(element) });
|
||||
}
|
||||
|
||||
std::string SchemaSystemTypeScope::module_name() const noexcept {
|
||||
return process::read_string(reinterpret_cast<std::uint64_t>(this) + 0x8, 256);
|
||||
}
|
||||
}
|
11
src/sdk/schema_type_declared_class.cpp
Normal file
11
src/sdk/schema_type_declared_class.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "sdk/sdk.hpp"
|
||||
|
||||
namespace sdk {
|
||||
std::string SchemaTypeDeclaredClass::binary_name() const noexcept {
|
||||
return process::read_string(process::read_memory<std::uint64_t>(reinterpret_cast<std::uint64_t>(this) + 0x8), 64);
|
||||
}
|
||||
|
||||
std::string SchemaTypeDeclaredClass::module_name() const noexcept {
|
||||
return process::read_string(process::read_memory<std::uint64_t>(reinterpret_cast<std::uint64_t>(this) + 0x10), 256);
|
||||
}
|
||||
}
|
48
src/utility/address.cpp
Normal file
48
src/utility/address.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include "utility/address.hpp"
|
||||
#include "process.hpp"
|
||||
|
||||
namespace utility {
|
||||
Address Address::add(const std::ptrdiff_t offset) const noexcept {
|
||||
return Address(address_ + offset);
|
||||
}
|
||||
|
||||
std::uintptr_t Address::address() const noexcept {
|
||||
return address_;
|
||||
}
|
||||
|
||||
Address Address::get(const std::size_t times) const noexcept {
|
||||
std::uintptr_t base = address_;
|
||||
|
||||
for (std::size_t i = 0; i < times; ++i)
|
||||
base = process::read_memory<std::uintptr_t>(base);
|
||||
|
||||
return Address(base);
|
||||
}
|
||||
|
||||
bool Address::is_valid() const noexcept {
|
||||
return address_ > static_cast<std::uintptr_t>(0x1000)
|
||||
&& address_ < static_cast<std::uintptr_t>(sizeof(void*) == 4 ? 0x7FFEFFFF : 0x7FFFFFFEFFFF);
|
||||
}
|
||||
|
||||
Address Address::jmp(const std::ptrdiff_t offset) const noexcept {
|
||||
std::uintptr_t base = address_ + offset;
|
||||
|
||||
const auto displacement = process::read_memory<std::int32_t>(address_);
|
||||
|
||||
base += displacement;
|
||||
base += sizeof(std::uint32_t);
|
||||
|
||||
return Address(base);
|
||||
}
|
||||
|
||||
Address Address::rip(const std::ptrdiff_t offset, const std::size_t length) const noexcept {
|
||||
std::uintptr_t base = address_;
|
||||
|
||||
const auto displacement = process::read_memory<std::int32_t>(base + offset);
|
||||
|
||||
base += displacement;
|
||||
base += length;
|
||||
|
||||
return Address(base);
|
||||
}
|
||||
}
|
@@ -1,53 +0,0 @@
|
||||
#include "utility/murmur_hash.hpp"
|
||||
|
||||
namespace utility {
|
||||
std::uint32_t murmur_hash2(const void* key, std::uint32_t length, const std::uint32_t seed) {
|
||||
constexpr auto get_block = [](const std::uint32_t* ptr) -> std::uint32_t {
|
||||
const auto bytes = reinterpret_cast<const std::uint8_t*>(ptr);
|
||||
|
||||
return static_cast<std::uint32_t>(bytes[0]) | static_cast<std::uint32_t>(bytes[1]) << 0x8 | static_cast<std::uint32_t>(bytes[2]) << 0x10 | static_cast<std::uint32_t>(bytes[3]) << 0x18;
|
||||
};
|
||||
|
||||
constexpr std::uint32_t HASH_CONSTANT = 0x5BD1E995;
|
||||
|
||||
std::uint32_t hash = seed ^ length;
|
||||
|
||||
auto data = static_cast<const std::uint8_t*>(key);
|
||||
|
||||
while (length >= 4) {
|
||||
std::uint32_t block = get_block(reinterpret_cast<const std::uint32_t*>(data));
|
||||
|
||||
block *= HASH_CONSTANT;
|
||||
block ^= block >> 0x18;
|
||||
block *= HASH_CONSTANT;
|
||||
|
||||
hash *= HASH_CONSTANT;
|
||||
hash ^= block;
|
||||
|
||||
data += 0x4;
|
||||
length -= 0x4;
|
||||
}
|
||||
|
||||
switch (length) {
|
||||
case 3:
|
||||
hash ^= data[2] << 0x10;
|
||||
|
||||
case 2:
|
||||
hash ^= data[1] << 0x8;
|
||||
|
||||
case 1: {
|
||||
hash ^= data[0];
|
||||
hash *= HASH_CONSTANT;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
hash ^= hash >> 0xD;
|
||||
hash *= HASH_CONSTANT;
|
||||
hash ^= hash >> 0xF;
|
||||
|
||||
return hash;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user