Advertisement
jille_Jr

CC: Startup program, executes folders

Feb 26th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.93 KB | None | 0 0
  1. --[[--
  2.     This program runs everything
  3.     in the folder /autorun/ once
  4.     and everything within the folder
  5.     /loop/ continously
  6. --]]--
  7.  
  8. -- folder path
  9. local runpath = "autorun"
  10. local looppath = "loop"
  11.  
  12. -- if the delay between each
  13. -- run is less than this, cancel out
  14. -- dont touch if you dont
  15. -- know what youre doing
  16. local throttle = 5
  17.  
  18. --(( Functions ))--
  19.  
  20. local function checkfolder(path)
  21.     if fs.isDir(path) then
  22.         -- not empty
  23.         return #fs.list(path) > 0
  24.     else
  25.         if fs.exists(path) then
  26.             -- path is a file, not folder
  27.             error("'"..path.."' is not a folder!",0)
  28.         else
  29.             -- no file nor folder
  30.             return false
  31.         end
  32.     end
  33. end
  34.  
  35. local function executefolder(folder)
  36.     local list = fs.list(folder)
  37.     for index,name in ipairs(list) do
  38.         local path = fs.combine(folder,name)
  39.         if fs.isDir(path) then
  40.             executefolder(path)
  41.         else
  42.             shell.run(path)
  43.         end
  44.     end
  45. end
  46.  
  47. local function oldStartupScript()
  48.     local label = os.getComputerLabel()
  49.     local info = function(key,value)
  50.         term.setTextColor(colors.lightGray)
  51.         write(key)
  52.         if term.isColor and term.isColor() then
  53.             term.setTextColor(colors.yellow)
  54.         else
  55.             term.setTextColor(colors.white)
  56.         end
  57.         print(value)
  58.     end
  59.  
  60.     info("Computer ID: ","#"..os.getComputerID())
  61.     info("Computer label: ", os.getComputerLabel() or "<unnamed>")
  62.     if turtle then
  63.         local fuel = turtle.getFuelLevel()
  64.         local limit = math.floor(100 * fuel / turtle.getFuelLimit()) .. "%"
  65.         info("Fuel level: ", fuel.." ("..limit..")")
  66.     end
  67. end
  68.  
  69. --(( Error handleing ))--
  70.  
  71. --(( Main program ))--
  72.  
  73. oldStartupScript()
  74.  
  75. if checkfolder(runpath) then
  76.     executefolder(runpath)
  77. end
  78.  
  79. while true and checkfolder(looppath) do
  80.     local starttime = os.clock()
  81.     executefolder(looppath)
  82.     local endtime = os.clock()
  83.  
  84.     if endtime - starttime < throttle then
  85.         error("Stopping loop in case of continous erroring",0)
  86.     end
  87. end
  88.  
  89. --(( EOF ))--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement