summaryrefslogtreecommitdiffhomepage
path: root/src/gameinfo.c
blob: 31752a1b7faf02ed6331705228b7404e337daa7b (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
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
/*
 * Copyright © 2022 Michael Smith <mikesmiffy128@gmail.com>
 *
 * 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 <ctype.h>
#include <limits.h>
#include <stdbool.h>
#ifdef _WIN32
#include <shlwapi.h>
#endif

#include "con_.h"
#include "gametype.h"
#include "intdefs.h"
#include "kv.h"
#include "os.h"
#include "vcall.h"

#ifdef _WIN32
#define fS "S" // os string (wide string) to regular string
#define Fs L"S" // regular string to os string (wide string)
#define PATHSEP L"\\" // for joining. could just be / but \ is more consistent
#else
// everything is just a regular string already
#define fS "s"
#define Fs "s"
#define PATHSEP "/"
#endif

// TODO(opt): get rid of the rest of the snprintf and strcpy, some day

static os_char bindir[PATH_MAX] = {0};
#ifdef _WIN32
static os_char gamedir[PATH_MAX] = {0};
#endif
static os_char clientlib[PATH_MAX] = {0};
static os_char serverlib[PATH_MAX] = {0};
static char title[64] = {0};

const os_char *gameinfo_bindir = bindir;
const os_char *gameinfo_gamedir
#ifdef _WIN32
	= gamedir; // on linux, the pointer gets directly set in gameinfo_init()
#endif
const os_char *gameinfo_clientlib = clientlib;
const os_char *gameinfo_serverlib = serverlib;
const char *gameinfo_title = title;

// case insensitive substring match, expects s2 to be lowercase already!
// note: in theory this shouldn't need to be case sensitive, but I've seen mods
// use both lowercase and TitleCase so this is just to be as lenient as possible
static bool matchtok(const char *s1, const char *s2, usize sz) {
	for (; sz; --sz, ++s1, ++s2) if (tolower(*s1) != *s2) return false;
	return true;
}

static void trygamelib(const os_char *path, os_char *outpath) {
	// _technically_ this is toctou, but I don't think that matters here
	if (os_access(path, F_OK) != -1) {
		os_strcpy(outpath, path);
	}
	else if (errno != ENOENT) {
		con_warn("gameinfo: failed to access %" fS ": %s\n", path,
				strerror(errno));
	}
}

// note: p and len are a non-null-terminated string
static inline void dolibsearch(const char *p, uint len, bool isgamebin,
		const os_char *cwd) {
	// sanity check: don't do a bunch of work for no reason
	if (len >= PATH_MAX - 1 - (sizeof("client" OS_DLSUFFIX) - 1)) goto toobig;
	os_char bindir[PATH_MAX];
	os_char *outp = bindir;
	// this should really be an snprintf, meh whatever
	os_strcpy(bindir, cwd);
	outp = bindir + os_strlen(bindir);
	// quick note about windows encoding conversion: this MIGHT clobber the
	// encoding of non-ascii mod names, but it's unclear if/how source handles
	// that anyway, so we just have to assume there *are no* non-ascii mod
	// names, since they'd also be clobbered, probably. if I'm wrong this can
	// just change later to an explicit charset conversion, so... it's kinda
	// whatever, I guess
	const os_char *fmt = isgamebin ?
		OS_LIT("/%.*") Fs OS_LIT("/") :
		OS_LIT("/%.*") Fs OS_LIT("/bin/");
	int spaceleft = PATH_MAX;
	if (len >= 25 && matchtok(p, "|all_source_engine_paths|", 25)) {
		// this special path doesn't seem any different to normal,
		// why is this a thing?
		p += 25; len -= 25;
	}
	else if (len >= 15 && matchtok(p, "|gameinfo_path|", 15)) {
		// search in the actual mod/game directory
		p += 15; len -= 15;
		int ret = os_snprintf(bindir, PATH_MAX, OS_LIT("%s"), gamedir);
		outp = bindir + ret;
		spaceleft -= ret;
	}
	else {
#ifdef _WIN32
		// sigh
		char api_needs_null_term[PATH_MAX];
		memcpy(api_needs_null_term, p, len * sizeof(*p));
		api_needs_null_term[len] = L'\0';
		if (!PathIsRelativeA(api_needs_null_term))
#else
		if (*p == '/') // so much easier :')
#endif
	{
		// the mod path is absolute, so we're not sticking anything else in
		// front of it, so skip the leading slash in fmt and point the pointer
		// at the start of the buffer
		++fmt;
		outp = bindir;
	}}

	// leave room for server/client.dll/so (note: server and client happen to
	// conveniently have the same number of letters)
	int fmtspace = spaceleft - (sizeof("client" OS_DLSUFFIX) - 1);
	int ret = os_snprintf(outp, fmtspace, fmt, len, p);
	if (ret >= fmtspace) {
toobig: con_warn("gameinfo: skipping an overly long search path\n");
		return;
	}
	outp += ret;
	if (!*gameinfo_clientlib) {
		os_strcpy(outp, OS_LIT("client" OS_DLSUFFIX));
		trygamelib(bindir, clientlib);
	}
	if (!*gameinfo_serverlib) {
		os_strcpy(outp, OS_LIT("server" OS_DLSUFFIX));
		trygamelib(bindir, serverlib);
	}
}

// state for the callback below to keep it somewhat reentrant (except where
// it's not because I got lazy and wrote some spaghetti)
struct kv_parsestate {
	const os_char *cwd;
	// after parsing a key we *don't* care about, how many nested subkeys have
	// we come across?
	short dontcarelvl;
	// after parsing a key we *do* care about, which key in the matchkeys[]
	// array below are we looking for next?
	schar nestlvl;
	// what kind of key did we just match?
	schar matchtype;
};

// this is a sprawling mess. Too Bad!
static void kv_cb(enum kv_token type, const char *p, uint len, void *_ctxt) {
	struct kv_parsestate *ctxt = _ctxt;

	static const struct {
		const char *s;
		uint len;
	} matchkeys[] = {
		{"gameinfo", 8},
		{"filesystem", 10},
		{"searchpaths", 11}
	};

	// values for ctxt->matchtype
	enum {
		mt_none,
		mt_title,
		mt_nest,
		mt_game,
		mt_gamebin
	};

	#define MATCH(s) (len == sizeof(s) - 1 && matchtok(p, s, sizeof(s) - 1))
	switch (type) {
		case KV_IDENT: case KV_IDENT_QUOTED:
			if (ctxt->nestlvl == 1 && MATCH("game")) {
				ctxt->matchtype = mt_title;
			}
			else if (ctxt->nestlvl == 3) {
				// for some reason there's a million different ways of
				// specifying the same type of path
				if (MATCH("mod+game") || MATCH("game+mod") || MATCH("game") ||
						MATCH("mod")) {
					ctxt->matchtype = mt_game;
				}
				else if (MATCH("gamebin")) {
					ctxt->matchtype = mt_gamebin;
				}
			}
			else if (len == matchkeys[ctxt->nestlvl].len &&
					matchtok(p, matchkeys[ctxt->nestlvl].s, len)) {
				ctxt->matchtype = mt_nest;
			}
			break;
		case KV_NEST_START:
			if (ctxt->matchtype == mt_nest) ++ctxt->nestlvl;
			else ++ctxt->dontcarelvl;
			ctxt->matchtype = mt_none;
			break;
		case KV_VAL: case KV_VAL_QUOTED:
			if (ctxt->dontcarelvl) break;
			// dumb hack: ignore Survivors title (they left it set to "Left 4
			// Dead 2" but it clearly isn't Left 4 Dead 2)
			if (ctxt->matchtype == mt_title && !GAMETYPE_MATCHES(L4DS)) {
				// title really shouldn't get this long, but truncate just to
				// avoid any trouble...
				// also note: leaving 1 byte of space for null termination (the
				// buffer is already zeroed initially)
				if (len > sizeof(title) - 1) len = sizeof(title) - 1;
				memcpy(title, p, len);
			}
			else if (ctxt->matchtype == mt_game ||
					ctxt->matchtype == mt_gamebin) {
				// if we already have everything, we can just stop!
				if (*gameinfo_clientlib && *gameinfo_serverlib) break;
				dolibsearch(p, len, ctxt->matchtype == mt_gamebin, ctxt->cwd);
			}
			ctxt->matchtype = mt_none;
			break;
		case KV_NEST_END:
			if (ctxt->dontcarelvl) --ctxt->dontcarelvl; else --ctxt->nestlvl;
			break;
		case KV_COND_PREFIX: case KV_COND_SUFFIX:
			con_warn("gameinfo: warning: just ignoring conditional \"%.*s\"",
					len, p);
	}
	#undef MATCH
}

bool gameinfo_init(void *(*ifacef)(const char *, int *)) {
	typedef char *(*VCALLCONV GetGameDirectory_func)(void *this);
	GetGameDirectory_func **engclient;
	int off;
	if (engclient = ifacef("VEngineClient015", 0)) { // portal 2
		off = 35;
	}
	else if (engclient = ifacef("VEngineClient014", 0)) { // bms, l4d2 2000
		off = 36;
	}
	else if (engclient = ifacef("VEngineClient013", 0)) { // ...most things?
		if (GAMETYPE_MATCHES(L4Dx)) off = 36; // l4d2 changed it BACK?!?
		else off = 35;
	}
	else if (engclient = ifacef("VEngineClient012", 0)) { // dmomm, ep1, ...
		off = 37;
	}
	else {
		con_warn("gameinfo: unsupported VEngineClient interface\n");
		return false;
	}
	GetGameDirectory_func GetGameDirectory = (*engclient)[off];

	// engine always calls chdir() with its own base path on startup, so engine
	// base dir is just cwd
	os_char cwd[PATH_MAX];
	if (!os_getcwd(cwd, sizeof(cwd) / sizeof(*cwd))) {
		con_warn("gameinfo: couldn't get working directory: %s\n",
				strerror(errno));
		return false;
	}
	int len = os_strlen(cwd);
	if (len + sizeof("/bin") > sizeof(bindir) / sizeof(*bindir)) {
		con_warn("gameinfo: working directory path is too long!\n");
		return false;
	}
	memcpy(bindir, cwd, len * sizeof(*cwd));
	memcpy(bindir + len, PATHSEP OS_LIT("bin"), 5 * sizeof(os_char));

#ifdef _WIN32
	int gamedirlen = _snwprintf(gamedir, sizeof(gamedir) / sizeof(*gamedir),
				L"%S", GetGameDirectory(engclient));
	if (gamedirlen < 0) { // encoding error??? ugh...
		con_warn("gameinfo: invalid game directory path!\n");
		return false;
	}
	// immediately bounds check /gameinfo as we cat that into an equal sized
	// buffer down below :^)
	if (gamedirlen + sizeof("/gameinfo.txt") > sizeof(gamedir) /
			sizeof(*gamedir)) {
		con_warn("gameinfo: game directory path is too long!\n");
		return false;
	}
#else
	// no need to munge charset, use the string pointer directly
	gameinfo_gamedir = GetGameDirectory(engclient);
	int gamedirlen = strlen(gameinfo_gamedir);
#endif

	os_char gameinfopath[PATH_MAX];
	memcpy(gameinfopath, gameinfo_gamedir, gamedirlen *
			sizeof(*gameinfo_gamedir));
	memcpy(gameinfopath + gamedirlen, PATHSEP OS_LIT("gameinfo.txt"),
			14 * sizeof(os_char));
	int fd = os_open(gameinfopath, O_RDONLY);
	if (fd == -1) {
		con_warn("gameinfo: couldn't open gameinfo.txt: %s\n", strerror(errno));
		return false;
	}
	char buf[1024];
	struct kv_parser kvp = {0};
	struct kv_parsestate ctxt = {.cwd = cwd};
	int nread;
	while (nread = read(fd, buf, sizeof(buf))) {
		if (nread == -1) {
			con_warn("gameinfo: couldn't read gameinfo.txt: %s\n",
					strerror(errno));
			goto e;
		}
		if (!kv_parser_feed(&kvp, buf, nread, &kv_cb, &ctxt)) goto ep;
	}
	if (!kv_parser_done(&kvp)) goto ep;
	close(fd);

	// dumb hack pt2, see also kv callback above
	if (GAMETYPE_MATCHES(L4DS)) gameinfo_title = "Left 4 Dead: Survivors";
	return true;

ep:	con_warn("gameinfo: couldn't parse gameinfo.txt (%d:%d): %s\n",
			kvp.line, kvp.col, kvp.errmsg);
e:	close(fd);
	return false;
}

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