summaryrefslogtreecommitdiffhomepage
path: root/src/ac.c
blob: 4d21a48b325ef375d0b4372531ca76c2831df47f (plain)
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
/*
 * Copyright © 2022 Michael Smith <mikesmiffy128@gmail.com>
 * Copyright © 2022 Willian Henrique <wsimanbrazil@yahoo.com.br>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdbool.h>
#include <stdlib.h>

#include "bind.h"
#include "con_.h"
#include "hook.h"
#include "engineapi.h"
#include "errmsg.h"
#include "event.h"
#include "feature.h"
#include "intdefs.h"
#include "mem.h"
#include "os.h"
#include "ppmagic.h"
#include "sst.h"
#include "vcall.h"
#include "x86.h"
#include "x86util.h"

FEATURE()
REQUIRE(bind)
REQUIRE(democustom)
REQUIRE_GAMEDATA(vtidx_GetDesktopResolution)
REQUIRE_GAMEDATA(vtidx_DispatchAllStoredGameMessages)

static bool lockdown = false;

#ifdef _WIN32

static void *gamewin, *inhookwin, *inhookthr;
static ulong inhooktid;

// UINT_PTR is a **stupid** typedef, but whatever.
static ssize __stdcall kproc(int code, UINT_PTR wp, ssize lp) {
	KBDLLHOOKSTRUCT *data = (KBDLLHOOKSTRUCT *)lp;
	if (lockdown && data->flags & LLKHF_INJECTED &&
			GetForegroundWindow() == gamewin) {
		return 1;
	}
	return CallNextHookEx(0, code, wp, lp);
}

static ssize __stdcall mproc(int code, UINT_PTR wp, ssize lp) {
	MSLLHOOKSTRUCT *data = (MSLLHOOKSTRUCT *)lp;
	if (lockdown && data->flags & LLMHF_INJECTED &&
			GetForegroundWindow() == gamewin) {
		return 1;
	}
	return CallNextHookEx(0, code, wp, lp);
}

// this is its own thread to meet the strict timing deadline, otherwise the
// hook gets silently removed. plus, we don't wanna incur latency anyway.
static ulong __stdcall inhookthrmain(void *unused) {
	if (!SetWindowsHookExW(WH_KEYBOARD_LL, &kproc, 0, 0) ||
			!SetWindowsHookExW(WH_MOUSE_LL, &mproc, 0, 0)) {
		return -1;
	}
	MSG m; int ret;
	while ((ret = GetMessageW(&m, inhookwin, 0, 0)) > 0) DispatchMessage(&m);
	return ret;
}

static bool win32_init(void) {
	gamewin = FindWindowW(L"Valve001", 0);
	if (!gamewin) {
		errmsg_errorsys("failed to get game window handle");
		return false;
	}
	return true;
}

static void inhook_start(void) {
	inhookwin = CreateWindowW(L"sst-eventloop", L"sst-eventloop",
			WS_DISABLED, 0, 0, 0, 0, HWND_MESSAGE, 0, 0, 0);
	inhookthr = CreateThread(0, 0, &inhookthrmain, 0, 0, &inhooktid);
}

static void inhook_check(void) {
	if (WaitForSingleObject(inhookthr, 0) == WAIT_OBJECT_0) {
		ulong status;
		GetExitCodeThread(inhookthr, &status);
		if (status) {
			// XXX: if this ever happens, it's a disaster! users might not
			// notice their run just dying all of a sudden. with any luck it
			// won't matter in practice but... this kind of sucks.
			con_warn("** sst: ERROR in message loop, abandoning RTA mode! **");
			// TODO(rta): stop demos, and stuff.
			lockdown = false;
		}
	}
}

static void inhook_stop(void) {
	PostThreadMessageW(inhooktid, WM_QUIT, 0, 0);
	if (WaitForSingleObject(inhookthr, INFINITE) == WAIT_FAILED) {
		errmsg_warnsys("couldn't wait for thread, status unknown");
		// XXX: now what!?
	}
	// assume WAIT_OBJECT_0
	ulong status;
	GetExitCodeThread(inhookthr, &status);
	if (status) {
		// not much else we can do now!
		con_warn("warning: RTA mode message loop had an error during shutdown");
	}
}

#else

// TODO(linux): do some stuff, I guess...

#endif

// TODO(rta): call these functions as part of the run lifecycle

static void startlockdown(void) {
	if (lockdown) return;
#ifdef _WIN32
	inhook_start();
#endif
	// FIXME: should really have some semaphore to make sure inhook thread got
	// going okay...
	lockdown = true;
	// TODO(rta): start demos, etc
}

HANDLE_EVENT(Tick) {
#ifdef _WIN32
	static int fewticks = 0;
	// just check this every so often (roughly 0.1-0.3s depending on game)
	if (lockdown && !(++fewticks & 7)) inhook_check();
#endif
}

static void endlockdown(void) {
	if (!lockdown) return;
#ifdef _WIN32
	inhook_stop();
#endif
	lockdown = false;
}

enum /* from InputEventType_t - terser names used here */ {
	BTNDOWN = 0, // data contains button code
	BTNUP, // "
	BTNDOUBLECLICK, // "
	ANALOGUEVALCHG, // data contains analogue code, data2 contains value
	SYSQUIT = 100,
	SYSCTRLHOTPLUG, // data contains controller ID
	SYSCTRLCOLDPLUG, // "
	// ranges for other things
	FIRSTVGUIEV = 1000,
	FIRSTAPPEV = 2000
};

