Cleaned it up a bit, fixed shellcheck errors, formatting

This commit is contained in:
2026-02-27 22:25:29 -06:00
parent 3204b17632
commit 73e04e0bf5
3 changed files with 65 additions and 83 deletions

View File

@@ -1,4 +1,4 @@
#!/bin/ash #!/bin/ash
# #shellcheck shell=dash
PAYLOAD=$(cat /tmp/es_state.inf) PAYLOAD=$(cat /tmp/es_state.inf)
echo "EVENT:$PAYLOAD" >/tmp/savesync_pipe echo "EVENT:$PAYLOAD" >/tmp/savesync_pipe

3
savesync.conf Normal file
View File

@@ -0,0 +1,3 @@
#!/bin/ash
#shellcheck shell=dash
export REMOTE_BASE="saves:gamepi-tv"

View File

@@ -1,98 +1,77 @@
#!/bin/ash #!/bin/ash
# shellcheck shell=dash
. /recalbox/share/system/configs/savesync/savesync.conf . /recalbox/share/system/configs/savesync/savesync.conf
PIPE="/tmp/savesync_pipe" PIPE="/tmp/savesync_pipe"
LOG_FILE="/recalbox/share/system/logs/savesync.log" LOG_FILE="/recalbox/share/system/logs/savesync.log"
mkfifo "$PIPE" # Setup the pipe if it doesn't exist
[ -p "$PIPE" ] || mkfifo "$PIPE"
chmod 666 "$PIPE" chmod 666 "$PIPE"
# Function to write to disk
write_log() { write_log() {
printf "[%s] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$1" >>"$LOG_FILE" printf "[%s] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$1" >> "$LOG_FILE"
} }
# The listener loop write_log "--- Daemon Started ---"
while true; do while true; do
if read -r line <"$PIPE"; then # Read one line from the pipe
case "$line" in if read -r line < "$PIPE"; then
LOG:*) case "$line" in
# Remove the "LOG:" prefix and write LOG:*)
write_log "${line#LOG:}" write_log "${line#LOG:}"
;; ;;
EVENT:*) EVENT:*)
# Handle Game Events (forked to background) # Fork the event handling
( (
event_data="${line#EVENT:}"
PIPE="/tmp/savesync_pipe"
event_data="${line#EVENT:}" # Process the multi-line event data
# We use echo and a pipe to feed the while loop
echo "$event_data" | tr -d '\r' | while IFS="=" read -r key val; do
case "$key" in
"SystemId") sid="$val" ;;
"GamePath")
gp="$val"
sp=$(echo "$val" | sed 's|roms|saves|; s|\.[^.]*$|.srm|')
bp=$(echo "$sp" | sed 's|saves|archives|')
;;
"Action") act="$val" ;;
"State")
st="$val"
# Clean input if [ "$st" = "playing" ] && [ "$act" = "rungame" ]; then
$(echo "$event_data" | tr -d '\r') write_log "Sync START: $(basename "$gp")"
while IFS= read -r key val; do remote_f="$REMOTE_BASE/$sid/$(basename "$sp")"
case "$key" in loc_sz=$(stat -c %s "$sp" 2>/dev/null || echo 0)
"SystemId") sid="$val" ;; rem_sz=$(rclone lsjson "$remote_f" 2>/dev/null | grep -o '"Size":[0-9]*' | cut -d: -f2)
"GamePath") : "${rem_sz:=0}"
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 [ "$loc_sz" -lt "$rem_sz" ]; then
if [ "$st" = "playing" ] && [ "$act" = "rungame" ]; then write_log "Cloud save larger ($rem_sz). Restoring..."
# FORK to background, but keep current variables! mkdir -p "$(dirname "$bp")"
( rclone copyto "$remote_f" "$sp" --backup-dir "$(dirname "$bp")"
printf "%s %s\n" "LOG:" "Syncing START for $(basename "$gp")" >${PIPE} else
write_log "Local save current. Updating..."
rclone update "$remote_f" "$sp"
fi
# Signal back to the publisher via MQTT or a second Response Pipe
#mosquitto_pub -t "$RESPONSE_TOPIC" -m "SaveSync=0" 2>/dev/null
remote_f="$REMOTE_BASE/$sid/$(basename "$sp")" elif [ "$st" = "endgame" ]; then
loc_sz=$(stat -c %s "$sp" 2>/dev/null || echo 0) write_log "Sync END: $(basename "$sp")"
rclone update "$sp" "$REMOTE_BASE/$sid/"
# Fetch remote size write_log "Final Sync Done."
rem_sz=$(rclone lsjson "$remote_f" 2>/dev/null | grep -o '"Size":[0-9]*' | cut -d: -f2) #mosquitto_pub -t "$RESPONSE_TOPIC" -m "SaveSync=0" 2>/dev/null
: "${rem_sz:=0}" fi
;;
if [ "$loc_sz" -lt "$rem_sz" ]; then esac
printf "%s %s\n" "LOG:" "Cloud save larger. Restoring..." >${PIPE} done
mkdir -p "$(dirname "$bp")" ) &
rclone copyto "$remote_f" "$sp" --backup-dir "$(dirname "$bp")" ;;
else esac
printf "%s %s\n" "LOG:" "Local save current. Updating..."> >${PIPE} fi
rclone update "$remote_f" "$sp"
fi
# SIGNAL SUCCESS (Key must match Publisher's expectation)
printf "%s %s\n" "LOG:" "Save synced successfully" >${PIPE}
) &
elif [ "$st" = "endgame" ]; then
# Syncing on end - we don't necessarily need to block here
(
printf "%s %s\n" "LOG:" "Syncing END for $(basename "$sp")" >${PIPE}
rclone update "$sp" "$REMOTE_BASE/$sid/"
printf "%s %s\n" "LOG:" "Final Sync Done." >${PIPE}
) &
fi
# Clear variables for next burst of events
sid=""
gp=""
sp=""
bp=""
act=""
st=""
;;
esac
done<$(printf "%s\n" "$event_data" | tr -d '\r')
printf '%s\n' "LOG:Sync complete for $sp" >"$PIPE"
) &
;;
esac
fi
done done