From e6f55965d82ac292805a2d6ad3528fbaa855b26f Mon Sep 17 00:00:00 2001 From: wytch Date: Fri, 27 Feb 2026 20:38:04 -0600 Subject: [PATCH] Formatting --- pub_event[rungame,endgame](sync).ash | 91 +++++++++--------- savesync-logger[start](permanent).ash | 19 ++-- savesync[start](permanent).ash | 129 +++++++++++++------------- 3 files changed, 121 insertions(+), 118 deletions(-) diff --git a/pub_event[rungame,endgame](sync).ash b/pub_event[rungame,endgame](sync).ash index 74bd9c3..aac08c6 100644 --- a/pub_event[rungame,endgame](sync).ash +++ b/pub_event[rungame,endgame](sync).ash @@ -6,25 +6,25 @@ # --- Logger Function --- log() { - local level="$1" - local msg="$2" - local timestamp - - timestamp=$(date '+%Y-%m-%d %H:%M:%S') - local log_line="[$timestamp] [$level] $msg" + local level="$1" + local msg="$2" + local timestamp - # Handle local emergency error log immediately - if [ "$level" = "ERROR" ]; then - mkdir -p "$(dirname "$ERROR_LOG")" - printf "%s\n" "$log_line" >> "$ERROR_LOG" - fi + timestamp=$(date '+%Y-%m-%d %H:%M:%S') + local log_line="[$timestamp] [$level] $msg" - # Dispatch to the Central Logging Daemon via MQTT - # We use -q 0 (fire and forget) so the game script doesn't wait - mosquitto_pub -h 127.0.0.1 -p 1833 -q 0 -t "$LOG_TOPIC" -m "SaveLog=$log_line" 2>/dev/null + # Handle local emergency error log immediately + if [ "$level" = "ERROR" ]; then + mkdir -p "$(dirname "$ERROR_LOG")" + printf "%s\n" "$log_line" >>"$ERROR_LOG" + fi - # Local debugging - [ "${DEBUG_MODE:-0}" -eq 1 ] && printf "%s\n" "$log_line" + # Dispatch to the Central Logging Daemon via MQTT + # We use -q 0 (fire and forget) so the game script doesn't wait + mosquitto_pub -h 127.0.0.1 -p 1833 -q 0 -t "$LOG_TOPIC" -m "SaveLog=$log_line" 2>/dev/null + + # Local debugging + [ "${DEBUG_MODE:-0}" -eq 1 ] && printf "%s\n" "$log_line" } # --- Exit function --- @@ -42,8 +42,8 @@ eventfile="/tmp/es_state.inf" # 1. Check if the file exists and is not empty if [ ! -s "$eventfile" ]; then - log "ERROR" "$eventfile is missing or empty." - exit 1 + log "ERROR" "$eventfile is missing or empty." + exit 1 fi # 2. Read the payload safely @@ -51,46 +51,47 @@ PAYLOAD=$(cat "$eventfile") # Step 3: Connection Check for i in $(seq 5); do - log "INFO" "Waiting on rclone endpoint... $i" - if nc -z "$RCLONE_ENDPOINT" "$RCLONE_PORT"; then - connect_success=1 - break - fi - sleep 0.5 + log "INFO" "Waiting on rclone endpoint... $i" + if nc -z "$RCLONE_ENDPOINT" "$RCLONE_PORT"; then + connect_success=1 + break + fi + sleep 0.5 done # Step 4: Publish # Added -m flag and removed the [ "$( ... )" ] wrapper if mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "$PAYLOAD"; then - log "INFO" "Successfully published event." + log "INFO" "Successfully published event." else - log "ERROR" "Failed to connect to mosquitto broker." - exit 1 + log "ERROR" "Failed to connect to mosquitto broker." + exit 1 fi # 5. Wait for response from daemon log "INFO" "Waiting for sync confirmation on $RESPONSE_TOPIC..." # Start a subshell that kills the subscriber after 10 seconds if no message arrives -( sleep 10; mosquitto_pub -h 127.0.0.1 -t "$RESPONSE_TOPIC" -m "SaveSync=timeout" ) & +( + sleep 10 + mosquitto_pub -h 127.0.0.1 -t "$RESPONSE_TOPIC" -m "SaveSync=timeout" +) & TIMEOUT_PID=$! # -C 1 ensures we exit after receiving either the real response or the timeout message -mosquitto_sub -h 127.0.0.1 -p 1883 -t "$RESPONSE_TOPIC" -C 1 | while IFS="=" read -r key value -do - # Kill the background sleep timer since we got a message - kill "$TIMEOUT_PID" 2>/dev/null - - case "$key" in - "SaveSync") - if [ "$value" = "timeout" ]; then - log "ERROR" "Sync timed out! Daemon might be down." - call_exit 1 - else - log "INFO" "Sync confirmed with status: $value" - call_exit "$value" - fi - ;; - esac -done +mosquitto_sub -h 127.0.0.1 -p 1883 -t "$RESPONSE_TOPIC" -C 1 | while IFS="=" read -r key value; do + # Kill the background sleep timer since we got a message + kill "$TIMEOUT_PID" 2>/dev/null + case "$key" in + "SaveSync") + if [ "$value" = "timeout" ]; then + log "ERROR" "Sync timed out! Daemon might be down." + call_exit 1 + else + log "INFO" "Sync confirmed with status: $value" + call_exit "$value" + fi + ;; + esac +done diff --git a/savesync-logger[start](permanent).ash b/savesync-logger[start](permanent).ash index 0e53852..662b213 100644 --- a/savesync-logger[start](permanent).ash +++ b/savesync-logger[start](permanent).ash @@ -7,17 +7,16 @@ touch "$LOG_FILE" touch "$ERROR_FILE" log_to_file() { - printf "%s\n" "$1" >> "$LOG_FILE" + printf "%s\n" "$1" >>"$LOG_FILE" } # Subscribe and wait for log entries -mosquitto_sub -h 127.0.0.1 -t "$LOG_TOPIC" | while read -r line -do - # The line will look like: SaveLog=[2026-...] [INFO] ... - # We strip the "SaveLog=" prefix - msg_content="${line#SaveLog=}" - - if [ -n "$msg_content" ]; then - log_to_file "$msg_content" - fi +mosquitto_sub -h 127.0.0.1 -t "$LOG_TOPIC" | while read -r line; do + # The line will look like: SaveLog=[2026-...] [INFO] ... + # We strip the "SaveLog=" prefix + msg_content="${line#SaveLog=}" + + if [ -n "$msg_content" ]; then + log_to_file "$msg_content" + fi done diff --git a/savesync[start](permanent).ash b/savesync[start](permanent).ash index ce63389..438244a 100644 --- a/savesync[start](permanent).ash +++ b/savesync[start](permanent).ash @@ -5,82 +5,85 @@ . /recalbox/share/system/configs/savesync/savesync.conf log() { - local level="$1" - local msg="$2" - local timestamp=$(date '+%Y-%m-%d %H:%M:%S') - local log_line="[$timestamp] [$level] $msg" + local level="$1" + local msg="$2" + local timestamp=$(date '+%Y-%m-%d %H:%M:%S') + local log_line="[$timestamp] [$level] $msg" - if [ "$level" = "ERROR" ]; then - mkdir -p "$(dirname "$ERROR_LOG")" - printf "%s\n" "$log_line" >> "$ERROR_LOG" - fi + if [ "$level" = "ERROR" ]; then + mkdir -p "$(dirname "$ERROR_LOG")" + printf "%s\n" "$log_line" >>"$ERROR_LOG" + fi - # FIX: Corrected port to 1883 and added -m - mosquitto_pub -h 127.0.0.1 -p 1883 -q 0 -t "$LOG_TOPIC" -m "SaveLog=$log_line" 2>/dev/null + # FIX: Corrected port to 1883 and added -m + mosquitto_pub -h 127.0.0.1 -p 1883 -q 0 -t "$LOG_TOPIC" -m "SaveLog=$log_line" 2>/dev/null - [ "${DEBUG_MODE:-0}" -eq 1 ] && printf "%s\n" "$log_line" + [ "${DEBUG_MODE:-0}" -eq 1 ] && printf "%s\n" "$log_line" } sleep 2 log "INFO" "--- ES Event Daemon Started ---" # --- Main Listener Loop --- -mosquitto_sub -h 127.0.0.1 -p 1883 -q 0 -t "$TOPIC" | while IFS="=" read -r key value -do - # 1. Clean input - value=$(echo "$value" | tr -d '\r') +mosquitto_sub -h 127.0.0.1 -p 1883 -q 0 -t "$TOPIC" | while IFS="=" read -r key value; do + # 1. Clean input + value=$(echo "$value" | tr -d '\r') - case "$key" in - "SystemId") this_system_id="$value" ;; - "GamePath") - this_game_path="$value" - # Corrected logic for paths - this_save_path=$(echo "$value" | sed 's|roms|saves|; s|\.[^.]*$|.srm|') - this_backup_path=$(echo "$this_save_path" | sed 's|saves|archives|') - ;; - "Action") this_action="$value" ;; + case "$key" in + "SystemId") this_system_id="$value" ;; + "GamePath") + this_game_path="$value" + # Corrected logic for paths + this_save_path=$(echo "$value" | sed 's|roms|saves|; s|\.[^.]*$|.srm|') + this_backup_path=$(echo "$this_save_path" | sed 's|saves|archives|') + ;; + "Action") this_action="$value" ;; "State") - this_state="$value" - - if [ "$this_state" = "playing" ] && [ "$this_action" = "rungame" ]; then - # We wrap the whole operation in ( ) & to background the entire sequence - ( - log "INFO" "Game Started: $(basename "$this_game_path")" - - filename=$(basename "$this_save_path") - remote_full="$REMOTE_BASE/$this_system_id/$filename" + this_state="$value" - local_size=$(stat -c %s "$this_save_path" 2>/dev/null || echo 0) - remote_size=$(rclone lsjson "$remote_full" 2>/dev/null | grep -o '"Size":[0-9]*' | cut -d: -f2) - : "${remote_size:=0}" + if [ "$this_state" = "playing" ] && [ "$this_action" = "rungame" ]; then + # We wrap the whole operation in ( ) & to background the entire sequence + ( + log "INFO" "Game Started: $(basename "$this_game_path")" - if [ "$local_size" -lt "$remote_size" ]; then - log "WARN" "Cloud save LARGER. Restoring..." - mkdir -p "$(dirname "$this_backup_path")" - rclone copyto "$remote_full" "$this_save_path" --backup-dir "$(dirname "$this_backup_path")" - else - log "INFO" "Local save safe. Updating..." - rclone update "$remote_full" "$this_save_path" - fi - - # Signal that sync is done - mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "SaveContinue=0" - ) & - log "DEBUG" "Sync process backgrounded for Start Game." + filename=$(basename "$this_save_path") + remote_full="$REMOTE_BASE/$this_system_id/$filename" - elif [ "$this_state" = "endgame" ]; then - ( - log "INFO" "Game Ended. Syncing..." - rclone update "$this_save_path" "$REMOTE_BASE/$this_system_id/" - log "INFO" "Sync Complete." - - mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "SaveContinue=0" - ) & - log "DEBUG" "Sync process backgrounded for End Game." - fi + local_size=$(stat -c %s "$this_save_path" 2>/dev/null || echo 0) + remote_size=$(rclone lsjson "$remote_full" 2>/dev/null | grep -o '"Size":[0-9]*' | cut -d: -f2) + : "${remote_size:=0}" - # Reset variables immediately so the loop is ready for the next line - this_system_id=""; this_game_path=""; this_save_path=""; this_action=""; this_backup_path="" - ;; - esac + if [ "$local_size" -lt "$remote_size" ]; then + log "WARN" "Cloud save LARGER. Restoring..." + mkdir -p "$(dirname "$this_backup_path")" + rclone copyto "$remote_full" "$this_save_path" --backup-dir "$(dirname "$this_backup_path")" + else + log "INFO" "Local save safe. Updating..." + rclone update "$remote_full" "$this_save_path" + fi + + # Signal that sync is done + mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "SaveContinue=0" + ) & + log "DEBUG" "Sync process backgrounded for Start Game." + + elif [ "$this_state" = "endgame" ]; then + ( + log "INFO" "Game Ended. Syncing..." + rclone update "$this_save_path" "$REMOTE_BASE/$this_system_id/" + log "INFO" "Sync Complete." + + mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "SaveContinue=0" + ) & + log "DEBUG" "Sync process backgrounded for End Game." + fi + + # Reset variables immediately so the loop is ready for the next line + this_system_id="" + this_game_path="" + this_save_path="" + this_action="" + this_backup_path="" + ;; + esac done