Reexport utility macros, remove unclear and annoying macros

This commit is contained in:
William Venner 2021-09-24 00:38:14 +01:00
parent 0148e6df88
commit 3d824932dd
4 changed files with 58 additions and 121 deletions

49
Cargo.lock generated
View File

@ -20,6 +20,12 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cfg_table"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f3690291e34881a89ceb8e80802f1582f29b1361fd476eeefb4e89dd6a121e"
[[package]]
name = "cstr"
version = "0.2.8"
@ -56,6 +62,28 @@ dependencies = [
"slice-pool",
]
[[package]]
name = "fn_abi"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8242bb800f5a67e61e17a2035b68596a1481fd5541187cd7f7392d8238482ac"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "fn_type_alias"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "809e807f01217f1f89959fbeebd10c6471b5ef3971fce1b02a70fb7212cc6c1f"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "generic-array"
version = "0.14.4"
@ -68,14 +96,18 @@ dependencies = [
[[package]]
name = "gmod"
version = "2.0.1"
version = "3.0.0"
dependencies = [
"cfg_table",
"cstr",
"ctor",
"detour",
"fn_abi",
"fn_type_alias",
"gmod-macros",
"lazy_static",
"libloading",
"null_fn",
"skidscan",
]
@ -150,6 +182,17 @@ dependencies = [
"winapi 0.2.8",
]
[[package]]
name = "null_fn"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4934ec63b88f0415c46aedf2ab6c753d2d2e354e847c17e03515658207243b9c"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "proc-macro-crate"
version = "1.1.0"
@ -225,9 +268,9 @@ checksum = "733fc6e5f1bd3a8136f842c9bdea4e5f17c910c2fcc98c90c3aa7604ef5e2e7a"
[[package]]
name = "syn"
version = "1.0.76"
version = "1.0.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6f107db402c2c2055242dbf4d2af0e69197202e9faacbef9571bbe47f5a1b84"
checksum = "5239bc68e0fef57495900cfea4e8dc75596d9a319d7e16b1e0a440d24e6fe0a0"
dependencies = [
"proc-macro2",
"quote",

View File

@ -1,6 +1,6 @@
[package]
name = "gmod"
version = "2.0.1"
version = "3.0.0"
authors = ["William Venner <william@venner.io>"]
edition = "2018"
license = "MIT"
@ -17,3 +17,8 @@ cstr = "0"
lazy_static = "1"
ctor = "0"
gmod-macros = { version = "0.1.0", path = "../gmod-macros" }
fn_type_alias = "0"
fn_abi = "0"
cfg_table = "0"
null_fn = "0"

View File

@ -1,120 +1,3 @@
#[macro_export]
/// Common pattern for detouring.
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]
/// Common pattern for detouring.
/// # Example
/// ```norun
/// __callingconv_func! {
/// #[win32 = "stdcall"]
/// #[win64 = "stdcall"]
/// #[linux32 = "C"]
/// #[linux64 = "C"]
/// CVoiceGameMgrHelper_CanPlayerHearPlayer = extern fn(listener: *mut c_void, talker: *mut c_void) -> bool {
/// println!("listener {:?} talker {:?}", listener, talker);
/// false
/// }
/// }
/// ```
macro_rules! __callingconv_func {
{ @callingconv $ty:ident = $vis:vis extern $callingconv:tt fn $fn_ident:ident($($ident:ident: $arg:ty),*) $(-> $rtn:ty)? $code:block } => {
$vis type $ty = extern $callingconv fn($($ident: $arg),*) $(-> $rtn)?;
$vis extern $callingconv fn $fn_ident($($ident: $arg),*) $(-> $rtn)? $code
};
{ @callingconv $ty:ident = $vis:vis extern $callingconv:tt fn($($ident:ident: $arg:ty),*) $(-> $rtn:ty)? } => {
$vis type $ty = extern $callingconv fn($($ident: $arg),*) $(-> $rtn)?;
};
{ #[win32 = $win32:tt] #[win64 = $win64:tt] #[linux32 = $linux32:tt] #[linux64 = $linux64:tt] $ty:ident = $vis:vis extern fn $($fn_ident:ident)?($($ident:ident: $arg:ty),*) $(-> $rtn:ty)? $code:block } => {
#[cfg(all(target_os = "windows", target_pointer_width = "32"))]
$crate::__callingconv_func! { @callingconv $ty = $vis extern $win32 fn $($fn_ident)?($($ident: $arg),*) $(-> $rtn)? $code }
#[cfg(all(target_os = "windows", target_pointer_width = "64"))]
$crate::__callingconv_func! { @callingconv $ty = $vis extern $win64 fn $($fn_ident)?($($ident: $arg),*) $(-> $rtn)? $code }
#[cfg(all(target_os = "linux", target_pointer_width = "32"))]
$crate::__callingconv_func! { @callingconv $ty = $vis extern $linux32 fn $($fn_ident)?($($ident: $arg),*) $(-> $rtn)? $code }
#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
$crate::__callingconv_func! { @callingconv $ty = $vis extern $linux64 fn $($fn_ident)?($($ident: $arg),*) $(-> $rtn)? $code }
};
{ #[win32 = $win32:tt] #[win64 = $win64:tt] #[linux32 = $linux32:tt] #[linux64 = $linux64:tt] $ty:ident = $vis:vis extern fn $($fn_ident:ident)?($($ident:ident: $arg:ty),*) $(-> $rtn:ty)? } => {
#[cfg(all(target_os = "windows", target_pointer_width = "32"))]
$crate::__callingconv_func! { @callingconv $ty = $vis extern $win32 fn $($fn_ident)?($($ident: $arg),*) $(-> $rtn)? }
#[cfg(all(target_os = "windows", target_pointer_width = "64"))]
$crate::__callingconv_func! { @callingconv $ty = $vis extern $win64 fn $($fn_ident)?($($ident: $arg),*) $(-> $rtn)? }
#[cfg(all(target_os = "linux", target_pointer_width = "32"))]
$crate::__callingconv_func! { @callingconv $ty = $vis extern $linux32 fn $($fn_ident)?($($ident: $arg),*) $(-> $rtn)? }
#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
$crate::__callingconv_func! { @callingconv $ty = $vis extern $linux64 fn $($fn_ident)?($($ident: $arg),*) $(-> $rtn)? }
};
}
#[macro_export]
/// Common pattern for detouring.
macro_rules! __gmod_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]
/// Common pattern for detouring.
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
};
}
#[macro_export]
/// Common pattern for detouring.
macro_rules! find_gmod_signature {

View File

@ -7,6 +7,12 @@ pub use cstr;
pub use ctor::{ctor as dllopen, dtor as dllclose};
pub use gmod_macros::*;
// Reexport common macros
pub use fn_type_alias::*;
pub use fn_abi::*;
pub use cfg_table::*;
pub use null_fn::*;
/// Lua interface
pub mod lua;