From 0cf03a3562f9883bd3ab8f549efa88fb69b8a9ac Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 27 Sep 2011 18:44:01 +0000 Subject: [PATCH] - added option to quit headless server after game has completed: ./megaglest --headless-server-mode=exit --- source/glest_game/main/battle_end.cpp | 4 ++++ source/glest_game/main/battle_end.h | 1 + source/glest_game/main/main.cpp | 16 +++++++++++++++- source/glest_game/main/program.cpp | 11 +++++++---- source/glest_game/main/program.h | 7 +++++++ .../include/platform/sdl/platform_main.h | 7 +++++-- 6 files changed, 39 insertions(+), 7 deletions(-) diff --git a/source/glest_game/main/battle_end.cpp b/source/glest_game/main/battle_end.cpp index 01675b2c0..c9b6c2515 100644 --- a/source/glest_game/main/battle_end.cpp +++ b/source/glest_game/main/battle_end.cpp @@ -78,6 +78,10 @@ void BattleEnd::update() { mouse2d= (mouse2d+1) % Renderer::maxMouse2dAnim; if(this->stats.getIsMasterserverMode() == true) { + if(program->getWantShutdownApplicationAfterGame() == true) { + program->setShutdownApplicationEnabled(true); + return; + } SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); //program->setState(new MainMenu(program)); program->initServer(program->getWindow(),false,true,true); diff --git a/source/glest_game/main/battle_end.h b/source/glest_game/main/battle_end.h index 4bc5e90da..ac1b861de 100644 --- a/source/glest_game/main/battle_end.h +++ b/source/glest_game/main/battle_end.h @@ -40,6 +40,7 @@ private: public: BattleEnd(Program *program, const Stats *stats); ~BattleEnd(); + virtual void update(); virtual void render(); virtual void keyDown(char key); diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index ffbf9fb61..9460f12d5 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -2194,6 +2194,20 @@ int glestMain(int argc, char** argv) { if( hasCommandArgument(argc, argv,string(GAME_ARGS[GAME_ARG_MASTERSERVER_MODE])) == true) { isMasterServerModeEnabled = true; Window::setMasterserverMode(isMasterServerModeEnabled); + + int foundParamIndIndex = -1; + hasCommandArgument(argc, argv,string(GAME_ARGS[GAME_ARG_MASTERSERVER_MODE]) + string("="),&foundParamIndIndex); + if(foundParamIndIndex < 0) { + hasCommandArgument(argc, argv,string(GAME_ARGS[GAME_ARG_MASTERSERVER_MODE]),&foundParamIndIndex); + } + string paramValue = argv[foundParamIndIndex]; + vector paramPartTokens; + Tokenize(paramValue,paramPartTokens,"="); + if(paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string exitHeadless = paramPartTokens[1]; + printf("Forcing quit after game has compelted [%s]\n",exitHeadless.c_str()); + Program::setWantShutdownApplicationAfterGame(true); + } } //off_t fileSize = getFileSize(argv[0]); @@ -3230,7 +3244,7 @@ int glestMain(int argc, char** argv) { } //main loop - while(Window::handleEvent()) { + while(program->isShutdownApplicationEnabled() == false && Window::handleEvent()) { if(isMasterServerModeEnabled == true) { #ifndef WIN32 if(poll(cinfd, 1, 0)) diff --git a/source/glest_game/main/program.cpp b/source/glest_game/main/program.cpp index c35447621..2c4db677a 100644 --- a/source/glest_game/main/program.cpp +++ b/source/glest_game/main/program.cpp @@ -43,6 +43,8 @@ const int Program::maxTimes= 10; Program *Program::singleton = NULL; const int SOUND_THREAD_UPDATE_MILLISECONDS = 25; +bool Program::wantShutdownApplicationAfterGame = false; + // ===================================================== // class Program::CrashProgramState // ===================================================== @@ -161,10 +163,11 @@ void Program::ShowMessageProgramState::update() { Program::Program() { this->masterserverMode = false; - skipRenderFrameCount = 0; - programState= NULL; - singleton = this; - soundThreadManager = NULL; + this->shutdownApplicationEnabled = false; + this->skipRenderFrameCount = 0; + this->programState= NULL; + this->singleton = this; + this->soundThreadManager = NULL; //mesage box Lang &lang= Lang::getInstance(); diff --git a/source/glest_game/main/program.h b/source/glest_game/main/program.h index 799bc9f98..476ee1ea1 100644 --- a/source/glest_game/main/program.h +++ b/source/glest_game/main/program.h @@ -145,6 +145,8 @@ private: int skipRenderFrameCount; bool masterserverMode; + bool shutdownApplicationEnabled; + static bool wantShutdownApplicationAfterGame; public: Program(); @@ -152,7 +154,12 @@ public: static Program *getInstance() {return singleton;} + static void setWantShutdownApplicationAfterGame(bool value) { wantShutdownApplicationAfterGame = value; } + static bool getWantShutdownApplicationAfterGame() { return wantShutdownApplicationAfterGame; } + bool isMasterserverMode() const; + bool isShutdownApplicationEnabled() const { return shutdownApplicationEnabled; } + void setShutdownApplicationEnabled(bool value) { shutdownApplicationEnabled = value; } GraphicMessageBox * getMsgBox() { return &msgBox; } void initNormal(WindowGl *window); diff --git a/source/shared_lib/include/platform/sdl/platform_main.h b/source/shared_lib/include/platform/sdl/platform_main.h index e19b63ca0..e6d60d110 100644 --- a/source/shared_lib/include/platform/sdl/platform_main.h +++ b/source/shared_lib/include/platform/sdl/platform_main.h @@ -119,7 +119,10 @@ void printParameterHelp(const char *argv0, bool foundInvalidArgs) { printf("\n%s\t\tAutomatically starts a game with the last game settings you played.",GAME_ARGS[GAME_ARG_AUTOSTART_LASTGAME]); printf("\n%s=x\t\t\tAuto connects to a network server at IP or hostname x",GAME_ARGS[GAME_ARG_CLIENT]); printf("\n%s\t\t\tAuto creates a network server.",GAME_ARGS[GAME_ARG_SERVER]); - printf("\n%s\t\t\tRun as a headless server.",GAME_ARGS[GAME_ARG_MASTERSERVER_MODE]); + + printf("\n%s=x\t\t\tRun as a headless server.",GAME_ARGS[GAME_ARG_MASTERSERVER_MODE]); + printf("\n \t\tWhere x is an option command: exit which quits the application after a game has no more connected players."); + printf("\n%s=x\t\tAuto loads the specified scenario by scenario name.",GAME_ARGS[GAME_ARG_LOADSCENARIO]); printf("\n%s=x\t\tAuto Preview the specified map by map name.",GAME_ARGS[GAME_ARG_PREVIEW_MAP]); printf("\n%s\t\t\tdisplays the version string of this program.",GAME_ARGS[GAME_ARG_VERSION]); @@ -265,7 +268,7 @@ bool hasCommandArgument(int argc, char** argv,const string argName, int *foundIn #define MAIN_FUNCTION(X) int main(int argc, char **argv) \ { \ if(hasCommandArgument(argc, argv,string(GAME_ARGS[GAME_ARG_MASTERSERVER_MODE])) == true) { \ - if(SDL_Init(SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) { \ + if(SDL_Init(SDL_INIT_TIMER | SDL_INIT_JOYSTICK) < 0) { \ std::cerr << "Couldn't initialize SDL: " << SDL_GetError() << "\n"; \ return 1; \ } \