Skip to content

Commit

Permalink
WIP sourcenet Lua code
Browse files Browse the repository at this point in the history
  • Loading branch information
danielga committed Apr 25, 2021
1 parent c7762cf commit 5955110
Show file tree
Hide file tree
Showing 7 changed files with 1,615 additions and 1,001 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
14 changes: 7 additions & 7 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 @@ -37,11 +37,11 @@ local function FilterGameEvent(netchan, read, write, hookname)
end

if CLIENT then
FilterIncomingMessage(svc_GameEvent, function(netchan, read, write)
FilterGameEvent(netchan, read, write, "ProcessGameEvent")
FilterIncomingMessage(svc_GameEvent, function(netchan, read, write, streamname)
FilterGameEvent(netchan, read, write, "ProcessGameEvent", streamname)
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
121 changes: 28 additions & 93 deletions sourcenet/incoming.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,133 +7,68 @@ 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 islocal = netchan == localchan
if not game.IsDedicated() and ((islocal and SERVER) or (not islocal and CLIENT)) then
CopyBufferEnd(write, read)
return
MsgC(Color(255, 255, 0), "Ignoring a stream\n")
return false
end

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

local changeLevelState = false
local totalbits = read:GetNumBitsLeft()

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 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--]]

local handler = NetMessage(msg, not SERVER)
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
MsgC(Color(255, 0, 0), "Unknown outgoing message " .. msg .. " with " .. read:GetNumBitsLeft() .. " bit(s) left\n")
return false
end

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

local success, ret = xpcall(func, debug.traceback, netchan, read, write)
local success = handler:WriteToBuffer(write)
if not success then
print(ret)

break
elseif ret == false then
--if func(netchan, read, write) == false then
Msg("Failed to filter message " .. msg .. "\n")

write:Seek(totalbits)

break
MsgC(Color(255, 0, 0), "Failed to write message " .. handler:GetName() .. " 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--]]
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

MsgC(Color(0, 255, 0), "Fully parsed stream with " .. totalbits .. " bit(s) written\n")
return true
end)

function FilterIncomingMessage(msg, func)
local handler = NET_MESSAGES[msg]

if not handler then
--[[local handler = NET_MESSAGES[msg]
if handler == nil then
if CLIENT then
handler = NET_MESSAGES.SVC[msg]
else
handler = NET_MESSAGES.CLC[msg]
end
end
if handler then
if handler ~= nil then
handler.IncomingCopy = func
end
end]]
end

function UnFilterIncomingMessage(msg)
Expand Down
Loading

0 comments on commit 5955110

Please sign in to comment.