Merge pull request #152 from diamante0018/add-cheat-cmds

[Script/Command] Add back cheat commands and gsc functions
This commit is contained in:
Dss0 2021-12-09 20:29:29 +01:00 committed by GitHub
commit 060eb4baaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 334 additions and 102 deletions

View File

@ -106,6 +106,7 @@ namespace Components
Loader::Register(new TextRenderer());
Loader::Register(new Movement());
Loader::Register(new Elevators());
Loader::Register(new ClientCommand());
Loader::Register(new Client());

View File

@ -134,6 +134,7 @@ namespace Components
#include "Modules/TextRenderer.hpp"
#include "Modules/Movement.hpp"
#include "Modules/Elevators.hpp"
#include "Modules/ClientCommand.hpp"
#include "Modules/Gamepad.hpp"
#include "Modules/Client.hpp"

View File

@ -0,0 +1,282 @@
#include "STDInclude.hpp"
namespace Components
{
std::unordered_map<std::string, Utils::Slot<ClientCommand::Callback>> ClientCommand::FunctionMap;
bool ClientCommand::CheatsOk(const Game::gentity_s* ent)
{
const auto entNum = ent->s.number;
if (!Dvar::Var("sv_cheats").get<bool>())
{
Logger::Print("CheatsOk: cheats are disabled!\n");
Game::SV_GameSendServerCommand(entNum, 0, Utils::String::VA("%c \"GAME_CHEATSNOTENABLED\"", 0x65));
return false;
}
if (ent->health < 1)
{
Logger::Print("CheatsOk: entity %u must be alive to use this command!\n", entNum);
Game::SV_GameSendServerCommand(entNum, 0, Utils::String::VA("%c \"GAME_MUSTBEALIVECOMMAND\"", 0x65));
return false;
}
return true;
}
bool ClientCommand::CallbackHandler(Game::gentity_s* ent, const char* cmd)
{
const auto command = Utils::String::ToLower(cmd);
if (ClientCommand::FunctionMap.find(command) != ClientCommand::FunctionMap.end())
{
ClientCommand::FunctionMap[command](ent);
return true;
}
return false;
}
void ClientCommand::Add(const char* name, Utils::Slot<Callback> callback)
{
const auto command = Utils::String::ToLower(name);
ClientCommand::FunctionMap[command] = callback;
}
void ClientCommand::ClientCommandStub(const int clientNum)
{
char cmd[1024]{};
const auto entity = &Game::g_entities[clientNum];
if (entity->client == nullptr)
{
Logger::Print("ClientCommand: client %d is not fully in game yet\n", clientNum);
return;
}
Game::SV_Cmd_ArgvBuffer(0, cmd, sizeof(cmd));
if (!ClientCommand::CallbackHandler(entity, cmd))
{
// If no callback was found call original game function
Utils::Hook::Call<void(const int)>(0x416790)(clientNum);
}
}
void ClientCommand::AddCheatCommands()
{
ClientCommand::Add("noclip", [](Game::gentity_s* ent)
{
if (!ClientCommand::CheatsOk(ent))
return;
ent->client->flags ^= Game::PLAYER_FLAG_NOCLIP;
const auto entNum = ent->s.number;
Logger::Print("Noclip toggled for entity %u\n", entNum);
Game::SV_GameSendServerCommand(entNum, 0, Utils::String::VA("%c \"%s\"", 0x65,
(ent->client->flags & Game::PLAYER_FLAG_NOCLIP) ? "GAME_NOCLIPON" : "GAME_NOCLIPOFF"));
});
ClientCommand::Add("ufo", [](Game::gentity_s* ent)
{
if (!ClientCommand::CheatsOk(ent))
return;
ent->client->flags ^= Game::PLAYER_FLAG_UFO;
const auto entNum = ent->s.number;
Logger::Print("UFO toggled for entity %u\n", entNum);
Game::SV_GameSendServerCommand(entNum, 0, Utils::String::VA("%c \"%s\"", 0x65,
(ent->client->flags & Game::PLAYER_FLAG_UFO) ? "GAME_UFOON" : "GAME_UFOOFF"));
});
ClientCommand::Add("god", [](Game::gentity_s* ent)
{
if (!ClientCommand::CheatsOk(ent))
return;
ent->flags ^= Game::FL_GODMODE;
const auto entNum = ent->s.number;
Logger::Print("God toggled for entity %u\n", entNum);
Game::SV_GameSendServerCommand(entNum, 0, Utils::String::VA("%c \"%s\"", 0x65,
(ent->flags & Game::FL_GODMODE) ? "GAME_GODMODE_ON" : "GAME_GODMODE_OFF"));
});
ClientCommand::Add("demigod", [](Game::gentity_s* ent)
{
if (!ClientCommand::CheatsOk(ent))
return;
ent->flags ^= Game::FL_DEMI_GODMODE;
const auto entNum = ent->s.number;
Logger::Print("Demigod toggled for entity %u\n", entNum);
Game::SV_GameSendServerCommand(entNum, 0, Utils::String::VA("%c \"%s\"", 0x65,
(ent->flags & Game::FL_DEMI_GODMODE) ? "GAME_DEMI_GODMODE_ON" : "GAME_DEMI_GODMODE_OFF"));
});
ClientCommand::Add("notarget", [](Game::gentity_s* ent)
{
if (!ClientCommand::CheatsOk(ent))
return;
ent->flags ^= Game::FL_NOTARGET;
const auto entNum = ent->s.number;
Logger::Print("Notarget toggled for entity %u\n", entNum);
Game::SV_GameSendServerCommand(entNum, 0, Utils::String::VA("%c \"%s\"", 0x65,
(ent->flags & Game::FL_NOTARGET) ? "GAME_NOTARGETON" : "GAME_NOTARGETOFF"));
});
}
void ClientCommand::AddScriptFunctions()
{
Script::AddFunction("Noclip", [](Game::scr_entref_t entref) // gsc: Noclip(<optional int toggle>);
{
if (entref >= Game::MAX_GENTITIES || Game::g_entities[entref].client == nullptr)
{
Game::Scr_Error(Utils::String::VA("^1NoClip: entity %u is not a client\n", entref));
return;
}
if (Game::Scr_GetNumParam() == 1u && Game::Scr_GetType(0) == Game::VAR_INTEGER)
{
if (Game::Scr_GetInt(0))
{
Game::g_entities[entref].client->flags |= Game::PLAYER_FLAG_NOCLIP;
}
else
{
Game::g_entities[entref].client->flags &= ~Game::PLAYER_FLAG_NOCLIP;
}
}
else
{
Game::g_entities[entref].client->flags ^= Game::PLAYER_FLAG_NOCLIP;
}
});
Script::AddFunction("Ufo", [](Game::scr_entref_t entref) // gsc: Ufo(<optional int toggle>);
{
if (entref >= Game::MAX_GENTITIES || Game::g_entities[entref].client == nullptr)
{
Game::Scr_Error(Utils::String::VA("^1Ufo: entity %u is not a client\n", entref));
return;
}
if (Game::Scr_GetNumParam() == 1u && Game::Scr_GetType(0) == Game::VAR_INTEGER)
{
if (Game::Scr_GetInt(0))
{
Game::g_entities[entref].client->flags |= Game::PLAYER_FLAG_UFO;
}
else
{
Game::g_entities[entref].client->flags &= ~Game::PLAYER_FLAG_UFO;
}
}
else
{
Game::g_entities[entref].client->flags ^= Game::PLAYER_FLAG_UFO;
}
});
Script::AddFunction("God", [](Game::scr_entref_t entref) // gsc: God(<optional int toggle>);
{
if (entref >= Game::MAX_GENTITIES)
{
Game::Scr_Error(Utils::String::VA("^1God: entity %u is out of bounds\n", entref));
return;
}
if (Game::Scr_GetNumParam() == 1u && Game::Scr_GetType(0) == Game::VAR_INTEGER)
{
if (Game::Scr_GetInt(0))
{
Game::g_entities[entref].flags |= Game::FL_GODMODE;
}
else
{
Game::g_entities[entref].flags &= ~Game::FL_GODMODE;
}
}
else
{
Game::g_entities[entref].flags ^= Game::FL_GODMODE;
}
});
Script::AddFunction("Demigod", [](Game::scr_entref_t entref) // gsc: Demigod(<optional int toggle>);
{
if (entref >= Game::MAX_GENTITIES)
{
Game::Scr_Error(Utils::String::VA("^1Demigod: entity %u is out of bounds\n", entref));
return;
}
if (Game::Scr_GetNumParam() == 1u && Game::Scr_GetType(0) == Game::VAR_INTEGER)
{
if (Game::Scr_GetInt(0))
{
Game::g_entities[entref].flags |= Game::FL_DEMI_GODMODE;
}
else
{
Game::g_entities[entref].flags &= ~Game::FL_DEMI_GODMODE;
}
}
else
{
Game::g_entities[entref].flags ^= Game::FL_DEMI_GODMODE;
}
});
Script::AddFunction("Notarget", [](Game::scr_entref_t entref) // gsc: Notarget(<optional int toggle>);
{
if (entref >= Game::MAX_GENTITIES)
{
Game::Scr_Error(Utils::String::VA("^1Notarget: entity %u is out of bounds\n", entref));
return;
}
if (Game::Scr_GetNumParam() == 1u && Game::Scr_GetType(0) == Game::VAR_INTEGER)
{
if (Game::Scr_GetInt(0))
{
Game::g_entities[entref].flags |= Game::FL_NOTARGET;
}
else
{
Game::g_entities[entref].flags &= ~Game::FL_NOTARGET;
}
}
else
{
Game::g_entities[entref].flags ^= Game::FL_NOTARGET;
}
});
}
ClientCommand::ClientCommand()
{
// Hook call to ClientCommand in SV_ExecuteClientCommand so we may add custom commands
Utils::Hook(0x6259FA, ClientCommand::ClientCommandStub, HOOK_CALL).install()->quick();
ClientCommand::AddCheatCommands();
ClientCommand::AddScriptFunctions();
}
ClientCommand::~ClientCommand()
{
ClientCommand::FunctionMap.clear();
}
}

