From a7f8504c2c78f5e0c7c3a3d2e30e31afcf0fc9c2 Mon Sep 17 00:00:00 2001 From: Jkibbels Date: Sat, 21 Dec 2024 02:28:28 -0500 Subject: [PATCH] [factions-banking] Updated build script to run both server and client at once properly with no options plugged in --- run.sh | 85 ++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 20 deletions(-) diff --git a/run.sh b/run.sh index c32511b..e910ea1 100755 --- a/run.sh +++ b/run.sh @@ -13,8 +13,10 @@ LOG_LEVEL=1 BUILD_PATH="build" RUN_PATH="run" RESET_ALL=0 +RUN_ALL=1 GRADLEW_PATH=${REPO_TOP}/gradlew - +declare SERVER_PID +declare CLIENT_PID # $1 is text level # LEVELS: @@ -41,25 +43,47 @@ function TermWrite() { function help() { echo -e "Help for running $0:" - echo -e " -s: Build and run the program in server mode" - echo -e " -c: Build and run the program in client mode" - echo -e " -l [1-3]: Turn up the logging level to see more messages (default: 1)" - echo -e " -g : The path to the gradlew executable (default: $(pwd)/gradlew)" - echo -e " -r: Reset everything. This will nuke the previous install area and re-build EVERYTHING from scratch" - echo -e " -b : Specify the build area (default is cur dir $(pwd)/build)" - echo -e " -b2 : Specify the run area (default is cur dir $(pwd)/run)" - echo -e " -h: Bring up this help message" + echo -e " -s: Build and run the program in server mode" + echo -e " -c: Build and run the program in client mode" + echo -e " -l [1-3]: Turn up the logging level to see more messages (default: 1)" + echo -e " -g : The path to the gradlew executable (default: $(pwd)/gradlew)" + echo -e " -r: Reset everything. This will nuke the previous install area and re-build EVERYTHING from scratch" + echo -e " -b : Specify the build area (default is cur dir $(pwd)/build)" + echo -e " -b2 : Specify the run area (default is cur dir $(pwd)/run)" + echo -e " -a: Run everything at once (default option if nothing specified)" + echo -e " -h: Bring up this help message" } +function ExitAll() { + TermWrite 1 "Cleaning up..." + if [ ! -z $SERVER_PID ]; then + TermWrite 1 "Killing PID $SERVER_PID" + kill -9 $SERVER_PID + fi + + if [ ! -z $CLIENT_PID ]; then + TermWrite 1 "Killing PID $CLIENT_PID" + kill -9 $CLIENT_PID + fi + + # Close with exit code 1 because a clean close isn't guarenteed with this + exit 1 +} + +# Capture CTRL+C event into ExitAll function +trap ExitAll INT + while getopts ":schl:g:r:b:b2:" opt; do case ${opt} in s) TermWrite 2 "SERVER MODE ENABLED" SERVER_MODE=1 + RUN_ALL=0 ;; c) TermWrite 2 "CLIENT MODE ENABLED" CLIENT_MODE=1 + RUN_ALL=0 ;; h) help @@ -125,19 +149,40 @@ if [[ $RESET_ALL -eq 1 ]]; then TermWrite 1 "All completed!" fi -if [[ $SERVER_MODE -eq 1 ]]; then - TermWrite 1 "Running ./gradlew build" - $BASH_PATH $GRADLEW_PATH build - TermWrite 1 "Running ./gradlew runServer" - $BASH_PATH $GRADLEW_PATH runServer -fi +if [[ $RUN_ALL -eq 0 ]]; then + if [[ $SERVER_MODE -eq 1 ]]; then + TermWrite 1 "Running ./gradlew build" + $BASH_PATH $GRADLEW_PATH build + TermWrite 1 "Running ./gradlew runServer" + $BASH_PATH $GRADLEW_PATH runServer & + SERVER_PID=$! + fi -if [[ $CLIENT_MODE -eq 1 ]]; then - TermWrite 1 "Running ./gradlew build" + if [[ $CLIENT_MODE -eq 1 ]]; then + TermWrite 1 "Running ./gradlew build" + $BASH_PATH $GRADLEW_PATH build + TermWrite 1 "Running ./gradlew runClient" + $BASH_PATH $GRADLEW_PATH runClient + CLIENT_PID=$! + fi +else + # Run everything + TermWrite 1 "Running both client and server processes..." + $BASH_PATH $GRADLEW_PATH clean $BASH_PATH $GRADLEW_PATH build - TermWrite 1 "Running ./gradlew runClient" - $BASH_PATH $GRADLEW_PATH runClient + $BASH_PATH $GRADLEW_PATH runClient & + CLIENT_PID=$! + # We do not run the server in the background so the player may run commands in the server terminal + $BASH_PATH $GRADLEW_PATH runServer + SERVER_PID=$! + + TermWrite 1 "Server and client processes started" fi TermWrite 1 "Finished running gradlew command with exit code $?" -exit 0 + +# We wait and hang here until BOTH background processes finish (or until CTRL+C event) +wait + +# Clean up and leave +ExitAll