-
Notifications
You must be signed in to change notification settings - Fork 3
/
esai-helper
executable file
·412 lines (397 loc) · 16 KB
/
esai-helper
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/bin/sh
set -eu
usage() {
echo "esai-helper: Get and apply ESAI strategies.ai in levels/<level>/server.zip"
echo "Options:"
echo " -d|--dir BF2 server installation directory. Defaults to '/server/bf2'"
echo " -m|--mod Mod. E.g. 'bf2' or 'xpack'. Defaults to 'bf2'"
echo "Commands:"
echo " get [subcommand]"
echo " gamemodes <level> Get all available levels' gamemodes"
echo " maplist <level> Generate a maplist"
echo " level-strategies <level> Get a level's server.zip's ESAI strategies"
echo " If <level> if omitted, all levels' strategies are shown"
echo " level-strategiescontent <level> Get file content of level's server.zip's ESAI strategies"
echo " If <level> if omitted, all levels' strategies are shown"
echo " levels Get all available levels"
echo " mods Get all available mods"
echo " strategies Get all available ESAI strategies"
echo " apply|delete [subcommand]"
echo " default-strategy <strategy> Apply a default ESAI strategy for all levels."
echo " This is the default strategy if a level-strategy is not found for a level"
echo " level-strategy <level> <gamemode> <strategy> Apply the specified ESAI strategy to a specified gamemode"
echo " of a specified level's server.zip"
echo " -f|--file <space_delimited_file> Apply ESAI strategies based on a space-delimited file"
echo " Format per line: <level> <gamemode> <strategy>"
echo " Use '#' for comments"
echo "Examples:"
echo " esai-helper -m bf2 get gamemodes"
echo " esai-helper -m xpack get gamemodes"
echo " esai-helper -m bf2 get maplist"
echo " esai-helper -m bf2 get level-strategies"
echo " esai-helper -m bf2 get level-strategiescontent"
echo " esai-helper -m bf2 get levels"
echo " esai-helper -m bf2 get mods"
echo " esai-helper -m bf2 get strategies"
echo " esai-helper -m bf2 apply default-strategy mediumfca"
echo " esai-helper -m bf2 apply level-strategy highway_tampa gpm_cq 32 mediumfca"
echo " esai-helper -m bf2 apply -f strategies.txt"
echo " esai-helper -m bf2 delete default-strategy mediumfca"
echo " esai-helper -m bf2 delete level-strategy highway_tampa gpm_cq 32 mediumfca"
echo " esai-helper -m bf2 delete -f strategies.txt"
}
# Exit if we got no options
if [ $# -eq 0 ]; then usage; exit 1; fi
# Get some options
while test $# -gt 0; do
case "$1" in
help|-h|--help)
usage
exit 0
;;
-d|--dir)
shift
if test $# -gt 0; then
DIR="$1"
shift
fi
;;
-m|--mod)
shift
if test $# -gt 0; then
MOD="$1"
shift
fi
;;
get)
shift
if [ $# = 0 ]; then
echo "Missing subcommand"
usage
exit 1
fi
case "$1" in
gamemodes)
GET_GAMEMODES=1
shift
if [ "$#" -gt 0 ]; then
LEVEL="${1:-}"
shift
fi
break
;;
maplist)
GET_MAPLIST=1
shift
if [ "$#" -gt 0 ]; then
LEVEL="${1:-}"
shift
fi
;;
level-strategies)
GET_LEVEL_STRATEGIES=1
shift
if [ "$#" -gt 0 ]; then
LEVEL="${1:-}"
shift
fi
;;
level-strategiescontent)
GET_LEVEL_STRATEGIESCONTENT=1
shift
if [ "$#" -gt 0 ]; then
LEVEL="${1:-}"
shift
fi
;;
levels)
GET_LEVELS=1
shift
;;
mods)
GET_MODS=1
shift
;;
strategies)
GET_STRATEGIES=1
shift
;;
esac
;;
apply|delete)
SUBCOMMAND="$1"
shift
if [ "$#" = 0 ]; then
echo "Missing options or arguments"
usage
exit 1
fi
case "$1" in
default-strategy)
if [ "$SUBCOMMAND" = 'apply' ]; then
APPLY_DEFAULT_STRATEGY=1
else
DELETE_DEFAULT_STRATEGY=1
fi
shift
STRATEGY="${1:-}"
if [ -z "$STRATEGY" ]; then
echo "Please specify an ESAI strategy an argument. E.g. mediumfcapb"
exit 1
fi
shift
;;
level-strategy)
if [ "$SUBCOMMAND" = 'apply' ]; then
APPLY_LEVEL_STRATEGY=1
else
DELETE_LEVEL_STRATEGY=1
fi
shift
LEVEL="${1:-}"
if [ -z "$LEVEL" ]; then
echo "Please specify a level as the first argument. E.g. dalian_plant"
exit 1
fi
shift
GAMEMODE="${1:-}"
if [ -z "$GAMEMODE" ]; then
echo "Please specify a gamemode as the second argument. E.g. gpm_coop"
exit 1
fi
shift
SIZE="${1:-}"
if [ -z "$SIZE" ]; then
echo "Please specify a map size as the third argument. E.g. 16"
exit 1
fi
shift
STRATEGY="${1:-}"
if [ -z "$STRATEGY" ]; then
echo "Please specify an ESAI strategy as the fourth argument. E.g. mediumfcapb"
exit 1
fi
shift
;;
-f|--file)
if [ "$SUBCOMMAND" = 'apply' ]; then
APPLY_FILE=1
else
DELETE_FILE=1
fi
shift
if [ "$#" -gt 0 ]; then
FILE="$1"
shift
fi
;;
*)
echo "Invalid option '$1'" 1>&2
usage
exit 1
;;
esac
;;
*)
echo "Invalid option '$1'" 1>&2
usage
exit 1
;;
esac
done
DIR=${DIR:-/server/bf2}
MOD=${MOD:-bf2} # Default mod is mods/bf2
GET_GAMEMODES=${GET_GAMEMODES:-}
GET_MAPLIST=${GET_MAPLIST:-}
GET_LEVEL_STRATEGIES=${GET_LEVEL_STRATEGIES:-}
GET_LEVEL_STRATEGIESCONTENT=${GET_LEVEL_STRATEGIESCONTENT:-}
GET_LEVELS=${GET_LEVELS:-}
GET_MODS=${GET_MODS:-}
GET_STRATEGIES=${GET_STRATEGIES:-}
APPLY_DEFAULT_STRATEGY=${APPLY_DEFAULT_STRATEGY:-}
DELETE_DEFAULT_STRATEGY=${DELETE_DEFAULT_STRATEGY:-}
APPLY_LEVEL_STRATEGY=${APPLY_LEVEL_STRATEGY:-}
DELETE_LEVEL_STRATEGY=${DELETE_LEVEL_STRATEGY:-}
APPLY_FILE=${APPLY_FILE:-}
DELETE_FILE=${DELETE_FILE:-}
LEVEL=${LEVEL:-}
GAMEMODE=${GAMEMODE:-}
SIZE=${SIZE:-}
STRATEGY=${STRATEGY:-}
# Validation
if ! cd "$DIR" 2>/dev/null; then
echo "No such directory: $DIR"
exit 1
fi
DIR=$( cd "$DIR" && pwd )
if [ ! -d "$DIR/mods/$MOD" ]; then
echo "Invalid mod. Mod directory does not exist: $DIR/mods/$MOD"
exit 1
fi
if [ -n "$GET_GAMEMODES" ]; then
SERVER_ZIPS=$( find "$DIR" -path "*/mods/$MOD/levels/${LEVEL:-*}/server.zip" | sort -n )
if [ -z "$SERVER_ZIPS" ]; then
echo "No such level: $LEVEL" >&2
exit 1
fi
for i in $( echo "$SERVER_ZIPS" | sort -n ); do
LEVEL=$( echo "$i" | xargs dirname | xargs basename )
for j in $( unzip -l "$i" | awk '{print $4}' | grep -iE "gamemodes/[^/]+/[0-9]+/.+" | cut -d '/' -f2-3 | sort -n | uniq || true ); do
GAMEMODE=$( echo "$j" | cut -d '/' -f1 )
SIZE=$( echo "$j" | cut -d '/' -f2 )
echo "$LEVEL $GAMEMODE $SIZE"
done
done
fi
if [ -n "$GET_MAPLIST" ]; then
SERVER_ZIPS=$( find "$DIR" -path "*/mods/$MOD/levels/${LEVEL:-*}/server.zip" | sort -n )
if [ -z "$SERVER_ZIPS" ]; then
echo "No such level: $LEVEL" >&2
exit 1
fi
for i in $( echo "$SERVER_ZIPS" | sort -n ); do
LEVEL=$( echo "$i" | xargs dirname | xargs basename )
for j in $( unzip -l "$i" | awk '{print $4}' | grep -iE "gamemodes/[^/]+/[0-9]+/.+" | cut -d '/' -f2-3 | sort -n | uniq || true ); do
GAMEMODE=$( echo "$j" | cut -d '/' -f1 )
SIZE=$( echo "$j" | cut -d '/' -f2 )
echo "maplist.append $LEVEL $GAMEMODE $SIZE"
done
done
fi
if [ -n "$GET_MODS" ]; then
ls -1 "$DIR/mods" | sort -n
fi
if [ -n "$GET_LEVELS" ]; then
ls -1 "$DIR/mods/$MOD/levels" | sort -n
fi
if [ -n "$GET_LEVEL_STRATEGIES" ]; then
SERVER_ZIPS=$( find "$DIR" -path "*/mods/$MOD/levels/${LEVEL:-*}/server.zip" | sort -n )
if [ -z "$SERVER_ZIPS" ]; then
echo "No such level: $LEVEL" >&2
exit 1
fi
for i in $SERVER_ZIPS; do
STRATEGIES_AI=$( unzip -l "$i" | grep -iE "gamemodes/[^/]+/[0-9]+/[^/]+/strategies.ai" | awk '{print $4}' | sort -n | uniq || true )
if [ -n "$STRATEGIES_AI" ]; then
for s in $STRATEGIES_AI; do
echo "$i $s"
done
else
echo "No strategies found in: $i" >&2
fi
done
fi
if [ -n "$GET_LEVEL_STRATEGIESCONTENT" ]; then
SERVER_ZIPS=$( find "$DIR" -path "*/mods/$MOD/levels/${LEVEL:-*}/server.zip" | sort -n )
if [ -z "$SERVER_ZIPS" ]; then
echo "No such level: $LEVEL" >&2
exit 1
fi
ERR=
for i in $SERVER_ZIPS; do
echo "Looking in: $i" >&2
STRATEGIES_AI=$( unzip -l "$i" | grep -iE "gamemodes/[^/]+/[0-9]+/[^/]+/strategies.ai" | awk '{print $4}' | sort -n | uniq || true )
if [ -n "$STRATEGIES_AI" ]; then
for s in $STRATEGIES_AI; do
echo "Content of $s in $i:" >&2
unzip -p "$i" "$s"
done
else
echo "No strategies found in: $i" >&2
ERR=1
fi
done
if [ -n "$ERR" ]; then
exit 1
fi
fi
if [ -n "$GET_STRATEGIES" ]; then
cd "$DIR"
find . | grep -E "mods/$MOD/esai/mapfiles/[^/]+/[^/]+/strategies.ai" | cut -d '/' -f7 | sort -n
fi
if [ -n "$APPLY_DEFAULT_STRATEGY" ]; then
# A trick not found in ESAI readmes
# The default ESAI strategy is used when there is no ESAI strategy for a particular level.
# According to Void, the original author of ESAI, it's easy to add the default ESAI strategy for all maps:
# 1. All except the last two lines of an ESAI strategy (i.e. mods/bf2/esai/mapfiles/*/<strategy>/strategies.ai) goes into aidefaultstrategies.ai
# 2. The last two lines go into aidefaultstrategiesadd.ai
STRATEGIES_AI=$( find "$DIR" | grep -E "mods/$MOD/esai/mapfiles/[^/]+/$STRATEGY/strategies.ai" | head -n1 || true )
if [ -z "$STRATEGIES_AI" ]; then
echo "No such strategy: $STRATEGY"
echo "To get a list of available strategies, run:"
echo " esai-helper -m "$MOD" get strategies"
exit 1
fi
CONTENT=$( cat "$STRATEGIES_AI" )
LINE_COUNT=$( echo "$CONTENT" | wc -l )
echo "Applying $STRATEGIES_AI to $DIR/mods/$MOD/ai/aidefaultstrategies.ai">&2
echo "$CONTENT" | head -n $(( $LINE_COUNT - 2 )) > "$DIR/mods/$MOD/ai/aidefaultstrategies.ai"
echo "Applying $STRATEGIES_AI to $DIR/mods/$MOD/ai/aidefaultstrategiesadd.ai">&2
echo "$CONTENT" | tail -n 2 > "$DIR/mods/$MOD/ai/aidefaultstrategiesadd.ai"
fi
if [ -n "$DELETE_DEFAULT_STRATEGY" ]; then
echo "Deleting a default ESAI strategy is not supported. The server needs a default strategy."
exit 1
fi
if [ -n "$APPLY_LEVEL_STRATEGY" ] || [ -n "$DELETE_LEVEL_STRATEGY" ] || [ -n "$APPLY_FILE" ] || [ -n "$DELETE_FILE" ]; then
# Validation
if [ -n "$APPLY_LEVEL_STRATEGY" ] || [ -n "$DELETE_LEVEL_STRATEGY" ]; then
CONTENT="$LEVEL $GAMEMODE $SIZE $STRATEGY"
fi
if [ -n "$APPLY_FILE" ] || [ -n "$DELETE_FILE" ]; then
if [ ! -f "$FILE" ]; then
echo "No such file: $FILE"
exit 1
fi
CONTENT=$( cat "$FILE" | grep -v '#' | awk NF || true ) # Remove lines starting with '#' or trailing with '# ...', as well as blank lines
if [ -z "$CONTENT" ]; then
echo "No ESAI strategies to apply in: $FILE"
exit 1
fi
fi
echo "$CONTENT" | while read -r LEVEL GAMEMODE SIZE STRATEGY; do
SERVER_ZIP="$DIR/mods/$MOD/levels/$LEVEL/server.zip"
if ! unzip -l "$SERVER_ZIP" | grep -iE "gamemodes/$GAMEMODE/$SIZE/" > /dev/null; then
echo "The gamemode '$GAMEMODE' does not exist in $SERVER_ZIP at gamemodes/$GAMEMODE/$SIZE/"
echo "To get a list of available gamemodes, run:"
echo " esai-helper -m "$MOD" get gamemodes"
exit 1
fi
STRATEGIES_AI=$( find "$DIR" | grep -E "mods/$MOD/esai/mapfiles/[^/]+/$STRATEGY/strategies.ai" | head -n1 || true )
if [ -z "$STRATEGIES_AI" ]; then
echo "No such strategy: $STRATEGY"
echo "To get a list of available strategies, run:"
echo " esai-helper -m "$MOD" get strategies"
exit 1
fi
done
if [ -n "$APPLY_LEVEL_STRATEGY" ] || [ -n "$APPLY_FILE" ]; then
echo "$CONTENT" | while read -r LEVEL GAMEMODE SIZE STRATEGY; do
cd "$DIR/mods/$MOD/levels/$LEVEL"
SERVER_ZIP="$PWD/server.zip"
STRATEGIES_AI=$( find "$DIR" | grep -E "mods/$MOD/esai/mapfiles/[^/]+/$STRATEGY/strategies.ai" | head -n1 || true )
STRATEGIES_AI_DESTINATION_IN_ZIP="gamemodes/$GAMEMODE/$SIZE/ai/strategies.ai"
echo "Applying strategy $STRATEGIES_AI to $SERVER_ZIP at $STRATEGIES_AI_DESTINATION_IN_ZIP"
mkdir -p "$( dirname "$STRATEGIES_AI_DESTINATION_IN_ZIP" )" # Same layout as content of server.zip
cp -f "$STRATEGIES_AI" "$STRATEGIES_AI_DESTINATION_IN_ZIP"
zip -qru server.zip gamemodes
rm -rf "$DIR/mods/$MOD/levels/$LEVEL/gamemodes"
done
fi
if [ -n "$DELETE_LEVEL_STRATEGY" ] || [ -n "$DELETE_FILE" ]; then
echo "$CONTENT" | while read -r LEVEL GAMEMODE SIZE STRATEGY; do
cd "$DIR/mods/$MOD/levels/$LEVEL"
SERVER_ZIP="$PWD/server.zip"
EXISTING_STRATEGIES_AI=$( unzip -l "$SERVER_ZIP" | grep -iE "gamemodes/$GAMEMODE/$SIZE/ai/strategies.ai" | awk '{print $4}' || true )
if [ -n "$EXISTING_STRATEGIES_AI" ]; then
for i in $EXISTING_STRATEGIES_AI; do
echo "Removing existing strategy $i from $SERVER_ZIP at $i"
zip -q -d "$SERVER_ZIP" "$i"
done
else
echo "No existing strategy in: $SERVER_ZIP"
exit 1
fi
done
fi
fi