aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 54f921194f3e384a1e8be819bfb6266b063f9fc9 (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
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
/*
 * Copyright © 2024 Matthew Wozniak <me@woz.blue>
 *
 * 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 "api.h"
#include "hook.h"
#include "log.h"
#include "render.h"
#include "os.h"
#include "opt.h"

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

struct {
	int width;
	int height;
	const char *game;
	int fps;
	int quality;
	int bitrate;
	const char *out;
	const char **demo;
	int demo_count;
	const char *encoder;
	const char *preset;
} args = {0};

void (*orig_cbuf_addtext)(char *);
void hook_cbuf_addtext(char *str) {
	orig_cbuf_addtext(str);
	// this is the last thing that happens when the game is opened
	if (!strcmp(str, "exec valve.rc\n")) {
		// after sound has been init'd, we can finish this
		if (!api_find_snd_recordbuffer())
			die("couldn't find SND_RecordBuffer");
		if (!render_init()) 
			die("couldn't init render");
		demoplayer->vt->start_playback(demoplayer, args.demo[0], false);
		orig_cbuf_addtext("exec prerender");
	}
}

char cmdline[128];
char WINAPI *hook_GetCommandLineA(void) {
	return cmdline;
}

void *(WINAPI *orig_LoadLibraryExA)(const char *, void *, int);
void WINAPI *hook_LoadLibraryExA(const char *filename, void *hfile, int flags) {
	// if the dll is already loaded, don't run our code again
	if (os_dlhandle(filename))
		return orig_LoadLibraryExA(filename, hfile, flags);
	void *ret = orig_LoadLibraryExA(filename, hfile, flags);
	if (!ret) return ret;
	// cut down to basename for display
	const char *basename = filename;
	for (const char *p = filename; *p; p++)
		if (*p == '\\') basename = p + 1;
	if (!strcmp(basename, "gameui.dll") || !strcmp(basename, "GameUI.dll")) {
		if (!api_init(basename[0] == 'G'))
			die("couldn't get apis");
		cbuf_addtext("echo THIS WORKS!!!!!!!!!!!!;");
		orig_cbuf_addtext = (void (*)(char *))
			hook_inline((void *)cbuf_addtext, (void *)hook_cbuf_addtext);
	}
	return ret;
}

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

void usage() {
	printf("usage:\n"
			"\trt [-w <width>] [-h <height>] [-g <game>] [-e <encoder>] [-r <fps>]\n"
			"\t[-q <crf>] OR [-b <bitrate>] path/to/video.mp4 path/to/demo1.dem...\n");
	exit(1);
}

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

	char *strend;
	const char *arg;
	FOR_OPTS(argc, argv) {
	case 'w':
		arg = OPTARG(argc, argv);
		args.width = strtol(arg, &strend, 10);
		if (strend == arg) die("width must be a number");
		break;
	case 'h':
		arg = OPTARG(argc, argv);
		args.height = strtol(arg, &strend, 10);
		if (strend == arg) die("height must be a number");
		break;
	case 'r':
		arg = OPTARG(argc, argv);
		args.fps = strtol(arg, &strend, 10);
		if (strend == arg) die("fps must be a number");
		break;
	case 'q':
		arg = OPTARG(argc, argv);
		args.quality = strtol(arg, &strend, 10);
		if (strend == arg) die("crf must be a number");
		break;
	case 'b':
		arg = OPTARG(argc, argv);
		args.bitrate = strtol(arg, &strend, 10);
		if (strend == arg) die("bitrate must be a number");
		break;
	case 'g':
		args.game = OPTARG(argc, argv);
		break;
	case 'p':
		args.preset = OPTARG(argc, argv);
		break;
	case 'e':
		args.encoder = OPTARG(argc, argv);
		break;
	}

	if (!args.encoder) args.encoder = "libx264";
	if (!args.width) args.width = 1280;
	if (!args.height) args.height = 720;
	if (!args.game) args.game = "hl2";
	if (!args.fps) args.fps = 30;
	if (!args.quality) args.quality = 25;
	if (!args.preset) args.preset = "fast";

	if (argc < 2) usage();
	args.out = argv[0];
	args.demo = argv + 1;
	args.demo_count = argc - 2;

	sprintf(cmdline, "hl2.exe -game %s -w %d -h %d -window -console",
			args.game, args.width, args.height);

	info("cmdline = %s", cmdline);

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

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

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

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