From c13f35a4a1a8efaac9a83528d9e4aaf2f30bf0ab Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Tue, 4 Oct 2022 20:49:02 -0400 Subject: [PATCH] Fix loading & saving non-persistent configs, hopefully --- backend/Cargo.lock | 2 +- backend/Cargo.toml | 2 +- backend/src/persist/general.rs | 13 +++++++++---- backend/src/settings/general.rs | 13 ++++++------- package.json | 4 ++-- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 223334d..d6a68ab 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -567,7 +567,7 @@ dependencies = [ [[package]] name = "powertools-rs" -version = "1.0.4" +version = "1.0.5" dependencies = [ "log", "serde", diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 769e94a..9178f52 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "powertools-rs" -version = "1.0.4" +version = "1.0.5" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/backend/src/persist/general.rs b/backend/src/persist/general.rs index da8e48c..f1183f7 100644 --- a/backend/src/persist/general.rs +++ b/backend/src/persist/general.rs @@ -31,11 +31,16 @@ impl Default for SettingsJson { impl SettingsJson { pub fn save>(&self, path: P) -> Result<(), JsonError> { let path = path.as_ref(); - if let Some(parent) = path.parent() { - std::fs::create_dir_all(parent).map_err(JsonError::Io)?; + if !self.persistent && path.exists() { + // remove settings file when persistence is turned off, to prevent it from be loaded next time. + std::fs::remove_file(path).map_err(JsonError::Io) + } else { + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent).map_err(JsonError::Io)?; + } + let mut file = std::fs::File::create(path).map_err(JsonError::Io)?; + serde_json::to_writer_pretty(&mut file, &self).map_err(JsonError::Serde) } - let mut file = std::fs::File::create(path).map_err(JsonError::Io)?; - serde_json::to_writer_pretty(&mut file, &self).map_err(JsonError::Serde) } pub fn open>(path: P) -> Result { diff --git a/backend/src/settings/general.rs b/backend/src/settings/general.rs index 96122d9..6249c9b 100644 --- a/backend/src/settings/general.rs +++ b/backend/src/settings/general.rs @@ -37,13 +37,6 @@ pub struct General { impl OnSet for General { fn on_set(&mut self) -> Result<(), SettingError> { - // remove settings file when persistence is turned off, to prevent it from be loaded next time. - if !self.persistent && self.path.exists() { - std::fs::remove_file(&self.path).map_err(|e| SettingError { - msg: format!("Failed to delete `{}`: {}", self.path.display(), e), - setting: SettingVariant::General, - })?; - } Ok(()) } } @@ -143,6 +136,12 @@ impl Settings { msg: e.to_string(), setting: SettingVariant::General, })?; + if !settings_json.persistent { + log::warn!("Loaded persistent config `{}` with persistent=false", json_path.display()); + general_lock.persistent = false; + general_lock.name = settings_json.name; + return Ok(false); + } let new_cpus = Self::convert_cpus(settings_json.cpus, settings_json.version); let new_gpu = Gpu::from_json(settings_json.gpu, settings_json.version); let new_battery = Battery::from_json(settings_json.battery, settings_json.version); diff --git a/package.json b/package.json index f1a707b..c35eabe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "PowerTools", - "version": "1.0.4", + "version": "1.0.5", "description": "Power tweaks for power users", "scripts": { "build": "shx rm -rf dist && rollup -c", @@ -39,7 +39,7 @@ "typescript": "^4.6.4" }, "dependencies": { - "decky-frontend-lib": "*", + "decky-frontend-lib": "3.*", "react-icons": "^4.4.0", "usdpl-front": "file:./src/usdpl_front" }