From 893d2130c44707c69bf3c79ed5785ddcb3362bf8 Mon Sep 17 00:00:00 2001 From: AlexKnauth Date: Tue, 28 May 2024 20:36:21 -0400 Subject: [PATCH] trait TimerAutoSplitterSettings --- src/auto_splitting/mod.rs | 11 ++++++--- src/event.rs | 51 +++++++++++++++++++++++++++++++++++++++ src/run/mod.rs | 19 +++++++++++++++ src/timing/timer/mod.rs | 9 +++++++ 4 files changed, 86 insertions(+), 4 deletions(-) diff --git a/src/auto_splitting/mod.rs b/src/auto_splitting/mod.rs index 194a7e65..4791323d 100644 --- a/src/auto_splitting/mod.rs +++ b/src/auto_splitting/mod.rs @@ -542,7 +542,7 @@ //! - There is no threading. use crate::{ - event::{self, TimerQuery}, + event::{self, TimerAutoSplitterSettings, TimerQuery}, platform::Arc, timing::TimerPhase, }; @@ -599,13 +599,15 @@ impl Drop for Runtime { } } -impl Default for Runtime { +impl Default + for Runtime +{ fn default() -> Self { Self::new() } } -impl Runtime { +impl Runtime { /// Starts the runtime. Doesn't actually load an auto splitter until /// [`load`][Runtime::load] is called. pub fn new() -> Self { @@ -666,8 +668,9 @@ impl Runtime { compiled_auto_splitter: &CompiledAutoSplitter, timer: T, ) -> Result<(), Error> { + let settings_map = timer.get_auto_splitter_settings(); let auto_splitter = compiled_auto_splitter - .instantiate(Timer(timer), None, None) + .instantiate(Timer(timer), Some(settings_map), None) .map_err(|e| Error::LoadFailed { source: e })?; self.auto_splitter diff --git a/src/event.rs b/src/event.rs index e8c44fa4..b55d968b 100644 --- a/src/event.rs +++ b/src/event.rs @@ -90,6 +90,16 @@ pub trait TimerQuery { fn current_phase(&self) -> TimerPhase; } +#[cfg(feature = "auto-splitting")] +/// Getting and setting the settings map for auto splitter settings. +pub trait TimerAutoSplitterSettings { + /// Gets an initial settings map from the auto splitter settings. + fn get_auto_splitter_settings(&self) -> livesplit_auto_splitting::settings::Map; + + /// Set the settings map in the parsed auto splitter settings. + fn set_auto_splitter_settings(&mut self, settings_map: livesplit_auto_splitting::settings::Map); +} + #[cfg(feature = "std")] impl Sink for crate::SharedTimer { fn start(&self) { @@ -243,3 +253,44 @@ impl TimerQuery for Arc { TimerQuery::current_phase(&**self) } } + +#[cfg(feature = "auto-splitting")] +impl TimerAutoSplitterSettings for crate::timing::Timer { + fn get_auto_splitter_settings(&self) -> livesplit_auto_splitting::settings::Map { + let run = self.run(); + if let Some(p) = run.parsed_auto_splitter_settings() { + return p.custom_settings.clone(); + } + livesplit_auto_splitting::settings::Map::new() + } + + fn set_auto_splitter_settings( + &mut self, + settings_map: livesplit_auto_splitting::settings::Map, + ) { + if self.run().parsed_auto_splitter_settings().is_none() && settings_map.is_empty() { + return; + } + self.set_run_auto_splitter_settings(settings_map); + } +} + +#[cfg(feature = "auto-splitting")] +impl TimerAutoSplitterSettings for crate::SharedTimer { + fn get_auto_splitter_settings(&self) -> livesplit_auto_splitting::settings::Map { + let Ok(t) = self.read() else { + return livesplit_auto_splitting::settings::Map::new(); + }; + t.get_auto_splitter_settings() + } + + fn set_auto_splitter_settings( + &mut self, + settings_map: livesplit_auto_splitting::settings::Map, + ) { + let Ok(mut t) = self.write() else { + return; + }; + t.set_auto_splitter_settings(settings_map); + } +} diff --git a/src/run/mod.rs b/src/run/mod.rs index d62f33fa..6c2cc59b 100644 --- a/src/run/mod.rs +++ b/src/run/mod.rs @@ -349,6 +349,25 @@ impl Run { &mut self.parsed_auto_splitter_settings } + /// Set the settings map in the parsed auto splitter settings. + #[cfg(feature = "auto-splitting")] + pub fn set_auto_splitter_settings( + &mut self, + settings_map: livesplit_auto_splitting::settings::Map, + ) { + let p = self.parsed_auto_splitter_settings_mut(); + match p { + None => { + let mut a = AutoSplitterSettings::default(); + a.set_custom_settings(settings_map); + *p = Some(a); + } + Some(a) => { + a.set_custom_settings(settings_map); + } + } + } + /// Accesses the [`LinkedLayout`] of this `Run`. If a /// [`Layout`](crate::Layout) is linked, it is supposed to be loaded to /// visualize the `Run`. diff --git a/src/timing/timer/mod.rs b/src/timing/timer/mod.rs index 3ec5a621..ecb1feed 100644 --- a/src/timing/timer/mod.rs +++ b/src/timing/timer/mod.rs @@ -180,6 +180,15 @@ impl Timer { &self.run } + /// Set the settings map in the parsed auto splitter settings. + #[cfg(feature = "auto-splitting")] + pub fn set_run_auto_splitter_settings( + &mut self, + settings_map: livesplit_auto_splitting::settings::Map, + ) { + self.run.set_auto_splitter_settings(settings_map); + } + /// Marks the Run as unmodified, so that it is known that all the changes /// have been saved. #[inline]