GitCorner

Code

ossartcraftsoundcraft๐ŸŒ
a02d892f831f
use super::{SoundApp, midi_editor};

impl SoundApp {
    /// The open document was replaced outside [`Self::run`] (Recover, a session the browser read
    /// as bytes). Screenshot requests admitted for the old document must never receive the new
    /// one's pixels, so every replacement path bumps the epoch.
    pub(crate) fn document_replaced(&mut self) {
        self.doc_epoch = self.doc_epoch.wrapping_add(1);
        // Note indices and gestures belong to the old session's clips (#443); every document
        // replacement (run, Recover, a session the browser read) goes through here.
        self.midi = midi_editor::MidiEditorState::default();
        // The Output window shows a track of the old session: a track of the new one can have
        // the same id.
        self.ui.output_window = None;
    }

    /// The session is modified, or a plugin editor gesture still holds a state the session hasn't
    /// taken (see `editor_states_due`): both read the plugins' states before autosaving.
    pub(crate) fn has_unsaved_changes(&self) -> bool {
        self.engine.is_dirty() || !self.editor_states.is_empty()
    }

    /// Every two minutes, copy a modified session to the autosave folder (not while recording).
    /// Timed by the wall clock: egui's stands still while the window is hidden.
    pub(crate) fn autosave(&mut self) {
        let Some(dir) = self.autosave_dir.clone() else { return };
        let last = *self.last_autosave.get_or_insert_with(std::time::Instant::now);
        if last.elapsed().as_secs() < 120 || !self.has_unsaved_changes() || self.engine.transport.recording {
            return;
        }
        self.last_autosave = Some(std::time::Instant::now());
        self.capture_plugin_states();
        self.autosave_to(&dir);
    }

    /// Autosave a modified session within a second of every change, also while recording. The
    /// plugins' own state reaches the session every two minutes (`autosave`).
    pub(super) fn autosave_changes(&mut self) {
        if let Some(e) = self.autosaver.as_ref().and_then(soundcraft_engine::autosave::Autosaver::failure) {
            self.ui.status = crate::localized_format!("Autosave failed: {e}", e = e);
        }
        if self.autosave_dir.is_none() || self.engine.revision == self.autosaved_rev {
            return;
        }
        self.autosaved_rev = self.engine.revision;
        if !self.engine.is_dirty() {
            self.release_autosave(false);
        } else if let Some(root) = self.autosave_dir.clone() {
            self.queue_autosave(root);
        }
    }

    /// Hand the session to the autosaver for its folder under `root`.
    pub(super) fn queue_autosave(&mut self, root: std::path::PathBuf) {
        self.autosaved_rev = self.engine.revision;
        let w = self.autosaver.get_or_insert_with(|| soundcraft_engine::autosave::Autosaver::new(root));
        w.write(self.engine.session_arc(), self.engine.path.clone(), self.take_plan.clone());
    }

    /// Write the session to its own folder under `root` and wait for it; false when that failed.
    fn autosave_to(&mut self, root: &std::path::Path) -> bool {
        self.queue_autosave(root.to_path_buf());
        let Some(w) = &self.autosaver else { return false };
        let written = w.flush();
        if let Err(e) = &written {
            self.ui.status = crate::localized_format!("Autosave failed: {e}", e = e);
        }
        written.is_ok()
    }

    /// Autosave now, if the app has an autosave folder.
    fn write_autosave(&mut self) -> bool {
        self.autosave_dir.clone().is_some_and(|root| self.autosave_to(&root))
    }

    /// Done with this session's autosave folder: keep it for the next launch, or delete it.
    pub(super) fn release_autosave(&mut self, keep: bool) {
        if let Some(w) = &self.autosaver {
            w.close(keep);
        }
    }

    /// Done with the open session. Unsaved changes stay in its autosave folder for the next
    /// launch, unless the user quit with SoundCraft's Quit; a take still recording is finished
    /// and always stays.
    pub fn close_autosave(&mut self) {
        let recording = self.engine.transport.recording;
        if recording {
            self.stop_play();
        }
        let keep = self.has_unsaved_changes() && (recording || !self.quit_requested);
        if keep {
            self.capture_plugin_states();
            self.write_autosave();
        }
        self.release_autosave(keep);
        if let Some(w) = &self.autosaver {
            let _ = w.flush();
        }
    }

    /// Run part of a frame. If it panics, the session is autosaved (once per change) and the app
    /// carries on instead of quitting.
    pub fn guarded(&mut self, f: impl FnOnce(&mut SoundApp)) {
        if std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(self))).is_ok() || self.panic_rev == self.engine.revision {
            return;
        }
        self.panic_rev = self.engine.revision;
        let saved = self.engine.is_dirty() && self.write_autosave();
        self.ui.status = if saved {
            crate::localization::text(
                "SoundCraft hit an internal error (see the log). Your changes were autosaved: save them, then restart SoundCraft.",
            )
            .into()
        } else {
            crate::localization::text("SoundCraft hit an internal error (see the log). Restart SoundCraft to be safe.").into()
        };
    }

    /// Replace the open session with unsaved work an earlier run left behind.
    pub fn recover(&mut self, left: &soundcraft_engine::autosave::Leftover) {
        if self.engine.transport.recording {
            self.ui.status = crate::localization::text("Stop recording before recovering other work.").into();
            return;
        }
        self.close_autosave();
        match soundcraft_engine::autosave::recover(&mut self.engine, left) {
            Ok(missing) => {
                self.document_replaced();
                if self.write_autosave() {
                    soundcraft_engine::autosave::discard(left);
                }
                self.ui.status = match missing.len() {
                    0 => crate::localized_format!("Recovered โ€œ{name}โ€. Save it to keep the changes.", name = left.name),
                    n => crate::localized_format!(
                        "Recovered โ€œ{name}โ€, but {n} audio file(s) are missing. Save it to keep the changes.",
                        name = left.name,
                        n = n
                    ),
                };
            }
            Err(e) => self.ui.status = crate::localized_format!("Couldn't recover โ€œ{name}โ€: {e}", name = left.name, e = e),
        }
    }

    /// The most recent autosave, if any.
    pub fn latest_autosave(&self) -> Option<std::path::PathBuf> {
        let dir = self.autosave_dir.as_ref()?;
        let mut best: Option<(std::time::SystemTime, std::path::PathBuf)> = None;
        for d in std::fs::read_dir(dir).ok()?.flatten() {
            for f in std::fs::read_dir(d.path()).into_iter().flatten().flatten() {
                let p = f.path();
                if p.extension().and_then(std::ffi::OsStr::to_str) == Some("scraft")
                    && let Ok(m) = f.metadata().and_then(|m| m.modified())
                    && best.as_ref().is_none_or(|(t, _)| m > *t)
                {
                    best = Some((m, p));
                }
            }
        }
        best.map(|(_, p)| p)
    }
}