Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bp3d-os-build"
version = "1.0.0"
version = "1.1.0"
authors = ["Yuri Edward <[email protected]>"]
edition = "2024"
description = "Operating System tools designed for BlockProject3D"
Expand Down
39 changes: 37 additions & 2 deletions build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,13 @@ impl ModuleMain {
(\"{mod_const_name}\", unsafe {{ &{mod_const_name} as *const *const i8 as *const std::ffi::c_void }})");
let out_path =
PathBuf::from(std::env::var_os("OUT_DIR").unwrap()).join("bp3d_os_module.rs");
Self {
let this = Self {
rust_code,
out_path,
crate_name,
virtual_lib,
}
};
this.add_init().add_uninit()
}

pub fn add_export(mut self, func_name: impl AsRef<str>) -> Self {
Expand All @@ -114,6 +115,40 @@ impl ModuleMain {
self
}

fn add_init(mut self) -> Self {
let motherfuckingrust = "extern \"C\"";
let crate_name = &self.crate_name;
let rust_code = format!(
r"
#[unsafe(no_mangle)]
#[inline(never)]
pub {motherfuckingrust} fn bp3d_os_module_{crate_name}_init(loader: &'static std::sync::Mutex<bp3d_os::module::loader::ModuleLoader>) {{
bp3d_os::module::loader::ModuleLoader::install_from_existing(loader);
}}
"
);
self.rust_code += &rust_code;
let motherfuckingrust = format!("bp3d_os_module_{crate_name}_init");
self.add_export(motherfuckingrust)
}

fn add_uninit(mut self) -> Self {
let motherfuckingrust = "extern \"C\"";
let crate_name = &self.crate_name;
let rust_code = format!(
r"
#[unsafe(no_mangle)]
#[inline(never)]
pub {motherfuckingrust} fn bp3d_os_module_{crate_name}_uninit() {{
bp3d_os::module::loader::ModuleLoader::uninstall();
}}
"
);
self.rust_code += &rust_code;
let motherfuckingrust = format!("bp3d_os_module_{crate_name}_uninit");
self.add_export(motherfuckingrust)
}

pub fn add_open(mut self) -> Self {
let motherfuckingrust = "extern \"C\"";
let crate_name = &self.crate_name;
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bp3d-os"
version = "1.0.2"
version = "2.0.0"
authors = ["Yuri Edward <[email protected]>"]
edition = "2021"
description = "Operating System tools designed for BlockProject3D"
Expand Down
3 changes: 3 additions & 0 deletions core/src/module/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ simple_error! {
/// Missing VERSION key for a module.
MissingModuleVersion => "missing VERSION metadata key",

/// Missing bp3d_os_module_<name>_init symbol for a Rust based module.
MissingModuleInitForRust => "missing module init function for a Rust module",

/// The given string was not UTF8.
InvalidUtf8(Utf8Error) => "invalid utf8: {}",

Expand Down
10 changes: 10 additions & 0 deletions core/src/module/library/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ impl<'a, T> Symbol<'a, T> {
}
}

impl<'a, R> Symbol<'a, extern "Rust" fn() -> R> {
/// Calls this symbol if this symbol is a function.
///
/// returns: R
pub fn call(&self) -> R {
let f: extern "Rust" fn() -> R = unsafe { std::mem::transmute(self.ptr) };
f()
}
}

impl<'a, T, R> Symbol<'a, extern "Rust" fn(T) -> R> {
/// Calls this symbol if this symbol is a function.
///
Expand Down
4 changes: 4 additions & 0 deletions core/src/module/library/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use crate::module;
use crate::module::error::Error;
use crate::module::library::symbol::Symbol;
use bp3d_debug::debug;
use libc::{dlclose, dlopen, dlsym, RTLD_LAZY};
use std::ffi::{c_void, CString};
use std::fmt::Debug;
Expand All @@ -46,6 +47,8 @@ pub const EXT: &str = "so";
#[repr(transparent)]
pub struct Library(*mut c_void);

unsafe impl Send for Library {}

impl Library {
/// Attempts to open a handle to the current running program.
pub fn open_self() -> module::Result<Self> {
Expand Down Expand Up @@ -95,6 +98,7 @@ impl super::Library for Library {

impl Drop for Library {
fn drop(&mut self) {
debug!("dlclose");
unsafe { dlclose(self.0) };
}
}
2 changes: 2 additions & 0 deletions core/src/module/library/virtual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub struct VirtualLibrary {
symbols: &'static [(&'static str, *const c_void)],
}

unsafe impl Send for VirtualLibrary {}

unsafe impl Sync for VirtualLibrary {}

impl VirtualLibrary {
Expand Down
2 changes: 2 additions & 0 deletions core/src/module/library/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub const EXT: &str = "dll";
#[derive(Debug)]
pub struct Library(HMODULE);

unsafe impl Send for Library {}

impl Library {
/// Attempts to open a handle to the current running program.
pub fn open_self() -> module::Result<Self> {
Expand Down
Loading