source: scripts/usr/local/lib/7dtd/common.sh@ 296

Last change on this file since 296 was 260, checked in by alloc, 9 years ago

v96 fix for hooks

File size: 7.4 KB
Line 
1#!/bin/bash
2
3# Copyright 2016 Christian 'Alloc' Illy
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17
18# Provides common functions for 7dtd-scripts. Not intended to be run directly.
19
20# Check if the script is run as root (exit otherwise) and load global config
21checkRootLoadConf() {
22 if [ `id -u` -ne 0 ]; then
23 echo "This script has to be run as root!"
24 exit 10
25 fi
26 . /etc/7dtd.conf
27}
28
29# Get the config path for the given instance
30# Params:
31# 1: Instance name
32# Returns:
33# Config path for instance
34getInstancePath() {
35 echo $SDTD_BASE/instances/$1
36}
37
38# Check if the given instance name is valid (no blanks, no special chars,
39# only letters, digits, underscore, hyphen -> [A-Za-z0-9_\-])
40# Params:
41# 1: Instance name
42# Returns:
43# 0/1 instance not valid/valid
44isValidInstanceName() {
45 if [[ "$1" =~ ^[A-Za-z0-9_\-]+$ ]]; then
46 echo 1
47 else
48 echo 0
49 fi
50}
51
52# Check if the given instance name is an existing instance
53# Params:
54# 1: Instance name
55# Returns:
56# 0/1 instance not valid/valid
57isValidInstance() {
58 if [ ! -z "$1" ]; then
59 if [ $(isValidInstanceName "$1") -eq 1 ]; then
60 if [ -d $(getInstancePath "$1") ]; then
61 if [ -f $(getInstancePath "$1")/config.xml ]; then
62 echo 1
63 return
64 fi
65 fi
66 fi
67 fi
68 echo 0
69}
70
71# Check if the given instance is currently running
72# Params:
73# 1: Instance name
74# Returns:
75# 0 = not running
76# 1 = running
77isRunning() {
78 $SSD --status --pidfile $(getInstancePath $1)/7dtd.pid
79 if [ $? -eq 0 ]; then
80 echo 1
81 else
82 echo 0
83 fi
84}
85
86# Get list of defined instances
87# Returns:
88# List of instances
89getInstanceList() {
90 local IF
91 for IF in $SDTD_BASE/instances/*; do
92 local I=`basename $IF`
93 if [ $(isValidInstance $I) -eq 1 ]; then
94 echo $I
95 fi
96 done
97}
98
99# Get the PID of the instance if it is running, 0 otherwise
100# Params:
101# 1: Instance name
102# Returns:
103# 0 if not running
104# PID otherwise
105getInstancePID() {
106 if [ $(isRunning $1) -eq 1 ]; then
107 cat $(getInstancePath $1)/7dtd.pid
108 else
109 echo 0
110 fi
111}
112
113# Get the local engine version number (i.e. build id)
114# Returns:
115# 0 if no engine installed or no appmanifest found or buildid could not be read
116# Build Id otherwise
117getLocalEngineVersion() {
118 local APPMANIFEST=$(find $SDTD_BASE/engine -type f -name "appmanifest_294420.acf")
119 local LOCAL=0
120 if [ -f "$APPMANIFEST" ]; then
121 LOCAL=$(grep buildid "$APPMANIFEST" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d\ -f3)
122 if [ $(isANumber "$LOCAL") -eq 0 ]; then
123 LOCAL=0
124 fi
125 fi
126 echo $LOCAL
127}
128
129# Get the latest remote engine version number (i.e. build id)
130# Returns:
131# 1 if build id could not be retrieved
132# Build Id otherwise
133getRemoteEngineVersion() {
134 cd $SDTD_BASE/steamcmd
135 local REMOTE=$(wget -qO- http://steamdb.info/api/GetRawDepots/?appid=294420 | sed 's/\\n/\n/g' | grep -EA 1000 "^\s+\[branches\]" | grep -EA 5 "^\s+\[public\]" | grep -m 1 -EB 10 "^\s+\)$" | grep -E "^\s+\[buildid\]\s+" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d\ -f4)
136
137 if [ $(isANumber "$REMOTE") -eq 0 ]; then
138 REMOTE=1
139 fi
140 echo $REMOTE
141}
142
143# Check if a given port range (baseport, baseport+1, baseport+2 each udp)
144# is already in use by any other instance
145# Params:
146# 1: Baseport
147# 2: Current instance (ignored)
148# Returns:
149# 0/1 not in use/in use
150checkGamePortUsed() {
151 local PORTMIN=$1
152 local PORTMAX=$(( $1 + 2 ))
153 local I
154 for I in $(getInstanceList); do
155 if [ "$2" != "$I" ]; then
156 local CURPORTMIN=$(getConfigValue $I "ServerPort")
157 local CURPORTMAX=$(( $CURPORTMIN + 2 ))
158 if [ $PORTMAX -ge $CURPORTMIN -a $PORTMIN -le $CURPORTMAX ]; then
159 echo 1
160 return
161 fi
162 fi
163 done
164 echo 0
165}
166
167# Check if a given TCP port is already in use by any instance (either by control
168# panel or telnet)
169# Params:
170# 1: Port
171# Returns:
172# 0/1 not in use/in use
173checkTCPPortUsed() {
174 local I
175 for I in $(getInstanceList); do
176 if [ "$2" != "$I" ]; then
177 local CURENABLED=$(getConfigValue $I "TelnetEnabled")
178 local CURPORT=$(getConfigValue $I "TelnetPort")
179 if [ "$CURENABLED" = "true" -a $CURPORT -eq $1 ]; then
180 echo 1
181 return
182 fi
183 CURENABLED=$(getConfigValue $I "ControlPanelEnabled")
184 CURPORT=$(getConfigValue $I "ControlPanelPort")
185 if [ "$CURENABLED" = "true" -a $CURPORT -eq $1 ]; then
186 echo 1
187 return
188 fi
189 fi
190 done
191 echo 0
192}
193
194# Send a single command to the telnet port
195# Params:
196# 1: Instance name
197# 2: Command
198# 3: (Optional) Timeout in sec, defaulting to 1
199# Returns:
200# String of telnet output
201telnetCommand() {
202 local TEL_ENABLED=$(getConfigValue $1 TelnetEnabled)
203 local TEL_PORT=$(getConfigValue $1 TelnetPort)
204 local TEL_PASS=$(getConfigValue $1 TelnetPassword)
205 if [ "$TEL_ENABLED" = "true" ]; then
206 local TEMPFILE=$(mktemp)
207 rm -f $TEMPFILE
208 mkfifo $TEMPFILE
209 exec 3<> $TEMPFILE
210 nc 127.0.0.1 $TEL_PORT <&3 &
211 local NCPID=$!
212 if [ -n "$TEL_PASS" ]; then
213 printf "$TEL_PASS\n$2\n" >&3
214 else
215 printf "$2\n" >&3
216 fi
217 sleep ${3:-1}
218 printf "exit\n" >&3
219 sleep 0.2
220 kill -9 $NCPID > /dev/null 2>&1
221 exec 3>&-
222 rm -f $TEMPFILE
223 else
224 echo "Telnet not enabled."
225 fi
226}
227
228# Get all hook files for the given hook-name
229# Params:
230# 1: Hook name
231# 2: Instance name
232# Returns:
233# Names of hook files
234getHooksFor() {
235 if [ -n $2 ]; then
236 if [ -d $(getInstancePath $2)/hooks/$1 ]; then
237 local H
238 for H in $(getInstancePath $2)/hooks/$1/*.sh; do
239 echo "$H"
240 done
241 fi
242 fi
243 if [ -d $SDTD_BASE/hooks/$1 ]; then
244 local H
245 for H in $SDTD_BASE/hooks/$1/*.sh; do
246 echo "$H"
247 done
248 fi
249}
250
251# Lowercase passed string
252# Params:
253# 1: String
254# Returns:
255# Lowercased string
256lowercase() {
257 echo "${1}" | tr "[:upper:]" "[:lower:]"
258}
259
260# Prepare passed string as part of camelcase, i.e. first char upper case, others
261# lowercase
262# Params:
263# 1: String
264# Returns:
265# Transformed string
266camelcasePrep() {
267 echo $(echo "${1:0:1}" | tr "[:lower:]" "[:upper:]")$(echo "${1:1}" | tr "[:upper:]" "[:lower:]")
268}
269
270# Check if given value is a (integer) number
271# Params:
272# 1: Value
273# Returns:
274# 0/1 for NaN / is a number
275isANumber() {
276 if [[ $1 =~ ^[0-9]+$ ]] ; then
277 echo "1"
278 else
279 echo "0"
280 fi
281}
282
283# Check if given value is a boolean (true/false, yes/no, y/n)
284# Params:
285# 1: Value
286# Returns:
287# 0/1
288isABool() {
289 local LOW=$(lowercase "$1")
290 if [ "$LOW" = "false" -o "$LOW" = "true"\
291 -o "$LOW" = "yes" -o "$LOW" = "y"\
292 -o "$LOW" = "no" -o "$LOW" = "n" ]; then
293 echo 1
294 else
295 echo 0
296 fi
297}
298
299# Convert the given value to a boolean 0/1
300# Params:
301# 1: Value
302# Returns:
303# 0/1 as false/true
304getBool() {
305 if [ $(isABool "$1") -eq 0 ]; then
306 echo 0
307 else
308 local LOW=$(lowercase "$1")
309 if [ "$LOW" = "true" -o "$LOW" = "yes" -o "$LOW" = "y" ]; then
310 echo 1
311 else
312 echo 0
313 fi
314 fi
315}
316
317listCommands() {
318 local C
319 for C in $(declare -F | cut -d\ -f3 | grep "^sdtdCommand"\
320 | grep -v "Help$"\
321 | grep -v "Description$"\
322 | grep -v "Expects$"); do
323 local CMD=$(lowercase "${C#sdtdCommand}")
324 printf "%s " "$CMD"
325 done
326}
327
328. /usr/local/lib/7dtd/help.sh
329. /usr/local/lib/7dtd/serverconfig.sh
330for M in /usr/local/lib/7dtd/commands/*.sh; do
331 . $M
332done
333
334checkRootLoadConf
335
Note: See TracBrowser for help on using the repository browser.