Skip to content

Commit

Permalink
add launcher script
Browse files Browse the repository at this point in the history
  • Loading branch information
LizzyFleckenstein03 committed Aug 11, 2024
1 parent 7458c95 commit d8936e9
Show file tree
Hide file tree
Showing 25 changed files with 325 additions and 104 deletions.
2 changes: 1 addition & 1 deletion BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ meson compile -C build
```

to build the dragonblocks client and server.
If you use a debug build, the singleplayer script should be invoked from the build/ directory, because that's where the binaries are located.
If you use a debug build, the singleplayer/launcher script should be invoked from the build/ directory, because that's where the binaries are located.

## Building a statically linked release snapshot

Expand Down
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,18 @@ Head to <https://dragonblocks.lizzy.rs> for snapshot and release builds.
```sh
# on posix
./dragonblocks-server "<address>:<port>"
./dragonblocks-client "<address>:<port>"
# alternatively:
./singleplayer.sh
./dragonblocks-client "<player name>" "<address>:<port>"

# on windows
dragonblocks-server.exe "<address>:<port>"
dragonblocks-client.exe "<address>:<port>"
# alternatively:
singleplayer.bat
dragonblocks-client.exe "<player name>" "<address>:<port>"
```

or alternatively:

```sh
# on posix
./singleplayer.sh
./dragonblocks.sh singleplayer singleplayer_world

# on windows
singleplayer.bat
Expand All @@ -51,8 +47,7 @@ singleplayer.bat
| ESC | Pause / unpause game |

## System Requirements
Dragonblocks Alpha targets PCs only. Non x86-64 platforms may work, however there is no guarantee whatsoever.
You need a POSIX system conforming to the ISO C and POSIX 2008 standards. However, so far only GNU/Linux systems, in particular Ubuntu and Debian, have been tested.
You need a system conforming to the C23 and POSIX 2024 standards. Other systems may be supported as well.
The minimum OpenGL version is 3.3 core.
A PC with at least 4 CPU cores is recommended, but not necessarily required.

Expand Down Expand Up @@ -101,7 +96,7 @@ The name "Dragonblocks _Alpha_" does not have anything to do with the game being
- Exciting and feature-rich gameplay with the focus on exploring and adventuring, while still being multi-optional and not too bloated
- A simple structure and invocation syntax
- Using modern OpenGL to combine performance with graphics quality on high-end computers
- Portability between PCs running POSIX systems (focus: Linux, BSD, MacOS, Plan 9 APE, Windows MinGW)
- Portability between PCs running POSIX systems (focus: Linux, BSD, MacOS, Windows MinGW)

### What Dragonblocks Alpha does not aim to achieve
- Portability to Phones / Consoles
Expand Down
206 changes: 206 additions & 0 deletions dragonblocks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
#!/bin/sh
# dragonblocks launcher script
set -e

script_name="$0"

: "${DRAGONBLOCKS_DATA:=${XDG_DATA_HOME:-$HOME/.local/share}/dragonblocks_alpha}"
: "${DRAGONBLOCKS_CONFIG:=${XDG_CONFIG_HOME:-$HOME/.config}/dragonblocks_alpha}"
: "${DRAGONBLOCKS_TMP:=${XDG_RUNTIME_DIR:-/tmp}/dragonblocks_alpha}"
: "${DRAGONBLOCKS_LOG:=${XDG_STATE_HOME:-$HOME/.local/state}/dragonblocks_alpha}"

mkdir -p "$DRAGONBLOCKS_DATA/"
mkdir -p "$DRAGONBLOCKS_CONFIG/"
mkdir -p "$DRAGONBLOCKS_TMP/"
mkdir -p "$DRAGONBLOCKS_LOG/"

: "${DRAGONBLOCKS_URL:=https://dragonblocks.lizzy.rs}"

load_version() {
case "$1" in
"system")
version_exec_dir=""
version_working_dir="."
;;

"local")
version_exec_dir="./"
version_working_dir="."
;;

*)
version_exec_dir="./"
version_working_dir="$DRAGONBLOCKS_DATA/versions/$1"
;;
esac
}

timestamp() {
date +%Y-%m-%d-%H-%M-%S
}

default_version() {
if [ -n "$DRAGONBLOCKS_VERSION" ]; then
echo "$DRAGONBLOCKS_VERSION"
elif [ -f "$DRAGONBLOCKS_DATA/default_version" ]; then
cat "$DRAGONBLOCKS_DATA/default_version"
elif [ -x "./dragonblocks-client" ]; then
echo "local"
elif command -v "dragonblocks-client" &>/dev/null; then
echo "system"
else
>&2 echo "no dragonblocks installation found. use '$script_name version download' to obtain versions"
return 1
fi
}

get_world() {
local world="${1:?missing world (see '$script_name --help' for usage)}"
local path="$DRAGONBLOCKS_DATA/worlds/$world"
mkdir -p "$path/"
if [ ! -f "$path/version" ]; then
default_version > "$path/version"
fi
echo "$path"
}

launch() {
local which="$1"
local config="$(realpath $DRAGONBLOCKS_CONFIG/$which.conf)"
local log="$(realpath $DRAGONBLOCKS_LOG/$which-$(timestamp).log)"

shift
cd "$version_working_dir"
stdbuf -o0 "${version_exec_dir}dragonblocks-$which" --config "$config" $@ |& tee -i "$log"
}

unlaunch() {
pkill -g 0 -f dragonblocks-$1
}

case $1 in
help|--help)
cat << EOF
Synopsis: $script_name COMMAND
Available commands:
help
print this help text
singleplayer WORLD [PLAYER] [ADDRESS]
launch a singleplayer instance playing WORLD. if present, internal server
will listen to ADDRESS. if PLAYER is not present, "singleplayer" will be used
client PLAYER ADDRESS
launch client and connect it to server at ADDRESS. PLAYER is used as player name
server WORLD ADDRESS
launch server playing WORLD listening to ADDRESS
worlds
list available worlds including their path and version
version
print default version used for creating new worlds (server and singleplayer)
and connecting to servers (client). this is either the manually selected default version
or, if not set, "local" if the local directory contains a dragonblocks installation,
or "system" if dragonblocks is installed globally on the system.
version default VERSION
change default version
version list
list installed versions
version download [VERSION]
downloads and installs VERSION from the update server ($DRAGONBLOCKS_URL),
and sets it as default version. if VERSION is not present, the latest snapshot
will be downloaded
version check
print information about versions available on the update server
EOF

;;

singleplayer)
world="$(get_world $2)"
load_version "$(<$world/version)"

addrfile="$(realpath $DRAGONBLOCKS_TMP/address-$(timestamp).fifo)"
mkfifo "$addrfile"

trap "unlaunch server" SIGINT SIGTERM

launch server --world "$(realpath $world)" --write-address "$addrfile" "${4:-::1:}" &
launch client "${3:-singleplayer}" "$(<$addrfile)"

unlaunch server
;;

client)
load_version "$(default_version)"

trap "unlaunch client" SIGINT SIGTERM

launch client "${2:?missing player (see '$script_name --help')}" \
"${3:?missing address (see '$script_name --help')}"
;;

server)
world="$(get_world $2)"
load_version "$(<$world/version)"

trap "unlaunch server" SIGINT SIGTERM

launch server --world "$(realpath $world)" \
"${3:?missing address (see '$script_name --help')}"
;;

worlds)
mkdir -p "$DRAGONBLOCKS_DATA/worlds/"

for world in "$DRAGONBLOCKS_DATA/worlds/"*; do
echo "$(basename $world '') on version $(<$world/version) at $(realpath $world)"
done
;;

version)
mkdir -p "$DRAGONBLOCKS_DATA/versions/"

case "$2" in
"")
default_version
;;

default)
echo "${3:?missing version (see '$script_name --help')}" > \
"$DRAGONBLOCKS_DATA/default_version"
;;

list)
for version in "$DRAGONBLOCKS_DATA/versions/"*; do
echo "$(basename $version '')"
done

;;

download)
echo "not implemented yet"
;;

check)
echo "not implemented yet"
;;

*)
>&2 echo "invalid command, see '$script_name --help'"
;;
esac
;;

*)
>&2 echo "invalid command, see '$script_name --help'"
exit 1
;;
esac
8 changes: 0 additions & 8 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ if not cc.has_function('getline')
deps += dependency('getline')
endif

linenoise = cc.find_library('linenoise', required: false)
if linenoise.found()
linenoise = [linenoise, cc.find_library('stdc++')]
else
linenoise = dependency('linenoise')
endif

common_lib = static_library('dragonblocks',
sources: [
vcs_tag(
Expand Down Expand Up @@ -114,7 +107,6 @@ executable('dragonblocks-client',
dependency('glew'),
dependency('gl'),
dependency('glfw3'),
linenoise,
],
install: true,
)
Expand Down
2 changes: 1 addition & 1 deletion singleplayer.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@echo off
start "Internal Server" dragonblocks-server.exe "[::1]:4000"
echo "singleplayer" | dragonblocks-client.exe "[::1]:4000"
dragonblocks-client.exe singleplayer "[::1]:4000"
taskkill /FI "Internal Server" /T /F
5 changes: 0 additions & 5 deletions singleplayer.sh

This file was deleted.

32 changes: 28 additions & 4 deletions src/client/client.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include <dragonnet/init.h>
#include <dragonstd/flag.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include "client/client.h"
#include "client/client_auth.h"
#include "client/client_config.h"
#include "client/client_inventory.h"
#include "client/client_node.h"
#include "client/client_player.h"
Expand Down Expand Up @@ -77,7 +79,6 @@ static void on_ToClientAuth(__attribute__((unused)) DragonnetPeer *peer, ToClien
printf("[access] authenticated successfully\n");
} else {
client_auth.state = AUTH_INIT;
printf("[access] authentication failed, please try again\n");
}
pthread_cond_signal(&client_auth.cv);
pthread_mutex_unlock(&client_auth.mtx);
Expand Down Expand Up @@ -110,9 +111,30 @@ static void on_ToClientMovement(__attribute__((unused)) DragonnetPeer *peer, ToC

int main(int argc, char **argv)
{
dragonblocks_init(argc);
dragonblocks_init();

if (!(client = dragonnet_connect(argv[1]))) {
char *config_path = "client.conf";

struct option long_options[] = {
{"config", required_argument, 0, 'c' },
{}
};

int option;
while ((option = getopt_long(argc, argv, "c:", long_options, NULL)) != -1) {
switch (option) {
case 'c': config_path = optarg; break;
}
}

client_config_load(config_path);

if (argc-optind < 2) {
fprintf(stderr, "[error] missing name or address\n");
exit(EXIT_FAILURE);
}

if (!(client = dragonnet_connect(argv[optind+1]))) {
fprintf(stderr, "[error] failed to connect to server\n");
return EXIT_FAILURE;
}
Expand All @@ -139,9 +161,11 @@ int main(int argc, char **argv)
client_terrain_init();
client_player_init();
client_entity_init();
dragonnet_peer_run(client);
client_auth_init();

dragonnet_peer_run(client);
client_auth_run(argv[optind]);

game(&gfx_init);

dragonnet_peer_shutdown(client);
Expand Down
Loading

0 comments on commit d8936e9

Please sign in to comment.