diff --git a/src/base/system.c b/src/base/system.c index 0b59c838b..1787e30ac 100644 --- a/src/base/system.c +++ b/src/base/system.c @@ -3214,11 +3214,17 @@ int pid(void) #endif } -int shell_execute(const char *file) +PROCESS shell_execute(const char *file) { #if defined(CONF_FAMILY_WINDOWS) - ShellExecute(NULL, NULL, file, NULL, NULL, SW_SHOWDEFAULT); - // TODO: Get PID + SHELLEXECUTEINFOA info; + mem_zero(&info, sizeof(SHELLEXECUTEINFOA)); + info.lpVerb = "open"; + info.lpFile = file; + info.nShow = SW_SHOWDEFAULT; + info.fMask = SEE_MASK_NOCLOSEPROCESS; + ShellExecuteEx(&info); + return info.hProcess; #elif defined(CONF_FAMILY_UNIX) char *argv[2]; pid_t pid; @@ -3231,14 +3237,14 @@ int shell_execute(const char *file) #endif } -int kill_process(int pid) +int kill_process(PROCESS process) { #if defined(CONF_FAMILY_WINDOWS) - // TODO: Implement + return TerminateProcess(process, 0); #elif defined(CONF_FAMILY_UNIX) int status; - kill(pid, SIGTERM); - return waitpid(pid, &status, 0); + kill(process, SIGTERM); + return !waitpid(process, &status, 0); #endif } diff --git a/src/base/system.h b/src/base/system.h index 97abb7dc7..eb3621ef4 100644 --- a/src/base/system.h +++ b/src/base/system.h @@ -1991,27 +1991,33 @@ int str_in_list(const char *list, const char *delim, const char *needle); */ int pid(void); +#if defined(CONF_FAMILY_WINDOWS) +typedef void *PROCESS; +#else +typedef pid_t PROCESS; +#endif + /* Function: shell_execute Executes a given file. Returns: - pid of the new process + handle/pid of the new process */ -int shell_execute(const char *file); +PROCESS shell_execute(const char *file); /* Function: kill_process Sends kill signal to a process. Parameters: - pid - pid of the process + process - handle/pid of the process Returns: - 0 - Success - 1 - Error + 0 - Error + 1 - Success */ -int kill_process(int pid); +int kill_process(PROCESS process); /* Function: os_is_winxp_or_lower diff --git a/src/engine/shared/storage.cpp b/src/engine/shared/storage.cpp index b00529850..8aca1658f 100644 --- a/src/engine/shared/storage.cpp +++ b/src/engine/shared/storage.cpp @@ -486,9 +486,9 @@ public: GetPath(Type, pDir, pBuffer, BufferSize); } - virtual const char* GetBinaryPath(const char *pDir, char *pBuffer, unsigned BufferSize) + virtual const char* GetBinaryPath(const char *pFilename, char *pBuffer, unsigned BufferSize) { - str_format(pBuffer, BufferSize, "%s%s%s", m_aBinarydir, !m_aBinarydir[0] ? "" : "/", pDir); + str_format(pBuffer, BufferSize, "%s%s%s", m_aBinarydir, !m_aBinarydir[0] ? "" : "/", pFilename); return pBuffer; } diff --git a/src/engine/storage.h b/src/engine/storage.h index 7efd765a1..41d402ca8 100644 --- a/src/engine/storage.h +++ b/src/engine/storage.h @@ -37,7 +37,7 @@ public: virtual bool RemoveBinaryFile(const char *pFilename) = 0; virtual bool RenameBinaryFile(const char* pOldFilename, const char* pNewFilename) = 0; - virtual const char* GetBinaryPath(const char *pDir, char *pBuffer, unsigned BufferSize) = 0; + virtual const char* GetBinaryPath(const char *pFilename, char *pBuffer, unsigned BufferSize) = 0; static void StripPathAndExtension(const char *pFilename, char *pBuffer, int BufferSize); }; diff --git a/src/game/client/components/menus.cpp b/src/game/client/components/menus.cpp index 05aaefacc..4906db963 100644 --- a/src/game/client/components/menus.cpp +++ b/src/game/client/components/menus.cpp @@ -85,7 +85,7 @@ CMenus::CMenus() m_DemoPlayerState = DEMOPLAYER_NONE; m_Dummy = false; - m_ServerProcess.Pid = -1; + m_ServerProcess.Process = 0; m_ServerProcess.Initialized = false; } @@ -2071,12 +2071,13 @@ extern "C" void font_debug_render(); void CMenus::OnRender() { - if(m_ServerProcess.Pid != -1) + if(m_ServerProcess.Process != 0) { // TODO: Maybe call this less often, or probably even cleaner, use econ if(!m_ServerProcess.Initialized) { - IOHANDLE File = Storage()->OpenFile("autoexec_server.log", IOFLAG_READ, IStorage::TYPE_ALL); + char aBuf[MAX_PATH_LENGTH]; + IOHANDLE File = Storage()->OpenFile(Storage()->GetBinaryPath("autoexec_server.log", aBuf, sizeof(aBuf)), IOFLAG_READ, IStorage::TYPE_ALL); if(File) { m_ServerProcess.LineReader.Init(File); diff --git a/src/game/client/components/menus.h b/src/game/client/components/menus.h index 5ca7f9e00..a702eebfe 100644 --- a/src/game/client/components/menus.h +++ b/src/game/client/components/menus.h @@ -17,7 +17,7 @@ struct CServerProcess { - pid_t Pid; + PROCESS Process; bool Initialized; CLineReader LineReader; }; diff --git a/src/game/client/components/menus_start.cpp b/src/game/client/components/menus_start.cpp index ead437d1d..7c7e54a5f 100644 --- a/src/game/client/components/menus_start.cpp +++ b/src/game/client/components/menus_start.cpp @@ -49,17 +49,18 @@ void CMenus::RenderStartMenu(CUIRect MainView) Menu.HSplitBottom(5.0f, &Menu, 0); // little space Menu.HSplitBottom(40.0f, &Menu, &Button); static int s_LocalServerButton = 0; - if(DoButton_Menu(&s_LocalServerButton, m_ServerProcess.Pid == -1 ? Localize("Run server") : Localize("Stop server"), 0, &Button, g_Config.m_ClShowStartMenuImages ? "local_server" : 0, CUI::CORNER_ALL, Rounding, 0.5f, vec4(0.0f, 0.0f, 0.0f, 0.5f), m_ServerProcess.Pid == -1 ? vec4(0.0f, 0.0f, 0.0f, 0.25f) : vec4(0.0f, 1.0f, 0.0f, 0.25f)) || (CheckHotKey(KEY_R) && Input()->KeyPress(KEY_R))) + if(DoButton_Menu(&s_LocalServerButton, m_ServerProcess.Process ? Localize("Stop server") : Localize("Run server"), 0, &Button, g_Config.m_ClShowStartMenuImages ? "local_server" : 0, CUI::CORNER_ALL, Rounding, 0.5f, vec4(0.0f, 0.0f, 0.0f, 0.5f), m_ServerProcess.Process ? vec4(0.0f, 1.0f, 0.0f, 0.25f) : vec4(0.0f, 0.0f, 0.0f, 0.25f)) || (CheckHotKey(KEY_R) && Input()->KeyPress(KEY_R))) { - if(m_ServerProcess.Pid == -1) + if(m_ServerProcess.Process) { - Storage()->RemoveFile("autoexec_server.log", IStorage::TYPE_ALL); - m_ServerProcess.Pid = shell_execute(PLAT_SERVER_EXEC); + kill_process(m_ServerProcess.Process); + m_ServerProcess.Process = 0; } else { - kill_process(m_ServerProcess.Pid); - m_ServerProcess.Pid = -1; + Storage()->RemoveBinaryFile("autoexec_server.log"); + char aBuf[MAX_PATH_LENGTH]; + m_ServerProcess.Process = shell_execute(Storage()->GetBinaryPath(PLAT_SERVER_EXEC, aBuf, sizeof(aBuf))); } }