struct inputevent {
	int type; // above enum
	int tick;
	int data, data2, data3;
};

DECL_VFUNC_DYN(void, GetDesktopResolution, int *, int *)
DECL_VFUNC_DYN(void, DispatchAllStoredGameMessages)

typedef void (*VCALLCONV DispatchInputEvent_func)(void *, struct inputevent *);
static DispatchInputEvent_func orig_DispatchInputEvent;
static void VCALLCONV hook_DispatchInputEvent(void *this,
		struct inputevent *ev) {
	// TODO(rta): do something here! (here's a quick reference/example)
	//switch (ev->type) {
	//	CASES(BTNDOWN, BTNUP, BTNDOUBLECLICK):
	//		const char *desc[] = {"DOWN", "UP", "DOUBLE"};
	//		const char *binding = bind_get(ev->data);
	//		if (!binding) binding = "[unbound]";
	//		con_msg("key %d %s => %s\n", ev->data, desc[ev->type - BTNDOWN],
	//					binding);
	//}
	orig_DispatchInputEvent(this, ev);
}

static bool find_DispatchInputEvent(void) {
#ifdef _WIN32
	// Crazy pointer-chasing path to get to DispachInputEvent (to log keypresses
	// and their associated binds):
	// IGameUIFuncs interface
	// -> CGameUIFuncs::GetDesktopResolution vfunc
	//  -> IGame/CGame (first mov into ECX)
	//   -> CGame::DispatchAllStoredGameMessages vfunc
	//    -> DispatchInputEvent (first call instruction)
	void *gameuifuncs = factory_engine("VENGINE_GAMEUIFUNCS_VERSION005", 0);
	if (!gameuifuncs) {
		errmsg_errorx("couldn't get engine game UI interface");
		return false;
	}
	void *cgame;
	GetDesktopResolution_func GetDesktopResolution =
			VFUNC(gameuifuncs, GetDesktopResolution);
	for (uchar *p = (uchar *)GetDesktopResolution;
			p - (uchar *)GetDesktopResolution < 16;) {
		if (p[0] == X86_MOVRMW && p[1] == X86_MODRM(0, 1, 5)) {
			void **indirect = mem_loadptr(p + 2);
			cgame = *indirect;
			goto ok;
		}
		NEXT_INSN(p, "CGame instance pointer");
	}
	errmsg_errorx("couldn't find pointer to CGame instance");
	return false;
ok:	DispatchAllStoredGameMessages_func DispatchAllStoredGameMessages =
			VFUNC(cgame, DispatchAllStoredGameMessages);
	for (uchar *p = (uchar *)DispatchAllStoredGameMessages;
			p - (uchar *)DispatchAllStoredGameMessages < 128;) {
		if (p[0] == X86_CALL) {
			orig_DispatchInputEvent = (DispatchInputEvent_func)(p + 5 +
					mem_loadoffset(p + 1));
			// Note: we could go further and dig HandleEngineKey from Key_Event,
			// but it seems like Key_Event isn't directly called from
			// DispatchInputEvent in L4D2 (or has a much different structure,
			// requiring another function call to lead to HandleEngineKey).
			return true;
		}
		NEXT_INSN(p, "DispatchInputEvent function");
	}
	errmsg_errorx("couldn't find DispatchInputEvent function");
#else
#warning TODO(linux): more find-y stuff
#endif
	return false;
}

INIT {
#if defined(_WIN32)
	if (!win32_init()) return false;
#elif defined(__linux__)
	// TODO(linux): call init things
#endif
	if (!find_DispatchInputEvent()) return false;
	orig_DispatchInputEvent = (DispatchInputEvent_func)hook_inline(
			(void *)orig_DispatchInputEvent, (void *)&hook_DispatchInputEvent);
	if (!orig_DispatchInputEvent) {
		errmsg_errorsys("couldn't hook DispatchInputEvent function");
		return false;
	}
	return true;
}

END {
	endlockdown();
	unhook_inline((void *)orig_DispatchInputEvent);
}

// vi: sw=4 ts=4 noet tw=80 cc=80