hoobonceagain

wolven sg

Sep 12th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.31 KB | None | 0 0
  1. --[[-------------------------------------------------------------------------
  2. Client code | Local copies
  3. ---------------------------------------------------------------------------]]
  4. if not CLIENT then return end
  5. -- Hooks
  6. local hook_Add = hook.Add
  7. local hook_Remove = hook.Remove
  8. local hook_GetTable = hook.GetTable
  9. local hook_Run = hook.Run
  10. local hook_Call = hook.Call
  11.  
  12. -- Net
  13. local net_Receive = net.Receive
  14. local net_Start = net.Start
  15. local net_SendToServer = net.SendToServer
  16. local net_Incoming = net.Incoming
  17.  
  18. local net_WriteData = net.WriteData
  19. local net_WriteEntity = net.WriteEntity
  20. local net_WriteFloat = net.WriteFloat
  21. local net_WriteUInt = net.WriteUInt
  22. local net_WriteColor = net.WriteColor
  23. local net_WriteString = net.WriteString
  24. local net_WriteInt = net.WriteInt
  25. local net_WriteBool = net.WriteBool
  26.  
  27. local net_ReadUInt = net.ReadUInt
  28. local net_ReadEntity = net.ReadEntity
  29.  
  30. local net_ReadHeader = net.ReadHeader
  31. local util_NetworkIDToString = util.NetworkIDToString
  32.  
  33. -- Math
  34. local os_time = os.time
  35. local math_ceil = math.ceil
  36. local math_min = math.min
  37. local math_random = math.random
  38. local math_randomseed = math.randomseed
  39.  
  40. -- String
  41. local string_sub = string.sub
  42. local string_len = string.len
  43.  
  44. -- Render
  45. local render_Capture = render.Capture
  46.  
  47. -- Compression/encoding
  48. local util_Compress = util.Compress
  49. local util_Base64Encode = util.Base64Encode
  50.  
  51. -- Debug
  52. local debug_getinfo = debug.getinfo
  53. local CurTime = CurTime
  54. local ScrH = ScrH
  55. local ScrW = ScrW
  56.  
  57. -- Timer
  58. local timer_Simple = timer.Simple
  59.  
  60. -- Tables
  61. local table_Copy = table.Copy
  62.  
  63. -- Render
  64. local render_RenderView = render.RenderView
  65. local render_RenderHUD = render.RenderHUD
  66.  
  67. --[[-------------------------------------------------------------------------
  68. Shared code
  69. ---------------------------------------------------------------------------]]
  70. if not CLIENT then return end
  71.  
  72. -- Net
  73. local REQUESTSCREENSHOT = "RequestScreenshot" --RequestScreenshot
  74. local SENDREQUESTTOVICTIM = "SendRequestToVictim" --SendRequestToVictim
  75.  
  76. local SENDPACKETTOSERVER = "SendPacketToServer" --SendPacketToServer
  77. local SENDPACKETTOADMIN = "SendPacketToAdmin" --SendPacketToAdmin
  78. local SENDCAPTUREINTO = "SendCaptureInfo" -- SendCaptureInfo
  79.  
  80. -- Used to determin the size of each packet used for sending screengrab data
  81. local blockLength = 20000
  82.  
  83. local canScreengrab = function(ply) return ply:IsSuperAdmin() end
  84.  
  85. local function breakupScreengrab(compressedData)
  86. -- Divide up into smaller packets for sending
  87. local compressedLength = string_len(compressedData)
  88. local blockCount = math_ceil(compressedLength / blockLength)
  89.  
  90. -- Create the smaller packets
  91. local blocks = {}
  92. for i=1, blockCount do
  93. blocks[i] = string_sub(compressedData, i*blockLength - blockLength + 1, math_min(i*blockLength, compressedLength))
  94. end
  95.  
  96. return blocks
  97. end
  98.  
  99. local function sendScreengrab(ply, dataTable)
  100. local parts = #dataTable
  101. local current = 1
  102.  
  103. -- Tell the client about what we're sending them
  104. if SERVER then
  105. local captureSize = 0
  106. for i=1, parts do
  107. captureSize = captureSize + #dataTable[i]
  108. end
  109. net.Start(SENDCAPTUREINTO)
  110. net.WriteUInt(captureSize, 32)
  111. net.Send(ply)
  112. end
  113.  
  114. -- Delay send the data
  115. local function delay()
  116. -- Send a block of the screen grab data
  117. net_Start(CLIENT and SENDPACKETTOSERVER or SENDPACKETTOADMIN)
  118. net_WriteInt(#dataTable[current], 32)
  119. net_WriteData(dataTable[current], #dataTable[current])
  120. net_WriteBool(current==parts)
  121. if CLIENT then net_SendToServer() else net_Send(ply) end
  122.  
  123. -- Get ready to send the next block
  124. if current < parts then
  125. timer_Simple(0.1, delay)
  126. end
  127. current = current + 1
  128. end
  129. delay()
  130. end
  131.  
  132. --[[-------------------------------------------------------------------------
  133. Variables
  134. ---------------------------------------------------------------------------]]
  135.  
  136. local filePath = debug_getinfo(1).short_src -- Current file path
  137. local sendInProgress = ""
  138.  
  139. -- Victim vars
  140. local victimSendingScreenshot = false
  141. local victimshouldScreengrab = false
  142. local victimCaptureQuaility = 50
  143.  
  144. -- Local colours
  145. local sg = {}
  146. sg.white = color_white
  147. sg.black = color_black
  148. sg.red = Color( 255, 0, 0 )
  149. sg.green = Color( 0, 255, 0 )
  150. sg.orange = Color( 255, 100, 0 )
  151. sg.yellow = Color( 255, 255, 0 )
  152. sg.blue = Color( 0, 200, 255 )
  153.  
  154. -- Create fonts
  155. surface.CreateFont( "rtfont2", {
  156. font = "Lucida Console",
  157. size = 13,
  158. antialias = true
  159. } )
  160. surface.CreateFont( "asdf2", {
  161. font = "Lucida Console",
  162. size = 15,
  163. antialias = true
  164. } )
  165. surface.CreateFont( "topmenu2", {
  166. font = "Lucida Console",
  167. size = 15,
  168. antialias = true
  169. } )
  170.  
  171. CreateClientConVar( "sg_auto_open", "0" )
  172.  
  173. math_randomseed(os_time()) -- Seed the random
  174.  
  175. --[[-------------------------------------------------------------------------
  176. Protected
  177. ---------------------------------------------------------------------------]]
  178.  
  179. -- Hooks, [eventName][identifier]
  180. local protectedHooks = {}
  181.  
  182. -- Net, [name] = func or true if only used for net.Start
  183. local protectedNet = {}
  184. protectedNet[SENDPACKETTOSERVER] = true
  185.  
  186. --[[-------------------------------------------------------------------------
  187. Detour hook library (c+p galore)
  188. Protects protected stuff
  189. ---------------------------------------------------------------------------]]
  190.  
  191. function hook.Add(...)
  192. local args = {...}
  193. local eventName = args[1]
  194. local identifier = args[2]
  195.  
  196. -- If protected
  197. if protectedHooks[eventName] then
  198. if protectedHooks[eventName][identifier] then
  199. -- If foreign source, block it
  200. local debugInfo = debug_getinfo(2)
  201. if debugInfo.short_src != filePath then
  202. return
  203. end
  204. end
  205. end
  206.  
  207. -- All is well, return the original
  208. return hook_Add(...)
  209. end
  210.  
  211. function hook.Remove(eventName, identifier)
  212. -- If protected
  213. if protectedHooks[eventName] then
  214. if protectedHooks[eventName][identifier] then
  215. -- If foreign source, block it
  216. local debugInfo = debug_getinfo(2)
  217. if debugInfo.short_src != filePath then
  218. return
  219. end
  220. end
  221. end
  222.  
  223. -- All is well, return the original
  224. return hook_Remove(eventName, identifier)
  225. end
  226.  
  227. function hook.GetTable()
  228. local copy = table_Copy(hook_GetTable())
  229.  
  230. -- No need to hide the names of our protected hooks,
  231. -- just make sure they cannot edit the hook table.
  232. -- So pass a local copy instead.
  233. return copy
  234. end
  235.  
  236. function hook.Run(...)
  237. local args = {...}
  238. local eventName = args[1]
  239. local identifier = args[2]
  240.  
  241. -- If protected
  242. if protectedHooks[eventName] then
  243. if protectedHooks[eventName][identifier] then
  244. -- If foreign source, block it
  245. local debugInfo = debug_getinfo(2)
  246. if debugInfo.short_src != filePath then
  247. return
  248. end
  249. end
  250. end
  251.  
  252. -- All is well, return the original
  253. return hook_Run(...)
  254. end
  255.  
  256. function hook.Call(...)
  257. local args = {...}
  258. local eventName = args[1]
  259. local identifier = args[2]
  260.  
  261. -- If protected
  262. if protectedHooks[eventName] then
  263. if protectedHooks[eventName][identifier] then
  264. -- If foreign source, block it
  265. local debugInfo = debug_getinfo(2)
  266. if debugInfo.short_src != filePath then
  267. return
  268. end
  269. end
  270. end
  271.  
  272. -- All is well, return the original
  273. return hook_Call(...)
  274. end
  275.  
  276. --[[-------------------------------------------------------------------------
  277. Detour net library
  278. Protects protected stuff
  279. ---------------------------------------------------------------------------]]
  280.  
  281. -- Stop foreign sources from creating receivers for protected net messages
  282. function net.Receive(netMessage, func)
  283. if protectedNet[netMessage] then
  284. -- If foreign source, block it
  285. local debugInfo = debug_getinfo(2)
  286. if debugInfo.short_src != filePath then
  287. -- Add ours back incase
  288. func = protectedNet[netMessage]
  289. end
  290. end
  291.  
  292. -- All is well, return the original, or our replacement
  293. return net_Receive(netMessage, func)
  294. end
  295.  
  296. -- Keep a copy of the current message being constructed
  297. function net.Start(netMessage)
  298. sendInProgress = netMessage
  299. return net_Start(netMessage)
  300. end
  301.  
  302. -- Block any attempts to send protected net messages from a foreign source
  303. function net.SendToServer()
  304. if protectedNet[sendInProgress] then
  305. -- If foreign source, block it
  306. local debugInfo = debug_getinfo(2)
  307. if debugInfo.short_src != filePath then
  308. -- Restart screen grab, if we haven't already done so
  309. if not isSendingScreenshot then
  310. protectedNet[SENDREQUESTTOVICTIM]() -- Run StartScreengrab
  311. end
  312. return
  313. end
  314. end
  315. return net_SendToServer()
  316. end
  317.  
  318. --[[-------------------------------------------------------------------------
  319. Screen grab code
  320. ---------------------------------------------------------------------------]]
  321.  
  322. --[[
  323. Screen grab code | victim
  324. ]]--
  325.  
  326. local currentGrab = {}
  327. local function resetCurrent()
  328. currentGrab.playerNick = ""
  329. currentGrab.totalParts = 0
  330. currentGrab.startTime = 0
  331. currentGrab.endTime = 0
  332. currentGrab.data = {}
  333. currentGrab.quality = 50
  334. sg.screenshot = false
  335. end
  336. resetCurrent()
  337.  
  338. -- Preps the victim for screengrabbing
  339. function sg.startScreengrab()
  340. render_RenderView()
  341. render_RenderHUD(0,0,ScrW(),ScrH())
  342. isSendingScreenshot = true
  343. shouldScreengrab = true
  344. currentGrab.quality = net_ReadUInt(16)
  345. end
  346. net_Receive(SENDREQUESTTOVICTIM, sg.startScreengrab)
  347. protectedNet[SENDREQUESTTOVICTIM] = sg.startScreengrab
  348.  
  349. -- Handles screengrabs
  350. function sg.hookScreengrab()
  351. if (!shouldScreengrab) then return end
  352. shouldScreengrab = false
  353.  
  354. -- Capture the screen
  355. local img = render_Capture({
  356. format = "jpeg",
  357. h = ScrH(),
  358. w = ScrW(),
  359. quality = currentGrab.quality,
  360. x = 0,
  361. y = 0
  362. })
  363.  
  364. -- Compress
  365. local compressed = util_Compress(util_Base64Encode(img))
  366.  
  367. -- Break up the screengrab
  368. local blocks = breakupScreengrab(compressed)
  369.  
  370. -- Send the screengrab
  371. sendScreengrab(nil, blocks)
  372. end
  373. hook_Add("PostRender", "Screengrab",sg.hookScreengrab)
  374. protectedHooks["PostRender"] = protectedHooks["PostRender"] or {}
  375. protectedHooks["PostRender"]["Screengrab"] = sg.hookScreengrab
  376.  
  377. --[[
  378. Screen grab code | admin
  379. ]]--
  380.  
  381. local function DisplayData( str, name )
  382. local elapsedtime
  383. if not name then
  384. elapsedtime = math.Round( currentGrab.endTime - currentGrab.startTime, 3 )
  385. end
  386. local main = vgui.Create( "DFrame", vgui.GetWorldPanel() )
  387. main:SetPos( 0, 0 )
  388. main:SetSize( ScrW(), ScrH() )
  389. if not name then
  390. main:SetTitle( "Screengrab of " .. currentGrab.playerNick .. " (" .. string.len( str ) .. " bytes, took " .. elapsedtime .. " seconds)" )
  391. else
  392. local str = name:sub( 1, -5 )
  393. main:SetTitle( str )
  394. end
  395. main:MakePopup()
  396. local html = vgui.Create( "HTML", main )
  397. html:DockMargin( 0, 0, 0, 0 )
  398. html:Dock( FILL )
  399. html:SetHTML( [[ <img width="]] .. ScrW() .. [[" height="]] .. ScrH() .. [[" src="data:image/jpeg;base64, ]] .. str .. [["/> ]] )
  400. end
  401.  
  402. --[[
  403. Screen grab code | GUI/derma
  404. ]]--
  405.  
  406. local function appendServerLog(color, text)
  407. if sg.serverLog:IsValid() and sg.serverLog:IsVisible() then
  408. if type( color ) == "string" then
  409. sg.serverLog:AppendText( color .. "\n" )
  410. return
  411. end
  412. if IsValid( sg.serverLog ) then
  413. sg.serverLog:InsertColorChange( color.r, color.g, color.b, color.a or 255 )
  414. sg.serverLog:AppendText( text .. "\n" )
  415. sg.serverLog:InsertColorChange( 255, 255, 255, 255 )
  416. end
  417. end
  418. end
  419.  
  420. local function appendClientLog(color, text)
  421. if sg.clientLog:IsValid() and sg.clientLog:IsVisible() then
  422. sg.clientLog:InsertColorChange( color.r, color.g, color.b, color.a or 255 )
  423. sg.clientLog:AppendText( text .. "\n" )
  424. sg.clientLog:InsertColorChange( 255, 255, 255, 255 )
  425. end
  426. end
  427.  
  428. local function OpenSGMenu()
  429.  
  430. if sg.main then
  431. return
  432. end
  433.  
  434. -- Main frame
  435. sg.main = vgui.Create( "DFrame" )
  436. sg.main:SetSize( 635, 300 )
  437. sg.main:SetTitle( "" )
  438. sg.main:SetVisible( true )
  439. sg.main:ShowCloseButton( true )
  440. sg.main:MakePopup()
  441. sg.main:Center()
  442. sg.main.btnMaxim:Hide()
  443. sg.main.btnMinim:Hide()
  444. sg.main.btnClose:Hide()
  445. sg.main.Paint = function(self)
  446. surface.SetDrawColor( 50, 50, 50, 135 )
  447. surface.DrawOutlinedRect( 0, 0, self:GetWide(), self:GetTall() )
  448. surface.SetDrawColor( 0, 0, 0, 240 )
  449. surface.DrawRect( 1, 1, self:GetWide() - 2, self:GetTall() - 2 )
  450. surface.SetFont( "topmenu2" )
  451. surface.SetTextPos( self:GetWide() / 2 - surface.GetTextSize( "Screengrab Menu" ) / 2, 5 )
  452. surface.SetTextColor( 255, 255, 255, 255 )
  453. surface.DrawText( "Screengrab Menu" )
  454. end
  455.  
  456. -- Close button
  457. local close = vgui.Create( "DButton", sg.main )
  458. close:SetPos( sg.main:GetWide() - 50, 0 )
  459. close:SetSize( 44, 22 )
  460. close:SetText( "" )
  461.  
  462. -- Close button painting
  463. local colorv = Color( 150, 150, 150, 250 )
  464. local function PaintClose()
  465. if not sg.main then
  466. return
  467. end
  468. surface.SetDrawColor( colorv )
  469. surface.DrawRect( 1, 1, close:GetWide() - 2, close:GetTall() - 2 )
  470. surface.SetFont( "asdf2" )
  471. surface.SetTextColor( 255, 255, 255, 255 )
  472. surface.SetTextPos( 19, 3 )
  473. surface.DrawText( "x" )
  474. return true
  475. end
  476.  
  477. -- Painting close button
  478. close.Paint = PaintClose
  479. close.OnCursorEntered = function()
  480. colorv = Color( 195, 75, 0, 250 )
  481. PaintClose()
  482. end
  483. close.OnCursorExited = function()
  484. colorv = Color( 150, 150, 150, 250 )
  485. PaintClose()
  486. end
  487. close.OnMousePressed = function()
  488. colorv = Color( 170, 0, 0, 250 )
  489. PaintClose()
  490. end
  491. -- Close the main frame on close button released
  492. close.OnMouseReleased = function()
  493. --if not LocalPlayer().InProgress then
  494. sg.main:Close()
  495. --end
  496. end
  497. sg.main.OnClose = function()
  498. sg.main:Remove()
  499. if sg.main then
  500. sg.main = nil
  501. end
  502. end
  503.  
  504. -- Padding?
  505. local inside = vgui.Create( "DPanel", sg.main )
  506. inside:SetPos( 7, 27 )
  507. inside:SetSize( sg.main:GetWide() - 14, sg.main:GetTall() - 34 )
  508. inside.Paint = function()
  509. surface.SetDrawColor( 255, 255, 255, 255 )
  510. surface.DrawOutlinedRect( 0, 0, inside:GetWide(), inside:GetTall() )
  511. surface.SetDrawColor( 255, 255, 255, 250 )
  512. surface.DrawRect( 1, 1, inside:GetWide() - 2, inside:GetTall() - 2 )
  513. end
  514.  
  515. -- Select player
  516. local plys = vgui.Create( "DComboBox", inside )
  517. plys:SetPos( 5, 5 )
  518. plys:SetSize( 150, 25 )
  519. plys:AddChoice( "Select a Player", nil, true )
  520. plys.curChoice = "Select a Player"
  521. -- Add players for selection
  522. for k, v in next, player.GetHumans() do
  523. plys:AddChoice( v:Nick(), v )
  524. end
  525.  
  526. plys.OnSelect = function( pnl, index, value )
  527. local ent = plys.Data[ index ]
  528. plys.curChoice = ent
  529. end
  530.  
  531. -- Image quality slider
  532. local q = vgui.Create( "Slider", inside )
  533. q:SetPos( 5, 55 )
  534. q:SetWide( 180 )
  535. q:SetMin( 1 )
  536. q:SetMax( 90 )
  537. q:SetDecimals( 0 )
  538. q:SetValue( 50 )
  539.  
  540. -- Run screen grab button
  541. local execute = vgui.Create( "DButton", inside )
  542. execute:SetPos( 5, 35 )
  543. execute:SetSize( 150, 25 )
  544. execute:SetText( "Screengrab" )
  545. execute.Think = function()
  546. local cur = plys.curChoice
  547. if cur and not isstring( cur ) then
  548. execute:SetDisabled( false )
  549. else
  550. execute:SetDisabled( true )
  551. end
  552. end
  553. execute.DoClick = function()
  554. timer.Simple( 0.1, function()
  555. if canScreengrab(LocalPlayer()) then
  556. if IsValid(plys.curChoice) then
  557. net.Start(REQUESTSCREENSHOT)
  558. net.WriteEntity( plys.curChoice )
  559. net.WriteUInt( q:GetValue(), 16 )
  560. net.SendToServer()
  561. resetCurrent()
  562. currentGrab.startTime = CurTime()
  563. currentGrab.playerNick = plys.curChoice:Nick()
  564. appendServerLog(sg.green, "Initializing")
  565. sg.progress:SetFraction( 0 )
  566. else
  567. appendServerLog(Color( 255, 0, 0 ),"Error: Player not found!")
  568. end
  569. else
  570. appendServerLog(Color( 255, 0, 0 ),"Error: Insufficient permissions")
  571. end
  572. end )
  573. end
  574.  
  575. -- Checkbox, controls whether to open the screengrab when received
  576. local auto = vgui.Create( "DCheckBoxLabel", inside )
  577. auto:SetPos( 5, 83 )
  578. auto:SetText( "Automatically Open" )
  579. auto:SetDark( true )
  580. auto:SizeToContents()
  581. auto:SetConVar( "sg_auto_open" )
  582.  
  583. -- List all saved screengrabs
  584. local files = vgui.Create( "DListView", inside )
  585. files:SetPos( 5, 100 )
  586. files:SetSize( 150, 110 )
  587. files:AddColumn( "Screenshots" )
  588. files.filetable = {}
  589. files:SetHeaderHeight( 15 )
  590. local f = file.Find( "screengrabs/*.txt", "DATA" )
  591. files.filetable = f
  592. for k, v in next, f do
  593. files:AddLine( v )
  594. end
  595. -- Screengrabs list autorefresh
  596. files.Think = function()
  597. local f = file.Find( "screengrabs/*.txt", "DATA" )
  598. if table.ToString( files.filetable ) ~= table.ToString( f ) then
  599. files.filetable = f
  600. files:Clear()
  601. for k, v in next, f do
  602. files:AddLine( v )
  603. end
  604. end
  605. end
  606.  
  607. files.OnRowRightClick = function( main, line )
  608. local menu = DermaMenu()
  609. menu:AddOption( "Delete file", function()
  610. local f = files:GetLine( line ):GetValue( 1 )
  611. file.Delete( "screengrabs/" .. f )
  612. end ):SetIcon( "icon16/delete.png" )
  613. menu:AddOption( "View Screenshot", function()
  614. local f = file.Read( "screengrabs/" .. files:GetLine( line ):GetValue( 1 ), "DATA" )
  615. hook.Add( "Think", "wait", function()
  616. if f and isstring( f ) and string.len( f ) > 1 then
  617. DisplayData( f, files:GetLine( line ):GetValue( 1 ) )
  618. hook.Remove( "Think", "wait" )
  619. end
  620. end )
  621. end ):SetIcon( "icon16/zoom.png" )
  622. menu:Open()
  623. end
  624.  
  625. -- Server Logs frame
  626. local svlogs = vgui.Create( "DFrame", inside )
  627. svlogs:SetSize( 220, 230 )
  628. svlogs:SetPos( 165, 5 )
  629. svlogs:SetTitle( "Server Logs" )
  630. svlogs:SetSizable( false )
  631. svlogs.Paint = function()
  632. surface.SetDrawColor( Color( 0, 0, 0, 250 ) )
  633. surface.DrawRect( 0, 0, svlogs:GetSize() )
  634. end
  635. svlogs:ShowCloseButton( false )
  636. -- Server Logs richtext
  637. sg.serverLog = vgui.Create( "RichText", svlogs )
  638. sg.serverLog:Dock( FILL )
  639. sg.serverLog.Paint = function()
  640. sg.serverLog.m_FontName = "rtfont2"
  641. sg.serverLog:SetFontInternal( "rtfont2" )
  642. sg.serverLog:SetBGColor( Color( 0, 0, 0, 0 ) )
  643. sg.serverLog.Paint = nil
  644. end
  645. sg.serverLog:InsertColorChange( 255, 255, 255, 255 )
  646.  
  647. -- Client logs frame
  648. local cllogs = vgui.Create( "DFrame", inside )
  649. cllogs:SetSize( 220, 230 )
  650. cllogs:SetPos( 395, 5 )
  651. cllogs:SetTitle( "Client Logs" )
  652. cllogs:SetSizable( false )
  653. cllogs.Paint = function()
  654. surface.SetDrawColor( Color( 0, 0, 0, 250 ) )
  655. surface.DrawRect( 0, 0, cllogs:GetSize() )
  656. end
  657. cllogs:ShowCloseButton( false )
  658.  
  659. -- Client logs richtext
  660. sg.clientLog = vgui.Create( "RichText", cllogs )
  661. sg.clientLog:Dock( FILL )
  662. sg.clientLog.Paint = function()
  663. sg.clientLog.m_FontName = "rtfont2"
  664. sg.clientLog:SetFontInternal( "rtfont2" )
  665. sg.clientLog:SetBGColor( Color( 0, 0, 0, 0 ) )
  666. sg.clientLog.Paint = nil
  667. end
  668. sg.clientLog:InsertColorChange( 255, 255, 255, 255 )
  669.  
  670. -- Progress bar
  671. sg.progress = vgui.Create( "DProgress", inside )
  672. sg.progress:SetPos( 165, 241 )
  673. sg.progress:SetSize( 450, 20 )
  674. sg.progress:SetFraction( 0 )
  675. --sg.progress.Think = function()
  676. -- sg.progress:SetFraction( progress.num )
  677. --end
  678.  
  679. sg.open = vgui.Create( "DButton", inside )
  680. sg.open:SetPos( 4, 241 )
  681. sg.open:SetSize( 150, 20 )
  682. sg.open:SetText( "Open" )
  683. sg.open:SetDisabled( true )
  684. sg.open.screenshot = nil
  685. sg.open.DoClick = function()
  686. DisplayData( sg.screenshot )
  687. end
  688.  
  689. sg.clientLog.Think = function()
  690. if type( sg.screenshot ) == "string" then
  691. sg.open:SetDisabled( false )
  692. elseif type( sg.screenshot ) == "nil" then
  693. sg.open:SetDisabled( true )
  694. end
  695. end
  696.  
  697. -- Save
  698. local save = vgui.Create( "DButton", inside )
  699. save:SetPos( 4, 220 )
  700. save:SetSize( 150, 20 )
  701. save:SetText( "Save Data" )
  702. save:SetDisabled( true )
  703. sg.serverLog.Think = function()
  704. if type( sg.screenshot ) == "string" then
  705. save:SetDisabled( false )
  706. elseif type( sg.screenshot ) == "nil" then
  707. save:SetDisabled( true )
  708. end
  709. end
  710. save.DoClick = function()
  711. if not file.Exists( "screengrabs", "DATA" ) then
  712. file.CreateDir( "screengrabs" )
  713. end
  714. local name = currentGrab.playerNick .. " - " .. os.date( "%m_%d %H_%M_%S" ) .. ".txt"
  715. local text = sg.screenshot
  716. appendClientLog( Color( 255, 100, 0 ), "Saving to file: " .. name .. " (" .. string.len( text ) .. " bytes)" )
  717. file.Write( "screengrabs/" .. name, text )
  718. timer.Simple( 1, function()
  719. if file.Exists( "screengrabs/" .. name, "DATA" ) then
  720. appendClientLog( Color( 255, 100, 0 ), "Screenshot saved!" )
  721. else
  722. appendClientLog( Color( 255, 0, 0 ), "Error: Screenshot not saved!" )
  723. end
  724. end )
  725. end
  726.  
  727. end
  728. concommand.Add( "screengrab", OpenSGMenu )
  729.  
  730. net_Receive(SENDCAPTUREINTO, function(len)
  731. local captureSize = net.ReadUInt(32)
  732. local blockCount = math.ceil(captureSize / blockLength)
  733. currentGrab.totalParts = blockCount
  734.  
  735. appendServerLog(color_white, "Captured " .. captureSize .. " bytes")
  736. appendServerLog(color_white, blockCount .. " parts")
  737. end)
  738.  
  739. net_Receive(SENDPACKETTOADMIN, function(len)
  740. local dataLength = net.ReadInt(32)
  741. local compressed = net.ReadData(dataLength)
  742. local finished = net.ReadBool()
  743.  
  744. local count = table.insert(currentGrab.data, compressed)
  745.  
  746. -- Update the progress bar and log
  747. if sg.progress and IsValid(sg.progress) then
  748. sg.progress:SetFraction( count / currentGrab.totalParts )
  749. appendClientLog(sg.blue, "Received " .. count .. STNDRD( count ) .. " part")
  750. end
  751.  
  752. -- If finished, build image
  753. if not finished then return end
  754.  
  755. appendClientLog( sg.green, "Finished" )
  756. currentGrab.endTime = CurTime()
  757.  
  758. local completeCompressed = table.concat(currentGrab.data)
  759. local decompressed = util.Decompress(completeCompressed)
  760.  
  761. sg.screenshot = decompressed
  762.  
  763. if GetConVar( "sg_auto_open" ):GetInt() > 0 then
  764. DisplayData( sg.screenshot )
  765. end
  766. end)
Add Comment
Please, Sign In to add comment