From 1612b367539fc5f0c18b6d204290e666cc650930 Mon Sep 17 00:00:00 2001 From: Ben Wallis Date: Sun, 20 Jun 2021 18:52:28 +0100 Subject: [PATCH] Reworked veloren_dynlib initialisation --- voxygen/anim/src/lib.rs | 12 +++--------- voxygen/dynlib/src/lib.rs | 10 ++++++---- voxygen/egui/src/lib.rs | 12 +++--------- voxygen/src/main.rs | 2 -- 4 files changed, 12 insertions(+), 24 deletions(-) diff --git a/voxygen/anim/src/lib.rs b/voxygen/anim/src/lib.rs index abdfe50ddd..ab9d933338 100644 --- a/voxygen/anim/src/lib.rs +++ b/voxygen/anim/src/lib.rs @@ -91,18 +91,12 @@ pub type Bone = Transform; #[cfg(feature = "use-dyn-lib")] lazy_static! { - static ref LIB: Arc>> = Arc::new(Mutex::new(None)); + static ref LIB: Arc>> = + voxygen_dynlib::init("veloren-voxygen-anim", "veloren-voxygen-anim-dyn", "anim"); } #[cfg(feature = "use-dyn-lib")] -pub fn init() { - voxygen_dynlib::init( - Arc::clone(&LIB), - "veloren-voxygen-anim", - "veloren-voxygen-anim-dyn", - "anim", - ); -} +pub fn init() { lazy_static::initialize(&LIB); } pub trait Skeleton: Send + Sync + 'static { type Attr; diff --git a/voxygen/dynlib/src/lib.rs b/voxygen/dynlib/src/lib.rs index e979360644..336908f5eb 100644 --- a/voxygen/dynlib/src/lib.rs +++ b/voxygen/dynlib/src/lib.rs @@ -120,12 +120,11 @@ impl LoadedLib { /// This will search for the directory named `package_source_dir` and watch the /// files within it for any changes. pub fn init( - lib_storage: Arc>>, package: &'static str, dyn_package: &'static str, package_source_dir: &'static str, -) { - *lib_storage.lock().unwrap() = Some(LoadedLib::compile_load(dyn_package)); +) -> Arc>> { + let lib_storage = Arc::new(Mutex::new(Some(LoadedLib::compile_load(dyn_package)))); // TODO: use crossbeam let (reload_send, reload_recv) = mpsc::channel(); @@ -148,6 +147,7 @@ pub fn init( // Start reloader that watcher signals // "Debounces" events since I can't find the option to do this in the latest // `notify` + let lib_storage_clone = Arc::clone(&lib_storage); std::thread::Builder::new() .name(format!("{}_hotreload_watcher", package)) .spawn(move || { @@ -164,13 +164,15 @@ pub fn init( "Hot reloading {} because files in `{}` modified.", package, package_source_dir ); - hotreload(dyn_package, &lib_storage); + hotreload(dyn_package, &lib_storage_clone); } }) .unwrap(); // Let the watcher live forever std::mem::forget(watcher); + + lib_storage } fn compiled_file(dyn_package: &str) -> String { dyn_lib_file(dyn_package, false) } diff --git a/voxygen/egui/src/lib.rs b/voxygen/egui/src/lib.rs index 530c460379..500612d820 100644 --- a/voxygen/egui/src/lib.rs +++ b/voxygen/egui/src/lib.rs @@ -35,7 +35,8 @@ use { #[cfg(feature = "use-dyn-lib")] lazy_static! { - static ref LIB: Arc>> = Arc::new(Mutex::new(None)); + static ref LIB: Arc>> = + voxygen_dynlib::init("veloren-voxygen-egui", "veloren-voxygen-egui-dyn", "egui"); } #[cfg(feature = "use-dyn-lib")] @@ -103,14 +104,7 @@ pub struct EguiActions { } #[cfg(feature = "use-dyn-lib")] -pub fn init() { - voxygen_dynlib::init( - Arc::clone(&LIB), - "veloren-voxygen-egui", - "veloren-voxygen-egui-dyn", - "egui", - ); -} +pub fn init() { lazy_static::initialize(&LIB); } pub fn maintain( platform: &mut Platform, diff --git a/voxygen/src/main.rs b/voxygen/src/main.rs index 0f5b1f7d13..7edb5315cc 100644 --- a/voxygen/src/main.rs +++ b/voxygen/src/main.rs @@ -19,8 +19,6 @@ use common::{ clock::Clock, }; use std::panic; -#[cfg(any(feature = "hot-anim", feature = "hot-egui"))] -use std::sync::Arc; use tracing::{error, info, warn}; #[cfg(feature = "egui-ui")] use veloren_voxygen::ui::egui::EguiState;