Skip to content

Commit d94f623

Browse files
committed
feat(scripting/player): GetIPAddress
1 parent a72f096 commit d94f623

File tree

6 files changed

+28
-3
lines changed

6 files changed

+28
-3
lines changed

plugin_files/scripting/includes/swiftly/player.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ class Player
139139
else
140140
return 0;
141141
}
142+
143+
const char *GetIPAddress()
144+
{
145+
void *player_GetIPAddress = FetchFunctionPtr(nullptr, "scripting_Player_GetIPAddress");
146+
if (player_GetIPAddress)
147+
return reinterpret_cast<Player_GetIPAddress>(player_GetIPAddress)(this->m_playerSlot);
148+
else
149+
return "";
150+
}
142151
};
143152

144153
#endif

plugin_files/scripting/includes/swiftly/swiftly_memory.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ typedef uint32_t (*Player_GetConnectedTime)(uint32_t);
3535
typedef int (*Player_FetchStat)(uint32_t, uint32_t);
3636
typedef void (*Player_SetStat)(uint32_t, uint32_t, int);
3737

38+
typedef const char *(*Player_GetIPAddress)(uint32_t);
39+
3840
typedef void (*Player_SendMessage)(uint32_t, HudDestination, const char *);
3941
typedef void (*Players_SendMessage)(HudDestination, const char *);
4042

src/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define SITE_UNKNOWN 2
1515

1616
#include <public/playerslot.h>
17+
#include <public/inetchannelinfo.h>
1718
#include <ISmmPlugin.h>
1819
#include <iplayerinfo.h>
1920
#include <sh_vector.h>

src/components/Plugins/src/scripting/Player.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,13 @@ SMM_API void scripting_Player_SetMatchStat(uint32 playerId, PlayerStat stat, int
342342
playerController->m_pActionTrackingServices->m_matchStats().m_iAssists = value;
343343
else if (stat == PlayerStat::DEATHS)
344344
playerController->m_pActionTrackingServices->m_matchStats().m_iDeaths = value;
345+
}
346+
347+
SMM_API const char *scripting_Player_GetIPAddress(uint32 playerId)
348+
{
349+
Player *player = g_playerManager->GetPlayer(playerId);
350+
if (!player)
351+
return "";
352+
353+
return player->GetIPAddress().c_str();
345354
}

src/entrypoint.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ void SwiftlyPlugin::Hook_OnClientConnected(CPlayerSlot slot, const char *pszName
240240
{
241241
if (bFakePlayer)
242242
{
243-
Player *player = new Player(true, slot.Get(), pszName, 0);
243+
Player *player = new Player(true, slot.Get(), pszName, 0, "127.0.0.1");
244244
g_playerManager->RegisterPlayer(player);
245245
}
246246
}
@@ -249,7 +249,8 @@ bool scripting_OnClientConnect(const OnClientConnect *e);
249249

250250
bool SwiftlyPlugin::Hook_ClientConnect(CPlayerSlot slot, const char *pszName, uint64 xuid, const char *pszNetworkID, bool unk1, CBufferString *pRejectReason)
251251
{
252-
Player *player = new Player(false, slot.Get(), pszName, xuid);
252+
std::string ip_address = explode(pszNetworkID, ":")[0];
253+
Player *player = new Player(false, slot.Get(), pszName, xuid, ip_address);
253254
g_playerManager->RegisterPlayer(player);
254255

255256
player->SetConnected(true);

src/player/Player.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,14 @@ enum ENetworkDisconnectionReason
9393
class Player
9494
{
9595
public:
96-
Player(bool m_isFakeClient, int m_slot, const char *m_name, uint64 m_xuid)
96+
Player(bool m_isFakeClient, int m_slot, const char *m_name, uint64 m_xuid, std::string ip_address)
9797
{
9898
this->slot = m_slot;
9999
this->isFakeClient = m_isFakeClient;
100100
this->connectTime = std::time(0);
101101
this->name = m_name;
102102
this->xuid = m_xuid;
103+
this->ip_address = ip_address;
103104
}
104105

105106
~Player()
@@ -115,6 +116,7 @@ class Player
115116
inline void SetEHandlerIdx(int eHandleId) { this->eHandleId = eHandleId; };
116117
inline int GetEHandlerIdx() { return this->eHandleId; };
117118
inline uint32 GetConnectedTime() { return (std::time(0) - this->connectTime); };
119+
inline std::string GetIPAddress() { return this->ip_address; }
118120

119121
inline void SetConnected(bool connected) { this->isConnected = connected; };
120122
inline bool IsConnected() { return this->isConnected; };
@@ -145,6 +147,7 @@ class Player
145147
std::time_t connectTime;
146148
const char *name;
147149
uint64 xuid;
150+
std::string ip_address = "0.0.0.0";
148151

149152
std::map<std::string, std::any> internalVars;
150153
};

0 commit comments

Comments
 (0)