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()