2020-12-12 01:45:46 +00:00
|
|
|
use hashbrown::HashSet;
|
2020-06-17 07:49:14 +00:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use libloading::Library;
|
2020-06-18 01:16:27 +00:00
|
|
|
use notify::{immediate_watcher, EventKind, RecursiveMode, Watcher};
|
2020-06-17 07:49:14 +00:00
|
|
|
use std::{
|
|
|
|
process::{Command, Stdio},
|
2020-06-18 06:45:49 +00:00
|
|
|
sync::{mpsc, Mutex},
|
|
|
|
thread,
|
|
|
|
time::Duration,
|
2020-06-17 07:49:14 +00:00
|
|
|
};
|
|
|
|
|
2020-06-25 13:07:34 +00:00
|
|
|
use find_folder::Search;
|
2020-06-25 04:46:01 +00:00
|
|
|
use std::{env, path::PathBuf};
|
|
|
|
use tracing::{debug, error, info};
|
|
|
|
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
const COMPILED_FILE: &str = "voxygen_anim.dll";
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
const ACTIVE_FILE: &str = "voxygen_anim_active.dll";
|
|
|
|
|
2020-06-25 13:07:34 +00:00
|
|
|
#[cfg(not(target_os = "windows"))]
|
2020-06-25 04:46:01 +00:00
|
|
|
const COMPILED_FILE: &str = "libvoxygen_anim.so";
|
2020-06-25 13:07:34 +00:00
|
|
|
#[cfg(not(target_os = "windows"))]
|
2020-06-25 04:46:01 +00:00
|
|
|
const ACTIVE_FILE: &str = "libvoxygen_anim_active.so";
|
|
|
|
|
|
|
|
// This option is required as `hotreload()` moves the `LoadedLib`.
|
2020-06-17 07:49:14 +00:00
|
|
|
lazy_static! {
|
2020-06-25 13:07:34 +00:00
|
|
|
pub static ref LIB: Mutex<Option<LoadedLib>> = Mutex::new(Some(LoadedLib::compile_load()));
|
2020-06-17 07:49:14 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 13:07:34 +00:00
|
|
|
/// LoadedLib holds a loaded dynamic library and the location of library file
|
|
|
|
/// with the appropriate OS specific name and extension i.e.
|
|
|
|
/// `libvoxygen_anim_active.dylib`, `voxygen_anim_active.dll`.
|
|
|
|
///
|
|
|
|
/// # NOTE
|
|
|
|
/// DOES NOT WORK ON MACOS, due to some limitations with hot-reloading the
|
|
|
|
/// `.dylib`.
|
2020-06-17 07:49:14 +00:00
|
|
|
pub struct LoadedLib {
|
2020-06-25 04:46:01 +00:00
|
|
|
/// Loaded library.
|
2020-06-17 07:49:14 +00:00
|
|
|
pub lib: Library,
|
2020-06-25 04:46:01 +00:00
|
|
|
/// Path to the library.
|
|
|
|
pub lib_path: PathBuf,
|
2020-06-17 07:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LoadedLib {
|
2020-06-25 13:07:34 +00:00
|
|
|
/// Compile and load the dynamic library
|
2020-06-25 04:46:01 +00:00
|
|
|
///
|
|
|
|
/// This is necessary because the very first time you use hot reloading you
|
|
|
|
/// wont have the library, so you can't load it until you have compiled it!
|
2020-06-25 13:07:34 +00:00
|
|
|
fn compile_load() -> Self {
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
error!("The hot reloading feature does not work on macos.");
|
|
|
|
|
2020-06-17 07:49:14 +00:00
|
|
|
// Compile
|
2020-06-25 04:46:01 +00:00
|
|
|
if !compile() {
|
|
|
|
panic!("Animation compile failed.");
|
|
|
|
} else {
|
|
|
|
info!("Animation compile succeeded.");
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(&LoadedLib::determine_path());
|
2020-06-18 06:45:49 +00:00
|
|
|
|
2020-06-25 13:07:34 +00:00
|
|
|
Self::load()
|
2020-06-17 07:49:14 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 13:07:34 +00:00
|
|
|
/// Load a library from disk.
|
2020-06-25 04:46:01 +00:00
|
|
|
///
|
|
|
|
/// Currently this is pretty fragile, it gets the path of where it thinks
|
|
|
|
/// the dynamic library should be and tries to load it. It will panic if it
|
|
|
|
/// is missing.
|
2020-06-17 07:49:14 +00:00
|
|
|
fn load() -> Self {
|
2020-06-25 04:46:01 +00:00
|
|
|
let lib_path = LoadedLib::determine_path();
|
|
|
|
|
|
|
|
// Try to load the library.
|
|
|
|
let lib = match Library::new(lib_path.clone()) {
|
|
|
|
Ok(lib) => lib,
|
|
|
|
Err(e) => panic!(
|
|
|
|
"Tried to load dynamic library from {:?}, but it could not be found. The first \
|
2020-12-01 12:58:58 +00:00
|
|
|
reason might be that you need to uncomment a line in `voxygen/anim/Cargo.toml` \
|
|
|
|
to build the library required for hot reloading. The second is we may require a \
|
|
|
|
special case for your OS so we can find it. {:?}",
|
2020-06-25 04:46:01 +00:00
|
|
|
lib_path, e
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
Self { lib, lib_path }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Determine the path to the dynamic library based on the path of the
|
|
|
|
/// current executable.
|
|
|
|
fn determine_path() -> PathBuf {
|
|
|
|
let current_exe = env::current_exe();
|
|
|
|
|
2020-06-25 13:07:34 +00:00
|
|
|
// If we got the current_exe, we need to go up a level and then down
|
|
|
|
// in to debug (in case we were in release or another build dir).
|
2020-06-25 04:46:01 +00:00
|
|
|
let mut lib_path = match current_exe {
|
2020-06-25 13:07:34 +00:00
|
|
|
Ok(mut path) => {
|
|
|
|
// Remove the filename to get the directory.
|
|
|
|
path.pop();
|
|
|
|
|
|
|
|
// Search for the debug directory.
|
|
|
|
let dir = Search::ParentsThenKids(1, 1)
|
|
|
|
.of(path)
|
|
|
|
.for_folder("debug")
|
|
|
|
.expect(
|
|
|
|
"Could not find the debug build directory relative to the current \
|
|
|
|
executable.",
|
|
|
|
);
|
|
|
|
|
|
|
|
debug!(?dir, "Found the debug build directory.");
|
2020-06-25 04:46:01 +00:00
|
|
|
dir
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
panic!(
|
|
|
|
"Could not determine the path of the current executable, this is needed to \
|
2020-06-25 13:07:34 +00:00
|
|
|
hotreload the dynamic library. {:?}",
|
2020-06-25 04:46:01 +00:00
|
|
|
e
|
|
|
|
);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// Determine the platform specific path and push it onto our already
|
|
|
|
// established target/debug dir.
|
|
|
|
lib_path.push(ACTIVE_FILE);
|
|
|
|
|
|
|
|
lib_path
|
2020-06-17 07:49:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 04:46:01 +00:00
|
|
|
/// Initialise a watcher.
|
|
|
|
///
|
|
|
|
/// The assumption is that this is run from the voxygen crate's root directory
|
2020-12-01 12:58:58 +00:00
|
|
|
/// as it will watch the relative path `anim` for any changes to `.rs`
|
2020-06-25 04:46:01 +00:00
|
|
|
/// files. Upon noticing changes it will wait a moment and then recompile.
|
2020-06-17 07:49:14 +00:00
|
|
|
pub fn init() {
|
2020-06-25 13:07:34 +00:00
|
|
|
// Make sure first compile is done by accessing the lazy_static and then
|
|
|
|
// immediately dropping (because we don't actually need it).
|
|
|
|
drop(LIB.lock());
|
|
|
|
|
2020-06-18 01:16:27 +00:00
|
|
|
// TODO: use crossbeam
|
2020-06-18 06:45:49 +00:00
|
|
|
let (reload_send, reload_recv) = mpsc::channel();
|
2020-06-18 01:16:27 +00:00
|
|
|
|
2020-06-17 07:49:14 +00:00
|
|
|
// Start watcher
|
2020-06-18 01:16:27 +00:00
|
|
|
let mut watcher = immediate_watcher(move |res| event_fn(res, &reload_send)).unwrap();
|
2020-12-01 12:58:58 +00:00
|
|
|
watcher.watch("anim", RecursiveMode::Recursive).unwrap();
|
2020-06-17 07:49:14 +00:00
|
|
|
|
2020-06-18 01:16:27 +00:00
|
|
|
// Start reloader that watcher signals
|
|
|
|
// "Debounces" events since I can't find the option to do this in the latest
|
|
|
|
// `notify`
|
2020-06-18 06:45:49 +00:00
|
|
|
thread::spawn(move || {
|
2020-12-12 01:45:46 +00:00
|
|
|
let mut modified_paths = HashSet::new();
|
2020-06-18 06:45:49 +00:00
|
|
|
|
|
|
|
while let Ok(path) = reload_recv.recv() {
|
|
|
|
modified_paths.insert(path);
|
2020-06-19 05:31:15 +00:00
|
|
|
// Wait for any additional modify events before reloading
|
2020-06-18 06:45:49 +00:00
|
|
|
while let Ok(path) = reload_recv.recv_timeout(Duration::from_millis(300)) {
|
|
|
|
modified_paths.insert(path);
|
|
|
|
}
|
|
|
|
|
2020-06-25 04:46:01 +00:00
|
|
|
info!(
|
2020-06-21 21:47:49 +00:00
|
|
|
?modified_paths,
|
2020-12-01 12:58:58 +00:00
|
|
|
"Hot reloading animations because files in `anim` modified."
|
2020-06-21 21:47:49 +00:00
|
|
|
);
|
2020-06-18 01:16:27 +00:00
|
|
|
|
2020-06-25 04:46:01 +00:00
|
|
|
hotreload();
|
2020-06-18 01:16:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-06-17 07:49:14 +00:00
|
|
|
// Let the watcher live forever
|
|
|
|
std::mem::forget(watcher);
|
|
|
|
}
|
|
|
|
|
2020-06-25 04:46:01 +00:00
|
|
|
/// Event function to hotreload the dynamic library
|
|
|
|
///
|
|
|
|
/// This is called by the watcher to filter for modify events on `.rs` files
|
|
|
|
/// before sending them back.
|
2020-06-18 06:45:49 +00:00
|
|
|
fn event_fn(res: notify::Result<notify::Event>, sender: &mpsc::Sender<String>) {
|
2020-06-17 07:49:14 +00:00
|
|
|
match res {
|
|
|
|
Ok(event) => match event.kind {
|
2020-06-18 01:16:27 +00:00
|
|
|
EventKind::Modify(_) => {
|
2020-06-18 06:45:49 +00:00
|
|
|
event
|
2020-06-17 07:49:14 +00:00
|
|
|
.paths
|
|
|
|
.iter()
|
2020-06-18 06:45:49 +00:00
|
|
|
.filter(|p| p.extension().map(|e| e == "rs").unwrap_or(false))
|
|
|
|
.map(|p| p.to_string_lossy().into_owned())
|
2020-06-18 01:16:27 +00:00
|
|
|
// Signal reloader
|
2020-06-18 06:45:49 +00:00
|
|
|
.for_each(|p| { let _ = sender.send(p); });
|
2020-06-17 07:49:14 +00:00
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
},
|
2020-06-25 13:07:34 +00:00
|
|
|
Err(e) => error!(?e, "Animation hotreload watcher error."),
|
2020-06-17 07:49:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 04:46:01 +00:00
|
|
|
/// Hotreload the dynamic library
|
|
|
|
///
|
|
|
|
/// This will reload the dynamic library by first internally calling compile
|
|
|
|
/// and then reloading the library.
|
|
|
|
fn hotreload() {
|
2020-06-25 13:07:34 +00:00
|
|
|
// Do nothing if recompile failed.
|
2020-06-25 04:46:01 +00:00
|
|
|
if compile() {
|
|
|
|
let mut lock = LIB.lock().unwrap();
|
|
|
|
|
|
|
|
// Close lib.
|
2020-06-25 13:07:34 +00:00
|
|
|
let loaded_lib = lock.take().unwrap();
|
|
|
|
loaded_lib.lib.close().unwrap();
|
|
|
|
copy(&loaded_lib.lib_path);
|
2020-06-25 04:46:01 +00:00
|
|
|
|
|
|
|
// Open new lib.
|
|
|
|
*lock = Some(LoadedLib::load());
|
|
|
|
|
|
|
|
info!("Updated animations.");
|
2020-06-17 07:49:14 +00:00
|
|
|
}
|
2020-06-18 06:45:49 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 04:46:01 +00:00
|
|
|
/// Recompile the anim package
|
|
|
|
///
|
|
|
|
/// Returns `false` if the compile failed.
|
2020-06-18 06:45:49 +00:00
|
|
|
fn compile() -> bool {
|
|
|
|
let output = Command::new("cargo")
|
|
|
|
.stderr(Stdio::inherit())
|
|
|
|
.stdout(Stdio::inherit())
|
|
|
|
.arg("build")
|
|
|
|
.arg("--package")
|
|
|
|
.arg("veloren-voxygen-anim")
|
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
|
2020-06-25 04:46:01 +00:00
|
|
|
output.status.success()
|
2020-06-18 06:45:49 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 04:46:01 +00:00
|
|
|
/// Copy the lib file, so we have an `_active` copy.
|
|
|
|
///
|
|
|
|
/// We do this for all OS's although it is only strictly necessary for windows.
|
|
|
|
/// The reason we do this is to make the code easier to understand and debug.
|
|
|
|
fn copy(lib_path: &PathBuf) {
|
2020-06-25 13:07:34 +00:00
|
|
|
// Use the platform specific names.
|
2020-06-25 04:46:01 +00:00
|
|
|
let lib_compiled_path = lib_path.with_file_name(COMPILED_FILE);
|
|
|
|
let lib_output_path = lib_path.with_file_name(ACTIVE_FILE);
|
|
|
|
|
|
|
|
// Get the path to where the lib was compiled to.
|
|
|
|
debug!(?lib_compiled_path, ?lib_output_path, "Moving.");
|
2020-06-25 13:07:34 +00:00
|
|
|
|
2020-06-25 04:46:01 +00:00
|
|
|
// Copy the library file from where it is output, to where we are going to
|
|
|
|
// load it from i.e. lib_path.
|
|
|
|
std::fs::copy(lib_compiled_path, lib_output_path).expect("Failed to rename dynamic library.");
|
2020-06-17 07:49:14 +00:00
|
|
|
}
|