Skip to content

Commit

Permalink
trait TimerAutoSplitterSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKnauth committed Jun 14, 2024
1 parent 6999d42 commit 893d213
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/auto_splitting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@
//! - There is no threading.
use crate::{
event::{self, TimerQuery},
event::{self, TimerAutoSplitterSettings, TimerQuery},
platform::Arc,
timing::TimerPhase,
};
Expand Down Expand Up @@ -599,13 +599,15 @@ impl<T> Drop for Runtime<T> {
}
}

impl<T: event::Sink + TimerQuery + Send + 'static> Default for Runtime<T> {
impl<T: event::Sink + TimerQuery + TimerAutoSplitterSettings + Send + 'static> Default
for Runtime<T>
{
fn default() -> Self {
Self::new()
}
}

impl<T: event::Sink + TimerQuery + Send + 'static> Runtime<T> {
impl<T: event::Sink + TimerQuery + TimerAutoSplitterSettings + Send + 'static> Runtime<T> {
/// Starts the runtime. Doesn't actually load an auto splitter until
/// [`load`][Runtime::load] is called.
pub fn new() -> Self {
Expand Down Expand Up @@ -666,8 +668,9 @@ impl<T: event::Sink + TimerQuery + Send + 'static> Runtime<T> {
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
Expand Down
51 changes: 51 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -243,3 +253,44 @@ impl<T: TimerQuery + ?Sized> TimerQuery for Arc<T> {
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);
}
}
19 changes: 19 additions & 0 deletions src/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
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_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]
Expand Down

0 comments on commit 893d213

Please sign in to comment.