Skip to content

Commit

Permalink
update_timer_auto_splitter_settings
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKnauth committed May 11, 2024
1 parent 715825b commit ba19f96
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/auto_splitting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -665,15 +665,28 @@ 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
.send(Some(auto_splitter))
.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.
Expand Down Expand Up @@ -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<settings::Map> {
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
}
19 changes: 19 additions & 0 deletions src/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
9 changes: 9 additions & 0 deletions src/timing/timer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit ba19f96

Please sign in to comment.