Skip to content

Instantly share code, notes, and snippets.

@DDuarte
Last active August 29, 2015 14:05
Show Gist options
  • Save DDuarte/6630bbe9dfe06abeb1b3 to your computer and use it in GitHub Desktop.
Save DDuarte/6630bbe9dfe06abeb1b3 to your computer and use it in GitHub Desktop.
diff --git a/src/server/game/World/World.cpp b/src/server/game/World/World.cpp
index f9c8c80..79dc565 100644
--- a/src/server/game/World/World.cpp
+++ b/src/server/game/World/World.cpp
@@ -2552,7 +2552,7 @@ void World::_UpdateGameTime()
}
/// Shutdown the server
-void World::ShutdownServ(uint32 time, uint32 options, uint8 exitcode)
+void World::ShutdownServ(uint32 time, uint32 options, uint8 exitcode, const std::string& reason)
{
// ignore if server shutdown at next tick
if (IsStopped())
@@ -2573,14 +2573,14 @@ void World::ShutdownServ(uint32 time, uint32 options, uint8 exitcode)
else
{
m_ShutdownTimer = time;
- ShutdownMsg(true);
+ ShutdownMsg(true, nullptr, reason);
}
sScriptMgr->OnShutdownInitiate(ShutdownExitCode(exitcode), ShutdownMask(options));
}
/// Display a shutdown message to the user(s)
-void World::ShutdownMsg(bool show, Player* player)
+void World::ShutdownMsg(bool show, Player* player, const std::string& reason)
{
// not show messages for idle shutdown mode
if (m_ShutdownMask & SHUTDOWN_MASK_IDLE)
@@ -2595,6 +2595,8 @@ void World::ShutdownMsg(bool show, Player* player)
(m_ShutdownTimer > 12 * HOUR && (m_ShutdownTimer % (12 * HOUR)) == 0)) // > 12 h ; every 12 h
{
std::string str = secsToTimeString(m_ShutdownTimer);
+ if (!reason.empty())
+ str += " - " + reason;
ServerMessageType msgid = (m_ShutdownMask & SHUTDOWN_MASK_RESTART) ? SERVER_MSG_RESTART_TIME : SERVER_MSG_SHUTDOWN_TIME;
diff --git a/src/server/game/World/World.h b/src/server/game/World/World.h
index b77f877..8a36a0a 100644
--- a/src/server/game/World/World.h
+++ b/src/server/game/World/World.h
@@ -637,9 +637,9 @@ class World
/// Are we in the middle of a shutdown?
bool IsShuttingDown() const { return m_ShutdownTimer > 0; }
uint32 GetShutDownTimeLeft() const { return m_ShutdownTimer; }
- void ShutdownServ(uint32 time, uint32 options, uint8 exitcode);
+ void ShutdownServ(uint32 time, uint32 options, uint8 exitcode, const std::string& reason = std::string());
void ShutdownCancel();
- void ShutdownMsg(bool show = false, Player* player = NULL);
+ void ShutdownMsg(bool show = false, Player* player = NULL, const std::string& reason = std::string());
static uint8 GetExitCode() { return m_ExitCode; }
static void StopNow(uint8 exitcode) { m_stopEvent = true; m_ExitCode = exitcode; }
static bool IsStopped() { return m_stopEvent; }
diff --git a/src/server/scripts/Commands/cs_server.cpp b/src/server/scripts/Commands/cs_server.cpp
index 04cc9ae..b19937e 100644
--- a/src/server/scripts/Commands/cs_server.cpp
+++ b/src/server/scripts/Commands/cs_server.cpp
@@ -30,6 +30,8 @@ EndScriptData */
#include "ScriptMgr.h"
#include "SystemConfig.h"
+#include <regex>
+
class server_commandscript : public CommandScript
{
public:
@@ -201,145 +203,22 @@ public:
static bool HandleServerShutDownCommand(ChatHandler* /*handler*/, char const* args)
{
- if (!*args)
- return false;
-
- char* timeStr = strtok((char*) args, " ");
- char* exitCodeStr = strtok(NULL, "");
-
- int32 time = atoi(timeStr);
-
- // Prevent interpret wrong arg value as 0 secs shutdown time
- if ((time == 0 && (timeStr[0] != '0' || timeStr[1] != '\0')) || time < 0)
- return false;
-
- if (exitCodeStr)
- {
- int32 exitCode = atoi(exitCodeStr);
-
- // Handle atoi() errors
- if (exitCode == 0 && (exitCodeStr[0] != '0' || exitCodeStr[1] != '\0'))
- return false;
-
- // Exit code should be in range of 0-125, 126-255 is used
- // in many shells for their own return codes and code > 255
- // is not supported in many others
- if (exitCode < 0 || exitCode > 125)
- return false;
-
- sWorld->ShutdownServ(time, 0, exitCode);
- }
- else
- sWorld->ShutdownServ(time, 0, SHUTDOWN_EXIT_CODE);
-
- return true;
+ return ShutdownServer(args, 0, SHUTDOWN_EXIT_CODE);
}
static bool HandleServerRestartCommand(ChatHandler* /*handler*/, char const* args)
{
- if (!*args)
- return false;
-
- char* timeStr = strtok((char*) args, " ");
- char* exitCodeStr = strtok(NULL, "");
-
- int32 time = atoi(timeStr);
-
- // Prevent interpret wrong arg value as 0 secs shutdown time
- if ((time == 0 && (timeStr[0] != '0' || timeStr[1] != '\0')) || time < 0)
- return false;
-
- if (exitCodeStr)
- {
- int32 exitCode = atoi(exitCodeStr);
-
- // Handle atoi() errors
- if (exitCode == 0 && (exitCodeStr[0] != '0' || exitCodeStr[1] != '\0'))
- return false;
-
- // Exit code should be in range of 0-125, 126-255 is used
- // in many shells for their own return codes and code > 255
- // is not supported in many others
- if (exitCode < 0 || exitCode > 125)
- return false;
-
- sWorld->ShutdownServ(time, SHUTDOWN_MASK_RESTART, exitCode);
- }
- else
- sWorld->ShutdownServ(time, SHUTDOWN_MASK_RESTART, RESTART_EXIT_CODE);
-
- return true;
+ return ShutdownServer(args, SHUTDOWN_MASK_RESTART, RESTART_EXIT_CODE);
}
static bool HandleServerIdleRestartCommand(ChatHandler* /*handler*/, char const* args)
{
- if (!*args)
- return false;
-
- char* timeStr = strtok((char*) args, " ");
- char* exitCodeStr = strtok(NULL, "");
-
- int32 time = atoi(timeStr);
-
- // Prevent interpret wrong arg value as 0 secs shutdown time
- if ((time == 0 && (timeStr[0] != '0' || timeStr[1] != '\0')) || time < 0)
- return false;
-
- if (exitCodeStr)
- {
- int32 exitCode = atoi(exitCodeStr);
-
- // Handle atoi() errors
- if (exitCode == 0 && (exitCodeStr[0] != '0' || exitCodeStr[1] != '\0'))
- return false;
-
- // Exit code should be in range of 0-125, 126-255 is used
- // in many shells for their own return codes and code > 255
- // is not supported in many others
- if (exitCode < 0 || exitCode > 125)
- return false;
-
- sWorld->ShutdownServ(time, SHUTDOWN_MASK_RESTART | SHUTDOWN_MASK_IDLE, exitCode);
- }
- else
- sWorld->ShutdownServ(time, SHUTDOWN_MASK_RESTART | SHUTDOWN_MASK_IDLE, RESTART_EXIT_CODE);
- return true;
+ return ShutdownServer(args, SHUTDOWN_MASK_RESTART | SHUTDOWN_MASK_IDLE, RESTART_EXIT_CODE);
}
static bool HandleServerIdleShutDownCommand(ChatHandler* /*handler*/, char const* args)
{
- if (!*args)
- return false;
-
- char* timeStr = strtok((char*) args, " ");
- char* exitCodeStr = strtok(NULL, "");
-
- int32 time = atoi(timeStr);
-
- // Prevent interpret wrong arg value as 0 secs shutdown time
- if ((time == 0 && (timeStr[0] != '0' || timeStr[1] != '\0')) || time < 0)
- return false;
-
- if (exitCodeStr)
- {
- int32 exitCode = atoi(exitCodeStr);
-
- // Handle atoi() errors
- if (exitCode == 0 && (exitCodeStr[0] != '0' || exitCodeStr[1] != '\0'))
- return false;
-
- // Exit code should be in range of 0-125, 126-255 is used
- // in many shells for their own return codes and code > 255
- // is not supported in many others
- if (exitCode < 0 || exitCode > 125)
- return false;
-
- sWorld->ShutdownServ(time, SHUTDOWN_MASK_IDLE, exitCode);
- }
- else
- sWorld->ShutdownServ(time, SHUTDOWN_MASK_IDLE, SHUTDOWN_EXIT_CODE);
-
- return true;
+ return ShutdownServer(args, SHUTDOWN_MASK_IDLE, SHUTDOWN_EXIT_CODE);
}
// Exit the realm
@@ -415,6 +294,56 @@ public:
return true;
}
+
+private:
+ static bool ParseExitCode(std::string const& exitCodeStr, int32& exitCode)
+ {
+ exitCode = atoi(exitCodeStr.c_str());
+
+ // Handle atoi() errors
+ if (exitCode == 0 && (exitCodeStr[0] != '0' || exitCodeStr[1] != '\0'))
+ return false;
+
+ // Exit code should be in range of 0-125, 126-255 is used
+ // in many shells for their own return codes and code > 255
+ // is not supported in many others
+ if (exitCode < 0 || exitCode > 125)
+ return false;
+
+ return true;
+ }
+
+ static bool ShutdownServer(char const* args, uint32 shutdownMask, int32 defaultExitCode)
+ {
+ if (!*args)
+ return false;
+
+ // #delay [#exit_code] [reason]
+ std::regex regex(R"([0-9]+) ([0-9]*) ?(.*)");
+ std::cmatch cm;
+
+ if (!std::regex_match(args, cm, regex))
+ return false;
+
+ std::string delayStr = cm[1];
+ std::string exitCodeStr = cm[2];
+ std::string reason = cm[3];
+
+ int32 delay = atoi(delayStr.c_str());
+
+ // Prevent interpret wrong arg value as 0 secs shutdown time
+ if ((delay == 0 && (delayStr[0] != '0' || delayStr[1] != '\0')) || delay < 0)
+ return false;
+
+ int32 exitCode = defaultExitCode;
+ if (exitCodeStr.length() > 0)
+ if (!ParseExitCode(exitCodeStr, exitCode))
+ return false;
+
+ sWorld->ShutdownServ(delay, shutdownMask, static_cast<uint8>(exitCode), reason);
+
+ return true;
+ }
};
void AddSC_server_commandscript()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment