Changeset 19 for scripts/usr/local/lib/7dtd/common.sh
- Timestamp:
- May 23, 2014, 6:39:58 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
scripts/usr/local/lib/7dtd/common.sh
r17 r19 22 22 } 23 23 24 # Check if the given instance name is valid (no blanks, no special chars, 25 # only letters, digits, underscore, hyphen -> [A-Za-z0-9_\-]) 26 # Params: 27 # 1: Instance name 28 # Returns: 29 # 0/1 instance not valid/valid 30 isValidInstanceName() { 31 if [[ "$1" =~ ^[A-Za-z0-9_\-]+$ ]]; then 32 echo 1 33 else 34 echo 0 35 fi 36 } 37 24 38 # Check if the given instance name is an existing instance 25 39 # Params: … … 28 42 # 0/1 instance not valid/valid 29 43 isValidInstance() { 30 if [ -z $1 ]; then 31 echo 0 32 else 33 if [ ! -d $(getInstancePath $1) ]; then 34 echo 0 35 else 36 if [ ! -f $(getInstancePath $1)/config.xml ]; then 37 echo 0 38 else 39 echo 1 40 fi 41 fi 42 fi 44 if [ ! -z "$1" ]; then 45 if [ $(isValidInstanceName "$1") -eq 1 ]; then 46 if [ -d $(getInstancePath "$1") ]; then 47 if [ -f $(getInstancePath "$1")/config.xml ]; then 48 echo 1 49 return 50 fi 51 fi 52 fi 53 fi 54 echo 0 43 55 } 44 56 … … 62 74 # List of instances 63 75 getInstanceList() { 76 local IF 64 77 for IF in $SDTD_BASE/instances/*; do 65 I=`basename $IF`78 local I=`basename $IF` 66 79 if [ $(isValidInstance $I) -eq 1 ]; then 67 80 echo $I … … 84 97 } 85 98 86 # Get a single value from a serverconfig87 # Params:88 # 1: Instance name89 # 2: Property name90 # Returns:91 # Property value92 getConfigValue() {93 CONF=$(getInstancePath $1)/config.xml94 $XMLSTARLET sel -t -v "/ServerSettings/property[@name='$2']/@value" $CONF95 }96 97 # Update a single value in a serverconfig98 # Params:99 # 1: Instance name100 # 2: Property name101 # 3: New value102 setConfigValue() {103 CONF=$(getInstancePath $1)/config.xml104 $XMLSTARLET ed -L -u "/ServerSettings/property[@name='$2']/@value" -v "$3" $CONF105 }106 107 99 # Check if a given port range (baseport, baseport+1, baseport+2 each udp) 108 # is already in use by any instance100 # is already in use by any other instance 109 101 # Params: 110 102 # 1: Baseport 103 # 2: Current instance (ignored) 111 104 # Returns: 112 105 # 0/1 not in use/in use 113 106 checkGamePortUsed() { 114 PORTMIN=$1 115 PORTMAX=$(( $1 + 2 )) 107 local PORTMIN=$1 108 local PORTMAX=$(( $1 + 2 )) 109 local I 116 110 for I in $(getInstanceList); do 117 CURPORTMIN=$(getConfigValue $I "ServerPort") 118 CURPORTMAX=$(( $CURPORTMIN + 2 )) 119 if [ $PORTMAX -ge $CURPORTMIN -a $PORTMIN -le $CURPORTMAX ]; then 120 echo 1 121 return 111 if [ "$2" != "$I" ]; then 112 local CURPORTMIN=$(getConfigValue $I "ServerPort") 113 local CURPORTMAX=$(( $CURPORTMIN + 2 )) 114 if [ $PORTMAX -ge $CURPORTMIN -a $PORTMIN -le $CURPORTMAX ]; then 115 echo 1 116 return 117 fi 122 118 fi 123 119 done … … 125 121 } 126 122 127 # Check if a given telnet port is already in use by any instance 123 # Check if a given TCP port is already in use by any instance (either by control 124 # panel or telnet) 128 125 # Params: 129 126 # 1: Port 130 127 # Returns: 131 128 # 0/1 not in use/in use 132 checkTelnetPortUsed() { 129 checkTCPPortUsed() { 130 local I 133 131 for I in $(getInstanceList); do 134 CURENABLED=$(getConfigValue $I "TelnetEnabled") 135 CURPORT=$(getConfigValue $I "TelnetPort") 136 if [ "$CURENABLED" = "true" -a $CURPORT -eq $1 ]; then 137 echo 1 138 return 132 if [ "$2" != "$I" ]; then 133 local CURENABLED=$(getConfigValue $I "TelnetEnabled") 134 local CURPORT=$(getConfigValue $I "TelnetPort") 135 if [ "$CURENABLED" = "true" -a $CURPORT -eq $1 ]; then 136 echo 1 137 return 138 fi 139 CURENABLED=$(getConfigValue $I "ControlPanelEnabled") 140 CURPORT=$(getConfigValue $I "ControlPanelPort") 141 if [ "$CURENABLED" = "true" -a $CURPORT -eq $1 ]; then 142 echo 1 143 return 144 fi 139 145 fi 140 146 done … … 149 155 # String of telnet output 150 156 telnetCommand() { 151 TEL_ENABLED=$(getConfigValue $1 TelnetEnabled)152 TEL_PORT=$(getConfigValue $1 TelnetPort)153 TEL_PASS=$(getConfigValue $1 TelnetPassword)157 local TEL_ENABLED=$(getConfigValue $1 TelnetEnabled) 158 local TEL_PORT=$(getConfigValue $1 TelnetPort) 159 local TEL_PASS=$(getConfigValue $1 TelnetPassword) 154 160 if [ "$TEL_ENABLED" = "true" ] && [ -n "$TEL_PASS" ]; then 155 161 echo -e "$TEL_PASS\n$2\nexit" | nc -q 2 127.0.0.1 $TEL_PORT … … 166 172 getHooksFor() { 167 173 if [ -d $SDTD_BASE/hooks/$1 ]; then 174 local H 168 175 for H in $SDTD_BASE/hooks/$1/*.sh; do 169 176 echo "$H" … … 191 198 } 192 199 200 # Check if given value is a (integer) number 201 # Params: 202 # 1: Value 203 # Returns: 204 # 0/1 for NaN / is a number 205 isANumber() { 206 if [[ $1 =~ ^[0-9]+$ ]] ; then 207 echo "1" 208 else 209 echo "0" 210 fi 211 } 212 213 # Check if given value is a boolean (true/false, yes/no, y/n) 214 # Params: 215 # 1: Value 216 # Returns: 217 # 0/1 218 isABool() { 219 local LOW=$(lowercase "$1") 220 if [ "$LOW" = "false" -o "$LOW" = "true"\ 221 -o "$LOW" = "yes" -o "$LOW" = "y"\ 222 -o "$LOW" = "no" -o "$LOW" = "n" ]; then 223 echo 1 224 else 225 echo 0 226 fi 227 } 228 229 # Convert the given value to a boolean 0/1 230 # Params: 231 # 1: Value 232 # Returns: 233 # 0/1 as false/true 234 getBool() { 235 if [ $(isABool "$1") -eq 0 ]; then 236 echo 0 237 else 238 local LOW=$(lowercase "$1") 239 if [ "$LOW" = "true" -o "$LOW" = "yes" -o "$LOW" = "y" ]; then 240 echo 1 241 else 242 echo 0 243 fi 244 fi 245 } 246 193 247 listCommands() { 248 local C 194 249 for C in $(declare -F | cut -d\ -f3 | grep "^sdtdCommand"\ 195 250 | grep -v "Help$"\ 196 251 | grep -v "Description$"\ 197 252 | grep -v "Expects$"); do 198 CMD=$(lowercase "${C#sdtdCommand}")253 local CMD=$(lowercase "${C#sdtdCommand}") 199 254 printf "%s " "$CMD" 200 255 done … … 203 258 . /usr/local/lib/7dtd/help.sh 204 259 . /usr/local/lib/7dtd/playerlog.sh 260 . /usr/local/lib/7dtd/serverconfig.sh 205 261 for M in /usr/local/lib/7dtd/commands/*.sh; do 206 262 . $M
Note:
See TracChangeset
for help on using the changeset viewer.