Fix exit after 10 seconds (#215)

Also fixes initializing the console too early by @lionkor
This commit is contained in:
Lion 2023-12-21 12:50:19 +01:00 committed by GitHub
commit defadf094f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 38 deletions

View File

@ -75,7 +75,7 @@ public:
static void RegisterShutdownHandler(const TShutdownHandler& Handler); static void RegisterShutdownHandler(const TShutdownHandler& Handler);
// Causes all threads to finish up and exit gracefull gracefully // Causes all threads to finish up and exit gracefull gracefully
static void GracefullyShutdown(); static void GracefullyShutdown();
static TConsole& Console() { return *mConsole; } static TConsole& Console() { return mConsole; }
static std::string ServerVersionString(); static std::string ServerVersionString();
static const Version& ServerVersion() { return mVersion; } static const Version& ServerVersion() { return mVersion; }
static uint8_t ClientMajorVersion() { return 2; } static uint8_t ClientMajorVersion() { return 2; }
@ -101,9 +101,7 @@ public:
static void SleepSafeSeconds(size_t Seconds); static void SleepSafeSeconds(size_t Seconds);
static void InitializeConsole() { static void InitializeConsole() {
if (!mConsole) { mConsole.InitializeCommandline();
mConsole = std::make_unique<TConsole>();
}
} }
enum class Status { enum class Status {
@ -129,7 +127,7 @@ private:
static inline SystemStatusMap mSystemStatusMap {}; static inline SystemStatusMap mSystemStatusMap {};
static inline std::mutex mSystemStatusMapMutex {}; static inline std::mutex mSystemStatusMapMutex {};
static inline std::string mPPS; static inline std::string mPPS;
static inline std::unique_ptr<TConsole> mConsole; static inline TConsole mConsole;
static inline std::shared_mutex mShutdownMtx {}; static inline std::shared_mutex mShutdownMtx {};
static inline bool mShutdown { false }; static inline bool mShutdown { false };
static inline std::mutex mShutdownHandlersMutex {}; static inline std::mutex mShutdownHandlersMutex {};

View File

@ -17,12 +17,15 @@ class TConsole {
public: public:
TConsole(); TConsole();
// Initializes the commandline app to take over I/O
void InitializeCommandline();
void Write(const std::string& str); void Write(const std::string& str);
void WriteRaw(const std::string& str); void WriteRaw(const std::string& str);
void InitializeLuaConsole(TLuaEngine& Engine); void InitializeLuaConsole(TLuaEngine& Engine);
void BackupOldLog(); void BackupOldLog();
void StartLoggingToFile(); void StartLoggingToFile();
Commandline& Internal() { return mCommandline; } Commandline& Internal() { return *mCommandline; }
private: private:
void RunAsCommand(const std::string& cmd, bool IgnoreNotACommand = false); void RunAsCommand(const std::string& cmd, bool IgnoreNotACommand = false);
@ -56,7 +59,7 @@ private:
{ "say", [this](const auto&, const auto&) { Command_Say(""); } }, // shouldn't actually be called { "say", [this](const auto&, const auto&) { Command_Say(""); } }, // shouldn't actually be called
}; };
Commandline mCommandline; std::unique_ptr<Commandline> mCommandline { nullptr };
std::vector<std::string> mCachedLuaHistory; std::vector<std::string> mCachedLuaHistory;
std::vector<std::string> mCachedRegularHistory; std::vector<std::string> mCachedRegularHistory;
TLuaEngine* mLuaEngine { nullptr }; TLuaEngine* mLuaEngine { nullptr };

View File

@ -157,13 +157,13 @@ void TConsole::ChangeToLuaConsole(const std::string& LuaStateId) {
mIsLuaConsole = true; mIsLuaConsole = true;
if (mStateId != mDefaultStateId) { if (mStateId != mDefaultStateId) {
Application::Console().WriteRaw("Attached to Lua state '" + mStateId + "'. For help, type `:help`. To detach, type `:exit`"); Application::Console().WriteRaw("Attached to Lua state '" + mStateId + "'. For help, type `:help`. To detach, type `:exit`");
mCommandline.set_prompt("lua @" + LuaStateId + "> "); mCommandline->set_prompt("lua @" + LuaStateId + "> ");
} else { } else {
Application::Console().WriteRaw("Attached to Lua. For help, type `:help`. To detach, type `:exit`"); Application::Console().WriteRaw("Attached to Lua. For help, type `:help`. To detach, type `:exit`");
mCommandline.set_prompt("lua> "); mCommandline->set_prompt("lua> ");
} }
mCachedRegularHistory = mCommandline.history(); mCachedRegularHistory = mCommandline->history();
mCommandline.set_history(mCachedLuaHistory); mCommandline->set_history(mCachedLuaHistory);
} }
} }
@ -175,9 +175,9 @@ void TConsole::ChangeToRegularConsole() {
} else { } else {
Application::Console().WriteRaw("Detached from Lua."); Application::Console().WriteRaw("Detached from Lua.");
} }
mCachedLuaHistory = mCommandline.history(); mCachedLuaHistory = mCommandline->history();
mCommandline.set_history(mCachedRegularHistory); mCommandline->set_history(mCachedRegularHistory);
mCommandline.set_prompt("> "); mCommandline->set_prompt("> ");
mStateId = mDefaultStateId; mStateId = mDefaultStateId;
} }
} }
@ -257,7 +257,7 @@ void TConsole::Command_Clear(const std::string&, const std::vector<std::string>&
if (!EnsureArgsCount(args, 0, size_t(-1))) { if (!EnsureArgsCount(args, 0, size_t(-1))) {
return; return;
} }
mCommandline.write("\x1b[;H\x1b[2J"); mCommandline->write("\x1b[;H\x1b[2J");
} }
void TConsole::Command_Kick(const std::string&, const std::vector<std::string>& args) { void TConsole::Command_Kick(const std::string&, const std::vector<std::string>& args) {
@ -589,16 +589,20 @@ Commands
} }
TConsole::TConsole() { TConsole::TConsole() {
mCommandline.enable_history(); }
mCommandline.set_history_limit(20);
mCommandline.set_prompt("> "); void TConsole::InitializeCommandline() {
mCommandline = std::make_unique<Commandline>();
mCommandline->enable_history();
mCommandline->set_history_limit(20);
mCommandline->set_prompt("> ");
BackupOldLog(); BackupOldLog();
mCommandline.on_command = [this](Commandline& c) { mCommandline->on_command = [this](Commandline& c) {
try { try {
auto TrimmedCmd = c.get_command(); auto TrimmedCmd = c.get_command();
TrimmedCmd = TrimString(TrimmedCmd); TrimmedCmd = TrimString(TrimmedCmd);
auto [cmd, args] = ParseCommand(TrimmedCmd); auto [cmd, args] = ParseCommand(TrimmedCmd);
mCommandline.write(mCommandline.prompt() + TrimmedCmd); mCommandline->write(mCommandline->prompt() + TrimmedCmd);
if (mIsLuaConsole) { if (mIsLuaConsole) {
if (!mLuaEngine) { if (!mLuaEngine) {
beammp_info("Lua not started yet, please try again in a second"); beammp_info("Lua not started yet, please try again in a second");
@ -633,7 +637,7 @@ TConsole::TConsole() {
beammp_error("Console died with: " + std::string(e.what()) + ". This could be a fatal error and could cause the server to terminate."); beammp_error("Console died with: " + std::string(e.what()) + ". This could be a fatal error and could cause the server to terminate.");
} }
}; };
mCommandline.on_autocomplete = [this](Commandline&, std::string stub, int) { mCommandline->on_autocomplete = [this](Commandline&, std::string stub, int) {
std::vector<std::string> suggestions; std::vector<std::string> suggestions;
try { try {
if (mIsLuaConsole) { // if lua if (mIsLuaConsole) { // if lua
@ -703,11 +707,21 @@ TConsole::TConsole() {
void TConsole::Write(const std::string& str) { void TConsole::Write(const std::string& str) {
auto ToWrite = GetDate() + str; auto ToWrite = GetDate() + str;
mCommandline.write(ToWrite); // allows writing to stdout without an initialized console
if (mCommandline) {
mCommandline->write(ToWrite);
} else {
std::cout << ToWrite << std::endl;
}
} }
void TConsole::WriteRaw(const std::string& str) { void TConsole::WriteRaw(const std::string& str) {
mCommandline.write(str); // allows writing to stdout without an initialized console
if (mCommandline) {
mCommandline->write(str);
} else {
std::cout << str << std::endl;
}
} }
void TConsole::InitializeLuaConsole(TLuaEngine& Engine) { void TConsole::InitializeLuaConsole(TLuaEngine& Engine) {

View File

@ -69,7 +69,6 @@ int main(int argc, char** argv) {
int BeamMPServerMain(MainArguments Arguments) { int BeamMPServerMain(MainArguments Arguments) {
setlocale(LC_ALL, "C"); setlocale(LC_ALL, "C");
Application::InitializeConsole();
ArgsParser Parser; ArgsParser Parser;
Parser.RegisterArgument({ "help" }, ArgsParser::NONE); Parser.RegisterArgument({ "help" }, ArgsParser::NONE);
Parser.RegisterArgument({ "version" }, ArgsParser::NONE); Parser.RegisterArgument({ "version" }, ArgsParser::NONE);
@ -80,12 +79,10 @@ int BeamMPServerMain(MainArguments Arguments) {
return 1; return 1;
} }
if (Parser.FoundArgument({ "help" })) { if (Parser.FoundArgument({ "help" })) {
Application::Console().Internal().set_prompt("");
Application::Console().WriteRaw(sCommandlineArguments); Application::Console().WriteRaw(sCommandlineArguments);
return 0; return 0;
} }
if (Parser.FoundArgument({ "version" })) { if (Parser.FoundArgument({ "version" })) {
Application::Console().Internal().set_prompt("");
Application::Console().WriteRaw("BeamMP-Server v" + Application::ServerVersionString()); Application::Console().WriteRaw("BeamMP-Server v" + Application::ServerVersionString());
return 0; return 0;
} }
@ -109,11 +106,22 @@ int BeamMPServerMain(MainArguments Arguments) {
} }
} }
} }
TConfig Config(ConfigPath);
if (Config.Failed()) {
beammp_info("Closing in 10 seconds");
// loop to make it possible to ctrl+c instead
for (size_t i = 0; i < 20; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
return 1;
}
Application::InitializeConsole();
Application::Console().StartLoggingToFile();
Application::SetSubsystemStatus("Main", Application::Status::Starting); Application::SetSubsystemStatus("Main", Application::Status::Starting);
Application::Console().StartLoggingToFile();
SetupSignalHandlers(); SetupSignalHandlers();
bool Shutdown = false; bool Shutdown = false;
@ -128,20 +136,11 @@ int BeamMPServerMain(MainArguments Arguments) {
}); });
TServer Server(Arguments.List); TServer Server(Arguments.List);
TConfig Config(ConfigPath);
auto LuaEngine = std::make_shared<TLuaEngine>(); auto LuaEngine = std::make_shared<TLuaEngine>();
LuaEngine->SetServer(&Server); LuaEngine->SetServer(&Server);
Application::Console().InitializeLuaConsole(*LuaEngine); Application::Console().InitializeLuaConsole(*LuaEngine);
if (Config.Failed()) {
beammp_info("Closing in 10 seconds");
// loop to make it possible to ctrl+c instead
for (size_t i = 0; i < 20; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
return 1;
}
RegisterThread("Main"); RegisterThread("Main");
beammp_trace("Running in debug mode on a debug build"); beammp_trace("Running in debug mode on a debug build");