Add common struct
This commit is contained in:
parent
cdb82d6ccc
commit
4b94fa645c
10 changed files with 65 additions and 25 deletions
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -19,13 +19,21 @@ name = "hotload"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"glob",
|
||||
"hotload_plugin",
|
||||
"hotload_toto1",
|
||||
"libloading",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hotload_plugin"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "hotload_toto1"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"hotload_plugin",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libloading"
|
||||
|
|
|
@ -8,5 +8,5 @@ edition = "2021"
|
|||
[dependencies]
|
||||
libloading = "0.7"
|
||||
glob = "0.3"
|
||||
# hotload_plugin = { path = "./crates/hotload_plugin" }
|
||||
hotload_plugin = { path = "./crates/hotload_plugin" }
|
||||
hotload_toto1 = { path = "./plugins/hotload_toto1" }
|
||||
|
|
7
crates/hotload_plugin/Cargo.lock
generated
Normal file
7
crates/hotload_plugin/Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "hotload_plugin"
|
||||
version = "0.1.0"
|
8
crates/hotload_plugin/Cargo.toml
Normal file
8
crates/hotload_plugin/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "hotload_plugin"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
12
crates/hotload_plugin/src/lib.rs
Normal file
12
crates/hotload_plugin/src/lib.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
#[repr(C)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HotloadPlugin {
|
||||
// The plugin's name
|
||||
pub name: String,
|
||||
|
||||
// The plugin's version
|
||||
pub version: String,
|
||||
|
||||
// The plugin's author
|
||||
pub author: String,
|
||||
}
|
7
plugins/hotload_toto1/Cargo.lock
generated
7
plugins/hotload_toto1/Cargo.lock
generated
|
@ -2,6 +2,13 @@
|
|||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "hotload_plugin"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "hotload_toto1"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"hotload_plugin",
|
||||
]
|
||||
|
|
|
@ -6,7 +6,7 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
# hotload_plugin = { path = "../../crates/hotload_plugin" }
|
||||
hotload_plugin = { path = "../../crates/hotload_plugin" }
|
||||
|
||||
[lib]
|
||||
crate-type = ["dylib"]
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
#[no_mangle]
|
||||
pub extern fn version() -> String {
|
||||
"0.2.0".to_string()
|
||||
}
|
||||
use hotload_plugin::HotloadPlugin;
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn name() -> String {
|
||||
"test name".to_string()
|
||||
pub extern "C" fn register() -> *const HotloadPlugin {
|
||||
let plugin = Box::new(HotloadPlugin {
|
||||
name: String::from("test_lib"),
|
||||
version: String::from("0.1.0"),
|
||||
author: String::from("mrdev023"),
|
||||
});
|
||||
|
||||
Box::into_raw(plugin)
|
||||
}
|
||||
|
|
|
@ -3,5 +3,11 @@ mod plugin_manager;
|
|||
fn main() {
|
||||
let plugin_manager = plugin_manager::PluginManager::new();
|
||||
|
||||
unsafe {
|
||||
for plugin in &plugin_manager.plugins {
|
||||
println!("Plugin {:?}", (*(*plugin)));
|
||||
}
|
||||
}
|
||||
|
||||
println!("Plugins number : {}", plugin_manager.plugins.len());
|
||||
}
|
||||
|
|
|
@ -1,15 +1,10 @@
|
|||
use glob::glob;
|
||||
use hotload_plugin::HotloadPlugin;
|
||||
use std::env::current_exe;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Plugin {
|
||||
pub version: String,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
pub struct PluginManager {
|
||||
pub plugins: Vec<Plugin>,
|
||||
pub plugins: Vec<*const HotloadPlugin>,
|
||||
}
|
||||
|
||||
impl PluginManager {
|
||||
|
@ -21,25 +16,19 @@ impl PluginManager {
|
|||
let lib_folder = parent_path.join("*.so");
|
||||
for lib_file in glob(lib_folder.to_str().unwrap()).unwrap() {
|
||||
let plugin = Self::load_library(lib_file.unwrap());
|
||||
println!("Loaded plugin {:?}", plugin);
|
||||
plugins.push(plugin);
|
||||
}
|
||||
|
||||
PluginManager { plugins }
|
||||
}
|
||||
|
||||
fn load_library(lib_file: PathBuf) -> Plugin {
|
||||
fn load_library(lib_file: PathBuf) -> *const HotloadPlugin {
|
||||
println!("Loading library {:?}", lib_file);
|
||||
unsafe {
|
||||
let lib = libloading::Library::new(lib_file).unwrap();
|
||||
let version_func: libloading::Symbol<extern "C" fn() -> String> =
|
||||
lib.get(b"version").unwrap();
|
||||
let name_func: libloading::Symbol<extern "C" fn() -> String> =
|
||||
lib.get(b"name").unwrap();
|
||||
Plugin {
|
||||
version: version_func(),
|
||||
name: name_func(),
|
||||
}
|
||||
let register_func: libloading::Symbol<extern "C" fn() -> *const HotloadPlugin> =
|
||||
lib.get(b"register").unwrap();
|
||||
register_func()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue