From 4424f3dd06dbd77e5f2ea3cfeb7096a148b7be3e Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 27 Nov 2019 18:54:40 +0000 Subject: [PATCH] WIP sourcenet Lua examples code --- examples/sn_changedisconnect.lua | 1 - examples/sn_cheatskick.lua | 9 ++++----- examples/sn_clientconnect.lua | 4 +++- examples/sn_clog.lua | 3 +-- examples/sn_entmessages.lua | 6 ++++-- examples/sn_fstream.lua | 23 +++++++++-------------- examples/sn_loadcodec.lua | 9 +++++---- examples/sn_namechange.lua | 15 +++++---------- examples/sn_setconvars.lua | 14 ++++++++------ examples/sn_slog.lua | 3 +-- examples/sn_test.lua | 4 ++-- examples/sn_test_fstream.lua | 24 ++++++++++-------------- examples/sn_umsghooks.lua | 18 +++++++++--------- examples/sn_umsgs.lua | 10 ++++++---- examples/sn_voicemimic.lua | 2 +- sourcenet/netmessages.lua | 17 ++++++++++++----- 16 files changed, 80 insertions(+), 82 deletions(-) diff --git a/examples/sn_changedisconnect.lua b/examples/sn_changedisconnect.lua index 3ec8beb..e0b29cc 100644 --- a/examples/sn_changedisconnect.lua +++ b/examples/sn_changedisconnect.lua @@ -7,7 +7,6 @@ hook.Add("SendGameEvent", "ChangeReason", function(netchan, event) if reason == "Disconnect by user." then event:SetString("reason", "Disconnected after " .. math.floor(netchan:GetTime() - netchan:GetConnectTime()) .. " seconds") - return event end end) diff --git a/examples/sn_cheatskick.lua b/examples/sn_cheatskick.lua index e1230cf..7b727f2 100644 --- a/examples/sn_cheatskick.lua +++ b/examples/sn_cheatskick.lua @@ -13,12 +13,11 @@ hook.Add("PlayerInitialSpawn", "InitialCheatsCheck", function(ply) end) hook.Add("RespondCvarValue", "InitialCheatsCheck", function(netchan, cookie, status, cvarname, cvarvalue) - if status ~= 0 then return end - if cvarname ~= "sv_cheats" then return end - if cvarvalue == GetConVarString("sv_cheats") then return end - + if status ~= 0 or cvarname ~= "sv_cheats" or cvarvalue == GetConVarString("sv_cheats") then + return + end + local ply = FindPlayerByNetChannel(netchan) - if IsValid(ply) and cookie == ply.CheatsCookie then ply:Kick("Incorrect sv_cheats value") end diff --git a/examples/sn_clientconnect.lua b/examples/sn_clientconnect.lua index dc369ce..3d1d802 100644 --- a/examples/sn_clientconnect.lua +++ b/examples/sn_clientconnect.lua @@ -1,7 +1,9 @@ include("sourcenet/gameevents.lua") hook.Add("ProcessGameEvent", "PlayerConnect", function(netchan, event) - if event:GetName() ~= "player_connect" then return end + if event:GetName() ~= "player_connect" then + return + end event:SetString("name", string.reverse(event:GetString("name"))) diff --git a/examples/sn_clog.lua b/examples/sn_clog.lua index e17147c..d95f48a 100644 --- a/examples/sn_clog.lua +++ b/examples/sn_clog.lua @@ -2,12 +2,11 @@ include("sourcenet/outgoing.lua") FilterOutgoingMessage(net_StringCmd, function(netchan, read, write) local cmd = read:ReadString() - + print(string.format("Sending command \"%s\"", cmd)) if string.Left(cmd, 6) == "status" then print("Stopped status command being sent") - return end diff --git a/examples/sn_entmessages.lua b/examples/sn_entmessages.lua index b6ad9c2..584e183 100644 --- a/examples/sn_entmessages.lua +++ b/examples/sn_entmessages.lua @@ -8,8 +8,10 @@ WEAPONSWEP_MSG_EQUIP = 3 function SendEntityMessage(netchan, entindex, classID, buffer) local messageType = buffer:ReadByte() local entity = Entity(entindex) - - if not IsValid(entity) then return end + + if not IsValid(entity) then + return + end if entity:IsWeapon() then -- There is no Entity.GetClassID function, so this is a workaround if messageType == WEAPONSWEP_MSG_HOLSTER then diff --git a/examples/sn_fstream.lua b/examples/sn_fstream.lua index 12bc53a..5a39e90 100644 --- a/examples/sn_fstream.lua +++ b/examples/sn_fstream.lua @@ -5,13 +5,11 @@ else end -- Initialization - HookNetChannel( {name = "CNetChan::ProcessPacket"} ) -- Check progress every incoming packet (Source seems to clear fragments here) - local history = {} hook.Add("PreProcessPacket", "TransferStatus", function(netchan) @@ -19,32 +17,29 @@ hook.Add("PreProcessPacket", "TransferStatus", function(netchan) for j = 0, netchan:GetOutgoingQueueSize(i) - 1 do local fragments = netchan:GetOutgoingQueueFragments(i, j) local filename = fragments:GetFileName() + if #filename ~= 0 and not table.HasValue(history, filename) and fragments:GetProgress() + fragments:GetNum() >= fragments:GetTotal() then + print("Finished " .. filename ) - if filename ~= "" and not table.HasValue(history, filename) then - if fragments:GetProgress() + fragments:GetNum() >= fragments:GetTotal() then - print("Finished " .. filename ) + umsg.Start("fstream_complete") + umsg.String(filename) + umsg.End() - umsg.Start("fstream_complete") - umsg.String(filename) - umsg.End() - - table.insert(history, filename) - end + table.insert(history, filename) end end end end) -- Tests - function QueueFile(netchan, filename) netchan:SendFile(filename, 1) end hook.Add("PlayerInitialSpawn", "BeginTransfer", function(ply) local netchan = CNetChan(ply:EntIndex()) - - if not netchan then return end + if netchan == nil then + return + end netchan:SetBackgroundMode(false) -- Removes 1 file fragment per-packet limit diff --git a/examples/sn_loadcodec.lua b/examples/sn_loadcodec.lua index e22d2f7..704715b 100644 --- a/examples/sn_loadcodec.lua +++ b/examples/sn_loadcodec.lua @@ -1,11 +1,12 @@ -require("sourcenet" ) +require("sourcenet") concommand.Add("loadcodec", function(ply, cmd, args) - if not IsValid(ply) then return end - if not args[1] then return end + if not IsValid(ply) or args[1] == nil then + return + end local buffer = CNetChan(ply:EntIndex()):GetReliableBuffer() - + buffer:WriteUInt(svc_VoiceInit, NET_MESSAGE_BITS) buffer:WriteString(args[1]) buffer:WriteByte(1) diff --git a/examples/sn_namechange.lua b/examples/sn_namechange.lua index afcb3e5..70fdda2 100644 --- a/examples/sn_namechange.lua +++ b/examples/sn_namechange.lua @@ -1,28 +1,23 @@ require("sourcenet") concommand.Add("setname", function(ply, cmd, args) - if not args[1] then + if args[1] == nil then print("Syntax: setname ") - return end - + local netchan = CNetChan() - - if not netchan then + if netchan == nil then print("setname: invalid netchan") - return end local buffer = netchan:GetReliableBuffer() - - if not buffer then + if buffer == nil then print("setname: invalid buffer") - return end - + buffer:WriteUInt(net_SetConVar, NET_MESSAGE_BITS) -- message type buffer:WriteByte(1) -- convar count buffer:WriteString("name") -- convar name diff --git a/examples/sn_setconvars.lua b/examples/sn_setconvars.lua index a9336f8..c0915f1 100644 --- a/examples/sn_setconvars.lua +++ b/examples/sn_setconvars.lua @@ -1,16 +1,18 @@ require("sourcenet") -function _R.Player:GetNetChannel() +local PLAYER = FindMetaTable("Player") +function PLAYER:GetNetChannel() return CNetChan(self:EntIndex()) end -function _R.Player:SetConVar(name, value) +function PLAYER:SetConVar(name, value) local netchan = self:GetNetChannel() - - if not netchan then return end - + if netchan == nil then + return + end + local buf = netchan:GetReliableBuffer() - + buf:WriteUInt(net_SetConVar, NET_MESSAGE_BITS) buf:WriteByte(1) buf:WriteString(name) diff --git a/examples/sn_slog.lua b/examples/sn_slog.lua index 75a02b0..51f5f12 100644 --- a/examples/sn_slog.lua +++ b/examples/sn_slog.lua @@ -2,12 +2,11 @@ include("sourcenet/incoming.lua") FilterIncomingMessage(net_StringCmd, function(netchan, read, write) local cmd = read:ReadString() - + print(string.format("Client %s ran command \"%s\"", netchan:GetAddress():ToString(), cmd)) if string.Left(cmd, 6) == "status" then print("Blocked status command") - return end diff --git a/examples/sn_test.lua b/examples/sn_test.lua index 1043166..c3e4f8a 100644 --- a/examples/sn_test.lua +++ b/examples/sn_test.lua @@ -3,7 +3,7 @@ include("sourcenet/outgoing.lua") FilterOutgoingMessage(net_StringCmd, function(netchan, read, write) local cmd = read:ReadString() - + print(string.format("Sending command \"%s\"", cmd)) write:WriteUInt(net_StringCmd, NET_MESSAGE_BITS) @@ -12,7 +12,7 @@ end) FilterIncomingMessage(net_StringCmd, function(netchan, read, write) local cmd = read:ReadString() - + print(string.format("Executing command \"%s\"", cmd)) write:WriteUInt(net_StringCmd, NET_MESSAGE_BITS) diff --git a/examples/sn_test_fstream.lua b/examples/sn_test_fstream.lua index 3168b39..f860fa1 100644 --- a/examples/sn_test_fstream.lua +++ b/examples/sn_test_fstream.lua @@ -1,13 +1,11 @@ include("sourcenet/server.lua") -- Initialization - HookNetChannel( {name = "CNetChan::ProcessPacket"} ) -- Check progress every incoming packet (Source seems to clear fragments here) - local history = {} hook.Add("PreProcessPacket", "TransferStatus", function(netchan) @@ -16,31 +14,29 @@ hook.Add("PreProcessPacket", "TransferStatus", function(netchan) local fragments = netchan:GetOutgoingQueueFragments(i, j) local filename = fragments:GetFileName() - if filename ~= "" and not table.HasValue(history, filename) then - if fragments:GetProgress() + fragments:GetNum() >= fragments:GetTotal() then - print("Finished " .. filename) + if #filename ~= 0 and not table.HasValue(history, filename) and fragments:GetProgress() + fragments:GetNum() >= fragments:GetTotal() then + print("Finished " .. filename) + + umsg.Start("fstream_complete") + umsg.String(filename) + umsg.End() - umsg.Start("fstream_complete") - umsg.String(filename) - umsg.End() - - table.insert(history, filename) - end + table.insert(history, filename) end end end end) -- Tests - function QueueFile(netchan, filename) netchan:SendFile(filename, 1) end hook.Add("PlayerInitialSpawn", "BeginTransfer", function(ply) local netchan = CNetChan(ply:EntIndex()) - - if not netchan then return end + if netchan == nil then + return + end QueueFile(netchan, "cl.db") QueueFile(netchan, "gameinfo.txt") diff --git a/examples/sn_umsghooks.lua b/examples/sn_umsghooks.lua index afc57e3..13f708e 100644 --- a/examples/sn_umsghooks.lua +++ b/examples/sn_umsghooks.lua @@ -5,15 +5,16 @@ LUASTRINGS_TABLE_NAME = "networkstring" function ReadUserMessageString(buf) if buf:ReadBit() == 1 then local container = INetworkStringTableContainer() - - if not container then return end - - local pool = container:FindTable(LUASTRINGS_TABLE_NAME) + if container == nil then + return + end - if not pool then return end + local pool = container:FindTable(LUASTRINGS_TABLE_NAME) + if pool == nil then + return + end local str = pool:GetString(buf:ReadShort()) - return str or "[STRING NOT POOLED]" else return buf:ReadString() @@ -25,10 +26,9 @@ function ProcessUserMessage(msg, data) if msg == 34 then local umsgName = ReadUserMessageString(buf) - Msg(string.format("Received Lua user message: curtime %f, name '%s', bytes %i\n", CurTime(), umsgName, buf:GetNumBytesLeft())) elseif msg == 40 then - local varUnknown = buf:ReadLong() + buf:ReadLong() local varType = buf:ReadByte() local varName = ReadUserMessageString(buf) local varValue @@ -53,7 +53,7 @@ end FilterIncomingMessage(svc_UserMessage, function(netchan, read, write) write:WriteUInt(svc_UserMessage, NET_MESSAGE_BITS) - + local msg = read:ReadByte() local bits = read:ReadUInt(11) local data = read:ReadBits(bits) diff --git a/examples/sn_umsgs.lua b/examples/sn_umsgs.lua index ecd9269..c43b203 100644 --- a/examples/sn_umsgs.lua +++ b/examples/sn_umsgs.lua @@ -1,13 +1,15 @@ require("sourcenet") -function _R.Player:GetNetChannel() +local PLAYER = FindMetaTable("Player") +function PLAYER:GetNetChannel() return CNetChan(self:EntIndex()) end -function _R.Player:Test() +function PLAYER:Test() local netchan = self:GetNetChannel() - - if not netchan then return end + if netchan == nil then + return + end local reliablebuffer = netchan:GetReliableBuffer() diff --git a/examples/sn_voicemimic.lua b/examples/sn_voicemimic.lua index 5512842..d132ed4 100644 --- a/examples/sn_voicemimic.lua +++ b/examples/sn_voicemimic.lua @@ -3,7 +3,7 @@ include("sourcenet/incoming.lua") FilterIncomingMessage(svc_VoiceData, function(netchan, read, write) write:WriteUInt(svc_VoiceData, NET_MESSAGE_BITS) - local client = read:ReadByte() + local client = read:ReadByte() write:WriteByte(client) local proximity = read:ReadByte() diff --git a/sourcenet/netmessages.lua b/sourcenet/netmessages.lua index 4b67974..10e75ca 100644 --- a/sourcenet/netmessages.lua +++ b/sourcenet/netmessages.lua @@ -1721,8 +1721,12 @@ NET_MESSAGES = { self.data = buffer:ReadBits(bits) end elseif self.msgtype == 4 then - self.length = buffer:ReadUInt(16) - self.data = buffer:ReadBytes(self.length) + self.id = buffer:ReadUInt(16) + bits = bits - 16 + + if bits > 0 then + self.data = buffer:ReadBits(bits) + end end self.initialized = true @@ -1739,7 +1743,7 @@ NET_MESSAGES = { if self.msgtype == 0 then buffer:WriteWord(self.id) - if bits > 0 then + if self.data then buffer:WriteBits(self.data) end elseif self.msgtype == 1 then @@ -1751,8 +1755,11 @@ NET_MESSAGES = { buffer:WriteBits(self.data) end elseif self.msgtype == 4 then - buffer:WriteUInt(self.length, 16) - buffer:WriteBytes(self.data) + buffer:WriteUInt(self.id, 16) + + if self.data then + buffer:WriteBits(self.data) + end end return true