From 56c6a70866a35f979c4d79789a200ff888b0b0ab Mon Sep 17 00:00:00 2001 From: William Venner Date: Sat, 5 Feb 2022 19:39:07 +0000 Subject: [PATCH] MacOS support --- gmod/src/gmcl.rs | 2 +- gmod/src/lib.rs | 51 ++++++++++++++++++++++++++++++++++-------- gmod/src/lua/import.rs | 12 ++++++++++ gmod/src/msgc.rs | 11 ++++----- 4 files changed, 59 insertions(+), 17 deletions(-) diff --git a/gmod/src/gmcl.rs b/gmod/src/gmcl.rs index a015fd4..006323f 100644 --- a/gmod/src/gmcl.rs +++ b/gmod/src/gmcl.rs @@ -38,7 +38,7 @@ pub fn override_stdout() { } }) } - #[cfg(target_os = "linux")] { + #[cfg(any(target_os = "linux", target_os = "macos"))] { _lib.get(b"ConMsg\0").or_else(|_| _lib.get(b"_Z6ConMsgPKcz\0")) } }.expect("Failed to find ConMsg"); diff --git a/gmod/src/lib.rs b/gmod/src/lib.rs index c6cc96b..76871bc 100644 --- a/gmod/src/lib.rs +++ b/gmod/src/lib.rs @@ -5,14 +5,22 @@ #![cfg_attr(feature = "gmcl", feature(internal_output_capture))] +#[cfg(not(all(any(target_os = "windows", target_os = "linux", target_os = "macos"), any(target_pointer_width = "32", target_pointer_width = "64"))))] +compile_error!("Unsupported platform"); + pub use cstr; pub use libloading; pub use gmod_macros::*; #[cfg(feature = "hax")] mod haxports { - pub use detour; + #[cfg(not(target_os = "macos"))] pub use skidscan as sigscan; + + #[cfg(target_os = "macos")] + compile_error!("Sigscanning is currently not supported on MacOS, please disable the `hax` feature on gmod-rs using `default-features = false` to make a normal module"); + + pub use detour; pub use ctor::{ctor as dllopen, dtor as dllclose}; pub use fn_type_alias::*; @@ -54,6 +62,9 @@ pub fn is_x86_64() -> bool { static ref IS_X86_64: bool = { use std::path::PathBuf; + #[cfg(target_os = "macos")] { + PathBuf::from("garrysmod/bin/lua_shared.dylib").is_file() + } #[cfg(target_os = "windows")] { PathBuf::from("srcds_win64.exe").is_file() } @@ -116,10 +127,6 @@ macro_rules! open_library_raw { #[macro_export] macro_rules! open_library_srv { ($name:literal) => {{ - #[cfg(not(all(any(target_os = "windows", target_os = "linux"), any(target_pointer_width = "32", target_pointer_width = "64"))))] { - compile_error!("Unsupported platform"); - } - #[cfg(all(target_os = "windows", target_pointer_width = "64"))] { $crate::__private__gmod_rs__try_chained_open! { $crate::open_library_raw!("bin/win64/", $name, ".dll") @@ -152,6 +159,21 @@ macro_rules! open_library_srv { $crate::open_library_raw!("garrysmod/bin/lib", $name, ".so") } } + + #[cfg(target_os = "macos")] { + $crate::__private__gmod_rs__try_chained_open! { + $crate::open_library_raw!("GarrysMod_Signed.app/Contents/MacOS/", $name, ".dylib"), + $crate::open_library_raw!("GarrysMod_Signed.app/Contents/MacOS/lib", $name, ".dylib"), + $crate::open_library_raw!("bin/", $name, "_srv.dylib"), + $crate::open_library_raw!("bin/lib", $name, "_srv.dylib"), + $crate::open_library_raw!("garrysmod/bin/", $name, "_srv.dylib"), + $crate::open_library_raw!("garrysmod/bin/lib", $name, "_srv.dylib"), + $crate::open_library_raw!("bin/", $name, ".dylib"), + $crate::open_library_raw!("bin/lib", $name, ".dylib"), + $crate::open_library_raw!("garrysmod/bin/", $name, ".dylib"), + $crate::open_library_raw!("garrysmod/bin/lib", $name, ".dylib") + } + } }}; } @@ -167,10 +189,6 @@ macro_rules! open_library_srv { #[macro_export] macro_rules! open_library { ($name:literal) => {{ - #[cfg(not(all(any(target_os = "windows", target_os = "linux"), any(target_pointer_width = "32", target_pointer_width = "64"))))] { - compile_error!("Unsupported platform"); - } - #[cfg(all(target_os = "windows", target_pointer_width = "64"))] { $crate::__private__gmod_rs__try_chained_open! { $crate::open_library_raw!("bin/win64/", $name, ".dll") @@ -203,6 +221,21 @@ macro_rules! open_library { $crate::open_library_raw!("garrysmod/bin/lib", $name, "_srv.so") } } + + #[cfg(target_os = "macos")] { + $crate::__private__gmod_rs__try_chained_open! { + $crate::open_library_raw!("GarrysMod_Signed.app/Contents/MacOS/", $name, ".dylib"), + $crate::open_library_raw!("GarrysMod_Signed.app/Contents/MacOS/lib", $name, ".dylib"), + $crate::open_library_raw!("bin/", $name, ".dylib"), + $crate::open_library_raw!("bin/lib", $name, ".dylib"), + $crate::open_library_raw!("garrysmod/bin/", $name, ".dylib"), + $crate::open_library_raw!("garrysmod/bin/lib", $name, ".dylib"), + $crate::open_library_raw!("bin/", $name, "_srv.dylib"), + $crate::open_library_raw!("bin/lib", $name, "_srv.dylib"), + $crate::open_library_raw!("garrysmod/bin/", $name, "_srv.dylib"), + $crate::open_library_raw!("garrysmod/bin/lib", $name, "_srv.dylib") + } + } }}; } diff --git a/gmod/src/lua/import.rs b/gmod/src/lua/import.rs index 6573a35..cdaf543 100644 --- a/gmod/src/lua/import.rs +++ b/gmod/src/lua/import.rs @@ -290,4 +290,16 @@ impl LuaShared { crate::open_library_raw!("bin/linux64/lua_shared.so") .expect("Failed to find lua_shared.so") } + + #[cfg(all(target_os = "macos", target_pointer_width = "32"))] + pub unsafe fn find_lua_shared() -> (Library, &'static str) { + crate::open_library_raw!("garrysmod/bin/lua_shared.dylib") + .expect("Failed to find lua_shared.dylib") + } + + #[cfg(all(target_os = "macos", target_pointer_width = "64"))] + pub unsafe fn find_lua_shared() -> (Library, &'static str) { + crate::open_library_raw!("GarrysMod_Signed.app/Contents/MacOS/lua_shared.dylib") + .expect("Failed to find lua_shared.dylib") + } } diff --git a/gmod/src/msgc.rs b/gmod/src/msgc.rs index 16fd11c..a66908c 100644 --- a/gmod/src/msgc.rs +++ b/gmod/src/msgc.rs @@ -35,6 +35,9 @@ lazy_static::lazy_static! { #[cfg(all(target_os = "linux", target_pointer_width = "32"))] let lib = libloading::Library::new("bin/libtier0_srv.so").or_else(|_| libloading::Library::new("bin/linux32/libtier0.so")).expect("Failed to open libtier0.so"); + #[cfg(target_os = "macos")] + let lib = libloading::Library::new("bin/libtier0.dylib").or_else(|_| libloading::Library::new("GarrysMod_Signed.app/Contents/MacOS/libtier0.dylib")).expect("Failed to open libtier0.dylib"); + let lib = Box::leak(Box::new(lib)); { #[cfg(all(target_os = "windows", target_pointer_width = "64"))] { @@ -46,15 +49,9 @@ lazy_static::lazy_static! { Err(_) => lib.get(b"?ConColorMsg@@YAXABVColor@@PBDZZ\0") } } - #[cfg(all(target_os = "linux", target_pointer_width = "64"))] { + #[cfg(any(target_os = "linux", target_os = "macos"))] { lib.get(b"_Z11ConColorMsgRK5ColorPKcz\0") } - #[cfg(all(target_os = "linux", target_pointer_width = "32"))] { - match lib.get(b"_Z11ConColorMsgRK5ColorPKcz\0") { - Ok(symbol) => Ok(symbol), - Err(_) => lib.get(b"_Z11ConColorMsgRK5ColorPKcz\0") - } - } } .expect("Failed to get ConColorMsg") };