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

Last change on this file since 478 was 478, checked in by alloc, 17 months ago

Scripts updates

File size: 9.8 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 installed branch name
114# Returns:
115# "public" if no engine installed or no appmanifest found or buildid could not be read
116# Branch name
117getLocalBranch() {
118 local LOCAL="public"
119 if [ -d "$SDTD_BASE/engine" ]; then
120 local APPMANIFEST=$(find $SDTD_BASE/engine -type f -name "appmanifest_294420.acf")
121 if [ -f "$APPMANIFEST" ]; then
122 LOCAL=$(grep betakey "$APPMANIFEST" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d\ -f3)
123 if [[ -z $LOCAL ]]; then
124 LOCAL="public"
125 else
126 echo $LOCAL
127 return
128 fi
129 fi
130 fi
131 echo $LOCAL
132}
133
134# Get the local engine version number (i.e. build id)
135# Returns:
136# 0 if no engine installed or no appmanifest found or buildid could not be read
137# Build Id otherwise
138getLocalEngineVersion() {
139 local LOCAL=0
140 if [ -d "$SDTD_BASE/engine" ]; then
141 local APPMANIFEST=$(find $SDTD_BASE/engine -type f -name "appmanifest_294420.acf")
142 if [ -f "$APPMANIFEST" ]; then
143 LOCAL=$(grep buildid "$APPMANIFEST" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d\ -f3)
144 if [ $(isANumber "$LOCAL") -eq 0 ]; then
145 LOCAL=0
146 fi
147 fi
148 fi
149 echo $LOCAL
150}
151
152# Get the local engine update time
153# Returns:
154# 0 if no engine installed or no appmanifest found or buildid could not be read
155# Update time otherwise
156getLocalEngineUpdateTime() {
157 local LOCAL=0
158 if [ -d "$SDTD_BASE/engine" ]; then
159 local APPMANIFEST=$(find $SDTD_BASE/engine -type f -name "appmanifest_294420.acf")
160 if [ -f "$APPMANIFEST" ]; then
161 LOCAL=$(grep LastUpdated "$APPMANIFEST" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d\ -f3)
162 if [ $(isANumber "$LOCAL") -eq 0 ]; then
163 LOCAL=0
164 else
165 date --date="@${LOCAL}" "+%Y-%m-%d %H:%M:%S"
166 return
167 fi
168 fi
169 fi
170 echo $LOCAL
171}
172
173
174# Get the latest remote (on Steam) engine version numbers etc
175updateRemoteEngineInfo() {
176 local DOCHECK=no
177 if [ ! -e /tmp/7dtd-appinfo ]; then
178 DOCHECK=yes
179 else
180 AGE=$((`date +%s` - `stat -L --format %Y /tmp/7dtd-appinfo`))
181 if [ $AGE -gt 300 ]; then
182 DOCHECK=yes
183 fi
184 fi
185 if [ "$DOCHECK" = "yes" ]; then
186 echo "Updating version information..."
187 rm /root/Steam/appcache/appinfo.vdf
188 cd $SDTD_BASE/steamcmd
189
190 expect /usr/local/lib/7dtd/steamcmd.exp $SDTD_BASE/steamcmd/steamcmd.sh 294420 2>/dev/null > /tmp/7dtd-appinfo
191
192 local BUILDID=$(getBuildId public)
193
194 if [ $(isANumber "$BUILDID") -eq 0 ]; then
195 rm -f /tmp/7dtd-appinfo
196 fi
197 fi
198}
199
200# Get the latest build id (on Steam)
201# Params:
202# 1. Branch name
203# Returns:
204# "?" if data could not be retrieved
205# BuildId otherwise
206getRemoteBuildId() {
207 local BUILDID=$(grep -A 1000 \"branches\" /tmp/7dtd-appinfo | grep -A 1000 \"$1\" | grep -B 10 \} --max-count=1 | grep \"buildid\" | cut -d\" -f4)
208
209 if [ $(isANumber "$BUILDID") -eq 0 ]; then
210 echo "?"
211 else
212 echo $BUILDID
213 fi
214}
215
216# Get the update time of the latest build (on Steam)
217# Params:
218# 1. Branch name
219# Returns:
220# "?" if data could not be retrieved
221# Update timestamp otherwise
222getRemoteBuildUpdateTime() {
223 local TIMESTAMP=$(grep -A 1000 \"branches\" /tmp/7dtd-appinfo | grep -A 1000 \"$1\" | grep -B 10 \} --max-count=1 | grep \"timeupdated\" | cut -d\" -f4)
224
225 if [ $(isANumber "$TIMESTAMP") -eq 0 ]; then
226 echo "?"
227 else
228 date --date="@${TIMESTAMP}" "+%Y-%m-%d %H:%M:%S"
229 fi
230}
231
232# Get a list of available branch names, blank separated
233# Returns:
234# Blank separated list of branch names (can be empty if an error occured)
235getBranchNames() {
236 grep -A 1000 \"branches\" /tmp/7dtd-appinfo | grep -E '^[[:space:]]*"[^"]+"[[:space:]]*$' | tail --lines=+2 | cut -d\" -f2
237}
238
239# Check if a given port range (baseport, baseport+1, baseport+2 each udp)
240# is already in use by any other instance
241# Params:
242# 1: Baseport
243# 2: Current instance (ignored)
244# Returns:
245# 0/1 not in use/in use
246checkGamePortUsed() {
247 local PORTMIN=$1
248 local PORTMAX=$(( $1 + 2 ))
249 local I
250 for I in $(getInstanceList); do
251 if [ "$2" != "$I" ]; then
252 local CURPORTMIN=$(getConfigValue $I "ServerPort")
253 local CURPORTMAX=$(( $CURPORTMIN + 2 ))
254 if [ $PORTMAX -ge $CURPORTMIN -a $PORTMIN -le $CURPORTMAX ]; then
255 echo 1
256 return
257 fi
258 fi
259 done
260 echo 0
261}
262
263# Check if a given TCP port is already in use by any instance (either by control
264# panel or telnet)
265# Params:
266# 1: Port
267# Returns:
268# 0/1 not in use/in use
269checkTCPPortUsed() {
270 local I
271 for I in $(getInstanceList); do
272 if [ "$2" != "$I" ]; then
273 local CURENABLED=$(getConfigValue $I "TelnetEnabled")
274 local CURPORT=$(getConfigValue $I "TelnetPort")
275 if [ "$CURENABLED" = "true" -a $CURPORT -eq $1 ]; then
276 echo 1
277 return
278 fi
279 CURENABLED=$(getConfigValue $I "ControlPanelEnabled")
280 CURPORT=$(getConfigValue $I "ControlPanelPort")
281 if [ "$CURENABLED" = "true" -a $CURPORT -eq $1 ]; then
282 echo 1
283 return
284 fi
285 fi
286 done
287 echo 0
288}
289
290# Send a single command to the telnet port
291# Params:
292# 1: Instance name
293# 2: Command
294# 3: (Optional) Timeout in sec, defaulting to 1
295# Returns:
296# String of telnet output
297telnetCommand() {
298 local TEL_ENABLED=$(getConfigValue $1 TelnetEnabled)
299 local TEL_PORT=$(getConfigValue $1 TelnetPort)
300 local TEL_PASS=$(getConfigValue $1 TelnetPassword)
301 if [ "$TEL_ENABLED" = "true" ]; then
302 local TEMPFILE=$(mktemp)
303 rm -f $TEMPFILE
304 mkfifo $TEMPFILE
305 exec 3<> $TEMPFILE
306 nc 127.0.0.1 $TEL_PORT <&3 &
307 local NCPID=$!
308 disown
309 if [ -n "$TEL_PASS" ]; then
310 printf "$TEL_PASS\n$2\n" >&3
311 else
312 printf "$2\n" >&3
313 fi
314 sleep ${3:-1}
315 printf "exit\n" >&3
316 sleep 0.2
317 kill -9 $NCPID > /dev/null 2>&1
318 exec 3>&-
319 rm -f $TEMPFILE
320 else
321 echo "Telnet not enabled."
322 fi
323}
324
325# Get all hook files for the given hook-name
326# Params:
327# 1: Hook name
328# 2: Instance name
329# Returns:
330# Names of hook files
331getHooksFor() {
332 if [ -n "$2" ]; then
333 if [ -d $(getInstancePath $2)/hooks/$1 ]; then
334 local H
335 for H in $(getInstancePath $2)/hooks/$1/*.sh; do
336 echo "$H"
337 done
338 fi
339 fi
340 if [ -d $SDTD_BASE/hooks/$1 ]; then
341 local H
342 for H in $SDTD_BASE/hooks/$1/*.sh; do
343 echo "$H"
344 done
345 fi
346}
347
348# Lowercase passed string
349# Params:
350# 1: String
351# Returns:
352# Lowercased string
353lowercase() {
354 echo "${1}" | tr "[:upper:]" "[:lower:]"
355}
356
357# Prepare passed string as part of camelcase, i.e. first char upper case, others
358# lowercase
359# Params:
360# 1: String
361# Returns:
362# Transformed string
363camelcasePrep() {
364 echo $(echo "${1:0:1}" | tr "[:lower:]" "[:upper:]")$(echo "${1:1}" | tr "[:upper:]" "[:lower:]")
365}
366
367# Check if given value is a (integer) number
368# Params:
369# 1: Value
370# Returns:
371# 0/1 for NaN / is a number
372isANumber() {
373 if [[ $1 =~ ^[0-9]+$ ]] ; then
374 echo "1"
375 else
376 echo "0"
377 fi
378}
379
380# Check if given value is a boolean (true/false, yes/no, y/n)
381# Params:
382# 1: Value
383# Returns:
384# 0/1
385isABool() {
386 local LOW=$(lowercase "$1")
387 if [ "$LOW" = "false" -o "$LOW" = "true"\
388 -o "$LOW" = "yes" -o "$LOW" = "y"\
389 -o "$LOW" = "no" -o "$LOW" = "n" ]; then
390 echo 1
391 else
392 echo 0
393 fi
394}
395
396# Convert the given value to a boolean 0/1
397# Params:
398# 1: Value
399# Returns:
400# 0/1 as false/true
401getBool() {
402 if [ $(isABool "$1") -eq 0 ]; then
403 echo 0
404 else
405 local LOW=$(lowercase "$1")
406 if [ "$LOW" = "true" -o "$LOW" = "yes" -o "$LOW" = "y" ]; then
407 echo 1
408 else
409 echo 0
410 fi
411 fi
412}
413
414listCommands() {
415 local C
416 for C in $(declare -F | cut -d\ -f3 | grep "^sdtdCommand"\
417 | grep -v "Help$"\
418 | grep -v "Description$"\
419 | grep -v "Expects$"); do
420 local CMD=$(lowercase "${C#sdtdCommand}")
421 printf "%s " "$CMD"
422 done
423}
424
425. /usr/local/lib/7dtd/help.sh
426. /usr/local/lib/7dtd/serverconfig.sh
427for M in /usr/local/lib/7dtd/commands/*.sh; do
428 . $M
429done
430
431checkRootLoadConf
432
Note: See TracBrowser for help on using the repository browser.