Speedy yet?

This commit is contained in:
2026-02-27 21:19:38 -06:00
parent 0028a864cc
commit ddd9df5b36
2 changed files with 65 additions and 81 deletions

View File

@@ -1,51 +1,48 @@
#!/bin/ash #!/bin/ash
# shellcheck shell=dash # shellcheck shell=dash
# 1. Load Configs - Use absolute paths
. /recalbox/share/system/configs/savesync/savesync.conf . /recalbox/share/system/configs/savesync/savesync.conf
# 2. Setup Variables # Constants
EVENT_FILE="/tmp/es_state.inf" EVENT_FILE="/tmp/es_state.inf"
RESPONSE_TOPIC="recalbox/savesync/response"
TIMEOUT_SEC=10 TIMEOUT_SEC=10
log() { log() {
# Ensure port is 1883 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
mosquitto_pub -h 127.0.0.1 -p 1883 -q 0 -t "$LOG_TOPIC" -m "SaveLog=[$(date '+%H:%M:%S')] [$1] $2" 2>/dev/null
} }
# 3. Check Payload # 1. Validation
[ ! -s "$EVENT_FILE" ] && exit 0 [ ! -s "$EVENT_FILE" ] && exit 0
PAYLOAD=$(cat "$EVENT_FILE") PAYLOAD=$(cat "$EVENT_FILE")
# 4. Check Connection quickly (1 second max) # 2. Network Check (Short-circuit exit)
#nc -z -w 1 "$RCLONE_ENDPOINT" "$RCLONE_PORT" || { log "ERROR" "Offline"; exit 1; } nc -z -w 1 "$RCLONE_ENDPOINT" "$RCLONE_PORT" || {
log "DEBUG" "Checking connection to: ${RCLONE_ENDPOINT}:${RCLONE_PORT}" log "Offline: Skipping sync"
nc -zv -w 1 "$RCLONE_ENDPOINT" "$RCLONE_PORT" 2>&1 | log "DEBUG" "$(cat)" exit 0
}
# 5. Publish Request # 3. Publish Event
if mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "$PAYLOAD"; then mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "$PAYLOAD" || {
log "INFO" "Event published. Waiting for Daemon..." log "MQTT Fail"
else
log "ERROR" "MQTT Broker unreachable."
exit 1 exit 1
fi }
# 6. The Wait (With Timeout) # 4. Wait for Response (Crucial: Using -C 1 and the same Response Key)
# We listen for the daemon to say "SaveContinue=0"
( (
sleep "$TIMEOUT_SEC" sleep "$TIMEOUT_SEC"
mosquitto_pub -h 127.0.0.1 -t "$RESPONSE_TOPIC" -m "SaveSync=timeout" mosquitto_pub -h 127.0.0.1 -t "$RESPONSE_TOPIC" -m "SaveSync=timeout"
) & ) &
TIMER_PID=$! TIMER_PID=$!
# Wait for exactly 1 message 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) RESPONSE=$(mosquitto_sub -h 127.0.0.1 -p 1883 -t "$RESPONSE_TOPIC" -C 1)
kill "$TIMER_PID" 2>/dev/null kill "$TIMER_PID" 2>/dev/null
case "$RESPONSE" in case "$RESPONSE" in
*"timeout"*) log "ERROR" "Daemon timed out. Starting game with local save." ;; *"timeout"*) log "WARN: Daemon timed out. Starting game anyway." ;;
*) log "INFO" "Daemon finished. Proceeding." ;; *"0"*) log "INFO: Sync Complete. Launching game." ;;
*) log "INFO: Received response: $RESPONSE" ;;
esac esac
exit 0 exit 0

View File

