-
Notifications
You must be signed in to change notification settings - Fork 2
/
start_tem.sh
executable file
·251 lines (231 loc) · 6.85 KB
/
start_tem.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env bash
shopt -s extglob # used for trailing slashes globbing
echo "========================"
echo "|| Teeworlds Econ Mod ||"
echo "|| by ChillerDragon ||"
echo "========================"
function log() {
echo "[TEM] $1"
}
function err() {
echo "[TEM:ERROR] $1"
}
# check dependencys
command -v expect >/dev/null 2>&1 || {
echo >&2 "Error: expect is not found please install it!";
if [ "$(uname)" == "Darwin" ]; then
echo >&2 "MacOS: brew install expect";
elif [ -x "$(command -v apt)" ]; then
echo >&2 "Debian/Ubuntu: sudo apt install expect";
fi
exit 1;
}
command -v nc >/dev/null 2>&1 || {
echo >&2 "Error: netcat is not found please install it!";
if [ "$(uname)" == "Darwin" ]; then
echo >&2 "MacOS: brew install netcat";
elif [ -x "$(command -v apt)" ]; then
echo >&2 "Debian/Ubuntu: sudo apt install netcat";
fi
exit 1;
}
# shellcheck disable=1091
[ -f venv/bin/activate ] && source ./venv/bin/activate
# init variables
settings_file="tem.settings"
aSettStr=();aSettVal=()
aSettStr+=("sh_tw_path");aSettVal+=("/path/to/your/teeworlds/directory")
aSettStr+=("sh_tw_binary");aSettVal+=("name_of_teeworlds_srv")
aSettStr+=("sh_econ_password");aSettVal+=("password")
aSettStr+=("sh_econ_port");aSettVal+=("8203")
aSettStr+=("sh_logs_path");aSettVal+=("/path/to/log/directory")
aSettStr+=("sh_tw_cfg_file");aSettVal+=("")
aSettStr+=("sh_tw_version");aSettVal+=("6")
aSettStr+=("sh_discord_token_verbose");aSettVal+=("")
aSettStr+=("sh_econ_addr");aSettVal+=("localhost")
if [ $# -gt 0 ]; then
log "settings file=$1"
settings_file=$1
fi
function check_path() {
local path=$1
local warning=$2
local create=$3
if [ -d "$path" ]
then
return # path found nothing todo
fi
if [ -d "$HOME/.teeworlds/dumps/$path" ]
then
# https://github.com/teeworlds/teeworlds/commit/c705f048f3f62e0ed92686e19763a61309125d98
# new logger system
# should also support other storage.cfg locations
# not too sure about how that actually should be supported
# only logs are under dumps/ so hardcoding dumps/ is not good
# TODO: improve this
log "Warning: using path $HOME/.teeworlds/dumps/$path"
return
fi
log "$warning"
log "should be at: $path"
if [ "$create" != "0" ]
then
read -p "Do you want to create the path? [y/N]" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
mkdir -p "$path"
log "created folder at: $path"
return
fi
fi
log "PathError: please provide a valid path."
exit 1
}
function create_settings() {
if [ -f "$settings_file" ];
then
return
fi
local i
log "FileError: '$settings_file' not found"
read -p "Do you want to create one? [y/N]" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
{
echo "# TeeworldsEconMod (TEM) by ChillerDragon"
echo "# https://github.com/chillavanilla/TeeworldsEconMod"
for i in "${!aSettStr[@]}"
do
echo "${aSettStr[$i]}=${aSettVal[$i]}"
done
} > "$settings_file"
if [ -z "$EDITOR" ]
then
nano "$settings_file"
else
$EDITOR "$settings_file"
fi
fi
exit 1
}
function parse_settings_line() {
local sett=$1
local val=$2
local i
for i in "${!aSettStr[@]}"
do
if [ "$sett" == "${aSettStr[$i]}" ]
then
printf "[TEM:setting] (%s)%-16s= %s\\n" "$i" "${sett:3}" "$val"
if [[ "${aSettStr[$i]}" =~ path ]]
then
val="${val%%+(/)}" # strip trailing slash
fi
aSettVal[$i]="$val"
return
fi
done
log "SettingsError: unkown setting $sett"
exit 1
}
function read_settings_file() {
local i
while read -r line
do
if [ "${line:0:1}" == "#" ]
then
continue # ignore comments
elif [ "${line:0:3}" == "py_" ]
then
continue # ignore python settings
elif [ -z "$line" ]
then
continue # ignore empty lines
fi
line_set=""
line_val=""
IFS='=' read -ra split_line <<< "$line"
for i in "${!split_line[@]}"
do
# split by '=' and then join all the elements bigger than 0
# thus we allow using '=' inside the value
if [ "$i" == "0" ]
then
line_set="${split_line[$i]}"
else
if [ "$i" -gt "1" ]
then
line_val+="="
fi
line_val+="${split_line[$i]}"
fi
done
if [ "${line: -1}" == "=" ] && [ "$(echo "$line" | grep -o "=" | wc -l)" -gt "1" ]
then
line_val+="="
fi
# echo "config: $line_set value: $line_val"
parse_settings_line "$line_set" "$line_val"
done < "$settings_file"
}
create_settings # create fresh if null
aSettVal[6]="" # overwrite defualt used for sample config by create_settings()
econ_mod_path="$(pwd)"
log "saved current path=$econ_mod_path"
log "loading settings.."
read_settings_file
# Settings:
# - teeworlds path 0
# - binary 1
# - econ_password 2
# - econ_port 3
# - logs path 4
# - cfg path 5
# - tw version 6
# - discord token 7 ( NOT USED BY THIS SCRIPT ONLY BY tools/discord_bot.sh )
# - econ_addr 8
twsettings=""
check_path "${aSettVal[0]}" "The teeworlds path is invalid" "0" # 0=dont create on fail
check_path "${aSettVal[0]}/stats" "No stats/ folder found in your teeworlds directory" "1" # 1=create on fail
if [ "${aSettVal[4]}" ]
then
check_path "${aSettVal[4]}" "The logpath is invalid" "1" # 1=create on fail
log "adding log path: ${aSettVal[4]}"
extension=".log"
if [ "${aSettVal[6]}" == "7" ] || [ "${aSettVal[6]}" == "0.7" ]
then
# 0.7 appends .txt anyways
extension=""
fi
twsettings="logfile ${aSettVal[4]}/${aSettVal[1]##*/}_$(date +%F_%H-%M-%S)$extension;"
fi
log "navigate to teeworlds path=${aSettVal[0]}"
cd "${aSettVal[0]}" || { err "invalid path '${aSettVal[0]}'"; exit 1; }
tw_settings_file=""
if [ "${aSettVal[5]}" ]
then
# TODO: properly check all storage.cfg paths
if [ ! -f "${aSettVal[5]}" ] && [ ! -f ~/.teeworlds/"${aSettVal[5]}" ]
then
log "Invalid config file '${aSettVal[5]}'"
exit 1
fi
log "settings path: ${aSettVal[5]}"
tw_settings_file="-f ${aSettVal[5]}"
fi
nc_os="nc"
if [ "$(uname)" == "Darwin" ]; then
nc_os="nc_macOS"
log "detected macOS"
elif [ "$(uname -s)" == "Linux" ]; then
log "detected Linux"
elif [ "$(uname -s)" == "MINGW32_NT" ]; then
log "warning MINGW support isnt guaranteed"
elif [ "$(uname -s)" == "MINGW64_NT" ]; then
log "warning MINGW support isnt guaranteed"
fi
log "run server | pipe into main.py | pipe into netcat connection: "
log "executing: ./${aSettVal[1]} \"$twsettings\" $tw_settings_file | cd $econ_mod_path;./src/main.py --settings=$settings_file | ./bin/$nc_os.exp ${aSettVal[8]} ${aSettVal[2]} ${aSettVal[3]} settings=$settings_file"
(./${aSettVal[1]} "$twsettings" $tw_settings_file) | (cd $econ_mod_path;./src/main.py --settings=$settings_file) | (cd $econ_mod_path;./bin/$nc_os.exp ${aSettVal[8]} ${aSettVal[2]} ${aSettVal[3]} settings=$settings_file)