source_sdk_2013 0.1.0-alpha.8

Higher-level bindings for the Source SDK.
Documentation
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Tests of the engine's services for the game (`IVEngineServer`): the
//! arguments its methods receive, edict and user ID lookups, clients' Steam
//! IDs, the map's entities, the lock of the network string tables, and
//! visibility queries.

use sdk_raw::edicts::FL_EDICT_FREE;
use sdk_raw::players::ABSOLUTE_PLAYER_LIMIT;
use sdk_raw::test_support::{mock_vtable, unexpected_call};
use sdk_raw::tier0::MAX_PATH;
use source_sdk_2013::Module;
use source_sdk_2013::edicts::Edict;
use source_sdk_2013::interfaces::ValveEngine;
use source_sdk_2013::math::Vector;
use source_sdk_2013::players::UserId;
use source_sdk_2013::test_support::edicts::edict_table;
use source_sdk_2013::test_support::interfaces::valve_engine::{
	lock_network_string_tables, lock_requests, set_tables_locked, tables_locked,
};
use source_sdk_2013::test_support::leak;
use source_sdk_2013::test_support::server::{export, mock_server};
use std::cell::{Cell, RefCell};
use std::ffi::{CStr, c_char, c_int, c_uchar};
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::ptr::{self, null_mut};

/// The game directory the mock engine reports.
const GAME_DIR: &CStr = c"C:/srcds/tf";

thread_local! {
	/// The engine and command `ServerCommand` last received.
	static RECEIVED: Cell<(*mut sys::IVEngineServer, *const c_char)> =
		const { Cell::new((null_mut(), ptr::null())) };

	/// The text `GetMapEntitiesString` returns.
	static MAP_ENTITIES: Cell<*const c_char> = const { Cell::new(ptr::null()) };

	/// The length of every set passed to `CheckOriginInPVS` and `CheckBoxInPVS`.
	static PVS_LENGTHS: RefCell<Vec<c_int>> = const { RefCell::new(Vec::new()) };

	/// The edict table and clients the mock engine serves.
	static SERVER: RefCell<MockServer> = const { RefCell::new(MockServer::EMPTY) };
}

/// A stand-in for the engine's edict table and client list.
struct MockServer {
	table: *mut sys::edict_t,
	table_len: usize,
	/// The edict and user ID of each client, as `GetPlayerUserId` sees them.
	clients: Vec<(*const sys::edict_t, c_int)>,
	/// The edict and Steam ID of each client, as `GetClientSteamID` sees them.
	steam_ids: Vec<(*const sys::edict_t, sys::CSteamID)>,
	/// Whether `PEntityOfEntIndex` returns free slots, which the engine does not.
	returns_free_slots: bool,
	/// Every index passed to `PEntityOfEntIndex`.
	requested: Vec<c_int>,
}

impl MockServer {
	const EMPTY: Self = Self {
		table: null_mut(),
		table_len: 0,
		clients: Vec::new(),
		steam_ids: Vec::new(),
		returns_free_slots: false,
		requested: Vec::new(),
	};
}

/// `IVEngineServer::GetClientSteamID`, which returns the Steam ID of a served
/// client's edict, or null for any other edict, as the engine does.
unsafe extern "C" fn client_steam_id(
	_: *mut sys::IVEngineServer,
	edict: *mut sys::edict_t,
) -> *const sys::CSteamID {
	SERVER.with_borrow(|server| {
		server
			.steam_ids
			.iter()
			.find(|&&(client_edict, _)| client_edict == edict.cast_const())
			.map_or(ptr::null(), |(_, steam_id)| ptr::from_ref(steam_id))
	})
}

#[test]
fn client_steam_ids_are_only_valid_ones() {
	const INDIVIDUAL: u64 = 0x0110_0001_0000_56BA;
	const GAME_SERVER: u64 = 0x0130_0000_0000_04D2;

	// Slot 1 holds an individual account's ID, 2 the zeroed ID of a fake client
	// the engine does not report, 3 a game server's, 4 an individual account's
	// in an instance it never uses, 5 one of no known universe, and 6 no client.
	let mut table = edict_table(8, |_| false);

	serve(&mut table, &[], false);
	serve_steam_ids(&[
		(1, INDIVIDUAL),
		(2, 0),
		(3, GAME_SERVER),
		(4, 0x0110_0000_0000_56BA),
		(5, 0x0510_0001_0000_56BA),
	]);

	export_edict_lookup();

	let scope = ();
	let engine = mock_server(&scope).valve_engine().unwrap();
	let steam_id = |slot| engine.client_steam_id(engine.edict_of_index(slot).unwrap());

	assert_eq!(INDIVIDUAL, 76561197960287930);
	assert_eq!(steam_id(1), Some(INDIVIDUAL));
	assert_eq!(steam_id(2), None);
	assert_eq!(steam_id(3), Some(GAME_SERVER));
	assert_eq!(steam_id(4), None);
	assert_eq!(steam_id(5), None);
	assert_eq!(steam_id(6), None);
}

