Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
228 changes: 214 additions & 14 deletions src-tauri/src/cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,23 +549,84 @@ fn first_time_paused(id: &str) -> bool {
true
}

/// What a game's saves are doing right now, for its page: "check", "up" or "down", then "idle".
/// The page asks saves_status for how it ended.
fn phase(app: &AppHandle, id: &str, what: &str) {
let _ = app.emit("save-sync", json!({"id": id, "phase": what}));
}

/// Says "idle" when the work it covers ends, on every way out of it (errors included).
struct Working {
app: AppHandle,
id: String,
}

impl Drop for Working {
fn drop(&mut self) {
phase(&self.app, &self.id, "idle");
}
}

fn working(app: &AppHandle, id: &str, what: &str) -> Working {
phase(app, id, what);
Working { app: app.clone(), id: id.to_string() }
}

/// The kind of trouble behind a failed upload, as saves_status reports it.
fn problem_kind(e: &Error) -> &'static str {
if paused() {
return "paused";
}
if is_full(e) {
return "full";
}
if e.to_string().starts_with("This save is too big") {
return "too-big-copy";
}
if matches!(e, Error::Net(_)) {
return "offline";
}
"error"
}

/// Remembers why this game's saves didn't go up, so its page can still say so after the toast is
/// gone. A conflict is kept on its own (upload sets it), not as a problem.
fn note_failure(dir: &Path, id: &str, e: &Error) {
if e.to_string() == SYNC_CONFLICT {
return;
}
let mut s = saves::state(dir, id);
s.problem = Some(saves::Problem { kind: problem_kind(e).into(), text: e.to_string(), at: crate::library::now() });
saves::set_state(dir, id, &s);
}

/// Puts one local copy up if the cloud hasn't changed since the last sync.
async fn upload(app: &AppHandle, id: &str, name: &str) -> Res<()> {
let r = upload_one(app, id, name).await;
if let Err(e) = &r {
note_failure(&app.state::<App>().dir, id, e);
}
r
}

