Fixed some issues
This commit is contained in:
@@ -21,7 +21,7 @@ log() {
|
|||||||
|
|
||||||
# Dispatch to the Central Logging Daemon via MQTT
|
# Dispatch to the Central Logging Daemon via MQTT
|
||||||
# We use -q 0 (fire and forget) so the game script doesn't wait
|
# 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
|
# Local debugging
|
||||||
[ "${DEBUG_MODE:-0}" -eq 1 ] && printf "%s\n" "$log_line"
|
[ "${DEBUG_MODE:-0}" -eq 1 ] && printf "%s\n" "$log_line"
|
||||||
|
|||||||
@@ -4,92 +4,77 @@
|
|||||||
# --- Configuration ---
|
# --- Configuration ---
|
||||||
. /recalbox/share/system/configs/savesync/savesync.conf
|
. /recalbox/share/system/configs/savesync/savesync.conf
|
||||||
|
|
||||||
|
|
||||||
# --- Logger Function ---
|
|
||||||
log() {
|
log() {
|
||||||
local level="$1"
|
local level="$1"
|
||||||
local msg="$2"
|
local msg="$2"
|
||||||
local timestamp
|
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||||
|
|
||||||
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
|
||||||
local log_line="[$timestamp] [$level] $msg"
|
local log_line="[$timestamp] [$level] $msg"
|
||||||
|
|
||||||
# Handle local emergency error log immediately
|
|
||||||
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
|
||||||
|
|
||||||
# Dispatch to the Central Logging Daemon via MQTT
|
# FIX: Corrected port to 1883 and added -m
|
||||||
# We use -q 0 (fire and forget) so the game script doesn't wait
|
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 -t "$LOG_TOPIC" -m "SaveLog=$log_line" 2>/dev/null
|
|
||||||
|
|
||||||
# Local debugging
|
|
||||||
[ "${DEBUG_MODE:-0}" -eq 1 ] && printf "%s\n" "$log_line"
|
[ "${DEBUG_MODE:-0}" -eq 1 ] && printf "%s\n" "$log_line"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# --- Sleep to ensure that the logger daemon has started ---
|
|
||||||
#
|
|
||||||
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 Carriage Returns from Windows-style line endings
|
# 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"
|
||||||
# Logic: Change .gba/.zip to .srm
|
# Corrected logic for paths
|
||||||
this_save_path="$(echo "${value%.*}.srm" | sed 's|roms|saves|')"
|
this_save_path=$(echo "$value" | sed 's|roms|saves|; s|\.[^.]*$|.srm|')
|
||||||
this_backup_path="$(dirname "$this_save_path" | sed 's|roms|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"
|
||||||
|
|
||||||
# --- TRIGGER: Only act when a game actually starts or ends ---
|
|
||||||
if [ "$this_state" = "playing" ] && [ "$this_action" = "rungame" ]; then
|
if [ "$this_state" = "playing" ] && [ "$this_action" = "rungame" ]; then
|
||||||
log "INFO" "Game Started: $(basename "$this_game_path")"
|
log "INFO" "Game Started: $(basename "$this_game_path")"
|
||||||
|
|
||||||
# Setup paths for rclone
|
|
||||||
filename=$(basename "$this_save_path")
|
filename=$(basename "$this_save_path")
|
||||||
remote_full="$REMOTE_BASE/$this_system_id/$filename"
|
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)
|
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=$(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
|
if [ "$local_size" -lt "$remote_size" ]; then
|
||||||
log "WARN" "Cloud save is LARGER ($remote_size vs $local_size). Restoring..."
|
log "WARN" "Cloud save LARGER. Restoring..."
|
||||||
mkdir -p "$(dirname "${this_backup_path}")"
|
mkdir -p "$(dirname "$this_backup_path")"
|
||||||
rclone copyto "$remote_full" "$this_save_path" --backup-dir "$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
|
else
|
||||||
log "INFO" "Local save is safe. Running update check..."
|
log "INFO" "Local save safe. Updating..."
|
||||||
rclone update "$remote_full" "$this_save_path"
|
rclone update "$remote_full" "$this_save_path"
|
||||||
fi
|
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/"
|
rclone update "$this_save_path" "$REMOTE_BASE/$this_system_id/"
|
||||||
log "INFO" "Sync Complete."
|
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
|
fi
|
||||||
|
|
||||||
# Reset variables for the next event block
|
# Reset variables
|
||||||
this_system_id=""; this_game_path=""; this_save_path=""; this_action=""
|
this_system_id=""; this_game_path=""; this_save_path=""; this_action=""; this_backup_path=""
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|||||||
Reference in New Issue
Block a user