Skip to content

Commit f606be3

Browse files
committed
trait TimerAutoSplitterSettings
1 parent 1e9d00d commit f606be3

File tree

4 files changed

+86
-4
lines changed

4 files changed

+86
-4
lines changed

src/auto_splitting/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@
542542
//! - There is no threading.
543543
544544
use crate::{
545-
event::{self, TimerQuery},
545+
event::{self, TimerAutoSplitterSettings, TimerQuery},
546546
platform::Arc,
547547
timing::TimerPhase,
548548
};
@@ -599,13 +599,15 @@ impl<T> Drop for Runtime<T> {
599599
}
600600
}
601601

602-
impl<T: event::Sink + TimerQuery + Send + 'static> Default for Runtime<T> {
602+
impl<T: event::Sink + TimerQuery + TimerAutoSplitterSettings + Send + 'static> Default
603+
for Runtime<T>
604+
{
603605
fn default() -> Self {
604606
Self::new()
605607
}
606608
}
607609

608-
impl<T: event::Sink + TimerQuery + Send + 'static> Runtime<T> {
610+
impl<T: event::Sink + TimerQuery + TimerAutoSplitterSettings + Send + 'static> Runtime<T> {
609611
/// Starts the runtime. Doesn't actually load an auto splitter until
610612
/// [`load`][Runtime::load] is called.
611613
pub fn new() -> Self {
@@ -666,8 +668,9 @@ impl<T: event::Sink + TimerQuery + Send + 'static> Runtime<T> {
666668
compiled_auto_splitter: &CompiledAutoSplitter,
667669
timer: T,
668670
) -> Result<(), Error> {
671+
let settings_map = timer.get_auto_splitter_settings();
669672
let auto_splitter = compiled_auto_splitter
670-
.instantiate(Timer(timer), None, None)
673+
.instantiate(Timer(timer), Some(settings_map), None)
671674
.map_err(|e| Error::LoadFailed { source: e })?;
672675

673676
self.auto_splitter

src/event.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ pub trait TimerQuery {
9090
fn current_phase(&self) -> TimerPhase;
9191
}
9292

93+
#[cfg(feature = "auto-splitting")]
94+
/// Getting and setting the settings map for auto splitter settings.
95+
pub trait TimerAutoSplitterSettings {
96+
/// Gets an initial settings map from the auto splitter settings.
97+
fn get_auto_splitter_settings(&self) -> livesplit_auto_splitting::settings::Map;
98+
99+
/// Set the settings map in the parsed auto splitter settings.
100+
fn set_auto_splitter_settings(&mut self, settings_map: livesplit_auto_splitting::settings::Map);
101+
}
102+
93103
#[cfg(feature = "std")]
94104
impl Sink for crate::SharedTimer {
95105
fn start(&self) {
@@ -243,3 +253,44 @@ impl<T: TimerQuery + ?Sized> TimerQuery for Arc<T> {
243253
TimerQuery::current_phase(&**self)
244254
}
245255
}
256+
257+
#[cfg(feature = "auto-splitting")]
258+
impl TimerAutoSplitterSettings for crate::timing::Timer {
259+
fn get_auto_splitter_settings(&self) -> livesplit_auto_splitting::settings::Map {
260+
let run = self.run();
261+
if let Some(p) = run.parsed_auto_splitter_settings() {
262+
return p.custom_settings.clone();
263+
}
264+
livesplit_auto_splitting::settings::Map::new()
265+
}
266+
267+
fn set_auto_splitter_settings(
268+
&mut self,
269+
settings_map: livesplit_auto_splitting::settings::Map,
270+
) {
271+
if self.run().parsed_auto_splitter_settings().is_none() && settings_map.is_empty() {
272+
return;
273+
}
274+
self.set_run_auto_splitter_settings(settings_map);
275+
}
276+
}
277+
278+
#[cfg(feature = "auto-splitting")]
279+
impl TimerAutoSplitterSettings for crate::SharedTimer {
280+
fn get_auto_splitter_settings(&self) -> livesplit_auto_splitting::settings::Map {
281+
let Ok(t) = self.read() else {
282+
return livesplit_auto_splitting::settings::Map::new();
283+
};
284+
t.get_auto_splitter_settings()
285+
}
286+
287+
fn set_auto_splitter_settings(
288+
&mut self,
289+
settings_map: livesplit_auto_splitting::settings::Map,
290+
) {
291+
let Ok(mut t) = self.write() else {
292+
return;
293+
};
294+
t.set_auto_splitter_settings(settings_map);
295+
}
296+
}

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(
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_run_auto_splitter_settings(
186+
&mut self,
187+
settings_map: livesplit_auto_splitting::settings::Map,
188+
) {
189+
self.run.set_auto_splitter_settings(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)