Skip to content

Commit ba19f96

Browse files
committed
update_timer_auto_splitter_settings
1 parent 715825b commit ba19f96

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

src/auto_splitting/mod.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@
543543
544544
use crate::{
545545
platform::Arc,
546-
timing::{SharedTimer, TimerPhase},
546+
timing::{self, SharedTimer, TimerPhase},
547547
};
548548
pub use livesplit_auto_splitting::{settings, wasi_path};
549549
use livesplit_auto_splitting::{
@@ -665,15 +665,28 @@ impl Runtime {
665665
compiled_auto_splitter: &CompiledAutoSplitter,
666666
timer: SharedTimer,
667667
) -> Result<(), Error> {
668+
let settings_map = get_timer_auto_splitter_settings(&timer);
668669
let auto_splitter = compiled_auto_splitter
669-
.instantiate(Timer(timer), None, None)
670+
.instantiate(Timer(timer), settings_map, None)
670671
.map_err(|e| Error::LoadFailed { source: e })?;
671672

672673
self.auto_splitter
673674
.send(Some(auto_splitter))
674675
.map_err(|_| Error::ThreadStopped)
675676
}
676677

678+
/// Update's the timer's run's auto splitter settings from the Runtime.
679+
/// Call this before saving a timer into a splits file.
680+
pub fn update_timer_auto_splitter_settings(&self, timer: &mut timing::Timer) {
681+
let Some(settings_map) = self.settings_map() else {
682+
return;
683+
};
684+
if timer.run().parsed_auto_splitter_settings().is_none() && settings_map.is_empty() {
685+
return;
686+
}
687+
timer.set_auto_splitter_settings_map(settings_map);
688+
}
689+
677690
/// Unloads the current auto splitter. This will _not_ return an error if
678691
/// there isn't currently an auto splitter loaded, only if the runtime
679692
/// thread stops unexpectedly.
@@ -889,3 +902,13 @@ async fn watchdog(
889902
}
890903
}
891904
}
905+
906+
/// Gets the timer's run's auto splitter settings to instantiate with.
907+
fn get_timer_auto_splitter_settings(timer: &SharedTimer) -> Option<settings::Map> {
908+
let t = timer.read().ok()?;
909+
let run = t.run();
910+
if let Some(p) = run.parsed_auto_splitter_settings() {
911+
return Some(p.custom_settings.clone());
912+
}
913+
None
914+
}

src/run/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,25 @@ impl Run {
340340
&mut self.parsed_auto_splitter_settings
341341
}
342342

343+
/// Set the settings map in the parsed auto splitter settings.
344+
#[cfg(feature = "auto-splitting")]
345+
pub fn set_auto_splitter_settings_map(
346+
&mut self,
347+
settings_map: livesplit_auto_splitting::settings::Map,
348+
) {
349+
let p = self.parsed_auto_splitter_settings_mut();
350+
match p {
351+
None => {
352+
let mut a = AutoSplitterSettings::default();
353+
a.set_custom_settings(settings_map);
354+
*p = Some(a);
355+
}
356+
Some(a) => {
357+
a.set_custom_settings(settings_map);
358+
}
359+
}
360+
}
361+
343362
/// Accesses the [`LinkedLayout`] of this `Run`. If a
344363
/// [`Layout`](crate::Layout) is linked, it is supposed to be loaded to
345364
/// visualize the `Run`.

src/timing/timer/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,15 @@ impl Timer {
180180
&self.run
181181
}
182182

183+
/// Set the settings map in the parsed auto splitter settings.
184+
#[cfg(feature = "auto-splitting")]
185+
pub fn set_auto_splitter_settings_map(
186+
&mut self,
187+
settings_map: livesplit_auto_splitting::settings::Map,
188+
) {
189+
self.run.set_auto_splitter_settings_map(settings_map);
190+
}
191+
183192
/// Marks the Run as unmodified, so that it is known that all the changes
184193
/// have been saved.
185194
#[inline]

0 commit comments

Comments
 (0)