Skip to content

Commit

Permalink
Use MT's config file to set craftium's tcp port
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikel committed Jun 27, 2024
1 parent c61f076 commit 16f0fa7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 29 deletions.
38 changes: 18 additions & 20 deletions craftium/minetest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,

Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down
8 changes: 2 additions & 6 deletions src/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 0 additions & 3 deletions src/client/sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
#include <string.h>
#include <arpa/inet.h>

#define CRAFTIUM_PORT_ENV_VAR "CRAFTIUM_PORT"
#define CRAFTIUM_DEFAULT_PORT 4343

/*
"Virtual" keyboard input handling
Expand Down
1 change: 1 addition & 0 deletions src/defaultsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 16f0fa7

Please sign in to comment.