From 6999d420ec4de7e86a27410bc09f09a763a0c844 Mon Sep 17 00:00:00 2001 From: AlexKnauth Date: Tue, 28 May 2024 17:50:15 -0400 Subject: [PATCH] reload --- src/auto_splitting/mod.rs | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/auto_splitting/mod.rs b/src/auto_splitting/mod.rs index 04c6f55e..194a7e65 100644 --- a/src/auto_splitting/mod.rs +++ b/src/auto_splitting/mod.rs @@ -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, @@ -587,6 +588,7 @@ pub struct Runtime { interrupt_receiver: watch::Receiver>, auto_splitter: watch::Sender>>>, runtime: livesplit_auto_splitting::Runtime, + compiled_auto_splitter: RefCell>, } impl Drop for Runtime { @@ -641,6 +643,7 @@ impl Runtime { auto_splitter: sender, // TODO: unwrap? runtime: livesplit_auto_splitting::Runtime::new(Config::default()).unwrap(), + compiled_auto_splitter: RefCell::new(None), } } @@ -648,10 +651,22 @@ impl Runtime { 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 })?; @@ -669,6 +684,15 @@ impl Runtime { .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