Skip to content

Commit c4e2876

Browse files
authored
byondapi-sys improvements and ffi documentation (#10)
1 parent 5a0ddd7 commit c4e2876

File tree

4 files changed

+56
-278
lines changed

4 files changed

+56
-278
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.h text linguist-vendored
2+
*.hpp text linguist-vendored

crates/byondapi-sys/Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ default-target = "i686-unknown-linux-gnu"
1717
targets = [] # Do not build the doc with any other target than the default.
1818

1919
[dependencies]
20-
libloading = "0.8.1"
20+
libloading = "0.8"
2121

2222
[build-dependencies]
23-
bindgen = "0.69.1"
24-
rustc_version = "0.4.0"
25-
walkdir = "2.4.0"
23+
bindgen = "0.69"
24+
rustc_version = "0.4"
25+
walkdir = "2"
26+
doxygen-rs = "0.4"
2627

2728
[features]
2829
default = ["byond-515-1621"]

crates/byondapi-sys/build.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use bindgen::callbacks::ParseCallbacks;
12
use std::path::{Path, PathBuf};
23

34
fn main() {
@@ -43,22 +44,34 @@ fn copy_wrapper(lib_dir: &Path) -> PathBuf {
4344
fn generate_all() {
4445
let out_dir = PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR not defined"));
4546

46-
get_headers().iter().for_each(|(path, version)| {
47-
let target = out_dir.join("byondapi.h");
48-
std::fs::copy(path, target).expect("Failed to copy to out_dir");
49-
let wrapper = copy_wrapper(&out_dir);
47+
get_headers()
48+
.into_iter()
49+
.for_each(|(path, (major, minor))| {
50+
let target = out_dir.join("byondapi.h");
51+
std::fs::copy(path, target).expect("Failed to copy to out_dir");
52+
let wrapper = copy_wrapper(&out_dir);
5053

51-
let builder = bindgen::Builder::default()
52-
.header(wrapper.to_string_lossy())
53-
.dynamic_library_name("ByondApi")
54-
.dynamic_link_require_all(true)
55-
// Also make headers included by main header dependencies of the build
56-
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()));
54+
let builder = bindgen::Builder::default()
55+
.header(wrapper.to_string_lossy())
56+
.dynamic_library_name("ByondApi")
57+
.dynamic_link_require_all(true)
58+
// Also make headers included by main header dependencies of the build
59+
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
60+
.parse_callbacks(Box::new(DoxygenCallbacks));
5761

58-
builder
59-
.generate()
60-
.expect("Unable to generate bindings")
61-
.write_to_file(out_dir.join(format!("bindings_{}_{}.rs", version.0, version.1)))
62-
.expect("Couldn't write bindings!");
63-
});
62+
builder
63+
.generate()
64+
.expect("Unable to generate bindings")
65+
.write_to_file(out_dir.join(format!("bindings_{major}_{minor}.rs")))
66+
.expect("Couldn't write bindings!");
67+
});
68+
}
69+
70+
#[derive(Debug)]
71+
struct DoxygenCallbacks;
72+
73+
impl ParseCallbacks for DoxygenCallbacks {
74+
fn process_comment(&self, comment: &str) -> Option<String> {
75+
Some(doxygen_rs::transform(comment))
76+
}
6477
}

crates/byondapi-sys/src/lib.rs

Lines changed: 20 additions & 258 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Ignore style warnings for for byondapi-c bindings
2-
#![allow(non_upper_case_globals)]
3-
#![allow(non_camel_case_types)]
4-
#![allow(non_snake_case)]
5-
#![allow(clippy::missing_safety_doc)]
6-
use std::ffi::c_char;
2+
#![allow(
3+
clippy::missing_safety_doc,
4+
non_upper_case_globals,
5+
non_camel_case_types,
6+
non_snake_case
7+
)]
8+
use std::ops::Deref;
79

810
#[cfg(not(target_pointer_width = "32"))]
911
compile_error!("BYOND API only functions with 32-bit targets");
@@ -15,12 +17,15 @@ compile_error!("BYOND API only functions on x86 targets");
1517
compile_error!("BYOND API only supports Windows and Linux");
1618

