Implement opening server on Windows (reading autoexec_server.log not working yet)

This commit is contained in:
def 2020-09-06 00:38:35 +02:00
parent 59ae288ead
commit 90f3199d98
7 changed files with 40 additions and 26 deletions

View file

@ -3214,11 +3214,17 @@ int pid(void)
#endif #endif
} }
int shell_execute(const char *file) PROCESS shell_execute(const char *file)
{ {
#if defined(CONF_FAMILY_WINDOWS) #if defined(CONF_FAMILY_WINDOWS)
ShellExecute(NULL, NULL, file, NULL, NULL, SW_SHOWDEFAULT); SHELLEXECUTEINFOA info;
// TODO: Get PID 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) #elif defined(CONF_FAMILY_UNIX)
char *argv[2]; char *argv[2];
pid_t pid; pid_t pid;
@ -3231,14 +3237,14 @@ int shell_execute(const char *file)
#endif #endif
} }
int kill_process(int pid) int kill_process(PROCESS process)
{ {
#if defined(CONF_FAMILY_WINDOWS) #if defined(CONF_FAMILY_WINDOWS)
// TODO: Implement return TerminateProcess(process, 0);
#elif defined(CONF_FAMILY_UNIX) #elif defined(CONF_FAMILY_UNIX)
int status; int status;
kill(pid, SIGTERM); kill(process, SIGTERM);
return waitpid(pid, &status, 0); return !waitpid(process, &status, 0);
#endif #endif
} }

View file

@ -1991,27 +1991,33 @@ int str_in_list(const char *list, const char *delim, const char *needle);
*/ */
int pid(void); int pid(void);
#if defined(CONF_FAMILY_WINDOWS)
typedef void *PROCESS;
#else
typedef pid_t PROCESS;
#endif
/* /*
Function: shell_execute Function: shell_execute
Executes a given file. Executes a given file.
Returns: 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 Function: kill_process
Sends kill signal to a process. Sends kill signal to a process.
Parameters: Parameters:
pid - pid of the process process - handle/pid of the process
Returns: Returns:
0 - Success 0 - Error
1 - Error 1 - Success
*/ */
int kill_process(int pid); int kill_process(PROCESS process);
/* /*
Function: os_is_winxp_or_lower Function: os_is_winxp_or_lower

View file

@ -486,9 +486,9 @@ public:
GetPath(Type, pDir, pBuffer, BufferSize); 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; return pBuffer;
} }

View file

@ -37,7 +37,7 @@ public:
virtual bool RemoveBinaryFile(const char *pFilename) = 0; virtual bool RemoveBinaryFile(const char *pFilename) = 0;
virtual bool RenameBinaryFile(const char* pOldFilename, const char* pNewFilename) = 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); static void StripPathAndExtension(const char *pFilename, char *pBuffer, int BufferSize);
}; };

View file

@ -85,7 +85,7 @@ CMenus::CMenus()
m_DemoPlayerState = DEMOPLAYER_NONE; m_DemoPlayerState = DEMOPLAYER_NONE;
m_Dummy = false; m_Dummy = false;
m_ServerProcess.Pid = -1; m_ServerProcess.Process = 0;
m_ServerProcess.Initialized = false; m_ServerProcess.Initialized = false;
} }
@ -2071,12 +2071,13 @@ extern "C" void font_debug_render();
void CMenus::OnRender() 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 // TODO: Maybe call this less often, or probably even cleaner, use econ
if(!m_ServerProcess.Initialized) 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) if(File)
{ {
m_ServerProcess.LineReader.Init(File); m_ServerProcess.LineReader.Init(File);

View file

@ -17,7 +17,7 @@
struct CServerProcess struct CServerProcess
{ {
pid_t Pid; PROCESS Process;
bool Initialized; bool Initialized;
CLineReader LineReader; CLineReader LineReader;
}; };

View file

@ -49,17 +49,18 @@ void CMenus::RenderStartMenu(CUIRect MainView)
Menu.HSplitBottom(5.0f, &Menu, 0); // little space Menu.HSplitBottom(5.0f, &Menu, 0); // little space
Menu.HSplitBottom(40.0f, &Menu, &Button); Menu.HSplitBottom(40.0f, &Menu, &Button);
static int s_LocalServerButton = 0; 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); kill_process(m_ServerProcess.Process);
m_ServerProcess.Pid = shell_execute(PLAT_SERVER_EXEC); m_ServerProcess.Process = 0;
} }
else else
{ {
kill_process(m_ServerProcess.Pid); Storage()->RemoveBinaryFile("autoexec_server.log");
m_ServerProcess.Pid = -1; char aBuf[MAX_PATH_LENGTH];
m_ServerProcess.Process = shell_execute(Storage()->GetBinaryPath(PLAT_SERVER_EXEC, aBuf, sizeof(aBuf)));
} }
} }