View File

@ -0,0 +1,23 @@
#pragma once
namespace Components
{
class ClientCommand : public Component
{
public:
typedef void(Callback)(Game::gentity_s* entity);
ClientCommand();
~ClientCommand();
static void Add(const char* name, Utils::Slot<Callback> callback);
static bool CheatsOk(const Game::gentity_s* ent);
private:
static std::unordered_map<std::string, Utils::Slot<Callback>> FunctionMap;
static bool CallbackHandler(Game::gentity_s* ent, const char* cmd);
static void ClientCommandStub(const int clientNum);
static void AddCheatCommands();
static void AddScriptFunctions();
};
}

View File

@ -164,55 +164,6 @@ namespace Components
static int toastDurationMedium = 2500;
static int toastDurationLong = 5000;
// Disable native noclip command
Utils::Hook::Nop(0x474846, 5);
Command::Add("noclip", [](Command::Params*)
{
int clientNum = Game::CG_GetClientNum();
if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client)
{
Logger::Print("You are not hosting a match!\n");
Toast::Show("cardicon_stop", "Error", "You are not hosting a match!", toastDurationMedium);
return;
}
if (!Dvar::Var("sv_cheats").get<bool>())
{
Logger::Print("Cheats disabled!\n");
Toast::Show("cardicon_stop", "Error", "Cheats disabled!", toastDurationMedium);
return;
}
Game::g_entities[clientNum].client->flags ^= Game::PLAYER_FLAG_NOCLIP;
Logger::Print("Noclip toggled\n");
Toast::Show("cardicon_abduction", "Success", "Noclip toggled", toastDurationShort);
});
Command::Add("ufo", [](Command::Params*)
{
int clientNum = Game::CG_GetClientNum();
if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client)
{
Logger::Print("You are not hosting a match!\n");
Toast::Show("cardicon_stop", "Error", "You are not hosting a match!", toastDurationMedium);
return;
}
if (!Dvar::Var("sv_cheats").get<bool>())
{
Logger::Print("Cheats disabled!\n");
Toast::Show("cardicon_stop", "Error", "Cheats disabled!", toastDurationMedium);
return;
}
Game::g_entities[clientNum].client->flags ^= Game::PLAYER_FLAG_UFO;
Logger::Print("UFO toggled\n");
Toast::Show("cardicon_abduction", "Success", "UFO toggled", toastDurationShort);
});
Command::Add("setviewpos", [](Command::Params* params)
{
int clientNum = Game::CG_GetClientNum();
@ -252,9 +203,7 @@ namespace Components
Game::TeleportPlayer(&Game::g_entities[clientNum], pos, orientation);
//Logging that will spam the console and screen if people use cinematics
//Logger::Print("Successfully teleported player!\n");
//Toast::Show("cardicon_abduction", "Success", "You have been teleported!", toastDurationShort);
// Logging will spam the console and screen if people use cinematics
});
Command::Add("openLink", [](Command::Params* params)

View File

@ -635,56 +635,6 @@ namespace Components
{
Script::ScriptStorage.clear();
});
Script::AddFunction("Noclip", [](Game::scr_entref_t entref)
{
if (entref >= Game::MAX_GENTITIES || Game::g_entities[entref].client == nullptr)
{
Game::Scr_Error(Utils::String::VA("^1NoClip: entity %u is not a client\n", entref));
return;
}
if (Game::Scr_GetNumParam() == 1 && Game::Scr_GetType(0) == Game::VAR_INTEGER)
{
if (Game::Scr_GetInt(0))
{
Game::g_entities[entref].client->flags |= Game::PLAYER_FLAG_NOCLIP;
}
else
{
Game::g_entities[entref].client->flags &= ~Game::PLAYER_FLAG_NOCLIP;
}
}
else
{
Game::g_entities[entref].client->flags ^= Game::PLAYER_FLAG_NOCLIP;
}
});
Script::AddFunction("Ufo", [](Game::scr_entref_t entref)
{
if (entref >= Game::MAX_GENTITIES || Game::g_entities[entref].client == nullptr)
{
Game::Scr_Error(Utils::String::VA("^1Ufo: entity %u is not a client\n", entref));
return;
}
if (Game::Scr_GetNumParam() == 1 && Game::Scr_GetType(0) == Game::VAR_INTEGER)
{
if (Game::Scr_GetInt(0))
{
Game::g_entities[entref].client->flags |= Game::PLAYER_FLAG_UFO;
}
else
{
Game::g_entities[entref].client->flags &= ~Game::PLAYER_FLAG_UFO;
}
}
else
{
Game::g_entities[entref].client->flags ^= Game::PLAYER_FLAG_UFO;
}
});
}
Script::Script()

