Advertisement
Guest User

Daemon

a guest
Feb 7th, 2016
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.24 KB | None | 0 0
  1. local function fIter (t, i)
  2.     i = not i and #t or (i - 1)
  3.     if i>0 then
  4.         return i, t[i]
  5.     end
  6. end
  7. local tBackground = setmetatable({},{
  8.     __newindex = function(t,k,v)
  9.         v = {cRoutine=coroutine.create(v[1]),sFilter=nil,sIdentifier=v[2]}
  10.         rawset(t,k,v)
  11.     end;
  12. })
  13. function add(fFunc,sIdentifier)
  14.     tBackground[#tBackground+1] = {fFunc,sIdentifier}
  15.     return tBackground[#tBackground]
  16. end
  17. function rem(cRoutine)
  18.     cRoutine.sFilter="Daemon_internal_remove"
  19. end
  20. function getBackground()
  21.     return tBackground
  22. end
  23. local bPullMeta = false
  24. function os.pullEventRaw(sEvent)
  25.     local tData
  26.     if bPullMeta then
  27.         return coroutine.yield(sEvent)
  28.     else
  29.         repeat
  30.             bPullMeta = true
  31.             tData = {coroutine.yield()}
  32.             for iIndex,cRoutine in fIter, tBackground do
  33.                 if cRoutine.sFilter==tData[1] or not cRoutine.sFilter then
  34.                     local bOk, sInnerEvent = coroutine.resume(cRoutine.cRoutine,unpack(tData))
  35.                     if bOk then
  36.                         cRoutine.sFilter = sInnerEvent
  37.                     else
  38.                         print(sInnerEvent)
  39.                         table.remove(tBackground,iIndex)
  40.                     end
  41.                 elseif cRoutine.sFilter=="Daemon_internal_remove" then
  42.                     table.remove(tBackground,iIndex)
  43.                 end
  44.             end
  45.             bPullMeta = false
  46.         until tData[1] == sEvent or not sEvent
  47.         return unpack(tData)
  48.     end
  49. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement