MudkipTheEpic

CoTest

May 19th, 2013
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. if background then return end
  2. term.clear()
  3. term.setCursorPos(1,1)
  4. local maxn=table.maxn or _G.table.maxn or getfenv(2).table.maxn or error("Couldn't find table.maxn!")
  5. local function sort(table)
  6. if #table==maxn(table) then return table end
  7. local i=1
  8. for j=1,maxn(table) do
  9. if table[j] and j~=i then
  10. table[i]=table[j]
  11. table[j]=nil
  12. i=i+1
  13. elseif table[j] then
  14. i=i+1
  15. end
  16. end
  17. return table
  18. end
  19.  
  20. local function coroutineManage()
  21. local bgCo={}
  22. local bgW={}
  23. local bgP={}
  24. local bgN={}
  25. _G.background={}
  26. function _G.background.addCo(f,...)
  27. local argsy={...}
  28. if not type(f)=="function" then error("function expected, got "..type(f),2) end
  29. local i=maxn(bgCo)+1
  30. bgCo[i]=coroutine.create(function() f(unpack(argsy)) end)
  31. bgN[i]=i
  32. os.queueEvent("coroutine_create",i)
  33. return i
  34. end
  35. function _G.background.addNamedCo(f,name,...)
  36. local argsy={...}
  37. local oName=name
  38. local match=true
  39. if not type(f)=="function" then error("function expected, got "..type(f),2) end
  40. if not type(name)=="string" then error("string expected, got "..type(name),2) end
  41. local ic=1
  42. while match do
  43. match=false
  44. for k,v in pairs(bgN) do
  45. if v==name then match=true end
  46. end
  47. if match then name=oName.." "..ic ic=ic+1 end
  48. end
  49. local i=maxn(bgCo)+1
  50. bgCo[i]=coroutine.create(function() f(unpack(argsy)) end)
  51. bgN[i]=name
  52. os.queueEvent("coroutine_create",i)
  53. return i,name
  54. end
  55. function _G.background.killCo(id)
  56. if not type(id)=="number" then error("number expected, got "..type(id),2) end
  57. if id==1 then error("Cannot kill shell",2) end
  58. if not bgCo[id] then return false, "No such coroutine" end
  59. local l= bgCo[id]
  60. bgCo[id]=nil
  61. bgW[id]=nil
  62. bgP[id]=nil
  63. bgN[id]=nil
  64. os.queueEvent("coroutine_kill",id)
  65. return l
  66. end
  67. function _G.background.pauseCo(id)
  68. if not type(id)==number then error("number expected, got "..type(id),2) end
  69. if not bgCo[id] then return false, "No such coroutine" end
  70. if not bgP[i] then os.queueEvent("coroutine_pause",id) end
  71. bgP[id]=true
  72. return true
  73. end
  74. function _G.background.resumeCo(id)
  75. if not type(id)==number then error("number expected, got "..type(id),2) end
  76. if not bgCo[id] then return false, "No such coroutine" end
  77. if bgP[id] then os.queueEvent("coroutine_resume",id) end
  78. bgP[id]=false
  79. return true
  80. end
  81. function _G.background.renameCo(name,newname)
  82. local match
  83. for k,v in pairs(bgN) do
  84. if v==name then match=k end
  85. end
  86. if not match then return nil, "No such coroutine" end
  87. bgN[match]=newname
  88. return true, bgN[match]
  89. end
  90. function _G.background.getNamedCoID(name)
  91. for k,v in pairs(bgN) do
  92. if v==name then return k end
  93. end
  94. end
  95. function _G.background.getCoName(id)
  96. return bgN[id]
  97. end
  98. function _G.background.getCoroutines()
  99. return {unpack(bgCo)}
  100. end
  101. function _G.background.getCoNames()
  102. return {unpack(bgN)}
  103. end
  104. function _G.background.isPaused(i)
  105. return not bgCo[i] and nil or (bgP[i] or false)
  106. end
  107. local backLocal={}
  108. for k,v in pairs(_G.background) do
  109. backLocal[k]=v
  110. end
  111. setmetatable(coroutine,{["__index"]=background})
  112. _G.background.addCo(dofile,"rom/programs/shell")
  113. --print("test")
  114. local e={}
  115. while true do
  116. bgCo=sort(bgCo)
  117. bgW=sort(bgW)
  118. bgN=sort(bgN)
  119. if #bgCo==0 then os.shutdown() end
  120. for i=1,#bgCo do
  121. local n=bgCo[i]
  122. --print(n)
  123. --print(i)
  124. if n then
  125. if (not (bgW[i] and bgW[i]~=e[1]) or e[1]=="terminate") and not bgP[i] then
  126. --print("Passing event to coroutine.")
  127. local ok,param=coroutine.resume(bgCo[i],unpack(e))
  128. --print(ok,":",param)
  129. --print(table.concat(e,":"))
  130. if not ok or coroutine.status(bgCo[i])=="dead" then
  131. if not ok then os.queueEvent("coroutine_error",i,err) else os.queueEvent("coroutine_death",i) end
  132. --print("Oh noes, coroutine #"..i.." has crashed!")
  133. --print(param)
  134. if i==1 then os.shutdown() else backLocal.killCo(i) end
  135. else
  136. bgW[i]=param or false
  137. --print(param)
  138. end
  139. end
  140. end
  141. end
  142. if #bgCo<#bgP+1 then for k,v in pairs(bgP) do if v then os.queueEvent("coroutine_resume",k) end end bgP={} end
  143. e={os.pullEventRaw()}
  144. --print("Recieved event")
  145. end
  146. end
  147. shell.exit()
  148. coroutineManage()
Advertisement
Add Comment
Please, Sign In to add comment