/// `IVEngineServer::PEntityOfEntIndex`, which records the index and returns
/// the slot of the served table, unless it is free and the mock does not
/// return free slots.
unsafe extern "C" fn edict_of_index(
	_: *mut sys::IVEngineServer,
	index: c_int,
) -> *mut sys::edict_t {
	SERVER.with_borrow_mut(|server| {
		server.requested.push(index);

		let Some(slot) = usize::try_from(index)
			.ok()
			.filter(|&slot| slot < server.table_len)
		else {
			return null_mut();
		};

		// SAFETY: The slot lies within the served table, which the test keeps
		// alive.
		let (edict, flags) = unsafe {
			let edict = server.table.add(slot);

			(edict, (&raw const (*edict)._base.m_fStateFlags).read())
		};

		if flags & FL_EDICT_FREE != 0 && !server.returns_free_slots {
			null_mut()
		} else {
			edict
		}
	})
}

/// Exports a mock engine that serves the edicts, clients and Steam IDs
/// [`serve`] and [`serve_steam_ids`] set.
fn export_edict_lookup() {
	// SAFETY: The patch only writes slots of the vtable.
	unsafe {
		export_engine(|vtable| {
			(&raw mut (*vtable).IVEngineServer_PEntityOfEntIndex).write(edict_of_index);
			(&raw mut (*vtable).IVEngineServer_GetPlayerUserId).write(player_user_id);
			(&raw mut (*vtable).IVEngineServer_GetClientSteamID).write(client_steam_id);
		})
	};
}

/// Exports a mock engine whose vtable `patch` fills, every other slot failing
/// the test if called, and returns it.
///
/// # Safety
///
/// `patch` must only write functions of the slots' types to the vtable's
/// slots.
unsafe fn export_engine(
	patch: impl FnOnce(*mut sys::IVEngineServer__bindgen_vtable),
) -> *mut sys::IVEngineServer {
	// SAFETY: The vtable holds only function pointers, `unexpected_call` aborts
	// whichever slot reaches it, and the caller's patch only writes slots of
	// the vtable being built.
	let vtable = unsafe { mock_vtable(unexpected_call as *const (), patch) };
	let engine = leak(sys::IVEngineServer {
		vtable_: Box::leak(vtable),
	});

	export(Module::Engine, ValveEngine::VERSION, engine);
	engine
}

/// Exports a mock engine that locks the string tables, and returns it.
fn export_lock() -> *mut sys::IVEngineServer {
	// SAFETY: The patch only writes a slot of the vtable.
	unsafe {
		export_engine(|vtable| {
			(&raw mut (*vtable).IVEngineServer_LockNetworkStringTables)
				.write(lock_network_string_tables);
		})
	}
}

#[test]
fn lookup_ignores_free_slots_even_if_the_engine_returns_them() {
	let mut table = edict_table(8, |slot| slot == 2);

	serve(&mut table, &[(1, 2), (2, 4)], true);
	export_edict_lookup();

	let scope = ();
	let engine = mock_server(&scope).valve_engine().unwrap();

	assert_eq!(engine.edict_of_user_id(UserId::new(4).unwrap()), None);
	assert_eq!(
		engine
			.edict_of_user_id(UserId::new(2).unwrap())
			.map(Edict::index),
		Some(1)
	);
}

#[test]
fn map_entities_are_copied_and_none_without_a_level() {
	// SAFETY: The patch only writes a slot of the vtable.
	unsafe {
		export_engine(|vtable| {
			(&raw mut (*vtable).IVEngineServer_GetMapEntitiesString).write(map_entities_string);
		})
	};

	let scope = ();
	let engine = mock_server(&scope).valve_engine().unwrap();
	let text = c"{\n\"classname\" \"worldspawn\"\n}\n{\n\"classname\" \"info_target\"\n}\n";

	MAP_ENTITIES.set(text.as_ptr());

	let entities = engine.map_entities().unwrap();

	assert_eq!(entities.as_c_str(), text);
	assert_ne!(entities.as_ptr(), text.as_ptr());

	// Without a level, the engine has no text, or an empty one.
	MAP_ENTITIES.set(ptr::null());
	assert_eq!(engine.map_entities(), None);

	MAP_ENTITIES.set(c"".as_ptr());
	assert_eq!(engine.map_entities(), None);
}

