Add manager

This commit is contained in:
Michael Mikovsky
2025-11-06 00:01:46 -07:00
parent ad6e6ffef2
commit 3e0d927465
15 changed files with 137 additions and 156 deletions
-16
View File
@@ -1,16 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "log"
version = "0.4.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432"
[[package]]
name = "unshell-logger"
version = "0.1.0"
dependencies = [
"log",
]
-7
View File
@@ -1,7 +0,0 @@
[package]
name = "unshell-logger"
version = "0.1.0"
edition = "2024"
[dependencies]
log = "0.4.28"
+1 -8
View File
@@ -129,13 +129,6 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "unshell-logger"
version = "0.1.0"
dependencies = [
"log",
]
[[package]] [[package]]
name = "unshell-module-load" name = "unshell-module-load"
version = "0.1.0" version = "0.1.0"
@@ -143,7 +136,6 @@ dependencies = [
"libloading", "libloading",
"log", "log",
"pretty_env_logger", "pretty_env_logger",
"unshell-logger",
"unshell-modules", "unshell-modules",
] ]
@@ -152,6 +144,7 @@ name = "unshell-modules"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"libloading", "libloading",
"log",
] ]
[[package]] [[package]]
+1 -1
View File
@@ -9,7 +9,7 @@ edition = "2024"
libloading = "0.8.9" libloading = "0.8.9"
log = "0.4.28" log = "0.4.28"
pretty_env_logger = "0.5.0" pretty_env_logger = "0.5.0"
unshell-logger = {path = "../unshell-logger"} # unshell-logger = {path = "../unshell-logger"}
unshell-modules = {path = "../unshell-modules"} unshell-modules = {path = "../unshell-modules"}
[profile.release] [profile.release]
+21 -53
View File
@@ -1,10 +1,15 @@
// #![no]
#[macro_use] #[macro_use]
extern crate log; extern crate log;
use libloading::{Library, Symbol}; mod manager;
use log::{info, warn};
use unshell_logger::SetupLogger; use std::sync::{Arc, Mutex};
use unshell_modules::module_interface;
use unshell_modules::{Module, ModuleError, module_interface};
use crate::manager::Manager;
module_interface! { module_interface! {
Interface { Interface {
@@ -14,50 +19,7 @@ module_interface! {
} }
} }
#[allow(dead_code)] // const modules: Arc<Mutex<Vec<Module>>> = Arc::new(Mutex::new(Vec::new()));
#[derive(Debug)]
enum ModuleError {
LibLoadingError(libloading::Error),
LinkError(String),
}
struct Module {
name: String,
lib: Library,
}
impl Module {
pub fn new(path: &str) -> Result<Self, ModuleError> {
let lib = unsafe { Library::new(&path) }.map_err(|e| ModuleError::LibLoadingError(e))?;
Ok(Self {
name: path.to_owned(),
lib,
})
}
pub fn get_symbol<T>(&self, symbol: &[u8]) -> Result<Symbol<'_, T>, ModuleError> {
let symbol = unsafe { self.lib.get::<T>(symbol) }
.map_err(|e| ModuleError::LinkError(format!("Failed to load symbol: {}", e)))?;
Ok(symbol)
}
pub fn init_logger(&self) {
if let Ok(setup_logger) = self.get_symbol::<SetupLogger>(b"setup_logger") {
setup_logger(log::logger(), log::max_level()).unwrap();
} else {
warn!("setup_logger not found");
}
}
pub fn get_interface<T>(&self) -> Result<T, ModuleError> {
if let Ok(interface_function) = self.get_symbol::<fn() -> T>(b"interface") {
Ok(interface_function())
} else {
Err(ModuleError::LinkError(format!(
"Interface function not found!"
)))
}
}
}
fn main() { fn main() {
pretty_env_logger::init(); pretty_env_logger::init();
@@ -65,13 +27,19 @@ fn main() {
info!("Initalized"); info!("Initalized");
match || -> Result<(), ModuleError> { match || -> Result<(), ModuleError> {
let module = let args = std::env::args();
Module::new("../unshell-module-test/target/release/libunshell_module_test.so")?;
module.init_logger();
let interface = module.get_interface::<Interface>()?; let mut modules = Vec::new();
for arg in args.skip(1) {
modules.push(Module::new(&arg)?)
}
let _manager = Manager::new(modules);
interface.test1(); // for i in 1..args.len() {}
// let interface = module.get_interface::<Interface>()?;
// interface.test1();
Ok(()) Ok(())
}() { }() {
+31
View File
@@ -0,0 +1,31 @@
use unshell_modules::{ManagerInterface, Module};
pub struct Manager {
modules: Vec<Module>,
}
impl Manager {
pub fn new(modules: Vec<Module>) -> Self {
let this = Self { modules };
let interface = this.get_interface();
for module in &this.modules {
if let Ok(init) = module.get_symbol::<fn(ManagerInterface)>(b"init") {
init(interface.clone());
} else {
warn!("init not found");
}
}
this
}
pub fn get_interface(&self) -> ManagerInterface {
ManagerInterface::from_raw(Self::test123)
}
pub extern "C" fn test123() {
info!("Manager Test Sucsessfull!");
}
}
+1 -8
View File
@@ -24,19 +24,11 @@ version = "0.4.28"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432"
[[package]]
name = "unshell-logger"
version = "0.1.0"
dependencies = [
"log",
]
[[package]] [[package]]
name = "unshell-module-test" name = "unshell-module-test"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"log", "log",
"unshell-logger",
"unshell-modules", "unshell-modules",
] ]
@@ -45,6 +37,7 @@ name = "unshell-modules"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"libloading", "libloading",
"log",
] ]
[[package]] [[package]]
+1 -1
View File
@@ -19,6 +19,6 @@ debug = false
[dependencies] [dependencies]
log = "0.4.28" log = "0.4.28"
unshell-logger = {path = "../unshell-logger"} # unshell-logger = {path = "../unshell-logger"}
unshell-modules = {path = "../unshell-modules"} unshell-modules = {path = "../unshell-modules"}
# log = "0.4.28" # log = "0.4.28"
+7 -4
View File
@@ -2,8 +2,11 @@
#[macro_use] #[macro_use]
extern crate log; extern crate log;
pub use unshell_logger::setup_logger; use std::thread::{self, Thread};
use unshell_modules::module_interface;
// pub use unshell_logger::setup_logger;
pub use unshell_modules::setup_logger;
use unshell_modules::{ManagerInterface, module_interface};
extern "C" fn test1() { extern "C" fn test1() {
warn!("Test1 called"); warn!("Test1 called");
@@ -29,6 +32,6 @@ pub fn interface() -> Interface {
} }
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
pub fn testfunc() { pub fn init(interface: ManagerInterface) {
info!("testfunc called"); thread::spawn(|| {});
} }
+7
View File
@@ -18,11 +18,18 @@ dependencies = [
"windows-link", "windows-link",
] ]
[[package]]
name = "log"
version = "0.4.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432"
[[package]] [[package]]
name = "unshell-modules" name = "unshell-modules"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"libloading", "libloading",
"log",
] ]
[[package]] [[package]]
+1
View File
@@ -9,3 +9,4 @@ default = ["exec"]
[dependencies] [dependencies]
libloading = {version = "0.8.9", optional = true} libloading = {version = "0.8.9", optional = true}
log = "0.4.28"
-32
View File
@@ -1,32 +0,0 @@
// enum CallableArg {
// Void,
// String,
// Int32,
// Uint32,
// }
// enum CallableArgFilled {
// String(String),
// Int32(i32),
// Uint32(u32),
// }
// struct Callable {
// name: String,
// args: Vec<CallableArg>,
// return_type: CallableArg,
// }
// impl Callable {
// fn new(name: String, args: Vec<CallableArg>) -> Self {
// Callable { name, args }
// }
// fn call(&self, args: Vec<CallableArgFilled>, lib: &libloading::Library) -> Result<(), String> {
// unsafe {
// //TODO: Call the function with the given arguments
// let func = lib.get::< (Find function type) >(self.name.as_bytes());
// unsafe { func() };
// }
// Ok(())
// }
// }
+20 -26
View File
@@ -1,3 +1,18 @@
#[macro_use]
extern crate log;
mod logger;
mod module;
pub use logger::setup_logger;
pub use module::Module;
#[derive(Debug)]
pub enum ModuleError {
LibLoadingError(libloading::Error),
LinkError(String),
}
#[macro_export] #[macro_export]
macro_rules! module_interface { macro_rules! module_interface {
($interface_name:ident { $(fn $fn_name:ident($($arg:ident : $ty:ty),* $(,)?) $(-> $ret:ty)?);* $(;)? }) => { ($interface_name:ident { $(fn $fn_name:ident($($arg:ident : $ty:ty),* $(,)?) $(-> $ret:ty)?);* $(;)? }) => {
@@ -34,30 +49,9 @@ macro_rules! module_interface {
} }
}; };
} }
// mod callable;
// pub enum Error { module_interface! {
// LibLoadingError(libloading::Error), ManagerInterface {
// } fn test123();
}
// #[cfg(feature = "exec")] }
// pub struct Module {
// lib: libloading::Library,
// }
// impl Module {
// pub fn new(path: &str) -> Result<Self, Error> {
// let lib =
// unsafe { libloading::Library::new(path) }.map_err(|e| Error::LibLoadingError(e))?;
// Ok(Module { lib })
// }
// pub fn functions(&self) {
// // self.lib.
// // self.lib.get(name).map_err(|e| Error::LibLoadingError(e))
// }
// }
// pub use callable::Callable;
// pub use callable::Function;
// pub use callable::wrap;
+46
View File
@@ -0,0 +1,46 @@
use libloading::{Library, Symbol};
use crate::{ModuleError, logger::SetupLogger};
pub struct Module {
// name: String,
lib: Library,
}
impl Module {
pub fn new(path: &str) -> Result<Self, ModuleError> {
let lib = unsafe { Library::new(&path) }.map_err(|e| ModuleError::LibLoadingError(e))?;
let this = Self {
// name: path.to_owned(),
lib,
};
if let Ok(setup_logger) = this.get_symbol::<SetupLogger>(b"setup_logger") {
setup_logger(log::logger(), log::max_level()).unwrap();
} else {
warn!("setup_logger not found");
}
Ok(this)
}
pub fn get_symbol<T>(&self, symbol: &[u8]) -> Result<Symbol<'_, T>, ModuleError> {
let symbol = unsafe { self.lib.get::<T>(symbol) }
.map_err(|e| ModuleError::LinkError(format!("Failed to load symbol: {}", e)))?;
Ok(symbol)
}
pub fn get_interface<T>(&self) -> Result<T, ModuleError> {
if let Ok(interface_function) = self.get_symbol::<fn() -> T>(b"interface") {
Ok(interface_function())
} else {
Err(ModuleError::LinkError(format!(
"Interface function not found!"
)))
}
}
}
// extern "C" fn test1234() {
// info!("Test1234!");
// }