Skip to content

Commit

Permalink
Updated support Lua code
Browse files Browse the repository at this point in the history
  • Loading branch information
danielga committed Jan 1, 2022
1 parent cc8a3cf commit 50add42
Show file tree
Hide file tree
Showing 7 changed files with 2,035 additions and 1,024 deletions.
21 changes: 10 additions & 11 deletions sourcenet/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ function HookNetChannel(...)
local name = v.name:gsub("::", "_")

local exists = false

for k, v in pairs(NET_HOOKS.attach) do
if v.name == name then
exists = true
break
end
end

if not exists then
table.insert(NET_HOOKS.attach, {name = name, hook = _G["Attach__" .. name], func = v.func, args = v.args, nochan = v.nochan})
table.insert(NET_HOOKS.detach, {name = name, hook = _G["Detach__" .. name], func = v.func, args = v.args, nochan = v.nochan})
end
end

local function StandardNetHook(netchan, nethook)
local args = {}

Expand All @@ -35,7 +35,7 @@ function HookNetChannel(...)
elseif not nethook.nochan then
table.insert(args, netchan)
end

if nethook.args then
for k, v in pairs(nethook.args) do
table.insert(args, v)
Expand All @@ -50,13 +50,13 @@ function HookNetChannel(...)
if not netchan then return false end

Attach__CNetChan_Shutdown(netchan)

NET_ATTACHED = true

for k, v in pairs(NET_HOOKS.attach) do
StandardNetHook(netchan, v)
end

return true
end

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

Detach__CNetChan_Shutdown(netchan)

NET_ATTACHED = false

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

if not AttachNetChannel(CNetChan()) then
hook.Add("Think", "CreateNetChannel", function() -- Wait until channel is created
if CNetChan() then
if AttachNetChannel(CNetChan()) then
hook.Remove("Think", "CreateNetChannel")
end
local netchan = CNetChan()
if netchan ~= nil and AttachNetChannel(netchan) then
hook.Remove("Think", "CreateNetChannel")
end
end )
end
Expand Down
12 changes: 6 additions & 6 deletions sourcenet/gameevents.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ else
end

local manager = IGameEventManager2()
local function FilterGameEvent(netchan, read, write, hookname)
local function FilterGameEvent(netchan, read, write, hookname, streamname)
local bits = read:ReadUInt(11)
local data = read:ReadBits(bits)

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

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

manager:SerializeEvent(event, serialized_buffer)

write:WriteUInt(serialized_buffer:GetNumBitsWritten(), 11)
write:WriteBits(serialized_buffer:GetBasePointer())
else
Expand All @@ -38,10 +38,10 @@ end

if CLIENT then
FilterIncomingMessage(svc_GameEvent, function(netchan, read, write)
FilterGameEvent(netchan, read, write, "ProcessGameEvent")
FilterGameEvent(netchan, read, write, "ProcessGameEvent", "client")
end)
else
FilterOutgoingMessage(svc_GameEvent, function(netchan, read, write)
FilterGameEvent(netchan, read, write, "SendGameEvent")
FilterOutgoingMessage(svc_GameEvent, function(netchan, read, write, streamname)
FilterGameEvent(netchan, read, write, "SendGameEvent", streamname)
end)
end
168 changes: 64 additions & 104 deletions sourcenet/incoming.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,135 +7,95 @@ end
include("netmessages.lua")

-- Initialization

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

local function CopyBufferEnd(dst, src)
local bitsleft = src:GetNumBitsLeft()
local data = src:ReadBits(bitsleft)

dst:WriteBits(data)
end

local specialmsg
local specialhandler = {
DefaultCopy = function(netchan, read, write)
specialmsg:ReadFromBuffer(read)
specialmsg:WriteToBuffer(write)
end
}
hook.Add("PreProcessMessages", "InFilter", function(netchan, read, write, localchan)
local totalbits = read:GetNumBitsLeft() + read:GetNumBitsRead()
local NET_MESSAGES_INSTANCES = {}

local islocal = netchan == localchan
if not game.IsDedicated() and ((islocal and SERVER) or (not islocal and CLIENT)) then
CopyBufferEnd(write, read)
return
local function GetNetMessageInstance(netchan, msgtype)
local handler = NET_MESSAGES_INSTANCES[msgtype]
if handler == nil then
handler = NetMessage(netchan, msgtype, not SERVER)
NET_MESSAGES_INSTANCES[msgtype] = handler
else
handler:Reset()
end

hook.Call("BASE_PreProcessMessages", nil, netchan, read, write)
return handler
end

local changeLevelState = false
local NET_MESSAGES_INCOMING_COPY = {
NET = {},
CLC = {},
SVC = {}
}

while read:GetNumBitsLeft() >= NET_MESSAGE_BITS do
local msg = read:ReadUInt(NET_MESSAGE_BITS)

if CLIENT then
-- Hack to prevent changelevel crashes
if msg == net_SignonState then
local state = read:ReadByte()

if state == SIGNONSTATE_CHANGELEVEL then
changeLevelState = true
--print( "[gm_sourcenet] Received changelevel packet" )
end

read:Seek(read:GetNumBitsRead() - 8)
end
end
local function GetIncomingCopyTableForMessageType(msgtype)
if NET_MESSAGES.NET[msgtype] ~= nil then
return NET_MESSAGES_INCOMING_COPY.NET
end

local handler = NET_MESSAGES[msg]

--[[if msg ~= net_NOP and msg ~= 3 and msg ~= 9 then
Msg("(in) Pre Message: " .. msg .. ", bits: " .. read:GetNumBitsRead() .. "/" .. totalbits .. "\n")
end--]]

if not handler then
if CLIENT then
handler = NET_MESSAGES.SVC[msg]
else
handler = NET_MESSAGES.CLC[msg]
end

if not handler then
for i = 1, netchan:GetNetMessageNum() do
local m = netchan:GetNetMessage(i)
if m:GetType() == msg then
handler = specialhandler
specialmsg = m
break
end
end

if not handler then
Msg("Unknown outgoing message: " .. msg .. "\n")

write:Seek(totalbits)

break
end
end
end
if CLIENT and NET_MESSAGES.SVC[msgtype] ~= nil then
return NET_MESSAGES_INCOMING_COPY.SVC
end

local func = handler.IncomingCopy or handler.DefaultCopy
if SERVER and NET_MESSAGES.CLC[msgtype] ~= nil then
return NET_MESSAGES_INCOMING_COPY.CLC
end

local success, ret = xpcall(func, debug.traceback, netchan, read, write)
if not success then
print(ret)
return nil
end

break
elseif ret == false then
--if func(netchan, read, write) == false then
Msg("Failed to filter message " .. msg .. "\n")
local function DefaultCopy(netchan, read, write, handler)
handler:ReadFromBuffer(read)
handler:WriteToBuffer(write)
end

write:Seek(totalbits)
hook.Add("PreProcessMessages", "InFilter", function(netchan, read, write, localchan)
local islocal = netchan == localchan
if not game.IsDedicated() and ((islocal and SERVER) or (not islocal and CLIENT)) then
return
end

break
while read:GetNumBitsLeft() >= NET_MESSAGE_BITS do
local msgtype = read:ReadUInt(NET_MESSAGE_BITS)
local handler = GetNetMessageInstance(netchan, msgtype)
if handler == nil then
MsgC(Color(255, 0, 0), "Unknown outgoing message " .. msgtype .. " with " .. read:GetNumBitsLeft() .. " bit(s) left\n")
return false
end

--[[if msg ~= net_NOP and msg ~= 3 and msg ~= 9 then
Msg("(in) Post Message: " .. msg .. " bits: " .. read:GetNumBitsRead() .. "/" .. totalbits .. "\n")
end--]]
local incoming_copy_table = GetIncomingCopyTableForMessageType(msgtype)
local copy_function = incoming_copy_table ~= nil and incoming_copy_table[msgtype] or DefaultCopy
copy_function(netchan, read, write, handler)

--MsgC(Color(255, 255, 255), "NetMessage: " .. tostring(handler) .. "\n")
end
if CLIENT then
if changeLevelState then
--print("[gm_sourcenet] Server is changing level, calling PreNetChannelShutdown")
hook.Call("PreNetChannelShutdown", nil, netchan, "Server Changing Level")
end

local bitsleft = read:GetNumBitsLeft()
if bitsleft > 0 then
-- Should be inocuous padding bits but just to be sure, let's copy them
local data = read:ReadBits(bitsleft)
write:WriteBits(data)
end
end)

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

if not handler then
if CLIENT then
handler = NET_MESSAGES.SVC[msg]
else
handler = NET_MESSAGES.CLC[msg]
end
function FilterIncomingMessage(msgtype, func)
local incoming_copy_table = GetIncomingCopyTableForMessageType(msgtype)
if incoming_copy_table == nil then
return false
end

if handler then
handler.IncomingCopy = func
end
incoming_copy_table[msgtype] = func
return true
end

function UnFilterIncomingMessage(msg)
FilterIncomingMessage(msg, nil)
function UnFilterIncomingMessage(msgtype)
return FilterIncomingMessage(msgtype, nil)
end
Loading

0 comments on commit 50add42

Please sign in to comment.