From 9ae3c1a18cdabb4c175279bc84077ddd71927373 Mon Sep 17 00:00:00 2001 From: William Venner Date: Fri, 10 Sep 2021 18:09:31 +0100 Subject: [PATCH] Add some wacky macros --- gmod/src/hax.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ gmod/src/lib.rs | 8 ++++++- 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 gmod/src/hax.rs diff --git a/gmod/src/hax.rs b/gmod/src/hax.rs new file mode 100644 index 0000000..ace999b --- /dev/null +++ b/gmod/src/hax.rs @@ -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 + }; +} \ No newline at end of file diff --git a/gmod/src/lib.rs b/gmod/src/lib.rs index 50b4ba4..403f61b 100644 --- a/gmod/src/lib.rs +++ b/gmod/src/lib.rs @@ -7,5 +7,11 @@ pub use cstr; pub use ctor::{ctor as dllopen, dtor as dllclose}; pub use gmod_macros::*; +/// Lua interface pub mod lua; -pub mod msgc; \ No newline at end of file + +/// Colorful printing +pub mod msgc; + +/// Advanced dark magic utilities +pub mod hax; \ No newline at end of file