feat: back up steam active_runtime and openvrpaths before replacing them; restore them from backup if found

This commit is contained in:
Gabriele Musco 2023-07-11 17:57:51 +02:00
parent 7744270310
commit 9f9201478e
No known key found for this signature in database
GPG key ID: 1068D795C80E51DE
3 changed files with 78 additions and 11 deletions

View file

@ -1,13 +1,14 @@
use crate::{
file_utils::{
deserialize_file, get_writer, get_xdg_config_dir, get_xdg_data_dir, set_file_radonly,
deserialize_file, get_backup_dir, get_writer, get_xdg_config_dir, get_xdg_data_dir,
set_file_radonly,
},
paths::SYSTEM_PREFIX,
profile::{Profile, XRServiceType},
};
use expect_dialog::ExpectDialog;
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::{fs::copy, path::Path};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ActiveRuntimeInnerRuntime {
@ -24,10 +25,7 @@ pub struct ActiveRuntime {
}
pub fn get_openxr_conf_dir() -> String {
format!(
"{config}/openxr",
config = get_xdg_config_dir()
)
format!("{config}/openxr", config = get_xdg_config_dir())
}
fn get_active_runtime_json_path() -> String {
@ -44,6 +42,30 @@ pub fn is_steam(active_runtime: &ActiveRuntime) -> bool {
}
}
fn get_backup_steam_active_runtime_path() -> String {
format!(
"{backup}/active_runtime.json.steam.bak",
backup = get_backup_dir()
)
}
fn get_backed_up_steam_active_runtime() -> Option<ActiveRuntime> {
get_active_runtime_from_path(&get_backup_steam_active_runtime_path())
}
fn backup_steam_active_runtime() {
let ar = get_current_active_runtime();
if ar.is_some() {
if is_steam(&ar.unwrap()) {
copy(
get_active_runtime_json_path(),
get_backup_steam_active_runtime_path(),
)
.expect_dialog("Failed to back up active_runtime.json");
}
}
}
fn get_active_runtime_from_path(path_s: &String) -> Option<ActiveRuntime> {
deserialize_file(path_s)
}
@ -65,6 +87,10 @@ pub fn dump_current_active_runtime(active_runtime: &ActiveRuntime) {
}
fn build_steam_active_runtime() -> ActiveRuntime {
let backup = get_backed_up_steam_active_runtime();
if backup.is_some() {
return backup.unwrap();
}
ActiveRuntime {
file_format_version: "1.0.0".into(),
runtime: ActiveRuntimeInnerRuntime {
@ -101,6 +127,7 @@ pub fn build_profile_active_runtime(profile: &Profile) -> ActiveRuntime {
}
pub fn set_current_active_runtime_to_profile(profile: &Profile) {
backup_steam_active_runtime();
let pfx = profile.clone().prefix;
let mut ar = build_profile_active_runtime(profile);
// hack: relativize libopenxr_monado.so path for system installs

View file

@ -1,11 +1,13 @@
use crate::{
file_utils::{
deserialize_file, get_writer, get_xdg_config_dir, get_xdg_data_dir, set_file_radonly,
deserialize_file, get_backup_dir, get_writer, get_xdg_config_dir, get_xdg_data_dir,
set_file_radonly,
},
profile::Profile,
};
use expect_dialog::ExpectDialog;
use serde::{Deserialize, Serialize};
use std::fs::copy;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct OpenVrPaths {
@ -18,10 +20,7 @@ pub struct OpenVrPaths {
}
pub fn get_openvr_conf_dir() -> String {
format!(
"{config}/openvr",
config = get_xdg_config_dir()
)
format!("{config}/openvr", config = get_xdg_config_dir())
}
fn get_openvrpaths_vrpath_path() -> String {
@ -42,6 +41,30 @@ pub fn is_steam(ovr_paths: &OpenVrPaths) -> bool {
.is_some()
}
fn get_backup_steam_openvrpaths_path() -> String {
format!(
"{backup}/openvrpaths.vrpath.steam.bak",
backup = get_backup_dir()
)
}
fn get_backed_up_steam_openvrpaths() -> Option<OpenVrPaths> {
get_openvrpaths_from_path(&get_backup_steam_openvrpaths_path())
}
fn backup_steam_openvrpaths() {
let openvrpaths = get_current_openvrpaths();
if openvrpaths.is_some() {
if is_steam(&openvrpaths.unwrap()) {
copy(
get_openvrpaths_vrpath_path(),
get_backup_steam_openvrpaths_path(),
)
.expect_dialog("Failed to back up openvrpaths.vrpath");
}
}
}
fn get_openvrpaths_from_path(path_s: &String) -> Option<OpenVrPaths> {
deserialize_file(path_s)
}
@ -62,6 +85,10 @@ pub fn dump_current_openvrpaths(ovr_paths: &OpenVrPaths) {
}
fn build_steam_openvrpaths() -> OpenVrPaths {
let backup = get_backed_up_steam_openvrpaths();
if backup.is_some() {
return backup.unwrap();
}
let datadir = get_xdg_data_dir();
OpenVrPaths {
config: vec![format!("{data}/Steam/config", data = datadir)],
@ -96,6 +123,7 @@ pub fn build_profile_openvrpaths(profile: &Profile) -> OpenVrPaths {
}
pub fn set_current_openvrpaths_to_profile(profile: &Profile) {
backup_steam_openvrpaths();
dump_current_openvrpaths(&build_profile_openvrpaths(profile))
}

View file

@ -102,6 +102,18 @@ pub fn get_cache_dir() -> String {
)
}
pub fn get_backup_dir() -> String {
let p_s = format!("{data}/backups", data = get_data_dir());
let p = Path::new(&p_s);
if !p.is_dir() {
if p.is_file() {
panic!("{} is a file but it should be a directory!", p.to_str().unwrap());
}
create_dir_all(p);
}
p.to_str().unwrap().to_string()
}
pub fn set_file_radonly(path_s: &String, readonly: bool) {
let path = Path::new(&path_s);
if !path.is_file() {