/// `IVEngineServer::GetMapEntitiesString`, which returns [`MAP_ENTITIES`].
unsafe extern "C" fn map_entities_string(_: *mut sys::IVEngineServer) -> *const c_char {
	MAP_ENTITIES.get()
}

#[test]
fn methods_forward_the_interface_and_arguments() {
	// SAFETY: The patch only writes slots of the vtable.
	let interface = unsafe {
		export_engine(|vtable| {
			(&raw mut (*vtable).IVEngineServer_ServerCommand).write(record_server_command);
			(&raw mut (*vtable).IVEngineServer_GetGameDir).write(write_game_dir);
		})
	};

	let scope = ();
	let engine = mock_server(&scope).valve_engine().unwrap();
	let command = c"bot\n";

	engine.server_command(command);

	assert_eq!(RECEIVED.get(), (interface, command.as_ptr()));
	assert_eq!(engine.game_dir().as_c_str(), GAME_DIR);
}

/// `IVEngineServer::GetPlayerUserId`, which returns the user ID of a served
/// client's edict, or -1 for any other edict, as the engine does.
unsafe extern "C" fn player_user_id(
	_: *mut sys::IVEngineServer,
	edict: *const sys::edict_t,
) -> c_int {
	SERVER.with_borrow(|server| {
		server
			.clients
			.iter()
			.find(|&&(client_edict, _)| client_edict == edict)
			.map_or(-1, |&(_, user_id)| user_id)
	})
}

/// `IVEngineServer::ServerCommand`, which records its arguments.
unsafe extern "C" fn record_server_command(this: *mut sys::IVEngineServer, command: *const c_char) {
	RECEIVED.set((this, command));
}

/// Every index passed to `PEntityOfEntIndex` so far.
fn requested_indices() -> Vec<c_int> {
	SERVER.with_borrow(|server| server.requested.clone())
}

/// Serves `table` and `clients`, given as `(slot, user ID)`, to the mock
/// engine.
fn serve(table: &mut [sys::edict_t], clients: &[(usize, c_int)], returns_free_slots: bool) {
	let base = table.as_mut_ptr();

	SERVER.set(MockServer {
		table: base,
		table_len: table.len(),
		clients: clients
			.iter()
			.map(|&(slot, user_id)| (base.wrapping_add(slot).cast_const(), user_id))
			.collect(),
		steam_ids: Vec::new(),
		returns_free_slots,
		requested: Vec::new(),
	});
}

/// Serves the Steam IDs of clients, given as `(slot, 64-bit Steam ID)`, in the
/// table [`serve`] set.
fn serve_steam_ids(steam_ids: &[(usize, u64)]) {
	SERVER.with_borrow_mut(|server| {
		server.steam_ids = steam_ids
			.iter()
			.map(|&(slot, steam_id)| {
				let steam_id = sys::CSteamID {
					m_steamid: sys::CSteamID_SteamID_t {
						m_unAll64Bits: steam_id,
					},
				};

				(server.table.wrapping_add(slot).cast_const(), steam_id)
			})
			.collect();
	});
}

#[test]
fn unlocked_scopes_leave_unlocked_tables_unlocked() {
	let interface = export_lock();
	let scope = ();
	let engine = mock_server(&scope).valve_engine().unwrap();

	set_tables_locked(false);
	engine.with_unlocked_string_tables(|| assert!(!tables_locked()));

	assert!(!tables_locked());
	assert_eq!(lock_requests(), [(interface, false), (interface, false)]);
}

#[test]
fn unlocked_scopes_restore_the_lock_when_they_panic() {
	let interface = export_lock();
	let scope = ();
	let engine = mock_server(&scope).valve_engine().unwrap();

	let outcome = catch_unwind(AssertUnwindSafe(|| {
		engine.with_unlocked_string_tables(|| {
			if !tables_locked() {
				panic!("the scope panicked");
			}
		});
	}));

	assert!(outcome.is_err());
	assert!(tables_locked());
	assert_eq!(lock_requests(), [(interface, false), (interface, true)]);
}

