An end-to-end run of mine put its settings through updateSettings, which wrote them to the user's real config: output path in %TEMP%, a "probe" application, 30 fps. The next game recorded into %TEMP%, so it showed up in the history with its metadata and no video beside it. RECORD_DAEMON_HOME now moves the config directory, the recordings, the logs and the IPC pipe under one directory. The app reads the same variable, so a probe talks to the scratch daemon instead of the real one. Default paths are unchanged. The daemon also logs its config and recordings paths at startup, which is the first thing to look at when a recording has no video.
168 lines
5.5 KiB
Rust
168 lines
5.5 KiB
Rust
//! 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;
|
|
|
|
/// Set this to point the app and the daemon at a scratch directory, as scripts do.
|
|
pub const HOME_ENV: &str = "RECORD_DAEMON_HOME";
|
|
|
|
/// The daemon's data directory, shared with the daemon itself.
|
|
pub fn data_dir() -> Option<PathBuf> {
|
|
if let Some(home) = std::env::var_os(HOME_ENV).filter(|value| !value.is_empty()) {
|
|
return Some(PathBuf::from(home).join("data"));
|
|
}
|
|
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<PathBuf> {
|
|
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.
|
|
///
|
|
/// A checkout can hold both `target/debug` and `target/release`, one of them stale; the newest
|
|
/// build wins, so the app talks to the daemon that was built last ("cargo build" then "cargo
|
|
/// run", rather than a release build from another week).
|
|
pub fn find_binary() -> Option<PathBuf> {
|
|
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);
|
|
}
|
|
}
|
|
if let Some(found) = newest_build(exe_dir, name) {
|
|
return Some(found);
|
|
}
|
|
}
|
|
|
|
newest_build(&std::env::current_dir().ok()?, name)
|
|
}
|
|
|
|
/// The most recently built daemon in a checkout, walking up from `start`.
|
|
fn newest_build(start: &Path, name: &str) -> Option<PathBuf> {
|
|
let mut dir = start.to_path_buf();
|
|
for _ in 0..6 {
|
|
let candidates = [
|
|
dir.join("record-daemon/target/release").join(name),
|
|
dir.join("record-daemon/target/debug").join(name),
|
|
];
|
|
if let Some(found) = candidates
|
|
.into_iter()
|
|
.filter_map(|path| {
|
|
let modified = std::fs::metadata(&path).ok()?.modified().ok()?;
|
|
Some((modified, path))
|
|
})
|
|
.max_by_key(|(modified, _)| *modified)
|
|
.map(|(_, path)| path)
|
|
{
|
|
return Some(found);
|
|
}
|
|
if !dir.pop() {
|
|
break;
|
|
}
|
|
}
|
|
None
|
|
}
|
|
/// Start the daemon detached, so it outlives the app.
|
|
pub fn spawn() -> Result<PathBuf, String> {
|
|
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)
|
|
}
|
|
|
|
/// 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<String, String> {
|
|
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::<Vec<_>>().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;
|
|
}
|