Skip to content

Commit

Permalink
Make errors use creationIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
thegrb93 committed Aug 18, 2024
1 parent af8d6e7 commit e021bd4
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 88 deletions.
39 changes: 10 additions & 29 deletions lua/entities/starfall_processor/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,43 +151,24 @@ end)

net.Receive("starfall_processor_download", function(len)
net.ReadStarfall(nil, function(ok, sfdata, err)
local proc, owner
local function setup()
if not IsValid(proc) then return end
if not (IsValid(owner) or IsWorld(owner)) then return end
sfdata.proc = proc
sfdata.owner = owner
proc.owner = owner
if ok then
proc:SetupFiles(sfdata)
else
proc:Error({message = "Failed to download and initialize client: " .. tostring(err), traceback = "" })
end
end

if sfdata.ownerindex == 0 then
owner = game.GetWorld()
else
SF.WaitForEntity(sfdata.ownerindex, sfdata.ownercreateindex, function(e) owner = e setup() end)
if ok then
sfdata.proc:SetupFiles(sfdata)
elseif IsValid(sfdata.proc) and IsValid(sfdata.owner) then
sfdata.proc.owner = sfdata.owner
sfdata.proc:Error({message = "Failed to download and initialize client: " .. tostring(err), traceback = "" })
end
SF.WaitForEntity(sfdata.procindex, sfdata.proccreateindex, function(e) proc = e setup() end)
end)
end)

net.Receive("starfall_processor_link", function()
local componenti, componentci = net.ReadUInt(16), net.ReadUInt(32)
local proci, procci = net.ReadUInt(16), net.ReadUInt(32)
local component, proc
local function link()
if not IsValid(component) then return end
if not (IsValid(proc) or proci==0) then return end
SF.LinkEnt(component, proc)
end

SF.WaitForEntity(componenti, componentci, function(e) component = e link() end)
if proci~=0 then
SF.WaitForEntity(proci, procci, function(e) proc = e link() end)
if component and proc then
SF.LinkEnt(component, proc)
end
end
net.ReadReliableEntity(function(e) component=e link() end)
net.ReadReliableEntity(function(e) proc=e link() end)
end)

net.Receive("starfall_processor_kill", function()
Expand Down
3 changes: 1 addition & 2 deletions lua/entities/starfall_processor/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ net.Receive("starfall_processor_download", function(len, ply)
end)

net.Receive("starfall_processor_link", function(len, ply)
local entIndex = net.ReadUInt(16)
local linked = Entity(entIndex)
local linked = Entity(net.ReadUInt(16))
if IsValid(linked.link) then
SF.LinkEnt(linked, linked.link, ply)
end
Expand Down
9 changes: 3 additions & 6 deletions lua/entities/starfall_processor/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,11 @@ function SF.LinkEnt(self, ent, transmit)
end
if SERVER and (changed or transmit) then
net.Start("starfall_processor_link")
net.WriteUInt(self:EntIndex(), 16)
net.WriteUInt(self:GetCreationID(), 32)
net.WriteReliableEntity(self)
if IsValid(ent) then
net.WriteUInt(ent:EntIndex(), 16)
net.WriteUInt(ent:GetCreationID(), 32)
net.WriteReliableEntity(ent)
else
net.WriteUInt(0, 16)
net.WriteUInt(0, 32)
net.WriteReliableEntity(Entity(0))
end
if transmit then net.Send(transmit) else net.Broadcast() end
end
Expand Down
4 changes: 1 addition & 3 deletions lua/entities/starfall_prop/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ function ENT:GetRenderMesh()
end

net.Receive("starfall_custom_prop", function()
local index = net.ReadUInt(16)
local creationindex = net.ReadUInt(32)
local self, data

local function applyData()
Expand Down Expand Up @@ -104,7 +102,7 @@ net.Receive("starfall_custom_prop", function()
self.rendermeshloaded = true
end

SF.WaitForEntity(index, creationindex, function(e)
net.ReadReliableEntity(function(e)
if e and e:GetClass()=="starfall_prop" then
self = e
applyData()
Expand Down
3 changes: 1 addition & 2 deletions lua/entities/starfall_prop/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ end

function ENT:TransmitData(recip)
net.Start("starfall_custom_prop")
net.WriteUInt(self:EntIndex(), 16)
net.WriteUInt(self:GetCreationID(), 32)
net.WriteReliableEntity(self)
local stream = net.WriteStream(self.streamdata, nil, true)
if recip then net.Send(recip) else net.Broadcast() end
return stream
Expand Down
104 changes: 58 additions & 46 deletions lua/starfall/transfer.lua
Original file line number Diff line number Diff line change
@@ -1,43 +1,55 @@

local IsValid = FindMetaTable("Entity").IsValid

function net.WriteReliableEntity(ent)
net.WriteUInt(ent:EntIndex(), 16)
net.WriteUInt(ent:GetCreationID(), 32)
end

function net.ReadReliableEntity(cb)
SF.WaitForEntity(net.ReadUInt(16), net.ReadUInt(32), cb)
end

-- Net extension stuff
function net.ReadStarfall(ply, callback)
local sfdata = {files = {}}
local callbacks = 0
local error
local sfdata = {}

local function setup()
callbacks = callbacks + 1
if callbacks<3 then return end
if error then callback(false, sfdata, error) return end
callback(true, sfdata)
end
local function setupProc(e)
if e==nil then error="Invalid starfall processor entity" end
sfdata.proc=e setup()
end
local function setupOwner(e)
if e==nil then error="Invalid starfall owner entity" end
sfdata.owner=e setup()
end
local function setupFiles(data)
if data==nil then error="Net timeout" setup() return end
local ok, decompress = pcall(SF.DecompressFiles, data)
if not ok then error=decompress setup() return end
sfdata.files=decompress setup()
end

if CLIENT then
sfdata.procindex = net.ReadUInt(16)
sfdata.proccreateindex = net.ReadUInt(32)
sfdata.proc = Entity(sfdata.procindex)
sfdata.ownerindex = net.ReadUInt(16)
sfdata.ownercreateindex = net.ReadUInt(32)
sfdata.owner = Entity(sfdata.ownerindex)
net.ReadReliableEntity(setupProc)
net.ReadReliableEntity(setupOwner)
end
sfdata.mainfile = net.ReadString()

net.ReadStream(ply, function(data)
if data then
local ok, files = pcall(SF.DecompressFiles, data)
if ok then
sfdata.files = files
callback(true, sfdata)
else
callback(false, sfdata, files)
end
else
callback(false, sfdata, "Net timeout")
end
end)

return sfdata
net.ReadStream(ply, setupFiles)
end

function net.WriteStarfall(sfdata, callback)
if #sfdata.mainfile > 255 then error("Main file name too large: " .. #sfdata.mainfile .. " (max is 255)") end
if SERVER then
net.WriteUInt(sfdata.proc:EntIndex(), 16)
net.WriteUInt(sfdata.proc:GetCreationID(), 32)
net.WriteUInt(sfdata.owner:EntIndex(), 16)
net.WriteUInt(sfdata.owner:GetCreationID(), 32)
net.WriteReliableEntity(sfdata.proc)
net.WriteReliableEntity(sfdata.owner)
end
net.WriteString(sfdata.mainfile)

Expand Down Expand Up @@ -119,14 +131,13 @@ if SERVER then
-- The chip owner gets more data
if client~=chip.owner then
net.Start("starfall_error")
net.WriteEntity(chip)
net.WriteEntity(chip.owner)
net.WriteReliableEntity(chip)
net.WriteString(string.sub(chip.sfdata.mainfile, 1, 1024))
net.WriteString(string.sub(message, 1, 1024))
net.WriteString(string.sub(traceback, 1, 1024))
if client~=nil and should_notify~=nil then
net.WriteBool(true)
net.WriteEntity(client)
net.WriteReliableEntity(client)
net.WriteBool(should_notify)
else
net.WriteBool(false)
Expand All @@ -135,14 +146,13 @@ if SERVER then
end

net.Start("starfall_error")
net.WriteEntity(chip)
net.WriteEntity(chip.owner)
net.WriteReliableEntity(chip)
net.WriteString(string.sub(chip.sfdata.mainfile, 1, 128))
net.WriteString(string.sub(message, 1, 128))
net.WriteString("")
if client~=nil and should_notify~=nil then
net.WriteBool(true)
net.WriteEntity(client)
net.WriteReliableEntity(client)
net.WriteBool(should_notify)
net.SendOmit({client, chip.owner})
else
Expand All @@ -152,7 +162,7 @@ if SERVER then
end

net.Receive("starfall_error", function(_, ply)
local chip = net.ReadEntity()
local chip = Entity(net.ReadUInt(16))
if not IsValid(chip) then return end
if not chip.ErroredPlayers or chip.ErroredPlayers[ply] then return end
chip.ErroredPlayers[ply] = true
Expand Down Expand Up @@ -224,28 +234,30 @@ else
is_blocked = SF.BlockedUsers:isBlocked(owner:SteamID())
end
net.Start("starfall_error")
net.WriteEntity(chip)
net.WriteUInt(chip:EntIndex(), 16)
net.WriteString(string.sub(message, 1, 1024))
net.WriteString(string.sub(traceback, 1, 1024))
net.WriteBool(GetConVarNumber("sf_timebuffer_cl") > 0 and not is_blocked)
net.SendToServer()
end

net.Receive("starfall_error", function()
local chip = net.ReadEntity()
if not IsValid(chip) then return end
local owner = net.ReadEntity()
if not IsValid(owner) then return end
local mainfile = net.ReadString()
local message = net.ReadString()
local traceback = net.ReadString()
local client, should_notify
local chip, client, mainfile, message, traceback, should_notify

local function doError()
if chip and (client or should_notify==nil) then
hook.Run("StarfallError", chip, chip.owner, client, mainfile, message, traceback, should_notify)
end
end

net.ReadReliableEntity(function(e) chip=e doError() end)
mainfile = net.ReadString()
message = net.ReadString()
traceback = net.ReadString()
if net.ReadBool() then
client = net.ReadEntity()
if not IsValid(client) then return end
net.ReadReliableEntity(function(e) client=e doError() end)
should_notify = net.ReadBool()
end
hook.Run("StarfallError", chip, owner, client, mainfile, message, traceback, should_notify)
end)

net.Receive("starfall_upload", function()
Expand Down

0 comments on commit e021bd4

Please sign in to comment.