diff --git a/dragonblocks.sh b/dragonblocks.sh index 149834b..bb8574e 100755 --- a/dragonblocks.sh +++ b/dragonblocks.sh @@ -35,7 +35,7 @@ load_version() { ;; /*) - version_exec_dir="$1/" + version_exec_dir="./" version_working_dir="$1" ;; @@ -79,16 +79,27 @@ launch() { local which="$1" local config="$(realpath $DRAGONBLOCKS_CONFIG/$which.conf)" local log="$(realpath $log_path/$which-$(timestamp).log)" - shift + cd "$version_working_dir" - stdbuf -o0 "${version_exec_dir}dragonblocks-$which" --config "$config" $@ |& tee -i "$log" + exec 3> >(tee -i "$log" >&2) + "${version_exec_dir}dragonblocks-$which" --config "$config" $@ 2>&3 } unlaunch() { pkill -g 0 -f dragonblocks-$1 } +server_ipc() { + while read -ra cmd; do + case "${cmd[0]}" in + listen) + echo "${cmd[@]:1}" > "$1" + ;; + esac + done +} + case $1 in help|--help) cat << EOF @@ -143,8 +154,16 @@ EOF trap "unlaunch server" SIGINT SIGTERM - launch server --world "$(realpath $world)" --write-address "$addrfile" "${4:-::1:}" & - launch client --screenshot-dir "$(realpath $screenshot_path)" "${3:-singleplayer}" "$(<$addrfile)" + launch server \ + --world "$(realpath $world)" \ + --ipc \ + "${4:-::1:}" \ + | server_ipc "$addrfile" & + + launch client \ + --screenshot-dir "$(realpath $screenshot_path)" \ + "${3:-singleplayer}" \ + "$(<$addrfile)" unlaunch server ;; @@ -154,7 +173,9 @@ EOF trap "unlaunch client" SIGINT SIGTERM - launch client --screenshot-dir "$(realpath $screenshot_path)" "${2:?missing player (see '$script_name --help')}" \ + launch client \ + --screenshot-dir "$(realpath $screenshot_path)" \ + "${2:?missing player (see '$script_name --help')}" \ "${3:?missing address (see '$script_name --help')}" ;; @@ -164,7 +185,8 @@ EOF trap "unlaunch server" SIGINT SIGTERM - launch server --world "$(realpath $world)" \ + launch server \ + --world "$(realpath $world)" \ "${3:?missing address (see '$script_name --help')}" ;; diff --git a/src/common/init.c b/src/common/init.c index 1f572b1..501c8a8 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -7,6 +7,9 @@ void dragonblocks_init() { + setvbuf(stdout, NULL, _IOLBF, 0); + setvbuf(stderr, NULL, _IOLBF, 0); + #ifdef __GLIBC__ // check whether bloat is enabled pthread_setname_np(pthread_self(), "main"); #endif // __GLIBC__ diff --git a/src/server/server.c b/src/server/server.c index c5bdc20..c5e395b 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -64,21 +64,21 @@ int main(int argc, char **argv) char *config_path = "server.conf"; char *world_path = "."; - char *write_address = NULL; + bool ipc = false; struct option long_options[] = { - {"config", required_argument, 0, 'c' }, - {"world", required_argument, 0, 'w' }, - {"write-address", required_argument, 0, 'a' }, + {"config", required_argument, 0, 'c' }, + {"world", required_argument, 0, 'w' }, + {"ipc", no_argument, 0, 'i' }, {} }; int option; - while ((option = getopt_long(argc, argv, "c:w:a:", long_options, NULL)) != -1) { + while ((option = getopt_long(argc, argv, "c:w:i", long_options, NULL)) != -1) { switch (option) { case 'c': config_path = optarg; break; case 'w': world_path = optarg; break; - case 'a': write_address = optarg; break; + case 'i': ipc = true; break; } } @@ -94,17 +94,7 @@ int main(int argc, char **argv) return EXIT_FAILURE; } - if (write_address) { - FILE *addrfile = fopen(write_address, "a"); - if (!addrfile) { - fprintf(stderr, "[error] failed to open address file\n"); - return EXIT_FAILURE; - } - - fprintf(addrfile, "%s\n", server->address); - fclose(addrfile); - } - + if (ipc) printf("listen %s\n", server->address); fprintf(stderr, "[info] listening on %s\n", server->address); server->on_connect = &server_player_add;