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