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

Last change on this file since 306 was 304, checked in by alloc, 7 years ago

108

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 disown
213 if [ -n "$TEL_PASS" ]; then
214 printf "$TEL_PASS\n$2\n" >&3
215 else
216 printf "$2\n" >&3
217 fi
218 sleep ${3:-1}
219 printf "exit\n" >&3
220 sleep 0.2
221 kill -9 $NCPID > /dev/null 2>&1
222 exec 3>&-
223 rm -f $TEMPFILE
224 else
225 echo "Telnet not enabled."
226 fi
227}
228
229# Get all hook files for the given hook-name
230# Params:
231# 1: Hook name
232# 2: Instance name
233# Returns:
234# Names of hook files
235getHooksFor() {
236 if [ -n $2 ]; then
237 if [ -d $(getInstancePath $2)/hooks/$1 ]; then
238 local H
239 for H in $(getInstancePath $2)/hooks/$1/*.sh; do
240 echo "$H"
241 done
242 fi
243 fi
244 if [ -d $SDTD_BASE/hooks/$1 ]; then
245 local H
246 for H in $SDTD_BASE/hooks/$1/*.sh; do
247 echo "$H"
248 done
249 fi
250}
251
252# Lowercase passed string
253# Params:
254# 1: String
255# Returns:
256# Lowercased string
257lowercase() {
258 echo "${1}" | tr "[:upper:]" "[:lower:]"
259}
260
261# Prepare passed string as part of camelcase, i.e. first char upper case, others
262# lowercase
263# Params:
264# 1: String
265# Returns:
266# Transformed string
267camelcasePrep() {
268 echo $(echo "${1:0:1}" | tr "[:lower:]" "[:upper:]")$(echo "${1:1}" | tr "[:upper:]" "[:lower:]")
269}
270
271# Check if given value is a (integer) number
272# Params:
273# 1: Value
274# Returns:
275# 0/1 for NaN / is a number
276isANumber() {
277 if [[ $1 =~ ^[0-9]+$ ]] ; then
278 echo "1"
279 else
280 echo "0"
281 fi
282}
283
284# Check if given value is a boolean (true/false, yes/no, y/n)
285# Params:
286# 1: Value
287# Returns:
288# 0/1
289isABool() {
290 local LOW=$(lowercase "$1")
291 if [ "$LOW" = "false" -o "$LOW" = "true"\
292 -o "$LOW" = "yes" -o "$LOW" = "y"\
293 -o "$LOW" = "no" -o "$LOW" = "n" ]; then
294 echo 1
295 else
296 echo 0
297 fi
298}
299
300# Convert the given value to a boolean 0/1
301# Params:
302# 1: Value
303# Returns:
304# 0/1 as false/true
305getBool() {
306 if [ $(isABool "$1") -eq 0 ]; then
307 echo 0
308 else
309 local LOW=$(lowercase "$1")
310 if [ "$LOW" = "true" -o "$LOW" = "yes" -o "$LOW" = "y" ]; then
311 echo 1
312 else
313 echo 0
314 fi
315 fi
316}
317
318listCommands() {
319 local C
320 for C in $(declare -F | cut -d\ -f3 | grep "^sdtdCommand"\
321 | grep -v "Help$"\
322 | grep -v "Description$"\
323 | grep -v "Expects$"); do
324 local CMD=$(lowercase "${C#sdtdCommand}")
325 printf "%s " "$CMD"
326 done
327}
328
329. /usr/local/lib/7dtd/help.sh
330. /usr/local/lib/7dtd/serverconfig.sh
331for M in /usr/local/lib/7dtd/commands/*.sh; do
332 . $M
333done
334
335checkRootLoadConf
336
Note: See TracBrowser for help on using the repository browser.