feat: functions to set profile or steam active runtime and openvrpaths

This commit is contained in:
Gabriele Musco 2023-06-15 18:03:33 +02:00
parent 9229d082ee
commit d743f300cd
No known key found for this signature in database
GPG key ID: 1068D795C80E51DE
2 changed files with 79 additions and 5 deletions

View file

@ -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<ActiveRuntime> {
}
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() {

View file

@ -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<OpenVrPaths> {
}
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};