From d481f1314409c99a0ed48b006a8e9b53f6caa8a5 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Sat, 18 Nov 2023 17:16:58 -0500 Subject: [PATCH] Make settings variant map use u64 instead of strings since no longer restricted by JSON --- backend/src/main.rs | 2 +- backend/src/persist/file.rs | 6 +- backend/src/persist/general.rs | 2 +- backend/src/settings/general.rs | 2 +- backend/src/utility.rs | 34 +++++++++- default_settings.ron | 108 ++++++++++++++++++++++++++++++++ 6 files changed, 147 insertions(+), 7 deletions(-) create mode 100644 default_settings.ron diff --git a/backend/src/main.rs b/backend/src/main.rs index 0c94237..1c34fd4 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -76,7 +76,7 @@ fn main() -> Result<(), ()> { let mut loaded_settings = persist::FileJson::open(utility::settings_dir().join(DEFAULT_SETTINGS_FILE)) - .map(|mut file| file.variants.remove("0") + .map(|mut file| file.variants.remove(&0) .map(|settings| settings::Settings::from_json(DEFAULT_SETTINGS_NAME.into(), settings, DEFAULT_SETTINGS_FILE.into())) .unwrap_or_else(|| settings::Settings::system_default( DEFAULT_SETTINGS_FILE.into(), diff --git a/backend/src/persist/file.rs b/backend/src/persist/file.rs index be575f4..2750996 100644 --- a/backend/src/persist/file.rs +++ b/backend/src/persist/file.rs @@ -9,7 +9,7 @@ use super::SettingsJson; pub struct FileJson { pub version: u64, pub name: String, - pub variants: HashMap, + pub variants: HashMap, } impl FileJson { @@ -45,11 +45,11 @@ impl FileJson { let file = if path.exists() { let mut file = Self::open(path)?; - file.variants.insert(setting.variant.to_string(), setting); + file.variants.insert(setting.variant, setting); file } else { let mut setting_variants = HashMap::with_capacity(1); - setting_variants.insert(setting.variant.to_string(), setting); + setting_variants.insert(setting.variant, setting); Self { version: 0, name: given_name, diff --git a/backend/src/persist/general.rs b/backend/src/persist/general.rs index 9809403..d3f7ab4 100644 --- a/backend/src/persist/general.rs +++ b/backend/src/persist/general.rs @@ -20,7 +20,7 @@ impl Default for SettingsJson { fn default() -> Self { Self { version: 0, - name: crate::consts::DEFAULT_SETTINGS_NAME.to_owned(), + name: crate::consts::DEFAULT_SETTINGS_VARIANT_NAME.to_owned(), variant: 0, persistent: false, cpus: Vec::with_capacity(8), diff --git a/backend/src/settings/general.rs b/backend/src/settings/general.rs index a2f729f..b1d5cf8 100644 --- a/backend/src/settings/general.rs +++ b/backend/src/settings/general.rs @@ -178,7 +178,7 @@ impl Settings { } pub fn get_variant<'a>(settings_file: &'a FileJson, variant_id: u64, variant_name: String) -> Result<&'a SettingsJson, SettingError> { - if let Some(variant) = settings_file.variants.get(&variant_id.to_string()) { + if let Some(variant) = settings_file.variants.get(&variant_id) { Ok(variant) } else { Err(SettingError { diff --git a/backend/src/utility.rs b/backend/src/utility.rs index 595f17a..123ce3a 100644 --- a/backend/src/utility.rs +++ b/backend/src/utility.rs @@ -121,8 +121,40 @@ mod generate { limits: limits_core::json_v2::GenericBatteryLimit::default_for(limits_core::json_v2::BatteryLimitType::SteamDeck), }, }; - let output_file = std::fs::File::create("../limits_override.ron").unwrap(); + let output_file = std::fs::File::create(format!("../{}", crate::consts::LIMITS_OVERRIDE_FILE)).unwrap(); ron::ser::to_writer_pretty(output_file, &limits, crate::utility::ron_pretty_config()).unwrap(); } + + #[test] + fn generate_default_minimal_save_file() { + let mut mini_variants = std::collections::HashMap::with_capacity(2); + mini_variants.insert(0, crate::persist::SettingsJson { + version: 0, + name: crate::consts::DEFAULT_SETTINGS_VARIANT_NAME.to_owned(), + variant: 0, + persistent: false, + cpus: vec![crate::persist::CpuJson::default(); 8], + gpu: crate::persist::GpuJson::default(), + battery: crate::persist::BatteryJson::default(), + provider: None, + }); + mini_variants.insert(42, crate::persist::SettingsJson { + version: 0, + name: "FortySecondary".to_owned(), + variant: 42, + persistent: false, + cpus: vec![crate::persist::CpuJson::default(); 8], + gpu: crate::persist::GpuJson::default(), + battery: crate::persist::BatteryJson::default(), + provider: None, + }); + let savefile = crate::persist::FileJson { + version: 0, + name: crate::consts::DEFAULT_SETTINGS_NAME.to_owned(), + variants: mini_variants, + }; + let output_file = std::fs::File::create(format!("../{}", crate::consts::DEFAULT_SETTINGS_FILE)).unwrap(); + ron::ser::to_writer_pretty(output_file, &savefile, crate::utility::ron_pretty_config()).unwrap(); + } } diff --git a/default_settings.ron b/default_settings.ron new file mode 100644 index 0000000..fbc356a --- /dev/null +++ b/default_settings.ron @@ -0,0 +1,108 @@ +FileJson( + version: 0, + name: "Main", + variants: { + 0: SettingsJson( + version: 0, + name: "Primary", + variant: 0, + persistent: false, + cpus: [CpuJson( + online: true, + clock_limits: None, + governor: "schedutil", + ), CpuJson( + online: true, + clock_limits: None, + governor: "schedutil", + ), CpuJson( + online: true, + clock_limits: None, + governor: "schedutil", + ), CpuJson( + online: true, + clock_limits: None, + governor: "schedutil", + ), CpuJson( + online: true, + clock_limits: None, + governor: "schedutil", + ), CpuJson( + online: true, + clock_limits: None, + governor: "schedutil", + ), CpuJson( + online: true, + clock_limits: None, + governor: "schedutil", + ), CpuJson( + online: true, + clock_limits: None, + governor: "schedutil", + )], + gpu: GpuJson( + fast_ppt: None, + slow_ppt: None, + clock_limits: None, + slow_memory: false, + ), + battery: BatteryJson( + charge_rate: None, + charge_mode: None, + events: [], +), + provider: None, + ), + 42: SettingsJson( + version: 0, + name: "FortySecondary", + variant: 42, + persistent: false, + cpus: [CpuJson( + online: true, + clock_limits: None, + governor: "schedutil", + ), CpuJson( + online: true, + clock_limits: None, + governor: "schedutil", + ), CpuJson( + online: true, + clock_limits: None, + governor: "schedutil", + ), CpuJson( + online: true, + clock_limits: None, + governor: "schedutil", + ), CpuJson( + online: true, + clock_limits: None, + governor: "schedutil", + ), CpuJson( + online: true, + clock_limits: None, + governor: "schedutil", + ), CpuJson( + online: true, + clock_limits: None, + governor: "schedutil", + ), CpuJson( + online: true, + clock_limits: None, + governor: "schedutil", + )], + gpu: GpuJson( + fast_ppt: None, + slow_ppt: None, + clock_limits: None, + slow_memory: false, + ), + battery: BatteryJson( + charge_rate: None, + charge_mode: None, + events: [], +), + provider: None, + ), + }, +) \ No newline at end of file