This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
DannySMc's profile picture

Multitasking Help

Started by DannySMc, 07 April 2015 - 01:38 PM
DannySMc #1
Posted 07 April 2015 - 03:38 PM
Hello, yes I am back.

I made a script and got a little 'stuck' when I attempt to create a process, it seems to switch to it? But I only want it to make it, I don't wish to switch to it? Also it seems that it won't actually resume threads either and redraw the window? Any ideas? Thanks

Code:
Spoiler

--[[
	Name: Omicron
	Creator: DannySMc (dannysmc95)
	Platform: Lua Virtual Machine (JVM)
	Network: CCSystems

	Bugs / Errors:
	--------------
	https://github.com/dannysmc95/OmicronOS/issues -> Submit an issue, i shall fix it when I get a chance.

	License:
	--------
	This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
]]

-- Vars:
nTries = 0
nLoadAnim = 0
os.pullEvent = os.pullEventRaw
ccsysurl = "https://ccsystems.dannysmc.com/ccsystems.php"

-- Main Functions
omi = {}
omi.__index = omi

-- Draw Functions
omi.draw = {}
omi.draw.__index = omi.draw

-- core Functions
omi.core = {}
omi.core.__index = omi.core

-- Database (CCSystems) Functions
ccsys = {}
ccsys.__index = ccsys
ccsys.user = {}
ccsys.user.__index = ccsys.user


-- Start Controls
term.clear()
term.setCursorPos(1, 1)

function ccsys.user.login(username, password)
	local req = http.post(ccsysurl, "ccsys="..textutils.urlEncode(tostring("user")).."&cccmd="..textutils.urlEncode(tostring("login")).."&username="..textutils.urlEncode(tostring(username)).."&password="..textutils.urlEncode(tostring(password)))
	req = req.readAll()
	if req == "true" then
		return true
	else
		return req:sub(10)
	end
end

function ccsys.user.register(username, password, email)
	local req = http.post(ccsysurl, "ccsys="..textutils.urlEncode(tostring("user")).."&cccmd="..textutils.urlEncode(tostring("register")).."&username="..textutils.urlEncode(tostring(username)).."&password="..textutils.urlEncode(tostring(password)).."&email="..textutils.urlEncode(tostring(email)))
	req = req.readAll()
	if req == "true" then
		return true
	else
		return req:sub(10)
	end
end

local thread = {
	tRunning = {{},{},{}},
	sCurrent = nil,
	create = function( self, name, func )
		table.insert(self.tRunning[1], name)
		table.insert(self.tRunning[2], window.create(term.current(), 1, 1, 51, 19, false))
		if func then
			table.insert(self.tRunning[3], coroutine.create(func))
		else
			table.insert(self.tRunning[3], false)
		end
		return true
	end,
	resume = function( self, name )
		for k, v in ipairs(self.tRunning[1]) do
			if v == name then
				if self.sCurrent then
					for k1, v1 in ipairs(self.tRunning[1]) do
						if v1 == self.sCurrent then
							self.tRunning[2][k1].setVisible(false)
						end
					end
				end
				self.sCurrent = name
				self.tRunning[2][k].setVisible(true)
				self.tRunning[2][k].redraw()
				term.redirect(self.tRunning[2][k])
				if self.tRunning[3][k] then
					coroutine.resume(self.tRunning[3][k])
				end
			end
		end
	end,
	list = function( self )
		return self.tRunning[3]
	end,
	listall = function( self )
		return self.tRunning
	end,
	kill = function( self, name )
		for k,v in ipairs(self.tRunning[1]) do
			if k == name then
				table.remove(self.tRunning[1][k])
				table.remove(self.tRunning[2][k])
				table.remove(self.tRunning[3][k])
			end
		end
	end,
	status = function( self, name )
		for k,v in ipairs(self.tRunning[1]) do
			if k == name then
				
			end
		end
	end,
	current = function( self )
		return sCurrent
	end,
}

function omi.init()
    local ok, err = pcall( function ()
    	if http then
    		if fs.exists("progutils") then
    		os.loadAPI("progutils")
    		    return true
    		end
    		aa = aa or {}
    		local a = http.get("https://vault.dannysmc.com/lua/api/dannysmcapi.lua")
    		a = a.readAll()
    		local env = {}
    		a = loadstring(a)
    		local env = getfenv()
    		setfenv(a,env)
    		local status, err = pcall(a, unpack(aa))
    		if (not status) and err then
    		    printError("Error loading api")
    		    return false
    		end
    		local returned = err
    		env = env
    		_G["progutils"] = env
    		
    		omi.main()
    	else
    		omi.crash("HTTP needs to be enabled.")
    	end
    end)
    if not ok then
    	if nTries == 3 then
    		print("The program has tried 3 times to get the API, We shall run shell instead.")
        	sleep(5)
        	shell.run("clear")
        	shell.run("shell")
        else
        	nTries = nTries + 1
            print("Program, failed to download API, Re-trying...")
            sleep(1)
            omi.init()
        end
    end
