Added MaxClients public var to replace GetMaxClients (bug 3283, r=pred).

This is a hard bump of the plugin version number.  1.1 plugins compiled against this include set
will not run against earlier versions of SourceMod.
This commit is contained in:
David Anderson 2008-10-05 15:20:17 -07:00
parent d9af295a29
commit f50c7469ca
7 changed files with 151 additions and 36 deletions

View File

@ -251,6 +251,8 @@ void PlayerManager::OnServerActivate(edict_t *pEdictList, int edictCount, int cl
memset(m_AuthQueue, 0, sizeof(unsigned int) * (ABSOLUTE_PLAYER_LIMIT + 1)); memset(m_AuthQueue, 0, sizeof(unsigned int) * (ABSOLUTE_PLAYER_LIMIT + 1));
g_NumPlayersToAuth = &m_AuthQueue[0]; g_NumPlayersToAuth = &m_AuthQueue[0];
g_PluginSys.SyncMaxClients(clientMax);
} }
g_Extensions.CallOnCoreMapStart(pEdictList, edictCount, clientMax); g_Extensions.CallOnCoreMapStart(pEdictList, edictCount, clientMax);
m_onActivate->Execute(NULL); m_onActivate->Execute(NULL);

View File

@ -622,6 +622,11 @@ static cell_t FindPluginByNumber(IPluginContext *pContext, const cell_t *params)
return pPlugin->GetMyHandle(); return pPlugin->GetMyHandle();
} }
static cell_t VerifyCoreVersion(IPluginContext *pContext, const cell_t *params)
{
return 4;
}
REGISTER_NATIVES(coreNatives) REGISTER_NATIVES(coreNatives)
{ {
{"AutoExecConfig", AutoExecConfig}, {"AutoExecConfig", AutoExecConfig},
@ -645,6 +650,7 @@ REGISTER_NATIVES(coreNatives)
{"LogToFileEx", LogToFileEx}, {"LogToFileEx", LogToFileEx},
{"GetExtensionFileStatus", GetExtensionFileStatus}, {"GetExtensionFileStatus", GetExtensionFileStatus},
{"FindPluginByNumber", FindPluginByNumber}, {"FindPluginByNumber", FindPluginByNumber},
{"VerifyCoreVersion", VerifyCoreVersion},
{NULL, NULL}, {NULL, NULL},
}; };

View File

