Advertisement
Guest User

Advanced Keypad Logger [24/01/21]

a guest
Jan 24th, 2021
6,945
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.51 KB | None | 0 0
  1. local AKPL = (file.Read("akpl.dat") != nil && util.JSONToTable(util.Decompress(file.Read("akpl.dat"))) || {})
  2.  
  3. // internal calculations
  4. local Col = {[-1]=Color(150,150,150,255), [0]=Color(255,0,0,255), [1]=Color(255,165,0,255), [2]=Color(0,255,0,255)}
  5. AKPL.LP = LocalPlayer()
  6. function AKPL.CalculateRenderPos(self)
  7. local pos = self:GetPos() pos:Add(self:GetForward() * self:OBBMaxs().x) pos:Add(self:GetRight() * self:OBBMaxs().y) pos:Add(self:GetUp() * self:OBBMaxs().z) pos:Add(self:GetForward() * 0.15) return pos
  8. end
  9. function AKPL.CalculateRenderAng(self)
  10. local ang = self:GetAngles() ang:RotateAroundAxis(ang:Right(), -90) ang:RotateAroundAxis(ang:Up(), 90) return ang
  11. end
  12. function AKPL.CalculateKeypadCursorPos(ply, ent)
  13. if !ply:IsValid() then return end local tr = util.TraceLine( { start = ply:EyePos(), endpos = ply:EyePos() + ply:GetAimVector() * 65, filter = ply } ) if !tr.Entity or tr.Entity ~= ent then return 0, 0 end local scale = ent.Scale || 0.02 if !scale then return 0, 0 end local pos, ang = AKPL.CalculateRenderPos(ent), AKPL.CalculateRenderAng(ent) if !pos or !ang then return 0, 0 end local normal = ent:GetForward() local intersection = util.IntersectRayWithPlane(ply:EyePos(), ply:GetAimVector(), pos, normal) if !intersection then return 0, 0 end local diff = pos - intersection local x = diff:Dot( -ang:Forward() ) / scale local y = diff:Dot( -ang:Right() ) / scale return x, y
  14. end
  15. local elements = {{x = 0.075, y = 0.04, w = 0.85, h = 0.25,},{x = 0.075, y = 0.04 + 0.25 + 0.03, w = 0.85 / 2 - 0.04 / 2 + 0.05, h = 0.125, text = "ABORT",},{x = 0.5 + 0.04 / 2 + 0.05, y = 0.04 + 0.25 + 0.03, w = 0.85 / 2 - 0.04 / 2 - 0.05, h = 0.125, text = "OK",}} do for i = 1, 9 do local column = (i - 1) % 3 local row = math.floor((i - 1) / 3) local element = {x = 0.075 + (0.3 * column), y = 0.175 + 0.25 + 0.05 + ((0.5 / 3) * row), w = 0.25, h = 0.13, text = tostring(i), } elements[#elements + 1] = element end end
  16. function AKPL.GetHoveredElement(ply, ent)
  17. local scale = ent.Scale || 0.02 local w, h = (ent:OBBMaxs().y - ent:OBBMins().y) / scale , (ent:OBBMaxs().z - ent:OBBMins().z) / scale local x, y = AKPL.CalculateKeypadCursorPos(ply, ent) for _, element in ipairs(elements) do local element_x = w * element.x local element_y = h * element.y local element_w = w * element.w local element_h = h * element.h if element_x < x and element_x + element_w > x and element_y < y and element_y + element_h > y then return element end end
  18. end
  19. // internal calculations
  20.  
  21. AKPL.KeypadCache = {}
  22. AKPL.KeypadOwners = AKPL.KeypadOwners || {}
  23. AKPL.AwaitingResponse = {}
  24. AKPL.ShouldUnlockKeypad = {}
  25.  
  26. AKPL.InsertKey = function(i, p, v)
  27. if(!tonumber(i) || !tonumber(p) || !tonumber(v)) then return i end
  28. local val = i:ToTable()
  29. val[p] = v
  30. return table.concat(val)
  31. end
  32.  
  33. AKPL.ResetLog = function(ent, validated)
  34. if(!validated) then
  35. AKPL.KeypadCache[ent]["Code"] = "0000"
  36. end
  37. AKPL.KeypadCache[ent]["Validated"] = validated && 2 || -1
  38. AKPL.AwaitingResponse[ent] = nil
  39. AKPL.ShouldUnlockKeypad[ent] = nil
  40. end
  41.  
  42. AKPL.ValidCode = function(code)
  43. return (tonumber(code) != 0 && code:StartWith(code:Replace("0", "")))
  44. end
  45.  
  46. AKPL.ValidateCode = function(ent, status)
  47. if(status == ent.Status_Granted) then
  48. local code = AKPL.KeypadCache[ent]["Code"]
  49. if (AKPL.ValidCode(code)) then
  50. AKPL.KeypadCache[ent]["Validated"] = 1
  51. if(AKPL.AwaitingResponse[ent]) then
  52. AKPL.AwaitingResponse[ent][1] = -1
  53. end
  54. end
  55. elseif(status == ent.Status_Denied) then
  56. if(AKPL.KeypadCache[ent]["Validated"] <= 0 || (AKPL.KeypadCache[ent]["Validated"] == 1 && AKPL.AwaitingResponse[ent] && AKPL.AwaitingResponse[ent][3] == 3)) then
  57. AKPL.ResetLog(ent)
  58. end
  59. end
  60. end
  61.  
  62. AKPL.Logger = function(ent, name, old, new)
  63. local Handler = {
  64. ["Text"] = function(ent, old, new)
  65. if(AKPL.AwaitingResponse[ent]) then
  66. if(AKPL.AwaitingResponse[ent][3] == 1) then
  67. if((new:len() - old:len()) == AKPL.AwaitingResponse[ent][1]) then
  68. if(!AKPL.AwaitingResponse[ent][2]) then
  69. AKPL.AwaitingResponse[ent][3] = 3
  70. else
  71. AKPL.AwaitingResponse[ent][3] = 2
  72. end
  73. else
  74. AKPL.AwaitingResponse[ent] = nil
  75. end
  76. elseif(AKPL.AwaitingResponse[ent][3] == 2) then
  77. if((new:len() - old:len()) == tostring(AKPL.AwaitingResponse[ent][2]):len()) then
  78. AKPL.AwaitingResponse[ent][3] = 3
  79. else
  80. AKPL.AwaitingResponse[ent] = nil
  81. end
  82. end
  83. return end
  84. if(new == "" && AKPL.KeypadCache[ent]["Validated"] <= 0) && AKPL.ValidCode(AKPL.KeypadCache[ent]["Code"]) then return AKPL.ResetLog(ent) end
  85. if(!ent:GetSecure()) then
  86. for k, v in ipairs(string.ToTable(new)) do
  87. AKPL.KeypadCache[ent]["Code"] = AKPL.InsertKey(AKPL.KeypadCache[ent]["Code"], k, v)
  88. end
  89. else
  90. for k, v in ipairs(ents.FindInSphere(ent:GetPos(), 120)) do
  91. if(!v:IsPlayer()) then continue end
  92. local element = AKPL.GetHoveredElement(v, ent)
  93. if(element) then
  94. if(tonumber(element.text)) then
  95. AKPL.KeypadCache[ent]["Code"] = AKPL.InsertKey(AKPL.KeypadCache[ent]["Code"], new:len(), tonumber(element.text))
  96. end
  97. else
  98. continue
  99. end
  100. end
  101. end
  102. if(AKPL.KeypadCache[ent]["Validated"] != -1 && AKPL.GetEntOwner(ent)) then
  103. AKPL.KeypadOwners[AKPL.GetEntOwner(ent)][AKPL.RenderText(AKPL.KeypadCache[ent]["Code"])] = AKPL.KeypadCache[ent]["Validated"]
  104. end
  105. end,
  106. ["Status"] = function(ent, old, new)
  107. AKPL.ValidateCode(ent, tonumber(new))
  108. end,
  109. }
  110. if(Handler[name]) then
  111. Handler[name](ent, old, new)
  112. end
  113. end
  114.  
  115. AKPL.GetPlayerByName = function(name)
  116. if(!name || name == "") then return false end
  117. name = string.lower(name)
  118. for _,v in ipairs(player.GetHumans()) do
  119. if(string.find(string.lower(v:Name()),name,1,true) != nil)
  120. then return v
  121. end
  122. end
  123. return false
  124. end
  125.  
  126. AKPL.GetEntOwner = function(ent)
  127. local ply = AKPL.GetPlayerByName(ent:GetNWString("FounderName"))
  128. return ply && ply:AccountID() || ply
  129. end
  130.  
  131. AKPL.RegisterCallback = function(ent)
  132. if(!AKPL.KeypadCache[ent] && isfunction(ent.GetStatus) && isfunction(ent.GetText)) then
  133. ent:NetworkVarNotify("Text", AKPL.Logger)
  134. ent:NetworkVarNotify("Status", AKPL.Logger)
  135. AKPL.KeypadCache[ent] = {["Code"]="0000", ["Validated"]=-1}
  136. local sid = AKPL.GetEntOwner(ent)
  137. if(sid == false) then return end
  138. if(!AKPL.KeypadOwners[sid]) then
  139. AKPL.KeypadOwners[sid] = {}
  140. end
  141. AKPL.KeypadOwners[sid][#AKPL.KeypadOwners[sid]+1] = ent
  142. end
  143. end
  144.  
  145. AKPL.SendCommand = function(ent, c, d)
  146. if(ent:GetStatus() != ent.Status_None || AKPL.LP:EyePos():Distance(ent:GetPos()) >= 120 || util.NetworkStringToID(ent:GetClass()) == 0) then return end
  147. net.Start(ent:GetClass())
  148. net.WriteEntity(ent)
  149. net.WriteUInt(c, 4)
  150. if(tonumber(d)) then
  151. net.WriteUInt(tonumber(d), 8)
  152. end
  153. net.SendToServer()
  154. end
  155.  
  156. AKPL.TestCodes = function(ent)
  157. if(AKPL.AwaitingResponse[ent] && AKPL.AwaitingResponse[ent][3] != 1) then
  158. if(AKPL.AwaitingResponse[ent][1] == -1) then
  159. AKPL.ResetLog(ent, true)
  160. elseif(AKPL.AwaitingResponse[ent][3] == 2) then
  161. AKPL.SendCommand(ent, 0, AKPL.AwaitingResponse[ent][2])
  162. elseif(AKPL.AwaitingResponse[ent][3] == 3) then
  163. AKPL.SendCommand(ent, 1)
  164. end
  165. else
  166. if(AKPL.KeypadCache[ent]["Validated"] == 2) then
  167. AKPL.ShouldUnlockKeypad[ent] = true
  168. end
  169. if(AKPL.KeypadCache[ent]["Validated"] != 1 && !AKPL.ShouldUnlockKeypad[ent]) then return end
  170. if(ent:GetText() != "") then
  171. return AKPL.SendCommand(ent, 2)
  172. end
  173. local code = AKPL.KeypadCache[ent]["Code"]:Replace("0",""):ToTable()
  174. local split = tonumber(table.concat({code[1], code[2], code[3]}))
  175. if(!split) then return end
  176. if(split < 2^8) then
  177. AKPL.AwaitingResponse[ent] = {3, code[4], 1}
  178. else
  179. split = tonumber(table.concat({code[1], code[2]}))
  180. AKPL.AwaitingResponse[ent] = {2, tonumber(table.concat({code[3], code[4]})), 1}
  181. end
  182. AKPL.SendCommand(ent, 0, split)
  183. end
  184. end
  185.  
  186. hook.Add("Tick", "AKPL", function()
  187. file.Write("akpl.dat", util.Compress(util.TableToJSON(AKPL)))
  188. for k, v in ipairs(ents.FindByClass("keypad*")) do
  189. AKPL.RegisterCallback(v)
  190. --AKPL.TestCodes(v) --this only works if the first 3 digits of a keypad code are under 255, TODO account for keypad cooldowns
  191. end
  192. end)
  193.  
  194. AKPL.RenderText = function(str)
  195. if(tonumber(str) == 0) then return "Unknown" elseif(AKPL.ValidCode(str)) then return str:Replace("0", "") else return str:Replace("0", "*") end
  196. end
  197. AKPL.Gradient = Material( "gui/gradient" )
  198.  
  199. hook.Add("HUDPaint","AKPL", function()
  200. local tr = AKPL.LP:GetEyeTrace().Entity
  201. if IsValid(tr) and AKPL.KeypadCache[tr] then
  202. local text = AKPL.RenderText(AKPL.KeypadCache[tr]["Code"])
  203. local color = Col[AKPL.KeypadCache[tr]["Validated"]]
  204. surface.SetDrawColor( Color(0,0,50,255) )
  205. surface.SetMaterial( AKPL.Gradient )
  206. surface.DrawTexturedRect( ScrW() / 2 + 57, ScrH() / 2 - 7, 50, 15 )
  207. draw.SimpleText(text, "DermaDefault", ScrW() / 2 + 60, ScrH() / 2, color, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
  208. end
  209.  
  210. for k, v in pairs(AKPL.KeypadCache) do
  211. if(IsValid(k) && k != tr) then
  212. local pos = k:GetPos():ToScreen()
  213. if(pos.visible) then
  214. local text = AKPL.RenderText(v["Code"])
  215. local color = Col[v["Validated"]]
  216. surface.SetDrawColor( Color(0,0,50,255) )
  217. surface.SetMaterial( AKPL.Gradient )
  218. surface.DrawTexturedRect( pos.x, pos.y, 50, 15 )
  219. draw.SimpleText( text, "DermaDefault", pos.x + 5, pos.y + 6, color, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
  220. end
  221. end
  222. end
  223. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement