manaphoenix

Ficsit Networks [Threaded Filesystem Loader]

Sep 25th, 2020 (edited)
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.18 KB | None | 0 0
  1. --[[ realtime program editor ]]--
  2. --[[
  3.     set myFile equal to the file you want to run and rerun whenever possible.
  4.     leave it blank for it to pick a file on its own.
  5.  
  6.     set myDrive equal to the ID of the drive you want run from.
  7.     leave it blank for it to pick a drive on its own
  8.     Note: Can pick the wrong drive if more than one exists :)
  9. ]]
  10.  
  11. local myFile = "testFile.lua"
  12. local myDrive = ""
  13.  
  14. -- [[ Code Do Not Touch ]] --
  15. local thread = {
  16.   threads = {},
  17.   watcherThread = nil,
  18.   shouldRun = true
  19. }
  20. local fs = filesystem
  21.  
  22. -- init filesystem
  23. if fs.initFileSystem("/dev") == false then
  24.     computer.panic("Cannot initialize /dev!")
  25. end
  26. -- find drive if one isn't stated
  27. if (myDrive:len() == 0) then
  28.   for _,f in pairs(fs.childs("/dev")) do
  29.    if f ~= "serial" then
  30.     myDrive = f
  31.     break
  32.    end
  33.   end
  34. end
  35. -- mount drive
  36. if (myDrive:len() > 0) then
  37.   local ch = fs.mount("/dev/"..myDrive, "/")
  38.   if (not ch) then
  39.     computer.panic(myDrive .. " could not be found!")
  40.   end
  41. else
  42.   computer.panic("No HardDrive Found!")
  43. end
  44.  
  45. -- find a file if one isn't stated
  46. if (myFile:len() == 0) then
  47.   for _,f in pairs(fs.childs("/")) do
  48.     if fs.isFile(f) then
  49.       myFile = f
  50.       break
  51.     end
  52.   end
  53. end
  54.  
  55. local function sleep(sec)
  56.   sec = sec or 0.0
  57.   local curTime = computer.millis()
  58.   local wantedTime = curTime + (sec*1000)
  59.   while computer.millis() < wantedTime do
  60.     computer.skip()
  61.     fs.exists("/")
  62.   end
  63. end
  64.  
  65. thread.create = function(func, isWatcher)
  66.   if type(func) == 'function' then
  67.     local index = tostring(func)
  68.     if thread.threads[index] == nil then
  69.       local t = {}
  70.       t.co = coroutine.create(func)
  71.       function t:stop()
  72.         if thread.threads[index] ~= nil then
  73.           thread.threads[index] = nil
  74.         end
  75.       end
  76.       thread.threads[index] = t
  77.       return t
  78.     end
  79.     if isWatcher then
  80.       thread.watcherThread = t
  81.     end
  82.   end
  83.   return false
  84. end
  85.  
  86. local function doFileThread()
  87.   local status, err = pcall(function()
  88.     fs.doFile(myFile)
  89.   end)
  90.   if err ~= nil then
  91.     print(err)
  92.   end
  93. end
  94.  
  95. local function resumeThreads()
  96.   for i,v in pairs(thread.threads) do
  97.     computer.skip()
  98.     if (v.co ~= nil) then
  99.       coroutine.resume(true,v.co)
  100.     end
  101.   end
  102. end
  103.  
  104. local function CreateFileThread(v)
  105.   print("creating new thread")
  106.   v:stop()
  107.   sleep(0.1)
  108.   thread.create(doFileThread)
  109. end
  110.  
  111. --TODO: override default event.pull() method
  112. thread.watcher = function()
  113.   local file = fs.open(myFile,"r")
  114.   local size = file:seek("end")
  115.   local newSize = size
  116.   file:close()
  117.   while thread.shouldRun do
  118.     sleep(5)
  119.     --file = fs.open(myFile,"r")
  120.     --local newSize = file:seek("end")
  121.     --file:close()
  122.     for i, v in pairs(thread.threads) do
  123.       sleep(0.1)
  124.       if (v.co == nil or coroutine.status(v.co) == "dead") then
  125.         thread.shouldRun = false
  126.         break
  127.       end
  128.       if (newSize ~= size and v ~= thread.watcherThread) then
  129.         size = newSize
  130.         CreateFileThread(v)
  131.         break
  132.       end
  133.     end
  134.   end
  135. end
  136.  
  137. thread.create(doFileThread)
  138. thread.create(thread.watcher)
  139. while (coroutine.status(thread.watcherThread) ~= "dead") do
  140.   resumeThreads()
  141.   sleep(1)
  142. end
  143.  
Add Comment
Please, Sign In to add comment