speedy daemon, hopefully?
This commit is contained in:
@@ -33,52 +33,51 @@ mosquitto_sub -h 127.0.0.1 -p 1883 -q 0 -t "$TOPIC" | while IFS="=" read -r key
|
||||
"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|')
|
||||
# Replace 'roms' with 'saves' and change extension to .srm
|
||||
tmp_path="${value/roms/saves}"
|
||||
this_save_path="${tmp_path%.*}.srm"
|
||||
# Replace 'saves' with 'archives' for backup
|
||||
this_backup_path="${this_save_path/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
|
||||
# PASS CURRENT VARS TO SUBSHELL
|
||||
(
|
||||
log "INFO" "Game Started: $(basename "$this_game_path")"
|
||||
# We use local copies inside the subshell to be safe
|
||||
sid="$this_system_id"
|
||||
gp="$this_game_path"
|
||||
sp="$this_save_path"
|
||||
bp="$this_backup_path"
|
||||
|
||||
filename=$(basename "$this_save_path")
|
||||
remote_full="$REMOTE_BASE/$this_system_id/$filename"
|
||||
log "INFO" "Game Started: $(basename "$gp")"
|
||||
filename=$(basename "$sp")
|
||||
remote_full="$REMOTE_BASE/$sid/$filename"
|
||||
|
||||
local_size=$(stat -c %s "$this_save_path" 2>/dev/null || echo 0)
|
||||
# rclone operations...
|
||||
local_size=$(stat -c %s "$sp" 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 [ "$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")"
|
||||
mkdir -p "$(dirname "$bp")"
|
||||
rclone copyto "$remote_full" "$sp" --backup-dir "$(dirname "$bp")"
|
||||
else
|
||||
log "INFO" "Local save safe. Updating..."
|
||||
rclone update "$remote_full" "$this_save_path"
|
||||
rclone update "$remote_full" "$sp"
|
||||
fi
|
||||
|
||||
# Signal that sync is done
|
||||
mosquitto_pub -h 127.0.0.1 -p 1883 -t "$RESPONSE_TOPIC" -m "SaveContinue=0"
|
||||
# FIX: Ensure you are sending the EXACT key the publisher is waiting for
|
||||
# If your publisher is looking for "SaveSync", change "SaveContinue" below
|
||||
mosquitto_pub -h 127.0.0.1 -p 1883 -t "$RESPONSE_TOPIC" -m "SaveSync=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 "$RESPONSE_TOPIC" -m "SaveContinue=0"
|
||||
) &
|
||||
log "DEBUG" "Sync process backgrounded for End Game."
|
||||
log "DEBUG" "Sync backgrounded."
|
||||
fi
|
||||
|
||||
# Reset variables immediately so the loop is ready for the next line
|
||||
# Variable resets happen here, safely outside the child process
|
||||
this_system_id=""
|
||||
this_game_path=""
|
||||
this_save_path=""
|
||||
|
||||
Reference in New Issue
Block a user