Add some wacky macros

This commit is contained in:
William Venner 2021-09-10 18:09:31 +01:00
parent 6a3f374c96
commit 9ae3c1a18c
2 changed files with 66 additions and 1 deletions

59
gmod/src/hax.rs Normal file
View File

@ -0,0 +1,59 @@
#[macro_export]
macro_rules! __vtable_offset {
($name:ident = {
win64: $win64:literal,
win32: $win32:literal,
linux64: $linux64:literal,
linux32: $linux32:literal
}) => {
#[cfg(all(target_os = "windows", target_pointer_width = "64"))]
pub const $name: usize = $win64;
#[cfg(all(target_os = "windows", target_pointer_width = "32"))]
pub const $name: usize = $win32;
#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
pub const $name: usize = $linux64;
#[cfg(all(target_os = "linux", target_pointer_width = "32"))]
pub const $name: usize = $linux32;
};
}
#[macro_export]
macro_rules! __vtable_func {
($ty:ident = extern fn($($ident:ident: $arg:ty),*) $(-> $rtn:ty)?) => {
#[cfg(target_pointer_width = "64")]
pub type $ty = extern "fastcall" fn($($ident: $arg),*) $(-> $rtn)?;
#[cfg(all(target_os = "windows", target_pointer_width = "32"))]
pub type $ty = extern "thiscall" fn($($ident: $arg),*) $(-> $rtn)?;
#[cfg(all(target_os = "linux", target_pointer_width = "32"))]
pub type $ty = extern "C" fn($($ident: $arg),*) $(-> $rtn)?;
}
}
#[macro_export]
macro_rules! __hook_func {
($ty:ident = extern fn $fn:ident($($ident:ident: $arg:ty),*) $(-> $rtn:ty)? $code:block) => {
#[cfg(target_pointer_width = "64")]
type $ty = extern "fastcall" fn($($ident: $arg),*) $(-> $rtn)?;
#[cfg(all(target_os = "windows", target_pointer_width = "32"))]
type $ty = extern "thiscall" fn($($ident: $arg),*) $(-> $rtn)?;
#[cfg(all(target_os = "linux", target_pointer_width = "32"))]
type $ty = extern "C" fn($($ident: $arg),*) $(-> $rtn)?;
#[cfg(target_pointer_width = "64")]
extern "fastcall" fn $fn($($ident: $arg),*) $(-> $rtn)? $code
#[cfg(all(target_os = "windows", target_pointer_width = "32"))]
extern "thiscall" fn $fn($($ident: $arg),*) $(-> $rtn)? $code
#[cfg(all(target_os = "linux", target_pointer_width = "32"))]
extern "C" fn $fn($($ident: $arg),*) $(-> $rtn)? $code
};
}

View File

@ -7,5 +7,11 @@ pub use cstr;
pub use ctor::{ctor as dllopen, dtor as dllclose}; pub use ctor::{ctor as dllopen, dtor as dllclose};
pub use gmod_macros::*; pub use gmod_macros::*;
/// Lua interface
pub mod lua; pub mod lua;
pub mod msgc;
/// Colorful printing
pub mod msgc;
/// Advanced dark magic utilities
pub mod hax;