forked from lloesche/valheim-server-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalheim-server
executable file
·135 lines (117 loc) · 3.71 KB
/
valheim-server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash
# valheim-server starts the Valheim server
# binary either vanilla from /opt/valheim/server
# or if VALHEIM_PLUS=true with the ValheimPlus mod
# from /opt/valheim/plus
# Include defaults and common functions
. /usr/local/etc/valheim/defaults
. /usr/local/etc/valheim/common
export SteamAppId=892970
valheim_server_pid=-1
timeout=29
kill_signal=TERM
password_args=(-password "$SERVER_PASS")
if [ "$VALHEIM_PLUS" = true ]; then
cd "$vp_install_path"
rm -rf "$vp_install_path.old"
export DOORSTOP_ENABLE=TRUE
export DOORSTOP_INVOKE_DLL_PATH="./BepInEx/core/BepInEx.Preloader.dll"
export DOORSTOP_CORLIB_OVERRIDE_PATH="./unstripped_corlib"
export LD_LIBRARY_PATH="$vp_install_path/linux64/:$vp_install_path/doorstop_libs/"
export LD_PRELOAD=libdoorstop_x64.so
valheim_server="$vp_install_path/valheim_server.x86_64"
[ -z "$SERVER_PASS" ] && password_args=()
else
cd "$valheim_install_path"
export LD_LIBRARY_PATH="$valheim_install_path/linux64/"
valheim_server="$valheim_install_path/valheim_server.x86_64"
fi
main() {
wait_for_server_download
run_server
}
wait_for_server_download() {
while :; do
if [ -f "$valheim_server" ]; then
break
else
debug "Valheim Server is not yet downloaded - waiting"
sleep 7
fi
done
}
# This runs after server start and waits for the
# config files to either exist or for the server
# to create them so their access permissions can
# be adjusted to the proper values.
ensure_permissions_after_start() {
while :; do
if [ -f "/config/adminlist.txt" ]; then
ensure_permissions
break
else
sleep 4
fi
done
}
run_server() {
info "Running Valheim Server"
"$valheim_server" -nographics -batchmode -name "$SERVER_NAME" -port "$SERVER_PORT" -world "$WORLD_NAME" -public "$SERVER_PUBLIC" "${password_args[@]}" > >(filter stdout) 2> >(filter stderr >&2) &
valheim_server_pid=$!
unset LD_LIBRARY_PATH
unset LD_PRELOAD
echo $valheim_server_pid > "$valheim_server_pidfile"
ensure_permissions_after_start &
permissions_wait_pid=$!
wait $valheim_server_pid
[ -d "/proc/$permissions_wait_pid" ] && kill -TERM $permissions_wait_pid
clear_lock "$valheim_server_pidfile"
exit 0
}
# This function gets the servers stdout/stderr
# output piped into. It allows for filtering
# of unwanted debug log messages.
filter() {
unset LD_LIBRARY_PATH
unset LD_PRELOAD
case $1 in
stdout)
log_filter=$SERVER_LOG_STDOUT_FILTER
;;
stderr)
log_filter=$SERVER_LOG_STDERR_FILTER
;;
esac
eval $log_filter
}
shutdown() {
unset LD_LIBRARY_PATH
unset LD_PRELOAD
if [ $valheim_server_pid -eq -1 ]; then
debug "Valheim server is not running yet - aborting startup"
exit
fi
info "Shutting down Valheim Server with PID $valheim_server_pid"
kill -INT $valheim_server_pid
shutdown_timeout=$(($(date +%s)+$timeout))
while [ -d "/proc/$valheim_server_pid" ]; do
if [ $(date +%s) -gt $shutdown_timeout ]; then
shutdown_timeout=$(($(date +%s)+$timeout))
warn "Timeout while waiting for server to shutdown - sending SIG$kill_signal to PID $valheim_server_pid"
kill -$kill_signal $valheim_server_pid
case "$kill_signal" in
INT)
kill_signal=TERM
;;
*)
kill_signal=KILL
esac
fi
debug "Waiting for Valheim Server with PID $valheim_server_pid to shutdown"
sleep 6
done
info "Shutdown complete"
exit
}
trap shutdown SIGINT SIGTERM
main