Skip to content

Commit 1e9d00d

Browse files
committed
reload
1 parent ac1375a commit 1e9d00d

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

src/auto_splitting/mod.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -548,10 +548,11 @@ use crate::{
548548
};
549549
pub use livesplit_auto_splitting::{settings, wasi_path};
550550
use livesplit_auto_splitting::{
551-
AutoSplitter, Config, CreationError, InterruptHandle, Timer as AutoSplitTimer, TimerState,
551+
AutoSplitter, CompiledAutoSplitter, Config, CreationError, InterruptHandle,
552+
Timer as AutoSplitTimer, TimerState,
552553
};
553554
use snafu::Snafu;
554-
use std::{fmt, fs, io, path::PathBuf, thread, time::Duration};
555+
use std::{cell::RefCell, fmt, fs, io, path::PathBuf, thread, time::Duration};
555556
use tokio::{
556557
runtime,
557558
sync::watch,
@@ -587,6 +588,7 @@ pub struct Runtime<T> {
587588
interrupt_receiver: watch::Receiver<Option<InterruptHandle>>,
588589
auto_splitter: watch::Sender<Option<AutoSplitter<Timer<T>>>>,
589590
runtime: livesplit_auto_splitting::Runtime,
591+
compiled_auto_splitter: RefCell<Option<CompiledAutoSplitter>>,
590592
}
591593

592594
impl<T> Drop for Runtime<T> {
@@ -641,17 +643,30 @@ impl<T: event::Sink + TimerQuery + Send + 'static> Runtime<T> {
641643
auto_splitter: sender,
642644
// TODO: unwrap?
643645
runtime: livesplit_auto_splitting::Runtime::new(Config::default()).unwrap(),
646+
compiled_auto_splitter: RefCell::new(None),
644647
}
645648
}
646649

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

651-
let auto_splitter = self
654+
let compiled_auto_splitter = self
652655
.runtime
653656
.compile(&data)
654-
.map_err(|e| Error::LoadFailed { source: e })?
657+
.map_err(|e| Error::LoadFailed { source: e })?;
658+
self.instantiate(&compiled_auto_splitter, timer)?;
659+
*self.compiled_auto_splitter.borrow_mut() = Some(compiled_auto_splitter);
660+
Ok(())
661+
}
662+
663+
/// Instantiates the compiled auto splitter.
664+
fn instantiate(
665+
&self,
666+
compiled_auto_splitter: &CompiledAutoSplitter,
667+
timer: T,
668+
) -> Result<(), Error> {
669+
let auto_splitter = compiled_auto_splitter
655670
.instantiate(Timer(timer), None, None)
656671
.map_err(|e| Error::LoadFailed { source: e })?;
657672

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

687+
/// Reloads the auto splitter without re-compiling.
688+
pub fn reload(&self, timer: T) -> Result<(), Error> {
689+
self.unload()?;
690+
if let Some(compiled_auto_splitter) = self.compiled_auto_splitter.borrow().as_ref() {
691+
self.instantiate(compiled_auto_splitter, timer)?;
692+
}
693+
Ok(())
694+
}
695+
672696
/// Accesses a copy of the currently stored settings. The auto splitter can
673697
/// change these at any time. If you intend to make modifications to the
674698
/// settings, you need to set them again via

0 commit comments

Comments
 (0)