Skip to content

Commit 2c341b3

Browse files
committed
WIP sourcenet Lua examples code
1 parent a46d2ae commit 2c341b3

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

examples/sn_test.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ include("sourcenet/outgoing.lua")
33

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

99
write:WriteUInt(net_StringCmd, NET_MESSAGE_BITS)
@@ -12,7 +12,7 @@ end)
1212

1313
FilterIncomingMessage(net_StringCmd, function(netchan, read, write)
1414
local cmd = read:ReadString()
15-
15+
1616
print(string.format("Executing command \"%s\"", cmd))
1717

1818
write:WriteUInt(net_StringCmd, NET_MESSAGE_BITS)

examples/sn_test_fstream.lua

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
include("sourcenet/server.lua")
22

33
-- Initialization
4-
54
HookNetChannel(
65
{name = "CNetChan::ProcessPacket"}
76
)
87

98
-- Check progress every incoming packet (Source seems to clear fragments here)
10-
119
local history = {}
1210

1311
hook.Add("PreProcessPacket", "TransferStatus", function(netchan)
@@ -16,31 +14,29 @@ hook.Add("PreProcessPacket", "TransferStatus", function(netchan)
1614
local fragments = netchan:GetOutgoingQueueFragments(i, j)
1715
local filename = fragments:GetFileName()
1816

19-
if filename ~= "" and not table.HasValue(history, filename) then
20-
if fragments:GetProgress() + fragments:GetNum() >= fragments:GetTotal() then
21-
print("Finished " .. filename)
17+
if #filename ~= 0 and not table.HasValue(history, filename) and fragments:GetProgress() + fragments:GetNum() >= fragments:GetTotal() then
18+
print("Finished " .. filename)
19+
20+
umsg.Start("fstream_complete")
21+
umsg.String(filename)
22+
umsg.End()
2223

23-
umsg.Start("fstream_complete")
24-
umsg.String(filename)
25-
umsg.End()
26-
27-
table.insert(history, filename)
28-
end
24+
table.insert(history, filename)
2925
end
3026
end
3127
end
3228
end)
3329

3430
-- Tests
35-
3631
function QueueFile(netchan, filename)
3732
netchan:SendFile(filename, 1)
3833
end
3934

4035
hook.Add("PlayerInitialSpawn", "BeginTransfer", function(ply)
4136
local netchan = CNetChan(ply:EntIndex())
42-
43-
if not netchan then return end
37+
if netchan == nil then
38+
return
39+
end
4440

4541
QueueFile(netchan, "cl.db")
4642
QueueFile(netchan, "gameinfo.txt")

examples/sn_umsghooks.lua

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ LUASTRINGS_TABLE_NAME = "networkstring"
55
function ReadUserMessageString(buf)
66
if buf:ReadBit() == 1 then
77
local container = INetworkStringTableContainer()
8-
9-
if not container then return end
10-
11-
local pool = container:FindTable(LUASTRINGS_TABLE_NAME)
8+
if container == nil then
9+
return
10+
end
1211

13-
if not pool then return end
12+
local pool = container:FindTable(LUASTRINGS_TABLE_NAME)
13+
if pool == nil then
14+
return
15+
end
1416

1517
local str = pool:GetString(buf:ReadShort())
16-
1718
return str or "[STRING NOT POOLED]"
1819
else
1920
return buf:ReadString()
@@ -25,10 +26,9 @@ function ProcessUserMessage(msg, data)
2526

2627
if msg == 34 then
2728
local umsgName = ReadUserMessageString(buf)
28-
2929
Msg(string.format("Received Lua user message: curtime %f, name '%s', bytes %i\n", CurTime(), umsgName, buf:GetNumBytesLeft()))
3030
elseif msg == 40 then
31-
local varUnknown = buf:ReadLong()
31+
buf:ReadLong()
3232
local varType = buf:ReadByte()
3333
local varName = ReadUserMessageString(buf)
3434
local varValue
@@ -53,7 +53,7 @@ end
5353

5454
FilterIncomingMessage(svc_UserMessage, function(netchan, read, write)
5555
write:WriteUInt(svc_UserMessage, NET_MESSAGE_BITS)
56-
56+
5757
local msg = read:ReadByte()
5858
local bits = read:ReadUInt(11)
5959
local data = read:ReadBits(bits)

examples/sn_umsgs.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
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:Test()
8+
function PLAYER:Test()
89
local netchan = self:GetNetChannel()
9-
10-
if not netchan then return end
10+
if netchan == nil then
11+
return
12+
end
1113

1214
local reliablebuffer = netchan:GetReliableBuffer()
1315

examples/sn_voicemimic.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ include("sourcenet/incoming.lua")
33
FilterIncomingMessage(svc_VoiceData, function(netchan, read, write)
44
write:WriteUInt(svc_VoiceData, NET_MESSAGE_BITS)
55

6-
local client = read:ReadByte()
6+
local client = read:ReadByte()
77
write:WriteByte(client)
88

99
local proximity = read:ReadByte()

0 commit comments

Comments
 (0)