Reworked SubProcess now it builds on windows as an amalgamation.

This commit is contained in:
Relintai 2024-01-19 14:36:54 +00:00
parent 2de9d4d9dc
commit 3f8ce2c542
7 changed files with 132 additions and 99 deletions

View File

@ -46,6 +46,8 @@ cl /D_REENTRANT /EHsc /Zi /Isfw /c sfw/core/thread.cpp /Fo:sfw/core/thread.obj
cl /D_REENTRANT /EHsc /Zi /Isfw /c sfw/core/socket.cpp /Fo:sfw/core/socket.obj
cl /D_REENTRANT /EHsc /Zi /Isfw /c sfw/core/inet_address.cpp /Fo:sfw/core/inet_address.obj
cl /D_REENTRANT /EHsc /Zi /Isfw /c sfw/core/sub_process.cpp /Fo:sfw/core/sub_process.obj
cl /D_REENTRANT /EHsc /Zi /Isfw /c sfw/core/sfw_core.cpp /Fo:sfw/core/sfw_core.obj
cl /D_REENTRANT /EHsc /Zi /Isfw /c sfw/object/object.cpp /Fo:sfw/object/object.obj
@ -126,6 +128,7 @@ cl /D_REENTRANT /Zi ^
sfw/core/pool_vector.obj sfw/core/pool_allocator.obj sfw/core/mutex.obj sfw/core/sfw_time.obj ^
sfw/core/dir_access.obj sfw/core/file_access.obj sfw/core/thread.obj ^
sfw/core/socket.obj sfw/core/inet_address.obj ^
sfw/core/sub_process.obj ^
sfw/core/sfw_core.obj ^
sfw/object/object.obj sfw/object/reference.obj sfw/object/core_string_names.obj ^
sfw/object/variant.obj sfw/object/variant_op.obj sfw/object/psignal.obj ^

View File

