MudkipTheEpic

AntiVirus

Feb 26th, 2013
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. local findtable=function(txt)
  2. --[[ Broken Stub
  3. local t
  4. if not string.find(txt, ".") then return (string.sub(txt, {string.find(txt,".")}[1] - 1, #txt)) end
  5. while true do
  6. if not string.find(string.sub(txt.."", {string.find(txt.."", ".")}[2]+1).."", ".") then return txt break end
  7. local t={string.find(txt.."", ".")}
  8. txt=string.sub(txt, t[2]+1)..""
  9. end
  10. ]]--
  11. return "fs"
  12. end
  13.  
  14. local function drawMenu(tMenu, xpos, ypos, curSel)
  15. if xpos == true then
  16. centered = true
  17. else
  18. centered = false
  19. end
  20. if curSel ~= nil or curSel~= "" then
  21. sel = 1
  22. else
  23. sel = curSel
  24. end
  25. tPlaces = {}
  26. while true do
  27. for i=1, #tMenu do
  28. iSel = i-1
  29. local w, h = term.getSize()
  30. table.remove(tPlaces, i)
  31. if i == sel then
  32. if centered == false then
  33. term.setCursorPos(xpos, ypos+iSel)
  34. term.clearLine()
  35. table.insert(tPlaces, i, term.getCursorPos()+2)
  36. write("[ "..tMenu[i].." ]")
  37. else
  38. term.setCursorPos(w/2-#tMenu[i]/2-2, ypos+iSel)
  39. term.clearLine()
  40. table.insert(tPlaces, i, term.getCursorPos()+2)
  41. write("[ "..tMenu[i].." ]")
  42. end
  43. else
  44. if centered == false then
  45. term.setCursorPos(xpos, ypos+iSel)
  46. term.clearLine()
  47. table.insert(tPlaces, i, term.getCursorPos())
  48. write(tMenu[i])
  49. else
  50. term.setCursorPos(w/2-#tMenu[i]/2, ypos+iSel)
  51. term.clearLine()
  52. table.insert(tPlaces, i, term.getCursorPos())
  53. write(tMenu[i])
  54. end
  55. end
  56. end
  57. local event, key, x, y = os.pullEvent()
  58. if event == "key" then
  59. if key == 200 and sel > 1 and sel <= #tMenu then sel = sel-1
  60. elseif key == 208 and sel >= 1 and sel < #tMenu then sel = sel+1
  61. elseif key == 208 and sel == #tMenu then sel = 1
  62. elseif key == 200 and sel == 1 then sel = #tMenu
  63. end
  64. for v = 1, #tMenu do
  65. if key == 28 and sel == v then
  66. return tMenu[v], sel
  67. end
  68. end
  69. elseif event == "mouse_click" then
  70. for s = 1, #tMenu do
  71. iSel = s-1
  72. if s == sel then
  73. if x >= tPlaces[s] and x < tPlaces[s]+#tMenu[s] and y == ypos+iSel then
  74. return tMenu[s], s
  75. end
  76. end
  77. end
  78. end
  79. end
  80. end
  81. local function centerPrint(message, yoffset)
  82. local width = term.getSize()
  83. local o = string.len(message)
  84. if o > width then
  85. return 1
  86. else
  87. term.setCursorPos((width-o)/2, yoffset)
  88. print(message)
  89. end
  90. end
  91. local function errorVirus(lfunction)
  92. local w,h=term.getSize()
  93. term.setTextColor(colors.red)
  94. centerPrint("ERROR:", 5)
  95. centerPrint("Program tried to use function "..lfunction.."!", 7)
  96. term.setTextColor(1)
  97. centerPrint("Would you like to let this program use this function?")
  98. local sel=drawMenu({"Yes", "No", "Let use all functions from "..findtable(lfunction)},nil,9,2)
  99. if sel==1 then env[lfunction]=oldenv[lfunction] return
  100. elseif sel==2 then error("Program terminated",2)
  101. elseif sel==3 then
  102. for k,v in pairs(oldenv[findtable(lfunction)]) do
  103. env[findtable(lfunction)[k]]=v
  104. end
  105. end
  106. end
  107. local env={["fs"]={}, ["shell"]={}, ["os"]={}}
  108. local oldenv={}
  109. for k,v in pairs(_G) do
  110. oldenv[k]=v
  111. end
  112. term.clear()
  113. errorVirus("fs.open")
Advertisement
Add Comment
Please, Sign In to add comment