Allow plugin info fields to outlive their runtime.

This commit is contained in:
David Anderson 2015-08-16 13:20:47 -07:00
parent 2adae27557
commit d473b0441d
2 changed files with 36 additions and 29 deletions

View File

@ -71,6 +71,8 @@ CPlugin::CPlugin(const char *file)
m_errormsg[0] = '\0'; m_errormsg[0] = '\0';
ke::SafeSprintf(m_filename, sizeof(m_filename), "%s", file); ke::SafeSprintf(m_filename, sizeof(m_filename), "%s", file);
memset(&m_info, 0, sizeof(m_info));
m_pPhrases = g_Translator.CreatePhraseCollection(); m_pPhrases = g_Translator.CreatePhraseCollection();
} }
@ -208,10 +210,7 @@ bool CPlugin::UpdateInfo()
IPluginContext *base = GetBaseContext(); IPluginContext *base = GetBaseContext();
int err = base->FindPubvarByName("myinfo", &idx); int err = base->FindPubvarByName("myinfo", &idx);
memset(&m_info, 0, sizeof(m_info)); if (err == SP_ERROR_NONE) {
if (err == SP_ERROR_NONE)
{
struct sm_plugininfo_c_t struct sm_plugininfo_c_t
{ {
cell_t name; cell_t name;
@ -223,22 +222,24 @@ bool CPlugin::UpdateInfo()
sm_plugininfo_c_t *cinfo; sm_plugininfo_c_t *cinfo;
cell_t local_addr; cell_t local_addr;
auto update_field = [base](cell_t addr, ke::AString *dest) {
const char* ptr;
if (base->LocalToString(addr, (char **)&ptr) == SP_ERROR_NONE)
*dest = ptr;
else
*dest = "";
};
base->GetPubvarAddrs(idx, &local_addr, (cell_t **)&cinfo); base->GetPubvarAddrs(idx, &local_addr, (cell_t **)&cinfo);
base->LocalToString(cinfo->name, (char **)&m_info.name); update_field(cinfo->name, &info_name_);
base->LocalToString(cinfo->description, (char **)&m_info.description); update_field(cinfo->description, &info_description_);
base->LocalToString(cinfo->author, (char **)&m_info.author); update_field(cinfo->author, &info_author_);
base->LocalToString(cinfo->url, (char **)&m_info.url); update_field(cinfo->version, &info_version_);
base->LocalToString(cinfo->version, (char **)&m_info.version); update_field(cinfo->url, &info_url_);
} }
m_info.author = m_info.author ? m_info.author : ""; ke::SafeStrcpy(m_DateTime, sizeof(m_DateTime), "unknown");
m_info.description = m_info.description ? m_info.description : ""; if ((err = base->FindPubvarByName("__version", &idx)) == SP_ERROR_NONE) {
m_info.name = m_info.name ? m_info.name : "";
m_info.url = m_info.url ? m_info.url : "";
m_info.version = m_info.version ? m_info.version : "";
if ((err = base->FindPubvarByName("__version", &idx)) == SP_ERROR_NONE)
{
struct __version_info struct __version_info
{ {
cell_t version; cell_t version;
@ -255,28 +256,24 @@ bool CPlugin::UpdateInfo()
base->GetPubvarAddrs(idx, &local_addr, (cell_t **)&info); base->GetPubvarAddrs(idx, &local_addr, (cell_t **)&info);
m_FileVersion = info->version; m_FileVersion = info->version;
if (m_FileVersion >= 4) if (m_FileVersion >= 4) {
{
base->LocalToString(info->date, (char **)&pDate); base->LocalToString(info->date, (char **)&pDate);
base->LocalToString(info->time, (char **)&pTime); base->LocalToString(info->time, (char **)&pTime);
ke::SafeSprintf(m_DateTime, sizeof(m_DateTime), "%s %s", pDate, pTime); ke::SafeSprintf(m_DateTime, sizeof(m_DateTime), "%s %s", pDate, pTime);
} }
if (m_FileVersion > 5) if (m_FileVersion > 5) {
{
base->LocalToString(info->filevers, (char **)&pFileVers); base->LocalToString(info->filevers, (char **)&pFileVers);
SetErrorState(Plugin_Failed, "Newer SourceMod required (%s or higher)", pFileVers); SetErrorState(Plugin_Failed, "Newer SourceMod required (%s or higher)", pFileVers);
return false; return false;
} }
} } else {
else
{
m_FileVersion = 0; m_FileVersion = 0;
} }
if ((err = base->FindPubvarByName("MaxClients", &idx)) == SP_ERROR_NONE) if ((err = base->FindPubvarByName("MaxClients", &idx)) == SP_ERROR_NONE)
{
base->GetPubvarByIndex(idx, &m_MaxClientsVar); base->GetPubvarByIndex(idx, &m_MaxClientsVar);
} else
m_MaxClientsVar = nullptr;
return true; return true;
} }
@ -465,9 +462,13 @@ PluginType CPlugin::GetType()
const sm_plugininfo_t *CPlugin::GetPublicInfo() const sm_plugininfo_t *CPlugin::GetPublicInfo()
{ {
if (GetStatus() >= Plugin_Created) if (GetStatus() >= Plugin_Created)
{ return nullptr;
return NULL;
} m_info.author = info_author_.chars();
m_info.description = info_description_.chars();
m_info.name = info_name_.chars();
m_info.url = info_url_.chars();
m_info.version = info_version_.chars();
return &m_info; return &m_info;
} }

View File

@ -51,6 +51,7 @@
#include "NativeOwner.h" #include "NativeOwner.h"
#include "ShareSys.h" #include "ShareSys.h"
#include "PhraseCollection.h" #include "PhraseCollection.h"
#include <am-string.h>
#include <bridge/include/IScriptManager.h> #include <bridge/include/IScriptManager.h>
class CPlayer; class CPlayer;
@ -286,6 +287,11 @@ private:
// Cached. // Cached.
sm_plugininfo_t m_info; sm_plugininfo_t m_info;
ke::AString info_name_;
ke::AString info_author_;
ke::AString info_description_;
ke::AString info_version_;
ke::AString info_url_;
}; };
class CPluginManager : class CPluginManager :