Address some parts of review

This commit is contained in:
FutureRave 2022-01-23 19:32:20 +00:00
parent 6687d73f20
commit cfc540991c
No known key found for this signature in database
GPG Key ID: E883E2BC9657D955
12 changed files with 188 additions and 140 deletions

View File

@ -107,8 +107,7 @@ namespace Components
Loader::Register(new Movement());
Loader::Register(new Elevators());
Loader::Register(new ClientCommand());
Loader::Register(new Client());
Loader::Register(new ScriptExtension());
Loader::Pregame = false;
}

View File

@ -137,4 +137,4 @@ namespace Components
#include "Modules/ClientCommand.hpp"
#include "Modules/Gamepad.hpp"
#include "Modules/Client.hpp"
#include "Modules/ScriptExtension.hpp"

View File

@ -1,74 +1,51 @@
#include "STDInclude.hpp"
#define KEY_MASK_FIRE 1
#define KEY_MASK_SPRINT 2
#define KEY_MASK_MELEE 4
#define KEY_MASK_RELOAD 16
#define KEY_MASK_LEANLEFT 64
#define KEY_MASK_LEANRIGHT 128
#define KEY_MASK_PRONE 256
#define KEY_MASK_CROUCH 512
#define KEY_MASK_JUMP 1024
#define KEY_MASK_ADS_MODE 2048
#define KEY_MASK_TEMP_ACTION 4096
#define KEY_MASK_HOLDBREATH 8192
#define KEY_MASK_FRAG 16384
#define KEY_MASK_SMOKE 32768
#define KEY_MASK_NIGHTVISION 262144
#define KEY_MASK_ADS 524288
#define KEY_MASK_USE 0x28
#define MAX_G_BOTAI_ENTRIES 18
namespace Components
{
std::vector<std::string> Bots::BotNames;
typedef struct BotMovementInfo_t
struct BotMovementInfo_t
{
/* Actions */
int buttons;
/* Movement */
int buttons; // Actions
int8_t forward;
int8_t right;
/* Weapon */
uint16_t weapon;
} BotMovementInfo_t;
};
static BotMovementInfo_t g_botai[MAX_G_BOTAI_ENTRIES];
static BotMovementInfo_t g_botai[18];
struct BotAction_t
struct BotAction
{
const char* action;
int key;
};
static const BotAction_t BotActions[] =
static const BotAction BotActions[] =
{
{ "gostand", KEY_MASK_JUMP },
{ "gocrouch", KEY_MASK_CROUCH },
{ "goprone", KEY_MASK_PRONE },
{ "fire", KEY_MASK_FIRE },
{ "melee", KEY_MASK_MELEE },
{ "frag", KEY_MASK_FRAG },
{ "smoke", KEY_MASK_SMOKE },
{ "reload", KEY_MASK_RELOAD },
{ "sprint", KEY_MASK_SPRINT },
{ "leanleft", KEY_MASK_LEANLEFT },
{ "leanright", KEY_MASK_LEANRIGHT },
{ "ads", KEY_MASK_ADS_MODE },
{ "holdbreath", KEY_MASK_HOLDBREATH },
{ "use", KEY_MASK_USE },
{ "0", 8 },
{ "1", 32 },
{ "2", 65536 },
{ "3", 131072 },
{ "4", 1048576 },
{ "5", 2097152 },
{ "6", 4194304 },
{ "7", 8388608 },
{ "8", 16777216 },
{ "9", 33554432 },
{ "gostand", Bots::JUMP },
{ "gocrouch", Bots::CROUCH },
{ "goprone", Bots::PRONE },
{ "fire", Bots::FIRE },
{ "melee", Bots::FIRE},
{ "frag", Bots::THROW_EQUIPMENT },
{ "smoke", Bots::THROW_SPECIAL },
{ "reload", Bots::RELOAD },
{ "sprint", Bots::SPRINT },
{ "leanleft", Bots::LEAN_LEFT},
{ "leanright", Bots::LEAN_RIGHT },
{ "ads", Bots::ADS_MODE },
{ "holdbreath", Bots::HOLD_BREATH },
{ "use", Bots::USE },
{ "0", Bots::NUM_0 },
{ "1", Bots::NUM_1 },
{ "2", Bots::NUM_2 },
{ "3", Bots::NUM_3 },
{ "4", Bots::NUM_4},
{ "5", Bots::NUM_5 },
{ "6", Bots::NUM_6 },
{ "7", Bots::NUM_7 },
{ "8", Bots::NUM_8 },
{ "9", Bots::NUM_9 },
};
void Bots::BuildConnectString(char* buffer, const char* connectString, int num, int, int protocol, int checksum, int statVer, int statStuff, int port)
@ -110,9 +87,9 @@ namespace Components
_snprintf_s(buffer, 0x400, _TRUNCATE, connectString, num, botName, protocol, checksum, statVer, statStuff, port);
}
void Bots::Spawn(int count)
void Bots::Spawn(unsigned int count)
{
for (auto i = 0; i < count; ++i)
for (auto i = 0u; i < count; ++i)
{
Scheduler::OnDelay([]()
{
@ -188,6 +165,12 @@ namespace Components
{
const auto* weapon = Game::Scr_GetString(0);
if (weapon == nullptr)
{
Game::Scr_ParamError(0, "^1BotWeapon: Illegal parameter!\n");
return;
}
const auto* gentity = Script::GetEntFromEntRef(entref);
const auto* client = Script::GetClientFromEnt(gentity);
@ -204,13 +187,19 @@ namespace Components
}
const auto weapId = Game::G_GetWeaponIndexForName(weapon);
g_botai[entref.entnum].weapon = static_cast<uint16_t>(weapId);
g_botai[entref.entnum].weapon = static_cast<unsigned short>(weapId);
});
Script::AddFunction("BotAction", [](Game::scr_entref_t entref) // Usage: <bot> BotAction(<str action>);
{
const auto* action = Game::Scr_GetString(0);
if (action == nullptr)
{
Game::Scr_ParamError(0, "^1BotAction: Illegal parameter!\n");
return;
}
const auto* gentity = Script::GetEntFromEntRef(entref);
const auto* client = Script::GetClientFromEnt(gentity);
@ -226,9 +215,9 @@ namespace Components
return;
}
for (auto i = 0u; i < sizeof(BotActions) / sizeof(BotAction_t); ++i)
for (auto i = 0u; i < std::extent_v<decltype(BotActions)>; ++i)
{
if (strcmp(&action[1], BotActions[i].action))
if (strcmp(&action[1], BotActions[i].action) != 0)
continue;
if (action[0] == '+')
@ -256,17 +245,8 @@ namespace Components
return;
}
if (forwardInt > 127)
forwardInt = 127;
if (forwardInt < -127)
forwardInt = -127;
if (rightInt > 127)
rightInt = 127;
if (rightInt < -127)
rightInt = -127;
forwardInt = std::clamp(forwardInt, -128, 127);
rightInt = std::clamp(rightInt, -128, 127);
g_botai[entref.entnum].forward = static_cast<int8_t>(forwardInt);
g_botai[entref.entnum].right = static_cast<int8_t>(rightInt);
@ -286,10 +266,10 @@ namespace Components
Utils::Hook(0x627241, 0x4BB9B0, HOOK_CALL).install()->quick();
// Zero the bot command array
for (auto i = 0; i < MAX_G_BOTAI_ENTRIES; i++)
for (auto i = 0u; i < std::extent_v<decltype(g_botai)>; i++)
{
g_botai[i] = {0};
g_botai[i].weapon = 1; // Prevent the bots from defaulting to the 'none' weapon
g_botai[i].weapon = 1; // Prevent the bots from defaulting to the 'none' weapon
}
// Have the bots perform the command every server frame
@ -325,17 +305,30 @@ namespace Components
Command::Add("spawnBot", [](Command::Params* params)
{
auto count = 1;
auto count = 1u;
if (params->length() > 1)
{
if (params->get(1) == "all"s)
{
count = *Game::svs_numclients;
}
else
count = std::atoi(params->get(1));
{
char* endptr;
const auto* input = params->get(1);
count = std::strtoul(input, &endptr, 10);
if (input == endptr)
{
Logger::Print("Warning: %s is not a valid input\n"
"Usage: %s optional <number of bots> or optional <all>\n",
input, params->get(0));
}
}
}
count = std::min(*Game::svs_numclients, count);
count = std::min(static_cast<unsigned int>(*Game::svs_numclients), count);
// Check if ingame and host
if (!Game::SV_Loaded())
@ -353,9 +346,4 @@ namespace Components
Bots::AddMethods();
}
Bots::~Bots()
{
Bots::BotNames.clear();
}
}

View File

@ -6,14 +6,46 @@ namespace Components
{
public:
Bots();
~Bots();
enum PlayerKeyFlag
{
FIRE = 0x1,
SPRINT = 0x2,
MELEE = 0x4,
RELOAD = 0x10,
LEAN_LEFT = 0x40,
LEAN_RIGHT = 0x80,
PRONE = 0x100,
CROUCH = 0x200,
JUMP = 0x400,
ADS_MODE = 0x800,
TEMP_ACTION = 0x1000,
HOLD_BREATH = 0x2000,
THROW_EQUIPMENT = 0x4000,
THROW_SPECIAL = 0x8000,
NIGHT_VISION = 0x40000,
ADS = 0x80000,
NUM_0 = 0x8,
NUM_1 = 0x20,
NUM_2 = 0x10000,
NUM_3 = 0x20000,
NUM_4 = 0x100000,
NUM_5 = 0x200000,
NUM_6 = 0x400000,
NUM_7 = 0x800000,
NUM_8 = 0x1000000,
NUM_9 = 0x2000000,
USE = 0x28,
};
private:
static std::vector<std::string> BotNames;
static void BuildConnectString(char* buffer, const char* connectString, int num, int, int protocol, int checksum, int statVer, int statStuff, int port);
static void Spawn(int count);
static void Spawn(unsigned int count);
static void AddMethods();
};

View File

@ -150,7 +150,7 @@ namespace Components
return;
}
if (Game::Scr_GetNumParam() == 1u)
if (Game::Scr_GetNumParam() >= 1u)
{
if (Game::Scr_GetInt(0))
{
@ -177,7 +177,7 @@ namespace Components
return;
}
if (Game::Scr_GetNumParam() == 1u)
if (Game::Scr_GetNumParam() >= 1u)
{
if (Game::Scr_GetInt(0))
{
@ -198,7 +198,7 @@ namespace Components
{
auto* ent = Script::GetEntFromEntRef(entref);
if (Game::Scr_GetNumParam() == 1u)
if (Game::Scr_GetNumParam() >= 1u)
{
if (Game::Scr_GetInt(0))
{
@ -219,7 +219,7 @@ namespace Components
{
auto* ent = Script::GetEntFromEntRef(entref);
if (Game::Scr_GetNumParam() == 1u)
if (Game::Scr_GetNumParam() >= 1u)
{
if (Game::Scr_GetInt(0))
{
@ -240,7 +240,7 @@ namespace Components
{
auto* ent = Script::GetEntFromEntRef(entref);
if (Game::Scr_GetNumParam() == 1u)
if (Game::Scr_GetNumParam() >= 1u)
{
if (Game::Scr_GetInt(0))
{

View File

@ -964,13 +964,20 @@ namespace Components
Download::ScriptDownloads.clear();
});
Script::AddFunction("httpGet", [](Game::scr_entref_t)
Script::AddFunction("HttpGet", [](Game::scr_entref_t)
{
if (!Dedicated::IsEnabled() && !Flags::HasFlag("scriptablehttp")) return;
if (Game::Scr_GetNumParam() < 1u) return;
std::string url = Game::Scr_GetString(0);
unsigned int object = Game::AllocObject();
if (url.empty())
{
Game::Scr_ParamError(0, "^1HttpGet: Illegal parameters!\n");
return;
}
auto object = Game::AllocObject();
Game::Scr_AddObject(object);

View File

@ -50,24 +50,25 @@ namespace Components
const auto developer = Dvar::Var("developer").get<int>();
// Allow error messages to be printed if developer mode is on
// Should check scrVarPub.developer but it's absent in this version of the game
// Should check scrVarPub.developer but it's absent
// in this version of the game so let's check the dvar
if (!Game::scrVmPub->terminal_error && !developer)
return;
// If were are developing let's just print a brief message
// If were are developing let's call RuntimeErrorInternal
// scrVmPub.debugCode seems to be always false
if (Game::scrVmPub->debugCode || Game::scrVarPub->developer_script)
{
Logger::Print(23, "%s\n", msg);
Game::RuntimeErrorInternal(23, codePos, index, msg);
if (!Game::scrVmPub->terminal_error)
return;
}
else
{
Game::RuntimeErrorInternal(23, codePos, index, msg);
Logger::Print(23, "%s\n", msg);
// Let's not throw error unless we have to
if (!Game::scrVmPub->abort_on_error && !Game::scrVmPub->terminal_error)
if (Game::scrVmPub->abort_on_error && !Game::scrVmPub->terminal_error)
return;
}
@ -522,14 +523,12 @@ namespace Components
Game::gentity_t* Script::GetEntFromEntRef(const Game::scr_entref_t entref)
{
if (entref.classnum != 0)
if (entref.classnum != 0 || entref.entnum >= Game::MAX_GENTITIES)
{
Game::Scr_ObjectError("Not an entity");
return nullptr;
}
assert(entref.entnum < Game::MAX_GENTITIES);
return &Game::g_entities[entref.entnum];
}
@ -541,6 +540,12 @@ namespace Components
return nullptr;
}
if (gentity->s.number >= *Game::svs_numclients)
{
Game::Scr_ObjectError(Utils::String::VA("Entity %i is out of bounds", gentity->s.number));
return nullptr;
}
return &Game::svs_clients[gentity->s.number];
}
@ -580,14 +585,28 @@ namespace Components
// Print to console, even without being in 'developer 1'.
Script::AddFunction("PrintConsole", [](Game::scr_entref_t) // gsc: PrintConsole(<string>)
{
const auto str = Game::Scr_GetString(0);
Game::Com_Printf(0, str);
const auto* str = Game::Scr_GetString(0);
if (str == nullptr)
{
Game::Scr_ParamError(0, "^1PrintConsole: Illegal parameters!\n");
return;
}
Logger::Print(0, "%s", str);
});
// Executes command to the console
Script::AddFunction("Exec", [](Game::scr_entref_t) // gsc: Exec(<string>)
{
const auto str = Game::Scr_GetString(0);
if (str == nullptr)
{
Game::Scr_ParamError(0, "^1Exec: Illegal parameters!\n");
return;
}
Command::Execute(str, false);
});

View File

@ -51,7 +51,7 @@ namespace Components
static void FunctionError();
static void StoreFunctionNameStub();
static void RuntimeError(const char*, unsigned int, const char*, const char*);
static void RuntimeError(const char* codePos, unsigned int index, const char* msg, const char* dialogMessage);
static void StoreScriptName(const char* name);
static void StoreScriptNameStub();

View File

@ -2,19 +2,25 @@
namespace Components
{
void Client::AddFunctions()
void ScriptExtension::AddFunctions()
{
//File functions
Script::AddFunction("fileWrite", [](Game::scr_entref_t) // gsc: fileWrite(<filepath>, <string>, <mode>)
Script::AddFunction("FileWrite", [](Game::scr_entref_t) // gsc: FileWrite(<filepath>, <string>, <mode>)
{
std::string path = Game::Scr_GetString(0);
auto text = Game::Scr_GetString(1);
auto mode = Game::Scr_GetString(2);
const std::string path = Game::Scr_GetString(0);
auto* text = Game::Scr_GetString(1);
auto* mode = Game::Scr_GetString(2);
if (path.empty())
{
Game::Com_Printf(0, "^1fileWrite: filepath not defined!\n");
Game::Scr_ParamError(0, "^1FileWrite: filepath not defined!\n");
return;
}
if (text == nullptr || mode == nullptr)
{
Game::Scr_Error("^1FileWrite: Illegal parameters!\n");
return;
}
@ -23,14 +29,14 @@ namespace Components
{
if (path.find(queryStrings[i]) != std::string::npos)
{
Game::Com_Printf(0, "^1fileWrite: directory traversal is not allowed!\n");
Logger::Print("^1FileWrite: directory traversal is not allowed!\n");
return;
}
}
if (mode != "append"s && mode != "write"s)
{
Game::Com_Printf(0, "^3fileWrite: mode not defined or was wrong, defaulting to 'write'\n");
Logger::Print("^3FileWrite: mode not defined or was wrong, defaulting to 'write'\n");
mode = "write";
}
@ -44,13 +50,13 @@ namespace Components
}
});
Script::AddFunction("fileRead", [](Game::scr_entref_t) // gsc: fileRead(<filepath>)
Script::AddFunction("FileRead", [](Game::scr_entref_t) // gsc: FileRead(<filepath>)
{
std::string path = Game::Scr_GetString(0);
if (path.empty())
{
Game::Com_Printf(0, "^1fileRead: filepath not defined!\n");
Game::Scr_ParamError(0, "^1FileRead: filepath not defined!\n");
return;
}
@ -59,27 +65,27 @@ namespace Components
{
if (path.find(queryStrings[i]) != std::string::npos)
{
Game::Com_Printf(0, "^1fileRead: directory traversal is not allowed!\n");
Logger::Print("^1FileRead: directory traversal is not allowed!\n");
return;
}
}
if (!FileSystem::FileReader(path).exists())
{
Game::Com_Printf(0, "^1fileRead: file not found!\n");
Logger::Print("^1FileRead: file not found!\n");
return;
}
Game::Scr_AddString(FileSystem::FileReader(path).getBuffer().data());
});
Script::AddFunction("fileExists", [](Game::scr_entref_t) // gsc: fileExists(<filepath>)
Script::AddFunction("FileExists", [](Game::scr_entref_t) // gsc: FileExists(<filepath>)
{
std::string path = Game::Scr_GetString(0);
if (path.empty())
{
Game::Com_Printf(0, "^1fileExists: filepath not defined!\n");
Game::Scr_ParamError(0, "^1FileExists: filepath not defined!\n");
return;
}
@ -88,7 +94,7 @@ namespace Components
{
if (path.find(queryStrings[i]) != std::string::npos)
{
Game::Com_Printf(0, "^1fileExists: directory traversal is not allowed!\n");
Logger::Print("^1FileExists: directory traversal is not allowed!\n");
return;
}
}
@ -96,13 +102,13 @@ namespace Components
Game::Scr_AddInt(FileSystem::FileReader(path).exists());
});
Script::AddFunction("fileRemove", [](Game::scr_entref_t) // gsc: fileRemove(<filepath>)
Script::AddFunction("FileRemove", [](Game::scr_entref_t) // gsc: FileRemove(<filepath>)
{
std::string path = Game::Scr_GetString(0);
if (path.empty())
{
Game::Com_Printf(0, "^1fileRemove: filepath not defined!\n");
Game::Scr_ParamError(0, "^1FileRemove: filepath not defined!\n");
return;
}
@ -111,7 +117,7 @@ namespace Components
{
if (path.find(queryStrings[i]) != std::string::npos)
{
Game::Com_Printf(0, "^1fileRemove: directory traversal is not allowed!\n");
Logger::Print("^1fileRemove: directory traversal is not allowed!\n");
return;
}
}
@ -123,9 +129,9 @@ namespace Components
});
}
void Client::AddMethods()
void ScriptExtension::AddMethods()
{
// Client methods
// ScriptExtension methods
Script::AddFunction("GetIp", [](Game::scr_entref_t entref) // gsc: self GetIp()
{
const auto* gentity = Script::GetEntFromEntRef(entref);
@ -133,8 +139,10 @@ namespace Components
std::string ip = Game::NET_AdrToString(client->netchan.remoteAddress);
if (ip.find_first_of(":") != std::string::npos)
ip.erase(ip.begin() + ip.find_first_of(":"), ip.end()); // Erase port
const auto pos = ip.find_first_of(":");
if (pos != std::string::npos)
ip.erase(ip.begin() + pos, ip.end()); // Erase port
Game::Scr_AddString(ip.data());
});
@ -148,13 +156,9 @@ namespace Components
});
}
Client::Client()
{
Client::AddFunctions();
Client::AddMethods();
}
Client::~Client()
ScriptExtension::ScriptExtension()
{
ScriptExtension::AddFunctions();
ScriptExtension::AddMethods();
}
}

View File

@ -2,11 +2,10 @@
namespace Components
{
class Client : public Component
class ScriptExtension : public Component
{
public:
Client();
~Client();
ScriptExtension();
private:

View File

@ -1218,11 +1218,11 @@ namespace Game
{
pushad
mov eax, [esp + 0x10 + 0x20]
mov edi, [esp + 0x4 + 0x20]
mov eax, [esp + 0x10 + 0x20] // msg
mov edi, [esp + 0x4 + 0x20] // channel
push [esp + 0xC + 0x20]
push [esp + 0xC + 0x20]
push [esp + 0xC + 0x20] // index
push [esp + 0xC + 0x20] // codePos
mov edx, 0x61ABE0
call edx

View File

@ -20,11 +20,11 @@ namespace Game
typedef vec_t vec3_t[3];
typedef vec_t vec4_t[4];
typedef struct
struct scr_entref_t
{
unsigned __int16 entnum;
unsigned __int16 classnum;
} scr_entref_t;
};
typedef void(__cdecl * scr_function_t)(scr_entref_t);