1
0
Fork 0

Add common struct

This commit is contained in:
Florian RICHER 2022-01-28 22:06:47 +01:00
parent cdb82d6ccc
commit 4b94fa645c
10 changed files with 65 additions and 25 deletions

8
Cargo.lock generated
View file

@ -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"

View file

@ -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
View 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"

View 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]

View 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,
}

View file

@ -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",
]

View file

@ -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"]

View file

@ -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)
}

View file

@ -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());
}

View file

@ -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()
}
}
}