Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if background then return end
- term.clear()
- term.setCursorPos(1,1)
- local maxn=table.maxn or _G.table.maxn or getfenv(2).table.maxn or error("Couldn't find table.maxn!")
- local function sort(table)
- if #table==maxn(table) then return table end
- local i=1
- for j=1,maxn(table) do
- if table[j] and j~=i then
- table[i]=table[j]
- table[j]=nil
- i=i+1
- elseif table[j] then
- i=i+1
- end
- end
- return table
- end
- local function coroutineManage()
- local bgCo={}
- local bgW={}
- local bgP={}
- local bgN={}
- _G.background={}
- function _G.background.addCo(f,...)
- local argsy={...}
- if not type(f)=="function" then error("function expected, got "..type(f),2) end
- local i=maxn(bgCo)+1
- bgCo[i]=coroutine.create(function() f(unpack(argsy)) end)
- bgN[i]=i
- os.queueEvent("coroutine_create",i)
- return i
- end
- function _G.background.addNamedCo(f,name,...)
- local argsy={...}
- local oName=name
- local match=true
- if not type(f)=="function" then error("function expected, got "..type(f),2) end
- if not type(name)=="string" then error("string expected, got "..type(name),2) end
- local ic=1
- while match do
- match=false
- for k,v in pairs(bgN) do
- if v==name then match=true end
- end
- if match then name=oName.." "..ic ic=ic+1 end
- end
- local i=maxn(bgCo)+1
- bgCo[i]=coroutine.create(function() f(unpack(argsy)) end)
- bgN[i]=name
- os.queueEvent("coroutine_create",i)
- return i,name
- end
- function _G.background.killCo(id)
- if not type(id)=="number" then error("number expected, got "..type(id),2) end
- if id==1 then error("Cannot kill shell",2) end
- if not bgCo[id] then return false, "No such coroutine" end
- local l= bgCo[id]
- bgCo[id]=nil
- bgW[id]=nil
- bgP[id]=nil
- bgN[id]=nil
- os.queueEvent("coroutine_kill",id)
- return l
- end
- function _G.background.pauseCo(id)
- if not type(id)==number then error("number expected, got "..type(id),2) end
- if not bgCo[id] then return false, "No such coroutine" end
- if not bgP[i] then os.queueEvent("coroutine_pause",id) end
- bgP[id]=true
- return true
- end
- function _G.background.resumeCo(id)
- if not type(id)==number then error("number expected, got "..type(id),2) end
- if not bgCo[id] then return false, "No such coroutine" end
- if bgP[id] then os.queueEvent("coroutine_resume",id) end
- bgP[id]=false
- return true
- end
- function _G.background.renameCo(name,newname)
- local match
- for k,v in pairs(bgN) do
- if v==name then match=k end
- end
- if not match then return nil, "No such coroutine" end
- bgN[match]=newname
- return true, bgN[match]
- end
- function _G.background.getNamedCoID(name)
- for k,v in pairs(bgN) do
- if v==name then return k end
- end
- end
- function _G.background.getCoName(id)
- return bgN[id]
- end
- function _G.background.getCoroutines()
- return {unpack(bgCo)}
- end
- function _G.background.getCoNames()
- return {unpack(bgN)}
- end
- function _G.background.isPaused(i)
- return not bgCo[i] and nil or (bgP[i] or false)
- end
- local backLocal={}
- for k,v in pairs(_G.background) do
- backLocal[k]=v
- end
- setmetatable(coroutine,{["__index"]=background})
- _G.background.addCo(dofile,"rom/programs/shell")
- --print("test")
- local e={}
- while true do
- bgCo=sort(bgCo)
- bgW=sort(bgW)
- bgN=sort(bgN)
- if #bgCo==0 then os.shutdown() end
- for i=1,#bgCo do
- local n=bgCo[i]
- --print(n)
- --print(i)
- if n then
- if (not (bgW[i] and bgW[i]~=e[1]) or e[1]=="terminate") and not bgP[i] then
- --print("Passing event to coroutine.")
- local ok,param=coroutine.resume(bgCo[i],unpack(e))
- --print(ok,":",param)
- --print(table.concat(e,":"))
- if not ok or coroutine.status(bgCo[i])=="dead" then
- if not ok then os.queueEvent("coroutine_error",i,err) else os.queueEvent("coroutine_death",i) end
- --print("Oh noes, coroutine #"..i.." has crashed!")
- --print(param)
- if i==1 then os.shutdown() else backLocal.killCo(i) end
- else
- bgW[i]=param or false
- --print(param)
- end
- end
- end
- end
- if #bgCo<#bgP+1 then for k,v in pairs(bgP) do if v then os.queueEvent("coroutine_resume",k) end end bgP={} end
- e={os.pullEventRaw()}
- --print("Recieved event")
- end
- end
- shell.exit()
- coroutineManage()
Advertisement
Add Comment
Please, Sign In to add comment