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

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

Scripts: License

File size: 7.2 KB
RevLine 
[17]1#!/bin/bash
2
[258]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
[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
[19]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
[17]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() {
[19]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
[17]65 fi
66 fi
67 fi
[19]68 echo 0
[17]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() {
[25]78 $SSD --status --pidfile $(getInstancePath $1)/7dtd.pid
[17]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() {
[19]90 local IF
[17]91 for IF in $SDTD_BASE/instances/*; do
[19]92 local I=`basename $IF`
[17]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
[53]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() {
[80]118 local APPMANIFEST=$(find $SDTD_BASE/engine -type f -name "appmanifest_294420.acf")
[53]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
[80]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)
[60]136
[53]137 if [ $(isANumber "$REMOTE") -eq 0 ]; then
138 REMOTE=1
139 fi
140 echo $REMOTE
141}
142
[17]143# Check if a given port range (baseport, baseport+1, baseport+2 each udp)
[19]144# is already in use by any other instance
[17]145# Params:
146# 1: Baseport
[19]147# 2: Current instance (ignored)
[17]148# Returns:
149# 0/1 not in use/in use
150checkGamePortUsed() {
[19]151 local PORTMIN=$1
152 local PORTMAX=$(( $1 + 2 ))
153 local I
[17]154 for I in $(getInstanceList); do
[19]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
[17]162 fi
163 done
164 echo 0
165}
166
[19]167# Check if a given TCP port is already in use by any instance (either by control
168# panel or telnet)
[17]169# Params:
170# 1: Port
171# Returns:
172# 0/1 not in use/in use
[19]173checkTCPPortUsed() {
174 local I
[17]175 for I in $(getInstanceList); do
[19]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
[17]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
[46]198# 3: (Optional) Timeout in sec, defaulting to 1
[17]199# Returns:
200# String of telnet output
201telnetCommand() {
[19]202 local TEL_ENABLED=$(getConfigValue $1 TelnetEnabled)
203 local TEL_PORT=$(getConfigValue $1 TelnetPort)
204 local TEL_PASS=$(getConfigValue $1 TelnetPassword)
[88]205 if [ "$TEL_ENABLED" = "true" ]; then
[46]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=$!
[88]212 if [ -n "$TEL_PASS" ]; then
213 printf "$TEL_PASS\n$2\n" >&3
214 else
215 printf "$2\n" >&3
216 fi
[46]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
[17]223 else
[88]224 echo "Telnet not enabled."
[17]225 fi
226}
227
228# Get all hook files for the given hook-name
229# Params:
230# 1: Hook name
231# Returns:
232# Names of hook files
233getHooksFor() {
234 if [ -d $SDTD_BASE/hooks/$1 ]; then
[19]235 local H
[17]236 for H in $SDTD_BASE/hooks/$1/*.sh; do
237 echo "$H"
238 done
239 fi
240}
241
242# Lowercase passed string
243# Params:
244# 1: String
245# Returns:
246# Lowercased string
247lowercase() {
248 echo "${1}" | tr "[:upper:]" "[:lower:]"
249}
250
251# Prepare passed string as part of camelcase, i.e. first char upper case, others
252# lowercase
253# Params:
254# 1: String
255# Returns:
256# Transformed string
257camelcasePrep() {
258 echo $(echo "${1:0:1}" | tr "[:lower:]" "[:upper:]")$(echo "${1:1}" | tr "[:upper:]" "[:lower:]")
259}
260
[19]261# Check if given value is a (integer) number
262# Params:
263# 1: Value
264# Returns:
265# 0/1 for NaN / is a number
266isANumber() {
267 if [[ $1 =~ ^[0-9]+$ ]] ; then
268 echo "1"
269 else
270 echo "0"
271 fi
272}
273
274# Check if given value is a boolean (true/false, yes/no, y/n)
275# Params:
276# 1: Value
277# Returns:
278# 0/1
279isABool() {
280 local LOW=$(lowercase "$1")
281 if [ "$LOW" = "false" -o "$LOW" = "true"\
282 -o "$LOW" = "yes" -o "$LOW" = "y"\
283 -o "$LOW" = "no" -o "$LOW" = "n" ]; then
284 echo 1
285 else
286 echo 0
287 fi
288}
289
290# Convert the given value to a boolean 0/1
291# Params:
292# 1: Value
293# Returns:
294# 0/1 as false/true
295getBool() {
296 if [ $(isABool "$1") -eq 0 ]; then
297 echo 0
298 else
299 local LOW=$(lowercase "$1")
300 if [ "$LOW" = "true" -o "$LOW" = "yes" -o "$LOW" = "y" ]; then
301 echo 1
302 else
303 echo 0
304 fi
305 fi
306}
307
[17]308listCommands() {
[19]309 local C
[17]310 for C in $(declare -F | cut -d\ -f3 | grep "^sdtdCommand"\
311 | grep -v "Help$"\
312 | grep -v "Description$"\
313 | grep -v "Expects$"); do
[19]314 local CMD=$(lowercase "${C#sdtdCommand}")
[17]315 printf "%s " "$CMD"
316 done
317}
318
319. /usr/local/lib/7dtd/help.sh
[19]320. /usr/local/lib/7dtd/serverconfig.sh
[17]321for M in /usr/local/lib/7dtd/commands/*.sh; do
322 . $M
323done
324
[64]325checkRootLoadConf
326
Note: See TracBrowser for help on using the repository browser.