1719
// Include byondapi-c bindings (generated by build.rs)
18-
#[allow(dead_code)]
20+
#[allow(dead_code, rustdoc::broken_intra_doc_links)]
1921
mod byond_rawbind {
2022
#[cfg(feature = "byond-515-1621")]
2123
include!(concat!(env!("OUT_DIR"), "/bindings_515_1621.rs"));
2224
}
2325

26+
#[cfg(doc)]
27+
pub use byond_rawbind::ByondApi as RawByondApi;
28+
2429
/// we must simply hope this never changes
2530
mod version {
2631
use super::byond_rawbind::u4c;
@@ -66,6 +71,15 @@ impl ByondApi {
6671
}
6772
}
6873

74+
impl Deref for ByondApi {
75+
type Target = byond_rawbind::ByondApi;
76+
77+
#[inline]
78+
fn deref(&self) -> &Self::Target {
79+
&self.internal
80+
}
81+
}
82+
6983
// Stabilized types
7084
pub use byond_rawbind::s1c;
7185
pub use byond_rawbind::s1cMAX;
@@ -87,255 +101,3 @@ pub use byond_rawbind::ByondValueData;
87101
pub use byond_rawbind::ByondValueType;
88102
pub use byond_rawbind::CByondValue;
89103
pub use byond_rawbind::CByondXYZ;
90-
91-
// Stabilized functions
92-
impl ByondApi {
93-
pub unsafe fn Byond_LastError(&self) -> *const c_char {
94-
self.internal.Byond_LastError()
95-
}
96-
pub unsafe fn Byond_GetVersion(&self, version: *mut u4c, build: *mut u4c) {
97-
self.internal.Byond_GetVersion(version, build)
98-
}
99-
pub unsafe fn Byond_GetDMBVersion(&self) -> u4c {
100-
self.internal.Byond_GetDMBVersion()
101-
}
102-
pub unsafe fn ByondValue_Clear(&self, v: *mut CByondValue) {
103-
self.internal.ByondValue_Clear(v)
104-
}
105-
pub unsafe fn ByondValue_Type(&self, v: *const CByondValue) -> ByondValueType {
106-
self.internal.ByondValue_Type(v)
107-
}
108-
pub unsafe fn ByondValue_IsNull(&self, v: *const CByondValue) -> bool {
109-
self.internal.ByondValue_IsNull(v)
110-
}
111-
pub unsafe fn ByondValue_IsNum(&self, v: *const CByondValue) -> bool {
112-
self.internal.ByondValue_IsNum(v)
113-
}
114-
pub unsafe fn ByondValue_IsStr(&self, v: *const CByondValue) -> bool {
115-
self.internal.ByondValue_IsStr(v)
116-
}
117-
pub unsafe fn ByondValue_IsList(&self, v: *const CByondValue) -> bool {
118-
self.internal.ByondValue_IsList(v)
119-
}
120-
pub unsafe fn ByondValue_IsTrue(&self, v: *const CByondValue) -> bool {
121-
self.internal.ByondValue_IsTrue(v)
122-
}
123-
pub unsafe fn ByondValue_GetNum(&self, v: *const CByondValue) -> f32 {
124-
self.internal.ByondValue_GetNum(v)
125-
}
126-
pub unsafe fn ByondValue_GetRef(&self, v: *const CByondValue) -> u4c {
127-
self.internal.ByondValue_GetRef(v)
128-
}
129-
pub unsafe fn ByondValue_SetNum(&self, v: *mut CByondValue, f: f32) {
130-
self.internal.ByondValue_SetNum(v, f)
131-
}
132-
pub unsafe fn ByondValue_SetStr(&self, v: *mut CByondValue, str_: *const c_char) {
133-
self.internal.ByondValue_SetStr(v, str_)
134-
}
135-
pub unsafe fn ByondValue_SetRef(&self, v: *mut CByondValue, type_: ByondValueType, ref_: u4c) {
136-
self.internal.ByondValue_SetRef(v, type_, ref_)
137-
}
138-
pub unsafe fn ByondValue_Equals(&self, a: *const CByondValue, b: *const CByondValue) -> bool {
139-
self.internal.ByondValue_Equals(a, b)
140-
}
141-
pub unsafe fn Byond_ThreadSync(
142-
&self,
143-
callback: ByondCallback,
144-
data: *mut std::ffi::c_void,
145-
block: bool,
146-
) -> CByondValue {
147-
self.internal.Byond_ThreadSync(callback, data, block)
148-
}
149-
pub unsafe fn Byond_GetStrId(&self, str_: *const c_char) -> u4c {
150-
self.internal.Byond_GetStrId(str_)
151-
}
152-
pub unsafe fn Byond_AddGetStrId(&self, str_: *const c_char) -> u4c {
153-
self.internal.Byond_AddGetStrId(str_)
154-
}
155-
pub unsafe fn Byond_ReadVar(
156-
&self,
157-
loc: *const CByondValue,
158-
varname: *const c_char,
159-
result: *mut CByondValue,
160-
) -> bool {
161-
self.internal.Byond_ReadVar(loc, varname, result)
162-
}
163-
pub unsafe fn Byond_ReadVarByStrId(
164-
&self,
165-
loc: *const CByondValue,
166-
varname: u4c,
167-
result: *mut CByondValue,
168-
) -> bool {
169-
self.internal.Byond_ReadVarByStrId(loc, varname, result)
170-
}
171-
pub unsafe fn Byond_WriteVar(
172-
&self,
173-
loc: *const CByondValue,
174-
varname: *const c_char,
175-
val: *const CByondValue,
176-
) -> bool {
177-
self.internal.Byond_WriteVar(loc, varname, val)
178-
}
179-
pub unsafe fn Byond_WriteVarByStrId(
180-
&self,
181-
loc: *const CByondValue,
182-
varname: u4c,
183-
val: *const CByondValue,
184-
) -> bool {
185-
self.internal.Byond_WriteVarByStrId(loc, varname, val)
186-
}
187-
pub unsafe fn Byond_CreateList(&self, result: *mut CByondValue) -> bool {
188-
self.internal.Byond_CreateList(result)
189-
}
190-
pub unsafe fn Byond_ReadList(
191-
&self,
192-
loc: *const CByondValue,
193-
list: *mut CByondValue,
194-
len: *mut u4c,
195-
) -> bool {
196-
self.internal.Byond_ReadList(loc, list, len)
197-
}
198-
pub unsafe fn Byond_WriteList(
199-
&self,
200-
loc: *const CByondValue,
201-
list: *const CByondValue,
202-
len: u4c,
203-
) -> bool {
204-
self.internal.Byond_WriteList(loc, list, len)
205-
}
206-
pub unsafe fn Byond_ReadListIndex(
207-
&self,
208-
loc: *const CByondValue,
209-
idx: *const CByondValue,
210-
result: *mut CByondValue,
211-
) -> bool {
212-
self.internal.Byond_ReadListIndex(loc, idx, result)
213-
}
214-
pub unsafe fn Byond_WriteListIndex(
215-
&self,
216-
loc: *const CByondValue,
217-
idx: *const CByondValue,
218-
val: *const CByondValue,
219-
) -> bool {
220-
self.internal.Byond_WriteListIndex(loc, idx, val)
221-
}
222-
pub unsafe fn Byond_ReadPointer(
223-
&self,
224-
ptr: *const CByondValue,
225-
result: *mut CByondValue,
226-
) -> bool {
227-
self.internal.Byond_ReadPointer(ptr, result)
228-
}
229-
pub unsafe fn Byond_WritePointer(
230-
&self,
231-
ptr: *const CByondValue,
232-
val: *const CByondValue,
233-
) -> bool {
234-
self.internal.Byond_WritePointer(ptr, val)
235-
}
236-
pub unsafe fn Byond_CallProc(
237-
&self,
238-
src: *const CByondValue,
239-
name: *const c_char,
240-
arg: *const CByondValue,
241-
arg_count: u4c,
242-
result: *mut CByondValue,
243-
) -> bool {
244-
self.internal
245-
.Byond_CallProc(src, name, arg, arg_count, result)
246-
}
247-
pub unsafe fn Byond_CallProcByStrId(
248-
&self,
249-
src: *const CByondValue,
250-
name: u4c,
251-
arg: *const CByondValue,
252-
arg_count: u4c,
253-
result: *mut CByondValue,
254-
) -> bool {
255-
self.internal
256-
.Byond_CallProcByStrId(src, name, arg, arg_count, result)
257-
}
258-
pub unsafe fn Byond_CallGlobalProc(
259-
&self,
260-
name: *const c_char,
261-
arg: *const CByondValue,
262-
arg_count: u4c,
263-
result: *mut CByondValue,
264-
) -> bool {
265-
self.internal
266-
.Byond_CallGlobalProc(name, arg, arg_count, result)
267-
}
268-
pub unsafe fn Byond_CallGlobalProcByStrId(
269-
&self,
270-
name: u4c,
271-
arg: *const CByondValue,
272-
arg_count: u4c,
273-
result: *mut CByondValue,
274-
) -> bool {
275-
self.internal
276-
.Byond_CallGlobalProcByStrId(name, arg, arg_count, result)
277-
}
278-
pub unsafe fn Byond_ToString(
279-
&self,
280-
src: *const CByondValue,
281-
result: *mut c_char,
282-
len: *mut u4c,
283-
) -> bool {
284-
self.internal.Byond_ToString(src, result, len)
285-
}
286-
pub unsafe fn Byond_Block(
287-
&self,
288-
corner1: *const CByondXYZ,
289-
corner2: *const CByondXYZ,
290-
result: *mut CByondValue,
291-
len: *mut u4c,
292-
) -> bool {
293-
self.internal.Byond_Block(corner1, corner2, result, len)
294-
}
295-
pub unsafe fn Byond_Length(&self, src: *const CByondValue, result: *mut CByondValue) -> bool {
296-
self.internal.Byond_Length(src, result)
297-
}
298-
pub unsafe fn Byond_LocateIn(
299-
&self,
300-
type_: *const CByondValue,
301-
list: *const CByondValue,
302-
result: *mut CByondValue,
303-
) -> bool {
304-
self.internal.Byond_LocateIn(type_, list, result)
305-
}
306-
pub unsafe fn Byond_LocateXYZ(&self, xyz: *const CByondXYZ, result: *mut CByondValue) -> bool {
307-
self.internal.Byond_LocateXYZ(xyz, result)
308-
}
309-
pub unsafe fn Byond_New(
310-
&self,
311-
type_: *const CByondValue,
312-
argA: *const CByondValue,
313-
argS: u4c,
314-
result: *mut CByondValue,
315-
) -> bool {
316-
self.internal.Byond_New(type_, argA, argS, result)
317-
}
318-
pub unsafe fn Byond_NewArglist(
319-
&self,
320-
type_: *const CByondValue,
321-
arglist: *const CByondValue,
322-
result: *mut CByondValue,
323-
) -> bool {
324-
self.internal.Byond_NewArglist(type_, arglist, result)
325-
}
326-
pub unsafe fn Byond_Refcount(&self, type_: *const CByondValue, result: *mut u4c) -> bool {
327-
self.internal.Byond_Refcount(type_, result)
328-
}
329-
pub unsafe fn Byond_XYZ(&self, src: *const CByondValue, xyz: *mut CByondXYZ) -> bool {
330-
self.internal.Byond_XYZ(src, xyz)
331-
}
332-
pub unsafe fn ByondValue_IncRef(&self, src: *const CByondValue) {
333-
self.internal.ByondValue_IncRef(src)
334-
}
335-
pub unsafe fn ByondValue_DecRef(&self, src: *const CByondValue) {
336-
self.internal.ByondValue_DecRef(src)
337-
}
338-
pub unsafe fn Byond_TestRef(&self, src: *mut CByondValue) -> bool {
339-
self.internal.Byond_TestRef(src)
340-
}
341-
}

0 commit comments

Comments
 (0)