From 1117e20e726785255f71f861c2bcbf7d0cd8a9df Mon Sep 17 00:00:00 2001 From: Nordup Date: Thu, 31 Aug 2023 14:29:24 +0400 Subject: [PATCH] windows sandbox_manager fix --- project/scripts/platform.gd | 42 +++++++++++++++++ project/scripts/sandbox/sandbox_executable.gd | 16 ++++--- project/scripts/sandbox/sandbox_manager.gd | 46 +++++++++++++++++-- 3 files changed, 95 insertions(+), 9 deletions(-) create mode 100644 project/scripts/platform.gd diff --git a/project/scripts/platform.gd b/project/scripts/platform.gd new file mode 100644 index 0000000..c52d77a --- /dev/null +++ b/project/scripts/platform.gd @@ -0,0 +1,42 @@ +extends Node +class_name Platform + +enum { + WINDOWS, + MACOS, + LINUX_BSD, + ANDROID, + IOS, + WEB +} + + +static func is_windows() -> bool: + return get_platform() == WINDOWS + + +static func is_linux() -> bool: + return get_platform() == LINUX_BSD + + +static func is_debug() -> bool: + return OS.is_debug_build() + + +static func get_platform() -> int: + match OS.get_name(): + "Windows", "UWP": + return WINDOWS + "macOS": + return MACOS + "Linux", "FreeBSD", "NetBSD", "OpenBSD", "BSD": + return LINUX_BSD + "Android": + return ANDROID + "iOS": + return IOS + "Web": + return WEB + _: + assert(false, "No such platform") + return -1 diff --git a/project/scripts/sandbox/sandbox_executable.gd b/project/scripts/sandbox/sandbox_executable.gd index cbcf63d..25e1c81 100644 --- a/project/scripts/sandbox/sandbox_executable.gd +++ b/project/scripts/sandbox/sandbox_executable.gd @@ -17,12 +17,16 @@ func get_executable_path() -> String: func get_filename() -> String: - if OS.is_debug_build(): - if OS.get_name() == "Windows": return windows_debug - else: return linux_debug - else: - if OS.get_name() == "Windows": return windows - else: return linux + var is_debug = Platform.is_debug() + + match Platform.get_platform(): + Platform.WINDOWS: + return windows_debug if is_debug else windows + Platform.LINUX_BSD: + return linux_debug if is_debug else linux + _: + assert(false, "Platform is not supported") + return "" func exists() -> bool: diff --git a/project/scripts/sandbox/sandbox_manager.gd b/project/scripts/sandbox/sandbox_manager.gd index 9710e4d..4b94a20 100644 --- a/project/scripts/sandbox/sandbox_manager.gd +++ b/project/scripts/sandbox/sandbox_manager.gd @@ -14,6 +14,16 @@ func _ready() -> void: func start_sandbox(gate: Gate) -> void: + match Platform.get_platform(): + Platform.WINDOWS: + start_sandbox_windows(gate) + Platform.LINUX_BSD: + start_sandbox_linux(gate) + _: + assert(false, "Platform is not supported") + + +func start_sandbox_linux(gate: Gate) -> void: if not snbx_executable.exists(): Debug.logerr("Sandbox executable not found at " + snbx_executable.path); return if not snbx_env.zip_exists(): @@ -21,12 +31,9 @@ func start_sandbox(gate: Gate) -> void: snbx_env.create_env(snbx_executable.path, gate) -# var pack_file = ProjectSettings.globalize_path(gate.resource_pack) -# var shared_libs = ProjectSettings.globalize_path(gate.shared_libs_dir) var args = [ snbx_env.start.get_base_dir(), # cd to dir "--main-pack", snbx_env.main_pack, -# "--gdext-libs-dir", shared_libs, "--resolution", "%dx%d" % [render_result.width, render_result.height] ] Debug.logclr(snbx_env.start + " " + " ".join(args), Color.DARK_VIOLET) @@ -35,7 +42,34 @@ func start_sandbox(gate: Gate) -> void: gate_events.gate_entered_emit() +func start_sandbox_windows(gate: Gate) -> void: + if not snbx_executable.exists(): + Debug.logerr("Sandbox executable not found at " + snbx_executable.path); return + + var pack_file = ProjectSettings.globalize_path(gate.resource_pack) + var shared_libs = ProjectSettings.globalize_path(gate.shared_libs_dir) + var args = [ + "--main-pack", pack_file, + "--gdext-libs-dir", shared_libs, + "--resolution", "%dx%d" % [render_result.width, render_result.height] + ] + Debug.logclr(snbx_executable.path + " " + " ".join(args), Color.DARK_VIOLET) + sandbox_pid = OS.create_process(snbx_executable.path, args) + + gate_events.gate_entered_emit() + + func kill_sandbox() -> void: + match Platform.get_platform(): + Platform.WINDOWS: + kill_sandbox_windows() + Platform.LINUX_BSD: + kill_sandbox_linux() + _: + assert(false, "Platform is not supported") + + +func kill_sandbox_linux() -> void: if sandbox_pid == 0: return var pids = snbx_env.get_subprocesses(sandbox_pid) @@ -48,5 +82,11 @@ func kill_sandbox() -> void: snbx_env.clean() +func kill_sandbox_windows() -> void: + if OS.is_process_running(sandbox_pid): + OS.kill(sandbox_pid) + Debug.logclr("Process killed " + str(sandbox_pid), Color.DIM_GRAY) + + func _exit_tree() -> void: kill_sandbox()