FIFO pipes for speed
This commit is contained in:
48
old/pub_event[rungame,endgame](sync).ash.donotuse
Normal file
48
old/pub_event[rungame,endgame](sync).ash.donotuse
Normal file
@@ -0,0 +1,48 @@
|
||||
#!/bin/ash
|
||||
# shellcheck shell=dash
|
||||
|
||||
. /recalbox/share/system/configs/savesync/savesync.conf
|
||||
|
||||
# Constants
|
||||
EVENT_FILE="/tmp/es_state.inf"
|
||||
TIMEOUT_SEC=10
|
||||
|
||||
log() {
|
||||
mosquitto_pub -h 127.0.0.1 -p 1883 -q 0 -t "$LOG_TOPIC" -m "SaveLog=[$(date '+%H:%M:%S')] [PUB] $1" 2>/dev/null
|
||||
}
|
||||
|
||||
# 1. Validation
|
||||
[ ! -s "$EVENT_FILE" ] && exit 0
|
||||
PAYLOAD=$(cat "$EVENT_FILE")
|
||||
|
||||
# 2. Network Check (Short-circuit exit)
|
||||
nc -z -w 1 "$RCLONE_ENDPOINT" "$RCLONE_PORT" || {
|
||||
log "Offline: Skipping sync"
|
||||
exit 0
|
||||
}
|
||||
|
||||
# 3. Publish Event
|
||||
mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "$PAYLOAD" || {
|
||||
log "MQTT Fail"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# 4. Wait for Response (Crucial: Using -C 1 and the same Response Key)
|
||||
(
|
||||
sleep "$TIMEOUT_SEC"
|
||||
mosquitto_pub -h 127.0.0.1 -t "$RESPONSE_TOPIC" -m "SaveSync=timeout"
|
||||
) &
|
||||
TIMER_PID=$!
|
||||
|
||||
log "Waiting for Daemon response..."
|
||||
# Listen for the specific "SaveSync" key
|
||||
RESPONSE=$(mosquitto_sub -h 127.0.0.1 -p 1883 -t "$RESPONSE_TOPIC" -C 1)
|
||||
kill "$TIMER_PID" 2>/dev/null
|
||||
|
||||
case "$RESPONSE" in
|
||||
*"timeout"*) log "WARN: Daemon timed out. Starting game anyway." ;;
|
||||
*"0"*) log "INFO: Sync Complete. Launching game." ;;
|
||||
*) log "INFO: Received response: $RESPONSE" ;;
|
||||
esac
|
||||
|
||||
exit 0
|
||||
22
old/savesync-logger[start](permanent).ash.donotuse
Normal file
22
old/savesync-logger[start](permanent).ash.donotuse
Normal file
@@ -0,0 +1,22 @@
|
||||
#!/bin/ash
|
||||
#shellcheck shell=dash
|
||||
#
|
||||
. /recalbox/share/system/configs/savesync/savesync.conf
|
||||
|
||||
touch "$LOG_FILE"
|
||||
touch "$ERROR_FILE"
|
||||
|
||||
log_to_file() {
|
||||
printf "%s\n" "$1" >>"$LOG_FILE"
|
||||
}
|
||||
|
||||
# Subscribe and wait for log entries
|
||||
mosquitto_sub -h 127.0.0.1 -t "$LOG_TOPIC" | while read -r line; do
|
||||
# The line will look like: SaveLog=[2026-...] [INFO] ...
|
||||
# We strip the "SaveLog=" prefix
|
||||
msg_content="${line#SaveLog=}"
|
||||
|
||||
if [ -n "$msg_content" ]; then
|
||||
log_to_file "$msg_content"
|
||||
fi
|
||||
done
|
||||
11
old/savesync.conf.donotuse
Normal file
11
old/savesync.conf.donotuse
Normal file
@@ -0,0 +1,11 @@
|
||||
#!/bin/ash
|
||||
#shellcheck shell=dash
|
||||
export LOG_FILE="/recalbox/share/system/logs/savesync_monitor.log"
|
||||
export ERROR_FILE="/recalbox/share/system/logs/savesync_error.log"
|
||||
export DEBUG_MODE=1
|
||||
export REMOTE_BASE="saves:gamepi-tv"
|
||||
export TOPIC="/Recalbox/SaveSync/Event"
|
||||
export LOG_TOPIC="/Recalbox/SaveSync/Service"
|
||||
export RESPONSE_TOPIC="/Recalbox/SaveSync/Response"
|
||||
export RCLONE_ENDPOINT="192.168.88.11"
|
||||
export RCLONE_PORT="22"
|
||||
79
old/savesync[start](permanent).ash.donotuse
Normal file
79
old/savesync[start](permanent).ash.donotuse
Normal file
@@ -0,0 +1,79 @@
|
||||
#!/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
|
||||
67
old/savesync[start](permanent).ash.donotuse_
Normal file
67
old/savesync[start](permanent).ash.donotuse_
Normal file
@@ -0,0 +1,67 @@
|
||||
|
||||
# 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
|
||||
Reference in New Issue
Block a user