From 244fea664121acf12871ab5858a5fe95a2606b52 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Mon, 10 Mar 2025 02:37:19 +0000 Subject: Rewrite and redesign codegen and feature system Also switch to somewhat proper C23 flags while we're at it. This is a huge change. It took me forever, in between being really busy. Sorry about that. But the good news is I'm now free to start integrating the various patches that have accumulated since last release. Well, at least in between still being really busy. Gotta manage expectations. The main benefit of introducing GAMESPECIFIC() is that features that don't apply to a particular game no longer show up *at all*, and less time is wasted on init. It also enables a cool optimisation wherein unnecessary REQUIRE_GAMEDATA() checks can elided at compile time whenever the gamedata is known up-front to always exist in supported games. The DEF_FEAT_CVAR macro family meanwhile makes it easier to manage the lifecycle of cvars/ccmds, with less manual registering, unhiding and such. Originally I was going to try and just hack these features into the existing codegen abomination, but it just got too terrible. This rewrite should make it easier to continue tweaking codegen behaviour in future. It also has slightly better error messages. --- test/hook.test.c | 51 +++++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) (limited to 'test') diff --git a/test/hook.test.c b/test/hook.test.c index a2447cc..9e7cfa9 100644 --- a/test/hook.test.c +++ b/test/hook.test.c @@ -12,7 +12,7 @@ #include #include -// stubs +// stub void con_warn(const char *msg, ...) { va_list l; va_start(l, msg); @@ -20,43 +20,38 @@ void con_warn(const char *msg, ...) { va_end(l); } -__attribute__((noinline)) -static int some_function(int a, int b) { return a + b; } -static int (*orig_some_function)(int, int); -static int some_hook(int a, int b) { - return orig_some_function(a, b) + 5; -} -__attribute__((noinline)) -static int other_function(int a, int b) { return a - b; } -static int (*orig_other_function)(int, int); -static int other_hook(int a, int b) { - return orig_other_function(a, b) + 5; -} +typedef int (*testfunc)(int, int); + +__attribute__((noinline)) static int func1(int a, int b) { return a + b; } +static int (*orig_func1)(int, int); +static int hook1(int a, int b) { return orig_func1(a, b) + 5; } + +__attribute__((noinline)) static int func2(int a, int b) { return a - b; } +static int (*orig_func2)(int, int); +static int hook2(int a, int b) { return orig_func2(a, b) + 5; } TEST("Inline hooks should be able to wrap the original function") { if (!hook_init()) return false; - orig_some_function = hook_inline(&some_function, &some_hook); - if (!orig_some_function) return false; - return some_function(5, 5) == 15; + orig_func1 = (testfunc)hook_inline((void *)&func1, (void *)&hook1); + if (!orig_func1) return false; + return func1(5, 5) == 15; } TEST("Inline hooks should be removable again") { if (!hook_init()) return false; - orig_some_function = hook_inline(&some_function, &some_hook); - if (!orig_some_function) return false; - unhook_inline(orig_some_function); - return some_function(5, 5) == 10; + orig_func1 = (testfunc)hook_inline((void *)&func1, (void *)&hook1); + if (!orig_func1) return false; + unhook_inline((void *)orig_func1); + return func1(5, 5) == 10; } -TEST("Multiple functions should be able to be inline hooked at once") { +TEST("Multiple functions should be able to be inline-hooked at once") { if (!hook_init()) return false; - orig_some_function = hook_inline(&some_function, &some_hook); - if (!orig_some_function) return false; - - orig_other_function = hook_inline(&other_function, &other_hook); - if (!orig_other_function) return false; - - return other_function(5, 5) == 5; + orig_func1 = (testfunc)hook_inline((void *)&func1, (void *)&hook1); + if (!orig_func1) return false; + orig_func2 = (testfunc)hook_inline((void *)&func2, (void *)&hook2); + if (!orig_func2) return false; + return func2(5, 5) == 5; } #endif -- cgit v1.2.3-54-g00ecf