Skip to content

Commit

Permalink
Cleaned up Lua examples code
Browse files Browse the repository at this point in the history
  • Loading branch information
danielga committed Dec 21, 2021
1 parent 5c0e488 commit cc8a3cf
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 77 deletions.
1 change: 0 additions & 1 deletion examples/sn_changedisconnect.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 4 additions & 5 deletions examples/sn_cheatskick.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion examples/sn_clientconnect.lua
Original file line number Diff line number Diff line change
@@ -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")))

Expand Down
3 changes: 1 addition & 2 deletions examples/sn_clog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions examples/sn_entmessages.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 9 additions & 14 deletions examples/sn_fstream.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,41 @@ 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)
for i = 0, MAX_STREAMS - 1 do
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

Expand Down
9 changes: 5 additions & 4 deletions examples/sn_loadcodec.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
15 changes: 5 additions & 10 deletions examples/sn_namechange.lua
Original file line number Diff line number Diff line change
@@ -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 <name>")

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
Expand Down
14 changes: 8 additions & 6 deletions examples/sn_setconvars.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
3 changes: 1 addition & 2 deletions examples/sn_slog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions examples/sn_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
24 changes: 10 additions & 14 deletions examples/sn_test_fstream.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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")
Expand Down
18 changes: 9 additions & 9 deletions examples/sn_umsghooks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions examples/sn_umsgs.lua
Original file line number Diff line number Diff line change
@@ -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()

Expand Down
2 changes: 1 addition & 1 deletion examples/sn_voicemimic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit cc8a3cf

Please sign in to comment.