@ -68,6 +68,7 @@ CPlugin::CPlugin(const char *file)
m_LibraryMissing = false; m_LibraryMissing = false;
m_bGotAllLoaded = false; m_bGotAllLoaded = false;
m_pPhrases = g_Translator.CreatePhraseCollection(); m_pPhrases = g_Translator.CreatePhraseCollection();
m_MaxClientsVar = NULL;
} }
CPlugin::~CPlugin() CPlugin::~CPlugin()
@ -218,7 +219,7 @@ void CPlugin::SetErrorState(PluginStatus status, const char *error_fmt, ...)
} }
} }
void CPlugin::UpdateInfo() bool CPlugin::UpdateInfo()
{ {
/* Now grab the info */ /* Now grab the info */
uint32_t idx; uint32_t idx;
@ -265,7 +266,7 @@ void CPlugin::UpdateInfo()
}; };
__version_info *info; __version_info *info;
cell_t local_addr; cell_t local_addr;
const char *pDate, *pTime; const char *pDate, *pTime, *pFileVers;
pDate = ""; pDate = "";
pTime = ""; pTime = "";
@ -278,11 +279,34 @@ void CPlugin::UpdateInfo()
base->LocalToString(info->time, (char **)&pTime); base->LocalToString(info->time, (char **)&pTime);
UTIL_Format(m_DateTime, sizeof(m_DateTime), "%s %s", pDate, pTime); UTIL_Format(m_DateTime, sizeof(m_DateTime), "%s %s", pDate, pTime);
} }
if (m_FileVersion > 4)
{
base->LocalToString(info->filevers, (char **)&pFileVers);
SetErrorState(Plugin_Failed, "Newer SourceMod required (%s or higher)", pFileVers);
return false;
}
} }
else else
{ {
m_FileVersion = 0; m_FileVersion = 0;
} }
if ((err = base->FindPubvarByName("MaxClients", &idx)) == SP_ERROR_NONE)
{
base->GetPubvarByIndex(idx, &m_MaxClientsVar);
}
return true;
}
void CPlugin::SyncMaxClients(int max_clients)
{
if (m_MaxClientsVar == NULL)
{
return;
}
*m_MaxClientsVar->offs = max_clients;
} }
void CPlugin::Call_OnPluginStart() void CPlugin::Call_OnPluginStart()
@ -294,6 +318,8 @@ void CPlugin::Call_OnPluginStart()
m_status = Plugin_Running; m_status = Plugin_Running;
SyncMaxClients(g_Players.MaxClients());
cell_t result; cell_t result;
IPluginFunction *pFunction = m_pRuntime->GetFunctionByName("OnPluginStart"); IPluginFunction *pFunction = m_pRuntime->GetFunctionByName("OnPluginStart");
if (!pFunction) if (!pFunction)
@ -427,6 +453,10 @@ PluginType CPlugin::GetType()
const sm_plugininfo_t *CPlugin::GetPublicInfo() const sm_plugininfo_t *CPlugin::GetPublicInfo()
{ {
if (GetStatus() >= Plugin_Created)
{
return NULL;
}
return &m_info; return &m_info;
} }
@ -959,11 +989,18 @@ LoadRes CPluginManager::_LoadPlugin(CPlugin **_plugin, const char *path, bool de
"Unable to load plugin (error %d: %s)", "Unable to load plugin (error %d: %s)",
err, err,
g_pSourcePawn2->GetErrorString(err)); g_pSourcePawn2->GetErrorString(err));
pPlugin->m_status = Plugin_BadLoad;
} }
else else
{ {
pPlugin->UpdateInfo(); if (pPlugin->UpdateInfo())
pPlugin->m_status = Plugin_Created; {
pPlugin->m_status = Plugin_Created;
}
else
{
UTIL_Format(error, maxlength, "%s", pPlugin->m_errormsg);
}
} }
} }
@ -1052,10 +1089,13 @@ void CPluginManager::LoadAutoPlugin(const char *plugin)
if ((res=_LoadPlugin(&pl, plugin, false, PluginType_MapUpdated, error, sizeof(error))) == LoadRes_Failure) if ((res=_LoadPlugin(&pl, plugin, false, PluginType_MapUpdated, error, sizeof(error))) == LoadRes_Failure)
{ {
g_Logger.LogError("[SM] Failed to load plugin \"%s\": %s", plugin, error); g_Logger.LogError("[SM] Failed to load plugin \"%s\": %s", plugin, error);
pl->SetErrorState(Plugin_Failed, "%s", error); pl->SetErrorState(
pl->GetStatus() == Plugin_BadLoad ? Plugin_BadLoad : Plugin_Failed,
"%s",
error);
} }
if (res == LoadRes_Successful) if (res == LoadRes_Successful || res == LoadRes_Failure)
{ {
AddPlugin(pl); AddPlugin(pl);
} }
@ -1458,7 +1498,7 @@ bool CPluginManager::UnloadPlugin(IPlugin *plugin)
} }
IPluginContext *pContext = plugin->GetBaseContext(); IPluginContext *pContext = plugin->GetBaseContext();
if (pContext->IsInExec()) if (pContext != NULL && pContext->IsInExec())
{ {
char buffer[255]; char buffer[255];
UTIL_Format(buffer, sizeof(buffer), "sm plugins unload %s\n", plugin->GetFilename()); UTIL_Format(buffer, sizeof(buffer), "sm plugins unload %s\n", plugin->GetFilename());
@ -1925,14 +1965,21 @@ void CPluginManager::OnRootConsoleCommand(const char *cmdname, const CCommand &c
{ {
len += UTIL_Format(buffer, sizeof(buffer), " %02d", id); len += UTIL_Format(buffer, sizeof(buffer), " %02d", id);
} }
len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " \"%s\"", (IS_STR_FILLED(info->name)) ? info->name : pl->GetFilename()); if (pl->GetStatus() < Plugin_Created)
if (IS_STR_FILLED(info->version))
{ {
len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", info->version); len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " \"%s\"", (IS_STR_FILLED(info->name)) ? info->name : pl->GetFilename());
if (IS_STR_FILLED(info->version))
{
len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", info->version);
}
if (IS_STR_FILLED(info->author))
{
UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", info->author);
}
} }
if (IS_STR_FILLED(info->author)) else
{ {
UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", info->author); UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", pl->m_filename);
} }
g_RootMenu.ConsolePrint("%s", buffer); g_RootMenu.ConsolePrint("%s", buffer);
} }
@ -2025,8 +2072,16 @@ void CPluginManager::OnRootConsoleCommand(const char *cmdname, const CCommand &c
} }
char name[PLATFORM_MAX_PATH]; char name[PLATFORM_MAX_PATH];
const sm_plugininfo_t *info = pl->GetPublicInfo();
strcpy(name, (IS_STR_FILLED(info->name)) ? info->name : pl->GetFilename()); if (pl->GetStatus() < Plugin_Created)
{
const sm_plugininfo_t *info = pl->GetPublicInfo();
UTIL_Format(name, sizeof(name), (IS_STR_FILLED(info->name)) ? info->name : pl->GetFilename());
}
else
{
UTIL_Format(name, sizeof(name), "%s", pl->GetFilename());
}
if (UnloadPlugin(pl)) if (UnloadPlugin(pl))
{ {
@ -2172,12 +2227,15 @@ void CPluginManager::OnRootConsoleCommand(const char *cmdname, const CCommand &c
else else
{ {
g_RootMenu.ConsolePrint(" Load error: %s", pl->m_errormsg); g_RootMenu.ConsolePrint(" Load error: %s", pl->m_errormsg);
g_RootMenu.ConsolePrint(" File info: (title \"%s\") (version \"%s\")", if (pl->GetStatus() < Plugin_Created)
info->name ? info->name : "<none>",
info->version ? info->version : "<none>");
if (IS_STR_FILLED(info->url))
{ {
g_RootMenu.ConsolePrint(" File URL: %s", info->url); g_RootMenu.ConsolePrint(" File info: (title \"%s\") (version \"%s\")",
info->name ? info->name : "<none>",
info->version ? info->version : "<none>");
if (IS_STR_FILLED(info->url))
{
g_RootMenu.ConsolePrint(" File URL: %s", info->url);
}
} }
} }
@ -2551,3 +2609,18 @@ CPlugin *CPluginManager::FindPluginByConsoleArg(const char *arg)
return pl; return pl;
} }
void CPluginManager::OnSourceModMaxPlayersChanged(int newvalue)
{
SyncMaxClients(newvalue);
}
void CPluginManager::SyncMaxClients(int max_clients)
{
List<CPlugin *>::iterator iter;
for (iter = m_plugins.begin(); iter != m_plugins.end(); iter++)
{
(*iter)->SyncMaxClients(max_clients);
}
}

View File

@ -234,8 +234,9 @@ public:
m_Libraries.push_back(name); m_Libraries.push_back(name);
} }
void LibraryActions(bool dropping); void LibraryActions(bool dropping);
void SyncMaxClients(int max_clients);
protected: protected:
void UpdateInfo(); bool UpdateInfo();
void SetTimeStamp(time_t t); void SetTimeStamp(time_t t);
void DependencyDropped(CPlugin *pOwner); void DependencyDropped(CPlugin *pOwner);
private: private:
@ -261,6 +262,7 @@ private:
char m_DateTime[256]; char m_DateTime[256];
IPluginRuntime *m_pRuntime; IPluginRuntime *m_pRuntime;
IPluginContext *m_pContext; IPluginContext *m_pContext;
sp_pubvar_t *m_MaxClientsVar;
}; };
class CPluginManager : class CPluginManager :
@ -307,6 +309,7 @@ public: //IPluginManager
public: //SMGlobalClass public: //SMGlobalClass
void OnSourceModAllInitialized(); void OnSourceModAllInitialized();
void OnSourceModShutdown(); void OnSourceModShutdown();
void OnSourceModMaxPlayersChanged(int newvalue);
public: //IHandleTypeDispatch public: //IHandleTypeDispatch
void OnHandleDestroy(HandleType_t type, void *object); void OnHandleDestroy(HandleType_t type, void *object);
bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize); bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize);
@ -390,6 +393,8 @@ public:
void UnloadAll(); void UnloadAll();
CPlugin *FindPluginByConsoleArg(const char *arg); CPlugin *FindPluginByConsoleArg(const char *arg);
void SyncMaxClients(int max_clients);
private: private:
LoadRes _LoadPlugin(CPlugin **pPlugin, const char *path, bool debug, PluginType type, char error[], size_t maxlength); LoadRes _LoadPlugin(CPlugin **pPlugin, const char *path, bool debug, PluginType type, char error[], size_t maxlength);

