From f5a707eea4c98a6546fd385b35bd51b6d9c84bf4 Mon Sep 17 00:00:00 2001 From: Linventif Date: Mon, 25 Sep 2023 11:11:51 +0200 Subject: [PATCH] remake networking --- lua/gmod_integration/client/cl_net.lua | 32 ++++++++++++++++++++++++++ lua/gmod_integration/server/sv_net.lua | 23 ++++++++++++++---- 2 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 lua/gmod_integration/client/cl_net.lua diff --git a/lua/gmod_integration/client/cl_net.lua b/lua/gmod_integration/client/cl_net.lua new file mode 100644 index 0000000..0d65056 --- /dev/null +++ b/lua/gmod_integration/client/cl_net.lua @@ -0,0 +1,32 @@ +// +// Network +// + +/* +Upload + 0 - Say I'm ready +Receive + 1 - Sync Chat +*/ + +// Send +function gmInte.SendNet(id, args, func) + net.Start("gmIntegration") + net.WriteUInt(id, 8) + net.WriteString(util.TableToJSON(args || {})) + func && func() + net.SendToServer() +end + +// Receive +local netFunc = { + [1] = function(args) + chat.AddText(unpack(args)) + end +} + +net.Receive("gmIntegration", function() + local id = net.ReadUInt(8) + local args = util.JSONToTable(net.ReadString()) + netFunc[id] && netFunc[id](args) +end) \ No newline at end of file diff --git a/lua/gmod_integration/server/sv_net.lua b/lua/gmod_integration/server/sv_net.lua index 9e469f0..3342ef9 100644 --- a/lua/gmod_integration/server/sv_net.lua +++ b/lua/gmod_integration/server/sv_net.lua @@ -2,8 +2,25 @@ // Network // +/* +Upload + 1 - Add Chat Message +Receive + 0 - Player is Ready +*/ + util.AddNetworkString("gmIntegration") +// Send +function gmInte.SendNet(id, data, ply, func) + net.Start("gmIntegration") + net.WriteUInt(id, 8) + net.WriteString(util.TableToJSON(data)) + func && func() + net.Send(ply) +end + +// Receive local netFuncs = { [0] = function(ply) gmInte.userFinishConnect(ply) @@ -14,9 +31,5 @@ net.Receive("gmIntegration", function(len, ply) if !ply:IsPlayer() then return end local id = net.ReadUInt(8) local data = util.JSONToTable(net.ReadString() || "{}") - - // check if argument is valid - if netFuncs[id] then - netFuncs[id](ply, data) - end + netFuncs[id] && netFuncs[id](ply, data) end) \ No newline at end of file