@ -9,6 +9,48 @@
#if defined(_WIN64) || defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
typedef struct tagLOGCONTEXTW {
WCHAR lcName[40];
UINT lcOptions;
UINT lcStatus;
UINT lcLocks;
UINT lcMsgBase;
UINT lcDevice;
UINT lcPktRate;
DWORD lcPktData;
DWORD lcPktMode;
DWORD lcMoveMask;
DWORD lcBtnDnMask;
DWORD lcBtnUpMask;
LONG lcInOrgX;
LONG lcInOrgY;
LONG lcInOrgZ;
LONG lcInExtX;
LONG lcInExtY;
LONG lcInExtZ;
LONG lcOutOrgX;
LONG lcOutOrgY;
LONG lcOutOrgZ;
LONG lcOutExtX;
LONG lcOutExtY;
LONG lcOutExtZ;
DWORD lcSensX;
DWORD lcSensY;
DWORD lcSensZ;
BOOL lcSysMode;
int lcSysOrgX;
int lcSysOrgY;
int lcSysExtX;
int lcSysExtY;
DWORD lcSysSensX;
DWORD lcSysSensY;
} LOGCONTEXTW;
typedef HANDLE(WINAPI *WTOpenPtr)(HWND p_window, LOGCONTEXTW *p_ctx, BOOL p_enable);
// TODO clean these up
#include <avrt.h>
#include <direct.h>
@ -18,6 +60,16 @@
#include <shlobj.h>
#include <wchar.h>
struct SubProcess::SubProcessWindowsData {
struct ProcessInfo {
STARTUPINFO si;
PROCESS_INFORMATION pi;
};
HANDLE _pipe_handles[2];
ProcessInfo _process_info;
};
Error SubProcess::start() {
if (_executable_path.empty()) {
return ERR_FILE_BAD_PATH;
@ -34,10 +86,10 @@ Error SubProcess::start() {
cmdline += " " + _quote_command_line_argument(_arguments[i]);
}
ZeroMemory(&_process_info.si, sizeof(_process_info.si));
_process_info.si.cb = sizeof(_process_info.si);
ZeroMemory(&_process_info.pi, sizeof(_process_info.pi));
LPSTARTUPINFOW si_w = (LPSTARTUPINFOW)&_process_info.si;
ZeroMemory(&_data->_process_info.si, sizeof(_data->_process_info.si));
_data->_process_info.si.cb = sizeof(_data->_process_info.si);
ZeroMemory(&_data->_process_info.pi, sizeof(_data->_process_info.pi));
LPSTARTUPINFOW si_w = (LPSTARTUPINFOW)&_data->_process_info.si;
Char16String modstr = cmdline.utf16(); // Windows wants to change this no idea why.
@ -50,13 +102,13 @@ Error SubProcess::start() {
sa.bInheritHandle = true;
sa.lpSecurityDescriptor = NULL;
ERR_FAIL_COND_V(!CreatePipe(&_pipe_handles[0], &_pipe_handles[1], &sa, 0), ERR_CANT_FORK);
ERR_FAIL_COND_V(!SetHandleInformation(_pipe_handles[0], HANDLE_FLAG_INHERIT, 0), ERR_CANT_FORK); // Read handle is for host process only and should not be inherited.
ERR_FAIL_COND_V(!CreatePipe(&_data->_pipe_handles[0], &_data->_pipe_handles[1], &sa, 0), ERR_CANT_FORK);
ERR_FAIL_COND_V(!SetHandleInformation(_data->_pipe_handles[0], HANDLE_FLAG_INHERIT, 0), ERR_CANT_FORK); // Read handle is for host process only and should not be inherited.
_process_info.si.dwFlags |= STARTF_USESTDHANDLES;
_process_info.si.hStdOutput = _pipe_handles[1];
_data->_process_info.si.dwFlags |= STARTF_USESTDHANDLES;
_data->_process_info.si.hStdOutput = _data->_pipe_handles[1];
if (_read_std_err) {
_process_info.si.hStdError = _pipe_handles[1];
_data->_process_info.si.hStdError = _data->_pipe_handles[1];
}
inherit_handles = true;
}
@ -68,20 +120,20 @@ Error SubProcess::start() {
creaton_flags |= CREATE_NO_WINDOW;
}
int ret = CreateProcessW(nullptr, (LPWSTR)(modstr.ptrw()), nullptr, nullptr, inherit_handles, creaton_flags, nullptr, nullptr, si_w, &_process_info.pi);
int ret = CreateProcessW(nullptr, (LPWSTR)(modstr.ptrw()), nullptr, nullptr, inherit_handles, creaton_flags, nullptr, nullptr, si_w, &_data->_process_info.pi);
if (!ret && _read_output) {
CloseHandle(_pipe_handles[0]); // Cleanup pipe handles.
CloseHandle(_pipe_handles[1]);
CloseHandle(_data->_pipe_handles[0]); // Cleanup pipe handles.
CloseHandle(_data->_pipe_handles[1]);
_pipe_handles[0] = NULL;
_pipe_handles[1] = NULL;
_data->_pipe_handles[0] = NULL;
_data->_pipe_handles[1] = NULL;
}
ERR_FAIL_COND_V(ret == 0, ERR_CANT_FORK);
if (_blocking) {
if (_read_output) {
CloseHandle(_pipe_handles[1]); // Close pipe write handle (only child process is writing).
CloseHandle(_data->_pipe_handles[1]); // Close pipe write handle (only child process is writing).
int bytes_in_buffer = 0;
@ -89,7 +141,7 @@ Error SubProcess::start() {
DWORD read = 0;
for (;;) { // Read StdOut and StdErr from pipe.
_bytes.resize(bytes_in_buffer + CHUNK_SIZE);
const bool success = ReadFile(_pipe_handles[0], _bytes.ptr() + bytes_in_buffer, CHUNK_SIZE, &read, NULL);
const bool success = ReadFile(_data->_pipe_handles[0], _bytes.ptr() + bytes_in_buffer, CHUNK_SIZE, &read, NULL);
if (!success || read == 0) {
break;
}
@ -119,27 +171,27 @@ Error SubProcess::start() {
_append_to_pipe(_bytes.ptr(), bytes_in_buffer);
}
CloseHandle(_pipe_handles[0]); // Close pipe read handle.
CloseHandle(_data->_pipe_handles[0]); // Close pipe read handle.
}
WaitForSingleObject(_process_info.pi.hProcess, INFINITE);
WaitForSingleObject(_data->_process_info.pi.hProcess, INFINITE);
DWORD ret2;
GetExitCodeProcess(_process_info.pi.hProcess, &ret2);
GetExitCodeProcess(_data->_process_info.pi.hProcess, &ret2);
_exitcode = ret2;
CloseHandle(_process_info.pi.hProcess);
CloseHandle(_process_info.pi.hThread);
CloseHandle(_data->_process_info.pi.hProcess);
CloseHandle(_data->_process_info.pi.hThread);
} else {
if (_read_output) {
//eventually we will need to keep this
CloseHandle(_pipe_handles[1]); // Close pipe write handle (only child process is writing).
_pipe_handles[1] = NULL;
CloseHandle(_data->_pipe_handles[1]); // Close pipe write handle (only child process is writing).
_data->_pipe_handles[1] = NULL;
}
_process_started = true;
ProcessID pid = _process_info.pi.dwProcessId;
ProcessID pid = _data->_process_info.pi.dwProcessId;
_process_id = pid;
}
@ -151,24 +203,24 @@ Error SubProcess::stop() {
return OK;
}
if (_pipe_handles[0]) {
CloseHandle(_pipe_handles[0]); // Cleanup pipe handles.
_pipe_handles[0] = NULL;
if (_data->_pipe_handles[0]) {
CloseHandle(_data->_pipe_handles[0]); // Cleanup pipe handles.
_data->_pipe_handles[0] = NULL;
}
if (_pipe_handles[1]) {
CloseHandle(_pipe_handles[1]);
_pipe_handles[1] = NULL;
if (_data->_pipe_handles[1]) {
CloseHandle(_data->_pipe_handles[1]);
_data->_pipe_handles[1] = NULL;
}
const int ret = TerminateProcess(_process_info.pi.hProcess, 0);
const int ret = TerminateProcess(_data->_process_info.pi.hProcess, 0);
CloseHandle(_process_info.pi.hProcess);
CloseHandle(_process_info.pi.hThread);
CloseHandle(_data->_process_info.pi.hProcess);
CloseHandle(_data->_process_info.pi.hThread);
ZeroMemory(&_process_info.si, sizeof(_process_info.si));
_process_info.si.cb = sizeof(_process_info.si);
ZeroMemory(&_process_info.pi, sizeof(_process_info.pi));
ZeroMemory(&_data->_process_info.si, sizeof(_data->_process_info.si));
_data->_process_info.si.cb = sizeof(_data->_process_info.si);
ZeroMemory(&_data->_process_info.pi, sizeof(_data->_process_info.pi));
_process_started = false;
@ -180,7 +232,7 @@ Error SubProcess::poll() {
return FAILED;
}
if (!_pipe_handles[0]) {
if (!_data->_pipe_handles[0]) {
return FAILED;
}
@ -192,7 +244,7 @@ Error SubProcess::poll() {
DWORD read = 0;
_bytes.resize(bytes_in_buffer + CHUNK_SIZE);
const bool success = ReadFile(_pipe_handles[0], _bytes.ptr() + bytes_in_buffer, CHUNK_SIZE, &read, NULL);
const bool success = ReadFile(_data->_pipe_handles[0], _bytes.ptr() + bytes_in_buffer, CHUNK_SIZE, &read, NULL);
if (!success) {
stop();
@ -251,7 +303,7 @@ bool SubProcess::is_process_running() const {
}
DWORD dw_exit_code = 0;
if (!GetExitCodeProcess(_process_info.pi.hProcess, &dw_exit_code)) {
if (!GetExitCodeProcess(_data->_process_info.pi.hProcess, &dw_exit_code)) {
return false;
}
@ -298,6 +350,8 @@ void SubProcess::_append_to_pipe(char *p_bytes, int p_size) {
}
SubProcess::SubProcess() {
_data = memnew(SubProcessWindowsData);
_blocking = false;
_read_output = true;
@ -314,17 +368,19 @@ SubProcess::SubProcess() {
_process_id = ProcessID();
_exitcode = 0;
_pipe_handles[0] = NULL;
_pipe_handles[1] = NULL;
_data->_pipe_handles[0] = NULL;
_data->_pipe_handles[1] = NULL;
_process_started = false;
ZeroMemory(&_process_info.si, sizeof(_process_info.si));
_process_info.si.cb = sizeof(_process_info.si);
ZeroMemory(&_process_info.pi, sizeof(_process_info.pi));
ZeroMemory(&_data->_process_info.si, sizeof(_data->_process_info.si));
_data->_process_info.si.cb = sizeof(_data->_process_info.si);
ZeroMemory(&_data->_process_info.pi, sizeof(_data->_process_info.pi));
}
SubProcess::~SubProcess() {
stop();
memdelete(_data);
}
#else
@ -652,8 +708,8 @@ Error SubProcess::run(const String &p_executable_path, const Vector<String> &p_a
return ERR_ALREADY_IN_USE;
}
String _executable_path = p_executable_path;
Vector<String> _arguments = p_arguments;
_executable_path = p_executable_path;
_arguments = p_arguments;
_blocking = p_blocking;

View File

@ -15,6 +15,7 @@
#include "core/mutex.h"
#include "core/typedefs.h"
#include "core/ustring.h"
#include "core/error_list.h"
#include <stdio.h>
//--STRIP
@ -25,49 +26,6 @@
#include "core/local_vector.h"
//--STRIP
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
typedef struct tagLOGCONTEXTW {
WCHAR lcName[40];
UINT lcOptions;
UINT lcStatus;
UINT lcLocks;
UINT lcMsgBase;
UINT lcDevice;
UINT lcPktRate;
DWORD lcPktData;
DWORD lcPktMode;
DWORD lcMoveMask;
DWORD lcBtnDnMask;
DWORD lcBtnUpMask;
LONG lcInOrgX;
LONG lcInOrgY;
LONG lcInOrgZ;
LONG lcInExtX;
LONG lcInExtY;
LONG lcInExtZ;
LONG lcOutOrgX;
LONG lcOutOrgY;
LONG lcOutOrgZ;
LONG lcOutExtX;
LONG lcOutExtY;
LONG lcOutExtZ;
DWORD lcSensX;
DWORD lcSensY;
DWORD lcSensZ;
BOOL lcSysMode;
int lcSysOrgX;
int lcSysOrgY;
int lcSysExtX;
int lcSysExtY;
DWORD lcSysSensX;
DWORD lcSysSensY;
} LOGCONTEXTW;
typedef HANDLE(WINAPI *WTOpenPtr)(HWND p_window, LOGCONTEXTW *p_ctx, BOOL p_enable);
#else
#endif
/**
@ -78,6 +36,10 @@ class SubProcess {
public:
typedef int64_t ProcessID;
#if defined(_WIN64) || defined(_WIN32)
struct SubProcessWindowsData;
#endif
static SubProcess *create();
String get_executable_path() const;
@ -123,7 +85,7 @@ public:
virtual Error send_data(const String &p_data);
virtual bool is_process_running() const;
Error run(const String &p_executable_path, const Vector<String> &p_arguments, bool p_output = true, bool p_blocking = true, bool p_read_std_err = false, bool p_use_pipe_mutex = false, bool p_open_console = false);
Error run(const String &p_executable_path, const Vector<String> &p_arguments = Vector<String>(), bool p_output = true, bool p_blocking = true, bool p_read_std_err = false, bool p_use_pipe_mutex = false, bool p_open_console = false);
SubProcess();
virtual ~SubProcess();
@ -157,16 +119,11 @@ protected:
String _quote_command_line_argument(const String &p_text) const;
void _append_to_pipe(char *p_bytes, int p_size);
struct ProcessInfo {
STARTUPINFO si;
PROCESS_INFORMATION pi;
};
bool _process_started;
HANDLE _pipe_handles[2];
ProcessInfo _process_info;
LocalVector<char> _bytes;
SubProcessWindowsData *_data;
#else
FILE *_process_fp;
char _process_buf[65535];

View File

@ -12,6 +12,7 @@
#include "render_core/mesh_utils.h"
#include "render_immediate/renderer.h"
//#include "render_core/font.h"
#include "core/sub_process.h"
void GameScene::input_event(const Ref<InputEvent> &event) {
//ERR_PRINT(event->as_text());
@ -77,6 +78,14 @@ void GameScene::input_event(const Ref<InputEvent> &event) {
}
}
if (k->get_physical_scancode() == KEY_H) {
if (pressed) {
ERR_PRINT("Running Game");
SubProcess s;
ERR_PRINT(itos(s.run("game")));
}
}
return;
}

View File

@ -423,7 +423,7 @@
//#include <shlobj.h>
//#include <wchar.h>
//--STRIP
//{//{FILE:sfw/core/sub_process.cpp}}
{{FILE:sfw/core/sub_process.cpp}}
//--STRIP
//#include "core/pool_vector.h"

View File

@ -443,7 +443,7 @@
//Win Only
//#include "core/local_vector.h"
//--STRIP
//{//{FILE:sfw/core/sub_process.h}}
{{FILE:sfw/core/sub_process.h}}
//--STRIP
//no includes

View File

@ -66,6 +66,14 @@ void GameScene::input_event(const Ref<InputEvent> &event) {
}
}
if (k->get_physical_scancode() == KEY_H) {
if (pressed) {
ERR_PRINT("Running Game");
SubProcess s;
ERR_PRINT(itos(s.run("game")));
}
}
return;
}