Skip to content

Commit 3317100

Browse files
committed
Cleaned up Lua examples code
1 parent c15ab25 commit 3317100

15 files changed

+68
-77
lines changed

examples/sn_changedisconnect.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ hook.Add("SendGameEvent", "ChangeReason", function(netchan, event)
77

88
if reason == "Disconnect by user." then
99
event:SetString("reason", "Disconnected after " .. math.floor(netchan:GetTime() - netchan:GetConnectTime()) .. " seconds")
10-
1110
return event
1211
end
1312
end)

examples/sn_cheatskick.lua

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ hook.Add("PlayerInitialSpawn", "InitialCheatsCheck", function(ply)
1313
end)
1414

1515
hook.Add("RespondCvarValue", "InitialCheatsCheck", function(netchan, cookie, status, cvarname, cvarvalue)
16-
if status ~= 0 then return end
17-
if cvarname ~= "sv_cheats" then return end
18-
if cvarvalue == GetConVarString("sv_cheats") then return end
19-
16+
if status ~= 0 or cvarname ~= "sv_cheats" or cvarvalue == GetConVarString("sv_cheats") then
17+
return
18+
end
19+
2020
local ply = FindPlayerByNetChannel(netchan)
21-
2221
if IsValid(ply) and cookie == ply.CheatsCookie then
2322
ply:Kick("Incorrect sv_cheats value")
2423
end

examples/sn_clientconnect.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
include("sourcenet/gameevents.lua")
22

33
hook.Add("ProcessGameEvent", "PlayerConnect", function(netchan, event)
4-
if event:GetName() ~= "player_connect" then return end
4+
if event:GetName() ~= "player_connect" then
5+
return
6+
end
57

68
event:SetString("name", string.reverse(event:GetString("name")))
79

examples/sn_clog.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ include("sourcenet/outgoing.lua")
22

33
FilterOutgoingMessage(net_StringCmd, function(netchan, read, write)
44
local cmd = read:ReadString()
5-
5+
66
print(string.format("Sending command \"%s\"", cmd))
77

88
if string.Left(cmd, 6) == "status" then
99
print("Stopped status command being sent")
10-
1110
return
1211
end
1312

examples/sn_entmessages.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ WEAPONSWEP_MSG_EQUIP = 3
88
function SendEntityMessage(netchan, entindex, classID, buffer)
99
local messageType = buffer:ReadByte()
1010
local entity = Entity(entindex)
11-
12-
if not IsValid(entity) then return end
11+
12+
if not IsValid(entity) then
13+
return
14+
end
1315

1416
if entity:IsWeapon() then -- There is no Entity.GetClassID function, so this is a workaround
1517
if messageType == WEAPONSWEP_MSG_HOLSTER then

examples/sn_fstream.lua

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,41 @@ else
55
end
66

77
-- Initialization
8-
98
HookNetChannel(
109
{name = "CNetChan::ProcessPacket"}
1110
)
1211

1312
-- Check progress every incoming packet (Source seems to clear fragments here)
14-
1513
local history = {}
1614

1715
hook.Add("PreProcessPacket", "TransferStatus", function(netchan)
1816
for i = 0, MAX_STREAMS - 1 do
1917
for j = 0, netchan:GetOutgoingQueueSize(i) - 1 do
2018
local fragments = netchan:GetOutgoingQueueFragments(i, j)
2119
local filename = fragments:GetFileName()
20+
if #filename ~= 0 and not table.HasValue(history, filename) and fragments:GetProgress() + fragments:GetNum() >= fragments:GetTotal() then
21+
print("Finished " .. filename )
2222

23-
if filename ~= "" and not table.HasValue(history, filename) then
24-
if fragments:GetProgress() + fragments:GetNum() >= fragments:GetTotal() then
25-
print("Finished " .. filename )
23+
umsg.Start("fstream_complete")
24+
umsg.String(filename)
25+
umsg.End()
2626

27-
umsg.Start("fstream_complete")
28-
umsg.String(filename)
29-
umsg.End()
30-
31-
table.insert(history, filename)
32-
end
27+
table.insert(history, filename)
3328
end
3429
end
3530
end
3631
end)
3732

3833
-- Tests
39-
4034
function QueueFile(netchan, filename)
4135
netchan:SendFile(filename, 1)
4236
end
4337

4438
hook.Add("PlayerInitialSpawn", "BeginTransfer", function(ply)
4539
local netchan = CNetChan(ply:EntIndex())
46-
47-
if not netchan then return end
40+
if netchan == nil then
41+
return
42+
end
4843

4944
netchan:SetBackgroundMode(false) -- Removes 1 file fragment per-packet limit
5045

examples/sn_loadcodec.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
require("sourcenet" )
1+
require("sourcenet")
22

33
concommand.Add("loadcodec", function(ply, cmd, args)
4-
if not IsValid(ply) then return end
5-
if not args[1] then return end
4+
if not IsValid(ply) or args[1] == nil then
5+
return
6+
end
67

78
local buffer = CNetChan(ply:EntIndex()):GetReliableBuffer()
8-
9+
910
buffer:WriteUInt(svc_VoiceInit, NET_MESSAGE_BITS)
1011
buffer:WriteString(args[1])
1112
buffer:WriteByte(1)

examples/sn_namechange.lua

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
require("sourcenet")
22

33
concommand.Add("setname", function(ply, cmd, args)
4-
if not args[1] then
4+
if args[1] == nil then
55
print("Syntax: setname <name>")
6-
76
return
87
end
9-
8+
109
local netchan = CNetChan()
11-
12-
if not netchan then
10+
if netchan == nil then
1311
print("setname: invalid netchan")
14-
1512
return
1613
end
1714

1815
local buffer = netchan:GetReliableBuffer()
19-
20-
if not buffer then
16+
if buffer == nil then
2117
print("setname: invalid buffer")
22-
2318
return
2419
end
25-
20+
2621
buffer:WriteUInt(net_SetConVar, NET_MESSAGE_BITS) -- message type
2722
buffer:WriteByte(1) -- convar count
2823
buffer:WriteString("name") -- convar name

examples/sn_setconvars.lua

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
require("sourcenet")
22

3-
function _R.Player:GetNetChannel()
3+
local PLAYER = FindMetaTable("Player")
4+
function PLAYER:GetNetChannel()
45
return CNetChan(self:EntIndex())
56
end
67

7-
function _R.Player:SetConVar(name, value)
8+
function PLAYER:SetConVar(name, value)
89
local netchan = self:GetNetChannel()
9-
10-
if not netchan then return end
11-
10+
if netchan == nil then
11+
return
12+
end
13+
1214
local buf = netchan:GetReliableBuffer()
13-
15+
1416
buf:WriteUInt(net_SetConVar, NET_MESSAGE_BITS)
1517
buf:WriteByte(1)
1618
buf:WriteString(name)

examples/sn_slog.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ include("sourcenet/incoming.lua")
22

33
FilterIncomingMessage(net_StringCmd, function(netchan, read, write)
44
local cmd = read:ReadString()
5-
5+
66
print(string.format("Client %s ran command \"%s\"", netchan:GetAddress():ToString(), cmd))
77

88
if string.Left(cmd, 6) == "status" then
99
print("Blocked status command")
10-
1110
return
1211
end
1312

0 commit comments

Comments
 (0)