-
Notifications
You must be signed in to change notification settings - Fork 0
/
_terrariad
614 lines (537 loc) · 13.9 KB
/
_terrariad
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#!/bin/bash
MAINDIR="/opt/terraria"
WORLDDIR="$MAINDIR/worlds"
SERVERDIR="$MAINDIR/servers"
BACKUPDIR="$MAINDIR/backups"
error() {
echo "$@" 1>&2
}
SCREEN=`which screen`
if [ -z "$SCREEN" ]; then
error "'screen' not installed"
exit 1
fi
RDIFF_BACKUP=`which rdiff-backup`
if [ -z "$RDIFF_BACKUP" ]; then
error "'rdiff-backup' not installed"
exit 1
fi
PERL=`which perl`
if [ -z "$PERL" ]; then
error "'perl' not installed"
exit 1
fi
UNZIP=`which unzip`
if [ -z "$UNZIP" ]; then
error "'unzip' not installed"
exit 1
fi
isworldrunning() {
("$SCREEN" -list | grep -q "terraria-$1") &>/dev/null
return $?
}
isworldstarted() {
if [[ -f "$WORLDDIR/$1/screenlog.0" && `tail -n 400 "$WORLDDIR/$1/screenlog.0" | grep '^: .*$' | wc -l` -gt "0" && ! `tail -n 400 "$WORLDDIR/$1/screenlog.0" | grep '^[^<>]*exit\s*$' | wc -l` -gt "0" ]]; then
return 0
fi
return 1
}
setworldoption() {
sed -i "s|^\s*#*\s*\($2\s*=\s*\).*$|\1$3|" "$WORLDDIR/$1/serverconfig.txt"
}
getworldoption() {
cat "$WORLDDIR/$1/serverconfig.txt" | grep "^\s*#*\s*$2\s*=\s*.*$" | sed "s|^\s*#*\s*$2\s*=\s*\(.*\)$|\1|"
}
getworldplayerscurrent() {
if isworldstarted "$1"; then
local JOINED=`cat "$WORLDDIR/$1/screenlog.0" | grep '^[^<>]*has joined\.\s*$' | wc -l`
local LEFT=`cat "$WORLDDIR/$1/screenlog.0" | grep '^[^<>]*has left\.\s*$' | wc -l`
echo $(($JOINED-$LEFT))
else
echo "0"
fi
}
getworldmemory() {
local PID=`"$SCREEN" -list | grep "terraria-$1" | sed 's|^\s*\([0-9]*\)\..*$|\1|'`
if [ -z $PID ]; then
echo "0 kB"
else
printf "%d kB\n" `pstree -p $PID | grep -o '([0-9]\+)' | grep -o '[0-9]\+' | xargs ps --no-headers -o rss -p | awk '{memory+=$1} END {print memory}'`
fi
}
getworlds() {
if [ -z "$1" ]; then
ls "$WORLDDIR" 2>/dev/null
else
echo "$1"
fi
}
getworldsWithOption() {
if [ -z "$3" ]; then
local WORLDS=""
for WORLD in `getworlds`; do
if [ `getworldoption "$WORLD" "$1"` = "$2" ]; then
WORLDS+="$WORLD "
fi
done
echo "$WORLDS" | "$PERL" -pe 's/(.*) $/\1/'
else
echo "$3"
fi
}
getworldsRunning() {
if [ -z "$1" ]; then
"$SCREEN" -list | grep 'terraria-' | sed 's|^.*\.terraria-\([^[:blank:]]*\).*$|\1|' | "$PERL" -pe 's/\n/ /' | "$PERL" -pe 's/(.*) $/\1\n/'
else
echo "$1"
fi
}
getworldsStartable() {
if [ -z "$1" ]; then
local WORLDS=""
for WORLD in `getworlds`; do
if ! isworldrunning "$WORLD" && [ `getworldoption "$WORLD" "state"` != "disabled" ]; then
WORLDS+="$WORLD "
fi
done
echo "$WORLDS" | "$PERL" -pe 's/(.*) $/\1/'
else
echo "$1"
fi
}
start() {
if [ ! -d "$WORLDDIR/$1" ]; then
error "World not found"
return 1
fi
if [ `getworldoption "$1" "state"` = "disabled" ]; then
error "World disabled"
return 1
fi
if isworldrunning "$1"; then
error "World already running"
return 0
fi
local SERVERTYPE=`getworldoption "$1" "server"`
local COMMAND=""
if [ -z "$SERVERTYPE" ]; then
SERVERTYPE="vanilla"
fi
case "$SERVERTYPE" in
vanilla)
COMMAND="\"$SERVERDIR/vanilla/TerrariaServer\" -autoarch -config \"$WORLDDIR/$1/serverconfig.txt\""
;;
tmodloader)
COMMAND="\"$SERVERDIR/tmodloader/tModLoaderServer\" -autoarch -config \"$WORLDDIR/$1/serverconfig.txt\" -modpath \"$WORLDDIR/$1/Mods\""
;;
*)
error "Could not find configured server type"
return 1
;;
esac
cd "$WORLDDIR/$1"
rm -f "$WORLDDIR/$1"/screenlog.*
if ! "$SCREEN" -L -dmS "terraria-$1" /bin/bash -c "$COMMAND"; then
cd "$MAINDIR"
error "Could not start server"
return 1
fi
cd "$MAINDIR"
while ! isworldrunning "$1"; do
sleep 1
done
}
stop() {
if ! isworldrunning "$1"; then
error "World not running"
return 0
fi
local WAIT=0
while ! isworldstarted "$1" && [[ -z $2 || $WAIT -le $2 ]]; do
WAIT=$(($WAIT+1))
sleep 1
done
if ! send "$1" "exit"; then
error "Could not stop server"
return 1
fi
WAIT=0
while isworldrunning "$1" && [[ -z $2 || $WAIT -le $2 ]]; do
WAIT=$(($WAIT+1))
sleep 1
done
}
forceStop() {
local TIMEOUT="16"
stop "$1" "$TIMEOUT"
sleep 1
if isworldrunning "$1"; then
echo "Quitting server screen"
"$SCREEN" -XS "terraria-$1" quit
local WAIT=0
while isworldrunning "$1" && [ $WAIT -le $TIMEOUT ]; do
WAIT=$(($WAIT+1))
sleep 1
done
echo "Killing server screen"
"$SCREEN" -XS "terraria-$1" kill
WAIT=0
while isworldrunning "$1" && [ $WAIT -le $TIMEOUT ]; do
WAIT=$(($WAIT+1))
sleep 1
done
fi
}
backup() {
if [ ! -d "$WORLDDIR/$1" ]; then
error "World not found"
return 1
fi
if isworldstarted "$1"; then
if ! send "$1" "save"; then
error "Could not save server"
return 1
fi
sleep 5
fi
mkdir -p "$BACKUPDIR"
if ! "$RDIFF_BACKUP" --print-statistics "$WORLDDIR/$1" "$BACKUPDIR/$1" > "$WORLDDIR/$1/lastbackup.log"; then
error "Could not backup server"
return 1
fi
if ! "$RDIFF_BACKUP" --remove-older-than 15D --force "$BACKUPDIR/$1" >> "$WORLDDIR/$1/lastbackup.log"; then
error "Could not backup server"
return 1
fi
}
list() {
printf "$1: "
if isworldstarted "$1"; then
echo "started"
elif isworldrunning "$1"; then
echo "starting/stopping"
else
echo "stopped"
fi
if [ ! -d "$WORLDDIR/$1" ]; then
echo " World folder not found"
else
local STATE=`getworldoption "$1" "state"`
echo " State: '$STATE'"
local CURRENT=`getworldplayerscurrent "$1"`
local MAX=`getworldoption "$1" "maxplayers"`
echo " $CURRENT of $MAX players online"
local MEMORY=`getworldmemory "$1"`
echo " Used Memory: $MEMORY"
local MOTD=`getworldoption "$1" "motd"`
echo " Motd: '$MOTD'"
local SIZE=`getworldoption "$1" "autocreate"`
echo " Size: '$SIZE'"
local DIFFICULTY=`getworldoption "$1" "difficulty"`
echo " Difficulty: '$DIFFICULTY'"
local PORT=`getworldoption "$1" "port"`
echo " Port: '$PORT'"
fi
}
create() {
if [ "$1" = "quickstart" ]; then
error "World name reserved for quickstart"
return 1
fi
if [ "$5" = "777" ]; then
echo "Port reserved for quickstart"
return 1
fi
if [ -e "$WORLDDIR/$1" ]; then
error "World already exists"
return 1
fi
mkdir -p "$WORLDDIR/$1"
cp "$MAINDIR/defaultserverconfig.txt" "$WORLDDIR/$1/serverconfig.txt"
setworldoption "$1" "world" "$WORLDDIR/$1/$1.wld"
setworldoption "$1" "worldpath" "$WORLDDIR/$1/"
setworldoption "$1" "worldname" "$1"
setworldoption "$1" "server" "$2"
setworldoption "$1" "autocreate" "$3"
setworldoption "$1" "difficulty" "$4"
setworldoption "$1" "port" "$5"
}
send() {
if ! isworldstarted "$1"; then
error "World not started"
return 1
fi
if ! "$SCREEN" -S "terraria-$1" -X stuff "$2\r"; then
error "Could not send command"
return 1
fi
}
attach() {
if ! isworldrunning "$1"; then
error "World not running"
return 1
fi
script /dev/null -qc "\"$SCREEN\" -r \"terraria-$1\""
return $?
}
updateMods() {
if [ `getworldoption "$1" "server"` != "tmodloader" ]; then
error "Not a modded world"
return 1
fi
if [ -z `getworldoption "$1" "modpackdownload"` ]; then
error "No modpack link provided"
return 1
fi
local STARTED="false"
if isworldrunning "$1"; then
STARTED="true"
echo "-- Stopping world: $1"
stop "$1"
fi
echo "-- Updating mods for: $WORLD"
rm -rf "$WORLDDIR/$1/Mods"
if ! curl -Lso "$WORLDDIR/$1/Mods.zip" "`getworldoption "$1" "modpackdownload"`"; then
rm -f "$WORLDDIR/$1/Mods.zip"
return 1
fi
"$UNZIP" -q "$WORLDDIR/$1/Mods.zip" -d "$WORLDDIR/$1/Mods"
rm -f "$WORLDDIR/$1/Mods.zip"
if $STARTED; then
echo "-- Starting world: $1"
start "$1"
fi
}
quickstartRemove() {
if isworldrunning "quickstart"; then
echo "-- Stopping world: quickstart"
forceStop "quickstart"
fi
echo "-- Removing world: quickstart"
rm -rf "$WORLDDIR/quickstart"
}
quickstartCreate() {
quickstartRemove
echo "-- Creating world: quickstart"
cp -a "$WORLDDIR/$1" "$WORLDDIR/quickstart"
rm -f "$WORLDDIR/quickstart"/screenlog.*
rm -f "$WORLDDIR/quickstart"*.wld
rm -f "$WORLDDIR/quickstart"*.wld.bak
rm -f "$WORLDDIR/quickstart"*.twld
rm -f "$WORLDDIR/quickstart"*.twld.bak
setworldoption "quickstart" "autocreate" "$2"
setworldoption "quickstart" "difficulty" "$3"
setworldoption "quickstart" "port" "777"
}
update() {
mkdir -p "$SERVERDIR"
# Vanilla
rm -rf "$SERVERDIR/vanilla"
local DOWNLOAD=`curl -Ls "https://terraria.gamepedia.com/Server" | "$PERL" -pe 's/\n//' | sed 's|.*\(https*://terraria.org/server/.*\.zip\).*|\1|'`
curl -Lso "$SERVERDIR/vanilla.zip" "$DOWNLOAD"
"$UNZIP" -q "$SERVERDIR/vanilla.zip" -d "$SERVERDIR/vanilla.tmp"
rm -f "$SERVERDIR/vanilla.zip"
local VERSIONFOLDER=`ls "$SERVERDIR/vanilla.tmp"`
mv "$SERVERDIR/vanilla.tmp/$VERSIONFOLDER/Linux" "$SERVERDIR/vanilla"
rm -rf "$SERVERDIR/vanilla.tmp"
chmod +x "$SERVERDIR/vanilla/TerrariaServer"
chmod +x "$SERVERDIR/vanilla/TerrariaServer.bin.x86"
chmod +x "$SERVERDIR/vanilla/TerrariaServer.bin.x86_64"
# tModLoader
rm -rf "$SERVERDIR/tmodloader"
cp -a "$SERVERDIR/vanilla" "$SERVERDIR/tmodloader"
chmod -x "$SERVERDIR/tmodloader/TerrariaServer"
chmod -x "$SERVERDIR/tmodloader/TerrariaServer.bin.x86"
chmod -x "$SERVERDIR/tmodloader/TerrariaServer.bin.x86_64"
local DOWNLOAD=`curl -Ls "https://forums.terraria.org/index.php?threads/23726" | "$PERL" -pe 's/\n//' | sed 's|.*Direct Download Links:.*<a href="\(.*\)" target="_blank" class="externalLink" rel="nofollow">Linux</a>.*Past Version Downloads.*|\1|'`
curl -Lso "$SERVERDIR/tmodloader.zip" "$DOWNLOAD"
"$UNZIP" -qao "$SERVERDIR/tmodloader.zip" -d "$SERVERDIR/tmodloader"
rm -f "$SERVERDIR/tmodloader.zip"
chmod +x "$SERVERDIR/tmodloader/tModLoaderServer"
chmod +x "$SERVERDIR/tmodloader/tModLoaderServer.bin.x86"
chmod +x "$SERVERDIR/tmodloader/tModLoaderServer.bin.x86_64"
}
case "$1" in
start)
for WORLD in `getworldsStartable "$2"`; do
echo "-- Starting world: $WORLD"
start "$WORLD"
done
;;
stop)
for WORLD in `getworldsRunning "$2"`; do
echo "-- Stopping world: $WORLD"
stop "$WORLD"
done
;;
force-stop)
for WORLD in `getworldsRunning "$2"`; do
echo "-- Stopping world: $WORLD"
forceStop "$WORLD"
done
;;
restart)
if [ ! -z "$2" ]; then
echo "-- Restarting world: $2"
if stop "$2"; then
start "$2"
fi
elif "$0" stop; then
"$0" start
fi
;;
force-restart)
if [ ! -z "$2" ]; then
echo "-- Restarting world: $2"
if force-stop "$2"; then
start "$2"
fi
elif "$0" force-stop; then
"$0" start
fi
;;
backup)
for WORLD in `getworlds "$2"`; do
echo "-- Backing up world: $WORLD"
backup "$WORLD"
done
;;
status|list|ls)
for WORLD in `getworlds "$2"`; do
list "$WORLD"
done
;;
send)
if [ -z "$2" ]; then
echo "Enter the world name to send to"
exit 1
fi
if [ -z "$3" ]; then
echo "Enter a command to send"
exit 1
fi
echo "-- Sending '${*:3}' to '$2'"
send "$2" "${*:3}"
;;
attach)
if [ -z "$2" ]; then
echo "Enter the world name to attach to"
exit 1
fi
echo "-- Attaching to '$2'"
attach "$2"
;;
create)
if [ -z "$2" ]; then
echo "Enter a name for the new world"
exit 1
fi
if [[ "$3" != "vanilla" && "$3" != "tmodloader" ]]; then
echo "Enter a valid server type for the new world (vanilla, tmodloader)"
exit 1
fi
if [[ "$4" != "1" && "$4" != "2" && "$4" != "3" ]]; then
echo "Enter a valid size for the new world (1, 2, 3)"
exit 1
fi
if [[ "$5" != "0" && "$5" != "1" ]]; then
echo "Enter a valid difficulty for the new world (0, 1)"
exit 1
fi
PORT="$6"
if [ -z "$PORT" ]; then
PORT="7777"
fi
echo "-- Creating world: $2"
create "$2" "$3" "$4" "$5" "$PORT"
;;
update)
if [ "$2" = "no-restart" ]; then
echo "Updating server..."
update
else
if "$0" stop; then
echo "Updating server..."
if update; then
"$0" start
fi
fi
fi
;;
update-mods)
for WORLD in `getworldsWithOption "server" "tmodloader" "$2"`; do
updateMods "$WORLD"
done
;;
enable)
if [ -z "$2" ]; then
echo "Enter a world to enable"
exit 1
fi
echo "-- Enabling world: $2"
setworldoption "$2" "state" "enabled"
;;
disable)
if [ -z "$2" ]; then
echo "Enter a world to disable"
exit 1
fi
if isworldrunning "$2"; then
echo "-- Stopping world: $2"
stop "$2"
fi
echo "-- Disabling world: $2"
setworldoption "$2" "state" "disabled"
;;
quickstart)
if [ "$2" = "remove" ]; then
quickstartRemove
elif [ "$2" = "create" ]; then
if [ -z "$3" ]; then
echo "Enter a name for the world to use as a base"
exit 1
fi
if [[ "$4" != "1" && "$4" != "2" && "$4" != "3" ]]; then
echo "Enter a valid size for the quickstart world (1, 2, 3)"
exit 1
fi
if [[ "$5" != "0" && "$5" != "1" ]]; then
echo "Enter a valid difficulty for the quickstart world (0, 1)"
exit 1
fi
quickstartCreate "$3" "$4" "$5"
echo "-- Starting world: quickstart"
start "quickstart"
else
error "Enter a valid action (create, remove)"
exit 1
fi
;;
list-worlds-all)
getworlds
;;
list-worlds-enabled)
getworldsWithOption "state" "enabled"
;;
list-worlds-disabled)
getworldsWithOption "state" "disabled"
;;
list-worlds-servertype)
if [ -z "$2" ]; then
echo "Enter a server type"
exit 1
fi
getworldsWithOption "server" "$2"
;;
list-worlds-running)
getworldsRunning
;;
list-worlds-startable)
getworldsStartable
;;
*)
error "Unknown command or parameter"
exit 1
;;
esac
exit 0