-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdllmain.cpp
137 lines (118 loc) · 3.25 KB
/
dllmain.cpp
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
133
134
135
136
137
#define UNICODE
#include <Windows.h>
#include <TlHelp32.h>
#include <chrono>
#include <fmt/format.h>
#include <fstream>
#include <thread>
#include <utility>
#include <vector>
#include "hook.h"
#include "logger.h"
#include "max.h"
#include "search.h"
#include "version.h"
using namespace std::chrono_literals;
struct ProcessInfo {
std::string name;
DWORD pid;
};
struct Process {
HANDLE handle;
ProcessInfo info;
};
std::vector<ProcessInfo> get_processes() {
// No unicode
#undef Process32First
#undef Process32Next
#undef PROCESSENTRY32
std::vector<ProcessInfo> res;
auto snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapshot == nullptr)
return {};
PROCESSENTRY32 ppe = {sizeof(ppe)};
auto proc = Process32First(snapshot, &ppe);
while (proc) {
auto name = ppe.szExeFile;
if (auto delim = strrchr(name, '\\'))
name = delim;
res.push_back({name, ppe.th32ProcessID});
proc = Process32Next(snapshot, &ppe);
}
return res;
}
std::optional<Process> find_process(std::string name) {
for (auto &proc : get_processes()) {
if (proc.name == name) {
return Process{OpenProcess(PROCESS_ALL_ACCESS, 0, proc.pid), proc};
}
}
return {};
}
BOOL WINAPI ctrl_handler(DWORD ctrl_type) {
switch (ctrl_type) {
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT: {
DEBUG("Console detached, you can now close this window.");
FreeConsole();
return TRUE;
}
}
return TRUE;
}
void attach_stdout(DWORD pid) {
AttachConsole(pid);
SetConsoleCtrlHandler(ctrl_handler, 1);
FILE *stream;
freopen_s(&stream, "CONOUT$", "w", stdout);
freopen_s(&stream, "CONOUT$", "w", stderr);
// freopen_s(&stream, "CONIN$", "r", stdin);
INFO("Do not close this window or the game will also die. Press Ctrl+C to "
"detach this window from the game process.");
}
DWORD WINAPI AttachThread(LPVOID lParam) {
// std::this_thread::sleep_for(500ms);
Process proc;
if (auto res = find_process("MAXWELL.exe")) {
proc = res.value();
attach_stdout(proc.info.pid);
}
register_application_version(fmt::format("MAXWELL {}", get_version()));
if (D3D12::Init() == D3D12::Status::Success) {
D3D12::InstallHooks(lParam);
}
return 0;
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call,
LPVOID lpReserved) {
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH: {
DisableThreadLibraryCalls(hModule);
Max::get();
CreateThread(nullptr, 0, &AttachThread, static_cast<LPVOID>(hModule), 0,
nullptr);
break;
}
case DLL_PROCESS_DETACH: {
D3D12::RemoveHooks();
break;
}
}
return TRUE;
}
extern "C" __declspec(dllexport) const char *dll_version() {
return get_version_cstr();
}
auto xinput = LoadLibraryA("c:\\Windows\\system32\\xinput9_1_0.dll");
typedef DWORD(__stdcall *XInputFun)(DWORD, void *);
auto xinput_get_state =
reinterpret_cast<XInputFun>(GetProcAddress(xinput, "XInputGetState"));
auto xinput_set_state =
reinterpret_cast<XInputFun>(GetProcAddress(xinput, "XInputSetState"));
extern "C" __declspec(dllexport) DWORD XInputGetState(DWORD a, void *b) {
return xinput_get_state(a, b);
}
extern "C" __declspec(dllexport) DWORD XInputSetState(DWORD a, void *b) {
return xinput_set_state(a, b);
}