aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: e16786ea9987b6a6ca4c6ae717f2cb96c69f44d8 (plain) (blame)
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
// SPDX-License-Identifier: ISC
// SPDX-FileCopyrightText: 2024 Matthew Wozniak <me@woz.blue>

#include "api.h"
#include "hook.h"
#include "log.h"
#include "os.h"

#include <stddef.h>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

void *(WINAPI *orig_LoadLibraryExA)(const char *, void *, int);
void WINAPI *hook_LoadLibraryExA(const char *filename, void *hfile, int flags) {
	void *ret = orig_LoadLibraryExA(filename, hfile, flags);
	if (!ret) return ret;
	// cut down to basename
	const char *basename = filename;
	for (const char *p = filename; *p; p++)
		if (*p == '\\') basename = p + 1;
	info("loaded %s", basename);

	// last dll to load
	if (!strcmp(basename, "serverbrowser.dll")) {
		api_init();
		// TODO: figure out hooks that run AFTER valve.rc is called
	}
	return ret;
}

typedef int (*LauncherMain_t)(void *instance, void *prev_inst, char *cmdline,
		int cmd_show);

int main(int argc, char **argv) {
	SetDllDirectoryA("bin/");

	void *launcher_dll = os_dlopen("launcher");
	LauncherMain_t launcher_main =
		(LauncherMain_t)os_dlsym(launcher_dll, "LauncherMain");

	hook_init();
	orig_LoadLibraryExA = (typeof(orig_LoadLibraryExA))hook_dllapi("kernel32",
			"LoadLibraryExA", (void *)hook_LoadLibraryExA);

	if (!launcher_main) die("couldn't open launcher");
	launcher_main(NULL, NULL, NULL, 0);
}

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