forked from escapingnetwork/core-keeper-dedicated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
launch.sh
182 lines (151 loc) · 7.12 KB
/
launch.sh
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/bash
# Switch to workdir
cd "${STEAMAPPDIR}"
xvfbpid=""
ckpid=""
function kill_corekeeperserver {
if [[ ! -z "$ckpid" ]]; then
kill $ckpid
wait $ckpid
fi
if [[ ! -z "$xvfbpid" ]]; then
kill $xvfbpid
fi
}
trap kill_corekeeperserver EXIT
if ! (dpkg -l xvfb >/dev/null) ; then
echo "Installing xvfb dependency..."
sleep 1
sudo apt-get update -yy && sudo apt-get install xvfb -yy
fi
set -m
rm -f /tmp/.X99-lock
Xvfb :99 -screen 0 1x1x24 -nolisten tcp &
export DISPLAY=:99
xvfbpid=$!
# Wait for xvfb ready.
retry_count=0
max_retries=2
xvfb_test=0
until [ $retry_count -gt $max_retries ]; do
xvinfo
xvfb_test=$?
if [ $xvfb_test != 255 ]; then
retry_count=$(($max_retries + 1))
else
retry_count=$(($retry_count + 1))
echo "Failed to start Xvfb, retry: $retry_count"
sleep 2
fi
done
if [ $xvfb_test == 255 ]; then exit 255; fi
rm -f GameID.txt
chmod +x ./CoreKeeperServer
# Build Parameters
declare -a params
params=(-batchmode -logfile "CoreKeeperServerLog.txt")
if [ ! -z "${WORLD_INDEX}" ]; then params=( "${params[@]}" -world "${WORLD_INDEX}" ); fi
if [ ! -z "${WORLD_NAME}" ]; then params=( "${params[@]}" -worldname "${WORLD_NAME}" ); fi
if [ ! -z "${WORLD_SEED}" ]; then params=( "${params[@]}" -worldseed "${WORLD_SEED}" ); fi
if [ ! -z "${WORLD_MODE}" ]; then params=( "${params[@]}" -worldmode "${WORLD_MODE}" ); fi
if [ ! -z "${GAME_ID}" ]; then params=( "${params[@]}" -gameid "${GAME_ID}" ); fi
if [ ! -z "${DATA_PATH}" ]; then params=( "${params[@]}" -datapath "${DATA_PATH}" ); fi
if [ ! -z "${MAX_PLAYERS}" ]; then params=( "${params[@]}" -maxplayers "${MAX_PLAYERS}" ); fi
if [ ! -z "${SEASON}" ]; then params=( "${params[@]}" -season "${SEASON}" ); fi
if [ ! -z "${SERVER_IP}" ]; then params=( "${params[@]}" -ip "${SERVER_IP}" ); fi
if [ ! -z "${SERVER_PORT}" ]; then params=( "${params[@]}" -port "${SERVER_PORT}" ); fi
echo "${params[@]}"
DISPLAY=:99 LD_LIBRARY_PATH="$LD_LIBRARY_PATH:../Steamworks SDK Redist/linux64/" ./CoreKeeperServer "${params[@]}"&
ckpid=$!
echo "Started server process with pid $ckpid"
while [ ! -f GameID.txt ]; do
sleep 0.1
done
gameid=$(cat GameID.txt)
echo "Game IDs: ${gameid}"
if [ -z "$DISCORD" ]; then
DISCORD=0
fi
if [ $DISCORD -eq 1 ]; then
echo "Discord eq 1"
if [ -z "$DISCORD_HOOK" ]; then
echo "Please set DISCORD_WEBHOOK url."
else
echo "Discord gameid"
format="${DISCORD_PRINTF_STR:-%s}"
# curl -i -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data "{\"content\": \"$(printf "${format}" "${gameid}")\"}" "${DISCORD_HOOK}"
# Monitor server logs for player join/leave, server start, and server stop
tail -f CoreKeeperServerLog.txt | while read LOGLINE
do
# Add timestamp to each log line
echo "$(date '+%Y-%m-%d %H:%M:%S') - $LOGLINE"
# Detect player join based on log: [userid:12345] is using new name PlayerName
if echo "$LOGLINE" | grep -q "is using new name"; then
PLAYER_NAME=$(echo "$LOGLINE" | grep -oP "is using new name \K\w+")
echo "Player Name: $PLAYER_NAME" # Debugging: ensure player name is correct
if [ -n "$DISCORD_MESSAGE_WELCOME" ]; then # Only send if DISCORD_MESSAGE_WELCOME is set
WELCOME_MSG=$(echo "$DISCORD_MESSAGE_WELCOME" | sed "s/\$\$user/$PLAYER_NAME/g")
echo "Generated Welcome Message: $WELCOME_MSG" # Debugging: ensure message is correct
# Check if WELCOME_MSG is empty before sending
if [ -z "$WELCOME_MSG" ]; then
echo "Error: Welcome message is empty"
else
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST --data "{\"content\": \"$(printf "${format}" "${WELCOME_MSG}")\"}" "${DISCORD_HOOK}"
fi
else
echo "DISCORD_MESSAGE_WELCOME not set. Skipping Discord notification."
fi
fi
# Detect potential player leave
if echo "$LOGLINE" | grep -q "Accepted connection from .* with result OK awaiting authentication"; then
PLAYER_NAME=$(echo "$LOGLINE" | grep -oP "Connected to userid:.*")
if [ -n "$DISCORD_MESSAGE_BYE" ]; then # Only send if DISCORD_MESSAGE_BYE is set
BYE_MSG=$(echo "$DISCORD_MESSAGE_BYE" | sed "s/\$\$user/$PLAYER_NAME/g")
echo "Generated Bye Message: $BYE_MSG" # Debugging: ensure message is correct
# Check if BYE_MSG is empty before sending
if [ -z "$BYE_MSG" ]; then
echo "Error: Bye message is empty"
else
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST --data "{\"content\": \"$(printf "${format}" "${BYE_MSG}")\"}" "${DISCORD_HOOK}"
fi
else
echo "DISCORD_MESSAGE_BYE not set. Skipping Discord notification."
fi
fi
# Detect server start message: Started session with Game ID
if echo "$LOGLINE" | grep -q "Started session with Game ID"; then
echo "Server start detected" # Debugging: detect start request
if [ -n "$DISCORD_MESSAGE_START" ]; then # Only send if DISCORD_MESSAGE_START is set
START_MSG=$(echo "$DISCORD_MESSAGE_START" | sed "s/\$\$WORLD_NAME/$WORLD_NAME/g" | sed "s/\$\$gameid/$gameid/g")
echo "Generated Start Message: $START_MSG" # Debugging: ensure start message is correct
# Check if START_MSG is empty before sending
if [ -z "$START_MSG" ]; then
echo "Error: Start message is empty"
else
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST --data "{\"content\": \"$(printf "${format}" "${START_MSG}")\"}" "${DISCORD_HOOK}"
fi
else
echo "DISCORD_MESSAGE_START not set. Skipping Discord notification."
fi
fi
# Detect server stop message: Got quit request
if echo "$LOGLINE" | grep -q "Got quit request"; then
echo "Server stop detected" # Debugging: detect quit request
if [ -n "$DISCORD_MESSAGE_STOP" ]; then # Only send if DISCORD_MESSAGE_STOP is set
STOP_MSG=$(echo "$DISCORD_MESSAGE_STOP" | sed "s/\$\$WORLD_NAME/$WORLD_NAME/g")
echo "Generated Stop Message: $STOP_MSG" # Debugging: ensure stop message is correct
# Check if STOP_MSG is empty before sending
if [ -z "$STOP_MSG" ]; then
echo "Error: Stop message is empty"
else
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST --data "{\"content\": \"$(printf "${format}" "${STOP_MSG}")\"}" "${DISCORD_HOOK}"
fi
else
echo "DISCORD_MESSAGE_STOP not set. Skipping Discord notification."
fi
fi
done
fi
fi
wait $ckpid
ckpid=""