|
1 | | -use rglua::interface::{EngineClient}; |
| 1 | +use rglua::interface; |
2 | 2 | use rglua::prelude::*; |
3 | 3 |
|
4 | | -fn get_iface() -> Option<&'static EngineClient> { |
5 | | - let iface: *mut EngineClient = iface!("engine", "VEngineClient015")?; |
6 | | - unsafe { iface.as_ref() } |
| 4 | +#[lua_function] |
| 5 | +fn concmd(l: LuaState) -> Result<i32, interface::Error> { |
| 6 | + let engine = iface!(EngineClient)?; |
| 7 | + engine.ExecuteClientCmd( luaL_checkstring(l, 1) ); |
| 8 | + Ok(0) |
7 | 9 | } |
8 | 10 |
|
9 | 11 | #[lua_function] |
10 | | -fn concmd(l: LuaState) -> i32 { |
11 | | - if let Some(iface) = get_iface() { |
12 | | - iface.ExecuteClientCmd( luaL_checkstring(l, 1) ); |
13 | | - } |
14 | | - 0 |
| 12 | +fn get_resolution(l: LuaState) -> Result<i32, interface::Error> { |
| 13 | + let engine = iface!(EngineClient)?; |
| 14 | + |
| 15 | + let (w, h) = (&mut 0, &mut 0); |
| 16 | + engine.GetScreenSize(w, h); |
| 17 | + |
| 18 | + lua_pushinteger(l, *w as isize); |
| 19 | + lua_pushinteger(l, *h as isize); |
| 20 | + |
| 21 | + Ok(2) |
15 | 22 | } |
16 | 23 |
|
17 | 24 | #[lua_function] |
18 | | -fn get_resolution(l: LuaState) -> i32 { |
19 | | - if let Some(iface) = get_iface() { |
20 | | - let (w, h): (*mut _, *mut _) = (&mut 0, &mut 0); |
21 | | - iface.GetScreenSize(w, h); |
22 | | - unsafe { |
23 | | - lua_pushinteger(l, *w as isize); |
24 | | - lua_pushinteger(l, *h as isize); |
25 | | - } |
26 | | - return 2; |
27 | | - } |
28 | | - 0 |
| 25 | +fn get_directory(l: LuaState) -> Result<i32, interface::Error> { |
| 26 | + let engine = iface!(EngineClient)?; |
| 27 | + |
| 28 | + let dir = engine.GetGameDirectory(); |
| 29 | + lua_pushstring(l, dir); |
| 30 | + |
| 31 | + Ok(1) |
29 | 32 | } |
30 | 33 |
|
31 | 34 | #[lua_function] |
32 | | -fn get_directory(l: LuaState) -> i32 { |
33 | | - if let Some(iface) = get_iface() { |
34 | | - let dir = iface.GetGameDirectory(); |
35 | | - lua_pushstring(l, dir); |
36 | | - return 1; |
37 | | - } |
38 | | - 0 |
| 35 | +fn get_level(l: LuaState) -> Result<i32, interface::Error> { |
| 36 | + let engine = iface!(EngineClient)?; |
| 37 | + |
| 38 | + let level = engine.GetLevelName(); |
| 39 | + lua_pushstring(l, level); |
| 40 | + |
| 41 | + Ok(1) |
39 | 42 | } |
40 | 43 |
|
41 | 44 | #[lua_function] |
42 | | -fn get_level(l: LuaState) -> i32 { |
43 | | - if let Some(iface) = get_iface() { |
44 | | - let level = iface.GetLevelName(); |
45 | | - lua_pushstring(l, level); |
46 | | - return 1; |
47 | | - } |
48 | | - 0 |
| 45 | +fn is_recording(l: LuaState) -> Result<i32, interface::Error> { |
| 46 | + let engine = iface!(EngineClient)?; |
| 47 | + |
| 48 | + let demo = engine.IsRecordingDemo(); |
| 49 | + lua_pushboolean(l, demo as i32); |
| 50 | + |
| 51 | + Ok(1) |
49 | 52 | } |
50 | 53 |
|
51 | 54 | #[lua_function] |
52 | | -fn is_recording(l: LuaState) -> i32 { |
53 | | - if let Some(iface) = get_iface() { |
54 | | - let demo = iface.IsRecordingDemo(); |
55 | | - lua_pushboolean(l, demo as i32); |
56 | | - return 1; |
57 | | - } |
58 | | - 0 |
| 55 | +fn is_paused(l: LuaState) -> Result<i32, interface::Error> { |
| 56 | + let engine = iface!(EngineClient)?; |
| 57 | + |
| 58 | + let paused = engine.IsPaused(); |
| 59 | + lua_pushboolean(l, paused as i32); |
| 60 | + |
| 61 | + Ok(1) |
59 | 62 | } |
60 | 63 |
|
61 | 64 | #[lua_function] |
62 | | -fn is_paused(l: LuaState) -> i32 { |
63 | | - if let Some(iface) = get_iface() { |
64 | | - let paused = iface.IsPaused(); |
65 | | - lua_pushboolean(l, paused as i32); |
66 | | - return 1; |
67 | | - } |
68 | | - 0 |
| 65 | +fn is_console_visible(l: LuaState) -> Result<i32, interface::Error> { |
| 66 | + let engine = iface!(EngineClient)?; |
| 67 | + |
| 68 | + lua_pushboolean(l, engine.IsConsoleVisible() as i32); |
| 69 | + |
| 70 | + Ok(1) |
| 71 | +} |
| 72 | + |
| 73 | +#[lua_function] |
| 74 | +fn net_infos(l: LuaState) -> Result<i32, interface::Error> { |
| 75 | + let flow = luaL_checkinteger(l, 1) as i32; |
| 76 | + |
| 77 | + let engine = iface!(EngineClient)?; |
| 78 | + |
| 79 | + let infos = unsafe { engine.GetNetChannelInfo().as_mut() } |
| 80 | + .ok_or(interface::Error::AsMut)?; |
| 81 | + |
| 82 | + lua_newtable(l); |
| 83 | + |
| 84 | + lua_pushstring(l, infos.GetName()); |
| 85 | + lua_setfield(l, -2, cstr!("name")); |
| 86 | + |
| 87 | + lua_pushstring(l, infos.GetAddress()); |
| 88 | + lua_setfield(l, -2, cstr!("address")); |
| 89 | + |
| 90 | + lua_pushnumber(l, infos.GetTime() as f64); |
| 91 | + lua_setfield(l, -2, cstr!("time")); |
| 92 | + |
| 93 | + lua_pushnumber(l, infos.GetTimeConnected() as f64); |
| 94 | + lua_setfield(l, -2, cstr!("time_connected")); |
| 95 | + |
| 96 | + lua_pushnumber(l, infos.GetBufferSize() as f64); |
| 97 | + lua_setfield(l, -2, cstr!("buffer_size")); |
| 98 | + |
| 99 | + lua_pushnumber(l, infos.GetDataRate() as f64); |
| 100 | + lua_setfield(l, -2, cstr!("data_rate")); |
| 101 | + |
| 102 | + lua_pushnumber(l, infos.GetAvgLoss(flow) as f64); |
| 103 | + lua_setfield(l, -2, cstr!("avg_loss")); |
| 104 | + |
| 105 | + lua_pushnumber(l, infos.GetAvgChoke(flow) as f64); |
| 106 | + lua_setfield(l, -2, cstr!("avg_choke")); |
| 107 | + |
| 108 | + lua_pushnumber(l, infos.GetAvgData(flow) as f64); |
| 109 | + lua_setfield(l, -2, cstr!("avg_data")); |
| 110 | + |
| 111 | + lua_pushnumber(l, infos.GetAvgLatency(flow) as f64); |
| 112 | + lua_setfield(l, -2, cstr!("avg_latency")); |
| 113 | + |
| 114 | + Ok(1) |
69 | 115 | } |
70 | 116 |
|
71 | 117 | #[gmod_open] |
72 | | -fn open(l: LuaState) -> i32 { |
| 118 | +fn open(l: LuaState) -> Result<i32, interface::Error> { |
73 | 119 | printgm!(l, "Loaded engine module!"); |
74 | 120 |
|
| 121 | + let lua = iface!(LuaShared)?; |
| 122 | + |
| 123 | + let client = lua.GetLuaInterface(0); |
| 124 | + let menu = lua.GetLuaInterface(2); |
| 125 | + if client.is_null() && menu.is_null() { |
| 126 | + // TODO: Serverside support & Clientside in one module :) |
| 127 | + luaL_error(l, cstr!("This module is only compatible with the CLIENT or MENU states!")); |
| 128 | + } |
| 129 | + |
75 | 130 | let lib = reg! [ |
76 | 131 | "concmd" => concmd, |
77 | 132 | "getResolution" => get_resolution, |
78 | 133 | "getGameDirectory" => get_directory, |
79 | 134 | "getLevel" => get_level, |
80 | 135 | "isRecording" => is_recording, |
81 | | - "isPaused" => is_paused |
| 136 | + "isPaused" => is_paused, |
| 137 | + "isConsoleVisible" => is_console_visible, |
| 138 | + "getNetInfo" => net_infos |
82 | 139 | ]; |
83 | 140 |
|
84 | 141 | luaL_register(l, cstr!("iengine"), lib.as_ptr()); |
85 | | - 0 |
| 142 | + Ok(0) |
86 | 143 | } |
87 | 144 |
|
88 | 145 | #[gmod_close] |
|
0 commit comments