80 lines
2.3 KiB
Plaintext
80 lines
2.3 KiB
Plaintext
#!/bin/ash
|
|
# shellcheck shell=dash
|
|
|
|
. /recalbox/share/system/configs/savesync/savesync.conf
|
|
|
|
log() {
|
|
mosquitto_pub -h 127.0.0.1 -p 1883 -q 0 -t "$LOG_TOPIC" -m "SaveLog=[$(date '+%H:%M:%S')] [DAEMON] [$1] $2" 2>/dev/null
|
|
}
|
|
|
|
log "INFO" "--- Daemon Active ---"
|
|
|
|
mosquitto_sub -h 127.0.0.1 -p 1883 -t "$TOPIC" | while IFS="=" read -r key value; do
|
|
# Clean input
|
|
val=$(echo "$value" | tr -d '\r')
|
|
|
|
case "$key" in
|
|
"SystemId") sid="$val" ;;
|
|
"GamePath")
|
|
gp="$val"
|
|
# Fast path transformation using sed
|
|
sp=$(echo "$val" | sed 's|roms|saves|; s|\.[^.]*$|.srm|')
|
|
bp=$(echo "$sp" | sed 's|saves|archives|')
|
|
;;
|
|
"Action") act="$val" ;;
|
|
"State")
|
|
st="$val"
|
|
|
|
# Check if we have the full "Start Game" context
|
|
if [ "$st" = "playing" ] && [ "$act" = "rungame" ]; then
|
|
# FORK to background, but keep current variables!
|
|
(
|
|
log "INFO" "Syncing START for $(basename "$gp")"
|
|
|
|
remote_f="$REMOTE_BASE/$sid/$(basename "$sp")"
|
|
loc_sz=$(stat -c %s "$sp" 2>/dev/null || echo 0)
|
|
|
|
# Fetch remote size
|
|
log "DEBUG" "rclone lsjson "$remote_f""
|
|
rem_sz=$(rclone lsjson "$remote_f" 2>/dev/null | grep -o '"Size":[0-9]*' | cut -d: -f2)
|
|
: "${rem_sz:=0}"
|
|
|
|
if [ "$loc_sz" -lt "$rem_sz" ]; then
|
|
log "WARN" "Cloud save larger. Restoring..."
|
|
mkdir -p "$(dirname "$bp")"
|
|
log "DEBUG" "rclone copyto "$remote_f" "$sp" --backup-dir "$(dirname "$bp")""
|
|
rclone copyto "$remote_f" "$sp" --backup-dir "$(dirname "$bp")"
|
|
else
|
|
log "INFO" "Local save current. Updating..."
|
|
log "DEBUG" "rclone update "$remote_f" "$sp""
|
|
rclone update "$remote_f" "$sp"
|
|
fi
|
|
|
|
# SIGNAL SUCCESS (Key must match Publisher's expectation)
|
|
mosquitto_pub -h 127.0.0.1 -p 1883 -t "$RESPONSE_TOPIC" -m "SaveSync=0"
|
|
) &
|
|
|
|
elif [ "$st" = "endgame" ]; then
|
|
# Syncing on end - we don't necessarily need to block here
|
|
(
|
|
log "INFO" "Syncing END for $(basename "$sp")"
|
|
log "DEBUG" "rclone update "$sp" "$REMOTE_BASE/$sid/""
|
|
rclone update "$sp" "$REMOTE_BASE/$sid/"
|
|
log "INFO" "Final Sync Done."
|
|
|
|
# Optional: Tell publisher we are done (though ES usually doesn't wait on endgame)
|
|
mosquitto_pub -h 127.0.0.1 -p 1883 -t "$RESPONSE_TOPIC" -m "SaveSync=0"
|
|
) &
|
|
fi
|
|
|
|
# Clear variables for next burst of events
|
|
sid=""
|
|
gp=""
|
|
sp=""
|
|
bp=""
|
|
act=""
|
|
st=""
|
|
;;
|
|
esac
|
|
done
|