diff --git a/pub_event[rungame,endgame](sync).ash b/pub_event[rungame,endgame](sync).ash index 0ec42b2..c792c2a 100644 --- a/pub_event[rungame,endgame](sync).ash +++ b/pub_event[rungame,endgame](sync).ash @@ -21,7 +21,7 @@ log() { # 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 -t "$LOG_TOPIC" -m "SaveLog=$log_line" 2>/dev/null + 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" diff --git a/savesync[start](permanent).ash b/savesync[start](permanent).ash index 736e8a4..f22f938 100644 --- a/savesync[start](permanent).ash +++ b/savesync[start](permanent).ash @@ -4,92 +4,77 @@ # --- Configuration --- . /recalbox/share/system/configs/savesync/savesync.conf - -# --- Logger Function --- log() { local level="$1" local msg="$2" - local timestamp - - timestamp=$(date '+%Y-%m-%d %H:%M:%S') + local timestamp=$(date '+%Y-%m-%d %H:%M:%S') local log_line="[$timestamp] [$level] $msg" - # Handle local emergency error log immediately if [ "$level" = "ERROR" ]; then mkdir -p "$(dirname "$ERROR_LOG")" printf "%s\n" "$log_line" >> "$ERROR_LOG" fi - # 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 -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 - # Local debugging [ "${DEBUG_MODE:-0}" -eq 1 ] && printf "%s\n" "$log_line" } - -# --- Sleep to ensure that the logger daemon has started --- -# 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 Carriage Returns from Windows-style line endings + # 1. Clean input value=$(echo "$value" | tr -d '\r') case "$key" in "SystemId") this_system_id="$value" ;; "GamePath") this_game_path="$value" - # Logic: Change .gba/.zip to .srm - this_save_path="$(echo "${value%.*}.srm" | sed 's|roms|saves|')" - this_backup_path="$(dirname "$this_save_path" | sed 's|roms|archives|')" + # 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" - # --- TRIGGER: Only act when a game actually starts or ends --- if [ "$this_state" = "playing" ] && [ "$this_action" = "rungame" ]; then log "INFO" "Game Started: $(basename "$this_game_path")" - # Setup paths for rclone filename=$(basename "$this_save_path") remote_full="$REMOTE_BASE/$this_system_id/$filename" - # 2. Safety Valve: Compare Sizes 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}" # Default to 0 if empty + : "${remote_size:=0}" if [ "$local_size" -lt "$remote_size" ]; then - log "WARN" "Cloud save is LARGER ($remote_size vs $local_size). Restoring..." - mkdir -p "$(dirname "${this_backup_path}")" - rclone copyto "$remote_full" "$this_save_path" --backup-dir "$this_backup_path" + log "WARN" "Cloud save LARGER. Restoring..." + mkdir -p "$(dirname "$this_backup_path")" + # Fixed: backup-dir needs to be a directory + rclone copyto "$remote_full" "$this_save_path" --backup-dir "$(dirname "$this_backup_path")" else - log "INFO" "Local save is safe. Running update check..." + log "INFO" "Local save safe. Updating..." rclone update "$remote_full" "$this_save_path" fi - - mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" "SaveContinue=0" - elif [ "$this_state" = "endgame" ]; then - log "INFO" "Game Ended. Backing up save..." - filename=$(basename "$this_save_path") - remote_full="$REMOTE_BASE/$this_system_id/$filename" - # Push the local save to the cloud if it's newer + # FIX: Added -m flag + mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "SaveContinue=0" + + 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" "SaveContinue=0" + + mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "SaveContinue=0" fi - # Reset variables for the next event block - this_system_id=""; this_game_path=""; this_save_path=""; this_action="" + # Reset variables + this_system_id=""; this_game_path=""; this_save_path=""; this_action=""; this_backup_path="" ;; esac done