|
1 | | -use rglua::interface::{get_from_interface, get_interface_handle, EngineClient}; |
| 1 | +use rglua::interface::{EngineClient}; |
2 | 2 | use rglua::prelude::*; |
3 | 3 |
|
4 | | -use anyhow::bail; |
5 | | - |
6 | | -fn get_iface() -> anyhow::Result<&'static EngineClient> { |
7 | | - let handle = unsafe { get_interface_handle("engine.dll")? }; |
8 | | - let iface = get_from_interface("VEngineClient015", handle)? as *mut EngineClient; |
9 | | - match unsafe { iface.as_ref() } { |
10 | | - Some(iface) => Ok(iface), |
11 | | - None => bail!("Failed to get interface"), |
12 | | - } |
| 4 | +fn get_iface() -> Option<&'static EngineClient> { |
| 5 | + let iface: *mut EngineClient = iface!("engine", "VEngineClient015")?; |
| 6 | + unsafe { iface.as_ref() } |
13 | 7 | } |
14 | 8 |
|
15 | 9 | #[lua_function] |
16 | 10 | fn concmd(l: LuaState) -> i32 { |
17 | | - match get_iface() { |
18 | | - Ok(iface) => { |
19 | | - iface.ExecuteClientCmd( luaL_checklstring(l, 1, 0) ); |
20 | | - } |
21 | | - Err(e) => printgm!(l, "{}", e) |
| 11 | + if let Some(iface) = get_iface() { |
| 12 | + iface.ExecuteClientCmd( luaL_checklstring(l, 1, 0) ); |
22 | 13 | } |
23 | 14 | 0 |
24 | 15 | } |
25 | 16 |
|
26 | 17 | #[lua_function] |
27 | 18 | fn get_resolution(l: LuaState) -> i32 { |
28 | | - match get_iface() { |
29 | | - Ok(iface) => unsafe { |
30 | | - let (w, h): (*mut _, *mut _) = (&mut 0, &mut 0); |
31 | | - iface.GetScreenSize(w, h); |
| 19 | + if let Some(iface) = get_iface() { |
| 20 | + let (w, h): (*mut _, *mut _) = (&mut 0, &mut 0); |
| 21 | + iface.GetScreenSize(w, h); |
| 22 | + unsafe { |
32 | 23 | lua_pushinteger(l, *w as isize); |
33 | 24 | lua_pushinteger(l, *h as isize); |
34 | | - return 2; |
35 | | - }, |
36 | | - Err(e) => { |
37 | | - printgm!(l, "Failed to get interface: {}", e); |
38 | 25 | } |
| 26 | + return 2; |
39 | 27 | } |
40 | 28 | 0 |
41 | 29 | } |
42 | 30 |
|
43 | 31 | #[lua_function] |
44 | 32 | fn get_directory(l: LuaState) -> i32 { |
45 | | - match get_iface() { |
46 | | - Ok(iface) => { |
47 | | - let dir = iface.GetGameDirectory(); |
48 | | - lua_pushstring(l, dir); |
49 | | - return 1; |
50 | | - }, |
51 | | - Err(e) => { |
52 | | - printgm!(l, "Failed to get interface: {}", e); |
53 | | - } |
| 33 | + if let Some(iface) = get_iface() { |
| 34 | + let dir = iface.GetGameDirectory(); |
| 35 | + lua_pushstring(l, dir); |
| 36 | + return 1; |
54 | 37 | } |
55 | 38 | 0 |
56 | 39 | } |
57 | 40 |
|
58 | 41 | #[lua_function] |
59 | 42 | fn get_level(l: LuaState) -> i32 { |
60 | | - match get_iface() { |
61 | | - Ok(iface) => { |
62 | | - let level = iface.GetLevelName(); |
63 | | - lua_pushstring(l, level); |
64 | | - return 1; |
65 | | - }, |
66 | | - Err(e) => { |
67 | | - printgm!(l, "Failed to get interface: {}", e); |
68 | | - } |
| 43 | + if let Some(iface) = get_iface() { |
| 44 | + let level = iface.GetLevelName(); |
| 45 | + lua_pushstring(l, level); |
| 46 | + return 1; |
69 | 47 | } |
70 | 48 | 0 |
71 | 49 | } |
72 | 50 |
|
73 | 51 | #[lua_function] |
74 | 52 | fn is_recording(l: LuaState) -> i32 { |
75 | | - match get_iface() { |
76 | | - Ok(iface) => { |
77 | | - let demo = iface.IsRecordingDemo(); |
78 | | - lua_pushboolean(l, demo as i32); |
79 | | - return 1; |
80 | | - }, |
81 | | - Err(e) => { |
82 | | - printgm!(l, "Failed to get interface: {}", e); |
83 | | - } |
| 53 | + if let Some(iface) = get_iface() { |
| 54 | + let demo = iface.IsRecordingDemo(); |
| 55 | + lua_pushboolean(l, demo as i32); |
| 56 | + return 1; |
84 | 57 | } |
85 | 58 | 0 |
86 | 59 | } |
87 | 60 |
|
88 | 61 | #[lua_function] |
89 | 62 | fn is_paused(l: LuaState) -> i32 { |
90 | | - match get_iface() { |
91 | | - Ok(iface) => { |
92 | | - let paused = iface.IsPaused(); |
93 | | - lua_pushboolean(l, paused as i32); |
94 | | - return 1; |
95 | | - }, |
96 | | - Err(e) => { |
97 | | - printgm!(l, "Failed to get interface: {}", e); |
98 | | - } |
| 63 | + if let Some(iface) = get_iface() { |
| 64 | + let paused = iface.IsPaused(); |
| 65 | + lua_pushboolean(l, paused as i32); |
| 66 | + return 1; |
99 | 67 | } |
100 | 68 | 0 |
101 | 69 | } |
|
0 commit comments