Skip to content

Commit

Permalink
run: auto_splitter_settings_map load, store
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKnauth committed Sep 21, 2024
1 parent 54c9f2b commit 1811791
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
13 changes: 7 additions & 6 deletions src/auto_splitting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ use livesplit_auto_splitting::{
Timer as AutoSplitTimer, TimerState,
};
use snafu::Snafu;
use std::{cell::RefCell, fmt, fs, io, path::PathBuf, thread, time::Duration};
use std::{fmt, fs, io, path::PathBuf, sync::RwLock, thread, time::Duration};
use tokio::{
runtime,
sync::watch,
Expand Down Expand Up @@ -588,7 +588,7 @@ pub struct Runtime<T> {
interrupt_receiver: watch::Receiver<Option<InterruptHandle>>,
auto_splitter: watch::Sender<Option<AutoSplitter<Timer<T>>>>,
runtime: livesplit_auto_splitting::Runtime,
compiled_auto_splitter: RefCell<Option<CompiledAutoSplitter>>,
compiled_auto_splitter: RwLock<Option<CompiledAutoSplitter>>,
}

impl<T> Drop for Runtime<T> {
Expand Down Expand Up @@ -643,7 +643,7 @@ impl<T: event::CommandSink + TimerQuery + Send + 'static> Runtime<T> {
auto_splitter: sender,
// TODO: unwrap?
runtime: livesplit_auto_splitting::Runtime::new(Config::default()).unwrap(),
compiled_auto_splitter: RefCell::new(None),
compiled_auto_splitter: RwLock::new(None),
}
}

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

Expand All @@ -666,8 +666,9 @@ impl<T: event::CommandSink + TimerQuery + Send + 'static> Runtime<T> {
compiled_auto_splitter: &CompiledAutoSplitter,
timer: T,
) -> Result<(), Error> {
let settings_map = timer.get_timer().run().auto_splitter_settings_map_load();
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
Expand All @@ -687,7 +688,7 @@ impl<T: event::CommandSink + TimerQuery + Send + 'static> Runtime<T> {
/// Reloads the auto splitter without re-compiling.
pub fn reload(&self, timer: T) -> Result<(), Error> {
self.unload()?;
if let Some(compiled_auto_splitter) = self.compiled_auto_splitter.borrow().as_ref() {
if let Some(compiled_auto_splitter) = self.compiled_auto_splitter.read().unwrap().as_ref() {
self.instantiate(compiled_auto_splitter, timer)?;
}
Ok(())
Expand Down
34 changes: 27 additions & 7 deletions src/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,18 +335,38 @@ impl Run {
&mut self.auto_splitter_settings
}

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

/// Accesses the Auto Splitter Settings as mutable.
#[inline]
/// Stores a settings map into the parsed auto splitter settings.
#[cfg(feature = "auto-splitting")]
pub fn parsed_auto_splitter_settings_mut(&mut self) -> &mut Option<AutoSplitterSettings> {
&mut self.parsed_auto_splitter_settings
pub fn auto_splitter_settings_map_store(
&mut self,
settings_map: livesplit_auto_splitting::settings::Map,
) {
let p = &mut self.parsed_auto_splitter_settings;
match p {
None => {
if settings_map.is_empty() {
return;
}
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
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 @@ -168,6 +168,15 @@ impl Timer {
&self.run
}

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

Please sign in to comment.