async fn upload_one(app: &AppHandle, id: &str, name: &str) -> Res<()> {
let st = app.state::<App>();
let mut state = saves::state(&st.dir, id);
let copy = saves::manifest(&saves::dir_of(&st.dir, id).join(name))
.ok_or_else(|| Error::Msg("That copy is gone.".into()))?;
let (list, _) = cloud_list(app, id).await?;
let Some(top) = list.first() else { return upload_copy(app, id, name, 0).await };
if top.hash == copy.hash {
state.synced_hash = copy.hash;
state.mark_synced(&copy.hash, top.made, &top.device);
if state.pending.as_deref() == Some(name) {
state.pending = None;
}
saves::set_state(&st.dir, id, &state);
return Ok(());
}
if top.hash != state.synced_hash {
state.conflict = Some(top.v.clone());
saves::set_state(&st.dir, id, &state);
return Err(Error::Msg(SYNC_CONFLICT.into()));
}
upload_copy(app, id, name, top.made.saturating_add(1)).await
Expand All @@ -590,16 +651,21 @@ async fn upload_copy(app: &AppHandle, id: &str, name: &str, earliest: u64) -> Re
let body = tokio::fs::read(&path).await?;
// Too big to go up: it stays on this PC, and isn't tried again (the next copy is).
if body.len() > MAX_UPLOAD {
let why = "This save is too big for cloud saves (over 95 MB). It's kept on this PC.";
let mut s = saves::state(&st.dir, id);
if s.pending.as_deref() == Some(name) {
s.pending = None;
saves::set_state(&st.dir, id, &s);
}
// Kept here, not only said in a toast: with nothing queued, the game's page would
// otherwise show this copy as waiting to go up for good.
s.problem = Some(saves::Problem { kind: "too-big-copy".into(), text: why.into(), at: crate::library::now() });
saves::set_state(&st.dir, id, &s);
crate::trail::note(format!("Cloud saves: {id}'s copy is {} MB, too big to send", body.len() / (1024 * 1024)));
return Err(Error::Msg("This save is too big for cloud saves (over 95 MB). It's kept on this PC.".into()));
return Err(Error::Msg(why.into()));
}
let a = load(&st.dir).ok_or_else(|| Error::Msg("Sign in first.".into()))?;
let (made, played) = (man.made.max(earliest).to_string(), man.played.to_string());
let made_at = man.made.max(earliest);
let (made, played) = (made_at.to_string(), man.played.to_string());
let size = body.len() as u64;
let url = api();
let req = st
Expand All @@ -614,6 +680,7 @@ async fn upload_copy(app: &AppHandle, id: &str, name: &str, earliest: u64) -> Re
if !crate::net::forced() && crate::net::resting(&save_url("save-put"), true) {
return Err(Error::Net("Can't reach recomplauncher.com right now.".into()));
}
let _working = working(app, id, "up");
let r = match crate::net::send(req, &save_url("save-put"), true).await {
Ok(r) => r,
Err(e) => {
Expand All @@ -637,7 +704,7 @@ async fn upload_copy(app: &AppHandle, id: &str, name: &str, earliest: u64) -> Re
}
copy_went_up(id, name);
let mut s = saves::state(&st.dir, id);
s.synced_hash = man.hash;
s.mark_synced(&man.hash, made_at, &man.device);
// The copy waiting is this one or an older one: what's in the cloud now is newer, so it
// doesn't go up after it.
if s.pending.as_deref().map(|p| p == name || copy_time(p) <= copy_time(name)).unwrap_or(false) {
Expand Down Expand Up @@ -800,6 +867,7 @@ pub async fn after_session(app: &AppHandle, id: &str) -> Res<Option<saves::Copy>
async fn take_cloud(app: &AppHandle, id: &str, c: &CloudCopy) -> Res<usize> {
let st = app.state::<App>();
let a = load(&st.dir).ok_or_else(|| Error::Msg("Sign in first.".into()))?;
let _working = working(app, id, "down");
let url = api();
let req = st
.http
Expand All @@ -824,7 +892,7 @@ async fn take_cloud(app: &AppHandle, id: &str, c: &CloudCopy) -> Res<usize> {
let _ = tokio::fs::remove_file(&tmp).await;
let n = n??;
let mut s = saves::state(&st.dir, id);
s.synced_hash = c.hash.clone();
s.mark_synced(&c.hash, c.made, &c.device);
s.last_hash = c.hash.clone();
saves::set_state(&st.dir, id, &s);
Ok(n)
Expand Down Expand Up @@ -863,7 +931,24 @@ async fn upload_current(app: &AppHandle, id: &str, g: Installed, spots: Vec<Spot
.ok_or_else(|| Error::Msg("The saves changed. Try syncing again.".into()))
}).await??;
// A player can choose a save that was backed up before the current cloud copy.
upload_copy(app, id, &copy.name, after.saturating_add(1)).await
let r = upload_copy(app, id, &copy.name, after.saturating_add(1)).await;
if let Err(e) = &r {
note_failure(&app.state::<App>().dir, id, e);
}
r
}

/// A save place too big to look through stops this game's sync: kept so its page says so on
/// every launch, not only in the once-per-launch toast. Cleared by the first check that finds
/// every place readable again.
fn note_folder_too_big(dir: &Path, id: &str, why: Option<String>) {
let mut s = saves::state(dir, id);
let was = s.problem.as_ref().is_some_and(|p| p.kind == "too-big-folder");
if why.is_none() && !was {
return;
}
s.problem = why.map(|text| saves::Problem { kind: "too-big-folder".into(), text, at: crate::library::now() });
saves::set_state(dir, id, &s);
}

/// Before Play: what to do about the cloud.
Expand All @@ -888,6 +973,7 @@ async fn check_saves(app: AppHandle, id: String, manual: bool) -> Res<Value> {
}
let _busy = busy().await?;
let _reserved = reserve_sync(&st, &id)?;
let _working = working(&app, &id, "check");
let (list, _) = match cloud_list(&app, &id).await {
Ok(x) => x,
Err(Error::Net(_)) => return Ok(json!({"action": "offline"})),
Expand All @@ -907,6 +993,7 @@ async fn check_saves(app: AppHandle, id: String, manual: bool) -> Res<Value> {
// A cloud copy put in place now could be older than the saves there and replace them. That
// holds even when other places had saves: the copy writes into every place it holds.
if !too_big.is_empty() {
note_folder_too_big(&st.dir, &id, Some(SYNC_PAUSED.into()));
if manual {
return Err(saves::too_big_error(&g, &spots, &too_big));
}
Expand All @@ -915,6 +1002,7 @@ async fn check_saves(app: AppHandle, id: String, manual: bool) -> Res<Value> {
}
return Ok(json!({"action": "play"}));
}
note_folder_too_big(&st.dir, &id, None);
let s = saves::state(&st.dir, &id);
let Some(top) = top else {
if local.is_empty() {
Expand All @@ -924,9 +1012,11 @@ async fn check_saves(app: AppHandle, id: String, manual: bool) -> Res<Value> {
return Ok(json!({"action": "uploaded"}));
};
if top.hash == local {
if s.synced_hash != local || s.pending.is_some() {
// Already the same: a conflict or a problem noted before is over too.
let stale = s.synced_hash != local || s.pending.is_some() || s.conflict.is_some() || s.problem.is_some();
if stale {
let mut s = s;
s.synced_hash = local;
s.mark_synced(&local, top.made, &top.device);
s.pending = None;
saves::set_state(&st.dir, &id, &s);
}
Expand All @@ -944,6 +1034,10 @@ async fn check_saves(app: AppHandle, id: String, manual: bool) -> Res<Value> {
// After Play, the local changes go up at the end of the session.
return Ok(json!({"action": "play"}));
}
// Both changed: kept until the player picks, so the game's page can ask after this prompt.
let mut s = s;
s.conflict = Some(top.v.clone());
saves::set_state(&st.dir, &id, &s);
let newest = entries.iter().filter_map(|e| std::fs::metadata(&e.abs).ok()?.modified().ok()).max();
let when = newest.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok()).map(|d| d.as_secs()).unwrap_or(0);
Ok(json!({"action": "conflict", "local": {"when": when, "device": saves::device_name(), "played": g.played_secs, "hash": local}, "cloud": top}))
Expand Down Expand Up @@ -1052,11 +1146,7 @@ pub async fn saves_info(app: AppHandle, id: String) -> Res<Value> {
Err(_) => {}
}
}
// Copies on this PC that haven't gone up yet: the newest ones, back to the one that's in the cloud.
let waiting = match state.pending.as_deref() {
Some(p) => local.iter().take_while(|c| c.hash != state.synced_hash).filter(|c| c.when >= copy_time(p)).count().max(1),
None => 0,
};
let waiting = waiting_copies(&local, &state);
Ok(json!({
"spots": spots, "local": local, "cloud": cloud, "space": space, "offline": offline,
"cloudOn": g.cloud, "signedIn": signed_in(&st.dir), "synced": state.synced_hash, "pending": state.pending.is_some(),
Expand All @@ -1070,6 +1160,38 @@ pub async fn saves_info(app: AppHandle, id: String) -> Res<Value> {
/// Where players see whether the site is having trouble on our side.
const STATUS_PAGE: &str = "https://recomplauncher.com/status";

/// Copies on this PC that haven't gone up yet: the newest ones, back to the one that's in the cloud.
fn waiting_copies(local: &[saves::Copy], s: &saves::State) -> usize {
let Some(p) = s.pending.as_deref() else { return 0 };
local.iter().take_while(|c| c.hash != s.synced_hash).filter(|c| c.when >= copy_time(p)).count().max(1)
}

/// Where a game's cloud saves stand, for the badge on its page. Only what this PC knows: it
/// never asks the website, so the page can ask on every draw. The save-sync event says when it
/// changes. `state` is one word (saves::State::sync_state); `problem` says why when it's stuck.
#[tauri::command]
pub async fn saves_status(app: AppHandle, id: String) -> Res<Value> {
blocking(move || sync_status(&app, &id)).await?
}

fn sync_status(app: &AppHandle, id: &str) -> Res<Value> {
let st = app.state::<App>();
let g = game(&st, id)?;
let s = saves::state(&st.dir, id);
let signed = signed_in(&st.dir);
let local = saves::list(&st.dir, id);
let retry = s.pending.as_deref().and_then(|p| copy_wait_left(id, p));
Ok(json!({
"state": s.sync_state(g.cloud == Some(true), signed),
"cloudOn": g.cloud, "signedIn": signed,
"syncedAt": s.synced_at, "syncedMade": s.synced_made, "syncedDevice": s.synced_device,
"waiting": waiting_copies(&local, &s), "retryIn": retry, "full": full_now(), "paused": paused(),
"problem": s.problem, "conflict": s.conflict.is_some(), "newest": local.first(),
// Where the saves go. Only RecompStore today; named so another cloud can say so later.
"provider": "recompstore",
}))
}

fn upload_candidate(dir: &Path, id: &str, running: bool) -> Res<Option<saves::Copy>> {
let s = saves::state(dir, id);
let latest = saves::list(dir, id).into_iter().next();
Expand Down Expand Up @@ -1180,6 +1302,7 @@ fn overview(app: &AppHandle) -> Value {
out.push(json!({
"id": id, "copies": local.len(), "size": size, "newest": local.first(),
"cloudOn": g.cloud, "synced": state.synced_hash, "pending": state.pending.is_some(), "found": found,
"state": state.sync_state(g.cloud == Some(true), signed),
}));
}
let chosen = saves::chosen_root();
Expand Down Expand Up @@ -1769,3 +1892,80 @@ mod sync_tests {
std::fs::remove_dir_all(dir).unwrap();
}
}

#[cfg(test)]
mod status_tests {
use super::*;

fn tmp(n: &str) -> PathBuf {
let d = std::env::temp_dir().join(format!("rl-cloud-status-{n}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&d);
std::fs::create_dir_all(&d).unwrap();
d
}

#[test]
fn a_full_cloud_is_its_own_problem() {
assert_eq!(problem_kind(&Error::Msg(FULL_TEXT.into())), "full");
}

#[test]
fn a_copy_over_the_limit_is_too_big() {
let e = Error::Msg("This save is too big for cloud saves (over 95 MB). It's kept on this PC.".into());
assert_eq!(problem_kind(&e), "too-big-copy");
}

#[test]
fn no_connection_is_offline() {
assert_eq!(problem_kind(&Error::Net("Can't reach recomplauncher.com right now.".into())), "offline");
}

#[test]
fn any_other_refusal_is_an_error() {
assert_eq!(problem_kind(&Error::Msg("That copy is gone.".into())), "error");
}

#[test]
fn a_failed_upload_is_remembered() {
let d = tmp("failed");
note_failure(&d, "g", &Error::Net("Can't reach recomplauncher.com right now.".into()));
let p = saves::state(&d, "g").problem.unwrap();
assert_eq!(p.kind, "offline");
assert_eq!(p.text, "Can't reach recomplauncher.com right now.");
std::fs::remove_dir_all(d).unwrap();
}

#[test]
fn a_conflict_is_not_recorded_as_a_problem() {
let d = tmp("conflict");
note_failure(&d, "g", &Error::Msg(SYNC_CONFLICT.into()));
assert!(saves::state(&d, "g").problem.is_none());
std::fs::remove_dir_all(d).unwrap();
}

#[test]
fn a_readable_folder_again_clears_the_folder_problem() {
let d = tmp("folder");
note_folder_too_big(&d, "g", Some(SYNC_PAUSED.into()));
assert_eq!(saves::state(&d, "g").problem.unwrap().kind, "too-big-folder");
note_folder_too_big(&d, "g", None);
assert!(saves::state(&d, "g").problem.is_none());
std::fs::remove_dir_all(d).unwrap();
}

#[test]
fn a_readable_folder_keeps_other_problems() {
let d = tmp("folder-other");
note_failure(&d, "g", &Error::Msg(FULL_TEXT.into()));
note_folder_too_big(&d, "g", None);
assert_eq!(saves::state(&d, "g").problem.unwrap().kind, "full");
std::fs::remove_dir_all(d).unwrap();
}

#[test]
fn nothing_waits_without_a_queued_copy() {
let local = vec![saves::Copy { name: "200.zip".into(), when: 200, size: 1, files: 1, hash: "b".into(), device: String::new(), played: 0 }];
let s = saves::State { synced_hash: "a".into(), ..Default::default() };
assert_eq!(waiting_copies(&local, &s), 0);
}
}
2 changes: 1 addition & 1 deletion src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9867,7 +9867,7 @@ pub fn run() {
convert_copy,
convert_cancel, looks_info, set_look, look_picture, ui_data,
reset_settings, tray::tray_words, tray::quit_launcher, ach::ach_access, ach::ach_status, ach::ach_apply, ach::ach_profile, ach::ach_games, ach::ach_board, ach::ach_game_web, ach::ach_report, ach::ach_tracker_get, ach::ach_game, ach::ach_set_disc, ach::ach_set_folder, ach::ach_sound, ach::ach_log, ach::ach_peek, ach::ach_rule, ach::ach_reset, ach::ach_test_toast, ach::ach_toast_take, ach::ach_toast_done, cloud::account_start, cloud::account_poll, cloud::account_cancel, cloud::account_state, cloud::account_signout,
cloud::saves_check, cloud::saves_pick, cloud::saves_cloud, download_history, report_counts, play_reports, cloud::saves_info, cloud::saves_upload_now, cloud::cloud_retry, cloud::saves_overview, cloud::saves_backup, cloud::saves_restore, cloud::saves_share, cloud::saves_forget_found, cloud::saves_set_place, cloud::saves_add_place, cloud::saves_remove_place, cloud::saves_open, saves_set_root, saves_open_root, missing_files, missing_made, dll_check, dll_aside, gpu_choice, prefer_fast_gpu, copy_check, wine_tools, deck_scores, latest_counts, team_notes, trending, ig_info, ig_view, usage::notifies, usage::notify, usage::pc_specs, usage::recomp_request,
cloud::saves_check, cloud::saves_pick, cloud::saves_cloud, download_history, report_counts, play_reports, cloud::saves_info, cloud::saves_status, cloud::saves_upload_now, cloud::cloud_retry, cloud::saves_overview, cloud::saves_backup, cloud::saves_restore, cloud::saves_share, cloud::saves_forget_found, cloud::saves_set_place, cloud::saves_add_place, cloud::saves_remove_place, cloud::saves_open, saves_set_root, saves_open_root, missing_files, missing_made, dll_check, dll_aside, gpu_choice, prefer_fast_gpu, copy_check, wine_tools, deck_scores, latest_counts, team_notes, trending, ig_info, ig_view, usage::notifies, usage::notify, usage::pc_specs, usage::recomp_request,
adopt::adopt_scan, adopt::migrate_scan, adopt::migrate_pick, adopt::adopt_pick, adopt::adopt_add, adopt::adopt_check, adopt::set_project, usage::usage_hello, usage::usage_event, usage::data_ids, usage::reset_ids, ping::usage_open, ping::usage_mode, usage::run_report, cloud::inbox, cloud::inbox_done, cloud::betas, cloud::beta_join, cloud::beta_leave, launcher_notes, scan_info, devtools::dev_add, devtools::add_any, devtools::dev_check, devtools::dev_starter, fs_places, fs_list, fs_extract, gb_mods, gb_mod, gb_install
])
.build(tauri::generate_context!())
Expand Down
Loading
Loading