From 16f0fa71e33ffd8b7ad298cda4686daec7dc24fd Mon Sep 17 00:00:00 2001 From: Mikel Date: Thu, 27 Jun 2024 11:16:47 +0200 Subject: [PATCH] Use MT's config file to set craftium's tcp port --- craftium/minetest.py | 38 ++++++++++++++++++-------------------- src/client/client.cpp | 8 ++------ src/client/sync.h | 3 --- src/defaultsettings.cpp | 1 + 4 files changed, 21 insertions(+), 29 deletions(-) diff --git a/craftium/minetest.py b/craftium/minetest.py index e513573e0..efabcaa19 100644 --- a/craftium/minetest.py +++ b/craftium/minetest.py @@ -64,11 +64,19 @@ def __init__( # delete the directory if it already exists if os.path.exists(self.run_dir): shutil.rmtree(self.run_dir) - # create the directory os.mkdir(self.run_dir) print(f"==> Creating Minetest run directory: {self.run_dir}") + if tcp_port is None: + # select a (random) free port for the craftium <-> minetest communication + while True: + self.port = random.randint(49152, 65535) + if not is_port_in_use(self.port): + break + else: + self.port = tcp_port + config = dict( # Base config enable_sound=False, @@ -84,6 +92,8 @@ def __init__( undersampling=1, # fov=self.fov_y, + craftium_port=self.port, + # Adapt HUD size to display size, based on (1024, 600) default # hud_scaling=self.display_size[0] / 1024, @@ -126,8 +136,6 @@ def __init__( else: root_path = minetest_dir - print(f"** Craftium's root path is: {root_path}") - # create the directory tree structure needed by minetest self._create_mt_dirs(root_dir=root_path, target_dir=self.run_dir, sync_dir=sync_dir) @@ -141,30 +149,20 @@ def __init__( self.proc = None # will hold the mintest's process - # set the env. variables to execute mintest in headless mode - # if headless: - # os.environ["SDL_VIDEODRIVER"] = "offscreen" - self.mt_env = {} - if headless: self.mt_env["SDL_VIDEODRIVER"] = "offscreen" - if tcp_port is None: - # select a (random) free port for the craftium <-> minetest communication - while True: - self.port = random.randint(49152, 65535) - if not is_port_in_use(self.port): - break - else: - self.port = tcp_port - self.mt_env["CRAFTIUM_PORT"] = str(self.port) - def start_process(self): - self.proc = launch_process(self.launch_cmd, self.run_dir, env_vars=self.mt_env) + self.proc = launch_process( + self.launch_cmd, + self.run_dir, + env_vars=self.mt_env + ) def kill_process(self): - self.proc.terminate() + if self.proc is not None: + self.proc.terminate() def clear(self): # delete the run's directory diff --git a/src/client/client.cpp b/src/client/client.cpp index 5c92c44b2..c301045bc 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -157,12 +157,8 @@ Client::Client( void Client::startPyServer() { - // Get the craftium port from the env. var. - char* port_s = getenv(CRAFTIUM_PORT_ENV_VAR); - if (port_s == NULL) - pyserv_port = CRAFTIUM_DEFAULT_PORT; - else - pyserv_port = atoi(port_s); + // Get the craftium port from the config file + pyserv_port = g_settings->getU32("craftium_port"); printf("[*] Minetest using port %d to communicate with craftium\n", pyserv_port); diff --git a/src/client/sync.h b/src/client/sync.h index ab4b409e9..b3d12476f 100644 --- a/src/client/sync.h +++ b/src/client/sync.h @@ -10,9 +10,6 @@ #include #include -#define CRAFTIUM_PORT_ENV_VAR "CRAFTIUM_PORT" -#define CRAFTIUM_DEFAULT_PORT 4343 - /* "Virtual" keyboard input handling diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp index f16c56db4..6268addc4 100644 --- a/src/defaultsettings.cpp +++ b/src/defaultsettings.cpp @@ -134,6 +134,7 @@ void set_default_settings() settings->setDefault("occlusion_culler", "bfs"); settings->setDefault("enable_raytraced_culling", "true"); settings->setDefault("chat_weblink_color", "#8888FF"); + settings->setDefault("craftium_port", "55555"); // Keymap settings->setDefault("remote_port", "30000");