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. --- src/ac.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'src/ac.c') diff --git a/src/ac.c b/src/ac.c index 7f3157c..e8aef8d 100644 --- a/src/ac.c +++ b/src/ac.c @@ -1,5 +1,5 @@ /* - * Copyright © 2024 Michael Smith + * Copyright © 2025 Michael Smith * Copyright © 2023 Willian Henrique * * Permission to use, copy, modify, and/or distribute this software for any @@ -32,13 +32,13 @@ #include "crypto.h" #include "democustom.h" #include "demorec.h" -#include "hook.h" #include "engineapi.h" #include "errmsg.h" #include "event.h" #include "feature.h" #include "gamedata.h" #include "gametype.h" +#include "hook.h" #include "intdefs.h" #include "langext.h" #include "mem.h" @@ -50,6 +50,7 @@ #include "x86util.h" FEATURE() +GAMESPECIFIC(L4D) // TODO(compat): wanna add support for more stuff, obviously! REQUIRE(bind) REQUIRE(democustom) REQUIRE_GAMEDATA(vtidx_GetDesktopResolution) @@ -291,7 +292,7 @@ static void hook_Key_Event(struct inputevent *ev) { //const char *desc[] = {"DOWN", "UP", "DBL"}; //const char desclen[] = {4, 2, 3}; switch (ev->type) { - CASES(BTNDOWN, BTNUP, BTNDOUBLECLICK):; + CASES(BTNDOWN, BTNUP, BTNDOUBLECLICK): // TODO(rta): do something interesting with button data //uchar buf[28], *p = buf; //msg_putasz4(p, 2); p += 1; @@ -378,24 +379,20 @@ HANDLE_EVENT(PluginUnloaded, void) { // TODO(rta): do something with plugin list here } -PREINIT { - return GAMETYPE_MATCHES(L4D); // TODO(compat): add more here obviously -} - INIT { - if_cold (!find_Key_Event()) return false; + if_cold (!find_Key_Event()) return FEAT_INCOMPAT; orig_Key_Event = (Key_Event_func)hook_inline((void *)orig_Key_Event, (void *)&hook_Key_Event); if_cold (!orig_Key_Event) { errmsg_errorsys("couldn't hook Key_Event function"); - return false; + return FEAT_FAIL; } #ifdef _WIN32 keybox = VirtualAlloc(0, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); if_cold (!keybox) { errmsg_errorsys("couldn't allocate memory for session state"); - return false; + return FEAT_FAIL; } if_cold (!VirtualLock(keybox, 4096)) { errmsg_errorsys("couldn't secure session state"); @@ -411,7 +408,7 @@ INIT { keybox = mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); if_cold (keybox == MAP_FAILED) { errmsg_errorstd("couldn't allocate memory for session state"); - return false; + return FEAT_FAIL; } // linux-specific madvise stuff (there are some equivalents in OpenBSD and // FreeBSD, if anyone's wondering, but we don't need to worry about those) @@ -432,7 +429,7 @@ INIT { // run of bytes memcpy(keybox->lbpub, lbpubkeys[LBPK_L4D], 32); } - return true; + return FEAT_OK; #ifdef _WIN32 e: WerUnregisterExcludedMemoryBlock(keybox); // this'd better not fail! @@ -441,7 +438,7 @@ e2: VirtualFree(keybox, 4096, MEM_RELEASE); e: munmap(keybox, 4096); #endif unhook_inline((void *)orig_Key_Event); - return false; + return FEAT_FAIL; } END { -- cgit v1.2.3-54-g00ecf