goldsrc-cheat/src/globals.c

91 lines
2.7 KiB
C
Raw Normal View History

#include <stdio.h>
2023-07-22 09:48:01 -04:00
#include <string.h>
#include <dlfcn.h>
#include <sys/mman.h> /* PROT_* */
#include "include/globals.h"
#include "include/sdk.h"
#include "include/util.h"
2023-07-30 14:05:12 -04:00
game_id this_game_id = HL;
vec3_t g_punchAngles = { 0, 0, 0 };
void* hw;
void** h_client;
DECL_INTF(cl_enginefunc_t, engine);
DECL_INTF(cl_clientfunc_t, client);
2023-07-20 14:40:34 -04:00
DECL_INTF(playermove_t, pmove);
DECL_INTF(engine_studio_api_t, enginestudio);
DECL_INTF(StudioModelRenderer_t, studiomodelrenderer);
2023-07-27 08:18:47 -04:00
/* Game struct with some useful info */
game_t* game_info;
2023-07-27 16:13:34 -04:00
/* Array of extra_player_info's for each player */
2023-07-28 10:31:21 -04:00
void* player_extra_info;
2023-07-27 16:13:34 -04:00
2023-07-20 17:30:44 -04:00
/* Updated in CL_CreateMove hook */
cl_entity_t* localplayer = NULL;
/*----------------------------------------------------------------------------*/
bool globals_init(void) {
/*
* Get handler for hw.so
* RTLD_LAZY: If the symbol is never referenced, then it is never resolved.
* RTLD_NOLOAD: Don't load the shared object.
*/
hw = dlopen("hw.so", RTLD_LAZY | RTLD_NOLOAD);
if (!hw) {
2023-07-20 11:31:16 -04:00
printf("hl-cheat: globals_init: can't open hw.so\n");
return false;
}
h_client = (void**)dlsym(hw, "hClientDLL");
if (!h_client) {
printf("hl-cheat: globals_init: can't find hClientDLL\n");
return false;
}
/* Get symbol addresses using dlsym and the handler we just opened */
i_engine = (cl_enginefunc_t*)dlsym(hw, "cl_enginefuncs");
i_client = (cl_clientfunc_t*)dlsym(hw, "cl_funcs");
i_pmove = *(playermove_t**)dlsym(hw, "pmove");
i_enginestudio = (engine_studio_api_t*)dlsym(hw, "engine_studio_api");
const char* SMR_STR = "g_StudioRenderer"; /* For clang-format */
i_studiomodelrenderer = *(StudioModelRenderer_t**)dlsym(*h_client, SMR_STR);
2023-07-28 10:31:21 -04:00
player_extra_info = dlsym(*h_client, "g_PlayerExtraInfo");
2023-07-27 16:13:34 -04:00
2023-07-27 08:18:47 -04:00
game_info = *(game_t**)dlsym(hw, "game");
if (!i_engine || !i_client || !i_pmove || !i_enginestudio ||
2023-07-27 08:18:47 -04:00
!i_studiomodelrenderer || !game_info) {
printf("hl-cheat: globals_init: couldn't load some symbols\n");
return false;
}
if (!protect_addr(i_studiomodelrenderer, PROT_READ | PROT_WRITE)) {
printf("hl-cheat: globals_init: couldn't unprotect address of SMR\n");
2023-07-20 17:19:50 -04:00
return false;
}
globals_store();
return true;
}
void globals_store(void) {
2023-07-22 09:48:01 -04:00
memcpy(&o_engine, i_engine, sizeof(cl_enginefunc_t));
memcpy(&o_client, i_client, sizeof(cl_clientfunc_t));
2023-07-25 08:24:50 -04:00
memcpy(&o_enginestudio, i_enginestudio, sizeof(engine_studio_api_t));
}
void globals_restore(void) {
2023-07-22 09:48:01 -04:00
memcpy(i_engine, &o_engine, sizeof(cl_enginefunc_t));
memcpy(i_client, &o_client, sizeof(cl_clientfunc_t));
2023-07-25 08:24:50 -04:00
memcpy(i_enginestudio, &o_enginestudio, sizeof(engine_studio_api_t));
}