View File

@ -237,8 +237,7 @@ public Action:Command_SmHsay(client, args)
decl String:nameBuf[MAX_NAME_LENGTH]; decl String:nameBuf[MAX_NAME_LENGTH];
new maxClients = GetMaxClients(); for (new i = 1; i <= MaxClients; i++)
for (new i = 1; i <= maxClients; i++)
{ {
if (!IsClientConnected(i) || IsFakeClient(i)) if (!IsClientConnected(i) || IsFakeClient(i))
{ {
@ -270,7 +269,6 @@ public Action:Command_SmTsay(client, args)
GetClientName(client, name, sizeof(name)); GetClientName(client, name, sizeof(name));
new color = FindColor(colorStr); new color = FindColor(colorStr);
new maxClients = GetMaxClients();
new String:nameBuf[MAX_NAME_LENGTH]; new String:nameBuf[MAX_NAME_LENGTH];
if (color == -1) if (color == -1)
@ -279,7 +277,7 @@ public Action:Command_SmTsay(client, args)
len = 0; len = 0;
} }
for (new i = 1; i <= maxClients; i++) for (new i = 1; i <= MaxClients; i++)
{ {
if (!IsClientConnected(i) || IsFakeClient(i)) if (!IsClientConnected(i) || IsFakeClient(i))
{ {
@ -407,11 +405,9 @@ FindColor(String:color[])
SendChatToAll(client, String:message[]) SendChatToAll(client, String:message[])
{ {
new maxClients;
new String:nameBuf[MAX_NAME_LENGTH]; new String:nameBuf[MAX_NAME_LENGTH];
maxClients = GetMaxClients(); for (new i = 1; i <= MaxClients; i++)
for (new i = 1; i <= maxClients; i++)
{ {
if (!IsClientConnected(i) || IsFakeClient(i)) if (!IsClientConnected(i) || IsFakeClient(i))
{ {
@ -433,9 +429,8 @@ SendChatToAll(client, String:message[])
DisplayCenterTextToAll(client, String:message[]) DisplayCenterTextToAll(client, String:message[])
{ {
new String:nameBuf[MAX_NAME_LENGTH]; new String:nameBuf[MAX_NAME_LENGTH];
new maxClients = GetMaxClients();
for (new i = 1; i < maxClients; i++) for (new i = 1; i < MaxClients; i++)
{ {
if (!IsClientConnected(i) || IsFakeClient(i)) if (!IsClientConnected(i) || IsFakeClient(i))
{ {
@ -448,9 +443,7 @@ DisplayCenterTextToAll(client, String:message[])
SendChatToAdmins(String:name[], String:message[]) SendChatToAdmins(String:name[], String:message[])
{ {
new iMaxClients = GetMaxClients(); for (new i = 1; i <= MaxClients; i++)
for (new i = 1; i <= iMaxClients; i++)
{ {
if (IsClientInGame(i)) if (IsClientInGame(i))
{ {
@ -500,8 +493,7 @@ SendPanelToAll(String:name[], String:message[])
SetPanelCurrentKey(mSayPanel, 10); SetPanelCurrentKey(mSayPanel, 10);
DrawPanelItem(mSayPanel, "Exit", ITEMDRAW_CONTROL); DrawPanelItem(mSayPanel, "Exit", ITEMDRAW_CONTROL);
new MaxClients = GetMaxClients(); for(new i = 1; i <= MaxClients; i++)
for(new i = 1; i < MaxClients; i++)
{ {
if(IsClientInGame(i) && !IsFakeClient(i)) if(IsClientInGame(i) && !IsFakeClient(i))
{ {

View File

@ -45,9 +45,19 @@ enum NetFlow
NetFlow_Both, /**< Both values added together */ NetFlow_Both, /**< Both values added together */
}; };
#define MAXPLAYERS 64 /**< Maximum number of players that can be in server */ /**
* MAXPLAYERS is not the same as MaxClients.
* MAXPLAYERS is a hardcoded value as an upper limit. MaxClients changes based on the server.
*
* Both GetMaxClients() and MaxClients are only available once the map is loaded, and should
* not be used in OnPluginStart().
*/
#define MAXPLAYERS 64 /**< Maximum number of players SourceMod supports */
#define MAX_NAME_LENGTH 32 /**< Maximum buffer required to store a client name */ #define MAX_NAME_LENGTH 32 /**< Maximum buffer required to store a client name */
public const MaxClients; /**< Maximum number of players the server supports (dynamic) */
/** /**
* Called on client connection. * Called on client connection.
* *
@ -169,10 +179,16 @@ forward OnClientPostAdminFilter(client);
forward OnClientPostAdminCheck(client); forward OnClientPostAdminCheck(client);
/** /**
* This function will be deprecated in a future release. Use the MaxClients variable instead.
*
* Returns the maximum number of clients allowed on the server. This may * Returns the maximum number of clients allowed on the server. This may
* return 0 if called before OnMapStart(), and thus should not be called * return 0 if called before OnMapStart(), and thus should not be called
* in OnPluginStart(). * in OnPluginStart().
* *
* You should not globally cache the value to GetMaxClients() because it can change from
* SourceTV or TF2's arena mode. Use the "MaxClients" dynamic variable documented at the
* top of this file.
*
* @return Maximum number of clients allowed. * @return Maximum number of clients allowed.
*/ */
native GetMaxClients(); native GetMaxClients();

View File

@ -37,7 +37,9 @@
#include <version> #include <version>
#define SOURCEMOD_PLUGINAPI_VERSION 3 /** If this gets changed, you need to update Core's check. */
#define SOURCEMOD_PLUGINAPI_VERSION 4
struct PlVers struct PlVers
{ {
version, version,
@ -136,6 +138,25 @@ struct SharedPlugin
public Float:NULL_VECTOR[3]; /**< Pass this into certain functions to act as a C++ NULL */ public Float:NULL_VECTOR[3]; /**< Pass this into certain functions to act as a C++ NULL */
public const String:NULL_STRING[1]; /**< pass this into certain functions to act as a C++ NULL */ public const String:NULL_STRING[1]; /**< pass this into certain functions to act as a C++ NULL */
/**
* Horrible compatibility shim.
*/
public Extension:__ext_core =
{
name = "Core",
file = "core",
autoload = 0,
required = 0,
};
native VerifyCoreVersion();
public __ext_core_SetNTVOptional()
{
VerifyCoreVersion();
}
#define AUTOLOAD_EXTENSIONS #define AUTOLOAD_EXTENSIONS
#define REQUIRE_EXTENSIONS #define REQUIRE_EXTENSIONS
#define REQUIRE_PLUGIN #define REQUIRE_PLUGIN