From c4cba5254c50d7e34599fa51cae60966a0b4dd79 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sat, 28 Feb 2009 23:14:16 -0500 Subject: [PATCH] Added auto-restart functionality to the Updater extensions (bug 3530, r=pred). --- core/GameConfigs.cpp | 6 +++--- core/HalfLife2.cpp | 10 ++++++++++ core/HalfLife2.h | 2 ++ core/sourcemod.cpp | 5 +++++ core/sourcemod.h | 1 + extensions/updater/extension.cpp | 27 +++++++++++++++++++++++++++ extensions/updater/sdk/smsdk_config.h | 2 +- public/IGameHelpers.h | 16 +++++++++++++++- public/ISourceMod.h | 9 ++++++++- 9 files changed, 72 insertions(+), 6 deletions(-) diff --git a/core/GameConfigs.cpp b/core/GameConfigs.cpp index 10d02907..a4509961 100644 --- a/core/GameConfigs.cpp +++ b/core/GameConfigs.cpp @@ -49,9 +49,9 @@ GameConfigManager g_GameConfigs; IGameConfig *g_pGameConf = NULL; -char g_Game[256]; -char g_GameDesc[256] = {'!', '\0'}; -char g_GameName[256] = {'$', '\0'}; +static char g_Game[256]; +static char g_GameDesc[256] = {'!', '\0'}; +static char g_GameName[256] = {'$', '\0'}; #define PSTATE_NONE 0 #define PSTATE_GAMES 1 diff --git a/core/HalfLife2.cpp b/core/HalfLife2.cpp index e813ecca..56437402 100644 --- a/core/HalfLife2.cpp +++ b/core/HalfLife2.cpp @@ -632,3 +632,13 @@ void CHalfLife2::SetHandleEntity(CBaseHandle &hndl, edict_t *pEnt) hndl.Set(pEntOther); } + +const char *CHalfLife2::GetCurrentMap() +{ + return STRING(gpGlobals->mapname); +} + +void CHalfLife2::ServerCommand(const char *buffer) +{ + engine->ServerCommand(buffer); +} diff --git a/core/HalfLife2.h b/core/HalfLife2.h index 80148a06..b2354661 100644 --- a/core/HalfLife2.h +++ b/core/HalfLife2.h @@ -113,6 +113,8 @@ public: //IGameHelpers int IndexOfEdict(edict_t *pEnt); edict_t *GetHandleEntity(CBaseHandle &hndl); void SetHandleEntity(CBaseHandle &hndl, edict_t *pEnt); + const char *GetCurrentMap(); + void ServerCommand(const char *buffer); public: void AddToFakeCliCmdQueue(int client, int userid, const char *cmd); void ProcessFakeCliCmdQueue(); diff --git a/core/sourcemod.cpp b/core/sourcemod.cpp index 0b3d7360..f6e67817 100644 --- a/core/sourcemod.cpp +++ b/core/sourcemod.cpp @@ -717,6 +717,11 @@ const char *SourceModBase::GetCoreConfigValue(const char *key) return g_CoreConfig.GetCoreConfigValue(key); } +int SourceModBase::GetPluginId() +{ + return g_PLID; +} + SMGlobalClass *SMGlobalClass::head = NULL; SMGlobalClass::SMGlobalClass() diff --git a/core/sourcemod.h b/core/sourcemod.h index ecf23921..11b7df6f 100644 --- a/core/sourcemod.h +++ b/core/sourcemod.h @@ -133,6 +133,7 @@ public: // ISourceMod size_t FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list ap); void AddFrameAction(FRAMEACTION fn, void *data); const char *GetCoreConfigValue(const char *key); + int GetPluginId(); private: CStack m_freepacks; char m_SMBaseDir[PLATFORM_MAX_PATH]; diff --git a/extensions/updater/extension.cpp b/extensions/updater/extension.cpp index 1dd0d8f3..331b5fbc 100644 --- a/extensions/updater/extension.cpp +++ b/extensions/updater/extension.cpp @@ -120,6 +120,7 @@ void SmUpdater::NotifyInterfaceDrop(SMInterface *pInterface) static void PumpUpdate(void *data) { String *str; + bool new_files = false; List::iterator iter; char path[PLATFORM_MAX_PATH]; @@ -163,6 +164,7 @@ static void PumpUpdate(void *data) smutils->LogMessage(myself, "Successfully updated gamedata file \"%s\"", part->file); + new_files = true; } skip_create: temp = part->next; @@ -186,6 +188,31 @@ skip_create: smutils->LogError(myself, "--- END ERRORS FROM AUTOMATIC UPDATER ---"); } + + if (new_files) + { + const char *force_restart = smutils->GetCoreConfigValue("ForceRestartAfterUpdate"); + if (force_restart == NULL || strcasecmp(force_restart, "yes") != 0) + { + smutils->LogMessage(myself, + "SourceMod has been updated, please reload it or restart your server."); + } + else + { + char buffer[255]; + smutils->Format(buffer, + sizeof(buffer), + "meta unload %d\n", + smutils->GetPluginId()); + gamehelpers->ServerCommand(buffer); + smutils->Format(buffer, + sizeof(buffer), + "changelevel \"%s\"\n", + gamehelpers->GetCurrentMap()); + gamehelpers->ServerCommand(buffer); + gamehelpers->ServerCommand("echo SourceMod has been restarted from an automatic update.\n"); + } + } } void SmUpdater::RunThread(IThreadHandle *pHandle) diff --git a/extensions/updater/sdk/smsdk_config.h b/extensions/updater/sdk/smsdk_config.h index 0e29316a..fda8eb95 100644 --- a/extensions/updater/sdk/smsdk_config.h +++ b/extensions/updater/sdk/smsdk_config.h @@ -66,7 +66,7 @@ //#define SMEXT_ENABLE_DBMANAGER #define SMEXT_ENABLE_GAMECONF //#define SMEXT_ENABLE_MEMUTILS -//#define SMEXT_ENABLE_GAMEHELPERS +#define SMEXT_ENABLE_GAMEHELPERS //#define SMEXT_ENABLE_TIMERSYS #define SMEXT_ENABLE_THREADER #define SMEXT_ENABLE_LIBSYS diff --git a/public/IGameHelpers.h b/public/IGameHelpers.h index 64c5de2c..8bf5a751 100644 --- a/public/IGameHelpers.h +++ b/public/IGameHelpers.h @@ -40,7 +40,7 @@ */ #define SMINTERFACE_GAMEHELPERS_NAME "IGameHelpers" -#define SMINTERFACE_GAMEHELPERS_VERSION 3 +#define SMINTERFACE_GAMEHELPERS_VERSION 4 class CBaseEntity; class CBaseHandle; @@ -179,6 +179,20 @@ namespace SourceMod * @noreturn */ virtual void SetHandleEntity(CBaseHandle &hndl, edict_t *pEnt) =0; + + /** + * @brief Returns the current map name. + * + * @return Current map name. + */ + virtual const char *GetCurrentMap() =0; + + /** + * @brief Wraps IVEngineServer::ServerCommand. + * + * @param buffer Command buffer (does not auto \n terminate). + */ + virtual void ServerCommand(const char *buffer) =0; }; } diff --git a/public/ISourceMod.h b/public/ISourceMod.h index 69fe1f0c..fd7944d3 100644 --- a/public/ISourceMod.h +++ b/public/ISourceMod.h @@ -43,7 +43,7 @@ #include #define SMINTERFACE_SOURCEMOD_NAME "ISourceMod" -#define SMINTERFACE_SOURCEMOD_VERSION 10 +#define SMINTERFACE_SOURCEMOD_VERSION 11 /** * @brief Forward declaration of the KeyValues class. @@ -296,6 +296,13 @@ namespace SourceMod * The string will be destroyed on core.cfg reparses. */ virtual const char *GetCoreConfigValue(const char *key) = 0; + + /** + * @brief Returns SourceMod's Metamod:Source plugin ID. + * + * @return Metamod:Source PluginId. + */ + virtual int GetPluginId() = 0; }; }