From 2adae275573c2e7dd2b8b459ddc4b1bff049d9f9 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sun, 16 Aug 2015 13:08:18 -0700 Subject: [PATCH] Clean up CPlugin member variables. --- core/logic/PluginSys.cpp | 70 +++++++++++++-------------------------- core/logic/PluginSys.h | 48 ++++++++++++++++----------- core/logic/Translator.cpp | 2 +- core/logic/Translator.h | 3 +- 4 files changed, 55 insertions(+), 68 deletions(-) diff --git a/core/logic/PluginSys.cpp b/core/logic/PluginSys.cpp index 76c61d33..bd4f7493 100644 --- a/core/logic/PluginSys.cpp +++ b/core/logic/PluginSys.cpp @@ -52,23 +52,26 @@ HandleType_t g_PluginType = 0; IdentityType_t g_PluginIdent = 0; CPlugin::CPlugin(const char *file) + : m_serial(0), + m_status(Plugin_Uncompiled), + m_SilentFailure(false), + m_FakeNativesMissing(false), + m_LibraryMissing(false), + m_pContext(nullptr), + m_MaxClientsVar(nullptr), + m_ident(nullptr), + m_bGotAllLoaded(false), + m_FileVersion(0), + m_LastAccess(0), + m_handle(BAD_HANDLE) { static int MySerial = 0; - m_type = PluginType_Private; - m_status = Plugin_Uncompiled; - m_bSilentlyFailed = false; m_serial = ++MySerial; - m_pRuntime = NULL; - m_errormsg[sizeof(m_errormsg) - 1] = '\0'; + m_errormsg[0] = '\0'; ke::SafeSprintf(m_filename, sizeof(m_filename), "%s", file); - m_handle = 0; - m_ident = NULL; - m_FakeNativesMissing = false; - m_LibraryMissing = false; - m_bGotAllLoaded = false; + m_pPhrases = g_Translator.CreatePhraseCollection(); - m_MaxClientsVar = NULL; } CPlugin::~CPlugin() @@ -83,22 +86,9 @@ CPlugin::~CPlugin() g_ShareSys.DestroyIdentity(m_ident); } - if (m_pRuntime != NULL) - { - delete m_pRuntime; - m_pRuntime = NULL; - } - for (size_t i=0; iDestroy(); - m_pPhrases = NULL; - } } void CPlugin::InitIdentity() @@ -120,25 +110,16 @@ unsigned int CPlugin::CalcMemUsage() + (m_configs.size() * (sizeof(AutoConfig *) + sizeof(AutoConfig))) + m_Props.mem_usage(); - for (unsigned int i = 0; i < m_configs.size(); i++) - { + for (unsigned int i = 0; i < m_configs.size(); i++) { base_size += m_configs[i]->autocfg.size(); base_size += m_configs[i]->folder.size(); } - for (List::iterator i = m_Libraries.begin(); - i != m_Libraries.end(); - i++) - { + for (auto i = m_Libraries.begin(); i != m_Libraries.end(); i++) base_size += (*i).size(); - } - for (List::iterator i = m_RequiredLibs.begin(); - i != m_RequiredLibs.end(); - i++) - { + for (auto i = m_RequiredLibs.begin(); i != m_RequiredLibs.end(); i++) base_size += (*i).size(); - } return base_size; } @@ -156,12 +137,9 @@ CPlugin *CPlugin::CreatePlugin(const char *file, char *error, size_t maxlength) CPlugin *pPlugin = new CPlugin(file); - if (!fp) - { + if (!fp) { if (error) - { ke::SafeSprintf(error, maxlength, "Unable to open file"); - } pPlugin->m_status = Plugin_BadLoad; return pPlugin; } @@ -481,7 +459,7 @@ const char *CPlugin::GetFilename() PluginType CPlugin::GetType() { - return m_type; + return PluginType_MapUpdated; } const sm_plugininfo_t *CPlugin::GetPublicInfo() @@ -505,7 +483,7 @@ PluginStatus CPlugin::GetStatus() bool CPlugin::IsSilentlyFailed() { - return m_bSilentlyFailed; + return m_SilentFailure; } bool CPlugin::IsDebugging() @@ -518,9 +496,9 @@ bool CPlugin::IsDebugging() return true; } -void CPlugin::SetSilentlyFailed(bool sf) +void CPlugin::SetSilentlyFailed() { - m_bSilentlyFailed = sf; + m_SilentFailure = true; } void CPlugin::LibraryActions(LibraryAction action) @@ -922,8 +900,6 @@ LoadRes CPluginManager::_LoadPlugin(CPlugin **aResult, const char *path, bool de pPlugin = CPlugin::CreatePlugin(path, error, maxlength); assert(pPlugin != NULL); - pPlugin->m_type = PluginType_MapUpdated; - if (pPlugin->m_status == Plugin_Uncompiled) { char fullpath[PLATFORM_MAX_PATH]; @@ -1001,7 +977,7 @@ LoadRes CPluginManager::_LoadPlugin(CPlugin **aResult, const char *path, bool de case APLRes_SilentFailure: pPlugin->SetErrorState(Plugin_Failed, "%s", error); loadFailure = LoadRes_SilentFailure; - pPlugin->SetSilentlyFailed(true); + pPlugin->SetSilentlyFailed(); break; default: assert(false); diff --git a/core/logic/PluginSys.h b/core/logic/PluginSys.h index 7fa9a929..8e08b2f5 100644 --- a/core/logic/PluginSys.h +++ b/core/logic/PluginSys.h @@ -50,6 +50,7 @@ #include "IGameConfigs.h" #include "NativeOwner.h" #include "ShareSys.h" +#include "PhraseCollection.h" #include class CPlayer; @@ -140,7 +141,7 @@ public: bool IsDebugging(); PluginStatus GetStatus(); bool IsSilentlyFailed(); - void SetSilentlyFailed(bool sf); + void SetSilentlyFailed(); const sm_plugininfo_t *GetPublicInfo(); bool SetPauseState(bool paused); unsigned int GetSerial(); @@ -251,31 +252,40 @@ protected: bool UpdateInfo(); void SetTimeStamp(time_t t); void DependencyDropped(CPlugin *pOwner); + private: - PluginType m_type; + // This information is static for the lifetime of the plugin. char m_filename[PLATFORM_MAX_PATH]; - PluginStatus m_status; - bool m_bSilentlyFailed; unsigned int m_serial; - sm_plugininfo_t m_info; - char m_errormsg[256]; - time_t m_LastAccess; - IdentityToken_t *m_ident; - Handle_t m_handle; - bool m_WasRunning; - IPhraseCollection *m_pPhrases; - List m_RequiredLibs; - List m_Libraries; - StringHashMap m_Props; + + PluginStatus m_status; + + // Statuses that are set during failure. + bool m_SilentFailure; bool m_FakeNativesMissing; bool m_LibraryMissing; - CVector m_configs; - bool m_bGotAllLoaded; - int m_FileVersion; - char m_DateTime[256]; - IPluginRuntime *m_pRuntime; + char m_errormsg[256]; + + // Internal properties that must by reset if the runtime is evicted. + ke::AutoPtr m_pRuntime; + ke::AutoPtr m_pPhrases; IPluginContext *m_pContext; sp_pubvar_t *m_MaxClientsVar; + StringHashMap m_Props; + CVector m_configs; + IdentityToken_t *m_ident; + bool m_bGotAllLoaded; + int m_FileVersion; + + // Information that survives past eviction. + List m_RequiredLibs; + List m_Libraries; + time_t m_LastAccess; + Handle_t m_handle; + char m_DateTime[256]; + + // Cached. + sm_plugininfo_t m_info; }; class CPluginManager : diff --git a/core/logic/Translator.cpp b/core/logic/Translator.cpp index 05e9684d..dbd49ac0 100644 --- a/core/logic/Translator.cpp +++ b/core/logic/Translator.cpp @@ -991,7 +991,7 @@ unsigned int Translator::GetInterfaceVersion() return SMINTERFACE_TRANSLATOR_VERSION; } -IPhraseCollection *Translator::CreatePhraseCollection() +CPhraseCollection *Translator::CreatePhraseCollection() { return new CPhraseCollection(); } diff --git a/core/logic/Translator.h b/core/logic/Translator.h index 601e9b41..cd88728f 100644 --- a/core/logic/Translator.h +++ b/core/logic/Translator.h @@ -39,6 +39,7 @@ #include "sm_memtable.h" #include "ITextParsers.h" #include +#include "PhraseCollection.h" /* :TODO: write a templatized version of tries? */ @@ -127,7 +128,7 @@ public: //ITranslator unsigned int GetClientLanguage(int client); const char *GetInterfaceName(); unsigned int GetInterfaceVersion(); - IPhraseCollection *CreatePhraseCollection(); + CPhraseCollection *CreatePhraseCollection(); int SetGlobalTarget(int index); int GetGlobalTarget() const; size_t FormatString(