Skip to content

Commit e4932e9

Browse files
committed
WIP sourcenet Lua code
1 parent 12b734d commit e4932e9

File tree

7 files changed

+1611
-1001
lines changed

7 files changed

+1611
-1001
lines changed

sourcenet/client.lua

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ function HookNetChannel(...)
1313
local name = v.name:gsub("::", "_")
1414

1515
local exists = false
16-
16+
1717
for k, v in pairs(NET_HOOKS.attach) do
1818
if v.name == name then
1919
exists = true
2020
break
2121
end
2222
end
23-
23+
2424
if not exists then
2525
table.insert(NET_HOOKS.attach, {name = name, hook = _G["Attach__" .. name], func = v.func, args = v.args, nochan = v.nochan})
2626
table.insert(NET_HOOKS.detach, {name = name, hook = _G["Detach__" .. name], func = v.func, args = v.args, nochan = v.nochan})
2727
end
2828
end
29-
29+
3030
local function StandardNetHook(netchan, nethook)
3131
local args = {}
3232

@@ -35,7 +35,7 @@ function HookNetChannel(...)
3535
elseif not nethook.nochan then
3636
table.insert(args, netchan)
3737
end
38-
38+
3939
if nethook.args then
4040
for k, v in pairs(nethook.args) do
4141
table.insert(args, v)
@@ -50,13 +50,13 @@ function HookNetChannel(...)
5050
if not netchan then return false end
5151

5252
Attach__CNetChan_Shutdown(netchan)
53-
53+
5454
NET_ATTACHED = true
5555

5656
for k, v in pairs(NET_HOOKS.attach) do
5757
StandardNetHook(netchan, v)
5858
end
59-
59+
6060
return true
6161
end
6262

@@ -65,7 +65,7 @@ function HookNetChannel(...)
6565
if not netchan then return false end
6666

6767
Detach__CNetChan_Shutdown(netchan)
68-
68+
6969
NET_ATTACHED = false
7070

7171
for k, v in pairs(NET_HOOKS.detach) do
@@ -77,10 +77,9 @@ function HookNetChannel(...)
7777

7878
if not AttachNetChannel(CNetChan()) then
7979
hook.Add("Think", "CreateNetChannel", function() -- Wait until channel is created
80-
if CNetChan() then
81-
if AttachNetChannel(CNetChan()) then
82-
hook.Remove("Think", "CreateNetChannel")
83-
end
80+
local netchan = CNetChan()
81+
if netchan ~= nil and AttachNetChannel(netchan) then
82+
hook.Remove("Think", "CreateNetChannel")
8483
end
8584
end )
8685
end

sourcenet/gameevents.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ else
55
end
66

77
local manager = IGameEventManager2()
8-
local function FilterGameEvent(netchan, read, write, hookname)
8+
local function FilterGameEvent(netchan, read, write, hookname, streamname)
99
local bits = read:ReadUInt(11)
1010
local data = read:ReadBits(bits)
1111

12-
SourceNetMsg(string.format("svc_GameEvent bits=%i\n", bits))
12+
SourceNetMsg(string.format("svc_GameEvent on %s stream: bits=%i\n", streamname, bits))
1313

1414
if not read:IsOverflowed() then
1515
local buffer = sn_bf_read(data)
@@ -25,7 +25,7 @@ local function FilterGameEvent(netchan, read, write, hookname)
2525
local serialized_buffer = sn_bf_write(serialized_data)
2626

2727
manager:SerializeEvent(event, serialized_buffer)
28-
28+
2929
write:WriteUInt(serialized_buffer:GetNumBitsWritten(), 11)
3030
write:WriteBits(serialized_buffer:GetBasePointer())
3131
else
@@ -37,11 +37,11 @@ local function FilterGameEvent(netchan, read, write, hookname)
3737
end
3838

