Repository navigation
Expand file tree
/
Copy pathshared_hooks.cpp
More file actions
132 lines (114 loc) · 5.4 KB
/
Copy pathshared_hooks.cpp
File metadata and controls
132 lines (114 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// A plugin that hooks through the CDAL hook manager, so it stacks cleanly with
// any other plugin that hooks the same functions through CDAL.
//
// - A function hook on StatusComponent::ApplyStatDelta that halves HP damage.
// - A mid hook on ItemDurability::ApplyDelta_Hook that stops items wearing down.
//
// It runs against the player's cdal.dll (installed once with CDAL; never ship your own). CDAL/hooks.log then shows these
// hooks, their owner and their place in each chain.
// Build: part of the CDAL CMake project on Windows (target shared_hooks).
#include <windows.h>
#include <cdal/cdal.hpp> // built with CDAL_SHARED: addresses come from cdal.dll
#include <cdal/hooks.hpp>
#include <cdal/ids.hpp>
#include <cstdarg>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <string>
namespace {
namespace ids = cdal::ids;
HMODULE g_module = nullptr;
// Resolve files next to this .asi rather than the process working directory,
// so the plugin still finds its log and config when installed in a subfolder.
std::filesystem::path sibling_path(const wchar_t* name) {
wchar_t path[MAX_PATH * 4];
const DWORD n = ::GetModuleFileNameW(g_module, path, MAX_PATH * 4);
if (!n || n >= MAX_PATH * 4) return name;
return std::filesystem::path(std::wstring(path, n)).parent_path() / name;
}
void log(const char* fmt, ...) {
char buf[1024];
va_list args;
va_start(args, fmt);
const int n = std::vsnprintf(buf, sizeof buf, fmt, args);
va_end(args);
if (n < 0) return;
if (std::ofstream out{sibling_path(L"shared_hooks.log"), std::ios::app}) out << buf << '\n';
}
// Argument types follow the id's description: rcx = stat container, dx = stat id, r8, r9 = delta, then on the
// stack the source, five byte flags and a pointer the game writes the result to. The game reads all eleven, so the
// detour takes and passes on all eleven.
using u8 = std::uint8_t;
using ApplyStatDelta = std::uint64_t (*)(void* stats, std::uint16_t stat, std::uint64_t r8, std::int64_t delta,
void* source, u8 a, u8 b, u8 c, u8 d, u8 e, std::uint64_t* out);
cdal::hooks::FunctionHook<ApplyStatDelta> g_apply;
cdal::hooks::MidHook g_no_wear;
std::uint64_t on_apply(void* stats, std::uint16_t stat, std::uint64_t r8, std::int64_t delta, void* source, u8 a,
u8 b, u8 c, u8 d, u8 e, std::uint64_t* out) {
if (stat == 0 && delta < 0) delta /= 2; // stat 0 is HP
// original() is the rest of the chain: lower-priority plugins first, then the game.
return g_apply.original()(stats, stat, r8, delta, source, a, b, c, d, e, out);
}
const cdal::hooks::Options opts{"shared_hooks example", 0};
// Each feature checks its own IDs, so a missing one switches off only that feature. cdal::Feature
// reports each one's state to cdal.dll too, so CDAL/hooks.log and crash notes say which is off and why.
cdal::Feature g_half_damage{opts.owner, "Half damage"};
cdal::Feature g_no_wear_feature{opts.owner, "No wear"};
void install_half_damage() {
std::string missing, err;
if (!g_half_damage.require({ids::StatusComponent::ApplyStatDelta}, &missing)) {
log("half damage off on CDAL %s: %s", cdal::build().c_str(), missing.c_str());
} else if (!g_apply.install(ids::StatusComponent::ApplyStatDelta, &on_apply, opts, &err)) {
log("half damage disabled: %s", err.c_str());
g_half_damage.off(err); // a failed hook switches the feature off like a missing ID (rule 7)
}
}
void install_no_wear() {
std::string missing, err;
if (!g_no_wear_feature.require({ids::ItemDurability::ApplyDelta_Hook}, &missing)) {
log("no wear off on CDAL %s: %s", cdal::build().c_str(), missing.c_str());
return;
}
g_no_wear = cdal::hooks::MidHook::create(
ids::ItemDurability::ApplyDelta_Hook,
[](cdal_context& ctx) {
if (static_cast<std::int16_t>(ctx.r12) < 0) ctx.r12 = 0; // r12w = signed durability delta
},
opts, &err);
if (!g_no_wear) {
log("no wear disabled: %s", err.c_str());
g_no_wear_feature.off(err);
}
}
DWORD WINAPI run(LPVOID) {
::Sleep(20000); // let the game unpack; never hook inside DllMain
if (!cdal::init()) {
log("CDAL unavailable: %s", cdal::last_error().c_str());
return 0;
}
install_half_damage();
install_no_wear();
for (const auto& h : cdal::hooks::list())
log("%s at 0x%llX: owner %s, priority %d, position %u%s%s", h.name.empty() ? "(address)" : h.name.c_str(),
static_cast<unsigned long long>(h.target), h.owner.c_str(), h.priority, h.chain_index,
h.foreign.empty() ? "" : ", also hooked outside CDAL by ", h.foreign.c_str());
return 0;
}
// ASI loaders also load every .asi into the game's crashpad_handler.exe: only start in the game.
bool in_game_process() {
wchar_t path[MAX_PATH * 4];
const DWORD n = ::GetModuleFileNameW(nullptr, path, MAX_PATH * 4);
const wchar_t* name = path + n;
while (name > path && name[-1] != L'\\' && name[-1] != L'/') --name;
return ::CompareStringOrdinal(name, -1, L"CrimsonDesert.exe", -1, TRUE) == CSTR_EQUAL;
}
} // namespace
BOOL WINAPI DllMain(HINSTANCE module, DWORD reason, LPVOID) {
if (reason == DLL_PROCESS_ATTACH && in_game_process()) {
g_module = module;
::DisableThreadLibraryCalls(module);
if (HANDLE t = ::CreateThread(nullptr, 0, run, nullptr, 0, nullptr)) ::CloseHandle(t);
}
return TRUE;
}