@@ -570,15 +570,16 @@ use crate::{
570570} ;
571571use arc_swap:: ArcSwapOption ;
572572use livesplit_auto_splitting:: {
573- AutoSplitter , Config , CreationError , LogLevel , Timer as AutoSplitTimer , TimerState ,
573+ AutoSplitter , CompiledAutoSplitter , Config , CreationError , LogLevel ,
574+ Timer as AutoSplitTimer , TimerState ,
574575} ;
575576pub use livesplit_auto_splitting:: { settings, wasi_path} ;
576577use snafu:: Snafu ;
577578use std:: {
578579 fmt, fs, io,
579580 path:: PathBuf ,
580581 sync:: {
581- Condvar , Mutex ,
582+ Condvar , Mutex , RwLock ,
582583 mpsc:: { self , Receiver , RecvTimeoutError , Sender } ,
583584 } ,
584585 thread,
@@ -614,6 +615,7 @@ pub struct Runtime<T: event::CommandSink + TimerQuery + Send + 'static> {
614615 shared_state : Arc < SharedState < T > > ,
615616 changed_sender : Sender < ( ) > ,
616617 runtime : livesplit_auto_splitting:: Runtime ,
618+ compiled_auto_splitter : RwLock < Option < CompiledAutoSplitter > > ,
617619}
618620
619621struct SharedState < T : ' static > {
@@ -688,18 +690,32 @@ impl<T: event::CommandSink + TimerQuery + Send + 'static> Runtime<T> {
688690 changed_sender,
689691 // TODO: unwrap?
690692 runtime : livesplit_auto_splitting:: Runtime :: new ( Config :: default ( ) ) . unwrap ( ) ,
693+ compiled_auto_splitter : RwLock :: new ( None ) ,
691694 }
692695 }
693696
694697 /// Attempts to load a wasm file containing an auto splitter module.
695698 pub fn load ( & self , path : PathBuf , timer : T ) -> Result < ( ) , Error > {
696699 let data = fs:: read ( path) . map_err ( |e| Error :: ReadFileFailed { source : e } ) ?;
697700
698- let auto_splitter = self
701+ let compiled_auto_splitter = self
699702 . runtime
700703 . compile ( & data)
701- . map_err ( |e| Error :: LoadFailed { source : e } ) ?
702- . instantiate ( Timer ( timer) , None , None )
704+ . map_err ( |e| Error :: LoadFailed { source : e } ) ?;
705+ self . instantiate ( & compiled_auto_splitter, timer) ?;
706+ * self . compiled_auto_splitter . write ( ) . unwrap ( ) = Some ( compiled_auto_splitter) ;
707+ Ok ( ( ) )
708+ }
709+
710+ /// Instantiates the compiled auto splitter.
711+ fn instantiate (
712+ & self ,
713+ compiled_auto_splitter : & CompiledAutoSplitter ,
714+ timer : T ,
715+ ) -> Result < ( ) , Error > {
716+ let settings_map = timer. get_timer ( ) . run ( ) . auto_splitter_settings_map_load ( ) ;
717+ let auto_splitter = compiled_auto_splitter
718+ . instantiate ( Timer ( timer) , settings_map, None )
703719 . map_err ( |e| Error :: LoadFailed { source : e } ) ?;
704720
705721 self . shared_state
@@ -722,6 +738,15 @@ impl<T: event::CommandSink + TimerQuery + Send + 'static> Runtime<T> {
722738 . map_err ( |_| Error :: ThreadStopped )
723739 }
724740
741+ /// Reloads the auto splitter without re-compiling.
742+ pub fn reload ( & self , timer : T ) -> Result < ( ) , Error > {
743+ self . unload ( ) ?;
744+ if let Some ( compiled_auto_splitter) = self . compiled_auto_splitter . read ( ) . unwrap ( ) . as_ref ( ) {
745+ self . instantiate ( compiled_auto_splitter, timer) ?;
746+ }
747+ Ok ( ( ) )
748+ }
749+
725750 /// Accesses a copy of the currently stored settings. The auto splitter can
726751 /// change these at any time. If you intend to make modifications to the
727752 /// settings, you need to set them again via
0 commit comments