Files
goldsrc-cheat/src/main.c

118 lines
3.2 KiB
C
Raw Normal View History

2023-07-19 19:39:50 +02:00
#include <stdio.h>
#include <dlfcn.h>
#include "include/main.h"
#include "include/sdk.h"
#include "include/globals.h"
2023-07-21 13:44:53 +02:00
#include "include/cvars.h"
#include "include/hooks.h"
#include "include/util.h"
#include "include/game_detection.h"
2023-07-19 19:39:50 +02:00
2023-07-21 07:02:38 +02:00
static bool loaded = false;
__attribute__((constructor)) /* Entry point when injected */
void load(void) {
2023-09-19 10:38:01 -04:00
printf("goldsource-cheat injected!\n");
/* Initialize globals/interfaces */
if (!globals_init()) {
2023-09-19 10:38:01 -04:00
fprintf(stderr, "goldsource-cheat: load: error loading globals, aborting\n");
self_unload();
return;
}
2023-07-21 13:44:53 +02:00
/* Create cvars for settings */
if (!cvars_init()) {
2023-09-19 10:38:01 -04:00
fprintf(stderr, "goldsource-cheat: load: error creating cvars, aborting\n");
2023-07-21 13:44:53 +02:00
self_unload();
return;
}
/* Hook functions */
if (!hooks_init()) {
2023-09-19 10:38:01 -04:00
fprintf(stderr, "goldsource-cheat: load: error hooking functions, aborting\n");
self_unload();
return;
}
/* Get game version after injecting */
2023-09-19 18:54:15 -04:00
if (IsCS16()) {
i_engine->pfnClientCmd("play 'sound/radio/go.wav'");
}
else if (IsDayOfDefeat()) {
i_engine->pfnClientCmd("play 'sound/player/gersniper.wav'");
}
else if (IsTFC()) {
i_engine->pfnClientCmd("play 'sound/misc/party2.wav'");
}
2023-09-20 08:46:08 -04:00
else if (IsDeathmatchClassic()) {
i_engine->pfnClientCmd("play 'sound/items/suit.wav'");
}
2023-09-19 18:54:15 -04:00
else
{
i_engine->pfnClientCmd("play 'valve/sound/vox/suit.wav'");
}
2023-09-19 10:38:01 -04:00
i_engine->pfnClientCmd("echo \"goldsource-cheat loaded successfully!\"");
i_engine->pfnClientCmd("echo \"Deadzone rulez!\"");
i_engine->pfnClientCmd("echo \"https://git.deadzone.lol/Wizzard/goldsource-cheat\"");
GameType game = get_current_game();
switch(game) {
case GAME_HALFLIFE:
i_engine->pfnClientCmd("echo \"Detected Game: Half-Life 1\"");
break;
case GAME_CS16:
i_engine->pfnClientCmd("echo \"Detected Game: Counter-Strike 1.6\"");
break;
case GAME_DAY_OF_DEFEAT:
i_engine->pfnClientCmd("echo \"Detected Game: Day of Defeat\"");
break;
case GAME_TFC:
i_engine->pfnClientCmd("echo \"Detected Game: Team Fortress Classic\"");
break;
2023-09-20 08:46:08 -04:00
case GAME_DMC:
i_engine->pfnClientCmd("echo \"Detected Game: Deathmatch Classic\"");
break;
default:
i_engine->pfnClientCmd("echo \"Detected Game: Unknown Game\"");
break;
}
return;
2023-07-21 07:02:38 +02:00
loaded = true;
}
__attribute__((destructor)) /* Entry point when unloaded */
2023-08-01 19:53:43 +02:00
void unload(void) {
2023-07-21 13:58:47 +02:00
if (loaded) {
/* TODO: Remove our cvars */
2023-07-21 07:02:38 +02:00
globals_restore();
2023-07-25 20:37:52 +02:00
hooks_restore();
2023-07-25 14:28:03 +02:00
GL_UNHOOK(glColor4f); /* Manually restore OpenGL hooks here */
2023-07-21 13:58:47 +02:00
}
2023-07-21 07:02:38 +02:00
2023-09-19 10:38:01 -04:00
printf("goldsource-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 19:39:50 +02:00
}