From 1e8e42dc293537e1e6695b50d4d86e49becb4f8f Mon Sep 17 00:00:00 2001 From: Timo Reichl Date: Tue, 26 Jul 2022 13:43:47 +0200 Subject: [PATCH] server.sh: Fix laggy CSGO console by waking up the server on attach and hibernating it on detach Signed-off-by: Timo Reichl --- image/games/base/hlds/usr/local/bin/server.sh | 5 +++ .../games/base/srcds/usr/local/bin/server.sh | 42 +++++++++++++++++++ .../usr/local/lib/steamcmd/server-common.sh | 7 +++- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/image/games/base/hlds/usr/local/bin/server.sh b/image/games/base/hlds/usr/local/bin/server.sh index 61e63ee..3994cdd 100755 --- a/image/games/base/hlds/usr/local/bin/server.sh +++ b/image/games/base/hlds/usr/local/bin/server.sh @@ -23,6 +23,11 @@ _update_restart_needed() { } +attach() { + _attach +} + + update() { _update diff --git a/image/games/base/srcds/usr/local/bin/server.sh b/image/games/base/srcds/usr/local/bin/server.sh index c9ef13b..efac568 100755 --- a/image/games/base/srcds/usr/local/bin/server.sh +++ b/image/games/base/srcds/usr/local/bin/server.sh @@ -20,6 +20,43 @@ EOF_TICKRATE_ENABLER_CFG } +_setup_csgo_hibernation_hook_attached() { + ${TMUX_CMD} set-hook -t ${STEAMCMD_SERVER_SESSION_NAME} -u client-attached + ${TMUX_CMD} set-hook -t ${STEAMCMD_SERVER_SESSION_NAME} client-attached 'send-keys "sv_hibernate_when_empty 0" "Enter"' +} + + +_setup_csgo_hibernation_hook_detached() { + ${TMUX_CMD} set-hook -t ${STEAMCMD_SERVER_SESSION_NAME} -u client-detached + ${TMUX_CMD} set-hook -t ${STEAMCMD_SERVER_SESSION_NAME} client-detached 'send-keys "sv_hibernate_when_empty 1" "Enter"' +} + + +_setup_csgo_hibernation_hooks() { + if healthy; then + _setup_csgo_hibernation_hook_detached + _setup_csgo_hibernation_hook_attached + fi +} + + +_run_post_csgo() { + if _is_attached; then + _setup_csgo_hibernation_hook_detached + ${TMUX_CMD} send-keys -t ${STEAMCMD_SERVER_SESSION_NAME} "sv_hibernate_when_empty 0" "Enter" + fi +} + + +attach() { + if [[ ${STEAMCMD_SERVER_GAME} == "csgo" ]]; then + _setup_csgo_hibernation_hooks + fi + + _attach +} + + update() { _update } @@ -56,6 +93,11 @@ run() { -nodev" "Enter" _run_post + + if [[ ${STEAMCMD_SERVER_GAME} == "csgo" ]]; then + _run_post_csgo + fi + return 0 } diff --git a/image/games/base/usr/local/lib/steamcmd/server-common.sh b/image/games/base/usr/local/lib/steamcmd/server-common.sh index 2c5555e..e463932 100755 --- a/image/games/base/usr/local/lib/steamcmd/server-common.sh +++ b/image/games/base/usr/local/lib/steamcmd/server-common.sh @@ -27,6 +27,11 @@ MESSAGE_STEAMCMD_SERVER_HEALTHY="${MESSAGE_PREFIX} Server is healthy! ${MESSAGE_ # Define common functions +_is_attached() { + return $(${TMUX_CMD} ls -F "#{session_name}: #{?session_attached,yes,no}" | grep -w "${STEAMCMD_SERVER_SESSION_NAME}: yes" > /dev/null) +} + + _is_running() { return $(ps cax | grep ${1} > /dev/null) } @@ -47,7 +52,7 @@ wait() { } -attach() { +_attach() { ${TMUX_CMD} a -t ${STEAMCMD_SERVER_SESSION_NAME} }