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
+7
View File
@@ -18,11 +18,18 @@ dependencies = [
"windows-link",
]
[[package]]
name = "log"
version = "0.4.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432"
[[package]]
name = "unshell-modules"
version = "0.1.0"
dependencies = [
"libloading",
"log",
]
[[package]]
+1
View File
@@ -9,3 +9,4 @@ default = ["exec"]
[dependencies]
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_rules! module_interface {
($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 {
// LibLoadingError(libloading::Error),
// }
// #[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;
module_interface! {
ManagerInterface {
fn test123();
}
}
+16
View File
@@ -0,0 +1,16 @@
#![allow(improper_ctypes_definitions)]
use log::{LevelFilter, Log, SetLoggerError};
#[allow(dead_code)]
pub type SetupLogger =
extern "C" fn(logger: &'static dyn Log, level: LevelFilter) -> Result<(), SetLoggerError>;
#[unsafe(no_mangle)]
pub extern "C" fn setup_logger(
logger: &'static dyn log::Log,
level: log::LevelFilter,
) -> Result<(), log::SetLoggerError> {
log::set_max_level(level);
log::set_logger(logger)
}
+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!");
// }