Skip to content

Commit f0feb78

Browse files
committed
run: auto_splitter_settings_map load, store
1 parent 395b0cb commit f0feb78

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

src/auto_splitting/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ use livesplit_auto_splitting::{
562562
Timer as AutoSplitTimer, TimerState,
563563
};
564564
use snafu::Snafu;
565-
use std::{cell::RefCell, fmt, fs, io, path::PathBuf, thread, time::Duration};
565+
use std::{fmt, fs, io, path::PathBuf, sync::RwLock, thread, time::Duration};
566566
use tokio::{
567567
runtime,
568568
sync::watch,
@@ -598,7 +598,7 @@ pub struct Runtime<T> {
598598
interrupt_receiver: watch::Receiver<Option<InterruptHandle>>,
599599
auto_splitter: watch::Sender<Option<AutoSplitter<Timer<T>>>>,
600600
runtime: livesplit_auto_splitting::Runtime,
601-
compiled_auto_splitter: RefCell<Option<CompiledAutoSplitter>>,
601+
compiled_auto_splitter: RwLock<Option<CompiledAutoSplitter>>,
602602
}
603603

604604
impl<T> Drop for Runtime<T> {
@@ -653,7 +653,7 @@ impl<T: event::CommandSink + TimerQuery + Send + 'static> Runtime<T> {
653653
auto_splitter: sender,
654654
// TODO: unwrap?
655655
runtime: livesplit_auto_splitting::Runtime::new(Config::default()).unwrap(),
656-
compiled_auto_splitter: RefCell::new(None),
656+
compiled_auto_splitter: RwLock::new(None),
657657
}
658658
}
659659

@@ -666,7 +666,7 @@ impl<T: event::CommandSink + TimerQuery + Send + 'static> Runtime<T> {
666666
.compile(&data)
667667
.map_err(|e| Error::LoadFailed { source: e })?;
668668
self.instantiate(&compiled_auto_splitter, timer)?;
669-
*self.compiled_auto_splitter.borrow_mut() = Some(compiled_auto_splitter);
669+
*self.compiled_auto_splitter.write().unwrap() = Some(compiled_auto_splitter);
670670
Ok(())
671671
}
672672

@@ -676,8 +676,9 @@ impl<T: event::CommandSink + TimerQuery + Send + 'static> Runtime<T> {
676676
compiled_auto_splitter: &CompiledAutoSplitter,
677677
timer: T,
678678
) -> Result<(), Error> {
679+
let settings_map = timer.get_timer().run().auto_splitter_settings_map_load();
679680
let auto_splitter = compiled_auto_splitter
680-
.instantiate(Timer(timer), None, None)
681+
.instantiate(Timer(timer), settings_map, None)
681682
.map_err(|e| Error::LoadFailed { source: e })?;
682683

683684
self.auto_splitter
@@ -697,7 +698,7 @@ impl<T: event::CommandSink + TimerQuery + Send + 'static> Runtime<T> {
697698
/// Reloads the auto splitter without re-compiling.
698699
pub fn reload(&self, timer: T) -> Result<(), Error> {
699700
self.unload()?;
700-
if let Some(compiled_auto_splitter) = self.compiled_auto_splitter.borrow().as_ref() {
701+
if let Some(compiled_auto_splitter) = self.compiled_auto_splitter.read().unwrap().as_ref() {
701702
self.instantiate(compiled_auto_splitter, timer)?;
702703
}
703704
Ok(())

src/run/mod.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,18 +335,38 @@ impl Run {
335335
&mut self.auto_splitter_settings
336336
}
337337

338-
/// Accesses the Auto Splitter Settings.
338+
/// Loads a copy of the Auto Splitter Settings as a settings map.
339339
#[inline]
340340
#[cfg(feature = "auto-splitting")]
341-
pub fn parsed_auto_splitter_settings(&self) -> &Option<AutoSplitterSettings> {
342-
&self.parsed_auto_splitter_settings
341+
pub fn auto_splitter_settings_map_load(
342+
&self,
343+
) -> Option<livesplit_auto_splitting::settings::Map> {
344+
if let Some(p) = &self.parsed_auto_splitter_settings {
345+
return Some(p.custom_settings.clone());
346+
}
347+
None
343348
}
344349

345-
/// Accesses the Auto Splitter Settings as mutable.
346-
#[inline]
350+
/// Stores a settings map into the parsed auto splitter settings.
347351
#[cfg(feature = "auto-splitting")]
348-
pub fn parsed_auto_splitter_settings_mut(&mut self) -> &mut Option<AutoSplitterSettings> {
349-
&mut self.parsed_auto_splitter_settings
352+
pub fn auto_splitter_settings_map_store(
353+
&mut self,
354+
settings_map: livesplit_auto_splitting::settings::Map,
355+
) {
356+
let p = &mut self.parsed_auto_splitter_settings;
357+
match p {
358+
None => {
359+
if settings_map.is_empty() {
360+
return;
361+
}
362+
let mut a = AutoSplitterSettings::default();
363+
a.set_custom_settings(settings_map);
364+
*p = Some(a);
365+
}
366+
Some(a) => {
367+
a.set_custom_settings(settings_map);
368+
}
369+
}
350370
}
351371

352372
/// Accesses the [`LinkedLayout`] of this `Run`. If a

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+
/// Stores a settings map into the parsed auto splitter settings.
172+
#[cfg(feature = "auto-splitting")]
173+
pub fn run_auto_splitter_settings_map_store(
174+
&mut self,
175+
settings_map: livesplit_auto_splitting::settings::Map,
176+
) {
177+
self.run.auto_splitter_settings_map_store(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)