Formatting

This commit is contained in:
2026-02-27 20:38:04 -06:00
parent e7d29c77b0
commit e6f55965d8
3 changed files with 121 additions and 118 deletions

View File

@@ -6,25 +6,25 @@
# --- Logger Function --- # --- Logger Function ---
log() { log() {
local level="$1" local level="$1"
local msg="$2" local msg="$2"
local timestamp local timestamp
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
local log_line="[$timestamp] [$level] $msg"
# Handle local emergency error log immediately timestamp=$(date '+%Y-%m-%d %H:%M:%S')
if [ "$level" = "ERROR" ]; then local log_line="[$timestamp] [$level] $msg"
mkdir -p "$(dirname "$ERROR_LOG")"
printf "%s\n" "$log_line" >> "$ERROR_LOG"
fi
# Dispatch to the Central Logging Daemon via MQTT # Handle local emergency error log immediately
# We use -q 0 (fire and forget) so the game script doesn't wait if [ "$level" = "ERROR" ]; then
mosquitto_pub -h 127.0.0.1 -p 1833 -q 0 -t "$LOG_TOPIC" -m "SaveLog=$log_line" 2>/dev/null mkdir -p "$(dirname "$ERROR_LOG")"
printf "%s\n" "$log_line" >>"$ERROR_LOG"
fi
# Local debugging # Dispatch to the Central Logging Daemon via MQTT
[ "${DEBUG_MODE:-0}" -eq 1 ] && printf "%s\n" "$log_line" # 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 --- # --- Exit function ---
@@ -42,8 +42,8 @@ eventfile="/tmp/es_state.inf"
# 1. Check if the file exists and is not empty # 1. Check if the file exists and is not empty
if [ ! -s "$eventfile" ]; then if [ ! -s "$eventfile" ]; then
log "ERROR" "$eventfile is missing or empty." log "ERROR" "$eventfile is missing or empty."
exit 1 exit 1
fi fi
# 2. Read the payload safely # 2. Read the payload safely
@@ -51,46 +51,47 @@ PAYLOAD=$(cat "$eventfile")
# Step 3: Connection Check # Step 3: Connection Check
for i in $(seq 5); do for i in $(seq 5); do
log "INFO" "Waiting on rclone endpoint... $i" log "INFO" "Waiting on rclone endpoint... $i"
if nc -z "$RCLONE_ENDPOINT" "$RCLONE_PORT"; then if nc -z "$RCLONE_ENDPOINT" "$RCLONE_PORT"; then
connect_success=1 connect_success=1
break break
fi fi
sleep 0.5 sleep 0.5
done done
# Step 4: Publish # Step 4: Publish
# Added -m flag and removed the [ "$( ... )" ] wrapper # Added -m flag and removed the [ "$( ... )" ] wrapper
if mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "$PAYLOAD"; then 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 else
log "ERROR" "Failed to connect to mosquitto broker." log "ERROR" "Failed to connect to mosquitto broker."
exit 1 exit 1
fi fi
# 5. Wait for response from daemon # 5. Wait for response from daemon
log "INFO" "Waiting for sync confirmation on $RESPONSE_TOPIC..." log "INFO" "Waiting for sync confirmation on $RESPONSE_TOPIC..."
# Start a subshell that kills the subscriber after 10 seconds if no message arrives # 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=$! TIMEOUT_PID=$!
# -C 1 ensures we exit after receiving either the real response or the timeout message # -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 mosquitto_sub -h 127.0.0.1 -p 1883 -t "$RESPONSE_TOPIC" -C 1 | while IFS="=" read -r key value; do
do # Kill the background sleep timer since we got a message
# Kill the background sleep timer since we got a message kill "$TIMEOUT_PID" 2>/dev/null
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
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

View File

@@ -7,17 +7,16 @@ touch "$LOG_FILE"
touch "$ERROR_FILE" touch "$ERROR_FILE"
log_to_file() { log_to_file() {
printf "%s\n" "$1" >> "$LOG_FILE" printf "%s\n" "$1" >>"$LOG_FILE"
} }
# Subscribe and wait for log entries # Subscribe and wait for log entries
mosquitto_sub -h 127.0.0.1 -t "$LOG_TOPIC" | while read -r line mosquitto_sub -h 127.0.0.1 -t "$LOG_TOPIC" | while read -r line; do
do # The line will look like: SaveLog=[2026-...] [INFO] ...
# The line will look like: SaveLog=[2026-...] [INFO] ... # We strip the "SaveLog=" prefix
# We strip the "SaveLog=" prefix msg_content="${line#SaveLog=}"
msg_content="${line#SaveLog=}"
if [ -n "$msg_content" ]; then
if [ -n "$msg_content" ]; then log_to_file "$msg_content"
log_to_file "$msg_content" fi
fi
done done

View File

@@ -5,82 +5,85 @@
. /recalbox/share/system/configs/savesync/savesync.conf . /recalbox/share/system/configs/savesync/savesync.conf
log() { log() {
local level="$1" local level="$1"
local msg="$2" local msg="$2"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S') local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
local log_line="[$timestamp] [$level] $msg" local log_line="[$timestamp] [$level] $msg"
if [ "$level" = "ERROR" ]; then if [ "$level" = "ERROR" ]; then
mkdir -p "$(dirname "$ERROR_LOG")" mkdir -p "$(dirname "$ERROR_LOG")"
printf "%s\n" "$log_line" >> "$ERROR_LOG" printf "%s\n" "$log_line" >>"$ERROR_LOG"
fi fi
# FIX: Corrected port to 1883 and added -m # 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 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 sleep 2
log "INFO" "--- ES Event Daemon Started ---" log "INFO" "--- ES Event Daemon Started ---"
# --- Main Listener Loop --- # --- Main Listener Loop ---
mosquitto_sub -h 127.0.0.1 -p 1883 -q 0 -t "$TOPIC" | while IFS="=" read -r key value mosquitto_sub -h 127.0.0.1 -p 1883 -q 0 -t "$TOPIC" | while IFS="=" read -r key value; do
do # 1. Clean input
# 1. Clean input value=$(echo "$value" | tr -d '\r')
value=$(echo "$value" | tr -d '\r')
case "$key" in case "$key" in
"SystemId") this_system_id="$value" ;; "SystemId") this_system_id="$value" ;;
"GamePath") "GamePath")
this_game_path="$value" this_game_path="$value"
# Corrected logic for paths # Corrected logic for paths
this_save_path=$(echo "$value" | sed 's|roms|saves|; s|\.[^.]*$|.srm|') this_save_path=$(echo "$value" | sed 's|roms|saves|; s|\.[^.]*$|.srm|')
this_backup_path=$(echo "$this_save_path" | sed 's|saves|archives|') this_backup_path=$(echo "$this_save_path" | sed 's|saves|archives|')
;; ;;
"Action") this_action="$value" ;; "Action") this_action="$value" ;;
"State") "State")
this_state="$value" 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"
local_size=$(stat -c %s "$this_save_path" 2>/dev/null || echo 0) if [ "$this_state" = "playing" ] && [ "$this_action" = "rungame" ]; then
remote_size=$(rclone lsjson "$remote_full" 2>/dev/null | grep -o '"Size":[0-9]*' | cut -d: -f2) # We wrap the whole operation in ( ) & to background the entire sequence
: "${remote_size:=0}" (
log "INFO" "Game Started: $(basename "$this_game_path")"
if [ "$local_size" -lt "$remote_size" ]; then filename=$(basename "$this_save_path")
log "WARN" "Cloud save LARGER. Restoring..." remote_full="$REMOTE_BASE/$this_system_id/$filename"
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 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)
log "INFO" "Game Ended. Syncing..." : "${remote_size:=0}"
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 if [ "$local_size" -lt "$remote_size" ]; then
this_system_id=""; this_game_path=""; this_save_path=""; this_action=""; this_backup_path="" log "WARN" "Cloud save LARGER. Restoring..."
;; mkdir -p "$(dirname "$this_backup_path")"
esac 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 done