Skip to content

Commit

Permalink
Cleaned C code from old unused code.
Browse files Browse the repository at this point in the history
  • Loading branch information
MinoMino committed Nov 3, 2015
1 parent 71a5858 commit e2d289a
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 169 deletions.
25 changes: 5 additions & 20 deletions commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
#include "pyminqlx.h"
#endif

uint64_t pending_slap = 0LL;
int slap_damages[64];
uint64_t pending_slay = 0LL;

void __cdecl SendServerCommand(void) {
SV_SendServerCommand(NULL, "%s\n", Cmd_Args());
}
Expand All @@ -30,6 +26,7 @@ void __cdecl RegularPrint(void) {
}

void __cdecl Slap(void) {
int dmg = 0;
int argc = Cmd_Argc();
if (argc < 2) {
Com_Printf("Usage: %s <client_id> [damage]\n", Cmd_Argv(0));
Expand All @@ -41,21 +38,18 @@ void __cdecl Slap(void) {
return;
}
else if (argc > 2)
slap_damages[i] = atoi(Cmd_Argv(2));
else
slap_damages[i] = 0;
dmg = atoi(Cmd_Argv(2));

if (g_entities[i].inuse && g_entities[i].health > 0) {
Com_Printf("Slapping...\n");
if (slap_damages[i])
SV_SendServerCommand(NULL, "print \"%s was slapped for %d damage!\n\"\n", svs->clients[i].name, slap_damages[i]);
if (dmg)
SV_SendServerCommand(NULL, "print \"%s was slapped for %d damage!\n\"\n", svs->clients[i].name, dmg);
else
SV_SendServerCommand(NULL, "print \"%s was slapped!\n\"\n", svs->clients[i].name);
//SetPendingPlayer(&pending_slap, i);
g_entities[i].client->ps.velocity[0] += RandomFloatWithNegative() * 200.0f;
g_entities[i].client->ps.velocity[1] += RandomFloatWithNegative() * 200.0f;
g_entities[i].client->ps.velocity[2] += 300.0f;
g_entities[i].health -= slap_damages[i]; // Will be 0 if argument wasn't passed.
g_entities[i].health -= dmg; // Will be 0 if argument wasn't passed.
if (g_entities[i].health > 0)
G_AddEvent(&g_entities[i], EV_PAIN, 99); // 99 health = pain100_1.wav
else
Expand All @@ -79,7 +73,6 @@ void __cdecl Slay(void) {
else if (g_entities[i].inuse && g_entities[i].health > 0) {
Com_Printf("Slaying player...\n");
SV_SendServerCommand(NULL, "print \"%s was slain!\n\"\n", svs->clients[i].name);
//SetPendingPlayer(&pending_slay, i);
DebugPrint("Slaying '%s'!\n", svs->clients[i].name);
g_entities[i].health = -40;
G_AddEvent(&g_entities[i], EV_GIB_PLAYER, g_entities[i].s.number);
Expand All @@ -88,14 +81,6 @@ void __cdecl Slay(void) {
Com_Printf("The player is currently not active.\n");
}

// These commands are usually not possible to use without being on the
// server as an admin, but we get around that by creating a fake
// client and calling the command as if the fake one did it.
void __cdecl AdminCommand(void) {
if (ExecuteAdminCommand(-1, Cmd_ArgsFrom(0)))
Com_Printf("Invalid admin command.\n");
}

#ifndef NOPY
void __cdecl PyCommand(void) {
if (!frame_handler) {
Expand Down
1 change: 0 additions & 1 deletion common.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ void DebugError(const char* fmt, const char* file, int line, const char* func, .
// Misc.
int GetPendingPlayer(uint64_t* players);
void SetPendingPlayer(uint64_t* players, int client_id);
int ExecuteAdminCommand(int client_id, const char* args);
float RandomFloat(void);
float RandomFloatWithNegative(void);
void* PatternSearch(void* address, size_t length, const char* pattern, const char* mask);
Expand Down
40 changes: 1 addition & 39 deletions dllmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,14 @@ SV_SetConfigstring_ptr SV_SetConfigstring;
SV_GetConfigstring_ptr SV_GetConfigstring;
SV_DropClient_ptr SV_DropClient;
Sys_SetModuleOffset_ptr Sys_SetModuleOffset;
va_ptr va;

// VM functions
G_RunFrame_ptr G_RunFrame;
G_AddEvent_ptr G_AddEvent;
G_InitGame_ptr G_InitGame;
CheckPrivileges_ptr CheckPrivileges;
ClientConnect_ptr ClientConnect;
ClientDisconnect_ptr ClientDisconnect;
GetClientName_ptr GetClientName;

// VM global variables.
gentity_t* g_entities;
Expand Down Expand Up @@ -250,13 +249,6 @@ static void SearchFunctions(void) {
}
else DebugPrint("Sys_SetModuleOffset: %p\n", Sys_SetModuleOffset);

va = (va_ptr)PatternSearchModule(&module, PTRN_VA, MASK_VA);
if (va == NULL) {
DebugPrint("ERROR: Unable to find va.\n");
failed = 1;
}
else DebugPrint("va: %p\n", va);

// Cmd_Argc is really small, making it hard to search for, so we use a reference to it instead.
Cmd_Argc = (Cmd_Argc_ptr)(*(int32_t*)OFFSET_RELP_CMD_ARGC + OFFSET_RELP_CMD_ARGC + 4);

Expand Down Expand Up @@ -305,14 +297,6 @@ void SearchVmFunctions(void) {
}
else DebugPrint("ClientDisconnect: %p\n", ClientDisconnect);

GetClientName = (GetClientName_ptr)PatternSearch((void*)((pint)qagame + 0xB000),
0xB0000, PTRN_GETCLIENTNAME, MASK_GETCLIENTNAME);
if (GetClientName == NULL) {
DebugPrint("ERROR: Unable to find GetClientName.\n");
failed = 1;
}
else DebugPrint("GetClientName: %p\n", GetClientName);

if (failed) {
DebugPrint("Exiting.\n");
exit(1);
Expand All @@ -333,27 +317,6 @@ void InitializeStatic(void) {
Cmd_AddCommand("print", RegularPrint);
Cmd_AddCommand("slap", Slap);
Cmd_AddCommand("slay", Slay);
// Slightly hacky way to make a bunch of admin commands work through the console.
/* sponge added these in an update. Keeping them around for now just in case.
Cmd_AddCommand("allready", AdminCommand);
Cmd_AddCommand("pause", AdminCommand);
Cmd_AddCommand("unpause", AdminCommand);
Cmd_AddCommand("lock", AdminCommand);
Cmd_AddCommand("unlock", AdminCommand);
Cmd_AddCommand("put", AdminCommand);
Cmd_AddCommand("mute", AdminCommand);
Cmd_AddCommand("unmute", AdminCommand);
Cmd_AddCommand("ban", AdminCommand);
Cmd_AddCommand("unban", AdminCommand);
Cmd_AddCommand("opsay", AdminCommand);
Cmd_AddCommand("addadmin", AdminCommand);
Cmd_AddCommand("addmod", AdminCommand);
Cmd_AddCommand("demote", AdminCommand);
Cmd_AddCommand("abort", AdminCommand);
Cmd_AddCommand("addscore", AdminCommand);
Cmd_AddCommand("addteamscore", AdminCommand);
Cmd_AddCommand("setmatchtime", AdminCommand);
*/
#ifndef NOPY
Cmd_AddCommand("py", PyCommand);
Cmd_AddCommand("pyrestart", RestartPython);
Expand Down Expand Up @@ -388,7 +351,6 @@ void InitializeVm(void) {
// Called after the game is initialized.
void InitializeCvars(void) {
sv_maxclients = Cvar_FindVar("sv_maxclients");
Cvar_GetLimit("sv_fps", "40", "10", "100000", 0x1000);

cvars_initialized = 1;
}
Expand Down
13 changes: 0 additions & 13 deletions hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,6 @@ void __cdecl My_ClientDisconnect(int clientNum) {
ClientDisconnect(clientNum);
}

char* __cdecl My_GetClientName(int clientNum) {
if (is_fake_admin)
return "An admin";

return GetClientName(clientNum);
}

void __cdecl My_SV_SetConfigstring(int index, char* value) {
// Indices 16 and 66X are spammed a ton every frame for some reason,
// so we add some exceptions for those. I don't think we should have any
Expand Down Expand Up @@ -257,12 +250,6 @@ void HookVm(void) {
failed = 1;
}

res = Hook((void*)GetClientName, My_GetClientName, (void*)&GetClientName);
if (res) {
DebugPrint("ERROR: Failed to hook GetClientName: %d\n", res);
failed = 1;
}

if (failed) {
DebugPrint("Exiting.\n");
exit(1);
Expand Down
34 changes: 0 additions & 34 deletions misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include "quake_common.h"
#include "maps_parser.h"

int is_fake_admin = 0;

/* Takes a 64-bit integer used as a bit field as flags for which player
* has an action pending, removes the flag and returns the client ID.
* The server only allows up to 64 players, so a 64-bit int covers it all.
Expand Down Expand Up @@ -35,38 +33,6 @@ void SetPendingPlayer(uint64_t* players, int client_id) {
*players |= 1LL << client_id;
}

int ExecuteAdminCommand(int client_id, const char* args) {
Cmd_TokenizeString(args);
char* cmd = Cmd_Argv(0);
gentity_t* executor;
if (client_id == -1)
executor = &fake_entity;
else
executor = &g_entities[client_id];

for (adminCmd_t* ac = admin_commands; ac->cmd; ac++) {
if (!strcmp(ac->cmd, cmd)) {
if (client_id == -1 && (!strcmp(ac->cmd, "opsay") || !strcmp(ac->cmd, "abort") ||
!strcmp(ac->cmd, "pause") || !strcmp(ac->cmd, "unpause") || !strcmp(ac->cmd, "timeout") ||
!strcmp(ac->cmd, "put") || !strcmp(ac->cmd, "timein")))
{
// The above commands prints the caller's name, so we tell our hooked "GetClientName"
// to print "An admin" instead when we use a fake client. Otherwise it'll try
// to access the name of a nonexsistent player and segfault.
is_fake_admin = 1;
ac->admin_func(executor);
is_fake_admin = 0;
}
else {
ac->admin_func(executor);
}
return 0;
}
}

return 1;
}

// (0.0f, 1.0f)
float RandomFloat(void) {
return (float)rand()/(float)RAND_MAX;
Expand Down
46 changes: 0 additions & 46 deletions python_embed.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,50 +367,6 @@ static PyObject* PyMinqlx_SetCvarLimit(PyObject* self, PyObject* args) {
Py_RETURN_NONE;
}

/*
* ================================================================
* admin_command
* ================================================================
*/

static PyObject* PyMinqlx_AdminCommand(PyObject* self, PyObject* args) {
int i;
char* cmd;
PyObject* client_id;
if (!PyArg_ParseTuple(args, "Os:admin_command", &client_id, &cmd))
return NULL;

if (client_id == Py_None) {
if (!ExecuteAdminCommand(-1, cmd))
Py_RETURN_NONE;
else {
PyErr_Format(PyExc_ValueError, "Invalid admin command.");
return NULL;
}
}
else if (PyLong_Check(client_id)) {
i = PyLong_AsLong(client_id);
if (i >= 0 && i < sv_maxclients->integer) {
if (svs->clients[i].state != CS_ACTIVE) {
PyErr_Format(PyExc_ValueError,
"client_id must be None or the ID of an active player.");
return NULL;
}
else if (!ExecuteAdminCommand(i, cmd))
Py_RETURN_NONE;
else {
PyErr_Format(PyExc_ValueError, "Invalid admin command.");
return NULL;
}
}
}

PyErr_Format(PyExc_ValueError,
"client_id needs to be a number from 0 to %d, or None.",
sv_maxclients->integer);
return NULL;
}

/*
* ================================================================
* kick
Expand Down Expand Up @@ -615,8 +571,6 @@ static PyMethodDef minqlxMethods[] = {
"Sets a cvar."},
{"set_cvar_limit", PyMinqlx_SetCvarLimit, METH_VARARGS,
"Sets a non-string cvar with a minimum and maximum value."},
{"admin_command", PyMinqlx_AdminCommand, METH_VARARGS,
"Executes an admin command."},
{"kick", PyMinqlx_Kick, METH_VARARGS,
"Kick a player and allowing the admin to supply a reason for it."},
{"console_print", PyMinqlx_ConsolePrint, METH_VARARGS,
Expand Down
16 changes: 0 additions & 16 deletions quake_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -1218,12 +1218,6 @@ typedef struct {
extern void* qagame;
extern void* qagame_dllentry;

// In order to execute admin commands that aren't meant to be used from the console, we
// need a fake client to fool the system into thinking a connected admin is doing it.
extern gentity_t fake_entity;
extern gclient_t fake_client;
extern int is_fake_admin;

// Additional key struct pointers.
extern server_t* sv;
extern serverStatic_t* svs;
Expand All @@ -1233,11 +1227,6 @@ extern adminCmd_t* admin_commands;
// Cvars.
extern cvar_t* sv_maxclients;

// Pending action bitfields and stuff.
extern uint64_t pending_slap;
extern int slap_damages[64];
extern uint64_t pending_slay;

// Internal QL function pointer types.
typedef void (__cdecl *Com_Printf_ptr)(char* fmt, ...);
typedef void (__cdecl *Cmd_AddCommand_ptr)(char* cmd, void* func);
Expand All @@ -1263,15 +1252,13 @@ typedef void (__cdecl *SV_DropClient_ptr)(client_t* drop, const char* reason);
typedef void (__cdecl *FS_Startup_ptr)(const char* gameName);
typedef void (__cdecl *Sys_SetModuleOffset_ptr)(char* moduleName, void* offset);
typedef void (__cdecl *SV_LinkEntity_ptr)(sharedEntity_t* gEnt);
typedef char* (__cdecl *va_ptr)(char *format, ...);
// VM functions.
typedef void (__cdecl *G_RunFrame_ptr)(int time);
typedef void (__cdecl *G_AddEvent_ptr)(gentity_t* ent, int event, int eventParm);
typedef void (__cdecl *G_InitGame_ptr)(int levelTime, int randomSeed, int restart);
typedef int (__cdecl *CheckPrivileges_ptr)(gentity_t* ent, char* cmd);
typedef char* (__cdecl *ClientConnect_ptr)(int clientNum, qboolean firstTime, qboolean isBot);
typedef void (__cdecl *ClientDisconnect_ptr)(int clientNum);
typedef char* (__cdecl *GetClientName_ptr)(int clientNum);

// Some of them are initialized by Initialize(), but not all of them necessarily.
extern Com_Printf_ptr Com_Printf;
Expand Down Expand Up @@ -1302,7 +1289,6 @@ extern G_InitGame_ptr G_InitGame;
extern CheckPrivileges_ptr CheckPrivileges;
extern ClientConnect_ptr ClientConnect;
extern ClientDisconnect_ptr ClientDisconnect;
extern GetClientName_ptr GetClientName;

// Server replacement functions for hooks.
void __cdecl My_Cmd_AddCommand(char* cmd, void* func);
Expand All @@ -1315,7 +1301,6 @@ void __cdecl My_G_RunFrame(int time);
void __cdecl My_G_InitGame(int levelTime, int randomSeed, int restart);
char* __cdecl My_ClientConnect(int clientNum, qboolean firstTime, qboolean isBot);
void __cdecl My_ClientDisconnect(int clientNum);
char* __cdecl My_GetClientName(int clientNum);
#endif

// Custom commands added using Cmd_AddCommand during initialization.
Expand All @@ -1324,7 +1309,6 @@ void __cdecl CenterPrint(void); // "cp"
void __cdecl RegularPrint(void); // "p"
void __cdecl Slap(void); // "slap"
void __cdecl Slay(void); // "slay"
void __cdecl AdminCommand(void); // All the admin commands.
#ifndef NOPY
// PyCommand is special. It'll serve as the handler for console commands added
// using Python. This means it can serve as the handler for a bunch of commands,
Expand Down

0 comments on commit e2d289a

Please sign in to comment.