Add some networking helpers

This commit is contained in:
William Venner 2021-10-06 18:23:09 +01:00
parent 2b94d70a38
commit 6ebb763f14
2 changed files with 38 additions and 0 deletions

View File

@ -26,6 +26,9 @@ pub mod hax;
/// Userdata types /// Userdata types
pub mod userdata; pub mod userdata;
/// Net library helpers
pub mod net;
/// Returns whether this client is running the x86-64 branch /// 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 /// Current implementation checks the contents of the bin/ directory, so this is a blocking operation and requires syscalls, use sparingly

35
gmod/src/net.rs Normal file
View File

@ -0,0 +1,35 @@
use crate::{lua::{self, LuaFunction}, lua_string};
#[inline(always)]
pub unsafe fn add_network_strings<S: AsRef<str>>(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<S: AsRef<str>>(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();
}