mirror of
https://github.com/Astatin3/unshell.git
synced 2026-06-08 22:38:01 -06:00
47 lines
1.3 KiB
Rust
47 lines
1.3 KiB
Rust
use libloading::{Library, Symbol};
|
|
|
|
use crate::{ModuleError, module::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()).map_err(|e| ModuleError::LogError(e))?;
|
|
} 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!");
|
|
// }
|