3939
if CLIENT then
40-
FilterIncomingMessage(svc_GameEvent, function(netchan, read, write)
41-
FilterGameEvent(netchan, read, write, "ProcessGameEvent")
40+
FilterIncomingMessage(svc_GameEvent, function(netchan, read, write, streamname)
41+
FilterGameEvent(netchan, read, write, "ProcessGameEvent", streamname)
4242
end)
4343
else
44-
FilterOutgoingMessage(svc_GameEvent, function(netchan, read, write)
45-
FilterGameEvent(netchan, read, write, "SendGameEvent")
44+
FilterOutgoingMessage(svc_GameEvent, function(netchan, read, write, streamname)
45+
FilterGameEvent(netchan, read, write, "SendGameEvent", streamname)
4646
end)
4747
end

sourcenet/incoming.lua

Lines changed: 28 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -7,133 +7,68 @@ end
77
include("netmessages.lua")
88

99
-- Initialization
10-
1110
HookNetChannel(
1211
-- nochan prevents a net channel being passed to the attach/detach functions
1312
-- CNetChan::ProcessMessages doesn't use a virtual hook, so we don't need to pass the net channel
1413
{name = "CNetChan::ProcessMessages", nochan = true}
1514
)
1615

17-
local function CopyBufferEnd(dst, src)
18-
local bitsleft = src:GetNumBitsLeft()
19-
local data = src:ReadBits(bitsleft)
20-
21-
dst:WriteBits(data)
22-
end
23-
24-
local specialmsg
25-
local specialhandler = {
26-
DefaultCopy = function(netchan, read, write)
27-
specialmsg:ReadFromBuffer(read)
28-
specialmsg:WriteToBuffer(write)
29-
end
30-
}
3116
hook.Add("PreProcessMessages", "InFilter", function(netchan, read, write, localchan)
32-
local totalbits = read:GetNumBitsLeft() + read:GetNumBitsRead()
33-
3417
local islocal = netchan == localchan
3518
if not game.IsDedicated() and ((islocal and SERVER) or (not islocal and CLIENT)) then
36-
CopyBufferEnd(write, read)
37-
return
19+
MsgC(Color(255, 255, 0), "Ignoring a stream\n")
20+
return false
3821
end
3922

40-
hook.Call("BASE_PreProcessMessages", nil, netchan, read, write)
41-
42-
local changeLevelState = false
23+
local totalbits = read:GetNumBitsLeft()
4324

4425
while read:GetNumBitsLeft() >= NET_MESSAGE_BITS do
4526
local msg = read:ReadUInt(NET_MESSAGE_BITS)
46-
47-
if CLIENT then
48-
-- Hack to prevent changelevel crashes
49-
if msg == net_SignonState then
50-
local state = read:ReadByte()
51-
52-
if state == SIGNONSTATE_CHANGELEVEL then
53-
changeLevelState = true
54-
--print( "[gm_sourcenet] Received changelevel packet" )
55-
end
56-
57-
read:Seek(read:GetNumBitsRead() - 8)
58-
end
59-
end
60-
61-
local handler = NET_MESSAGES[msg]
62-
63-
--[[if msg ~= net_NOP and msg ~= 3 and msg ~= 9 then
64-
Msg("(in) Pre Message: " .. msg .. ", bits: " .. read:GetNumBitsRead() .. "/" .. totalbits .. "\n")
65-
end--]]
66-
27+
local handler = NetMessage(msg, not SERVER)
6728
if not handler then
68-
if CLIENT then
69-
handler = NET_MESSAGES.SVC[msg]
70-
else
71-
handler = NET_MESSAGES.CLC[msg]
72-
end
73-
74-
if not handler then
75-
for i = 1, netchan:GetNetMessageNum() do
76-
local m = netchan:GetNetMessage(i)
77-
if m:GetType() == msg then
78-
handler = specialhandler
79-
specialmsg = m
80-
break
81-
end
82-
end
83-
84-
if not handler then
85-
Msg("Unknown outgoing message: " .. msg .. "\n")
86-
87-
write:Seek(totalbits)
88-
89-
break
90-
end
91-
end
29+
MsgC(Color(255, 0, 0), "Unknown outgoing message " .. msg .. " with " .. read:GetNumBitsLeft() .. " bit(s) left\n")
30+
return false
9231
end
9332

94-
local func = handler.IncomingCopy or handler.DefaultCopy
33+
--[[local success =]] handler:ReadFromBuffer(read)
34+
--[[if not success then
35+
MsgC(Color(255, 0, 0), "Failed to read message " .. handler:GetName() .. " with " .. read:GetNumBitsLeft() .. " bit(s) left\n")
36+
return false
37+
end]]
9538

96-
local success, ret = xpcall(func, debug.traceback, netchan, read, write)
39+
local success = handler:WriteToBuffer(write)
9740
if not success then
98-
print(ret)
99-
100-
break
101-
elseif ret == false then
102-
--if func(netchan, read, write) == false then
103-
Msg("Failed to filter message " .. msg .. "\n")
104-
105-
write:Seek(totalbits)
106-
107-
break
41+
MsgC(Color(255, 0, 0), "Failed to write message " .. handler:GetName() .. " with " .. read:GetNumBitsLeft() .. " bit(s) left\n")
42+
return false
10843
end
10944

110-
--[[if msg ~= net_NOP and msg ~= 3 and msg ~= 9 then
111-
Msg("(in) Post Message: " .. msg .. " bits: " .. read:GetNumBitsRead() .. "/" .. totalbits .. "\n")
112-
end--]]
45+
MsgC(Color(255, 255, 255), "NetMessage: " .. tostring(handler) .. "\n")
11346
end
114-
115-
if CLIENT then
116-
if changeLevelState then
117-
--print("[gm_sourcenet] Server is changing level, calling PreNetChannelShutdown")
118-
hook.Call("PreNetChannelShutdown", nil, netchan, "Server Changing Level")
119-
end
47+
48+
local bitsleft = read:GetNumBitsLeft()
49+
if bitsleft > 0 then
50+
-- Should be inocuous padding bits but just to be sure, let's copy them
51+
local data = read:ReadBits(bitsleft)
52+
write:WriteBits(data)
12053
end
54+
55+
MsgC(Color(0, 255, 0), "Fully parsed stream with " .. totalbits .. " bit(s) written\n")
56+
return true
12157
end)
12258

12359
function FilterIncomingMessage(msg, func)
124-
local handler = NET_MESSAGES[msg]
125-
126-
if not handler then
60+
--[[local handler = NET_MESSAGES[msg]
61+
if handler == nil then
12762
if CLIENT then
12863
handler = NET_MESSAGES.SVC[msg]
12964
else
13065
handler = NET_MESSAGES.CLC[msg]
13166
end
13267
end
13368
134-
if handler then
69+
if handler ~= nil then
13570
handler.IncomingCopy = func
136-
end
71+
end]]
13772
end
13873

13974
function UnFilterIncomingMessage(msg)

0 commit comments

Comments
 (0)