[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
|
---|
| 21 | checkRootLoadConf() {
|
---|
| 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
|
---|
| 34 | getInstancePath() {
|
---|
| 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
|
---|
| 44 | isValidInstanceName() {
|
---|
| 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
|
---|
| 57 | isValidInstance() {
|
---|
[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
|
---|
| 77 | isRunning() {
|
---|
[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
|
---|
| 89 | getInstanceList() {
|
---|
[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
|
---|
| 105 | getInstancePID() {
|
---|
| 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
|
---|
| 117 | getLocalEngineVersion() {
|
---|
[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
|
---|
| 133 | getRemoteEngineVersion() {
|
---|
| 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
|
---|
| 150 | checkGamePortUsed() {
|
---|
[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] | 173 | checkTCPPortUsed() {
|
---|
| 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
|
---|
| 201 | telnetCommand() {
|
---|
[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
|
---|
[259] | 231 | # 2: Instance name
|
---|
[17] | 232 | # Returns:
|
---|
| 233 | # Names of hook files
|
---|
| 234 | getHooksFor() {
|
---|
[259] | 235 | if [ -n $2 ]; then
|
---|
| 236 | if [ -d $SDTD_BASE/$(getInstancePath $2)/hooks/$1 ]; then
|
---|
| 237 | local H
|
---|
| 238 | for H in $SDTD_BASE/$(getInstancePath $2)/hooks/$1/*.sh; do
|
---|
| 239 | echo "$H"
|
---|
| 240 | done
|
---|
| 241 | fi
|
---|
| 242 | fi
|
---|
[17] | 243 | if [ -d $SDTD_BASE/hooks/$1 ]; then
|
---|
[19] | 244 | local H
|
---|
[17] | 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
|
---|
| 256 | lowercase() {
|
---|
| 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
|
---|
| 266 | camelcasePrep() {
|
---|
| 267 | echo $(echo "${1:0:1}" | tr "[:lower:]" "[:upper:]")$(echo "${1:1}" | tr "[:upper:]" "[:lower:]")
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[19] | 270 | # Check if given value is a (integer) number
|
---|
| 271 | # Params:
|
---|
| 272 | # 1: Value
|
---|
| 273 | # Returns:
|
---|
| 274 | # 0/1 for NaN / is a number
|
---|
| 275 | isANumber() {
|
---|
| 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
|
---|
| 288 | isABool() {
|
---|
| 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
|
---|
| 304 | getBool() {
|
---|
| 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 |
|
---|
[17] | 317 | listCommands() {
|
---|
[19] | 318 | local C
|
---|
[17] | 319 | for C in $(declare -F | cut -d\ -f3 | grep "^sdtdCommand"\
|
---|
| 320 | | grep -v "Help$"\
|
---|
| 321 | | grep -v "Description$"\
|
---|
| 322 | | grep -v "Expects$"); do
|
---|
[19] | 323 | local CMD=$(lowercase "${C#sdtdCommand}")
|
---|
[17] | 324 | printf "%s " "$CMD"
|
---|
| 325 | done
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | . /usr/local/lib/7dtd/help.sh
|
---|
[19] | 329 | . /usr/local/lib/7dtd/serverconfig.sh
|
---|
[17] | 330 | for M in /usr/local/lib/7dtd/commands/*.sh; do
|
---|
| 331 | . $M
|
---|
| 332 | done
|
---|
| 333 |
|
---|
[64] | 334 | checkRootLoadConf
|
---|
| 335 |
|
---|