diff --git a/core/logic/PluginSys.cpp b/core/logic/PluginSys.cpp index f7e6269d..273e05d3 100644 --- a/core/logic/PluginSys.cpp +++ b/core/logic/PluginSys.cpp @@ -133,7 +133,7 @@ Handle_t CPlugin::GetMyHandle() return m_handle; } -CPlugin *CPlugin::CreatePlugin(const char *file, char *error, size_t maxlength) +CPlugin *CPlugin::Create(const char *file) { char fullpath[PLATFORM_MAX_PATH]; g_pSM->BuildPath(Path_SM, fullpath, sizeof(fullpath), "plugins/%s", file); @@ -142,9 +142,7 @@ CPlugin *CPlugin::CreatePlugin(const char *file, char *error, size_t maxlength) CPlugin *pPlugin = new CPlugin(file); if (!fp) { - if (error) - ke::SafeSprintf(error, maxlength, "Unable to open file"); - pPlugin->m_status = Plugin_BadLoad; + pPlugin->SetErrorState(Plugin_BadLoad, "Unable to open file"); return pPlugin; } @@ -428,7 +426,7 @@ void *CPlugin::GetPluginStructure() } // Only called during plugin construction. -bool CPlugin::TryCompile(char *error, size_t maxlength) +bool CPlugin::TryCompile() { char fullpath[PLATFORM_MAX_PATH]; g_pSM->BuildPath(Path_SM, fullpath, sizeof(fullpath), "plugins/%s", m_filename); @@ -436,16 +434,13 @@ bool CPlugin::TryCompile(char *error, size_t maxlength) char loadmsg[255]; m_pRuntime = g_pSourcePawn2->LoadBinaryFromFile(fullpath, loadmsg, sizeof(loadmsg)); if (!m_pRuntime) { - ke::SafeSprintf(error, maxlength, "Unable to load plugin (%s)", loadmsg); - m_status = Plugin_BadLoad; + SetErrorState(Plugin_BadLoad, "Unable to load plugin (%s)", loadmsg); return false; } // ReadInfo() sets its own error state. - if (!ReadInfo()) { - ke::SafeSprintf(error, maxlength, "%s", m_errormsg); + if (!ReadInfo()) return false; - } m_status = Plugin_Created; return true; @@ -874,7 +869,7 @@ void CPluginManager::LoadPluginsFromDir(const char *basedir, const char *localpa libsys->CloseDirectory(dir); } -LoadRes CPluginManager::LoadPlugin(CPlugin **aResult, const char *path, bool debug, PluginType type, char error[], size_t maxlength) +LoadRes CPluginManager::LoadPlugin(CPlugin **aResult, const char *path, bool debug, PluginType type) { if (m_LoadingLocked) return LoadRes_NeverLoad; @@ -900,7 +895,7 @@ LoadRes CPluginManager::LoadPlugin(CPlugin **aResult, const char *path, bool deb } } - CPlugin *plugin = CompileAndPrep(path, error, maxlength); + CPlugin *plugin = CompileAndPrep(path); // Assign our outparam so we can return early. It must be set. *aResult = plugin; @@ -921,8 +916,9 @@ IPlugin *CPluginManager::LoadPlugin(const char *path, bool debug, PluginType typ LoadRes res; *wasloaded = false; - if ((res=LoadPlugin(&pl, path, true, PluginType_MapUpdated, error, maxlength)) == LoadRes_Failure) + if ((res=LoadPlugin(&pl, path, true, PluginType_MapUpdated)) == LoadRes_Failure) { + ke::SafeStrcpy(error, maxlength, pl->m_errormsg); delete pl; return NULL; } @@ -933,19 +929,11 @@ IPlugin *CPluginManager::LoadPlugin(const char *path, bool debug, PluginType typ return pl; } - if (res == LoadRes_NeverLoad) - { - if (error) - { - if (m_LoadingLocked) - { - ke::SafeSprintf(error, maxlength, "There is a global plugin loading lock in effect"); - } - else - { - ke::SafeSprintf(error, maxlength, "This plugin is blocked from loading (see plugin_settings.cfg)"); - } - } + if (res == LoadRes_NeverLoad) { + if (m_LoadingLocked) + ke::SafeSprintf(error, maxlength, "There is a global plugin loading lock in effect"); + else + ke::SafeSprintf(error, maxlength, "This plugin is blocked from loading (see plugin_settings.cfg)"); return NULL; } @@ -969,15 +957,9 @@ void CPluginManager::LoadAutoPlugin(const char *plugin) { CPlugin *pl = NULL; LoadRes res; - char error[255] = "Unknown error"; - - if ((res=LoadPlugin(&pl, plugin, false, PluginType_MapUpdated, error, sizeof(error))) == LoadRes_Failure) + if ((res=LoadPlugin(&pl, plugin, false, PluginType_MapUpdated)) == LoadRes_Failure) { - g_Logger.LogError("[SM] Failed to load plugin \"%s\": %s.", plugin, error); - pl->SetErrorState( - pl->GetStatus() <= Plugin_Created ? Plugin_BadLoad : pl->GetStatus(), - "%s", - error); + g_Logger.LogError("[SM] Failed to load plugin \"%s\": %s.", plugin, pl->m_errormsg); } if (res == LoadRes_Successful || res == LoadRes_Failure) @@ -1179,19 +1161,19 @@ bool CPluginManager::RequireExtensions(CPlugin *pPlugin, char *error, size_t max return pPlugin->ForEachExtVar(ke::Move(callback)); } -CPlugin *CPluginManager::CompileAndPrep(const char *path, char *error, size_t maxlength) +CPlugin *CPluginManager::CompileAndPrep(const char *path) { - CPlugin *plugin = CPlugin::CreatePlugin(path, error, maxlength); + CPlugin *plugin = CPlugin::Create(path); if (plugin->GetStatus() != Plugin_Uncompiled) { assert(plugin->GetStatus() == Plugin_BadLoad); return plugin; } - if (!plugin->TryCompile(error, maxlength)) + if (!plugin->TryCompile()) return plugin; assert(plugin->GetStatus() == Plugin_Created); - if (!MalwareCheckPass(plugin, error, maxlength)) + if (!MalwareCheckPass(plugin)) return plugin; assert(plugin->GetStatus() == Plugin_Created); @@ -1201,7 +1183,7 @@ CPlugin *CPluginManager::CompileAndPrep(const char *path, char *error, size_t ma } -bool CPluginManager::MalwareCheckPass(CPlugin *pPlugin, char *error, size_t maxlength) +bool CPluginManager::MalwareCheckPass(CPlugin *pPlugin) { unsigned char *pCodeHash = pPlugin->GetRuntime()->GetCodeHash(); @@ -1216,11 +1198,10 @@ bool CPluginManager::MalwareCheckPass(CPlugin *pPlugin, char *error, size_t maxl if (m_bBlockBadPlugins) { if (bulletinUrl[0] != '\0') { - ke::SafeSprintf(error, maxlength, "Known malware detected and blocked. See %s for more info", bulletinUrl); + pPlugin->SetErrorState(Plugin_BadLoad, "Known malware detected and blocked. See %s for more info", bulletinUrl); } else { - ke::SafeSprintf(error, maxlength, "Possible malware or illegal plugin detected and blocked"); + pPlugin->SetErrorState(Plugin_BadLoad, "Possible malware or illegal plugin detected and blocked"); } - pPlugin->m_status = Plugin_BadLoad; return false; } diff --git a/core/logic/PluginSys.h b/core/logic/PluginSys.h index e82c0b90..835ac0f7 100644 --- a/core/logic/PluginSys.h +++ b/core/logic/PluginSys.h @@ -172,7 +172,7 @@ public: * If an error buffer is not specified, the error will be copied to an internal buffer and * a valid (but error-stated) CPlugin will be returned. */ - static CPlugin *CreatePlugin(const char *file, char *error, size_t maxlength); + static CPlugin *Create(const char *file); static inline bool matches(const char *file, const CPlugin *plugin) { @@ -259,7 +259,7 @@ public: protected: bool ReadInfo(); void DependencyDropped(CPlugin *pOwner); - bool TryCompile(char *error, size_t maxlength); + bool TryCompile(); private: time_t GetFileTimeStamp(); @@ -452,7 +452,7 @@ public: void ListPluginsToClient(CPlayer *player, const CCommand &args); 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); void LoadAutoPlugin(const char *plugin); @@ -469,8 +469,8 @@ private: /** * First pass for loading a plugin, and its helpers. */ - CPlugin *CompileAndPrep(const char *path, char *error, size_t maxlength); - bool MalwareCheckPass(CPlugin *pPlugin, char *error, size_t maxlength); + CPlugin *CompileAndPrep(const char *path); + bool MalwareCheckPass(CPlugin *pPlugin); /** * Runs the second loading pass on a plugin.