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

View File

@ -17,12 +17,15 @@ class TConsole {
public:
TConsole();
// Initializes the commandline app to take over I/O
void InitializeCommandline();
void Write(const std::string& str);
void WriteRaw(const std::string& str);
void InitializeLuaConsole(TLuaEngine& Engine);
void BackupOldLog();
void StartLoggingToFile();
Commandline& Internal() { return mCommandline; }
Commandline& Internal() { return *mCommandline; }
private:
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
};
Commandline mCommandline;
std::unique_ptr<Commandline> mCommandline { nullptr };
std::vector<std::string> mCachedLuaHistory;
std::vector<std::string> mCachedRegularHistory;
TLuaEngine* mLuaEngine { nullptr };

View File

@ -157,13 +157,13 @@ void TConsole::ChangeToLuaConsole(const std::string& LuaStateId) {
mIsLuaConsole = true;
if (mStateId != mDefaultStateId) {
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 {
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();
mCommandline.set_history(mCachedLuaHistory);
mCachedRegularHistory = mCommandline->history();
mCommandline->set_history(mCachedLuaHistory);
}
}
@ -175,9 +175,9 @@ void TConsole::ChangeToRegularConsole() {
} else {
Application::Console().WriteRaw("Detached from Lua.");
}
mCachedLuaHistory = mCommandline.history();
mCommandline.set_history(mCachedRegularHistory);
mCommandline.set_prompt("> ");
mCachedLuaHistory = mCommandline->history();
mCommandline->set_history(mCachedRegularHistory);
mCommandline->set_prompt("> ");
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))) {
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) {
@ -589,16 +589,20 @@ Commands
}
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();
mCommandline.on_command = [this](Commandline& c) {
mCommandline->on_command = [this](Commandline& c) {
try {
auto TrimmedCmd = c.get_command();
TrimmedCmd = TrimString(TrimmedCmd);
auto [cmd, args] = ParseCommand(TrimmedCmd);
mCommandline.write(mCommandline.prompt() + TrimmedCmd);
mCommandline->write(mCommandline->prompt() + TrimmedCmd);
if (mIsLuaConsole) {
if (!mLuaEngine) {
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.");
}
};
mCommandline.on_autocomplete = [this](Commandline&, std::string stub, int) {
mCommandline->on_autocomplete = [this](Commandline&, std::string stub, int) {
std::vector<std::string> suggestions;
try {
if (mIsLuaConsole) { // if lua
@ -703,11 +707,21 @@ TConsole::TConsole() {
void TConsole::Write(const std::string& 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) {
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) {

View File

@ -69,7 +69,6 @@ int main(int argc, char** argv) {
int BeamMPServerMain(MainArguments Arguments) {
setlocale(LC_ALL, "C");
Application::InitializeConsole();
ArgsParser Parser;
Parser.RegisterArgument({ "help" }, ArgsParser::NONE);
Parser.RegisterArgument({ "version" }, ArgsParser::NONE);
@ -80,12 +79,10 @@ int BeamMPServerMain(MainArguments Arguments) {
return 1;
}
if (Parser.FoundArgument({ "help" })) {
Application::Console().Internal().set_prompt("");
Application::Console().WriteRaw(sCommandlineArguments);
return 0;
}
if (Parser.FoundArgument({ "version" })) {
Application::Console().Internal().set_prompt("");
Application::Console().WriteRaw("BeamMP-Server v" + Application::ServerVersionString());
return 0;
}
@ -110,10 +107,21 @@ int BeamMPServerMain(MainArguments Arguments) {
}
}
Application::SetSubsystemStatus("Main", Application::Status::Starting);
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);
SetupSignalHandlers();
bool Shutdown = false;
@ -128,20 +136,11 @@ int BeamMPServerMain(MainArguments Arguments) {
});
TServer Server(Arguments.List);
TConfig Config(ConfigPath);
auto LuaEngine = std::make_shared<TLuaEngine>();
LuaEngine->SetServer(&Server);
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");
beammp_trace("Running in debug mode on a debug build");