Skip to content

Commit

Permalink
reload
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKnauth committed Jun 14, 2024
1 parent cf5f15d commit 6999d42
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/auto_splitting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,11 @@ use crate::{
};
pub use livesplit_auto_splitting::{settings, wasi_path};
use livesplit_auto_splitting::{
AutoSplitter, Config, CreationError, InterruptHandle, Timer as AutoSplitTimer, TimerState,
AutoSplitter, CompiledAutoSplitter, Config, CreationError, InterruptHandle,
Timer as AutoSplitTimer, TimerState,
};
use snafu::Snafu;
use std::{fmt, fs, io, path::PathBuf, thread, time::Duration};
use std::{cell::RefCell, fmt, fs, io, path::PathBuf, thread, time::Duration};
use tokio::{
runtime,
sync::watch,
Expand Down Expand Up @@ -587,6 +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>>,
}

impl<T> Drop for Runtime<T> {
Expand Down Expand Up @@ -641,17 +643,30 @@ impl<T: event::Sink + 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),
}
}

/// Attempts to load a wasm file containing an auto splitter module.
pub fn load(&self, path: PathBuf, timer: T) -> Result<(), Error> {
let data = fs::read(path).map_err(|e| Error::ReadFileFailed { source: e })?;

let auto_splitter = self
let compiled_auto_splitter = self
.runtime
.compile(&data)
.map_err(|e| Error::LoadFailed { source: e })?
.map_err(|e| Error::LoadFailed { source: e })?;
self.instantiate(&compiled_auto_splitter, timer)?;
*self.compiled_auto_splitter.borrow_mut() = Some(compiled_auto_splitter);
Ok(())
}

/// Instantiates the compiled auto splitter.
fn instantiate(
&self,
compiled_auto_splitter: &CompiledAutoSplitter,
timer: T,
) -> Result<(), Error> {
let auto_splitter = compiled_auto_splitter
.instantiate(Timer(timer), None, None)
.map_err(|e| Error::LoadFailed { source: e })?;

Expand All @@ -669,6 +684,15 @@ impl<T: event::Sink + TimerQuery + Send + 'static> Runtime<T> {
.map_err(|_| Error::ThreadStopped)
}

/// 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() {
self.instantiate(compiled_auto_splitter, timer)?;
}
Ok(())
}

/// Accesses a copy of the currently stored settings. The auto splitter can
/// change these at any time. If you intend to make modifications to the
/// settings, you need to set them again via
Expand Down

0 comments on commit 6999d42

Please sign in to comment.