[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
|
---|
[60] | 120 | local REMOTE=$(wget -qO- http://steamdb.info/api/GetRawDepots/?appid=251570 | 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)
|
---|
| 121 |
|
---|
[53] | 122 | if [ $(isANumber "$REMOTE") -eq 0 ]; then
|
---|
| 123 | REMOTE=1
|
---|
| 124 | fi
|
---|
| 125 | echo $REMOTE
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[17] | 128 | # Check if a given port range (baseport, baseport+1, baseport+2 each udp)
|
---|
[19] | 129 | # is already in use by any other instance
|
---|
[17] | 130 | # Params:
|
---|
| 131 | # 1: Baseport
|
---|
[19] | 132 | # 2: Current instance (ignored)
|
---|
[17] | 133 | # Returns:
|
---|
| 134 | # 0/1 not in use/in use
|
---|
| 135 | checkGamePortUsed() {
|
---|
[19] | 136 | local PORTMIN=$1
|
---|
| 137 | local PORTMAX=$(( $1 + 2 ))
|
---|
| 138 | local I
|
---|
[17] | 139 | for I in $(getInstanceList); do
|
---|
[19] | 140 | if [ "$2" != "$I" ]; then
|
---|
| 141 | local CURPORTMIN=$(getConfigValue $I "ServerPort")
|
---|
| 142 | local CURPORTMAX=$(( $CURPORTMIN + 2 ))
|
---|
| 143 | if [ $PORTMAX -ge $CURPORTMIN -a $PORTMIN -le $CURPORTMAX ]; then
|
---|
| 144 | echo 1
|
---|
| 145 | return
|
---|
| 146 | fi
|
---|
[17] | 147 | fi
|
---|
| 148 | done
|
---|
| 149 | echo 0
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[19] | 152 | # Check if a given TCP port is already in use by any instance (either by control
|
---|
| 153 | # panel or telnet)
|
---|
[17] | 154 | # Params:
|
---|
| 155 | # 1: Port
|
---|
| 156 | # Returns:
|
---|
| 157 | # 0/1 not in use/in use
|
---|
[19] | 158 | checkTCPPortUsed() {
|
---|
| 159 | local I
|
---|
[17] | 160 | for I in $(getInstanceList); do
|
---|
[19] | 161 | if [ "$2" != "$I" ]; then
|
---|
| 162 | local CURENABLED=$(getConfigValue $I "TelnetEnabled")
|
---|
| 163 | local CURPORT=$(getConfigValue $I "TelnetPort")
|
---|
| 164 | if [ "$CURENABLED" = "true" -a $CURPORT -eq $1 ]; then
|
---|
| 165 | echo 1
|
---|
| 166 | return
|
---|
| 167 | fi
|
---|
| 168 | CURENABLED=$(getConfigValue $I "ControlPanelEnabled")
|
---|
| 169 | CURPORT=$(getConfigValue $I "ControlPanelPort")
|
---|
| 170 | if [ "$CURENABLED" = "true" -a $CURPORT -eq $1 ]; then
|
---|
| 171 | echo 1
|
---|
| 172 | return
|
---|
| 173 | fi
|
---|
[17] | 174 | fi
|
---|
| 175 | done
|
---|
| 176 | echo 0
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | # Send a single command to the telnet port
|
---|
| 180 | # Params:
|
---|
| 181 | # 1: Instance name
|
---|
| 182 | # 2: Command
|
---|
[46] | 183 | # 3: (Optional) Timeout in sec, defaulting to 1
|
---|
[17] | 184 | # Returns:
|
---|
| 185 | # String of telnet output
|
---|
| 186 | telnetCommand() {
|
---|
[19] | 187 | local TEL_ENABLED=$(getConfigValue $1 TelnetEnabled)
|
---|
| 188 | local TEL_PORT=$(getConfigValue $1 TelnetPort)
|
---|
| 189 | local TEL_PASS=$(getConfigValue $1 TelnetPassword)
|
---|
[17] | 190 | if [ "$TEL_ENABLED" = "true" ] && [ -n "$TEL_PASS" ]; then
|
---|
[46] | 191 | local TEMPFILE=$(mktemp)
|
---|
| 192 | rm -f $TEMPFILE
|
---|
| 193 | mkfifo $TEMPFILE
|
---|
| 194 | exec 3<> $TEMPFILE
|
---|
| 195 | nc 127.0.0.1 $TEL_PORT <&3 &
|
---|
| 196 | local NCPID=$!
|
---|
| 197 | printf "$TEL_PASS\n$2\n" >&3
|
---|
| 198 | sleep ${3:-1}
|
---|
| 199 | printf "exit\n" >&3
|
---|
| 200 | sleep 0.2
|
---|
| 201 | kill -9 $NCPID > /dev/null 2>&1
|
---|
| 202 | exec 3>&-
|
---|
| 203 | rm -f $TEMPFILE
|
---|
[17] | 204 | else
|
---|
| 205 | echo "Telnet not enabled or no password set."
|
---|
| 206 | fi
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | # Get all hook files for the given hook-name
|
---|
| 210 | # Params:
|
---|
| 211 | # 1: Hook name
|
---|
| 212 | # Returns:
|
---|
| 213 | # Names of hook files
|
---|
| 214 | getHooksFor() {
|
---|
| 215 | if [ -d $SDTD_BASE/hooks/$1 ]; then
|
---|
[19] | 216 | local H
|
---|
[17] | 217 | for H in $SDTD_BASE/hooks/$1/*.sh; do
|
---|
| 218 | echo "$H"
|
---|
| 219 | done
|
---|
| 220 | fi
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | # Lowercase passed string
|
---|
| 224 | # Params:
|
---|
| 225 | # 1: String
|
---|
| 226 | # Returns:
|
---|
| 227 | # Lowercased string
|
---|
| 228 | lowercase() {
|
---|
| 229 | echo "${1}" | tr "[:upper:]" "[:lower:]"
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | # Prepare passed string as part of camelcase, i.e. first char upper case, others
|
---|
| 233 | # lowercase
|
---|
| 234 | # Params:
|
---|
| 235 | # 1: String
|
---|
| 236 | # Returns:
|
---|
| 237 | # Transformed string
|
---|
| 238 | camelcasePrep() {
|
---|
| 239 | echo $(echo "${1:0:1}" | tr "[:lower:]" "[:upper:]")$(echo "${1:1}" | tr "[:upper:]" "[:lower:]")
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[19] | 242 | # Check if given value is a (integer) number
|
---|
| 243 | # Params:
|
---|
| 244 | # 1: Value
|
---|
| 245 | # Returns:
|
---|
| 246 | # 0/1 for NaN / is a number
|
---|
| 247 | isANumber() {
|
---|
| 248 | if [[ $1 =~ ^[0-9]+$ ]] ; then
|
---|
| 249 | echo "1"
|
---|
| 250 | else
|
---|
| 251 | echo "0"
|
---|
| 252 | fi
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | # Check if given value is a boolean (true/false, yes/no, y/n)
|
---|
| 256 | # Params:
|
---|
| 257 | # 1: Value
|
---|
| 258 | # Returns:
|
---|
| 259 | # 0/1
|
---|
| 260 | isABool() {
|
---|
| 261 | local LOW=$(lowercase "$1")
|
---|
| 262 | if [ "$LOW" = "false" -o "$LOW" = "true"\
|
---|
| 263 | -o "$LOW" = "yes" -o "$LOW" = "y"\
|
---|
| 264 | -o "$LOW" = "no" -o "$LOW" = "n" ]; then
|
---|
| 265 | echo 1
|
---|
| 266 | else
|
---|
| 267 | echo 0
|
---|
| 268 | fi
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | # Convert the given value to a boolean 0/1
|
---|
| 272 | # Params:
|
---|
| 273 | # 1: Value
|
---|
| 274 | # Returns:
|
---|
| 275 | # 0/1 as false/true
|
---|
| 276 | getBool() {
|
---|
| 277 | if [ $(isABool "$1") -eq 0 ]; then
|
---|
| 278 | echo 0
|
---|
| 279 | else
|
---|
| 280 | local LOW=$(lowercase "$1")
|
---|
| 281 | if [ "$LOW" = "true" -o "$LOW" = "yes" -o "$LOW" = "y" ]; then
|
---|
| 282 | echo 1
|
---|
| 283 | else
|
---|
| 284 | echo 0
|
---|
| 285 | fi
|
---|
| 286 | fi
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[17] | 289 | listCommands() {
|
---|
[19] | 290 | local C
|
---|
[17] | 291 | for C in $(declare -F | cut -d\ -f3 | grep "^sdtdCommand"\
|
---|
| 292 | | grep -v "Help$"\
|
---|
| 293 | | grep -v "Description$"\
|
---|
| 294 | | grep -v "Expects$"); do
|
---|
[19] | 295 | local CMD=$(lowercase "${C#sdtdCommand}")
|
---|
[17] | 296 | printf "%s " "$CMD"
|
---|
| 297 | done
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | . /usr/local/lib/7dtd/help.sh
|
---|
| 301 | . /usr/local/lib/7dtd/playerlog.sh
|
---|
[19] | 302 | . /usr/local/lib/7dtd/serverconfig.sh
|
---|
[17] | 303 | for M in /usr/local/lib/7dtd/commands/*.sh; do
|
---|
| 304 | . $M
|
---|
| 305 | done
|
---|
| 306 |
|
---|
[64] | 307 | checkRootLoadConf
|
---|
| 308 |
|
---|