#[test]
fn user_ids_and_player_edicts_round_trip() {
	// Slot 2 is an empty player slot, slots 4 onwards hold other entities,
	// and slot 256 would exceed the engine's player limit.
	let clients = [(1, 2), (3, 7), (255, 9), (256, 11)];
	let mut table = edict_table(300, |slot| slot == 2);

	serve(&mut table, &clients, false);
	export_edict_lookup();

	let scope = ();
	let engine = mock_server(&scope).valve_engine().unwrap();

	for (slot, user_id) in [(1, 2), (3, 7), (255, 9)] {
		let user_id = UserId::new(user_id).unwrap();
		let edict = engine.edict_of_user_id(user_id).unwrap();

		assert_eq!(edict.index(), slot);
		assert_eq!(engine.edict_of_index(slot), Some(edict));
		assert_eq!(engine.user_id_of_edict(edict), Some(user_id));
	}

	assert_eq!(engine.edict_of_user_id(UserId::new(11).unwrap()), None);
	assert_eq!(engine.edict_of_user_id(UserId::new(5).unwrap()), None);
	assert_eq!(
		engine.user_id_of_edict(engine.edict_of_index(0).unwrap()),
		None
	);
	assert_eq!(
		engine.user_id_of_edict(engine.edict_of_index(4).unwrap()),
		None
	);
	assert_eq!(engine.edict_of_index(2), None);
	assert!(
		requested_indices()
			.iter()
			.all(|index| (0..=ABSOLUTE_PLAYER_LIMIT).contains(index))
	);
}

/// `IVEngineServer::GetGameDir`, which writes [`GAME_DIR`].
unsafe extern "C" fn write_game_dir(
	_: *mut sys::IVEngineServer,
	buffer: *mut c_char,
	length: c_int,
) {
	let length_with_nul = GAME_DIR.to_bytes_with_nul().len();

	assert_eq!(length, MAX_PATH as c_int);

	// SAFETY: The wrapper passes a buffer of `length` bytes, which the
	// directory fits.
	unsafe { ptr::copy_nonoverlapping(GAME_DIR.as_ptr(), buffer, length_with_nul) };
}

/// The clusters of the mock engine's map, three bytes of a set.
const CLUSTERS: c_int = 20;

/// `IVEngineServer::GetClusterForOrigin`, whose map lays its clusters out
/// along the x axis, one unit wide, from 0 to [`CLUSTERS`].
unsafe extern "C" fn cluster_for_origin(
	_: *mut sys::IVEngineServer,
	origin: *const sys::Vector,
) -> c_int {
	// SAFETY: The wrapper passes a vector it holds.
	mock_cluster(unsafe { (*origin).x })
}

/// `IVEngineServer::CheckBoxInPVS`, which records the set's length and checks
/// each cluster the box spans, as the engine does.
unsafe extern "C" fn check_box_in_pvs(
	_: *mut sys::IVEngineServer,
	mins: *const sys::Vector,
	maxs: *const sys::Vector,
	pvs: *const c_uchar,
	length: c_int,
) -> bool {
	PVS_LENGTHS.with_borrow_mut(|lengths| lengths.push(length));

	// SAFETY: The wrapper passes vectors it holds.
	let (mins, maxs) = unsafe { ((*mins).x, (*maxs).x) };

	(mins.floor() as c_int..=maxs.floor() as c_int)
		.filter(|&cluster| (0..CLUSTERS).contains(&cluster))
		// SAFETY: The wrapper passes a set of `length` bytes, which holds a bit for
		// every cluster of the map.
		.any(|cluster| unsafe { mock_pvs_contains(pvs, length, cluster) })
}

/// `IVEngineServer::CheckOriginInPVS`, which records the set's length and
/// checks the point's cluster, as the engine does.
unsafe extern "C" fn check_origin_in_pvs(
	_: *mut sys::IVEngineServer,
	origin: *const sys::Vector,
	pvs: *const c_uchar,
	length: c_int,
) -> bool {
	PVS_LENGTHS.with_borrow_mut(|lengths| lengths.push(length));

	// SAFETY: The wrapper passes a vector it holds.
	let cluster = mock_cluster(unsafe { (*origin).x });

	// SAFETY: As for `check_box_in_pvs`.
	cluster >= 0 && unsafe { mock_pvs_contains(pvs, length, cluster) }
}

