Skip to content

Instantly share code, notes, and snippets.

@vega0
Created November 5, 2019 16:00
Show Gist options
  • Save vega0/eb917186db0acdf4728b61b1f2a9f5a5 to your computer and use it in GitHub Desktop.
Save vega0/eb917186db0acdf4728b61b1f2a9f5a5 to your computer and use it in GitHub Desktop.
local DATA_STEP = 0xffff - 100
if CLIENT then
local function get_screenshot_data(on_data_extract)
hook.Add("PostRender", "dataextraction", function()
hook.Remove("PostRender", "dataextraction")
local data = render.Capture
{format = "jpeg", quality = 70,h = ScrH(), w = ScrW(),x = 0, y = 0}
on_data_extract (data)
end)
end
local function send_image_data(data, ply)
data = util.Compress (data)
for i = 1, #data - 1, DATA_STEP do
local part = data:sub(i, i + DATA_STEP - 1)
net.Start "GetClientScreenshot"
net.WriteEntity(ply)
net.WriteUInt(#part, 32)
net.WriteData(part, #part)
net.SendToServer()
end
end
if LocalPlayer():IsAdmin() then
local DATA = [[]]
net.Receive("GetClientScreenshot", function()
local datapart = net.ReadData(net.ReadUInt(32))
DATA = DATA .. datapart
if #datapart == DATA_STEP then return end
DATA = util.Decompress (DATA)
-- opening image data.
local frame = vgui.Create "DFrame"
frame:SetSize(ScrW(), ScrH())
frame:Center()
frame:MakePopup()
local html = frame:Add "DHTML"
html:Dock(FILL)
html:SetHTML("<img src=\"data:image/jpeg;base64,"
.. util.Base64Encode(DATA) .. "\"/>")
DATA = ""
end)
end
usermessage.Hook("extract_client_image", function(um)
local ply = um:ReadEntity()
get_screenshot_data(function(data) send_image_data(data, ply) end)
end)
else
util.AddNetworkString "GetClientScreenshot"
net.Receive("GetClientScreenshot", function(_, ply)
local subject = net.ReadEntity()
local length = net.ReadUInt(32)
local data = net.ReadData(length)
net.Start "GetClientScreenshot"
net.WriteUInt(length, 32)
net.WriteData(data, length)
net.Send(subject)
end)
concommand.Add ("sv_screenshot_get_all", function(ply)
if not ply:IsAdmin() then return end
umsg.Start("extract_client_image")
umsg.Entity(ply)
umsg.End()
end)
concommand.Add ("sv_screenshot_get", function(ply, _, args)
if not ply:IsAdmin() then return end
local p
for k, v in pairs(player.GetAll()) do
if v:Nick():find(args[1]) then
p = v
break
end
end
if not IsValid(p) then
return ply:ChatPrint("Invalid player!")
end
ply:ChatPrint("Getting screenshot from player: " .. tostring(p))
umsg.Start("extract_client_image", p)
umsg.Entity(ply)
umsg.End()
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment