Skip to content

Commit 3922d6f

Browse files
committed
Updated support Lua code
1 parent 3df25cd commit 3922d6f

File tree

7 files changed

+2034
-1024
lines changed

7 files changed

+2034
-1024
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: 6 additions & 6 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
@@ -38,10 +38,10 @@ end
3838

3939
if CLIENT then
4040
FilterIncomingMessage(svc_GameEvent, function(netchan, read, write)
41-
FilterGameEvent(netchan, read, write, "ProcessGameEvent")
41+
FilterGameEvent(netchan, read, write, "ProcessGameEvent", "client")
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: 64 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -7,135 +7,95 @@ 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-
}
31-
hook.Add("PreProcessMessages", "InFilter", function(netchan, read, write, localchan)
32-
local totalbits = read:GetNumBitsLeft() + read:GetNumBitsRead()
16+
local NET_MESSAGES_INSTANCES = {}
3317

34-
local islocal = netchan == localchan
35-
if not game.IsDedicated() and ((islocal and SERVER) or (not islocal and CLIENT)) then
36-
CopyBufferEnd(write, read)
37-
return
18+
local function GetNetMessageInstance(netchan, msgtype)
19+
local handler = NET_MESSAGES_INSTANCES[msgtype]
20+
if handler == nil then
21+
handler = NetMessage(netchan, msgtype, not SERVER)
22+
NET_MESSAGES_INSTANCES[msgtype] = handler
23+
else
24+
handler:Reset()
3825
end
3926

40-
hook.Call("BASE_PreProcessMessages", nil, netchan, read, write)
27+
return handler
28+
end
4129

42-
local changeLevelState = false
30+
local NET_MESSAGES_INCOMING_COPY = {
31+
NET = {},
32+
CLC = {},
33+
SVC = {}
34+
}
4335

44-
while read:GetNumBitsLeft() >= NET_MESSAGE_BITS do
45-
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
36+
local function GetIncomingCopyTableForMessageType(msgtype)
37+
if NET_MESSAGES.NET[msgtype] ~= nil then
38+
return NET_MESSAGES_INCOMING_COPY.NET
39+
end
6040

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-
67-
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
92-
end
41+
if CLIENT and NET_MESSAGES.SVC[msgtype] ~= nil then
42+
return NET_MESSAGES_INCOMING_COPY.SVC
43+
end
9344

94-
local func = handler.IncomingCopy or handler.DefaultCopy
45+
if SERVER and NET_MESSAGES.CLC[msgtype] ~= nil then
46+
return NET_MESSAGES_INCOMING_COPY.CLC
47+
end
9548

96-
local success, ret = xpcall(func, debug.traceback, netchan, read, write)
97-
if not success then
98-
print(ret)
49+
return nil
50+
end
9951

100-
break
101-
elseif ret == false then
102-
--if func(netchan, read, write) == false then
103-
Msg("Failed to filter message " .. msg .. "\n")
52+
local function DefaultCopy(netchan, read, write, handler)
53+
handler:ReadFromBuffer(read)
54+
handler:WriteToBuffer(write)
55+
end
10456

105-
write:Seek(totalbits)
57+
hook.Add("PreProcessMessages", "InFilter", function(netchan, read, write, localchan)
58+
local islocal = netchan == localchan
59+
if not game.IsDedicated() and ((islocal and SERVER) or (not islocal and CLIENT)) then
60+
return
61+
end
10662

107-
break
63+
while read:GetNumBitsLeft() >= NET_MESSAGE_BITS do
64+
local msgtype = read:ReadUInt(NET_MESSAGE_BITS)
65+
local handler = GetNetMessageInstance(netchan, msgtype)
66+
if handler == nil then
67+
MsgC(Color(255, 0, 0), "Unknown outgoing message " .. msgtype .. " with " .. read:GetNumBitsLeft() .. " bit(s) left\n")
68+
return false
10869
end
10970

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--]]
71+
local incoming_copy_table = GetIncomingCopyTableForMessageType(msgtype)
72+
local copy_function = incoming_copy_table ~= nil and incoming_copy_table[msgtype] or DefaultCopy
73+
copy_function(netchan, read, write, handler)
74+
75+
--MsgC(Color(255, 255, 255), "NetMessage: " .. tostring(handler) .. "\n")
11376
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
77+
78+
local bitsleft = read:GetNumBitsLeft()
79+
if bitsleft > 0 then
80+
-- Should be inocuous padding bits but just to be sure, let's copy them
81+
local data = read:ReadBits(bitsleft)
82+
write:WriteBits(data)
12083
end
121-
end)
12284

123-
function FilterIncomingMessage(msg, func)
124-
local handler = NET_MESSAGES[msg]
85+
--MsgC(Color(0, 255, 0), "Fully parsed stream with " .. totalbits .. " bit(s) written\n")
86+
return true
87+
end)
12588

126-
if not handler then
127-
if CLIENT then
128-
handler = NET_MESSAGES.SVC[msg]
129-
else
130-
handler = NET_MESSAGES.CLC[msg]
131-
end
89+
function FilterIncomingMessage(msgtype, func)
90+
local incoming_copy_table = GetIncomingCopyTableForMessageType(msgtype)
91+
if incoming_copy_table == nil then
92+
return false
13293
end
13394

134-
if handler then
135-
handler.IncomingCopy = func
136-
end
95+
incoming_copy_table[msgtype] = func
96+
return true
13797
end
13898

139-
function UnFilterIncomingMessage(msg)
140-
FilterIncomingMessage(msg, nil)
99+
function UnFilterIncomingMessage(msgtype)
100+
return FilterIncomingMessage(msgtype, nil)
141101
end

0 commit comments

Comments
 (0)