diff --git a/gmod/src/lib.rs b/gmod/src/lib.rs index e4cbf6a..3f5e347 100644 --- a/gmod/src/lib.rs +++ b/gmod/src/lib.rs @@ -26,6 +26,9 @@ pub mod hax; /// Userdata types pub mod userdata; +/// Net library helpers +pub mod net; + /// Returns whether this client is running the x86-64 branch /// /// Current implementation checks the contents of the bin/ directory, so this is a blocking operation and requires syscalls, use sparingly diff --git a/gmod/src/net.rs b/gmod/src/net.rs new file mode 100644 index 0000000..265c5e6 --- /dev/null +++ b/gmod/src/net.rs @@ -0,0 +1,35 @@ +use crate::{lua::{self, LuaFunction}, lua_string}; + +#[inline(always)] +pub unsafe fn add_network_strings>(lua: lua::State, network_strings: &[S]) { + match network_strings.len() { + 0 => {}, + 1 => { + lua.get_global(lua_string!("util")); + lua.get_field(-1, lua_string!("AddNetworkString")); + lua.push_string(network_strings[0].as_ref()); + lua.call(1, 0); + lua.pop(); + }, + _ => { + lua.get_global(lua_string!("util")); + lua.get_field(-1, lua_string!("AddNetworkString")); + for network_string in network_strings { + lua.push_value(-1); + lua.push_string(network_string.as_ref()); + lua.call(1, 0); + } + lua.pop_n(2); + } + } +} + +#[inline(always)] +pub unsafe fn receive>(lua: lua::State, network_string: S, func: LuaFunction) { + lua.get_global(lua_string!("net")); + lua.get_field(-1, lua_string!("Receive")); + lua.push_string(network_string.as_ref()); + lua.push_function(func); + lua.call(2, 0); + lua.pop(); +} \ No newline at end of file