|
7 | 7 | include("netmessages.lua")
|
8 | 8 |
|
9 | 9 | -- Initialization
|
10 |
| - |
11 | 10 | HookNetChannel(
|
12 | 11 | -- nochan prevents a net channel being passed to the attach/detach functions
|
13 | 12 | -- CNetChan::ProcessMessages doesn't use a virtual hook, so we don't need to pass the net channel
|
14 | 13 | {name = "CNetChan::ProcessMessages", nochan = true}
|
15 | 14 | )
|
16 | 15 |
|
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 | 16 | hook.Add("PreProcessMessages", "InFilter", function(netchan, read, write, localchan)
|
32 |
| - local totalbits = read:GetNumBitsLeft() + read:GetNumBitsRead() |
33 |
| - |
34 | 17 | local islocal = netchan == localchan
|
35 | 18 | 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 |
38 | 21 | end
|
39 | 22 |
|
40 |
| - hook.Call("BASE_PreProcessMessages", nil, netchan, read, write) |
41 |
| - |
42 |
| - local changeLevelState = false |
| 23 | + local totalbits = read:GetNumBitsLeft() |
43 | 24 |
|
44 | 25 | while read:GetNumBitsLeft() >= NET_MESSAGE_BITS do
|
45 | 26 | 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) |
67 | 28 | 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 |
92 | 31 | end
|
93 | 32 |
|
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]] |
95 | 38 |
|
96 |
| - local success, ret = xpcall(func, debug.traceback, netchan, read, write) |
| 39 | + local success = handler:WriteToBuffer(write) |
97 | 40 | 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 |
108 | 43 | end
|
109 | 44 |
|
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") |
113 | 46 | 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) |
120 | 53 | end
|
| 54 | + |
| 55 | + MsgC(Color(0, 255, 0), "Fully parsed stream with " .. totalbits .. " bit(s) written\n") |
| 56 | + return true |
121 | 57 | end)
|
122 | 58 |
|
123 | 59 | 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 |
127 | 62 | if CLIENT then
|
128 | 63 | handler = NET_MESSAGES.SVC[msg]
|
129 | 64 | else
|
130 | 65 | handler = NET_MESSAGES.CLC[msg]
|
131 | 66 | end
|
132 | 67 | end
|
133 | 68 |
|
134 |
| - if handler then |
| 69 | + if handler ~= nil then |
135 | 70 | handler.IncomingCopy = func
|
136 |
| - end |
| 71 | + end]] |
137 | 72 | end
|
138 | 73 |
|
139 | 74 | function UnFilterIncomingMessage(msg)
|
|
0 commit comments