#!/bin/bash # game # Copyright (C) 2009 Thunor # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # Object IDs OBJECT_ROOT=1 # Event IDs EVENT_INIT=1 EVENT_QUIT=2 # Function IDs FUNC_INIT=1 FUNC_QUIT=2 FUNC_EXIT=3 FUNC_DO_SOMETHING=4 # Make settings folder if it doesn't already exist mkdir -p $HOME/.basheng # Pipe names PIPEIN=$HOME/.basheng/pipein_$$_$RANDOM PIPEOUT=$HOME/.basheng/pipeout_$$_$RANDOM # Start the engine and background it ./engine $PIPEIN $PIPEOUT & echo "$0: engine pid=$!" # Force engine shutdown on script exit e.g. ctrl+c trap "if [ -p $PIPEIN ]; then echo $FUNC_EXIT > $PIPEIN; fi" EXIT # If necessary, wait a few seconds for both pipes to become available. # On my PC, most of the time it's instant, but on a hendheld device I # have experienced a 3 second delay. if [ ! -p $PIPEIN -o ! -p $PIPEOUT ]; then echo "$0: Waiting briefly for both pipes to become available..." STARTTIME=$(date "+%s"); ENDTIME=STARTTIME; MAXTIME=9 until [ $(($ENDTIME-$STARTTIME)) -gt $MAXTIME ]; do ENDTIME=$(date "+%s") if [ -p $PIPEIN -a -p $PIPEOUT ]; then break; fi done fi if [ -p $PIPEIN -a -p $PIPEOUT ]; then # Sending FUNC_INIT results in the engine unblocking on # "pipein = open(pipein_filename, O_RDONLY);" and eventually # sending EVENT_INIT which we'll use to set-up the game/engine echo "$0: Sending FUNC_INIT" echo $FUNC_INIT > $PIPEIN echo "$0: Entering event processing loop" while true do # read will wait until something is received if read OBJECT EVENT VALUE1 VALUE2 VALUE3 < $PIPEOUT; then if [ $OBJECT == $OBJECT_ROOT ]; then if [ $EVENT == $EVENT_INIT ]; then echo "$0: Received EVENT_INIT" PARAM1=123; PARAM2=456 echo "$0: Sending FUNC_DO_SOMETHING $PARAM1 $PARAM2" # This will result in a reply echo $FUNC_DO_SOMETHING $PARAM1 $PARAM2 > $PIPEIN read RETVAL < $PIPEOUT echo "$0: FUNC_DO_SOMETHING returned $RETVAL" elif [ $EVENT == $EVENT_QUIT ]; then echo "$0: Received EVENT_QUIT" # Shutdown the game echo "$0: Sending FUNC_EXIT" # This will not result in a reply echo $FUNC_EXIT > $PIPEIN break fi fi fi done echo "$0: Goodbye" exit 0 else echo "$0: Aborting due to unavailable pipes" exit 1 fi