diff --git a/src/auto_splitting/mod.rs b/src/auto_splitting/mod.rs index 918c486b..2d2fa2a8 100644 --- a/src/auto_splitting/mod.rs +++ b/src/auto_splitting/mod.rs @@ -543,7 +543,7 @@ use crate::{ platform::Arc, - timing::{SharedTimer, TimerPhase}, + timing::{self, SharedTimer, TimerPhase}, }; pub use livesplit_auto_splitting::{settings, wasi_path}; use livesplit_auto_splitting::{ @@ -665,8 +665,9 @@ impl Runtime { compiled_auto_splitter: &CompiledAutoSplitter, timer: SharedTimer, ) -> Result<(), Error> { + let settings_map = get_timer_auto_splitter_settings(&timer); let auto_splitter = compiled_auto_splitter - .instantiate(Timer(timer), None, None) + .instantiate(Timer(timer), settings_map, None) .map_err(|e| Error::LoadFailed { source: e })?; self.auto_splitter @@ -674,6 +675,18 @@ impl Runtime { .map_err(|_| Error::ThreadStopped) } + /// Update's the timer's run's auto splitter settings from the Runtime. + /// Call this before saving a timer into a splits file. + pub fn update_timer_auto_splitter_settings(&self, timer: &mut timing::Timer) { + let Some(settings_map) = self.settings_map() else { + return; + }; + if timer.run().parsed_auto_splitter_settings().is_none() && settings_map.is_empty() { + return; + } + timer.set_auto_splitter_settings_map(settings_map); + } + /// Unloads the current auto splitter. This will _not_ return an error if /// there isn't currently an auto splitter loaded, only if the runtime /// thread stops unexpectedly. @@ -889,3 +902,13 @@ async fn watchdog( } } } + +/// Gets the timer's run's auto splitter settings to instantiate with. +fn get_timer_auto_splitter_settings(timer: &SharedTimer) -> Option { + let t = timer.read().ok()?; + let run = t.run(); + if let Some(p) = run.parsed_auto_splitter_settings() { + return Some(p.custom_settings.clone()); + } + None +} diff --git a/src/run/mod.rs b/src/run/mod.rs index 3d258e30..f028e6ae 100644 --- a/src/run/mod.rs +++ b/src/run/mod.rs @@ -340,6 +340,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_map( + &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 7da2bfe2..ef46019f 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_auto_splitter_settings_map( + &mut self, + settings_map: livesplit_auto_splitting::settings::Map, + ) { + self.run.set_auto_splitter_settings_map(settings_map); + } + /// Marks the Run as unmodified, so that it is known that all the changes /// have been saved. #[inline]