end

function omi.crash(errcode)
	col.screen("white")
	draw.box(1, 51, 1, 1, " ", "grey", "grey")
	draw.textc(" Omicron has crashed", 1, false, "cyan", "grey")
	local sText = "An error has caused Omicron to crash, the error is below, if this happens again, please report the problem and error code to DannySMc (dannysmc95). Error code is stated below in red."
	for k,v in ipairs(data.wordwrap(sText, 51)) do
		draw.texta(v, 1, k+2, false, "lightGrey", "white")
	end

	for k, v in ipairs(data.wordwrap(errcode, 45)) do
		draw.texta(v, 4, k+8, false, "red", "white")
	end

	draw.texta("-< Press enter to restart or 's' for shell >-", 4, 19, false, "grey", "white")


	while true do
		local args = { os.pullEvent() }
		if args[1] == "key" then
			if args[2] == keys.enter then
				omicron.main()
			elseif args[2] == keys.s then
				shell.run("clear")
				shell.run("shell")
			end
		end
	end
end

function omi.draw.loadanimation(intx, inty, txtcol, bkgcol)
	if nLoadAnim == 0 then
		draw.texta("|", intx, inty, false, txtcol, bkgcol)
		nLoadAnim = 1
	elseif nLoadAnim == 1 then
		draw.texta("/", intx, inty, false, txtcol, bkgcol)
		nLoadAnim = 2
	elseif nLoadAnim == 2 then
		draw.texta("-", intx, inty, false, txtcol, bkgcol)
		nLoadAnim = 3
	elseif nLoadAnim == 3 then
		draw.texta("\\", intx, inty, false, txtcol, bkgcol)
		nLoadAnim = 0
	end
end

function omi.draw.bar(screenname)
	draw.box(1, 51, 1, 1, " ", "grey", "grey")
	draw.texta("Omicron:", 1, 1, false, "cyan", "grey")
	draw.texta(misc.time(), 47, 1, false, "lime", "grey")
	draw.texta("<", 45, 1, false, "lightBlue", "grey")
	if screenname then
		draw.texta(screenname, 10, 1, false, "white", "grey")
	end
end

