BrainStone

Lua Protect

Dec 25th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.97 KB | None | 0 0
  1. -- Simple API to protect your program from being terminated by pressing and holding CTRL + T
  2.  
  3. local terminated = false
  4. local useColor = term.isColor ~= nil and term.isColor()
  5. local terminateEvent
  6.  
  7. local function runLocal(parallelFunc, password, func)
  8.   local function pullEvent(...)
  9.     local result = {os.pullEventRaw(...)}
  10.    
  11.     while result[1] == "terminate" do
  12.       terminateEvent = true
  13.      
  14.       result = {os.pullEventRaw(...)}
  15.     end
  16.    
  17.     return unpack(result)
  18.   end
  19.  
  20.   local function warnTerminate()
  21.     local x, y
  22.    
  23.     repeat
  24.       x, y = term.getCursorPos()
  25.       term.setCursorPos(1, 2)
  26.       term.write("                                ")
  27.       term.setCursorPos(1, 3)
  28.       term.write("                      ")
  29.       term.setCursorPos(x, y)
  30.      
  31.       repeat
  32.         sleep(0.1)
  33.       until terminateEvent or terminated
  34.      
  35.       if terminated then break end
  36.      
  37.       terminateEvent = false
  38.      
  39.       if useColor then
  40.         term.setTextColor(colors.red)
  41.       end
  42.      
  43.       x, y = term.getCursorPos()
  44.       term.setCursorPos(1, 2)
  45.       term.write("Terminate (CTRL + T) is blocked!")
  46.       term.setCursorPos(1, 3)
  47.       term.write("Please Enter Password!")
  48.       term.setCursorPos(x, y)
  49.      
  50.       if useColor then
  51.         term.setTextColor(colors.white)
  52.       end
  53.      
  54.       sleep(2)
  55.     until terminated
  56.   end
  57.  
  58.   local function passwordCheck()
  59.     local warnWrongPass = false
  60.    
  61.     repeat
  62.       if warnWrongPass then
  63.         term.clear()
  64.         term.setCursorPos(1, 1)
  65.        
  66.         if useColor then
  67.           term.setTextColor(colors.red)
  68.         end
  69.        
  70.         term.write("Wrong Password!")
  71.        
  72.         sleep(2)
  73.        
  74.         if useColor then
  75.           term.setTextColor(colors.white)
  76.         end
  77.       else
  78.         warnWrongPass = true
  79.       end
  80.      
  81.       term.clear()
  82.       term.setCursorPos(1, 1)
  83.       term.write("Enter passwort to terminate: ")
  84.     until read("*") == password
  85.    
  86.     if useColor then
  87.       term.setTextColor(colors.lime)
  88.     end
  89.    
  90.     terminated = true
  91.    
  92.     term.write("Passwort correct!")
  93.     term.setCursorPos(1, 3)
  94.     term.write("Terminating...")
  95.     term.setCursorPos(1, 4)
  96.    
  97.     if useColor then
  98.       term.setTextColor(colors.white)
  99.     end
  100.   end
  101.  
  102.   local oldPullEvent = os.pullEvent
  103.   terminateEvent = false
  104.   terminated = false
  105.  
  106.   os.pullEvent = pullEvent
  107.  
  108.   parallelFunc(passwordCheck, warnTerminate, func)
  109.  
  110.   os.pullEvent = oldPullEvent
  111. end
  112.  
  113. function run(password, func)
  114.   runLocal(parallel.waitForAny, password, func)
  115. end
  116.  
  117. function runNotify(password, func)
  118.   runLocal(parallel.waitForAll, password, func)
  119. end
  120.  
  121. function safeRun(func, ...)
  122.   if func == nil then
  123.     error("attempt to call nil", 2)
  124.   end
  125.  
  126.   local ans
  127.  
  128.   repeat
  129.     ans = {pcall(func, ...)}
  130.   until ans[1]
  131.  
  132.   table.remove(ans, 1)
  133.   return unpack(ans)
  134. end
  135.  
  136. function isTerminated()
  137.   return terminated
  138. end
Advertisement
Add Comment
Please, Sign In to add comment