-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.cpp
385 lines (297 loc) · 10.1 KB
/
main.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include "ini.h"
#include <atomic>
#include <charconv>
#include <string_view>
#include <system_error>
#include <cassert>
#include <windows.h>
namespace {
// Global variables are necessary, because Windows hooks have a terrible API.
HHOOK MouseHook;
HHOOK KeyboardHook;
constexpr unsigned SpamDownBit = 0x1;
constexpr unsigned SpamUpBit = 0x2;
std::atomic<unsigned> SpamFlags(0);
DWORD Interval = 5;
DWORD DownKeyCode = 0;
DWORD UpKeyCode = 0;
template <typename T>
inline auto ParseInt(std::string_view Value,T* r,const char* Message){
auto Begin = Value.data();
auto End = Begin+Value.size();
int Base = 10;
if(Begin[0] == '0' && (Begin[1]|32) == 'x'){
Begin += 2;
Base = 16;
}
auto [Ptr,Error] = std::from_chars(Begin,End,*r,Base);
if(Error != std::errc()){
throw std::system_error(std::make_error_code(Error),Message);
}
return Value.begin()+(Ptr-Value.data());
}
void ReadConfiguration(ini_lexer Ini){
auto Token = Ini.Next();
if(Token.Type != ini_token_type::Section || Token.Key != "general"){
Ini.Error("Expected [general] section.");
}
bool HasInterval = false;
while(Token.Type != ini_token_type::Eof){
Token = Ini.Next();
switch(Token.Type){
case ini_token_type::Eof: {
break;
}
case ini_token_type::Section: {
Ini.Error("Unexpected section.");
}
case ini_token_type::KeyValue: {
if(Token.Key == "interval"){
if(HasInterval){
Ini.Error("Event interval specified twice.");
}
if(ParseInt(Token.Value,&Interval,Ini.Name().c_str()) != Token.Value.end()){
Ini.Error("Invalid value for the event interval.");
}
if(Interval == 0){
Ini.Error("Invalid value for the event interval.");
}
HasInterval = true;
}else if(Token.Key == "upbind"){
if(UpKeyCode != 0){
Ini.Error("Scroll up key bind specified twice.");
}
if(ParseInt(Token.Value,&UpKeyCode,Ini.Name().c_str()) != Token.Value.end()){
Ini.Error("Invalid value for scroll up key bind.");
}
if(UpKeyCode == 0 || UpKeyCode > 0xFE){
Ini.Error("Invalid value for scroll up key bind.");
}
}else if(Token.Key == "downbind"){
if(DownKeyCode != 0){
Ini.Error("Scroll down key bind specified twice.");
}
if(ParseInt(Token.Value,&DownKeyCode,Ini.Name().c_str()) != Token.Value.end()){
Ini.Error("Invalid value for scroll down key bind.");
}
if(DownKeyCode == 0 || DownKeyCode > 0xFE){
Ini.Error("Invalid value for scroll down key bind.");
}
}else{
Ini.Error("Invalid key/value pair.");
}
break;
}
}
}
if(DownKeyCode == UpKeyCode){
if(DownKeyCode == 0){
Ini.Error("No key was bound to either scroll direction.");
}else{
Ini.Error("Scroll up and scroll down both have the same binding.");
}
}
}
bool IsGameInFocus(){
auto Window = GetForegroundWindow();
constexpr int BufferSize = 35;
wchar_t Buffer[BufferSize];
std::wstring_view Str = {
Buffer,
static_cast<std::size_t>(GetClassNameW(Window,Buffer,BufferSize))
};
if(Str == L"LaunchUnrealUWindowsClient"){
Str = {Buffer,static_cast<std::size_t>(GetWindowTextW(Window,Buffer,BufferSize))};
if(Str != L"Dishonored"){
return false;
}
}else if(Str != L"Dishonored 2" && Str != L"Dishonored: Death of the Outsider"){
return false;
}
POINT Point;
[[maybe_unused]]
auto r = GetCursorPos(&Point);
assert(r);
if(Window != WindowFromPoint(Point)){
return false;
}
return true;
}
bool HandleKey(DWORD KeyCode,bool IsDown){
unsigned Mask = 0;
if(KeyCode != 0){
if(KeyCode == DownKeyCode){
Mask = SpamDownBit;
}else if(KeyCode == UpKeyCode){
Mask = SpamUpBit;
}
}
if(Mask == 0 || !IsGameInFocus()){
return false;
}
if(IsDown){
SpamFlags |= Mask;
}else{
SpamFlags &= ~Mask;
}
return true;
}
LRESULT CALLBACK LowLevelMouseProc(int Code,WPARAM WParam,LPARAM LParam){
if(Code < HC_ACTION){
return CallNextHookEx(MouseHook,Code,WParam,LParam);
}
assert(Code == HC_ACTION);
DWORD KeyCode = 0;
bool IsDown = false;
switch(WParam){
case WM_LBUTTONDOWN: {
IsDown = true;
[[fallthrough]];
}
case WM_LBUTTONUP: {
KeyCode = VK_LBUTTON;
break;
}
case WM_RBUTTONDOWN: {
IsDown = true;
[[fallthrough]];
}
case WM_RBUTTONUP: {
KeyCode = VK_RBUTTON;
break;
}
case WM_MBUTTONDOWN: {
IsDown = true;
[[fallthrough]];
}
case WM_MBUTTONUP: {
KeyCode = VK_MBUTTON;
break;
}
case WM_XBUTTONDOWN: {
IsDown = true;
[[fallthrough]];
}
case WM_XBUTTONUP: {
auto& Info = *reinterpret_cast<MSLLHOOKSTRUCT*>(LParam);
KeyCode = VK_XBUTTON1+HIWORD(Info.mouseData)-XBUTTON1;
break;
}
}
if(HandleKey(KeyCode,IsDown)){
return 1;
}
return CallNextHookEx(MouseHook,Code,WParam,LParam);
}
LRESULT CALLBACK LowLevelKeyboardProc(int Code,WPARAM WParam,LPARAM LParam){
if(Code < HC_ACTION){
return CallNextHookEx(KeyboardHook,Code,WParam,LParam);
}
assert(Code == HC_ACTION);
auto& Info = *reinterpret_cast<KBDLLHOOKSTRUCT*>(LParam);
auto IsDown = (WParam == WM_KEYDOWN || WParam == WM_SYSKEYDOWN);
if(HandleKey(Info.vkCode,IsDown)){
return 1;
}
return CallNextHookEx(KeyboardHook,Code,WParam,LParam);
}
DWORD GetWheelDelta(unsigned Flags){
DWORD r = 0;
if((Flags&SpamDownBit) != 0){
r -= WHEEL_DELTA;
}
if((Flags&SpamUpBit) != 0){
r += WHEEL_DELTA;
}
return r;
}
void CALLBACK TimerProc(UINT,UINT,DWORD_PTR,DWORD_PTR,DWORD_PTR){
if(IsGameInFocus()){
auto WheelDelta = GetWheelDelta(SpamFlags);
if(WheelDelta != 0){
INPUT Input = {};
Input.type = INPUT_MOUSE;
Input.mi.mouseData = WheelDelta;
Input.mi.dwFlags = MOUSEEVENTF_WHEEL;
SendInput(1,&Input,sizeof(Input));
}
}else{
SpamFlags = 0;
}
}
bool IsMouseButton(DWORD KeyCode){
return
(VK_LBUTTON <= KeyCode && KeyCode <= VK_RBUTTON) ||
(VK_MBUTTON <= KeyCode && KeyCode <= VK_XBUTTON2);
}
bool IsKeyboardKey(DWORD KeyCode){
return KeyCode != 0 && !IsMouseButton(KeyCode);
}
}
std::wstring GetKeyName(DWORD KeyCode){
auto ScanCode = MapVirtualKeyW(KeyCode,MAPVK_VK_TO_VSC);
constexpr int BufferLength = 256;
wchar_t Buffer[BufferLength];
auto Length = GetKeyNameTextW(static_cast<LONG>(ScanCode << 16),Buffer,BufferLength);
switch(KeyCode){
case VK_LBUTTON: return L"Mouse1";
case VK_RBUTTON: return L"Mouse2";
case VK_MBUTTON: return L"Mouse3";
case VK_XBUTTON1: return L"Mouse4";
case VK_XBUTTON2: return L"Mouse5";
}
return {Buffer,Buffer+Length};
}
int main(int argc,char** argv){
try {
auto ConfigFilename = "Dish2Macro.ini";
if(argc >= 2){
ConfigFilename = argv[1];
}
ReadConfiguration(ini_lexer(ConfigFilename));
if(IsMouseButton(DownKeyCode) || IsMouseButton(UpKeyCode)){
MouseHook = SetWindowsHookExW(WH_MOUSE_LL,LowLevelMouseProc,nullptr,0);
if(!MouseHook){
throw std::system_error(GetLastError(),std::system_category());
}
}
if(IsKeyboardKey(DownKeyCode) || IsKeyboardKey(UpKeyCode)){
KeyboardHook = SetWindowsHookExW(WH_KEYBOARD_LL,LowLevelKeyboardProc,nullptr,0);
if(!KeyboardHook){
throw std::system_error(GetLastError(),std::system_category());
}
}
if(!timeSetEvent(Interval,1,TimerProc,0,TIME_PERIODIC|TIME_CALLBACK_FUNCTION)){
throw std::system_error(GetLastError(),std::system_category());
}
std::printf("The macro is running.\n");
if(DownKeyCode != 0){
std::printf("Scroll down bound to %ls (0x%X).\n",
GetKeyName(DownKeyCode).c_str(),DownKeyCode);
}
if(UpKeyCode != 0){
std::printf("Scroll up bound to %ls (0x%X).\n",
GetKeyName(UpKeyCode).c_str(),UpKeyCode);
}
std::printf("Macro will fire every %u milliseconds, %u times a second.\n\n",
Interval,1000/Interval);
std::printf("Close this window to stop the macro.\n");
for(;;){
MSG Message;
while(GetMessageW(&Message,nullptr,0,0)){
TranslateMessage(&Message);
DispatchMessageW(&Message);
}
}
}catch(std::exception& e){
if(MouseHook){
UnhookWindowsHookEx(MouseHook);
}
if(KeyboardHook){
UnhookWindowsHookEx(KeyboardHook);
}
std::fprintf(stderr,"Error: %s\n\nPress Enter to exit...\n",e.what());
(void)std::getchar();
return 1;
}
}