Back to basics

This commit is contained in:
2026-02-28 00:07:56 -06:00
parent 2952bd6c33
commit 6153efb3dd
4 changed files with 83 additions and 81 deletions

View File

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

View File

@@ -0,0 +1,58 @@
#!/bin/ash
#shellcheck shell=dash
#
PAYLOAD=$(cat /tmp/es_state.inf)
REMOTE_BASE="saves:gamepi-tv"
PIPE="/tmp/savesync_pipe"
log() {
# $1 = level
# $2 = message
printf "LOG:[%s] %s" "$1" "$2" >"$PIPE"
}
log "INFO" "Event received: $PAYLOAD"
# Process the multi-line event data
# We use echo and a pipe to feed the while loop
echo "$PAYLOAD" | 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"
if [ "$st" = "playing" ] && [ "$act" = "rungame" ]; then
log "INFO" "Sync START: $(basename "$gp")"
remote_f="$REMOTE_BASE/$sid/$(basename "$sp")"
loc_sz=$(stat -c %s "$sp" 2>/dev/null || echo 0)
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 ($rem_sz). 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
elif [ "$st" = "endgame" ]; then
log "INFO" "Sync END: $(basename "$sp")"
log "DEBUG" "rclone update $sp $REMOTE_BASE/$sid/"
rclone update "$sp" "$REMOTE_BASE/$sid/"
log "INFO" "Final Sync Done."
#mosquitto_pub -t "$RESPONSE_TOPIC" -m "SaveSync=0" 2>/dev/null
fi
;;
esac
done

View File

@@ -1,77 +0,0 @@
#!/bin/ash
# shellcheck shell=dash
export LOG_FILE="/recalbox/share/system/logs/savesync.log"
export REMOTE_BASE="saves:gamepi-tv"
export PIPE="/tmp/savesync_pipe"
export LOG_FILE="/recalbox/share/system/logs/savesync.log"
# Setup the pipe if it doesn't exist
[ -p "$PIPE" ] || mkfifo "$PIPE"
chmod 666 "$PIPE"
write_log() {
printf "[%s] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$1" >>"$LOG_FILE"
}
write_log "--- Daemon Started ---"
while true; do
# Read one line from the pipe
if read -r line <"$PIPE"; then
case "$line" in
LOG:*)
write_log "${line#LOG:}"
;;
EVENT:*)
# Fork the event handling
(
event_data="${line#EVENT:}"
write_log "Event received"
# 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"
if [ "$st" = "playing" ] && [ "$act" = "rungame" ]; then
write_log "Sync START: $(basename "$gp")"
remote_f="$REMOTE_BASE/$sid/$(basename "$sp")"
loc_sz=$(stat -c %s "$sp" 2>/dev/null || echo 0)
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
write_log "Cloud save larger ($rem_sz). Restoring..."
mkdir -p "$(dirname "$bp")"
rclone copyto "$remote_f" "$sp" --backup-dir "$(dirname "$bp")"
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
elif [ "$st" = "endgame" ]; then
write_log "Sync END: $(basename "$sp")"
rclone update "$sp" "$REMOTE_BASE/$sid/"
write_log "Final Sync Done."
#mosquitto_pub -t "$RESPONSE_TOPIC" -m "SaveSync=0" 2>/dev/null
fi
;;
esac
done
) &
;;
esac
fi
done

View File

@@ -0,0 +1,25 @@
#!/bin/ash
# shellcheck shell=dash
export LOG_FILE="/recalbox/share/system/logs/savesync.log"
export PIPE="/tmp/savesync_pipe"
# Setup the pipe if it doesn't exist
[ -p "$PIPE" ] || mkfifo "$PIPE"
chmod 666 "$PIPE"
write_log() {
printf "[%s] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$1" >>"$LOG_FILE"
}
write_log "--- Logger Started ---"
while true; do
# Read one line from the pipe
if read -r line <"$PIPE"; then
case "$line" in
LOG:*)
write_log "${line#LOG:}"
;;
esac
fi
done