@@ -1,88 +1,75 @@
#!/bin/ash #!/bin/ash
# shellcheck shell=ash # shellcheck shell=dash
# --- Configuration ---
. /recalbox/share/system/configs/savesync/savesync.conf . /recalbox/share/system/configs/savesync/savesync.conf
log() { log() {
local level="$1" mosquitto_pub -h 127.0.0.1 -p 1883 -q 0 -t "$LOG_TOPIC" -m "SaveLog=[$(date '+%H:%M:%S')] [DAEMON] $1" 2>/dev/null
local msg="$2"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
local log_line="[$timestamp] [$level] $msg"
if [ "$level" = "ERROR" ]; then
mkdir -p "$(dirname "$ERROR_LOG")"
printf "%s\n" "$log_line" >>"$ERROR_LOG"
fi
# FIX: Corrected port to 1883 and added -m
mosquitto_pub -h 127.0.0.1 -p 1883 -q 0 -t "$LOG_TOPIC" -m "SaveLog=$log_line" 2>/dev/null
[ "${DEBUG_MODE:-0}" -eq 1 ] && printf "%s\n" "$log_line"
} }
sleep 2 log "INFO" "--- Daemon Active ---"
log "INFO" "--- ES Event Daemon Started ---"
# --- Main Listener Loop --- mosquitto_sub -h 127.0.0.1 -p 1883 -t "$TOPIC" | while IFS="=" read -r key value; do
mosquitto_sub -h 127.0.0.1 -p 1883 -q 0 -t "$TOPIC" | while IFS="=" read -r key value; do # Clean input
# 1. Clean input val=$(echo "$value" | tr -d '\r')
value=$(echo "$value" | tr -d '\r')
case "$key" in case "$key" in
"SystemId") this_system_id="$value" ;; "SystemId") sid="$val" ;;
"GamePath") "GamePath")
this_game_path="$value" gp="$val"
# Replace 'roms' with 'saves' and change extension to .srm # Fast path transformation using sed
tmp_path="${value/roms/saves}" sp=$(echo "$val" | sed 's|roms|saves|; s|\.[^.]*$|.srm|')
this_save_path="${tmp_path%.*}.srm" bp=$(echo "$sp" | sed 's|saves|archives|')
# Replace 'saves' with 'archives' for backup
this_backup_path="${this_save_path/saves/archives}"
;; ;;
"Action") this_action="$value" ;; "Action") act="$val" ;;
"State") "State")
this_state="$value" st="$val"
if [ "$this_state" = "playing" ] && [ "$this_action" = "rungame" ]; then # Check if we have the full "Start Game" context
# PASS CURRENT VARS TO SUBSHELL if [ "$st" = "playing" ] && [ "$act" = "rungame" ]; then
# FORK to background, but keep current variables!
( (
# We use local copies inside the subshell to be safe log "INFO" "Syncing START for $(basename "$gp")"
sid="$this_system_id"
gp="$this_game_path"
sp="$this_save_path"
bp="$this_backup_path"
log "INFO" "Game Started: $(basename "$gp")" remote_f="$REMOTE_BASE/$sid/$(basename "$sp")"
filename=$(basename "$sp") loc_sz=$(stat -c %s "$sp" 2>/dev/null || echo 0)
remote_full="$REMOTE_BASE/$sid/$filename"
# rclone operations... # Fetch remote size
local_size=$(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)
remote_size=$(rclone lsjson "$remote_full" 2>/dev/null | grep -o '"Size":[0-9]*' | cut -d: -f2) : "${rem_sz:=0}"
: "${remote_size:=0}"
if [ "$local_size" -lt "$remote_size" ]; then if [ "$loc_sz" -lt "$rem_sz" ]; then
log "WARN" "Cloud save LARGER. Restoring..." log "WARN" "Cloud save larger. Restoring..."
mkdir -p "$(dirname "$bp")" mkdir -p "$(dirname "$bp")"
rclone copyto "$remote_full" "$sp" --backup-dir "$(dirname "$bp")" rclone copyto "$remote_f" "$sp" --backup-dir "$(dirname "$bp")"
else else
log "INFO" "Local save safe. Updating..." log "INFO" "Local save current. Updating..."
rclone update "$remote_full" "$sp" rclone update "$remote_f" "$sp"
fi fi
# FIX: Ensure you are sending the EXACT key the publisher is waiting for # SIGNAL SUCCESS (Key must match Publisher's expectation)
# 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"
mosquitto_pub -h 127.0.0.1 -p 1883 -t "$RESPONSE_TOPIC" -m "SaveSync=ok" ) &
elif [ "$st" = "endgame" ]; then
# Syncing on end - we don't necessarily need to block here
(
log "INFO" "Syncing END for $(basename "$sp")"
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"
) & ) &
log "DEBUG" "Sync backgrounded."
fi fi
# Variable resets happen here, safely outside the child process # Clear variables for next burst of events
this_system_id="" sid=""
this_game_path="" gp=""
this_save_path="" sp=""
this_action="" bp=""
this_backup_path="" act=""
st=""
;; ;;
esac esac
done done