1#![allow(unused)]
2
3#[macro_export]
19macro_rules! cstr {
20 ($rstring:literal) => {
21 concat!($rstring, "\0").as_ptr() as *const i8
22 };
23 ($rstring:expr) => {
24 std::ffi::CString::new($rstring).expect("Couldn't make CString from rust string")
25 };
26}
27
28#[macro_export]
42macro_rules! try_cstr {
43 ($rstring:literal) => {
44 concat!($rstring, "\0").as_ptr() as *const i8
45 };
46 ($rstring:expr) => {{
47 std::ffi::CString::new($rstring)
48 }};
49}
50
51#[macro_export]
61macro_rules! rstr {
62 ($cstring:expr) => {{
63 #[allow(unused_unsafe)]
64 let cstr = unsafe { std::ffi::CStr::from_ptr($cstring) };
65 cstr.to_str().expect("Couldn't unwrap CString")
66 }};
67}
68
69#[macro_export]
70macro_rules! try_rstr {
79 ($cstring:expr) => {{
80 #[allow(unused_unsafe)]
81 let cstr = unsafe { std::ffi::CStr::from_ptr($cstring) };
82 cstr.to_str()
83 }};
84}
85
86#[allow(unused_macros)]
87#[macro_export]
88macro_rules! printgm {
101 ($state:expr, $($x:expr),*) => {
102 {
103 let printargs = format!( $($x,)* );
104 if let Ok(fmt) = std::ffi::CString::new(printargs) {
105 $crate::lua::lua_getglobal( $state, $crate::cstr!("print") );
106 $crate::lua::lua_pushstring( $state, fmt.as_ptr() );
107 $crate::lua::lua_call( $state, 1, 0 );
108 }
109 }
110 };
111}
112
113#[macro_export]
129macro_rules! reg {
130 ( $( $name:expr => $func:expr ),* ) => {
131 &[ $( $crate::types::LuaReg { name: $crate::cstr!($name), func: Some($func) } ),*, $crate::types::LuaReg { name: std::ptr::null(), func: None } ]
132 };
133}
134
135#[cfg(feature = "interfaces")]
137fn get_from_interface(
138 iface: &str,
139 factory: crate::interface::CreateInterfaceFn
140) -> Result<*mut (), crate::interface::Error> {
141 let mut status = 0;
142
143 let iface = try_cstr!(iface)?;
144 let result = factory(iface.as_ptr(), &mut status);
145
146 if status == 0 && !result.is_null() {
147 Ok(result as *mut ())
148 } else {
149 Err(crate::interface::Error::FactoryNotFound(
150 iface.to_string_lossy().to_string()
151 ))
152 }
153}
154
155#[macro_export]
179macro_rules! iface {
180 ( LuaShared ) => {
181 iface!("lua_shared", "LUASHARED003", $crate::interface::LuaShared)
182 };
183 ( EngineClient ) => {
184 iface!(
185 "engine",
186 "VEngineClient015",
187 $crate::interface::EngineClient
188 )
189 };
190 ( EngineServer ) => {
191 iface!(
192 "engine",
193 "VEngineServer021",
194 $crate::interface::EngineServer
195 )
196 };
197 ( MdlCache ) => {
198 iface!("datacache", "MDLCache004", $crate::interface::MdlCache)
199 };
200 ( MaterialSystem ) => {
201 iface!(
202 "materialsystem",
203 "VMaterialSystem080",
204 $crate::interface::MaterialSystem
205 )
206 };
207 ( Panel ) => {
208 iface!("vgui2", "VGUI_Panel009", $crate::interface::Panel)
209 };
210 ( ConVar ) => {
211 iface!("vstdlib", "VEngineCvar007", $crate::interface::ConVar)
212 };
213
214 ( $name:literal, $iface:literal, $ty:ty ) => {{
215 match unsafe { $crate::interface::get_interface_handle($name) } {
217 Ok(handle) => {
218 let mut status = 0;
219
220 let result = handle(cstr!($iface), &mut status);
222
223 if status == 0 && !result.is_null() {
224 let ptr = result as *mut $ty;
225 unsafe { ptr.as_mut() }
226 .ok_or($crate::interface::Error::IFaceMut(String::from($iface)))
227 } else {
228 Err($crate::interface::Error::CreateInterface(
229 status,
230 String::from($iface),
231 ))
232 }
233 }
234 Err(why) => Err($crate::interface::Error::Libloading(why)),
235 }
236 }};
237}
238
239use crate::types::LuaState;
240pub fn dump_stack(l: LuaState) -> Result<String, std::fmt::Error> {
250 use std::fmt::Write;
251
252 use crate::lua::*;
253
254 let mut buf = String::new();
255
256 let top = lua_gettop(l);
257 for i in 1..=top {
258 write!(&mut buf, "[{}] '{}' = ", i, rstr!(luaL_typename(l, i)));
259 match lua_type(l, i) {
260 TNUMBER => write!(&mut buf, "{}", lua_tonumber(l, i)),
261 TSTRING => write!(&mut buf, "{}", rstr!(lua_tostring(l, i))),
262 TBOOLEAN => write!(
263 &mut buf,
264 "{}",
265 if lua_toboolean(l, i) == 1 {
266 "true"
267 } else {
268 "false"
269 }
270 ),
271 TNIL => write!(&mut buf, "nil"),
272 TNONE => write!(&mut buf, "none"),
273 TUSERDATA | TLIGHTUSERDATA => write!(&mut buf, "{:p}", lua_touserdata(l, i)),
274 TTHREAD => write!(&mut buf, "{:p}", lua_tothread(l, i)),
275 _ => write!(&mut buf, "Unknown type")
276 }?
277 }
278
279 Ok(buf)
280}