forked from Arceden/TerrariaService
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraria
executable file
·628 lines (539 loc) · 11.9 KB
/
terraria
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#!/bin/bash
### BEGIN INIT INFO
# Provides: terraria
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $network $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop the Terraria multiplayer server
# Description: TerrariaService lets you start, stop and save a terraria server without keeping the TUI open
### END INIT INFO
# Source: https://wiki.debian.org/LSBInitScripts
CONFIG_PATH=`readlink -e $0 | sed "s:[^/]*$:config.cfg:"`
## Import configuration file
if [ -f $CONFIG_PATH ]; then
source $CONFIG_PATH
else
# Could not find the config, create new one
cp $CONFIG_PATH.example $CONFIG_PATH
if ! [ -f $CONFIG_PATH ]; then
echo "Could not create new config file in: $PWD"
exit 1
else
echo "New file added: config.cfg"
echo "Edit the config.cfg file and start the script again."
exit 0
# source config.cfg
fi
fi
## Default values
init()
{
if [ -z "${DEBUGGING}" ]; then
DEBUGGING=true
fi
if [ -z "${WRITE_DIR}" ]; then
WRITE_DIR=/opt/terraria-service
fi
if [ -z "${TERRARIA_PATH}" ]; then
TERRARIA_PATH=/opt/terraria
fi
if [ -z "${TERRARIA_BINARY}" ]; then
TERRARIA_BINARY=TerrariaServer
fi
CURRENT_USER=`whoami`
FIFO="${WRITE_DIR}/server.fifo" # First in, First out file for commands
CMDOUT="${WRITE_DIR}/server.out" # Server output log
PIDFILE="${WRITE_DIR}/server.pid" # Server process ID
ERRLOG="${WRITE_DIR}/errors.log" # Error log
TERRARIA_CONFIG="${TERRARIA_PATH}/serverconfig.txt"
ARGS="-config ${TERRARIA_PATH}/serverconfig.txt" # Server requires a server config to instantly boot
INVOKE="mono --server --gc=sgen -O=all ${TERRARIA_PATH}/${TERRARIA_BINARY} ${ARGS}"
}
init
## Debugger
debug()
{
echo "Debugger:"
echo -e " USERNAME:\t\t${USERNAME}"
echo -e " CURRENT_USER:\t\t${CURRENT_USER}"
echo -e " TERRARIA_PATH:\t${TERRARIA_PATH}"
echo -e " TERRARIA_BINARY:\t${TERRARIA_BINARY}"
echo -e " WRITE_DIR:\t\t${WRITE_DIR}"
echo -e " PIDFILE:\t\t${PIDFILE}"
echo -e " ERRLOG:\t\t${ERRLOG}"
echo -e " LOG:\t\t\t${CMDOUT}"
echo
}
## Usage display
usage()
{
echo "Usage: $0 <COMMAND>"
echo
echo "Service commands:"
echo -e " start\t\t Starts the server"
echo -e " stop\t\t Stops the server without saving"
echo -e " checkfiles\t Validates the service and server files"
echo -e " state\t\t Displays the server state"
echo -e " kill\t\t Kills the server process"
echo -e " dir\t\t Shows the directories in use"
echo -e " help\t\t Shows this help message"
echo ""
echo "Server commands:"
echo -e " save\t\t\t Save the game world"
echo -e " exit\t\t\t Save the game world and stop the server"
echo -e " kick <\"player name\">\t Kicks a player from the server"
echo -e " ban <\"player name\">\t Bans a player from the server"
echo -e " password\t\t Show password"
echo -e " password <pass>\t Change password"
echo -e " version\t\t Print version number"
echo -e " time\t\t\t Display game time"
echo -e " port\t\t\t Print the listening port"
echo -e " maxplayers\t\t Print the max number of players"
echo -e " say <\"message\">\t\t Send a message to all players. They will see the message in yellow prefixed with <server> in the chat"
echo -e " motd\t\t\t Print MOTD"
echo -e " motd <\"message\">\t Change MOTD"
echo -e " dawn\t\t\t Change time to dawn (4:30 AM)"
echo -e " noon\t\t\t Change time to noon (12:00 PM)"
echo -e " dusk\t\t\t Change time to dusk (7:30 PM)"
echo -e " midnight\t\t Change time to midnight (12:00 AM)"
echo -e " settle\t\t Settle all water"
}
# Check file status
check_files()
{
PERMISSIONS_ERR=0
MISSING_CONFIG=false
# Terraria server
if ! [ -d ${TERRARIA_PATH} ] ; then
echo "Could not find the terraria server directory at ${TERRARIA_PATH}."
echo "Make sure that you have downloaded the terraria dedicated server from https://terraria.org before starting the service."
exit 1
fi
# Service folder permissions
## Not using the compact [-w ${WRITE_DIR} so i can test the user]
if ! exec_user "test -w ${WRITE_DIR}" ; then
echo "Missing write permissions: ${WRITE_DIR}"
((PERMISSIONS_ERR++))
fi
# Server folder permissions
if ! exec_user "test -w ${TERRARIA_PATH}" ; then
echo "Missing write permissions: ${TERRARIA_PATH}"
((PERMISSIONS_ERR++))
fi
# Terraria server config
if ! [ -f ${TERRARIA_CONFIG} ] ; then
echo "Missing file: ${TERRARIA_CONFIG}"
MISSING_CONFIG=true
elif ! exec_user "test -r ${TERRARIA_CONFIG}" ; then
echo "Missing permissions: ${TERRARIA_CONFIG}"
((PERMISSIONS_ERR++))
fi
# Terraria executable permissions
if ! [ -x ${TERRARIA_PATH}/${TERRARIA_BINARY} ] ; then
echo "Missing executable permissions: ${TERRARIA_PATH}/${TERRARIA_BINARY}"
((PERMISSIONS_ERR++))
fi
# Fix permissions
if ! [ "$PERMISSIONS_ERR" -eq "0" ]; then
echo "Found ${PERMISSIONS_ERR} errors!"
fix_permissions
fi
# Create missing files
if $MISSING_CONFIG ; then
init_terraria_config
fi
}
# Attempt to fix the permissions
fix_permissions()
{
read -r -p "Attempt to auto-fix permissions? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
# Set user permissions to the whole terraria path
sudo chown -R $USERNAME:$USERNAME ${TERRARIA_PATH}
# Set user permissions to the whole service path
sudo chown -R $USERNAME:$USERNAME ${WRITE_DIR}
# Set executable permissions on the binary
sudo chmod +x ${TERRARIA_PATH}/${TERRARIA_BINARY}
# Check again
check_files
;;
*)
exit 1
;;
esac
}
# Init new server config
init_terraria_config()
{
read -r -p "Want to generate a new simple terraria config? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
# Create new file
echo "maxplayers=8" > ${TERRARIA_CONFIG}
echo "world=${TERRARIA_PATH}/Worlds/NewWorld.wld" >> ${TERRARIA_CONFIG}
echo "worldname=NewWorld" >> ${TERRARIA_CONFIG}
echo "worldpath=${TERRARIA_PATH}/Worlds/" >> ${TERRARIA_CONFIG}
echo "autocreate=2" >> ${TERRARIA_CONFIG}
echo "difficulty=0" >> ${TERRARIA_CONFIG}
echo "Created new file: ${TERRARIA_CONFIG}"
# Check again
check_files
;;
*)
exit 1
;;
esac
}
# Execute for User
## Helper function
## Executes the provided command
exec_user()
{
if [ $CURRENT_USER == $USERNAME ]; then # Are you the terraria user?
if $DEBUGGING; then
echo "Executing: ${1}"
fi
bash -c "$1"
elif [ "$(id -u)" == "0" ]; then # Are you root?
if $DEBUGGING; then
echo "Executing as ROOT: ${1}"
fi
su $USERNAME -s /bin/bash -c "$1"
else # You are neither
echo "Run this script as the $USERNAME user or edit the config.cfg file!"
exit 1
fi
}
# Start script
## Creates a new PID file
start_service()
{
# Check if file exists
if [ -f ${PIDFILE} ]; then
echo "File already exists!"
return 1
fi
# Create new PIDFILE
if ! exec_user "touch ${PIDFILE}" ; then
echo "Could not create a new PID file"
return 1
fi
# Create error log
if ! [ -f ${ERRLOG} ]; then
echo "Error log does not exist! Making a new one"
if ! exec_user "touch ${ERRLOG}"; then
echo "Could not make a error log file!"
return 1;
fi
fi
# FIFO file?
if ! [ -f ${FIFO} ]; then
exec_user "touch ${FIFO}"
fi
# Attempt to start the process along with the PID into the PIDFILE
exec_user "tail -f ${FIFO} 2>${ERRLOG} | ${INVOKE} >> ${CMDOUT} 2>&1 & echo \$! > ${PIDFILE}"
# Check if online
if [ "$?" -ne "0" ]; then
echo "Unable to start ${SERVICE_NAME}"
return 1
else
echo "Started the server, please see log for details (${CMDOUT})"
fi
}
# Stop service
stop_service()
{
echo "Stopping server.."
terraria_command "exit"
}
# Kill service
kill_service()
{
if is_running ; then
echo "Killing server process.."
exec_user "kill -TERM $(cat ${PIDFILE} 2> /dev/null) 2> /dev/null"
exec_user "rm ${PIDFILE} 2> /dev/null"
fi
}
# Check if server is running
## Why is 0 true and 1 false?
is_running()
{
# Check if file does not exists
if ! [ -r ${PIDFILE} ]; then
return 1
fi
# Check if the process is still running
if ! ps --pid $(cat ${PIDFILE}) > /dev/null ; then
# Process is not running and the PIDFILE should not be here
exec_user "rm ${PIDFILE} 2> /dev/null"
return 1
fi
# Server up and running
return 0
}
# Show the directories used by this service
show_directories()
{
echo "The service directory is"
echo "$WRITE_DIR"
echo "The server directory is"
echo "$TERRARIA_PATH"
}
# Save game
save()
{
echo "Saving game.."
terraria_command "save"
}
# Execute server command
terraria_command()
{
exec_user "echo $1 >> ${FIFO}" # Send command to file
sleep 0.25s
dd if=/dev/null iflag=nonblock of="${FIFO}" status=none # Clear file
}
# Print the last $1 lines of server output
print_output()
{
hn="$1"
tn=$((hn+1))
exec_user "tail -$tn $CMDOUT | head -$hn | sed -e 's/^: : *//g;s/^: *//g'"
}
# Kick player
kick()
{
read -r -p "Kick player $1? [y/N]" response #Get the confirmation
echo ""
case "$response" in
[yY][eE][sS]|[yY])
terraria_command "kick $1" #Kick the User (if it exists)
print_output 2
;;
*)
exit 1
;;
esac
}
# Ban player
ban()
{
read -r -p "Ban player $1? [y/N]" response #Get the confirmation
echo ""
case "$response" in
[yY][eE][sS]|[yY])
terraria_command "ban $1" #Ban the User (if it exists)
print_output 2
;;
*)
exit 1
;;
esac
}
# Show password
show_password()
{
terraria_command "password"
print_output 1
}
# Set password
set_password()
{
terraria_command "password $1"
print_output 1
}
# Show Version
version()
{
terraria_command "version"
print_output 1
}
# Show ingame time
print_time()
{
terraria_command "time"
print_output 1
}
# Show Port (1 line)
port()
{
terraria_command "port"
print_output 1
}
# Show maximum number of players (1 line)
maxplayers()
{
terraria_command "maxplayers"
print_output 1
}
# Say something as the server (1 line)
say()
{
terraria_command "say $1"
print_output 1
}
# Print the message of the day (1 line)
print_motd()
{
terraria_command "motd"
print_output 1
}
# Change the message of the day
change_motd()
{
terraria_command "motd $1"
terraria_command "motd"
print_output 1
}
# Set ingame time to dawn
dawn()
{
terraria_command "dawn"
echo "Time set to dawn"
}
# Set ingame time to noon
noon()
{
terraria_command "noon"
echo "Time set to noon"
}
# Set ingame time to dusk
dusk()
{
terraria_command "dusk"
echo "Time set to dusk"
}
# Set ingame time to midnight
midnight()
{
terraria_command "midnight"
echo "Time set to midnight"
}
# Force the water to settle
settle()
{
terraria_command "settle"
echo "Settling water"
}
## Middleware
if $DEBUGGING; then
debug
fi
## Check User before executing any command
if [ $CURRENT_USER != $USERNAME ] && [ "$(id -u)" != "0" ]; then
echo "Run this script as the $USERNAME user or edit the config.cfg file!"
exit 1
fi
## Routing
case "$1" in
start)
check_files
if is_running; then
echo "Could not start the terraria server: Server is already running"
exit 1
elif ! start_service; then
echo "Could not start the terraria server"
exit 1
else
exit 0
fi
;;
stop)
## A service handles stop on its own by killing the process PID
;;
checkfiles)
check_files
;;
state)
if is_running ; then
echo "Server is up and running"
else
echo "Server is offline"
fi
;;
kill)
kill_service
;;
help|--help|-h)
usage
;;
dir)
show_directories
;;
save)
save
;;
exit)
stop_service
;;
kick)
if [[ -z "$2" ]]; then
echo "Usage: kick <player>"
else
kick "$2"
fi
;;
ban)
if [[ -z "$2" ]]; then
echo "Usage: ban <player>"
else
ban "$2"
fi
;;
password)
if [[ -z "$2" ]]; then
show_password
else
set_password "$2"
fi
;;
version)
version
;;
time)
print_time
;;
port)
port
;;
maxplayers)
maxplayers
;;
say)
if [[ -z "$2" ]]; then
echo "Usage: say <words>"
else
say "$2"
fi
;;
motd)
if [[ -z "$2" ]]; then
print_motd
else
change_motd "$2"
fi
;;
dawn)
dawn
;;
noon)
noon
;;
dusk)
dusk
;;
midnight)
midnight
;;
settle)
settle
;;
*)
echo "Unknown command"
usage
exit 1
;;
esac
exit 0