byondapi_sys/
lib.rs

1// Ignore style warnings for for byondapi-c bindings
2#![allow(
3    clippy::missing_safety_doc,
4    non_upper_case_globals,
5    non_camel_case_types,
6    non_snake_case
7)]
8use std::ops::Deref;
9
10#[cfg(not(target_pointer_width = "32"))]
11compile_error!("BYOND API only functions with 32-bit targets");
12
13#[cfg(not(target_arch = "x86"))]
14compile_error!("BYOND API only functions on x86 targets");
15
16#[cfg(not(any(target_os = "linux", target_os = "windows")))]
17compile_error!("BYOND API only supports Windows and Linux");
18
19// Include byondapi-c bindings (generated by build.rs)
20#[allow(dead_code, rustdoc::broken_intra_doc_links)]
21mod byond_rawbind {
22    #[cfg(feature = "byond-515-1621")]
23    include!(concat!(env!("OUT_DIR"), "/bindings_515_1621.rs"));
24    #[cfg(feature = "byond-516-1651")]
25    include!(concat!(env!("OUT_DIR"), "/bindings_516_1651.rs"));
26}
27
28#[cfg(doc)]
29pub use byond_rawbind::ByondApi as RawByondApi;
30
31/// we must simply hope this never changes
32mod version {
33    use super::byond_rawbind::u4c;
34
35    pub unsafe fn get_byond_version(library: &libloading::Library) -> (u32, u32) {
36        let Byond_GetVersion: unsafe extern "C" fn(version: *mut u4c, build: *mut u4c) = library
37            .get(b"Byond_GetVersion\0")
38            .map(|sym| *sym)
39            .expect("Failed to find Byond_GetVersion");
40
41        let mut version = 0;
42        let mut build = 0;
43
44        Byond_GetVersion(&mut version, &mut build);
45
46        (version, build)
47    }
48}
49
50pub struct ByondApi {
51    internal: byond_rawbind::ByondApi,
52    version: (u32, u32),
53}
54
55unsafe impl Sync for ByondApi {}
56unsafe impl Send for ByondApi {}
57
58impl ByondApi {
59    pub unsafe fn init_from_library<L>(library: L) -> Result<ByondApi, libloading::Error>
60    where
61        L: Into<::libloading::Library>,
62    {
63        let lib = library.into();
64        let version = version::get_byond_version(&lib);
65
66        let internal = byond_rawbind::ByondApi::from_library(lib)?;
67
68        Ok(ByondApi { internal, version })
69    }
70
71    pub fn get_version(&self) -> (u32, u32) {
72        self.version
73    }
74}
75
76impl Deref for ByondApi {
77    type Target = byond_rawbind::ByondApi;
78
79    #[inline]
80    fn deref(&self) -> &Self::Target {
81        &self.internal
82    }
83}
84
85// Stabilized types
86pub use byond_rawbind::s1c;
87pub use byond_rawbind::s1cMAX;
88pub use byond_rawbind::s1cMIN;
89pub use byond_rawbind::s2c;
90pub use byond_rawbind::s2cMAX;
91pub use byond_rawbind::s2cMIN;
92pub use byond_rawbind::s4c;
93pub use byond_rawbind::s4cMAX;
94pub use byond_rawbind::s4cMIN;
95pub use byond_rawbind::s8c;
96pub use byond_rawbind::u1c;
97pub use byond_rawbind::u2c;
98pub use byond_rawbind::u4c;
99pub use byond_rawbind::u4cOrPointer;
100pub use byond_rawbind::u8c;
101pub use byond_rawbind::ByondCallback;
102pub use byond_rawbind::ByondValueData;
103pub use byond_rawbind::ByondValueType;
104#[cfg(feature = "byond-516-1651")]
105pub use byond_rawbind::CByondPixLoc;
106pub use byond_rawbind::CByondValue;
107pub use byond_rawbind::CByondXYZ;