Added CPlayer class
Added new player natives Fixed floatround returning a float instead of an int Added new float.inc file Added OnClientSettingsChanged forward --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40247
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
#include "CPlayer.h"
|
||||
|
||||
CPlayer::CPlayer()
|
||||
{
|
||||
m_IsConnected = false;
|
||||
m_IsInGame = false;
|
||||
m_IsAuthorized = false;
|
||||
m_PlayerEdict = NULL;
|
||||
}
|
||||
|
||||
void CPlayer::Initialize(const char *name, const char *ip, edict_t *pEntity)
|
||||
{
|
||||
m_IsConnected = true;
|
||||
m_Name.assign(name);
|
||||
m_Ip.assign(ip);
|
||||
m_PlayerEdict = pEntity;
|
||||
}
|
||||
|
||||
void CPlayer::Connect()
|
||||
{
|
||||
m_IsInGame = true;
|
||||
}
|
||||
|
||||
void CPlayer::Authorize(const char *steamid)
|
||||
{
|
||||
m_IsAuthorized = true;
|
||||
m_AuthID.assign(steamid);
|
||||
}
|
||||
|
||||
void CPlayer::Disconnect()
|
||||
{
|
||||
m_IsConnected = false;
|
||||
m_IsInGame = false;
|
||||
m_IsAuthorized = false;
|
||||
m_Name.clear();
|
||||
m_Ip.clear();
|
||||
m_AuthID.clear();
|
||||
m_PlayerEdict = NULL;
|
||||
}
|
||||
|
||||
void CPlayer::SetName(const char *name)
|
||||
{
|
||||
m_Name.assign(name);
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
#ifndef _INCLUDE_SOURCEMOD_CPLAYER_H_
|
||||
#define _INCLUDE_SOURCEMOD_CPLAYER_H_
|
||||
|
||||
#include <sh_string.h>
|
||||
#include <eiface.h>
|
||||
|
||||
using namespace SourceHook;
|
||||
|
||||
class CPlayer
|
||||
{
|
||||
friend class CPlayerManager;
|
||||
public:
|
||||
CPlayer();
|
||||
public:
|
||||
const char *PlayerName() const;
|
||||
const char *PlayerIP() const;
|
||||
const char *PlayerAuthString() const;
|
||||
edict_t *GetPlayerEdict() const;
|
||||
bool IsPlayerInGame() const;
|
||||
bool IsPlayerConnected() const;
|
||||
bool IsPlayerAuthorized() const;
|
||||
bool IsPlayerFakeClient() const;
|
||||
//:TODO: is user alive function
|
||||
private:
|
||||
void Initialize(const char *name, const char *ip, edict_t *pEntity);
|
||||
void Connect();
|
||||
void Authorize(const char *steamid);
|
||||
void Disconnect();
|
||||
void SetName(const char *name);
|
||||
private:
|
||||
bool m_IsConnected;
|
||||
bool m_IsInGame;
|
||||
bool m_IsAuthorized;
|
||||
String m_Name;
|
||||
String m_Ip;
|
||||
String m_AuthID;
|
||||
edict_t *m_PlayerEdict;
|
||||
};
|
||||
|
||||
inline const char *CPlayer::PlayerName() const
|
||||
{
|
||||
return m_Name.c_str();
|
||||
}
|
||||
|
||||
inline const char *CPlayer::PlayerIP() const
|
||||
{
|
||||
return m_Ip.c_str();
|
||||
}
|
||||
|
||||
inline const char *CPlayer::PlayerAuthString() const
|
||||
{
|
||||
return m_AuthID.c_str();
|
||||
}
|
||||
|
||||
inline edict_t *CPlayer::GetPlayerEdict() const
|
||||
{
|
||||
return m_PlayerEdict;
|
||||
}
|
||||
|
||||
inline bool CPlayer::IsPlayerInGame() const
|
||||
{
|
||||
return m_IsInGame;
|
||||
}
|
||||
|
||||
inline bool CPlayer::IsPlayerConnected() const
|
||||
{
|
||||
return m_IsConnected;
|
||||
}
|
||||
|
||||
inline bool CPlayer::IsPlayerAuthorized() const
|
||||
{
|
||||
return m_IsAuthorized;
|
||||
}
|
||||
|
||||
inline bool CPlayer::IsPlayerFakeClient() const
|
||||
{
|
||||
return (strcmp(m_AuthID.c_str(), "BOT") == 0);
|
||||
}
|
||||
|
||||
#endif // _INCLUDE_SOURCEMOD_CPLAYER_H_
|
||||
+50
-5
@@ -1,10 +1,13 @@
|
||||
#include "CPlayerManager.h"
|
||||
#include "ForwardSys.h"
|
||||
|
||||
CPlayerManager g_PlayerManager;
|
||||
|
||||
SH_DECL_HOOK5(IServerGameClients, ClientConnect, SH_NOATTRIB, 0, bool, edict_t *, const char *, const char *, char *, int);
|
||||
SH_DECL_HOOK2_void(IServerGameClients, ClientPutInServer, SH_NOATTRIB, 0, edict_t *, const char *);
|
||||
SH_DECL_HOOK1_void(IServerGameClients, ClientDisconnect, SH_NOATTRIB, 0, edict_t *);
|
||||
SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
|
||||
SH_DECL_HOOK1_void(IServerGameClients, ClientSettingsChanged, SH_NOATTRIB, 0, edict_t *);
|
||||
|
||||
void CPlayerManager::OnSourceModAllInitialized()
|
||||
{
|
||||
@@ -13,6 +16,7 @@ void CPlayerManager::OnSourceModAllInitialized()
|
||||
SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, serverClients, this, &CPlayerManager::OnClientDisconnect, false);
|
||||
SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, serverClients, this, &CPlayerManager::OnClientDisconnect_Post, true);
|
||||
SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientCommand, serverClients, this, &CPlayerManager::OnClientCommand, false);
|
||||
SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, serverClients, this, &CPlayerManager::OnClientSettingsChanged, true);
|
||||
|
||||
/* Register OnClientConnect */
|
||||
ParamType p1[] = {Param_Cell, Param_String, Param_Cell};
|
||||
@@ -31,8 +35,16 @@ void CPlayerManager::OnSourceModAllInitialized()
|
||||
/* Register OnClientCommand */
|
||||
m_clcommand = g_Forwards.CreateForward("OnClientCommand", ET_Hook, 1, p2);
|
||||
|
||||
/* Register OnClientSettingsChanged */
|
||||
m_clinfochanged = g_Forwards.CreateForward("OnClientSettingsChanged", ET_Ignore, 1, p2);
|
||||
|
||||
/* Register OnClientAuthorized */
|
||||
//:TODO:
|
||||
|
||||
/* Initialize all players */
|
||||
m_maxClients = g_SMAPI->pGlobals()->maxClients;
|
||||
m_PlayerCount = 0;
|
||||
m_Players = new CPlayer[m_maxClients];
|
||||
}
|
||||
|
||||
void CPlayerManager::OnSourceModShutdown()
|
||||
@@ -42,6 +54,7 @@ void CPlayerManager::OnSourceModShutdown()
|
||||
SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, serverClients, this, &CPlayerManager::OnClientDisconnect, false);
|
||||
SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, serverClients, this, &CPlayerManager::OnClientDisconnect_Post, true);
|
||||
SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientCommand, serverClients, this, &CPlayerManager::OnClientCommand, false);
|
||||
SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, serverClients, this, &CPlayerManager::OnClientSettingsChanged, true);
|
||||
|
||||
/* Release forwards */
|
||||
g_Forwards.ReleaseForward(m_clconnect);
|
||||
@@ -49,13 +62,18 @@ void CPlayerManager::OnSourceModShutdown()
|
||||
g_Forwards.ReleaseForward(m_cldisconnect);
|
||||
g_Forwards.ReleaseForward(m_cldisconnect_post);
|
||||
g_Forwards.ReleaseForward(m_clcommand);
|
||||
g_Forwards.ReleaseForward(m_clinfochanged);
|
||||
|
||||
delete [] m_Players;
|
||||
}
|
||||
|
||||
bool CPlayerManager::OnClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen)
|
||||
{
|
||||
cell_t res = 1;
|
||||
int client = engine->IndexOfEdict(pEntity);
|
||||
|
||||
m_clconnect->PushCell(engine->IndexOfEdict(pEntity));
|
||||
m_Players[client].Initialize(pszName, pszAddress, pEntity);
|
||||
m_clconnect->PushCell(client);
|
||||
m_clconnect->PushStringEx(reject, maxrejectlen, SM_PARAM_STRING_UTF8, SM_PARAM_COPYBACK);
|
||||
m_clconnect->PushCell(maxrejectlen);
|
||||
m_clconnect->Execute(&res, NULL);
|
||||
@@ -66,8 +84,11 @@ bool CPlayerManager::OnClientConnect(edict_t *pEntity, const char *pszName, cons
|
||||
void CPlayerManager::OnClientPutInServer(edict_t *pEntity, const char *playername)
|
||||
{
|
||||
cell_t res;
|
||||
int client = engine->IndexOfEdict(pEntity);
|
||||
|
||||
m_clputinserver->PushCell(engine->IndexOfEdict(pEntity));
|
||||
m_Players[client].Connect();
|
||||
m_PlayerCount++;
|
||||
m_clputinserver->PushCell(client);
|
||||
m_clputinserver->Execute(&res, NULL);
|
||||
}
|
||||
|
||||
@@ -79,9 +100,18 @@ void CPlayerManager::OnClientAuthorized()
|
||||
void CPlayerManager::OnClientDisconnect(edict_t *pEntity)
|
||||
{
|
||||
cell_t res;
|
||||
int client = engine->IndexOfEdict(pEntity);
|
||||
|
||||
m_cldisconnect->PushCell(engine->IndexOfEdict(pEntity));
|
||||
m_cldisconnect->Execute(&res, NULL);
|
||||
if (m_Players[client].IsPlayerConnected())
|
||||
{
|
||||
m_cldisconnect->PushCell(client);
|
||||
m_cldisconnect->Execute(&res, NULL);
|
||||
}
|
||||
if (m_Players[client].IsPlayerInGame())
|
||||
{
|
||||
m_PlayerCount--;
|
||||
}
|
||||
m_Players[client].Disconnect();
|
||||
}
|
||||
|
||||
void CPlayerManager::OnClientDisconnect_Post(edict_t *pEntity)
|
||||
@@ -98,4 +128,19 @@ void CPlayerManager::OnClientCommand(edict_t *pEntity)
|
||||
|
||||
m_clcommand->PushCell(engine->IndexOfEdict(pEntity));
|
||||
m_clcommand->Execute(&res, NULL);
|
||||
}
|
||||
|
||||
//:TODO: res should be evaluated here for something, DOCUMENT this in the INC FILE!
|
||||
}
|
||||
|
||||
void CPlayerManager::OnClientSettingsChanged(edict_t *pEntity)
|
||||
{
|
||||
cell_t res;
|
||||
int client = engine->IndexOfEdict(pEntity);
|
||||
|
||||
m_clinfochanged->PushCell(engine->IndexOfEdict(pEntity));
|
||||
m_clinfochanged->Execute(&res, NULL);
|
||||
if (m_Players[client].IsPlayerInGame())
|
||||
{
|
||||
m_Players[client].SetName(engine->GetClientConVarValue(client, "name"));
|
||||
}
|
||||
}
|
||||
|
||||
+35
-2
@@ -1,16 +1,23 @@
|
||||
#ifndef _INCLUDE_SOURCEMOD_CPLAYERMANAGER_H_
|
||||
#define _INCLUDE_SOURCEMOD_CPLAYERMANAGER_H_
|
||||
|
||||
#include "sourcemod.h"
|
||||
#include "sm_globals.h"
|
||||
#include <eiface.h>
|
||||
#include "sourcemm_api.h"
|
||||
#include <IForwardSys.h>
|
||||
#include "CPlayer.h"
|
||||
|
||||
class CPlayer;
|
||||
|
||||
class CPlayerManager : public SMGlobalClass
|
||||
{
|
||||
public: //SMGlobalClass
|
||||
virtual void OnSourceModAllInitialized();
|
||||
virtual void OnSourceModShutdown();
|
||||
public:
|
||||
int GetMaxClients() const;
|
||||
CPlayer *GetPlayerByIndex(int client) const;
|
||||
int GetPlayerCount() const;
|
||||
public:
|
||||
bool OnClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen);
|
||||
void OnClientPutInServer(edict_t *pEntity, char const *playername);
|
||||
@@ -18,12 +25,38 @@ public:
|
||||
void OnClientDisconnect_Post(edict_t *pEntity);
|
||||
void OnClientAuthorized(); //:TODO: any args needed?
|
||||
void OnClientCommand(edict_t *pEntity);
|
||||
void OnClientSettingsChanged(edict_t *pEntity);
|
||||
private:
|
||||
IForward *m_clconnect;
|
||||
IForward *m_cldisconnect;
|
||||
IForward *m_cldisconnect_post;
|
||||
IForward *m_clputinserver;
|
||||
IForward *m_clcommand;
|
||||
IForward *m_clinfochanged;
|
||||
CPlayer *m_Players;
|
||||
int m_maxClients;
|
||||
int m_PlayerCount;
|
||||
};
|
||||
|
||||
#endif //_INCLUDE_SOURCEMOD_CPLAYERMANAGER_H_
|
||||
extern CPlayerManager g_PlayerManager;
|
||||
|
||||
inline int CPlayerManager::GetMaxClients() const
|
||||
{
|
||||
return m_maxClients;
|
||||
}
|
||||
|
||||
inline CPlayer *CPlayerManager::GetPlayerByIndex(int client) const
|
||||
{
|
||||
if (client > m_maxClients || client < 0)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
return &m_Players[client];
|
||||
}
|
||||
|
||||
inline int CPlayerManager::GetPlayerCount() const
|
||||
{
|
||||
return m_PlayerCount;
|
||||
}
|
||||
|
||||
#endif //_INCLUDE_SOURCEMOD_CPLAYERMANAGER_H_
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="Windows-1252"?>
|
||||
<VisualStudioProject
|
||||
ProjectType="Visual C++"
|
||||
Version="8.00"
|
||||
Version="8,00"
|
||||
Name="sourcemod_mm"
|
||||
ProjectGUID="{E39527CD-7CAB-4420-97CC-DA1B93B260BC}"
|
||||
RootNamespace="sourcemod_mm"
|
||||
@@ -178,6 +178,10 @@
|
||||
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
|
||||
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
|
||||
>
|
||||
<File
|
||||
RelativePath="..\CPlayer.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\CPlayerManager.cpp"
|
||||
>
|
||||
@@ -210,6 +214,10 @@
|
||||
RelativePath="..\smn_float.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_player.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_string.cpp"
|
||||
>
|
||||
@@ -228,6 +236,10 @@
|
||||
Filter="h;hpp;hxx;hm;inl;inc;xsd"
|
||||
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
|
||||
>
|
||||
<File
|
||||
RelativePath="..\CPlayer.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\CPlayerManager.h"
|
||||
>
|
||||
@@ -315,6 +327,10 @@
|
||||
RelativePath="..\systems\PluginSys.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\systems\ShareSys.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Source Files"
|
||||
|
||||
+1
-1
@@ -150,7 +150,7 @@ static cell_t sm_floatround(IPluginContext *pCtx, const cell_t *params)
|
||||
}
|
||||
}
|
||||
|
||||
return sp_ftoc(val);
|
||||
return static_cast<int>(val);
|
||||
}
|
||||
|
||||
static cell_t sm_floatstr(IPluginContext *pCtx, const cell_t *params)
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
#include "CPlayerManager.h"
|
||||
|
||||
static cell_t sm_getclientcount(IPluginContext *pCtx, const cell_t *params)
|
||||
{
|
||||
if (params[1])
|
||||
{
|
||||
return g_PlayerManager.GetPlayerCount();
|
||||
}
|
||||
|
||||
int maxplayers = g_PlayerManager.GetMaxClients();
|
||||
int count = 0;
|
||||
for (int i=1; i<=maxplayers; ++i)
|
||||
{
|
||||
CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(i);
|
||||
if ((pPlayer->IsPlayerConnected()) && !(pPlayer->IsPlayerInGame()))
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
return (g_PlayerManager.GetPlayerCount() + count);
|
||||
}
|
||||
|
||||
static cell_t sm_getmaxclients(IPluginContext *pCtx, const cell_t *params)
|
||||
{
|
||||
return g_PlayerManager.GetMaxClients();
|
||||
}
|
||||
|
||||
static cell_t sm_getclientname(IPluginContext *pCtx, const cell_t *params)
|
||||
{
|
||||
int index = params[1];
|
||||
if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
|
||||
{
|
||||
//:TODO: runtime error
|
||||
}
|
||||
|
||||
pCtx->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), g_PlayerManager.GetPlayerByIndex(index)->PlayerName(), NULL);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static cell_t sm_getclientip(IPluginContext *pCtx, const cell_t *params)
|
||||
{
|
||||
int index = params[1];
|
||||
if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
|
||||
{
|
||||
//:TODO: runtime error
|
||||
}
|
||||
|
||||
pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), g_PlayerManager.GetPlayerByIndex(index)->PlayerIP());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static cell_t sm_getclientauthstr(IPluginContext *pCtx, const cell_t *params)
|
||||
{
|
||||
int index = params[1];
|
||||
if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
|
||||
{
|
||||
//:TODO: runtime error
|
||||
}
|
||||
|
||||
pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), g_PlayerManager.GetPlayerByIndex(index)->PlayerAuthString());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static cell_t sm_isplayerconnected(IPluginContext *pCtx, const cell_t *params)
|
||||
{
|
||||
int index = params[1];
|
||||
if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
|
||||
{
|
||||
//:TODO: runtime error
|
||||
}
|
||||
|
||||
return (g_PlayerManager.GetPlayerByIndex(index)->IsPlayerConnected()) ? 1 : 0;
|
||||
}
|
||||
|
||||
static cell_t sm_isplayeringame(IPluginContext *pCtx, const cell_t *params)
|
||||
{
|
||||
int index = params[1];
|
||||
if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
|
||||
{
|
||||
//:TODO: runtime error
|
||||
}
|
||||
|
||||
return (g_PlayerManager.GetPlayerByIndex(index)->IsPlayerInGame()) ? 1 : 0;
|
||||
}
|
||||
|
||||
static cell_t sm_isplayerauthorized(IPluginContext *pCtx, const cell_t *params)
|
||||
{
|
||||
int index = params[1];
|
||||
if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
|
||||
{
|
||||
//:TODO: runtime error
|
||||
}
|
||||
|
||||
return (g_PlayerManager.GetPlayerByIndex(index)->IsPlayerAuthorized()) ? 1 : 0;
|
||||
}
|
||||
|
||||
static cell_t sm_isplayerfakeclient(IPluginContext *pCtx, const cell_t *params)
|
||||
{
|
||||
int index = params[1];
|
||||
if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
|
||||
{
|
||||
//:TODO: runtime error
|
||||
}
|
||||
|
||||
return (g_PlayerManager.GetPlayerByIndex(index)->IsPlayerFakeClient()) ? 1 : 0;
|
||||
}
|
||||
Reference in New Issue
Block a user