From d743f300cd3c8588e1143fa2d40865fbf8f6aca5 Mon Sep 17 00:00:00 2001 From: Gabriele Musco Date: Thu, 15 Jun 2023 18:03:33 +0200 Subject: [PATCH] feat: functions to set profile or steam active runtime and openvrpaths --- src/file_builders/active_runtime_json.rs | 48 ++++++++++++++++++++++-- src/file_builders/openvrpaths_vrpath.rs | 36 +++++++++++++++++- 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/src/file_builders/active_runtime_json.rs b/src/file_builders/active_runtime_json.rs index 4e301ce..6da1cb8 100644 --- a/src/file_builders/active_runtime_json.rs +++ b/src/file_builders/active_runtime_json.rs @@ -3,7 +3,7 @@ use std::{fs::File, io::BufReader, path::Path}; use expect_dialog::ExpectDialog; use serde::{Deserialize, Serialize}; -use crate::file_utils::{get_config_dir, get_writer}; +use crate::{file_utils::{get_config_dir, get_data_dir, get_writer, set_file_radonly}, profile::Profile}; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct ActiveRuntimeInnerRuntime { @@ -48,20 +48,60 @@ pub fn get_current_active_runtime() -> Option { } fn dump_active_runtime_to_path(active_runtime: ActiveRuntime, path_s: String) { + set_file_radonly(&path_s, false); let writer = get_writer(&path_s); // TODO: deal with write protection - serde_json::to_writer_pretty(writer, &active_runtime).expect_dialog("Unable to save active runtime"); + serde_json::to_writer_pretty(writer, &active_runtime) + .expect_dialog("Unable to save active runtime"); + set_file_radonly(&path_s, true); } 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 +fn build_steam_active_runtime() -> ActiveRuntime { + ActiveRuntime { + file_format_version: "1.0.0".into(), + runtime: ActiveRuntimeInnerRuntime { + valve_runtime_is_steamvr: Some(true), + library_path: format!( + "{data}/Steam/steamapps/common/SteamVR/bin/linux64/vrclient.so", + data = get_data_dir() + ), + name: Some("SteamVR".into()), + }, + } +} + +pub fn set_current_active_runtime_to_steam() { + dump_current_active_runtime(build_steam_active_runtime()) +} + +fn build_profile_active_runtime(profile: Profile) -> ActiveRuntime { + ActiveRuntime { + file_format_version: "1.0.0".into(), + runtime: ActiveRuntimeInnerRuntime { + name: None, + valve_runtime_is_steamvr: None, + library_path: format!( + "{prefix}/lib/libopenxr_monado.so", + prefix = profile.prefix + ), + }, + } +} + +pub fn set_current_active_runtime_to_profile(profile: Profile) { + dump_current_active_runtime(build_profile_active_runtime(profile)) +} #[cfg(test)] mod tests { - use super::{get_active_runtime_from_path, ActiveRuntime, ActiveRuntimeInnerRuntime, dump_active_runtime_to_path}; + use super::{ + dump_active_runtime_to_path, get_active_runtime_from_path, ActiveRuntime, + ActiveRuntimeInnerRuntime, + }; #[test] fn can_read_active_runtime_json_steamvr() { diff --git a/src/file_builders/openvrpaths_vrpath.rs b/src/file_builders/openvrpaths_vrpath.rs index ca3d796..1bb1de3 100644 --- a/src/file_builders/openvrpaths_vrpath.rs +++ b/src/file_builders/openvrpaths_vrpath.rs @@ -3,7 +3,7 @@ use std::{fs::File, io::BufReader, path::Path}; use expect_dialog::ExpectDialog; use serde::{Deserialize, Serialize}; -use crate::file_utils::{get_config_dir, get_writer}; +use crate::{file_utils::{get_config_dir, get_data_dir, get_writer, set_file_radonly}, profile::Profile}; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct OpenVrPaths { @@ -48,14 +48,48 @@ pub fn get_current_openvrpaths() -> Option { } fn dump_openvrpaths_to_path(ovr_paths: OpenVrPaths, path_s: String) { + set_file_radonly(&path_s, false); let writer = get_writer(&path_s); serde_json::to_writer_pretty(writer, &ovr_paths).expect_dialog("Unable to save openvrpaths"); + set_file_radonly(&path_s, true); } pub fn dump_current_openvrpaths(ovr_paths: OpenVrPaths) { dump_openvrpaths_to_path(ovr_paths, get_openvrpaths_vrpath_path()) } +fn build_steam_openvrpaths() -> OpenVrPaths { + let datadir = get_data_dir(); + OpenVrPaths { + config: vec![format!("{data}/Steam/config", data = datadir)], + external_drivers: None, + jsonid: "vrpathreg".into(), + log: vec![format!("{data}/Steam/logs", data = datadir)], + runtime: vec![format!("{data}/Steam/steamapps/common/SteamVR", data = datadir)], + version: 1, + } +} + +pub fn set_current_openvrpaths_to_steam() { + dump_current_openvrpaths(build_steam_openvrpaths()) +} + +fn build_profile_openvrpaths(profile: Profile) -> OpenVrPaths { + let datadir = get_data_dir(); + OpenVrPaths { + config: vec![format!("{data}/Steam/config", data = datadir)], + external_drivers: None, + jsonid: "vrpathreg".into(), + log: vec![format!("{data}/Steam/logs", data = datadir)], + runtime: vec![format!("{opencomp_dir}/build", opencomp_dir = profile.opencomposite_path)], + version: 1, + } +} + +pub fn set_current_openvrpaths_to_profile(profile: Profile) { + dump_current_openvrpaths(build_profile_openvrpaths(profile)) +} + #[cfg(test)] mod tests { use super::{dump_openvrpaths_to_path, get_openvrpaths_from_path, OpenVrPaths};