diff --git a/src/file_builders/active_runtime_json.rs b/src/file_builders/active_runtime_json.rs new file mode 100644 index 0000000..e6600ea --- /dev/null +++ b/src/file_builders/active_runtime_json.rs @@ -0,0 +1,92 @@ +use std::{fs::File, io::BufReader, path::Path}; + +use serde::{Deserialize, Serialize}; + +use crate::file_utils::{get_config_dir, get_writer}; + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct ActiveRuntimeInnerRuntime { + #[serde(rename = "VALVE_runtime_is_steamvr")] + pub valve_runtime_is_steamvr: Option, + pub library_path: String, + pub name: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct ActiveRuntime { + pub file_format_version: String, + pub runtime: ActiveRuntimeInnerRuntime, +} + +fn get_active_runtime_json_path() -> String { + format!( + "{config}/openxr/1/active_runtime.json", + config = get_config_dir() + ) +} + +pub fn is_steam(active_runtime: ActiveRuntime) -> bool { + match active_runtime.runtime.valve_runtime_is_steamvr { + Some(true) => true, + _ => false, + } +} + +fn get_active_runtime_from_path(path_s: String) -> Option { + let path = Path::new(&path_s); + if !(path.is_file() || path.is_symlink()) { + return None; + } + let fd = File::open(path).expect("Unable to open active_runtime.json"); + let reader = BufReader::new(fd); + Some(serde_json::from_reader(reader).expect("Failed to deserialize active_runtime.json")) +} + +pub fn get_current_active_runtime() -> Option { + get_active_runtime_from_path(get_active_runtime_json_path()) +} + +fn dump_active_runtime_to_path(active_runtime: ActiveRuntime, path_s: String) { + let writer = get_writer(&path_s); + // TODO: deal with write protection + serde_json::to_writer_pretty(writer, &active_runtime).expect("Unable to save active runtime"); +} + +pub fn dump_current_active_runtime(active_runtime: ActiveRuntime) { + dump_active_runtime_to_path(active_runtime, get_active_runtime_json_path()); +} + +// TODO: make library_path relative when dumping + +#[cfg(test)] +mod tests { + use super::{get_active_runtime_from_path, ActiveRuntime, ActiveRuntimeInnerRuntime, dump_active_runtime_to_path}; + + #[test] + fn can_read_active_runtime_json_steamvr() { + let ar = get_active_runtime_from_path("./test/files/active_runtime.json.steamvr".into()) + .unwrap(); + assert_eq!(ar.file_format_version, "1.0.0"); + assert!(ar.runtime.valve_runtime_is_steamvr.unwrap()); + assert_eq!( + ar.runtime.library_path, + "/home/user/.local/share/Steam/steamapps/common/SteamVR/bin/linux64/vrclient.so" + ); + assert_eq!(ar.runtime.name.unwrap(), "SteamVR"); + } + + #[test] + fn can_dump_active_runtime_json() { + let ar = ActiveRuntime { + file_format_version: "1.0.0".into(), + runtime: ActiveRuntimeInnerRuntime { + valve_runtime_is_steamvr: Some(true), + library_path: + "/home/user/.local/share/Steam/steamapps/common/SteamVR/bin/linux64/vrclient.so" + .into(), + name: Some("SteamVR".into()), + }, + }; + dump_active_runtime_to_path(ar, "./target/testout/active_runtime.json.steamvr".into()); + } +} diff --git a/src/file_builders/mod.rs b/src/file_builders/mod.rs new file mode 100644 index 0000000..fced075 --- /dev/null +++ b/src/file_builders/mod.rs @@ -0,0 +1,2 @@ +pub mod active_runtime_json; + diff --git a/src/main.rs b/src/main.rs index c67d710..9f805fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ pub mod profile; pub mod profiles; pub mod runner; pub mod ui; +pub mod file_builders; fn main() { MainWin::run(Settings::default()).expect("Application encountered an error"); diff --git a/test/files/active_runtime.json.steamvr b/test/files/active_runtime.json.steamvr new file mode 100644 index 0000000..57ed949 --- /dev/null +++ b/test/files/active_runtime.json.steamvr @@ -0,0 +1,9 @@ +{ + "file_format_version" : "1.0.0", + "runtime" : + { + "VALVE_runtime_is_steamvr" : true, + "library_path" : "/home/user/.local/share/Steam/steamapps/common/SteamVR/bin/linux64/vrclient.so", + "name" : "SteamVR" + } +}