goldsrc-cheat/src/main.c

74 lines
1.7 KiB
C
Raw Normal View History

2023-07-19 13:39:50 -04:00
#include <stdio.h>
#include <dlfcn.h>
#include "include/main.h"
#include "include/sdk.h"
#include "include/globals.h"
2023-07-21 07:44:53 -04:00
#include "include/cvars.h"
#include "include/hooks.h"
#include "include/util.h"
2023-07-19 13:39:50 -04:00
2023-07-21 01:02:38 -04:00
static bool loaded = false;
__attribute__((constructor)) /* Entry point when injected */
void load(void) {
printf("hl-cheat injected!\n");
/* Initialize globals/interfaces */
if (!globals_init()) {
fprintf(stderr, "hl-cheat: load: error loading globals, aborting\n");
self_unload();
return;
}
2023-07-21 07:44:53 -04:00
/* Create cvars for settings */
if (!cvars_init()) {
fprintf(stderr, "hl-cheat: load: error creating cvars, aborting\n");
self_unload();
return;
}
/* Hook functions */
if (!hooks_init()) {
fprintf(stderr, "hl-cheat: load: error hooking functions, aborting\n");
self_unload();
return;
}
/* Get game version after injecting */
this_game_id = get_cur_game();
i_engine->pfnClientCmd("echo \"hl-cheat loaded successfully!\"");
2023-07-21 01:02:38 -04:00
loaded = true;
}
__attribute__((destructor)) /* Entry point when unloaded */
void unload() {
2023-07-21 07:58:47 -04:00
if (loaded) {
/* TODO: Remove our cvars */
2023-07-21 01:02:38 -04:00
globals_restore();
2023-07-25 14:37:52 -04:00
hooks_restore();
2023-07-25 08:28:03 -04:00
GL_UNHOOK(glColor4f); /* Manually restore OpenGL hooks here */
2023-07-21 07:58:47 -04:00
}
2023-07-21 01:02:38 -04:00
printf("hl-cheat unloaded.\n\n");
}
void self_unload(void) {
/*
* RTLD_LAZY: If the symbol is never referenced, then it is never resolved.
* RTLD_NOLOAD: Don't load the shared object.
*/
void* self = dlopen("libhlcheat.so", RTLD_LAZY | RTLD_NOLOAD);
/* Close the call we just made to dlopen() */
dlclose(self);
/* Close the call our injector made */
dlclose(self);
2023-07-19 13:39:50 -04:00
}