View File

@ -314,6 +314,7 @@ namespace Game
SV_GameSendServerCommand_t SV_GameSendServerCommand = SV_GameSendServerCommand_t(0x4BC3A0);
SV_Cmd_TokenizeString_t SV_Cmd_TokenizeString = SV_Cmd_TokenizeString_t(0x4B5780);
SV_Cmd_EndTokenizedString_t SV_Cmd_EndTokenizedString = SV_Cmd_EndTokenizedString_t(0x464750);
SV_Cmd_ArgvBuffer_t SV_Cmd_ArgvBuffer = SV_Cmd_ArgvBuffer_t(0x40BB60);
SV_DirectConnect_t SV_DirectConnect = SV_DirectConnect_t(0x460480);
SV_SetConfigstring_t SV_SetConfigstring = SV_SetConfigstring_t(0x4982E0);
SV_Loaded_t SV_Loaded = SV_Loaded_t(0x4EE3E0);

View File

@ -750,6 +750,9 @@ namespace Game
typedef void(__cdecl * SV_Cmd_EndTokenizedString_t)();
extern SV_Cmd_EndTokenizedString_t SV_Cmd_EndTokenizedString;
typedef void(__cdecl* SV_Cmd_ArgvBuffer_t)(int arg, char* buf, int size);
extern SV_Cmd_ArgvBuffer_t SV_Cmd_ArgvBuffer;
typedef void(__cdecl * SV_SetConfigstring_t)(int index, const char* string);
extern SV_SetConfigstring_t SV_SetConfigstring;

View File

@ -189,6 +189,28 @@ namespace Game
ERR_MAPLOADERRORSUMMARY = 0x7
} errorParm_t;
enum entityFlag
{
FL_GODMODE = 0x1,
FL_DEMI_GODMODE = 0x2,
FL_NOTARGET = 0x4,
FL_NO_KNOCKBACK = 0x8,
FL_NO_RADIUS_DAMAGE = 0x10,
FL_SUPPORTS_LINKTO = 0x1000,
FL_NO_AUTO_ANIM_UPDATE = 0x2000,
FL_GRENADE_TOUCH_DAMAGE = 0x4000,
FL_STABLE_MISSILES = 0x20000,
FL_REPEAT_ANIM_UPDATE = 0x40000,
FL_VEHICLE_TARGET = 0x80000,
FL_GROUND_ENT = 0x100000,
FL_CURSOR_HINT = 0x200000,
FL_MISSILE_ATTRACTOR = 0x800000,
FL_WEAPON_BEING_GRABBED = 0x1000000,
FL_DELETE = 0x2000000,
FL_BOUNCE = 0x4000000,
FL_MOVER_SLIDE = 0x8000000
};
struct FxEffectDef;
struct pathnode_t;
struct pathnode_tree_t;