From 95471e97d83bde31cb4490f2ac3a65e39150c2ad Mon Sep 17 00:00:00 2001 From: OrsellGaming <34631691+OrsellGaming@users.noreply.github.com> Date: Sun, 3 Nov 2024 01:05:40 -0700 Subject: [PATCH] Added ENTINDEX different to the og ENTINDEX now known as EDICTINDEX. Plus other minor code changes. --- globals.cpp | 4 +++- globals.hpp | 26 ++++++++++++++++++-------- p2mm.cpp | 2 +- vscript.cpp | 30 +++++++++++++++--------------- 4 files changed, 37 insertions(+), 25 deletions(-) diff --git a/globals.cpp b/globals.cpp index b84e6d8..cd24fd6 100644 --- a/globals.cpp +++ b/globals.cpp @@ -74,14 +74,16 @@ int GFunc::UserIDToPlayerIndex(int userid) //--------------------------------------------------------------------------------- const char* GFunc::GetPlayerName(int index) { - if (index <= 0) + if (index <= 0 || index > g_pGlobals->maxClients) { + P2MMLog(0, true, "Invalid index passed to GetPlayerName: %i!", index); return ""; } player_info_t playerinfo; if (!engineServer->GetPlayerInfo(index, &playerinfo)) { + P2MMLog(0, true, "Couldn't retrieve playerinfo of player index \"%i\" in GetPlayerName!", index); return ""; } diff --git a/globals.hpp b/globals.hpp index 4472973..61240b8 100644 --- a/globals.hpp +++ b/globals.hpp @@ -38,6 +38,7 @@ class CPortal_Player; #define COMMAND_COMPLETION_MAXITEMS 64 #define COMMAND_COMPLETION_ITEM_LENGTH 64 +// Team number macros. #define TEAM_SINGLEPLAYER 0 #define TEAM_SPECTATOR 1 #define TEAM_RED 2 @@ -73,23 +74,23 @@ namespace GFunc { inline const char* GetGameBaseDir(); } +CBasePlayer* UTIL_PlayerByIndex(int playerIndex); + void CBaseEntity__RemoveEntity(CBaseEntity* pEntity); int CBaseEntity__GetTeamNumber(CBasePlayer* pPlayer); HSCRIPT CBaseEntity__GetScriptScope(CBaseEntity* entity); HSCRIPT CBaseEntity__GetScriptInstance(CBaseEntity* entity); -CBasePlayer* UTIL_PlayerByIndex(int playerIndex); - void CPortal_Player__RespawnPlayer(int playerIndex); void CPortal_Player__SetFlashlightState(int playerIndex, bool enable); -// If String Equals String helper function +// If String Equals String helper function. Taken from utils.h. inline bool FStrEq(const char* sz1, const char* sz2) { return (V_stricmp(sz1, sz2) == 0); } -// If String Has Substring helper function +// If String Has Substring helper function. Taken from utils.h. inline bool FSubStr(const char* sz1, const char* search) { return (V_strstr(sz1, search)); @@ -98,7 +99,7 @@ inline bool FSubStr(const char* sz1, const char* search) //--------------------------------------------------------------------------------- // Purpose: Entity edict to entity index. Taken from utils.h. //--------------------------------------------------------------------------------- -inline int ENTINDEX(edict_t* pEdict) +inline int EDICTINDEX(edict_t* pEdict) { if (!pEdict) return 0; @@ -107,6 +108,15 @@ inline int ENTINDEX(edict_t* pEdict) return edictIndex; } +//--------------------------------------------------------------------------------- +// Purpose: Entity to entity index. +//--------------------------------------------------------------------------------- +inline int ENTINDEX(CBaseEntity* pEnt) +{ + static auto _ENTINDEX = reinterpret_cast(Memory::Scanner::Scan(SERVERDLL, "55 8B EC 8B 45 ?? 85 C0 74 ?? 8B 40 ?? 85 C0 74 ?? 8B 0D")); + return _ENTINDEX(pEnt); +} + //--------------------------------------------------------------------------------- // Purpose: Entity index to entity edict. Taken from utils.h. //--------------------------------------------------------------------------------- @@ -128,12 +138,12 @@ inline edict_t* INDEXENT(int iEdictNum) //--------------------------------------------------------------------------------- inline HSCRIPT INDEXHANDLE(int iEdictNum) { edict_t* pEdict = INDEXENT(iEdictNum); - CBaseEntity* p_baseEntity = pEdict->GetUnknown()->GetBaseEntity(); - if (!p_baseEntity) + CBaseEntity* pBaseEntity = pEdict->GetUnknown()->GetBaseEntity(); + if (!pBaseEntity) { return nullptr; } - HSCRIPT entityHandle = CBaseEntity__GetScriptInstance(p_baseEntity); + HSCRIPT entityHandle = CBaseEntity__GetScriptInstance(pBaseEntity); return entityHandle; } diff --git a/p2mm.cpp b/p2mm.cpp index cae6852..30cb1fc 100644 --- a/p2mm.cpp +++ b/p2mm.cpp @@ -524,7 +524,7 @@ bool CP2MMServerPlugin::Load(CreateInterfaceFn interfaceFactory, CreateInterface P2MMLog(0, false, "Loaded plugin! Horray!"); m_bPluginLoaded = true; - } catch(std::exception& ex) { + } catch (const std::exception& ex) { P2MMLog(0, false, "Failed to load plugin! :( Exception: (%s)", ex.what()); this->m_bNoUnload = true; return false; diff --git a/vscript.cpp b/vscript.cpp index 45bd9ae..6e4e44f 100644 --- a/vscript.cpp +++ b/vscript.cpp @@ -141,7 +141,7 @@ class CFilter : public IRecipientFilter //--------------------------------------------------------------------------------- // Purpose: Sends a raw message to the chat HUD. //--------------------------------------------------------------------------------- -static void SendToChat(const char* msg, int index) +static void SendToChat(const char* msg, int playerIndex) { if (!msg) { @@ -151,7 +151,7 @@ static void SendToChat(const char* msg, int index) CFilter recipient_filter; // Send to all players - if (index == 0) + if (playerIndex == 0) { for (int i = 1; i < g_pGlobals->maxClients; i++) { @@ -164,10 +164,10 @@ static void SendToChat(const char* msg, int index) } else { - recipient_filter.AddPlayer(index); + recipient_filter.AddPlayer(playerIndex); } - netmsg = engineServer->UserMessageBegin(&recipient_filter, 3, "SayText2"); + netmsg = engineServer->UserMessageBegin(&recipient_filter, 4, "SayText2"); netmsg->WriteByte(0); netmsg->WriteString(msg); netmsg->WriteByte(1); @@ -240,22 +240,22 @@ void RegisterFuncsAndRun() return; } - ScriptRegisterFunction(g_pScriptVM, printlP2MM, "Logging for the P2MM VScript. The log message must be passed as a string or it will error."); + ScriptRegisterFunction (g_pScriptVM, printlP2MM, "Logging for the P2MM VScript. The log message must be passed as a string or it will error."); ScriptRegisterFunctionNamed(g_pScriptVM, GFunc::GetPlayerName, "GetPlayerName", "Gets player username by index."); ScriptRegisterFunctionNamed(g_pScriptVM, GFunc::GetSteamID, "GetSteamID", "Gets the account ID component of player SteamID by index."); ScriptRegisterFunctionNamed(g_pScriptVM, GFunc::UserIDToPlayerIndex, "UserIDToPlayerIndex", "Gets player entity index by userid."); - ScriptRegisterFunction(g_pScriptVM, IsMapValid, "Returns true is the supplied string is a available map to load and run."); - ScriptRegisterFunction(g_pScriptVM, GetDeveloperLevelP2MM, "Returns the value of ConVar p2mm_developer."); - ScriptRegisterFunction(g_pScriptVM, SetPhysTypeConVar, "Sets 'player_held_object_use_view_model' to the supplied integer value."); - ScriptRegisterFunction(g_pScriptVM, SetMaxPortalSeparationConvar, "Sets 'portal_max_separation_force' to the supplied integer value."); - ScriptRegisterFunction(g_pScriptVM, IsDedicatedServer, "Returns true if this is a dedicated server."); - ScriptRegisterFunction(g_pScriptVM, InitializeEntity, "Initializes an entity. Note: Not all entities will work even after being initialized with this function."); - ScriptRegisterFunction(g_pScriptVM, SendToChat, "Sends a raw message to the chat HUD."); + ScriptRegisterFunction (g_pScriptVM, IsMapValid, "Returns true is the supplied string is a available map to load and run."); + ScriptRegisterFunction (g_pScriptVM, GetDeveloperLevelP2MM, "Returns the value of ConVar p2mm_developer."); + ScriptRegisterFunction (g_pScriptVM, SetPhysTypeConVar, "Sets 'player_held_object_use_view_model' to the supplied integer value."); + ScriptRegisterFunction (g_pScriptVM, SetMaxPortalSeparationConvar, "Sets 'portal_max_separation_force' to the supplied integer value."); + ScriptRegisterFunction (g_pScriptVM, IsDedicatedServer, "Returns true if this is a dedicated server."); + ScriptRegisterFunction (g_pScriptVM, InitializeEntity, "Initializes an entity. Note: Not all entities will work even after being initialized with this function."); + ScriptRegisterFunction (g_pScriptVM, SendToChat, "Sends a raw message to the chat HUD."); ScriptRegisterFunctionNamed(g_pScriptVM, GFunc::GetGameMainDir, "GetGameMainDir", "Returns the game directory. Ex. portal2/portal_stories"); ScriptRegisterFunctionNamed(g_pScriptVM, GFunc::GetGameBaseDir, "GetGameBaseDir", "Get the main game directory being used. Ex. Portal 2/Portal Stories Mel"); - ScriptRegisterFunction(g_pScriptVM, GetLastMap, "Returns the last map recorded by the launcher's Last Map system."); - ScriptRegisterFunction(g_pScriptVM, FirstRunState, "Get or set the state of whether the first map was run or not. Set false/true = 0/1 | -1 to get state."); - ScriptRegisterFunction(g_pScriptVM, CallFirstRunPrompt, "Shows the first run prompt if enabled in config.nut."); + ScriptRegisterFunction (g_pScriptVM, GetLastMap, "Returns the last map recorded by the launcher's Last Map system."); + ScriptRegisterFunction (g_pScriptVM, FirstRunState, "Get or set the state of whether the first map was run or not. Set false/true = 0/1 | -1 to get state."); + ScriptRegisterFunction (g_pScriptVM, CallFirstRunPrompt, "Shows the first run prompt if enabled in config.nut."); ScriptRegisterFunctionNamed(g_pScriptVM, GFunc::GetConVarInt, "GetConVarInt", "Get the integer value of a ConVar."); ScriptRegisterFunctionNamed(g_pScriptVM, GFunc::GetConVarString, "GetConVarString", "Get the string value of a ConVar."); ScriptRegisterFunctionNamed(g_pScriptVM, INDEXHANDLE, "PlayerIndexToPlayerHandle", "Takes the player's entity index and returns the player's script handle.");