Advertisement
Treyzania

HyperCoro v1.1

Feb 17th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.46 KB | None | 0 0
  1. -- HyperCoro v1.1
  2. -- Author: Treyzania
  3.  
  4. --[[
  5.     There's a few things in this API that look like they should be uncommented.
  6.     If you think that you need them uncommented, the do so.  But don't ask me
  7.     for help if you screw something up.  I barely understand this stuff, either.
  8. ]]
  9.  
  10. -- CONFIG
  11.  
  12. -- made ya' look!
  13.  
  14. -- END CONFIG
  15.  
  16. --== CODE ==--
  17. local coros = {}
  18.  
  19. local nativepullevent = nil
  20. local nativecreate = nil
  21.  
  22. verbose = true
  23. local function vprint(text) if verbose then print(text) end end
  24.  
  25. function addCoroutine(name, coro) coros[name] = coro end
  26. function getCoroutine(name) return coros[name] end
  27.  
  28. function removeCoroutine(name)
  29.     local coro = coros[name]
  30.     coros[name] = nil
  31.     return coro
  32. end
  33.  
  34. -- Never called locally, used as an abstraction layer.
  35. local function coroPullEvent(filter)
  36.    
  37.     -- Stolen from http://www.computercraft.info/forums2/index.php?/topic/19908-run-code-in-background/, but modified to loop through a table.
  38.    
  39.     while true do
  40.        
  41.         local event = { nativepullevent() } -- Calls the native one.
  42.        
  43.         for k, v in pairs(coros) do
  44.            
  45.             if coroutine.status(v) == "suspended" then -- We to make sure it is not our function (now a coroutine) calling it.
  46.                 coroutine.resume(v, unpack(event)) -- Unpack( tbl ) returns the contents of the table.
  47.             end
  48.            
  49.         end
  50.        
  51.         if sFilter == event[1] or not filter then -- If the event is the correct type, or there is no filter.
  52.             return unpack(event)
  53.         end
  54.        
  55.     end
  56.    
  57. end
  58.  
  59. function create(name, func, ...)
  60.    
  61.     vprint("Creating coroutine \"" .. name .. "\"...")
  62.    
  63.     local coro = coroutine.create(func)
  64.     addCoroutine(name, coro)
  65.     coroutine.resume(coro, ...)
  66.    
  67.     vprint("Done!")
  68.    
  69.     return coro
  70.    
  71. end
  72.  
  73. local function coroCreate_native(func)
  74.    
  75.     local coro = nativecreate(func)
  76.     addCoroutine(tostring(func), coro)
  77.    
  78. end
  79.  
  80. function init()
  81.    
  82.     -- Abstract away `os.pullEvent` and `coroutine.create`.
  83.     nativepullevent = os.pullEvent
  84.     os.pullEvent = coroPullEvent
  85.     --nativecreate = coroutine.create
  86.     --coroutine.create = coroCreate_native
  87.    
  88.     vprint("HyperCoro initialization complete.")
  89.    
  90. end
  91.  
  92. function finish()
  93.    
  94.     os.pullEvent = nativepullevent
  95.     --coroutine.create = nativecreate
  96.    
  97.     vprint("HyperCoro disengaged.")
  98.    
  99. end
  100.  
  101. -- END OF FILE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement