Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lua/advdupe2/sv_file.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
--Save a file to the client
local function SaveFile(ply, cmd, args)
if(not ply.AdvDupe2 or not ply.AdvDupe2.Entities or next(ply.AdvDupe2.Entities)==nil)then AdvDupe2.Notify(ply,"Duplicator is empty, nothing to save.", NOTIFY_ERROR) return end
if(not game.SinglePlayer() and CurTime()-(ply.AdvDupe2.FileMod or 0) < 0)then

Check warning on line 4 in lua/advdupe2/sv_file.lua

View workflow job for this annotation

GitHub Actions / lint

"Syntax inconsistency"

Inconsistent use of '||' and 'or'
AdvDupe2.Notify(ply,"Cannot save at the moment. Please Wait...", NOTIFY_ERROR)
return
end

if(ply.AdvDupe2.Pasting || ply.AdvDupe2.Downloading)then

Check warning on line 9 in lua/advdupe2/sv_file.lua

View workflow job for this annotation

GitHub Actions / lint

"Syntax inconsistency"

Inconsistent use of '||' and 'or'
AdvDupe2.Notify(ply,"Advanced Duplicator 2 is busy.",NOTIFY_ERROR)
return false
end

ply.AdvDupe2.FileMod = CurTime()+tonumber(GetConVarString("AdvDupe2_FileModificationDelay")+2)

Check warning on line 14 in lua/advdupe2/sv_file.lua

View workflow job for this annotation

GitHub Actions / lint

"Deprecated"

Deprecated: Use ConVar objects instead

local name = string.Explode("/", args[1])
ply.AdvDupe2.Name = name[#name]
Expand Down Expand Up @@ -49,7 +49,7 @@
net.WriteBool(autosave)
net.WriteStream(data, function()
ply.AdvDupe2.Downloading = false
end)
end, nil, {ply})
net.Send(ply)
end

Expand All @@ -62,8 +62,8 @@
end

if(not game.SinglePlayer())then
if(tonumber(GetConVarString("AdvDupe2_MaxConstraints"))~=0 and #dupe["Constraints"]>tonumber(GetConVarString("AdvDupe2_MaxConstraints")))then

Check warning on line 65 in lua/advdupe2/sv_file.lua

View workflow job for this annotation

GitHub Actions / lint

"Deprecated"

Deprecated: Use ConVar objects instead

Check warning on line 65 in lua/advdupe2/sv_file.lua

View workflow job for this annotation

GitHub Actions / lint

"Deprecated"

Deprecated: Use ConVar objects instead
AdvDupe2.Notify(ply,"Amount of constraints is greater than "..GetConVarString("AdvDupe2_MaxConstraints"),NOTIFY_ERROR)

Check warning on line 66 in lua/advdupe2/sv_file.lua

View workflow job for this annotation

GitHub Actions / lint

"Deprecated"

Deprecated: Use ConVar objects instead
return false
end
end
Expand Down
76 changes: 62 additions & 14 deletions lua/autorun/netstream.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@
timer.Remove("netstream_queueclean")
end
end,

CleanStream = function(self, cleanStream)
for k, stream in pairs(self.queue) do
if stream == cleanStream then
stream:Remove()
self.queue[k] = nil
break
end
end
if next(self.queue)==nil then
timer.Remove("netstream_queueclean")
end
end,
},
__call = function(t)
return setmetatable({
Expand All @@ -87,7 +100,7 @@
ErrorNoHalt("Receiving too many ReadStream requests!")
return
end

Check warning on line 103 in lua/autorun/netstream.lua

View workflow job for this annotation

GitHub Actions / lint

"Trailing whitespace"

Trailing whitespace
for _, v in ipairs(queue) do
if v.identifier == stream.identifier then
ErrorNoHalt("Tried to start a new ReadStream for an already existing stream!")
Expand Down Expand Up @@ -146,7 +159,7 @@
__index = {
Write = function(self, ply, chunkidx)
local client = self.clients[ply]
if client.finished then return false end
if client==nil or client.finished then return false end
if chunkidx == #self.chunks+1 then self:Finished(ply) return true end

if client.downloads+#self.chunks-client.progress >= net.Stream.MaxTries * #self.chunks then self:Finished(ply) return false end
Expand All @@ -170,14 +183,24 @@

Finished = function(self, ply)
self.clients[ply].finished = true
if self.protected then
local readyToClean = true
for _, client in pairs(self.clients) do
if not client.finished then readyToClean = false break end
end
if readyToClean then
net.Stream.WriteStreams:CleanStream(self)
end
end
if self.callback then
local ok, err = xpcall(self.callback, debug.traceback, ply)
if not ok then ErrorNoHalt(err) end
end
end,

GetProgress = function(self, ply)
return self.clients[ply].progress / #self.chunks
local client = self.clients[ply]
return client and (client.progress / #self.chunks) or 0
end,

Remove = function(self)
Expand All @@ -196,28 +219,45 @@
net.WriteUInt(0, 32)
if SERVER then net.Send(sendTo) else net.SendToServer() end
end
end
end,

},
__call = function(t, data, callback)
__call = function(t, data, callback, recipients)
local chunks = {}
for i=1, math.ceil(#data / net.Stream.SendSize) do
local datachunk = string.sub(data, (i - 1) * net.Stream.SendSize + 1, i * net.Stream.SendSize)
chunks[i] = { data = datachunk, crc = util.CRC(datachunk) }
end

return setmetatable({
timeout = CurTime()+net.Stream.Timeout,
chunks = chunks,
callback = callback,
lasttouched = 0,
local clients, protected
if recipients then
clients = {}
for k, v in ipairs(recipients) do
clients[v] = {
finished = false,
downloads = 0,
progress = 0
}
end
protected = true
else
clients = setmetatable({},{__index = function(t,k)
local r = {
finished = false,
downloads = 0,
progress = 0,
} t[k]=r return r
end})
protected = false
end

return setmetatable({
timeout = CurTime()+net.Stream.Timeout,
chunks = chunks,
callback = callback,
lasttouched = 0,
clients = clients,
protected = protected,
}, t)
end
}
Expand Down Expand Up @@ -297,7 +337,7 @@
setmetatable(ReadingDataItem, ReadingDataItem)


function net.WriteStream(data, callback, dontcompress)
function net.WriteStream(data, callback, dontcompress, recipients)
if not isstring(data) then
error("bad argument #1 to 'WriteStream' (string expected, got " .. type(data) .. ")", 2)
end
Expand All @@ -309,6 +349,14 @@
if compressed then
data = util.Compress(data) or ""
end
if recipients ~= nil then
if not istable(recipients) then error("bad argument #4 to 'WriteStream' (table expected, got " .. type(recipients) .. ")", 2) end
if CLIENT then error("recipients argument can't be used in clientside!", 2) end
if #recipients==0 then error("recipients argument is empty!", 2) end
for k, v in ipairs(recipients) do
if not v:IsPlayer() then error("recipients contains non-player entry in key ("..k..")!", 2) end
end
end

if #data == 0 then
net.WriteUInt(0, 32)
Expand All @@ -321,9 +369,9 @@
return
end

local stream = net.Stream.WriteStreams:Add(WritingDataItem(data, callback, compressed))
local stream = net.Stream.WriteStreams:Add(WritingDataItem(data, callback, compressed, recipients))
if not stream then return end

Check warning on line 374 in lua/autorun/netstream.lua

View workflow job for this annotation

GitHub Actions / lint

"Trailing whitespace"

Trailing whitespace
--print("WriteStream", #stream.chunks, stream.identifier, compressed)
net.WriteUInt(#stream.chunks, 32)
net.WriteUInt(stream.identifier, 32)
Expand All @@ -347,7 +395,7 @@
if not isfunction(callback) then
error("bad argument #2 to 'ReadStream' (function expected, got " .. type(callback) .. ")", 2)
end

Check warning on line 398 in lua/autorun/netstream.lua

View workflow job for this annotation

GitHub Actions / lint

"Trailing whitespace"

Trailing whitespace
local numchunks = net.ReadUInt(32)
if numchunks == nil then
return
Expand Down
Loading