1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#![warn(missing_docs, nonstandard_style)]
use std::sync::Arc;
pub use arma_rs_proc::arma;
use crossbeam_queue::SegQueue;
pub use libc;
#[macro_use]
extern crate log;
mod arma;
pub use arma::{FromArma, IntoArma, Value};
mod command;
mod context;
mod group;
mod testing;
pub use command::*;
pub use context::Context;
pub use group::Group;
pub use testing::Result;
#[cfg(windows)]
pub type Callback = extern "stdcall" fn(
*const libc::c_char,
*const libc::c_char,
*const libc::c_char,
) -> libc::c_int;
#[cfg(not(windows))]
pub type Callback =
extern "C" fn(*const libc::c_char, *const libc::c_char, *const libc::c_char) -> libc::c_int;
pub struct Extension {
version: String,
group: Group,
allow_no_args: bool,
callback: Option<Callback>,
callback_queue: Arc<SegQueue<(String, String, Option<Value>)>>,
}
impl Extension {
#[must_use]
pub fn build() -> ExtensionBuilder {
ExtensionBuilder {
version: env!("CARGO_PKG_VERSION").to_string(),
group: Group::new(),
allow_no_args: false,
}
}
#[must_use]
pub fn version(&self) -> &str {
&self.version
}
#[must_use]
pub const fn allow_no_args(&self) -> bool {
self.allow_no_args
}
pub fn register_callback(&mut self, callback: Callback) {
self.callback = Some(callback);
}
#[must_use]
pub fn context(&self) -> Context {
Context::new(self.callback_queue.clone())
}
pub unsafe fn handle(
&self,
function: *mut libc::c_char,
output: *mut libc::c_char,
size: libc::size_t,
args: Option<*mut *mut i8>,
count: Option<libc::c_int>,
) -> libc::c_int {
let function = if let Ok(cstring) = std::ffi::CStr::from_ptr(function).to_str() {
cstring.to_string()
} else {
return 1;
};
self.group.handle(
self.context().with_buffer_size(size - 8),
&function,
output,
size,
args,
count,
)
}
#[must_use]
pub fn testing(self) -> testing::Extension {
testing::Extension::new(self)
}
pub fn run_callbacks(&self) {
let queue = self.callback_queue.clone();
let callback = self.callback;
std::thread::spawn(move || loop {
if let Some((name, func, data)) = queue.pop() {
if let Some(c) = callback {
let name = if let Ok(cstring) = std::ffi::CString::new(name) {
cstring.into_raw()
} else {
error!("callback name was not valid");
continue;
};
let func = if let Ok(cstring) = std::ffi::CString::new(func) {
cstring.into_raw()
} else {
error!("callback func was not valid");
continue;
};
let data = if let Ok(cstring) = std::ffi::CString::new(match data {
Some(value) => match value {
Value::String(s) => s,
v => v.to_string(),
},
None => String::new(),
}) {
cstring.into_raw()
} else {
error!("callback data was not valid");
continue;
};
loop {
if c(name, func, data) >= 0 {
break;
}
std::thread::sleep(std::time::Duration::from_millis(1));
}
}
}
});
}
}
pub struct ExtensionBuilder {
version: String,
group: Group,
allow_no_args: bool,
}
impl ExtensionBuilder {
#[inline]
#[must_use]
pub fn version(mut self, version: String) -> Self {
self.version = version;
self
}
#[inline]
pub fn group<S>(mut self, name: S, group: Group) -> Self
where
S: Into<String>,
{
self.group = self.group.group(name.into(), group);
self
}
#[inline]
#[must_use]
pub const fn allow_no_args(mut self) -> Self {
self.allow_no_args = true;
self
}
#[inline]
pub fn command<S, F, I, R>(mut self, name: S, handler: F) -> Self
where
S: Into<String>,
F: Factory<I, R> + 'static,
{
self.group = self.group.command(name, handler);
self
}
#[inline]
#[must_use]
pub fn finish(self) -> Extension {
Extension {
version: self.version,
group: self.group,
allow_no_args: self.allow_no_args,
callback: None,
callback_queue: Arc::new(SegQueue::new()),
}
}
}
pub unsafe fn write_cstr(
string: String,
ptr: *mut libc::c_char,
buf_size: libc::size_t,
) -> Option<libc::size_t> {
let cstr = std::ffi::CString::new(string).ok()?;
let cstr_bytes = cstr.as_bytes();
let len_to_copy = cstr_bytes.len();
if len_to_copy * 8 >= buf_size {
return None;
}
ptr.copy_from(cstr.as_ptr(), len_to_copy);
ptr.add(len_to_copy).write(0x00);
Some(len_to_copy)
}