diff --git a/auto/Cargo.lock b/auto/Cargo.lock index d77333e..b34493f 100644 --- a/auto/Cargo.lock +++ b/auto/Cargo.lock @@ -1100,6 +1100,7 @@ dependencies = [ "lock_api", "once_cell", "parking_lot_core", + "serde", ] [[package]] diff --git a/interface/Cargo.lock b/interface/Cargo.lock index 85d0c52..64a03b0 100644 --- a/interface/Cargo.lock +++ b/interface/Cargo.lock @@ -1092,6 +1092,7 @@ dependencies = [ "lock_api", "once_cell", "parking_lot_core", + "serde", ] [[package]] diff --git a/interface/Cargo.toml b/interface/Cargo.toml index ad272d1..aed473a 100644 --- a/interface/Cargo.toml +++ b/interface/Cargo.toml @@ -23,4 +23,4 @@ chrono = "0.4.40" image = "0.25.5" libloading = "0.8.6" emath = "0.31.1" -dashmap = "6.1.0" +dashmap = { version = "6.1.0", features = ["serde"] } diff --git a/interface/src/auto.rs b/interface/src/auto.rs index dd81603..283884b 100644 --- a/interface/src/auto.rs +++ b/interface/src/auto.rs @@ -6,6 +6,8 @@ use eframe::Storage; use libloading::{Library, Symbol}; use tokio::sync::{self, broadcast, mpsc, watch}; +use crate::storage::StorageManager; + pub struct AutoConfig { turn_gain: f32, auto_fire_distance: f32, @@ -15,7 +17,7 @@ pub struct AutoConfig { pub struct AutoInterface { command_sender: Option>, data_receiver: watch::Receiver, - config: &'static dyn Storage, + config: StorageManager, enabled: bool, } @@ -45,9 +47,7 @@ impl AutoInterface { } pub fn enabled(&self) -> bool { self.enabled} pub fn conf(&self, key: &'static Configurable) -> f32 { - self.config.get_string(&key.name) - .map(|s| s.parse().ok()) - .flatten().unwrap_or(key.default) + self.config.load(key) } fn send_message(&self, message: String) {unimplemented!()} } diff --git a/interface/src/storage.rs b/interface/src/storage.rs index 7392d3b..75fdc5d 100644 --- a/interface/src/storage.rs +++ b/interface/src/storage.rs @@ -1,14 +1,42 @@ -use std::{cell::RefCell, collections::HashMap, path::PathBuf, sync::Arc}; +use std::{cell::RefCell, collections::HashMap, fs::File, io::{Read, Write}, path::PathBuf, sync::Arc}; use dashmap::DashMap; -struct StorageManager { - map: DashMap, +use crate::auto::Configurable; + +#[derive(Clone)] +pub struct StorageManager { + map: Arc>, storage: Arc, } impl StorageManager { - fn new(path: PathBuf) -> Self { - Self { map, storage } + pub fn new(path: PathBuf) -> Result { + let map = if let Ok(mut file) = File::open(path) { + let mut vec = Vec::new(); + file.read_to_end(&mut vec)?; + postcard::from_bytes(&vec)? + } else { + DashMap::new() + }; + + let map = Arc::new(map); + + Ok(Self { map, storage: Arc::new(path) }) + } + + pub fn save(&self) { + let map = postcard::to_stdvec_cobs(&self.map).unwrap(); + + let mut backing_file = File::open(self.storage.as_path()).unwrap(); + backing_file.write_all(&map).unwrap(); + } + + pub fn load(&self, setting: &'static Configurable) -> f32 { + self.map.get(setting.name).map(|v| *v).unwrap_or(setting.default) + } + + pub fn store(&self, setting: &'static Configurable, value: f32) { + self.map.insert(setting.name.to_owned(), value); } }