//! Finding, starting and logging the record-daemon process. //! //! Ported from `tauri-app/src-tauri/src/daemon_ipc.rs`. The daemon binary ships next to the //! app; inside a checkout it is found in `record-daemon/target/{release,debug}`. use std::path::{Path, PathBuf}; use std::time::Duration; use crate::ipc::Client; /// The daemon's data directory, shared with the daemon itself. pub fn data_dir() -> Option { directories::ProjectDirs::from("com", "leaguerecorder", "record-daemon") .map(|dirs| dirs.data_dir().to_path_buf()) } /// Where recordings and timelines live. pub fn recordings_dir() -> PathBuf { data_dir() .map(|p| p.join("recordings")) .unwrap_or_else(|| PathBuf::from("./recordings")) } /// Where the daemon writes its log. pub fn log_file() -> Option { let dir = data_dir()?.join("logs"); std::fs::create_dir_all(&dir).ok()?; Some(dir.join("daemon.log")) } /// Find the record-daemon binary next to the app, in an installed `resources` folder, or in a /// development checkout. pub fn find_binary() -> Option { let name = if cfg!(windows) { "record-daemon.exe" } else { "record-daemon" }; if let Ok(exe) = std::env::current_exe() { let exe_dir = exe.parent()?; let beside = exe_dir.join(name); if beside.exists() { return Some(beside); } if let Some(parent) = exe_dir.parent() { let resources = parent.join("resources").join(name); if resources.exists() { return Some(resources); } } let mut dir = exe_dir.to_path_buf(); for _ in 0..6 { for suffix in ["record-daemon/target/release", "record-daemon/target/debug"] { let candidate = dir.join(suffix).join(name); if candidate.exists() { return Some(candidate); } } if !dir.pop() { break; } } } let cwd = std::env::current_dir().ok()?; let mut dir = cwd.as_path(); for _ in 0..5 { for suffix in ["record-daemon/target/release", "record-daemon/target/debug"] { let candidate = dir.join(suffix).join(name); if candidate.exists() { return Some(candidate); } } dir = dir.parent()?; } None } /// Start the daemon detached, so it outlives the app. pub fn spawn() -> Result { let binary = find_binary().ok_or_else(|| { "record-daemon was not found next to the app or in record-daemon/target".to_string() })?; let log = log_file().ok_or_else(|| "no data directory for the daemon log".to_string())?; let mut command = std::process::Command::new(&binary); command.arg("--foreground").arg("--log-file").arg(&log); if let Some(dir) = binary.parent() { // The daemon finds its bundled OBS plugin data relative to itself. command.current_dir(dir); } #[cfg(windows)] { use std::os::windows::process::CommandExt; // CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS command.creation_flags(0x0000_0200 | 0x0000_0008); } command .stdin(std::process::Stdio::null()) .stdout(std::process::Stdio::null()) .stderr(std::process::Stdio::null()); command .spawn() .map_err(|e| format!("cannot start record-daemon: {e}"))?; Ok(binary) } /// Is a daemon answering on the pipe? /// Wait up to `timeout` for the daemon to answer, starting it if it isn't running yet. pub async fn ensure_running() -> Result<(), String> { let client = Client::new(); if client.is_running().await { return Ok(()); } spawn()?; let deadline = std::time::Instant::now() + Duration::from_secs(30); loop { if client.is_running().await { return Ok(()); } if std::time::Instant::now() >= deadline { return Err("record-daemon did not answer within 30 s".to_string()); } tokio::time::sleep(Duration::from_millis(500)).await; } } /// The last `n` lines of the daemon log. pub fn read_logs(n: usize) -> Result { let path = log_file().ok_or_else(|| "no data directory for the daemon log".to_string())?; let text = std::fs::read_to_string(&path) .map_err(|e| format!("cannot read {}: {e}", path.display()))?; let lines: Vec<&str> = text.lines().rev().take(n).collect(); Ok(lines.into_iter().rev().collect::>().join("\n")) } /// The log path as text, for the About tab. pub fn log_path() -> String { log_file() .map(|p| p.display().to_string()) .unwrap_or_default() } /// Open a folder. pub fn open_folder(path: &Path) { #[cfg(windows)] { let _ = std::process::Command::new("explorer").arg(path).spawn(); } #[cfg(not(windows))] let _ = path; }