Skip to content

Commit

Permalink
Added ENTINDEX different to the og ENTINDEX now known as EDICTINDEX. …
Browse files Browse the repository at this point in the history
…Plus other minor code changes.
  • Loading branch information
OrsellGaming committed Nov 3, 2024
1 parent ba8f30f commit 95471e9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
4 changes: 3 additions & 1 deletion globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
}

Expand Down
26 changes: 18 additions & 8 deletions globals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand All @@ -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;
Expand All @@ -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<int (__cdecl*)(CBaseEntity*)>(Memory::Scanner::Scan<void*>(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.
//---------------------------------------------------------------------------------
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion p2mm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 15 additions & 15 deletions vscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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++)
{
Expand All @@ -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);
Expand Down Expand Up @@ -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.");
Expand Down

0 comments on commit 95471e9

Please sign in to comment.