2 Copyright (C) 2020 Cloudwalk
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 hook_t *_Hook_Register(hook_t *hook, const char *name, void *func, unsigned int argc)
29 Con_Printf("Hook %s already registered\n",hook->name);
31 hook = (hook_t *)Mem_Alloc(hooks, sizeof(hook_t));
32 hook->name = Mem_Alloc(hooks, strlen(name) + 1);
33 hook->arg = Mem_Alloc(hooks, sizeof(hook_val_t) * argc);
35 memcpy(hook->name, name, strlen(name) + 1);
42 // Needs NULL pad to know when va_list ends.
43 hook_val_t *_Hook_Call(hook_t *hook, ... )
45 uintptr_t arg_ptr; // Align to platform size
50 return (hook_val_t *)NULL;
52 va_start(arg_list, hook);
54 arg_ptr = va_arg(arg_list,intptr_t);
56 if((void *)arg_ptr && !hook->argc)
59 // Loop until we encounter that NULL pad, but stop if we overflow.
60 while ((void *)arg_ptr != NULL && i != hook->argc)
64 hook->arg[i].val = arg_ptr;
65 arg_ptr = va_arg(arg_list,intptr_t);
71 // Should be fairly obvious why it's bad if args don't match
75 hook->ret.uval = (uintptr_t)hook->func(hook->arg);
79 return (hook_val_t *)NULL;
82 Sys_Error("Hook_Call: Attempt to call hook '%s' with incorrect number of arguments. Got %i, expected %i\n", hook->name, i, hook->argc);
84 Sys_Error("Hook_Call: Stack overflow calling hook '%s' (argc = %u)\n", hook->name, hook->argc);
90 hooks = Mem_AllocPool("hooks",0,NULL);