function omi.draw.threads()
	draw.box(51, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(50, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(49, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(48, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(47, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(46, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(45, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(44, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(43, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(42, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(41, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(40, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)

	local list = thread:listall()

	for k,v in ipairs(list[1]) do
		draw.texta(v, 41, k+2, false, "white", "cyan")
	end

	while true do
		local args = { os.pullEvent("mouse_click") }
		if args[2] == 1 then
			if (args[3] >= 40 and args[3] <= 51) and (args[4] >= 3 and args[4] <= 18) then
				print(list[1][args[4] - 2])
				thread:resume(list[3][args[4] - 2])
				break
			end
		end
	end
end

function omi.main()
	if term.isColour() then
		local ok, err = pcall(function()
			thread:create("kernal", omi.startup)
   			local tFilters = {}
   			local tRunning = thread:list()
   			local tEventData = {}
   			while true do
    			for k, v in pairs( tRunning ) do
         			if tFilters[ k ] == tEventData[ 1 ] or tFilters[ k ] == nil then
          				_, tFilters[ k ] = coroutine.resume(v, unpack(tEventData))
         			end
    			end
    			tEventData = { os.pullEvent() }
   			end
		end)
		if not ok then
			omi.crash(err)
		end
	else
		omi.crash("Please use an advanced (gold) computer.")
	end
end

function omi.startup()
	col.screen("black")
	sleep(0.15)
	col.screen("grey")
	sleep(0.15)
	col.screen("lightGrey")
	sleep(0.30)
	draw.textc(" Omicron Operating System", 7, false, "white", "lightGrey")
	draw.textc(" Created By DannySMc", 19, false, "lightBlue", "lightGrey")
	sleep(0.30)
	omi.draw.loadanimation(26, 12, "red", "lightGrey")
	os.startTimer(0.15)

	local count = 0

	while true do
		local args = { os.pullEvent() }
		if args[1] == "timer" then
			count = count + 1
			omi.draw.loadanimation(26, 12, "red", "lightGrey")
			os.startTimer(0.15)
			if count == 10 then
				if fs.exists("/core/") then
					--thread:create("login", omi.core.login())
					--thread:goto("login")
					thread:create("desktop", omi.core.desktop())
					thread:goto("desktop")
					break
				else
					omi.core.install()
					break
				end
			end
		end
	end
end

function omi.core.install()
	-- Install Omicron OS
	local tFolderList = {"/core/","/core/users/","/core/apps/","/core/addons/","/core/settings/", "/core/bios/"}
	col.screen("lightGrey")
	draw.textc(" Omicron: Install", 1, false, "white", "lightGrey")
	draw.textc(" ----------------", 2, false, "white", "lightGrey")
	draw.texta("We are going to install Omicron, please wait...", 1, 4, false, "lightBlue", "lightGrey")
	sleep(1)
	draw.texta("Creating folders [0/"..#tFolderList.."]", 1, 6, false, "lightBlue", "lightGrey")
	for k, v in ipairs(tFolderList) do
		draw.texta("Creating folders ["..k.."/"..#tFolderList.."]", 1, 6, false, "lightBlue", "lightGrey")
		fs.makeDir(v)
		sleep(0.01)
	end

	local url = "https://ccsystems.dannysmc.com/programs/omicron/core"
	local tSettings = {
		"addons/gps.lua", 
		"addons/rednet.lua", 
		"apps/ink.lua", 
		"apps/luaide.lua", 
		"apps/sketch.lua", 
		"bios/osdata.lua", 
		"bios/bios.lua",
		"settings/omidata.lua",
		"settings/userdata.lua",
	}
	draw.texta("Downloading files [0/"..#tSettings.."]", 1, 7, false, "lightBlue", "lightGrey")
	for k, v in pairs(tSettings) do
		draw.texta("Downloading files ["..k.."/"..#tSettings.."]", 1, 7, false, "lightBlue", "lightGrey")
		local req = http.get(url.."/"..v)
		local temp = fs.open(fs.combine("/core/",v), "w")
		temp.write(req.readAll())
		temp.close()
	end

	local url = "https://ccsystems.dannysmc.com/ccsystems.php"
	draw.texta("Pinging CCSystems Database...", 1, 8, false, "lightBlue", "lightGrey")
	local req = http.post(url, "ccsys="..textutils.urlEncode(tostring("ping")))
	if req.readAll() == "true" then
		draw.texta("Server is online...", 1, 9, false, "lightBlue", "lightGrey")
	else
		draw.texta("Server is offline...", 1, 9, false, "lightBlue", "lightGrey")
	end

	sleep(1.5)

	thread:create("desktop", omi.core.desktop)
	thread:create("test", omi.core.desktop)
	thread:resume("desktop")

	--thread:create("login", omi.core.login())
	--thread:goto("login")

end

function omi.core.register()

end

function omi.core.login()
	if fs.exists("/core/settings/.saveuserdata") then
		local config = config.load("/core/settings/userdata.lua")
		username = config.username
		password = config.password
		local stat = ccsys.user.login(username, password)
		if stat then
			thread:create("desktop", omi.core.desktop)
			thread:goto("desktop")
		end
	else
		local savecred = false
		col.screen("white")
		draw.box(1, 51, 1, 1, " ", "grey", "grey")
		draw.texta("Omicron Login", 1, 1, false, "cyan", "grey")
		draw.texta(misc.time(), 47, 1, false, "lime", "grey")
		draw.box(1, 51, 2, 1, " ", "lightGrey", "lightGrey")
		draw.texta("         Register         |          Login          ", 1, 2, false, "white", "lightGrey")
		draw.texta("Login", 38, 2, false, "lime", "lightGrey")
	
		draw.texta("Username", 8, 5, false, "lightGrey", "white")
		draw.texta("Password", 8, 9, false, "lightGrey", "white")
		draw.texta("Login", 38, 2, false, "lime", "lightGrey")
		draw.box(8, 37, 6, 1, false, "cyan", "cyan")
		draw.box(8, 37, 10, 1, false, "cyan", "cyan")
		draw.texta("   Login   ", 8, 16, false, "white", "cyan")
		draw.texta("   Exit   ", 35, 16, false, "white", "cyan")
		draw.texta(" Toggle ", 8, 13, false, "white", "cyan")
		draw.texta("Save Credentials: "..tostring(savecred), 17, 13, false, "lightGrey", "white")
	
		while true do
			local args = { os.pullEvent() }
			if args[1] == "timer" then
				draw.texta(misc.time(), 47, 1, false, "lime", "grey")
			elseif args[1] == "mouse_click" then
				if args[2] == 1 then
					if (args[3] >= 1 and args[3] <= 25) and (args[4] == 2) then
						omi.core.register()
					elseif (args[3] >= 8 and args[3] <= 45) and (args[4] == 6) then
						col.set("white", "cyan")
						term.setCursorPos(8, 6)
						write("                                     ")
						term.setCursorPos(8, 6)
						write(": ")
						username = tostring(read())
					elseif (args[3] >= 8 and args[3] <= 45) and (args[4] == 10) then
						col.set("white", "cyan")
						term.setCursorPos(8, 10)
						write("                                     ")
						term.setCursorPos(8, 10)
						write(": ")
						password = crypt.sha256(tostring(read("*")))
					elseif (args[3] >= 8 and args[3] <= 18) and (args[4] == 16) then
						local stat = ccsys.user.login(username, password)
						if stat then
							draw.popup("Logged In!")
							sleep(1)
							thread:create("desktop", omi.core.desktop)
							thread:create("test", omi.core.desktop)
							thread:resume("desktop")
						else
							omi.core.login()
						end
					elseif (args[3] >= 35 and args[3] <= 44) and (args[4] == 16) then						
						shell.run("shell")
					end
				end
			end
		end
	end
end

function omi.core.uninstall()
	-- Uninstall Omicron OS
	
end

function omi.core.desktop()
	col.screen("white")
	omi.draw.bar("Desktop")
	col.set("black", "white")
	term.setCursorPos(1,3)
	local list = thread:listall()
	for k,v in pairs(list[1]) do
		print(v)
	end
	while true do
		local args = { os.pullEvent() }
		if args[1] == "timer" then
			omi.draw.bar("Desktop")
		elseif args[1] == "mouse_click" then
			if args[2] == 1 then
				if (args[3] == 45) and (args[4] == 1) then
					thread:create("switcher", omi.draw.threads)
				end
			end
		end
	end
end

function omi.core.testers()
	col.screen("white")
	omi.draw.bar("Desktop 2")
	col.set("black", "white")
	term.setCursorPos(1,3)
	local list = thread:listall()
	for k,v in pairs(list[1]) do
		print(v)
	end
	while true do
		local args = { os.pullEvent() }
		if args[1] == "timer" then
			omi.draw.bar("Desktop")
		elseif args[1] == "mouse_click" then
			if args[2] == 1 then
				if (args[3] == 45) and (args[4] == 1) then
					thread:create("switcher", omi.draw.threads)
				end
			end
		end
	end
end

function omi.test()
	col.screen("black")
	print("> Running coroutine")
	for i = 1, 10 do
		print(i)
	end

	print("Killing...")
	thread:resume("desktop")
end

omi.init()
KingofGamesYami #2
Posted 07 April 2015 - 03:52 PM
One thing that's weird, why are you setting

tbl.__index = tbl
?

It doesn't do anything special, and you never access the __index key.

Edit: Also, there is no thread:goto function that I can see…
Edited on 07 April 2015 - 01:58 PM
DannySMc #3
Posted 07 April 2015 - 04:19 PM
One thing that's weird, why are you setting

tbl.__index = tbl
?

It doesn't do anything special, and you never access the __index key.
Edit: Also, there is no thread:goto function that I can see…

Where is this sorry?:s

Also yes I mean to use resume, but still it keeps running a process? When I only create it?:s
KingofGamesYami #4
Posted 07 April 2015 - 04:58 PM
omni.startup uses thread:goto( "desktop" )

Also, why do you have windows if you never redirect or draw any of them?
DannySMc #5
Posted 07 April 2015 - 05:35 PM
omni.startup uses thread:goto( "desktop" )

Also, why do you have windows if you never redirect or draw any of them?

I do?:s When I use thread:resume() it automatically redraws the window.
A window is created with a coroutine and name when using thread:create().

Here is the updated code, hasn't changed; still running the co routines when I create them.
Spoiler

--[[
	Name: Omicron
	Creator: DannySMc (dannysmc95)
	Platform: Lua Virtual Machine (JVM)
	Network: CCSystems

	Bugs / Errors:
	--------------
	https://github.com/dannysmc95/OmicronOS/issues -> Submit an issue, i shall fix it when I get a chance.

	License:
	--------
	This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
]]

-- Vars:
nTries = 0
nLoadAnim = 0
os.pullEvent = os.pullEventRaw
ccsysurl = "https://ccsystems.dannysmc.com/ccsystems.php"

-- Main Functions
omi = {}
omi.__index = omi

-- Draw Functions
omi.draw = {}
omi.draw.__index = omi.draw

-- core Functions
omi.core = {}
omi.core.__index = omi.core

-- Database (CCSystems) Functions
ccsys = {}
ccsys.__index = ccsys
ccsys.user = {}
ccsys.user.__index = ccsys.user


-- Start Controls
term.clear()
term.setCursorPos(1, 1)

function ccsys.user.login(username, password)
	local req = http.post(ccsysurl, "ccsys="..textutils.urlEncode(tostring("user")).."&amp;cccmd="..textutils.urlEncode(tostring("login")).."&amp;username="..textutils.urlEncode(tostring(username)).."&amp;password="..textutils.urlEncode(tostring(password)))
	req = req.readAll()
	if req == "true" then
		return true
	else
		return req:sub(10)
	end
end

function ccsys.user.register(username, password, email)
	local req = http.post(ccsysurl, "ccsys="..textutils.urlEncode(tostring("user")).."&amp;cccmd="..textutils.urlEncode(tostring("register")).."&amp;username="..textutils.urlEncode(tostring(username)).."&amp;password="..textutils.urlEncode(tostring(password)).."&amp;email="..textutils.urlEncode(tostring(email)))
	req = req.readAll()
	if req == "true" then
		return true
	else
		return req:sub(10)
	end
end

local thread = {
	tRunning = {{},{},{}},
	sCurrent = nil,
	create = function( self, name, func )
		table.insert(self.tRunning[1], name)
		table.insert(self.tRunning[2], window.create(term.current(), 1, 1, 51, 19, false))
		if func then
			table.insert(self.tRunning[3], coroutine.create(func))
		else
			table.insert(self.tRunning[3], false)
		end
		return true
	end,
	resume = function( self, name )
		for k, v in ipairs(self.tRunning[1]) do
			if v == name then
				if self.sCurrent then
					for k1, v1 in ipairs(self.tRunning[1]) do
						if v1 == self.sCurrent then
							self.tRunning[2][k1].setVisible(false)
						end
					end
				end
				self.sCurrent = name
				self.tRunning[2][k].setVisible(true)
				self.tRunning[2][k].redraw()
				term.redirect(self.tRunning[2][k])
				if self.tRunning[3][k] then
					coroutine.resume(self.tRunning[3][k])
				end
			end
		end
	end,
	list = function( self )
		return self.tRunning[3]
	end,
	listall = function( self )
		return self.tRunning
	end,
	kill = function( self, name )
		for k,v in ipairs(self.tRunning[1]) do
			if k == name then
				table.remove(self.tRunning[1][k])
				table.remove(self.tRunning[2][k])
				table.remove(self.tRunning[3][k])
			end
		end
	end,
	status = function( self, name )
		for k,v in ipairs(self.tRunning[1]) do
			if k == name then
				
			end
		end
	end,
	current = function( self )
		return sCurrent
	end,
}

function omi.init()
    local ok, err = pcall( function ()
    	if http then
    		if fs.exists("progutils") then
    		os.loadAPI("progutils")
    		    return true
    		end
    		aa = aa or {}
    		local a = http.get("https://vault.dannysmc.com/lua/api/dannysmcapi.lua")
    		a = a.readAll()
    		local env = {}
    		a = loadstring(a)
    		local env = getfenv()
    		setfenv(a,env)
    		local status, err = pcall(a, unpack(aa))
    		if (not status) and err then
    		    printError("Error loading api")
    		    return false
    		end
    		local returned = err
    		env = env
    		_G["progutils"] = env
    		
    		omi.main()
    	else
    		omi.crash("HTTP needs to be enabled.")
    	end
    end)
    if not ok then
    	if nTries == 3 then
    		print("The program has tried 3 times to get the API, We shall run shell instead.")
        	sleep(5)
        	shell.run("clear")
        	shell.run("shell")
        else
        	nTries = nTries + 1
            print("Program, failed to download API, Re-trying...")
            sleep(1)
            omi.init()
        end
    end
end

function omi.crash(errcode)
	col.screen("white")
	draw.box(1, 51, 1, 1, " ", "grey", "grey")
	draw.textc(" Omicron has crashed", 1, false, "cyan", "grey")
	local sText = "An error has caused Omicron to crash, the error is below, if this happens again, please report the problem and error code to DannySMc (dannysmc95). Error code is stated below in red."
	for k,v in ipairs(data.wordwrap(sText, 51)) do
		draw.texta(v, 1, k+2, false, "lightGrey", "white")
	end

	for k, v in ipairs(data.wordwrap(errcode, 45)) do
		draw.texta(v, 4, k+8, false, "red", "white")
	end

	draw.texta("-< Press enter to restart or 's' for shell >-", 4, 19, false, "grey", "white")


	while true do
		local args = { os.pullEvent() }
		if args[1] == "key" then
			if args[2] == keys.enter then
				omicron.main()
			elseif args[2] == keys.s then
				shell.run("clear")
				shell.run("shell")
			end
		end
	end
end

function omi.draw.loadanimation(intx, inty, txtcol, bkgcol)
	if nLoadAnim == 0 then
		draw.texta("|", intx, inty, false, txtcol, bkgcol)
		nLoadAnim = 1
	elseif nLoadAnim == 1 then
		draw.texta("/", intx, inty, false, txtcol, bkgcol)
		nLoadAnim = 2
	elseif nLoadAnim == 2 then
		draw.texta("-", intx, inty, false, txtcol, bkgcol)
		nLoadAnim = 3
	elseif nLoadAnim == 3 then
		draw.texta("\\", intx, inty, false, txtcol, bkgcol)
		nLoadAnim = 0
	end
end

function omi.draw.bar(screenname)
	draw.box(1, 51, 1, 1, " ", "grey", "grey")
	draw.texta("Omicron:", 1, 1, false, "cyan", "grey")
	draw.texta(misc.time(), 47, 1, false, "lime", "grey")
	draw.texta("<", 45, 1, false, "lightBlue", "grey")
	if screenname then
		draw.texta(screenname, 10, 1, false, "white", "grey")
	end
end

function omi.draw.threads()
	draw.box(51, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(50, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(49, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(48, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(47, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(46, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(45, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(44, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(43, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(42, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(41, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)
	draw.box(40, 1, 2, 18, " ", "cyan", "cyan")
	sleep(0.05)

	local list = thread:listall()

	for k,v in ipairs(list[1]) do
		draw.texta(v, 41, k+2, false, "white", "cyan")
	end

	while true do
		local args = { os.pullEvent("mouse_click") }
		if args[2] == 1 then
			if (args[3] >= 40 and args[3] <= 51) and (args[4] >= 3 and args[4] <= 18) then
				print(list[1][args[4] - 2])
				thread:resume(list[3][args[4] - 2])
				break
			end
		end
	end
end

function omi.main()
	if term.isColour() then
		local ok, err = pcall(function()
			thread:create("kernal", omi.startup)
   			local tFilters = {}
   			local tRunning = thread:list()
   			local tEventData = {}
   			while true do
    			for k, v in pairs( tRunning ) do
         			if tFilters[ k ] == tEventData[ 1 ] or tFilters[ k ] == nil then
          				_, tFilters[ k ] = coroutine.resume(v, unpack(tEventData))
         			end
    			end
    			tEventData = { os.pullEvent() }
   			end
		end)
		if not ok then
			omi.crash(err)
		end
	else
		omi.crash("Please use an advanced (gold) computer.")
	end
end

function omi.startup()
	col.screen("black")
	sleep(0.15)
	col.screen("grey")
	sleep(0.15)
	col.screen("lightGrey")
	sleep(0.30)
	draw.textc(" Omicron Operating System", 7, false, "white", "lightGrey")
	draw.textc(" Created By DannySMc", 19, false, "lightBlue", "lightGrey")
	sleep(0.30)
	omi.draw.loadanimation(26, 12, "red", "lightGrey")
	os.startTimer(0.15)

	local count = 0

	while true do
		local args = { os.pullEvent() }
		if args[1] == "timer" then
			count = count + 1
			omi.draw.loadanimation(26, 12, "red", "lightGrey")
			os.startTimer(0.15)
			if count == 10 then
				if fs.exists("/core/") then
					--thread:create("login", omi.core.login())
					--thread:goto("login")
					thread:create("desktop", omi.core.desktop())
					thread:resume("desktop")
					break
				else
					omi.core.install()
					break
				end
			end
		end
	end
end

function omi.core.install()
	-- Install Omicron OS
	local tFolderList = {"/core/","/core/users/","/core/apps/","/core/addons/","/core/settings/", "/core/bios/"}
	col.screen("lightGrey")
	draw.textc(" Omicron: Install", 1, false, "white", "lightGrey")
	draw.textc(" ----------------", 2, false, "white", "lightGrey")
	draw.texta("We are going to install Omicron, please wait...", 1, 4, false, "lightBlue", "lightGrey")
	sleep(1)
	draw.texta("Creating folders [0/"..#tFolderList.."]", 1, 6, false, "lightBlue", "lightGrey")
	for k, v in ipairs(tFolderList) do
		draw.texta("Creating folders ["..k.."/"..#tFolderList.."]", 1, 6, false, "lightBlue", "lightGrey")
		fs.makeDir(v)
		sleep(0.01)
	end

	local url = "https://ccsystems.dannysmc.com/programs/omicron/core"
	local tSettings = {
		"addons/gps.lua", 
		"addons/rednet.lua", 
		"apps/ink.lua", 
		"apps/luaide.lua", 
		"apps/sketch.lua", 
		"bios/osdata.lua", 
		"bios/bios.lua",
		"settings/omidata.lua",
		"settings/userdata.lua",
	}
	draw.texta("Downloading files [0/"..#tSettings.."]", 1, 7, false, "lightBlue", "lightGrey")
	for k, v in pairs(tSettings) do
		draw.texta("Downloading files ["..k.."/"..#tSettings.."]", 1, 7, false, "lightBlue", "lightGrey")
		local req = http.get(url.."/"..v)
		local temp = fs.open(fs.combine("/core/",v), "w")
		temp.write(req.readAll())
		temp.close()
	end

	local url = "https://ccsystems.dannysmc.com/ccsystems.php"
	draw.texta("Pinging CCSystems Database...", 1, 8, false, "lightBlue", "lightGrey")
	local req = http.post(url, "ccsys="..textutils.urlEncode(tostring("ping")))
	if req.readAll() == "true" then
		draw.texta("Server is online...", 1, 9, false, "lightBlue", "lightGrey")
	else
		draw.texta("Server is offline...", 1, 9, false, "lightBlue", "lightGrey")
	end

	sleep(1.5)

	thread:create("desktop", omi.core.desktop)
	thread:create("test", omi.core.desktop)
	thread:resume("desktop")

	--thread:create("login", omi.core.login())
	--thread:goto("login")

end

function omi.core.register()

end

function omi.core.login()
	if fs.exists("/core/settings/.saveuserdata") then
		local config = config.load("/core/settings/userdata.lua")
		username = config.username
		password = config.password
		local stat = ccsys.user.login(username, password)
		if stat then
			thread:create("desktop", omi.core.desktop)
			thread:resume("desktop")
		end
	else
		local savecred = false
		col.screen("white")
		draw.box(1, 51, 1, 1, " ", "grey", "grey")
		draw.texta("Omicron Login", 1, 1, false, "cyan", "grey")
		draw.texta(misc.time(), 47, 1, false, "lime", "grey")
		draw.box(1, 51, 2, 1, " ", "lightGrey", "lightGrey")
		draw.texta("         Register         |          Login          ", 1, 2, false, "white", "lightGrey")
		draw.texta("Login", 38, 2, false, "lime", "lightGrey")
	
		draw.texta("Username", 8, 5, false, "lightGrey", "white")
		draw.texta("Password", 8, 9, false, "lightGrey", "white")
		draw.texta("Login", 38, 2, false, "lime", "lightGrey")
		draw.box(8, 37, 6, 1, false, "cyan", "cyan")
		draw.box(8, 37, 10, 1, false, "cyan", "cyan")
		draw.texta("   Login   ", 8, 16, false, "white", "cyan")
		draw.texta("   Exit   ", 35, 16, false, "white", "cyan")
		draw.texta(" Toggle ", 8, 13, false, "white", "cyan")
		draw.texta("Save Credentials: "..tostring(savecred), 17, 13, false, "lightGrey", "white")
	
		while true do
			local args = { os.pullEvent() }
			if args[1] == "timer" then
				draw.texta(misc.time(), 47, 1, false, "lime", "grey")
			elseif args[1] == "mouse_click" then
				if args[2] == 1 then
					if (args[3] >= 1 and args[3] <= 25) and (args[4] == 2) then
						omi.core.register()
					elseif (args[3] >= 8 and args[3] <= 45) and (args[4] == 6) then
						col.set("white", "cyan")
						term.setCursorPos(8, 6)
						write("                                     ")
						term.setCursorPos(8, 6)
						write(": ")
						username = tostring(read())
					elseif (args[3] >= 8 and args[3] <= 45) and (args[4] == 10) then
						col.set("white", "cyan")
						term.setCursorPos(8, 10)
						write("                                     ")
						term.setCursorPos(8, 10)
						write(": ")
						password = crypt.sha256(tostring(read("*")))
					elseif (args[3] >= 8 and args[3] <= 18) and (args[4] == 16) then
						local stat = ccsys.user.login(username, password)
						if stat then
							draw.popup("Logged In!")
							sleep(1)
							thread:create("desktop", omi.core.desktop)
							thread:create("test", omi.core.desktop)
							thread:resume("desktop")
						else
							omi.core.login()
						end
					elseif (args[3] >= 35 and args[3] <= 44) and (args[4] == 16) then						
						shell.run("shell")
					end
				end
			end
		end
	end
end

function omi.core.uninstall()
	-- Uninstall Omicron OS
	
end

function omi.core.desktop()
	col.screen("white")
	omi.draw.bar("Desktop")
	col.set("black", "white")
	term.setCursorPos(1,3)
	local list = thread:listall()
	for k,v in pairs(list[1]) do
		print(v)
	end
	while true do
		local args = { os.pullEvent() }
		if args[1] == "timer" then
			omi.draw.bar("Desktop")
		elseif args[1] == "mouse_click" then
			if args[2] == 1 then
				if (args[3] == 45) and (args[4] == 1) then
					thread:create("switcher", omi.draw.threads)
				end
			end
		end
	end
end

function omi.core.testers()
	col.screen("white")
	omi.draw.bar("Desktop 2")
	col.set("black", "white")
	term.setCursorPos(1,3)
	local list = thread:listall()
	for k,v in pairs(list[1]) do
		print(v)
	end
	while true do
		local args = { os.pullEvent() }
		if args[1] == "timer" then
			omi.draw.bar("Desktop")
		elseif args[1] == "mouse_click" then
			if args[2] == 1 then
				if (args[3] == 45) and (args[4] == 1) then
					thread:create("switcher", omi.draw.threads)
				end
			end
		end
	end
end

function omi.test()
	col.screen("black")
	print("> Running coroutine")
	for i = 1, 10 do
		print(i)
	end

	print("Killing...")
	thread:resume("desktop")
end
omi.init()
Edited on 07 April 2015 - 03:36 PM
KingofGamesYami #6
Posted 07 April 2015 - 06:29 PM

        resume = function( self, name )
                for k, v in ipairs(self.tRunning[1]) do
                        if v == name then
                                if self.sCurrent then
                                        for k1, v1 in ipairs(self.tRunning[1]) do
                                                if v1 == self.sCurrent then
                                                        self.tRunning[2][k1].setVisible(false)
                                                end
                                        end
                                end
                                self.sCurrent = name
                                self.tRunning[2][k].setVisible(true)
                                self.tRunning[2][k].redraw()
                                term.redirect(self.tRunning[2][k])
                                if self.tRunning[3][k] then
                                        coroutine.resume(self.tRunning[3][k])
                                end
                        end
                end
        end,
…is so completely wrong based on what I've told you in other topics, I just don't know anymore…

To clarify:

#1 - you resume only a single coroutine
#2 - you never use events
#3 - you never capture filters
#4 - this works exactly the same as if you had never turned it into a coroutine and just called it, except it doesn't get to finish what it's doing before it returns/yields… and 1-3
Edited on 07 April 2015 - 05:38 PM
Bomb Bloke #7
Posted 07 April 2015 - 10:51 PM
If memory serves, he doesn't want his multi-tasking engine to run "background processes". Rather, he wants all processes except the current one to basically be paused until they become the active process again.

Which unfortunately leads to issues with timer events, which can't really be discarded. Putting aside that he's still discarding all events.

If it helps, think of a coroutine as a function which returns every single time it yields, but until it returns "properly", it can be resumed from where it left off. Every time it yields, it's able to return data - think of coroutine.yield("char") (or os.pullEvent("char"), they boil down to nearly the same thing) as being sorta like return "char" - and every time it resumes, it expects data back - think of coroutine.resume(myFunc, "char", "p") as being sorta like myFunc("char", "p").
DannySMc #8
Posted 07 April 2015 - 10:58 PM

		resume = function( self, name )
				for k, v in ipairs(self.tRunning[1]) do
						if v == name then
								if self.sCurrent then
										for k1, v1 in ipairs(self.tRunning[1]) do
												if v1 == self.sCurrent then
														self.tRunning[2][k1].setVisible(false)
												end
										end
								end
								self.sCurrent = name
								self.tRunning[2][k].setVisible(true)
								self.tRunning[2][k].redraw()
								term.redirect(self.tRunning[2][k])
								if self.tRunning[3][k] then
										coroutine.resume(self.tRunning[3][k])
								end
						end
				end
		end,
…is so completely wrong based on what I've told you in other topics, I just don't know anymore…

To clarify:

#1 - you resume only a single coroutine
#2 - you never use events
#3 - you never capture filters
#4 - this works exactly the same as if you had never turned it into a coroutine and just called it, except it doesn't get to finish what it's doing before it returns/yields… and 1-3

Oh I want to be able to like say thread:resume(processname) and then it runs that process, and then I can switch between multiple ones? I think?
KingofGamesYami #9
Posted 07 April 2015 - 11:45 PM
What you'd do is you'd have a function that sets a variable, then yield. The manager (which is running aforementioned code) uses that variable to determine which events to give to what. This means everything is running at the same time, you can't stop something from running because that causes major problems.