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"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"glob",
|
"glob",
|
||||||
|
"hotload_plugin",
|
||||||
"hotload_toto1",
|
"hotload_toto1",
|
||||||
"libloading",
|
"libloading",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hotload_plugin"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hotload_toto1"
|
name = "hotload_toto1"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"hotload_plugin",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libloading"
|
name = "libloading"
|
||||||
|
|
|
@ -8,5 +8,5 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
libloading = "0.7"
|
libloading = "0.7"
|
||||||
glob = "0.3"
|
glob = "0.3"
|
||||||
# hotload_plugin = { path = "./crates/hotload_plugin" }
|
hotload_plugin = { path = "./crates/hotload_plugin" }
|
||||||
hotload_toto1 = { path = "./plugins/hotload_toto1" }
|
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.
|
# It is not intended for manual editing.
|
||||||
version = 3
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hotload_plugin"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hotload_toto1"
|
name = "hotload_toto1"
|
||||||
version = "0.1.0"
|
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
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
# hotload_plugin = { path = "../../crates/hotload_plugin" }
|
hotload_plugin = { path = "../../crates/hotload_plugin" }
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
crate-type = ["dylib"]
|
crate-type = ["dylib"]
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
#[no_mangle]
|
use hotload_plugin::HotloadPlugin;
|
||||||
pub extern fn version() -> String {
|
|
||||||
"0.2.0".to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern fn name() -> String {
|
pub extern "C" fn register() -> *const HotloadPlugin {
|
||||||
"test name".to_string()
|
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() {
|
fn main() {
|
||||||
let plugin_manager = plugin_manager::PluginManager::new();
|
let plugin_manager = plugin_manager::PluginManager::new();
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
for plugin in &plugin_manager.plugins {
|
||||||
|
println!("Plugin {:?}", (*(*plugin)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
println!("Plugins number : {}", plugin_manager.plugins.len());
|
println!("Plugins number : {}", plugin_manager.plugins.len());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,10 @@
|
||||||
use glob::glob;
|
use glob::glob;
|
||||||
|
use hotload_plugin::HotloadPlugin;
|
||||||
use std::env::current_exe;
|
use std::env::current_exe;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct Plugin {
|
|
||||||
pub version: String,
|
|
||||||
pub name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct PluginManager {
|
pub struct PluginManager {
|
||||||
pub plugins: Vec<Plugin>,
|
pub plugins: Vec<*const HotloadPlugin>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PluginManager {
|
impl PluginManager {
|
||||||
|
@ -21,25 +16,19 @@ impl PluginManager {
|
||||||
let lib_folder = parent_path.join("*.so");
|
let lib_folder = parent_path.join("*.so");
|
||||||
for lib_file in glob(lib_folder.to_str().unwrap()).unwrap() {
|
for lib_file in glob(lib_folder.to_str().unwrap()).unwrap() {
|
||||||
let plugin = Self::load_library(lib_file.unwrap());
|
let plugin = Self::load_library(lib_file.unwrap());
|
||||||
println!("Loaded plugin {:?}", plugin);
|
|
||||||
plugins.push(plugin);
|
plugins.push(plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
PluginManager { plugins }
|
PluginManager { plugins }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_library(lib_file: PathBuf) -> Plugin {
|
fn load_library(lib_file: PathBuf) -> *const HotloadPlugin {
|
||||||
println!("Loading library {:?}", lib_file);
|
println!("Loading library {:?}", lib_file);
|
||||||
unsafe {
|
unsafe {
|
||||||
let lib = libloading::Library::new(lib_file).unwrap();
|
let lib = libloading::Library::new(lib_file).unwrap();
|
||||||
let version_func: libloading::Symbol<extern "C" fn() -> String> =
|
let register_func: libloading::Symbol<extern "C" fn() -> *const HotloadPlugin> =
|
||||||
lib.get(b"version").unwrap();
|
lib.get(b"register").unwrap();
|
||||||
let name_func: libloading::Symbol<extern "C" fn() -> String> =
|
register_func()
|
||||||
lib.get(b"name").unwrap();
|
|
||||||
Plugin {
|
|
||||||
version: version_func(),
|
|
||||||
name: name_func(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue