Add glColor4f hook

Add GL_HOOK and GL_UNHOOK macros
This commit is contained in:
8dcc 2023-07-25 14:20:38 +02:00
parent fdb5cdf356
commit b6cdb9a55d
2 changed files with 47 additions and 0 deletions

View File

@ -11,14 +11,20 @@ DECL_HOOK(CL_CreateMove);
DECL_HOOK(HUD_Redraw); DECL_HOOK(HUD_Redraw);
DECL_HOOK(StudioRenderModel); DECL_HOOK(StudioRenderModel);
DECL_HOOK(glColor4f);
bool hooks_init(void) { bool hooks_init(void) {
HOOK(i_client, CL_CreateMove); HOOK(i_client, CL_CreateMove);
HOOK(i_client, HUD_Redraw); HOOK(i_client, HUD_Redraw);
HOOK(i_studiomodelrenderer, StudioRenderModel); HOOK(i_studiomodelrenderer, StudioRenderModel);
GL_HOOK(glColor4f);
return true; return true;
} }
/*----------------------------------------------------------------------------*/
void h_CL_CreateMove(float frametime, usercmd_t* cmd, int active) { void h_CL_CreateMove(float frametime, usercmd_t* cmd, int active) {
ORIGINAL(CL_CreateMove, frametime, cmd, active); ORIGINAL(CL_CreateMove, frametime, cmd, active);
@ -33,6 +39,8 @@ void h_CL_CreateMove(float frametime, usercmd_t* cmd, int active) {
vec_clamp(cmd->viewangles); vec_clamp(cmd->viewangles);
} }
/*----------------------------------------------------------------------------*/
int h_HUD_Redraw(float time, int intermission) { int h_HUD_Redraw(float time, int intermission) {
int ret = ORIGINAL(HUD_Redraw, time, intermission); int ret = ORIGINAL(HUD_Redraw, time, intermission);
@ -54,7 +62,15 @@ int h_HUD_Redraw(float time, int intermission) {
return ret; return ret;
} }
/*----------------------------------------------------------------------------*/
void h_StudioRenderModel(void* this_ptr) { void h_StudioRenderModel(void* this_ptr) {
if (!chams(this_ptr)) if (!chams(this_ptr))
ORIGINAL(StudioRenderModel, this_ptr); ORIGINAL(StudioRenderModel, this_ptr);
} }
/*----------------------------------------------------------------------------*/
void h_glColor4f(GLfloat r, GLfloat g, GLfloat b, GLfloat a) {
ORIGINAL(glColor4f, r, g, b, a);
}

View File

@ -6,6 +6,8 @@
#include "sdk.h" #include "sdk.h"
#include <dlfcn.h> /* dlsym */
#include <GL/gl.h> /* GLFloat */
/* /*
* Table of prefixes: * Table of prefixes:
* prefix | meaning * prefix | meaning
@ -13,6 +15,7 @@
* *_t | typedef (function type) * *_t | typedef (function type)
* h_* | hook function (ours) * h_* | hook function (ours)
* ho_* | hook original (ptr to orig) * ho_* | hook original (ptr to orig)
* hp_* | hook pointer (pointer to function pointer)
* *
* *
* DECL_HOOK_EXTERN: Version for the header. typedef's the function pointer with * DECL_HOOK_EXTERN: Version for the header. typedef's the function pointer with
@ -43,6 +46,23 @@
* *
* ORIGINAL(CL_CreateMove, frametime, cmd, active); * ORIGINAL(CL_CreateMove, frametime, cmd, active);
* ho_CL_CreateMove(frametime, cmd, active); // Original * ho_CL_CreateMove(frametime, cmd, active); // Original
*
*
* GL_HOOK: Hooks a OpenGL function. Example:
*
* GL_HOOK(glColor4f);
* void** hp_glColor4f = (void**)dlsym(hw, "qglColor4f"); // Ptr
* ho_glColor4f = (glColor4f_t)(*hp_glColor4f); // Original from ptr
* *hp_glColor4f = (void*)h_glColor4f; // Set ptr to our func
* Note: ho_glColor4f and h_glColor4f sould be declared with DECL_HOOK_EXTERN
*
*
* GL_UNHOOK: Restores a OpenGL hook created by GL_HOOK. Example:
*
* GL_UNHOOK(glColor4f);
* void** hp_glColor4f = (void**)dlsym(hw, "qglColor4f"); // Ptr
* *hp_glColor4f = (void*)ho_glColor4f; // Set to original
*/ */
#define DECL_HOOK_EXTERN(type, name, ...) \ #define DECL_HOOK_EXTERN(type, name, ...) \
typedef type (*name##_t)(__VA_ARGS__); \ typedef type (*name##_t)(__VA_ARGS__); \
@ -57,6 +77,15 @@
#define ORIGINAL(name, ...) ho_##name(__VA_ARGS__); #define ORIGINAL(name, ...) ho_##name(__VA_ARGS__);
#define GL_HOOK(name) \
void** hp_##name = (void**)dlsym(hw, "q" #name); \
ho_##name = (name##_t)(*hp_##name); \
*hp_##name = (void*)h_##name;
#define GL_UNHOOK(name) \
void** hp_##name = (void**)dlsym(hw, "q" #name); \
*hp_##name = (void*)ho_##name;
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
bool hooks_init(void); bool hooks_init(void);
@ -64,4 +93,6 @@ DECL_HOOK_EXTERN(void, CL_CreateMove, float, usercmd_t*, int);
DECL_HOOK_EXTERN(int, HUD_Redraw, float, int); DECL_HOOK_EXTERN(int, HUD_Redraw, float, int);
DECL_HOOK_EXTERN(void, StudioRenderModel, void* this_ptr); DECL_HOOK_EXTERN(void, StudioRenderModel, void* this_ptr);
DECL_HOOK_EXTERN(void, glColor4f, GLfloat r, GLfloat g, GLfloat b, GLfloat a);
#endif /* HOOKS_H_ */ #endif /* HOOKS_H_ */