Skip to content

Commit 2b0f248

Browse files
author
Tobias Wallin
committed
Merge branch 'develop'
2 parents 8ba8a8c + ad6d092 commit 2b0f248

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

factorio

+23-3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ if ! [ $1 == "install" ]; then
8989
FIFO="${WRITE_DIR}/server.fifo"
9090
fi
9191

92+
if [ -z "${CMDOUT}" ];then
93+
CMDOUT="${WRITE_DIR}/server.out"
94+
fi
95+
9296
# Finally, set up the invocation
9397
INVOCATION="${BINARY} --config ${FCONF} --port ${PORT} --start-server-load-latest --server-settings ${SERVER_SETTINGS} --autosave-interval ${AUTOSAVE_INTERVAL} --autosave-slots ${AUTOSAVE_SLOTS} --latency-ms ${LATENCY} ${RCON} ${EXTRA_BINARGS}"
9498

@@ -151,7 +155,7 @@ start_service() {
151155
return 1
152156
fi
153157

154-
as_user "tail -f ${FIFO} |${INVOCATION} > /dev/null 2>&1 & echo \$! > ${PIDFILE}"
158+
as_user "tail -f ${FIFO} |${INVOCATION} > ${CMDOUT} 2>&1 & echo \$! > ${PIDFILE}"
155159

156160
ps -p $(cat ${PIDFILE}) > /dev/null 2>&1
157161
if [ "$?" -ne "0" ]; then
@@ -200,9 +204,20 @@ stop_service() {
200204
send_cmd(){
201205
if is_running; then
202206
if [ -p ${FIFO} ]; then
203-
echo $@
207+
# Grab a random 16 character id for our command
208+
cmdid=$(cat /dev/urandom |tr -dc 'a-zA-Z0-9' |fold -w 16 |head -n 1)
209+
# Whisper that unknown player named after our random id
210+
echo "/w ${cmdid}" > ${FIFO}
211+
# Wait for factorio to read stdin
212+
sleep 0.5
213+
# Run the actual command
204214
echo $@ > ${FIFO}
205-
sleep 1
215+
# Wait again, this might need more sleep for larger command output - time will tell (sorry future self for any head aches)
216+
sleep 0.5
217+
# Read the output file backwards - capture everything between the end of file until
218+
# factorio tells us our random command id player does not exist
219+
response=$(sed "/Player ${cmdid} doesn't exist./q" <(tac ${CMDOUT})| grep -v ${cmdid})
220+
echo "${response}"
206221
else
207222
echo "${FIFO} is not a pipe!"
208223
return 1
@@ -235,6 +250,11 @@ check_permissions(){
235250
exit 1
236251
fi
237252
fi
253+
254+
if ! as_user "touch ${CMDOUT}" ; then
255+
echo "Check Permissions. Cannot touch cmd output file ${CMDOUT}"
256+
exit 1
257+
fi
238258
}
239259

240260
test_deps(){

0 commit comments

Comments
 (0)