From 8a669bc96ffdb9d0f6f54e464da11e3375c80a55 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Thu, 17 Apr 2025 01:39:10 +0100 Subject: Add type-safety to virtual calls and accessors This probably should have been the design from the start. It's still possible to use void pointers, and this is done in a couple of places for simplicity, but wherever possible, we have actual structs for things now. Additionally, in places where vtables are fiddled with, e.g. vtable hooks, we have actual struct definitions with vtable pointers so there's need for pointer-casting horror. --- src/sst.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/sst.c') diff --git a/src/sst.c b/src/sst.c index bd73d44..e02f196 100644 --- a/src/sst.c +++ b/src/sst.c @@ -356,16 +356,16 @@ static void do_featureinit() { } } -typedef void (*VCALLCONV VGuiConnect_func)(void *this); +typedef void (*VCALLCONV VGuiConnect_func)(struct CEngineVGui *this); static VGuiConnect_func orig_VGuiConnect; -static void VCALLCONV hook_VGuiConnect(void *this) { +static void VCALLCONV hook_VGuiConnect(struct CEngineVGui *this) { orig_VGuiConnect(this); do_featureinit(); fixes_apply(); - unhook_vtable(*(void ***)vgui, vtidx_VGuiConnect, (void *)orig_VGuiConnect); + unhook_vtable(vgui->vtable, vtidx_VGuiConnect, (void *)orig_VGuiConnect); } -DECL_VFUNC_DYN(bool, VGuiIsInitialized) +DECL_VFUNC_DYN(struct CEngineVGui, bool, VGuiIsInitialized) // --- Magical deferred load order hack nonsense! --- // VDF plugins load right after server.dll, but long before most other stuff. We @@ -386,13 +386,13 @@ static bool deferinit() { // CEngineVGui::IsInitialized() which works everywhere. if (VGuiIsInitialized(vgui)) return false; sst_earlyloaded = true; // let other code know - if_cold (!os_mprot(*(void ***)vgui + vtidx_VGuiConnect, ssizeof(void *), + if_cold (!os_mprot(vgui->vtable + vtidx_VGuiConnect, ssizeof(void *), PAGE_READWRITE)) { errmsg_warnsys("couldn't make CEngineVGui vtable writable for deferred " "feature setup"); goto e; } - orig_VGuiConnect = (VGuiConnect_func)hook_vtable(*(void ***)vgui, + orig_VGuiConnect = (VGuiConnect_func)hook_vtable(vgui->vtable, vtidx_VGuiConnect, (void *)&hook_VGuiConnect); return true; -- cgit v1.2.3-54-g00ecf