Repository navigation
Expand file tree
/
Copy pathhello_cdal.cpp
More file actions
79 lines (67 loc) · 2.97 KB
/
Copy pathhello_cdal.cpp
File metadata and controls
79 lines (67 loc) · 2.97 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
// Smallest useful CDAL plugin: an ASI that finds the WorldSystem and the local
// player through CDAL instead of its own signatures, and logs what it found.
// Build: part of the CDAL CMake project on Windows (target hello_cdal).
#include <windows.h>
#include <cdal/cdal.hpp>
#include <cdal/ids.hpp>
#include <cstdarg>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <string>
namespace {
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"hello_cdal.log"), std::ios::app}) out << buf << '\n';
}
DWORD WINAPI run(LPVOID) {
::Sleep(20000); // let the game unpack and reach the menu; never resolve inside DllMain
if (!cdal::init()) {
log("CDAL unavailable: %s", cdal::last_error().c_str());
return 0;
}
namespace ids = cdal::ids;
// One feature, one require(). cdal::Feature also tells cdal.dll (C API 6 or later) whether the
// feature is on, for CDAL/hooks.log, session.json and crash notes; an older cdal.dll just ignores it.
static cdal::Feature world_info{"hello_cdal example", "World info"};
std::string missing;
if (!world_info.require({ids::WorldSystem::sInstance, ids::StatEntry::Current}, &missing)) {
log("CDAL %s is missing what this plugin needs: %s", cdal::build().c_str(), missing.c_str());
return 0; // disable cleanly instead of crashing
}
auto world = *cdal::ptr<void**>(ids::WorldSystem::sInstance);
log("CDAL %s: WorldSystem at %p, StatEntry::Current at +0x%llX", cdal::build().c_str(), world,
static_cast<unsigned long long>(*cdal::value(ids::StatEntry::Current)));
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;
}