Created
March 8, 2016 23:30
-
-
Save dradtke/a13a7b3658463ca7d241 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extern crate dylib; | |
| extern crate notify; | |
| use dylib::DynamicLibrary; | |
| use notify::Watcher; | |
| use std::fs; | |
| use std::mem::transmute; | |
| use std::path::Path; | |
| use std::sync::mpsc::channel; | |
| use std::thread; | |
| use std::time::Duration; | |
| const LIB_PATH: &'static str = "../game/target/debug/libgame.so"; | |
| const FPS: u64 = 1; | |
| fn main() { | |
| let mut lib = DynamicLibrary::open(Some(Path::new(LIB_PATH))).unwrap_or_else(|error| panic!("{}", error)); | |
| let mut get_message: fn() -> &'static str = unsafe { transmute(lib.symbol::<usize>("get_message").unwrap()) }; | |
| let mut needs_reload = false; | |
| let (ns, nr) = channel(); | |
| let mut watcher = notify::new(ns).unwrap(); | |
| watcher.watch(LIB_PATH).unwrap(); | |
| let dur = Duration::from_millis(1000 / FPS); | |
| loop { | |
| thread::sleep(dur); | |
| if let Ok(ref event) = nr.try_recv() { | |
| if let Ok(ref op) = event.op { | |
| if op.contains(notify::op::REMOVE) { | |
| watcher.watch(LIB_PATH).unwrap(); | |
| } else { | |
| needs_reload = true; | |
| } | |
| } | |
| } | |
| if needs_reload && fs::metadata(LIB_PATH).unwrap().len() > 0 { | |
| drop(lib); | |
| lib = DynamicLibrary::open(Some(Path::new(LIB_PATH))).unwrap_or_else(|error| panic!("{}", error)); | |
| get_message = unsafe { | |
| transmute(lib.symbol::<usize>("get_message").unwrap()) | |
| }; | |
| needs_reload = false; | |
| } | |
| println!("message: {}", get_message()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment