More reliable method of detecting x86-64

This commit is contained in:
William Venner 2021-12-04 18:07:57 +00:00
parent 44852b8fad
commit efeb394826
4 changed files with 34 additions and 13 deletions

2
Cargo.lock generated
View File

@ -135,7 +135,7 @@ dependencies = [
[[package]]
name = "gmod"
version = "10.0.0"
version = "10.1.0"
dependencies = [
"cfg_table 1.0.0",
"cstr",

View File

@ -1,6 +1,6 @@
[package]
name = "gmod"
version = "10.0.0"
version = "10.1.0"
authors = ["William Venner <william@venner.io>"]
edition = "2018"
license = "MIT"

View File

@ -39,15 +39,36 @@ pub mod userdata;
pub mod net;
/// Returns whether this client is running the x86-64 branch
///
/// Current implementation checks the contents of the bin/ directory, so this is a blocking operation and requires syscalls, use sparingly
pub fn is_x86_64() -> bool {
use std::path::PathBuf;
#[cfg(target_os = "linux")] {
PathBuf::from("bin/linux64").is_dir()
#[cfg(target_pointer_width = "64")] {
// 64-bit can only be x86-64
true
}
#[cfg(target_os = "windows")] {
PathBuf::from("bin/win64").is_dir()
#[cfg(target_pointer_width = "32")] {
lazy_static::lazy_static! {
static ref IS_X86_64: bool = {
use std::path::PathBuf;
// Check LuaJIT version
unsafe {
let (lib, _) = lua::LuaShared::find_lua_shared();
if lib.get::<()>(b"luaJIT_version_2_0_4\0").is_ok() {
false
} else if lib.get::<()>(b"luaJIT_version_2_1_0_beta3\0").is_ok() {
true
} else {
// Check bin folder
#[cfg(target_os = "linux")] {
PathBuf::from("bin/linux64").is_dir()
}
#[cfg(target_os = "windows")] {
PathBuf::from("bin/win64").is_dir()
}
}
}
};
}
*IS_X86_64
}
}

View File

@ -249,27 +249,27 @@ impl LuaShared {
}
#[cfg(all(target_os = "windows", target_pointer_width = "64"))]
unsafe fn find_lua_shared() -> (Library, &'static str) {
pub unsafe fn find_lua_shared() -> (Library, &'static str) {
find_library!("bin/win64/lua_shared.dll")
.expect("Failed to load lua_shared.dll")
}
#[cfg(all(target_os = "windows", target_pointer_width = "32"))]
unsafe fn find_lua_shared() -> (Library, &'static str) {
pub unsafe fn find_lua_shared() -> (Library, &'static str) {
find_library!("garrysmod/bin/lua_shared.dll")
.or_else(|_| find_library!("bin/lua_shared.dll"))
.expect("Failed to load lua_shared.dll")
}
#[cfg(all(target_os = "linux", target_pointer_width = "32"))]
unsafe fn find_lua_shared() -> (Library, &'static str) {
pub unsafe fn find_lua_shared() -> (Library, &'static str) {
find_library!("garrysmod/bin/lua_shared_srv.so")
.or_else(|_| find_library!("bin/linux32/lua_shared.so"))
.expect("Failed to find lua_shared.so or lua_shared_srv.so")
}
#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
unsafe fn find_lua_shared() -> (Library, &'static str) {
pub unsafe fn find_lua_shared() -> (Library, &'static str) {
find_library!("bin/linux64/lua_shared.so")
.expect("Failed to find lua_shared.so")
}