Skip to content

Commit 6981cb3

Browse files
committed
trait TimerAutoSplitterSettings
1 parent 54c9f2b commit 6981cb3

4 files changed

Lines changed: 86 additions & 4 deletions

File tree

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

608-
impl<T: event::CommandSink + TimerQuery + Send + 'static> Runtime<T> {
610+
impl<T: event::CommandSink + 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::CommandSink + 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
@@ -285,6 +285,16 @@ pub trait TimerQuery {
285285
fn get_timer(&self) -> Self::Guard<'_>;
286286
}
287287

288+
#[cfg(feature = "auto-splitting")]
289+
/// Getting and setting the settings map for auto splitter settings.
290+
pub trait TimerAutoSplitterSettings {
291+
/// Gets an initial settings map from the auto splitter settings.
292+
fn get_auto_splitter_settings(&self) -> livesplit_auto_splitting::settings::Map;
293+
294+
/// Set the settings map in the parsed auto splitter settings.
295+
fn set_auto_splitter_settings(&mut self, settings_map: livesplit_auto_splitting::settings::Map);
296+
}
297+
288298
#[cfg(feature = "std")]
289299
impl CommandSink for crate::SharedTimer {
290300
fn start(&self) -> impl Future<Output = Result> + 'static {
@@ -507,3 +517,44 @@ impl<T: TimerQuery + ?Sized> TimerQuery for Arc<T> {
507517
TimerQuery::get_timer(&**self)
508518
}
509519
}
520+
521+
#[cfg(feature = "auto-splitting")]
522+
impl TimerAutoSplitterSettings for crate::timing::Timer {
523+
fn get_auto_splitter_settings(&self) -> livesplit_auto_splitting::settings::Map {
524+
let run = self.run();
525+
if let Some(p) = run.parsed_auto_splitter_settings() {
526+
return p.custom_settings.clone();
527+
}
528+
livesplit_auto_splitting::settings::Map::new()
529+
}
530+
531+
fn set_auto_splitter_settings(
532+
&mut self,
533+
settings_map: livesplit_auto_splitting::settings::Map,
534+
) {
535+
if self.run().parsed_auto_splitter_settings().is_none() && settings_map.is_empty() {
536+
return;
537+
}
538+
self.set_run_auto_splitter_settings(settings_map);
539+
}
540+
}
541+
542+
#[cfg(feature = "auto-splitting")]
543+
impl TimerAutoSplitterSettings for crate::SharedTimer {
544+
fn get_auto_splitter_settings(&self) -> livesplit_auto_splitting::settings::Map {
545+
let Ok(t) = self.read() else {
546+
return livesplit_auto_splitting::settings::Map::new();
547+
};
548+
t.get_auto_splitter_settings()
549+
}
550+
551+
fn set_auto_splitter_settings(
552+
&mut self,
553+
settings_map: livesplit_auto_splitting::settings::Map,
554+
) {
555+
let Ok(mut t) = self.write() else {
556+
return;
557+
};
558+
t.set_auto_splitter_settings(settings_map);
559+
}
560+
}

src/run/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,25 @@ impl Run {
349349
&mut self.parsed_auto_splitter_settings
350350
}
351351

352+
/// Set the settings map in the parsed auto splitter settings.
353+
#[cfg(feature = "auto-splitting")]
354+
pub fn set_auto_splitter_settings(
355+
&mut self,
356+
settings_map: livesplit_auto_splitting::settings::Map,
357+
) {
358+
let p = self.parsed_auto_splitter_settings_mut();
359+
match p {
360+
None => {
361+
let mut a = AutoSplitterSettings::default();
362+
a.set_custom_settings(settings_map);
363+
*p = Some(a);
364+
}
365+
Some(a) => {
366+
a.set_custom_settings(settings_map);
367+
}
368+
}
369+
}
370+
352371
/// Accesses the [`LinkedLayout`] of this `Run`. If a
353372
/// [`Layout`](crate::Layout) is linked, it is supposed to be loaded to
354373
/// visualize the `Run`.

src/timing/timer/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ impl Timer {
168168
&self.run
169169
}
170170

171+
/// Set the settings map in the parsed auto splitter settings.
172+
#[cfg(feature = "auto-splitting")]
173+
pub fn set_run_auto_splitter_settings(
174+
&mut self,
175+
settings_map: livesplit_auto_splitting::settings::Map,
176+
) {
177+
self.run.set_auto_splitter_settings(settings_map);
178+
}
179+
171180
/// Marks the Run as unmodified, so that it is known that all the changes
172181
/// have been saved.
173182
#[inline]

0 commit comments

Comments
 (0)