/// Exports a mock engine that answers visibility queries for a map of
/// [`CLUSTERS`] clusters.
fn export_visibility() {
	PVS_LENGTHS.take();

	// SAFETY: The patch only writes slots of the vtable.
	unsafe {
		export_engine(|vtable| {
			(&raw mut (*vtable).IVEngineServer_GetClusterForOrigin).write(cluster_for_origin);
			(&raw mut (*vtable).IVEngineServer_GetPVSForCluster).write(pvs_for_cluster);
			(&raw mut (*vtable).IVEngineServer_CheckOriginInPVS).write(check_origin_in_pvs);
			(&raw mut (*vtable).IVEngineServer_CheckBoxInPVS).write(check_box_in_pvs);
		})
	};
}

/// The cluster of the mock map at `x`, or -1 outside it.
fn mock_cluster(x: f32) -> c_int {
	let cluster = x.floor() as c_int;

	if (0..CLUSTERS).contains(&cluster) {
		cluster
	} else {
		-1
	}
}

/// Whether the cluster's bit is set in a set of `length` bytes.
///
/// # Safety
///
/// `pvs` must point to `length` readable bytes, which hold `cluster`'s bit.
unsafe fn mock_pvs_contains(pvs: *const c_uchar, length: c_int, cluster: c_int) -> bool {
	assert!(cluster / 8 < length, "the engine would read past the set");

	// SAFETY: As the caller promises.
	unsafe { *pvs.add(cluster as usize / 8) & (1 << (cluster % 8)) != 0 }
}

/// `IVEngineServer::GetPVSForCluster`, which sees each cluster and the next
/// one from it, and nothing from outside the map, and returns the length of
/// the map's sets, as the engine does.
unsafe extern "C" fn pvs_for_cluster(
	_: *mut sys::IVEngineServer,
	cluster: c_int,
	capacity: c_int,
	pvs: *mut c_uchar,
) -> c_int {
	let length = (CLUSTERS + 7) / 8;

	assert!(
		capacity >= length,
		"the engine stops the server for a short buffer"
	);

	// SAFETY: The wrapper passes a buffer of `capacity` bytes.
	let pvs = unsafe { std::slice::from_raw_parts_mut(pvs, length as usize) };

	pvs.fill(0);

	for visible in [cluster, cluster + 1] {
		if cluster >= 0 && (0..CLUSTERS).contains(&visible) {
			pvs[visible as usize / 8] |= 1 << (visible % 8);
		}
	}

	length
}

#[test]
fn pvs_checks_pass_the_set_and_its_length() {
	export_visibility();

	let scope = ();
	let engine = mock_server(&scope).valve_engine().unwrap();
	let at = |x| Vector::new(x, 0.0, 0.0);
	let mut pvs = engine.pvs_for_origin(at(2.5));

	pvs.union_with(&engine.pvs_for_origin(at(10.0)));

	assert!(engine.check_origin_in_pvs(at(3.0), &pvs));
	assert!(engine.check_origin_in_pvs(at(11.9), &pvs));
	assert!(!engine.check_origin_in_pvs(at(4.0), &pvs));
	assert!(!engine.check_origin_in_pvs(at(-5.0), &pvs));
	assert!(!engine.check_box_in_pvs(at(4.0), at(9.5), &pvs));
	assert!(engine.check_box_in_pvs(at(4.0), at(10.0), &pvs));
	assert_eq!(PVS_LENGTHS.take(), [3; 6]);
}

#[test]
fn pvs_sets_have_the_length_the_engine_reports() {
	export_visibility();

	let scope = ();
	let engine = mock_server(&scope).valve_engine().unwrap();
	let at = |x| Vector::new(x, 0.0, 0.0);
	let cluster = engine.cluster_for_origin(at(7.5)).unwrap();
	let pvs = engine.pvs_for_cluster(cluster);

	assert_eq!(cluster.index(), 7);
	assert_eq!(pvs.as_bytes(), [0b1000_0000, 0b0000_0001, 0]);
	assert!(pvs.contains(cluster));
	assert!(!pvs.is_empty());
	assert_eq!(pvs, engine.pvs_for_origin(at(7.0)));

	// Outside the map: no cluster, and an empty set as long as any other.
	assert_eq!(engine.cluster_for_origin(at(-1.0)), None);
	assert_eq!(engine.cluster_for_origin(at(20.0)), None);

	let outside = engine.pvs_for_origin(at(-1.0));

	assert!(outside.is_empty());
	assert_eq!(outside.as_bytes().len(), 3);
	assert!(!engine.check_origin_in_pvs(at(7.0), &outside));
}