feat: handle active runtime and openvrpaths replacement non fatal errors

This commit is contained in:
Gabriele Musco 2023-08-04 12:39:50 +00:00
parent 7a88489ef3
commit 7816cf436e
6 changed files with 87 additions and 33 deletions

8
src/checkerr.rs Normal file
View file

@ -0,0 +1,8 @@
#[macro_export]
macro_rules! checkerr {
($ex:expr) => {
if $ex.is_err() {
return Err(());
}
};
}

View file

@ -1,4 +1,5 @@
use crate::{
checkerr,
file_utils::{copy_file, deserialize_file, get_writer, set_file_readonly},
paths::{get_backup_dir, get_xdg_config_dir, get_xdg_data_dir, SYSTEM_PREFIX},
profile::{Profile, XRServiceType},
@ -69,13 +70,18 @@ pub fn get_current_active_runtime() -> Option<ActiveRuntime> {
get_active_runtime_from_path(&get_active_runtime_json_path())
}
fn dump_active_runtime_to_path(active_runtime: &ActiveRuntime, path_s: &String) {
fn dump_active_runtime_to_path(
active_runtime: &ActiveRuntime,
path_s: &String,
) -> Result<(), serde_json::Error> {
let writer = get_writer(path_s);
serde_json::to_writer_pretty(writer, active_runtime).expect("Unable to save active runtime");
serde_json::to_writer_pretty(writer, active_runtime)
}
pub fn dump_current_active_runtime(active_runtime: &ActiveRuntime) {
dump_active_runtime_to_path(active_runtime, &get_active_runtime_json_path());
pub fn dump_current_active_runtime(
active_runtime: &ActiveRuntime,
) -> Result<(), serde_json::Error> {
dump_active_runtime_to_path(active_runtime, &get_active_runtime_json_path())
}
fn build_steam_active_runtime() -> ActiveRuntime {
@ -96,9 +102,10 @@ fn build_steam_active_runtime() -> ActiveRuntime {
}
}
pub fn set_current_active_runtime_to_steam() {
set_file_readonly(&get_active_runtime_json_path(), false);
dump_current_active_runtime(&build_steam_active_runtime())
pub fn set_current_active_runtime_to_steam() -> Result<(), ()> {
checkerr!(set_file_readonly(&get_active_runtime_json_path(), false));
checkerr!(dump_current_active_runtime(&build_steam_active_runtime()));
Ok(())
}
pub fn build_profile_active_runtime(profile: &Profile) -> ActiveRuntime {
@ -119,9 +126,9 @@ pub fn build_profile_active_runtime(profile: &Profile) -> ActiveRuntime {
}
}
pub fn set_current_active_runtime_to_profile(profile: &Profile) {
pub fn set_current_active_runtime_to_profile(profile: &Profile) -> Result<(), ()> {
let dest = get_active_runtime_json_path();
set_file_readonly(&dest, false);
checkerr!(set_file_readonly(&dest, false));
backup_steam_active_runtime();
let pfx = profile.clone().prefix;
let mut ar = build_profile_active_runtime(profile);
@ -139,8 +146,9 @@ pub fn set_current_active_runtime_to_profile(profile: &Profile) {
rels = rel_chain.join("/")
);
}
dump_current_active_runtime(&ar);
set_file_readonly(&dest, true);
checkerr!(dump_current_active_runtime(&ar));
checkerr!(set_file_readonly(&dest, true));
Ok(())
}
#[cfg(test)]
@ -175,6 +183,7 @@ mod tests {
name: Some("SteamVR".into()),
},
};
dump_active_runtime_to_path(&ar, &"./target/testout/active_runtime.json.steamvr".into());
dump_active_runtime_to_path(&ar, &"./target/testout/active_runtime.json.steamvr".into())
.expect("could not dump active runtime to path");
}
}

View file

@ -1,4 +1,5 @@
use crate::{
checkerr,
file_utils::{copy_file, deserialize_file, get_writer, set_file_readonly},
paths::{get_backup_dir, get_xdg_config_dir, get_xdg_data_dir},
profile::Profile,
@ -68,12 +69,15 @@ pub fn get_current_openvrpaths() -> Option<OpenVrPaths> {
get_openvrpaths_from_path(&get_openvrpaths_vrpath_path())
}
fn dump_openvrpaths_to_path(ovr_paths: &OpenVrPaths, path_s: &String) {
fn dump_openvrpaths_to_path(
ovr_paths: &OpenVrPaths,
path_s: &String,
) -> Result<(), serde_json::Error> {
let writer = get_writer(path_s);
serde_json::to_writer_pretty(writer, ovr_paths).expect("Unable to save openvrpaths");
serde_json::to_writer_pretty(writer, ovr_paths)
}
pub fn dump_current_openvrpaths(ovr_paths: &OpenVrPaths) {
pub fn dump_current_openvrpaths(ovr_paths: &OpenVrPaths) -> Result<(), serde_json::Error> {
dump_openvrpaths_to_path(ovr_paths, &get_openvrpaths_vrpath_path())
}
@ -96,9 +100,10 @@ fn build_steam_openvrpaths() -> OpenVrPaths {
}
}
pub fn set_current_openvrpaths_to_steam() {
set_file_readonly(&get_openvrpaths_vrpath_path(), false);
dump_current_openvrpaths(&build_steam_openvrpaths())
pub fn set_current_openvrpaths_to_steam() -> Result<(), ()> {
checkerr!(set_file_readonly(&get_openvrpaths_vrpath_path(), false));
checkerr!(dump_current_openvrpaths(&build_steam_openvrpaths()));
Ok(())
}
pub fn build_profile_openvrpaths(profile: &Profile) -> OpenVrPaths {
@ -116,12 +121,13 @@ pub fn build_profile_openvrpaths(profile: &Profile) -> OpenVrPaths {
}
}
pub fn set_current_openvrpaths_to_profile(profile: &Profile) {
pub fn set_current_openvrpaths_to_profile(profile: &Profile) -> Result<(), ()> {
let dest = get_openvrpaths_vrpath_path();
set_file_readonly(&dest, false);
checkerr!(set_file_readonly(&dest, false));
backup_steam_openvrpaths();
dump_current_openvrpaths(&build_profile_openvrpaths(profile));
set_file_readonly(&dest, true);
checkerr!(dump_current_openvrpaths(&build_profile_openvrpaths(profile)));
checkerr!(set_file_readonly(&dest, true));
Ok(())
}
#[cfg(test)]
@ -152,5 +158,6 @@ mod tests {
version: 1,
};
dump_openvrpaths_to_path(&ovrp, &"./target/testout/openvrpaths.vrpath".into())
.expect("could not dump openvrpaths to path");
}
}

View file

@ -52,17 +52,17 @@ pub fn deserialize_file<T: serde::de::DeserializeOwned>(path_s: &String) -> Opti
}
}
pub fn set_file_readonly(path_s: &String, readonly: bool) {
pub fn set_file_readonly(path_s: &String, readonly: bool) -> Result<(), std::io::Error> {
let path = Path::new(&path_s);
if !path.is_file() {
println!("WARN: trying to set readonly on a file that does not exist");
return;
return Ok(());
}
let mut perms = fs::metadata(path)
.expect("Could not get metadata for file")
.permissions();
perms.set_readonly(readonly);
fs::set_permissions(path, perms).expect("Could not set permissions for file")
fs::set_permissions(path, perms)
}
pub fn setcap_cap_sys_nice_eip(file: String) {

View file

@ -33,6 +33,7 @@ pub mod runner_pipeline;
pub mod ui;
pub mod xr_devices;
pub mod build_tools;
pub mod checkerr;
fn restore_steam_xr_files() {
let active_runtime = get_current_active_runtime();

View file

@ -126,10 +126,23 @@ impl App {
}
pub fn start_xrservice(&mut self) {
self.set_inhibit_session(true);
let prof = self.get_selected_profile();
set_current_active_runtime_to_profile(&prof);
set_current_openvrpaths_to_profile(&prof);
if set_current_active_runtime_to_profile(&prof).is_err() {
alert(
"Failed to start XR Service",
Some("Error setting current active runtime to profile"),
Some(&self.app_win.clone().upcast::<gtk::Window>()),
);
return;
}
if set_current_openvrpaths_to_profile(&prof).is_err() {
alert(
"Failed to start XR Service",
Some("Error setting current openvrpaths file to profile"),
Some(&self.app_win.clone().upcast::<gtk::Window>()),
);
return;
};
self.devices_processed = match prof.xrservice_type {
XRServiceType::Monado => false,
XRServiceType::Wivrn => true, // no device from log in wivrn
@ -145,6 +158,7 @@ impl App {
true,
Some(self.get_selected_profile()),
));
self.set_inhibit_session(true);
}
Err(_) => {
alert(
@ -159,15 +173,31 @@ impl App {
};
}
pub fn restore_openxr_openvr_files(&self) {
if set_current_active_runtime_to_steam().is_err() {
alert(
"Could not restore Steam active runtime",
None,
Some(&self.app_win.clone().upcast::<gtk::Window>()),
);
}
if set_current_openvrpaths_to_steam().is_err() {
alert(
"Could not restore Steam openvrpaths",
None,
Some(&self.app_win.clone().upcast::<gtk::Window>()),
);
};
}
pub fn shutdown_xrservice(&mut self) {
self.set_inhibit_session(false);
if self.xrservice_runner.is_some() {
if self.xrservice_runner.as_mut().unwrap().status() == RunnerStatus::Running {
self.xrservice_runner.as_mut().unwrap().terminate();
}
}
set_current_active_runtime_to_steam();
set_current_openvrpaths_to_steam();
self.set_inhibit_session(false);
self.restore_openxr_openvr_files();
self.main_view
.sender()
.emit(MainViewMsg::XRServiceActiveChanged(false, None));
@ -231,8 +261,7 @@ impl SimpleComponent for App {
self.xrservice_runner.as_mut().unwrap().terminate();
}
}
set_current_active_runtime_to_steam();
set_current_openvrpaths_to_steam();
self.restore_openxr_openvr_files();
}
fn update(&mut self, message: Self::Input, sender: ComponentSender<Self>) {