From 663d9336324110648ffdea7c7092a004353419b5 Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Fri, 1 May 2009 17:14:46 -0500 Subject: Initial commit of source from zwzsg --- KPL_Start.cpp | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 KPL_Start.cpp (limited to 'KPL_Start.cpp') diff --git a/KPL_Start.cpp b/KPL_Start.cpp new file mode 100644 index 0000000..ac001c3 --- /dev/null +++ b/KPL_Start.cpp @@ -0,0 +1,128 @@ +//--------------------------------------------------------------------------- +#include "KPL_Start.h" +#include "KPLTypes.h" +#include "KPLFillModSpecific.h" +#include "WriteScript.h" +#include "GenerateScript.h" + +#ifdef __WIN32__ + #include "process.h"// For spawnlp + #include + #pragma package(smart_init) + DWORD ProcessID=0; +#else + #include + #include + #include + #include + pid_t ProcessID=0; + #define P_NOWAIT 0 + static int spawnl(int flg, const char* file, const char* name, const char* arg1, const char* arg2) { + int pid = fork(); + if (pid == 0) { + // We are in the son + execl(file, name, arg1, arg2, NULL); + exit(1); + } + return pid; + } +#endif + +int StartSpectator(void*) +{ + return LaunchSpringExecutable(0); +} + +int StartEasy(void*) +{ + return LaunchSpringExecutable(1); +} + +int StartMedium(void*) +{ + return LaunchSpringExecutable(2); +} + +int StartHard(void*) +{ + return LaunchSpringExecutable(3); +} + +int StartVeryHard(void*) +{ + return LaunchSpringExecutable(4); +} + +int StartMultiPlayer(void*) +{ + ProcessID=spawnl(P_NOWAIT,cKPgame::ClientExecutableFileName,cKPgame::ClientExecutableFileName,"",NULL); + return 0; +} + +int StartSettings(void*) +{ + ProcessID=spawnl(P_NOWAIT,cKPgame::SettingsExecutableFileName,cKPgame::SettingsExecutableFileName,"",NULL); + return 0; +} + +int LaunchSpringExecutable(int Difficulty) +{ + + // Check that Spring isn't already running + #ifdef __WIN32__ + HANDLE ProcessHandle; + ProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION,false,ProcessID); + if(ProcessHandle!=NULL) + { + // Spring is already running + return 2; + } + #else + if (waitpid(ProcessID, NULL, WNOHANG) >= 0) { + // Spring is already running + return 2; + } + #endif + + KPLFillModSpecific(); + + // Create and allocate "game" and its "players" and its "modoptions" + cKPgame* game=(cKPgame*)malloc(sizeof(cKPgame)); + game->players=(cKPplayer**)malloc(32*sizeof(cKPplayer*)); + for(int p=0;p<32;++p) + game->players[p]=(cKPplayer*)malloc(sizeof(cKPplayer)); + game->ModOptions=(cKPmodoptions*)malloc(sizeof(cKPmodoptions)); + + // Generate a random script according to difficulty + GenerateScript(game,Difficulty); + + // Writing that script on the disk + WriteScript(game); + + #ifdef __WIN32__ + Sleep(500); + #else + sleep(1); + #endif + + // Running Spring + //ShellExecute(NULL,"open",game->ExecutableFileName,game->ScriptFileName,NULL,SW_SHOWNORMAL); + ProcessID=spawnl(P_NOWAIT,cKPgame::ExecutableFileName,game->ExecutableFileName,game->ScriptFileName,NULL); + + // Check that Spring has run + #ifdef __WIN32__ + ProcessHandle=OpenProcess(PROCESS_QUERY_INFORMATION,false,ProcessID); + if(ProcessHandle==NULL) + { + return 1; + } + #else + if (waitpid(ProcessID, NULL, WNOHANG) < 0) { + return 1; + } + #endif + + //Application->Terminate(); + return 0; +} + -- cgit v1.2.3-70-g09d2