sPhone/src/sPhone.lua

1019 lines
26 KiB
Lua
Raw Normal View History

2015-07-31 20:27:52 +02:00
local function kernel()
2015-08-19 11:45:35 +02:00
_G.sPhone = {
2016-11-04 19:38:13 +01:00
version = "Alpha 3.6.2",
2015-12-24 16:17:55 +01:00
user = "Guest",
2015-09-26 11:32:04 +02:00
devMode = false,
2016-09-02 12:57:19 +02:00
mainTerm = term.current(),
safeMode = false,
}
if safemode then
sPhone.safeMode = true
_G.safemode = nil
end
sPhone.defaultApps = {
["home"] = "/.sPhone/apps/home",
2015-08-19 11:45:35 +02:00
}
2016-03-20 17:27:01 +01:00
sPhone.theme = { --Default colors
["header"] = colors.blue,
["headerText"] = colors.white,
["text"] = colors.black,
["background"] = "",
["backgroundColor"] = colors.white,
["window.background"] = colors.lightBlue,
["window.side"] = colors.blue,
["window.button"] = colors.lightBlue,
["window.text"] = colors.white,
}
sPhone.defaultTheme = sPhone.theme
2015-11-18 16:54:17 +01:00
if not fs.exists("/.sPhone/apis") then
fs.makeDir("/.sPhone/apis")
end
for k, v in pairs(fs.list("/.sPhone/apis")) do
if not fs.isDir("/.sPhone/apis/"..v) then
os.loadAPI("/.sPhone/apis/"..v)
end
end
2016-11-01 17:13:41 +01:00
2016-11-02 21:13:23 +01:00
if not config.write("/.sPhone/config/sPhone","newConfigFormat",true) then
os.pullEvent = os.pullEventRaw
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.clear()
term.setCursorPos(1,1)
print("sPhone")
printError("WARNING")
print("Config Format Changed")
print("")
print("We adopted a new format for all config files.")
print("To continue using sPhone all config data (.sPhone/config) will be deleted")
print("Y/N")
while true do
local _,k = os.pullEvent("char")
if string.lower(k) == "y" then
fs.delete("/.sPhone/config")
print("Config deleted")
print("Rebooting...")
sleep(1)
os.reboot()
elseif string.lower(k) == "n" then
print("Cannot delete config without user authorization")
print("Delete aborted")
print("Shuttind down...")
sleep(1)
os.shutdown()
end
end
end
2016-11-01 17:13:41 +01:00
if not fs.exists("/.sPhone/system") then
fs.makeDir("/.sPhone/system")
end
for k, v in pairs(fs.list("/.sPhone/system")) do
if not fs.isDir("/.sPhone/system/"..v) then
dofile("/.sPhone/system/"..v)
end
end
2016-05-16 19:32:01 +02:00
local sPath = shell.path()
2016-07-01 12:19:25 +02:00
sPath = sPath..":/bin"
2016-05-16 19:32:01 +02:00
shell.setPath(sPath)
2015-11-18 16:54:17 +01:00
2015-09-22 18:49:37 +02:00
if not fs.exists("/.sPhone/autorun") then
fs.makeDir("/.sPhone/autorun")
end
2015-09-23 14:09:20 +02:00
term.setBackgroundColor(colors.white)
term.clear()
2015-10-10 16:18:02 +02:00
term.setCursorPos(1,1)
2015-09-22 18:49:37 +02:00
for k, v in pairs(fs.list("/.sPhone/autorun")) do
2015-09-23 14:09:20 +02:00
term.setTextColor(colors.black)
2015-09-23 14:04:24 +02:00
if not fs.isDir("/.sPhone/autorun/"..v) then
2016-09-02 12:57:19 +02:00
if not sPhone.safemode then
2015-11-18 16:54:17 +01:00
local f = fs.open("/.sPhone/autorun/"..v,"r")
local script = f.readAll()
f.close()
print("Loading script "..v)
sleep(0)
local ok, err = pcall(function() setfenv(loadstring(script),getfenv())() end)
if not ok then
term.setTextColor(colors.red)
print("Script error: "..v..": "..err)
fs.move("/.sPhone/autorun/"..v, "/.sPhone/autorun/disabled/"..v)
term.setTextColor(colors.blue)
print(v.." disabled to prevent errors")
sleep(0.5)
end
else
print("Script "..v.." not loaded because Safe Mode")
sleep(0)
2015-09-23 14:04:24 +02:00
end
end
2015-09-22 18:49:37 +02:00
end
2015-09-09 17:31:27 +02:00
if runningOnStartup then
fs.open("/startup","r")
end
2016-11-02 21:02:04 +01:00
if config.read("/.sPhone/config/sPhone","username") then
sPhone.user = config.read("/.sPhone/config/sPhone","username")
2015-08-19 11:45:35 +02:00
end
2015-11-10 16:48:13 +01:00
if not fs.exists("/.sPhone/config/sPhone") then
config.write("/.sPhone/config/sPhone","devMode",false)
end
sPhone.devMode = config.read("/.sPhone/config/sPhone","devMode")
if sPhone.devMode then
sPhone.crash = crash
2015-08-19 11:45:35 +02:00
end
2015-12-15 18:54:15 +01:00
_G.crash = nil
2016-03-20 17:27:01 +01:00
2015-08-19 11:45:35 +02:00
function os.version()
return "sPhone "..sPhone.version
end
2016-03-19 23:01:02 +01:00
function sPhone.getSize()
return term.getSize()
end
2016-03-20 17:27:01 +01:00
local fileTheme = "/.sPhone/config/theme"
if fs.exists(fileTheme) then
sPhone.theme["header"] = config.read(fileTheme, "header")
sPhone.theme["headerText"] = config.read(fileTheme, "headerText")
sPhone.theme["text"] = config.read(fileTheme, "text")
sPhone.theme["background"] = config.read(fileTheme, "background")
sPhone.theme["backgroundColor"] = config.read(fileTheme, "backgroundColor")
sPhone.theme["window.background"] = config.read(fileTheme, "window.background")
sPhone.theme["window.side"] = config.read(fileTheme, "window.side")
sPhone.theme["window.button"] = config.read(fileTheme, "window.button")
sPhone.theme["window.text"] = config.read(fileTheme, "window.text")
else
for k, v in pairs(sPhone.theme) do
config.write(fileTheme, k, v)
end
end
function sPhone.applyTheme(id, value)
if not value or not id then
error("bad arguement: double expected, got nil",2)
end
sPhone.theme[id] = value
config.write(fileTheme, id, value)
end
function sPhone.getTheme(id)
if not id then
error("bad arguement: double expected, got nil",2)
end
local n = config.read(fileTheme, id)
if n then
return n
end
return sPhone.defaultTheme[id]
2016-03-20 17:27:01 +01:00
end
2016-09-02 12:57:19 +02:00
function sPhone.setDefaultApp(app, path)
if not path then
error("got nil",2)
end
sPhone.defaultApps[app] = path
config.write("/.sPhone/config/defaultApps",app,path)
end
function sPhone.getDefaultApp(app)
if not app then
error("got nil",2)
end
local n = config.read("/.sPhone/config/defaultApps",app)
return n
end
if not fs.exists("/.sPhone/config/defaultApps") then
sPhone.setDefaultApp("home","/.sPhone/apps/home")
end
2016-11-01 17:13:41 +01:00
function string.getExtension(name)
local ext = ""
local exten = false
name = string.reverse(name)
for i = 1, #name do
local s = string.sub(name,i,i)
if s == "." then
ch = i - 1
exten = true
break
end
end
if exten then
ext = string.sub(name, 1, ch)
return string.reverse(ext)
else
return nil
end
end
2016-10-30 21:37:45 +01:00
function sPhone.list(path, opt)
opt = opt or {}
opt.bg1 = opt.bg1 or sPhone.getTheme("backgroundColor")
opt.fg1 = opt.fg1 or sPhone.getTheme("text")
opt.fg1b = opt.fg1b or colors.lime
opt.bg2 = opt.bg2 or sPhone.getTheme("header")
opt.fg2 = opt.fg2 or sPhone.getTheme("headerText")
opt.bg3 = opt.bg3 or colors.red
opt.fg3 = opt.fg3 or sPhone.getTheme("headerText")
opt.output = opt.output or true
opt.list = opt.list or false
if not path then
path = ""
end
if not fs.isDir(path) and not opt.list then
error("Invalid path")
end
local scroll = 0
local items
local w, h
local function rebuild()
local files, dirs = {}, {}
items = {}
local flist
if not opt.list then
flist = fs.list(path)
else
flist = opt.list
end
for i, v in ipairs(flist) do
if fs.isDir(fs.combine(path, v)) then
table.insert(dirs, v)
else
table.insert(files, v)
end
end
table.sort(files)
table.sort(dirs)
for i, v in ipairs(dirs) do
table.insert(items, v)
end
for i, v in ipairs(files) do
table.insert(items, v)
end
scroll = 0
end
rebuild()
local setVisible = term.current().setVisible
or function()end
local function redraw()
w, h = term.getSize()
setVisible(false)
term.setBackgroundColor(opt.bg1)
term.clear()
for i = scroll + 1, h + scroll - 1 do
local str = items[i]
if str then
term.setCursorPos(2, 1 + i - scroll)
local isDir = fs.isDir(fs.combine(path, str))
term.setTextColor(isDir and opt.fg1b or opt.fg1)
local _w = w - (isDir and 2 or 1)
if #str > _w then
str = str:sub(1, _w - 2) .. ".."
end
if isDir then
str = str .. "/"
end
term.write(str)
end
end
term.setBackgroundColor(opt.bg2)
term.setTextColor(opt.fg2)
term.setCursorPos(1, 1)
term.clearLine()
local _path = path .. "/"
if #_path > w - 2 then
_path = ".." .. _path:sub(-w + 4)
end
term.write(_path)
term.setBackgroundColor(opt.bg3)
term.setTextColor(opt.fg3)
term.setCursorPos(w, 1)
term.write("X")
term.setCursorPos(w, 2)
term.write("^")
term.setCursorPos(w, h)
term.write("v")
setVisible(true)
end
while true do
redraw()
local ev = {os.pullEventRaw()}
if ev[1] == "terminate" then
return nil
elseif ev[1] == "mouse_scroll" and ev[4] > 1 then
scroll = scroll + ev[2]
elseif ev[1] == "mouse_click" and ev[2] == 1 then
if ev[3] == w then
if ev[4] == 1 then
return nil
elseif ev[4] == 2 then
scroll = scroll - 1
elseif ev[4] == h then
scroll = scroll + 1
end
elseif ev[3] < w and ev[4] == 1 then
path = fs.getDir(path)
if path == ".." then
path = ""
end
rebuild()
elseif ev[3] < w and ev[4] > 1 then
local item = items[ev[4] + scroll - 1]
if item then
local fullPath = fs.combine(path, item)
if fs.isDir(fullPath) then
path = fullPath
rebuild()
else
if opt.output then
return fullPath
end
end
end
end
end
scroll = math.min(math.max(0, scroll), math.max(0, #items - h + 1))
end
end
2015-08-19 11:45:35 +02:00
local function clear()
2016-03-20 17:27:01 +01:00
term.setBackgroundColor(sPhone.theme["backgroundColor"])
2015-08-19 11:45:35 +02:00
term.clear()
term.setCursorPos(1,1)
2016-03-20 17:27:01 +01:00
term.setTextColor(sPhone.theme["text"])
2015-08-19 11:45:35 +02:00
end
sPhone.forceShutdown = os.shutdown
sPhone.forceReboot = os.reboot
function os.shutdown()
2016-10-31 19:55:07 +01:00
local w, h = term.getSize()
local text = "Shutting down"
local x = math.ceil(w/2)-math.ceil(#text/2)+1
local y = math.ceil(h/2)
2016-03-19 23:01:02 +01:00
sPhone.inHome = false
2015-08-19 11:45:35 +02:00
os.pullEvent = os.pullEventRaw
2016-10-31 19:55:07 +01:00
local function printMsg(color)
term.setBackgroundColor(color)
term.setTextColor(colors.white)
term.clear()
term.setCursorPos(x,y)
print(text)
sleep(0.1)
2015-08-19 11:45:35 +02:00
end
2016-10-31 19:55:07 +01:00
printMsg(colors.white)
printMsg(colors.lightGray)
printMsg(colors.gray)
printMsg(colors.black)
sleep(0.6)
sPhone.forceShutdown()
2015-08-19 11:45:35 +02:00
end
function os.reboot()
2016-10-31 19:55:07 +01:00
local w, h = term.getSize()
2016-10-31 19:56:39 +01:00
local text = "Rebooting"
2016-10-31 19:55:07 +01:00
local x = math.ceil(w/2)-math.ceil(#text/2)+1
local y = math.ceil(h/2)
2016-03-19 23:01:02 +01:00
sPhone.inHome = false
2015-08-19 11:45:35 +02:00
os.pullEvent = os.pullEventRaw
2016-10-31 19:55:07 +01:00
local function printMsg(color)
term.setBackgroundColor(color)
term.setTextColor(colors.white)
term.clear()
term.setCursorPos(x,y)
print(text)
sleep(0.1)
2015-08-19 11:45:35 +02:00
end
2016-10-31 19:55:07 +01:00
printMsg(colors.white)
printMsg(colors.lightGray)
printMsg(colors.gray)
printMsg(colors.black)
sleep(0.6)
sPhone.forceReboot()
2015-08-19 11:45:35 +02:00
end
2015-09-21 21:34:14 +02:00
2016-03-19 23:31:00 +01:00
function sPhone.header(title, butt)
if not title then
title = "sPhone"
end
2015-09-21 21:34:14 +02:00
2016-03-19 23:31:00 +01:00
local w, h = term.getSize()
2016-03-20 17:27:01 +01:00
paintutils.drawLine(1,1,w,1, sPhone.theme["header"])
term.setTextColor(sPhone.theme["headerText"])
2016-03-19 23:31:00 +01:00
term.setCursorPos(1,1)
write(" "..title)
term.setCursorPos(w,1)
if butt then
write(butt)
2016-03-19 23:01:02 +01:00
end
2016-03-20 17:27:01 +01:00
term.setBackgroundColor(sPhone.theme["backgroundColor"])
term.setTextColor(sPhone.theme["text"])
2016-03-19 23:31:00 +01:00
term.setCursorPos(1,3)
2015-09-21 21:34:14 +02:00
end
function sPhone.menu(items, title, closeButton)
local function cprint(text)
if type(text) ~= 'table' then
text = {text}
end
local w, h = term.getSize()
for i=1,#text do
local x, y = term.getCursorPos()
term.setCursorPos(math.floor(w/2)-math.floor(text[i]:len()/2), y)
print(text[i])
end
end
local function clear()
term.clear()
term.setCursorPos(1, 1)
end
local termWidth, termHeight = term.getSize()
local drawSize = termHeight - 6
local function maxPages()
local itemCount = #items
local pageCount = 0
while itemCount > 0 do
itemCount = itemCount - drawSize
pageCount = pageCount + 1
end
return pageCount
end
local function iif(cond, trueval, falseval)
if cond then
return trueval
else
return falseval
end
end
local function pagedItems()
local ret = {}
for i = 1, maxPages() do
local tmp = {}
local nElements = 0
for j = drawSize*(i-1)+1, iif(drawSize*(i+1) > #items, #items, drawSize*(i+1)) do
if nElements < drawSize then
table.insert(tmp, items[j])
nElements = nElements + 1
end
end
table.insert(ret, tmp)
end
return ret
end
local selected = 1
if start then
selected = start
end
local page = 1
local function redraw()
2016-03-20 17:27:01 +01:00
term.setBackgroundColor(sPhone.theme["backgroundColor"])
term.setTextColor(sPhone.theme["text"])
2015-09-21 21:34:14 +02:00
term.clear()
term.setCursorPos(1,1)
2016-03-19 23:31:00 +01:00
sPhone.header("",closeButton)
2015-09-21 21:34:14 +02:00
term.setCursorPos(1,3)
if not title then
2016-03-19 23:01:02 +01:00
title = " sPhone"
2015-09-21 21:34:14 +02:00
end
cprint(" "..title)
if moreTitle then
head = moreTitle
else
head = {"\n",}
if not allowNil or allowNil == true then
--head[3] = 'Terminate to cancel.'
end
end
for i=1,#head do
print(head[i])
end
if maxPages() > 1 then
pages = "<- (page "..page.." of "..maxPages()..") ->"
print(pages)
end
for i = 1, #pagedItems()[page] do
if selected == drawSize*(page-1)+i then
2016-03-20 17:27:01 +01:00
term.setBackgroundColor(sPhone.theme["backgroundColor"])
term.setTextColor(sPhone.theme["text"])
2015-09-21 21:34:14 +02:00
else
2016-03-20 17:27:01 +01:00
term.setBackgroundColor(sPhone.theme["backgroundColor"])
term.setTextColor(sPhone.theme["text"])
2015-09-21 21:34:14 +02:00
end
term.clearLine()
cprint(iif(selected == drawSize*(page-1)+i,"","").." "..pagedItems()[page][i])
2016-03-20 17:27:01 +01:00
term.setBackgroundColor(sPhone.theme["backgroundColor"])
term.setTextColor(sPhone.theme["text"])
2015-09-21 21:34:14 +02:00
end
end
local function changePage(pW)
if pW == 1 and page < maxPages() then
page = page + 1
if selected + drawSize > #items then
selected = #items
else
selected = selected + drawSize
end
elseif pW == -1 and page > 1 then
page = page - 1
if selected - drawSize < 1 then
selected = 1
else
selected = selected - drawSize
end
end
end
while true do
redraw()
local eventData = {os.pullEventRaw()}
if eventData[1] == 'mouse_click' then
2016-03-19 23:01:02 +01:00
if eventData[4] == 1 and eventData[3] == termWidth then
2015-09-21 21:34:14 +02:00
return false, 0
2016-03-19 23:01:02 +01:00
elseif eventData[4] > 3 then
2015-09-21 21:34:14 +02:00
clear()
selected = (eventData[4]-6+((page-1)*drawSize))+1
if selected then
return items[selected], selected
end
end
end
end
end
2015-08-19 11:45:35 +02:00
2015-09-02 14:47:30 +02:00
function sPhone.yesNo(title, desc, hideUser)
2015-11-02 22:08:38 +01:00
term.setCursorBlink(false)
2016-03-20 17:27:01 +01:00
term.setBackgroundColor(sPhone.theme["backgroundColor"])
2015-08-24 20:49:02 +02:00
term.clear()
term.setCursorPos(1,1)
2016-03-20 17:27:01 +01:00
term.setTextColor(sPhone.theme["text"])
2015-08-24 20:49:02 +02:00
local w, h = term.getSize()
2016-03-20 17:27:01 +01:00
paintutils.drawLine(1,1,w,1, sPhone.theme["header"])
term.setTextColor(sPhone.theme["headerText"])
2015-08-24 20:49:02 +02:00
term.setCursorPos(1,1)
2015-09-02 14:48:37 +02:00
if not hideUser then
2015-09-02 14:47:30 +02:00
if not sPhone.user then
write(" sPhone")
else
write(" "..sPhone.user)
end
2015-08-24 20:49:02 +02:00
end
term.setCursorPos(1,3)
2016-03-20 17:27:01 +01:00
term.setBackgroundColor(sPhone.theme["backgroundColor"])
term.setTextColor(sPhone.theme["text"])
2015-11-27 22:39:21 +01:00
visum.align("center", " "..title, false, 3)
2015-08-24 20:49:02 +02:00
if desc then
2016-11-02 21:02:04 +01:00
term.setCursorPos(2,6)
print(desc)
2015-08-24 20:49:02 +02:00
end
paintutils.drawFilledBox(3, 16, 9, 18, colors.green)
paintutils.drawFilledBox(18, 16, 24, 18, colors.red)
term.setTextColor(colors.white)
term.setCursorPos(5,17)
term.setBackgroundColor(colors.green)
write("Yes")
term.setCursorPos(20,17)
term.setBackgroundColor(colors.red)
write("No")
while true do
local _,_,x,y = os.pullEvent("mouse_click")
if (x > 2 and y > 15) and (x < 10 and y < 19) then
return true
elseif (x > 17 and y > 15) and (x < 25 and y < 19) then
return false
end
end
end
2015-08-19 11:45:35 +02:00
function sPhone.winOk(fmessage, smessage, bg, side, text, button)
if not fmessage then
fmessage = ""
end
2015-09-16 15:39:18 +02:00
if not smessage then
smessage = ""
end
2015-08-19 11:45:35 +02:00
if not bg then
2016-03-20 17:27:01 +01:00
bg = sPhone.theme["window.background"]
2015-08-19 11:45:35 +02:00
end
if not text then
2016-03-20 17:27:01 +01:00
text = sPhone.theme["window.text"]
2015-08-19 11:45:35 +02:00
end
if not button then
2016-03-20 17:27:01 +01:00
button = sPhone.theme["window.button"]
2015-08-19 11:45:35 +02:00
end
if not side then
2016-03-20 17:27:01 +01:00
side = sPhone.theme["window.side"]
2015-08-19 11:45:35 +02:00
end
2015-11-02 22:08:38 +01:00
term.setCursorBlink(false)
2015-09-16 15:39:18 +02:00
if #fmessage >= #smessage then
local w, h = term.getSize
term.setBackgroundColor(side)
2015-10-09 17:51:06 +02:00
paintutils.drawBox(12 - math.ceil(#fmessage / 2), 5, 15 + math.ceil(#fmessage / 2), 10, side)
2015-09-16 15:39:18 +02:00
term.setBackgroundColor(bg)
2015-10-09 17:51:06 +02:00
paintutils.drawFilledBox(13 - math.ceil(#fmessage / 2), 6, 14 + math.ceil(#fmessage / 2), 9, bg)
2015-09-16 15:39:18 +02:00
term.setCursorPos(14 - math.ceil(#fmessage / 2), 7)
term.setTextColor(text)
write(fmessage)
term.setCursorPos(14 - math.ceil(#smessage / 2), 8)
write(smessage)
else
local w, h = term.getSize
term.setBackgroundColor(side)
2015-10-09 17:51:06 +02:00
paintutils.drawBox(12 - math.ceil(#smessage / 2), 5, 15 + math.ceil(#smessage / 2), 10, side)
2015-09-16 15:39:18 +02:00
term.setBackgroundColor(bg)
2015-10-09 17:51:06 +02:00
paintutils.drawFilledBox(13 - math.ceil(#smessage / 2), 6, 14 + math.ceil(#smessage / 2), 9, bg)
2015-09-16 15:39:18 +02:00
term.setCursorPos(14 - math.ceil(#fmessage / 2), 7)
term.setTextColor(text)
write(fmessage)
2015-08-19 11:45:35 +02:00
term.setCursorPos(14 - math.ceil(#smessage / 2), 8)
write(smessage)
end
term.setCursorPos(13,10)
term.setBackgroundColor(button)
write("Ok")
while true do
local e, k, x,y = os.pullEvent()
if e == "mouse_click" then
if y == 10 then
if x == 13 or x == 14 then
return
end
end
elseif e == "key" then
if k == 28 then
return
end
end
end
end
2016-03-20 17:27:01 +01:00
function sPhone.colorPicker(message, old) -- From Impulse
local current = math.log(old) / math.log(2)
-- first line is already code wizardry
local function redraw()
term.setBackgroundColour(sPhone.theme["backgroundColor"])
term.clear()
sPhone.header(message)
term.setCursorPos(2,5)
term.setTextColor(colors.white)
term.setBackgroundColor(colors.lime)
write(" Ok ")
term.setCursorPos(7,5)
term.setTextColor(colors.white)
term.setBackgroundColor(colors.red)
write(" Cancel ")
term.setTextColor(colors.black)
term.setCursorPos(2, 3)
for i = 0, 15 do
term.setBackgroundColour(2^i)
term.write(i == current and "#" or ":")
end
end
while true do
redraw()
local ev = {os.pullEvent()}
if ev[1] == "key" and ev[2] == keys.enter then
return 2^current
elseif ev[1] == "mouse_click" then
if ev[4] == 3 and ev[3] >= 2 and ev[3] <= 17 then
current = ev[3] - 2 % 16
elseif ev[4] == 5 and ev[3] >= 2 and ev[3] <= 6 then
return 2^current
elseif ev[4] == 5 and ev[3] >= 7 and ev[3] <= 14 then
return old
end
end
end
end
sPhone.colourPicker = sPhone.colorPicker -- For UK
2016-11-01 17:13:41 +01:00
function sPhone.install(spk)
2016-11-01 21:28:11 +01:00
if not fs.exists("/.sPhone/config/spklist") then
local f = fs.open("/.sPhone/config/spklist","w")
f.write("{}")
f.close()
end
2016-11-01 17:13:41 +01:00
if string.getExtension(spk) == "spk" then
if fs.exists(spk) and not fs.isDir(spk) then
local f = fs.open(spk,"r")
local script = f.readAll()
f.close()
script = textutils.unserialize(script)
if not script then
error("spk corrupted",2)
end
local function writeFile(patha,contenta)
local file = fs.open(patha,"w")
file.write(contenta)
file.close()
end
function writeDown(inputa,dira)
for i,v in pairs(inputa) do
if type(v) == "table" then
writeDown(v,dira.."/"..i)
elseif type(v) == "string" then
writeFile(dira.."/"..i,v)
end
end
end
local _config = textutils.unserialize(script.config)
if not _config.id then
2016-11-03 13:19:34 +01:00
error("SPK: id not found",2)
end
if not _config.main then
2016-11-03 13:19:34 +01:00
error("SPK: main not found",2)
end
writeDown(textutils.unserialize(script.files),"/.sPhone/apps/spk/".._config.id)
local f = fs.open("/.sPhone/apps/spk/".._config.id.."/.spk","w")
f.write(textutils.serialize(_config))
2016-11-01 17:13:41 +01:00
f.close()
local f = fs.open("/.sPhone/config/spklist","r")
local lists = f.readAll()
f.close()
lists = textutils.unserialize(lists)
if not lists then
error("Cannot open config",2)
end
if not _config.name then
_config.name = _config.id
2016-11-03 13:19:34 +01:00
end
lists[_config.id] = _config.name
2016-11-01 17:13:41 +01:00
local f = fs.open("/.sPhone/config/spklist","w")
f.write(textutils.serialize(lists))
f.close()
return true
else
return false, "not a spk file"
end
else
return false, "not a spk file"
end
end
function sPhone.launch(spk)
if not fs.exists("/.sPhone/config/spklist") then
local f = fs.open("/.sPhone/config/spklist","w")
f.write("{}")
f.close()
2015-10-10 18:28:12 +02:00
end
2016-11-01 17:13:41 +01:00
local f = fs.open("/.sPhone/config/spklist","r")
local lists = f.readAll()
f.close()
lists = textutils.unserialize(lists)
if not lists then
error("Cannot open config",2)
2016-03-19 23:01:02 +01:00
end
2016-11-01 17:13:41 +01:00
if not lists[spk] then
return false, "not installed"
2015-10-10 18:28:12 +02:00
end
2016-11-01 17:13:41 +01:00
local f = fs.open("/.sPhone/apps/spk/"..spk.."/.spk","r")
local script = f.readAll()
f.close()
_config = textutils.unserialize(script)
2016-11-01 17:13:41 +01:00
if not script then
error("config corrupted",2)
end
local ok, err = pcall(function()
setfenv(loadfile(fs.combine("/.sPhone/apps/spk",_config.id.."/".._config.main)), setmetatable({
2016-11-01 17:13:41 +01:00
spk = {
getName = function()
return (_config.name or nil)
2016-11-01 17:13:41 +01:00
end,
getID = function()
return (_config.id or nil)
2016-11-01 17:13:41 +01:00
end,
getPath = function()
return "/.sPhone/apps/spk/".._config.id
2016-11-01 17:13:41 +01:00
end,
getAuthor = function()
return (_config.author or nil)
2016-11-01 17:13:41 +01:00
end,
getVersion = function()
return (_config.version or nil)
2016-11-01 17:13:41 +01:00
end,
},
string = string,
sPhone = sPhone,
}, {__index = getfenv()}))()
end)
if not ok then
return false, err
2016-03-19 23:01:02 +01:00
end
2016-11-01 17:13:41 +01:00
return true
2015-10-10 18:28:12 +02:00
end
2015-08-19 11:45:35 +02:00
local function home()
2016-03-19 23:01:02 +01:00
sPhone.inHome = true
2016-09-02 12:57:19 +02:00
while true do
os.pullEvent = os.oldPullEvent
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.clear()
term.setCursorPos(1,1)
2016-11-01 17:33:42 +01:00
local homePath = sPhone.getDefaultApp("home")
2016-09-02 12:57:19 +02:00
if not sPhone.safeMode then
2016-11-01 17:33:42 +01:00
if not fs.exists(homePath) then
homePath = "/.sPhone/apps/home"
2016-03-19 23:01:02 +01:00
end
2016-09-02 12:57:19 +02:00
else
2016-11-01 17:33:42 +01:00
homePath = "/.sPhone/apps/home"
2015-08-19 11:45:35 +02:00
end
2016-11-01 17:33:42 +01:00
temp.set("homePID",task.add(function() shell.run(homePath) end))
task.run()
2015-08-19 11:45:35 +02:00
end
2016-03-19 23:01:02 +01:00
sPhone.inHome = false
2015-08-19 11:45:35 +02:00
end
function login()
2016-11-01 17:13:41 +01:00
local old = os.pullEvent
os.pullEvent = os.pullEventRaw
2015-11-20 18:38:55 +01:00
sPhone.locked = true
2016-11-02 21:02:04 +01:00
if config.read("/.sPhone/config/sPhone","password") then
2015-08-19 11:45:35 +02:00
while true do
2016-03-20 17:27:01 +01:00
term.setBackgroundColor(sPhone.theme["backgroundColor"])
2015-08-19 11:45:35 +02:00
term.clear()
term.setCursorPos(1,1)
2016-03-20 17:27:01 +01:00
sPhone.header(sPhone.user)
paintutils.drawBox(7,9,20,11,sPhone.theme["window.background"])
2015-08-19 11:45:35 +02:00
if sPhone.wrongPassword then
term.setTextColor(colors.red)
2016-03-20 17:27:01 +01:00
term.setBackgroundColor(sPhone.theme["backgroundColor"])
2015-11-27 22:39:21 +01:00
visum.align("center"," Wrong Password",false,13)
2015-08-19 11:45:35 +02:00
end
2016-03-20 17:27:01 +01:00
term.setTextColor(sPhone.theme["text"])
term.setBackgroundColor(sPhone.theme["backgroundColor"])
2015-11-27 22:39:21 +01:00
visum.align("center"," Insert Password",false,7)
2015-09-21 21:34:14 +02:00
local loginTerm = window.create(term.native(), 8,10,12,1, true)
term.redirect(loginTerm)
2016-03-20 17:27:01 +01:00
term.setBackgroundColor(sPhone.theme["backgroundColor"])
2015-09-21 21:34:14 +02:00
term.clear()
term.setCursorPos(1,1)
2016-03-20 17:27:01 +01:00
term.setTextColor(sPhone.theme["text"])
2016-03-19 23:01:02 +01:00
local passwordLogin = read("*")
2015-09-21 21:34:14 +02:00
term.redirect(sPhone.mainTerm)
2016-11-02 21:02:04 +01:00
local fpw = config.read("/.sPhone/config/sPhone","password")
if sha256.sha256(passwordLogin) == fpw then
2016-03-19 23:01:02 +01:00
sPhone.wrongPassword = false
2016-11-01 17:13:41 +01:00
os.pullEvent = old
2016-03-19 23:01:02 +01:00
return
else
sPhone.wrongPassword = true
end
end
2015-08-19 11:45:35 +02:00
else
2015-08-25 17:46:25 +02:00
local name
local pw
local pwr
2015-08-25 17:56:00 +02:00
local rServer
2016-03-20 17:27:01 +01:00
sPhone.firstBoot = true
2015-08-19 11:45:35 +02:00
while true do
2016-03-20 17:27:01 +01:00
term.setBackgroundColor(sPhone.theme["backgroundColor"])
2015-08-19 11:45:35 +02:00
term.clear()
term.setCursorPos(1,1)
2016-03-20 17:27:01 +01:00
sPhone.header("Setup")
paintutils.drawBox(7,9,20,11,sPhone.theme["window.background"])
2015-08-19 11:45:35 +02:00
if sPhone.wrongPassword then
term.setTextColor(colors.red)
2015-11-27 22:39:21 +01:00
visum.align("center"," Wrong Password",false,13)
2015-08-19 11:45:35 +02:00
end
2016-03-20 17:27:01 +01:00
term.setTextColor(sPhone.theme["text"])
term.setBackgroundColor(sPhone.theme["backgroundColor"])
visum.align("center"," Insert Password",false,7)
local loginTerm = window.create(term.native(), 8,10,12,1, true)
term.redirect(loginTerm)
term.setBackgroundColor(sPhone.theme["backgroundColor"])
term.clear()
term.setCursorPos(1,1)
term.setTextColor(sPhone.theme["text"])
2015-09-21 21:34:14 +02:00
local password1 = read("*")
2015-10-05 17:24:13 +02:00
term.redirect(sPhone.mainTerm)
2016-03-20 17:27:01 +01:00
term.setBackgroundColor(sPhone.theme["backgroundColor"])
2015-08-19 11:45:35 +02:00
term.clear()
term.setCursorPos(1,1)
2016-03-20 17:27:01 +01:00
sPhone.header("Setup")
paintutils.drawBox(7,9,20,11,sPhone.theme["window.background"])
term.setTextColor(sPhone.theme["text"])
term.setBackgroundColor(sPhone.theme["backgroundColor"])
2015-11-27 22:39:21 +01:00
visum.align("center"," Repeat",false,7)
2016-03-20 17:27:01 +01:00
local loginTerm = window.create(term.native(), 8,10,12,1, true)
term.redirect(loginTerm)
term.setBackgroundColor(sPhone.theme["backgroundColor"])
term.clear()
term.setCursorPos(1,1)
term.setTextColor(sPhone.theme["text"])
2015-09-21 21:34:14 +02:00
local password2 = read("*")
2015-10-05 17:24:13 +02:00
term.redirect(sPhone.mainTerm)
2015-08-19 11:45:35 +02:00
if password1 == password2 then
2016-11-02 21:02:04 +01:00
config.write("/.sPhone/config/sPhone", "password",sha256.sha256(password1))
2015-08-19 11:45:35 +02:00
term.setTextColor(colors.lime)
2015-11-27 22:39:21 +01:00
visum.align("center"," Password set!",false,13)
2015-08-19 11:45:35 +02:00
sleep(2)
break
else
sPhone.wrongPassword = true
end
end
2015-08-29 15:57:40 +02:00
2015-12-24 16:17:55 +01:00
local name
2016-07-18 13:30:48 +02:00
term.setBackgroundColor(sPhone.theme["backgroundColor"])
term.clear()
sPhone.header("Setup")
term.setCursorPos(1,1)
term.setTextColor(sPhone.theme["text"])
term.setCursorPos(2,3)
visum.align("center","Username",false,3)
term.setCursorPos(2,5)
local newUsername = read()
2016-11-02 21:02:04 +01:00
config.write("/.sPhone/config/sPhone","username",newUsername)
2016-07-18 13:30:48 +02:00
2016-11-02 21:02:04 +01:00
if fs.exists("/.sPhone/config/sPhone") then
name = config.read("/.sPhone/config/sPhone","username")
2015-12-24 16:17:55 +01:00
else
name = "Guest"
end
2016-03-20 17:27:01 +01:00
term.setBackgroundColor(sPhone.theme["backgroundColor"])
2015-08-29 15:57:40 +02:00
term.clear()
2016-03-20 17:27:01 +01:00
sPhone.header("Setup")
2015-08-29 15:57:40 +02:00
term.setCursorPos(1,1)
2016-03-20 17:27:01 +01:00
term.setTextColor(sPhone.theme["text"])
2016-03-27 15:33:16 +02:00
sPhone.user = name
2016-09-02 12:57:19 +02:00
os.setComputerLabel(sPhone.user.."'s sPhone")
2016-03-20 17:27:01 +01:00
visum.align("center"," All Set!",false,3)
visum.align("center"," Have fun with sPhone",false,5)
2015-08-25 17:56:00 +02:00
sleep(2)
2015-11-20 18:38:55 +01:00
sPhone.locked = false
2016-03-20 17:27:01 +01:00
sPhone.inHome = true
sPhone.firstBoot = false
2016-11-01 17:13:41 +01:00
os.pullEvent = old
2015-11-20 18:38:55 +01:00
return
2015-08-19 11:45:35 +02:00
end
end
2015-11-20 18:38:55 +01:00
sPhone.lock = login
2015-11-20 18:39:41 +01:00
sPhone.login = login
2015-12-24 16:25:13 +01:00
2016-06-04 21:35:31 +02:00
local newVersion = http.get("https://raw.githubusercontent.com/BeaconNet/sPhone/master/src/version").readLine()
2015-08-19 11:45:35 +02:00
2015-12-24 16:25:13 +01:00
if newVersion ~= sPhone.version then
2015-09-02 14:41:09 +02:00
sPhone.newUpdate = true
else
sPhone.newUpdate = false
end
2015-09-02 14:44:57 +02:00
2016-09-02 12:57:19 +02:00
login()
if sPhone.newUpdate then
sPhone.winOk("New Update!")
end
2015-11-20 18:38:55 +01:00
home()
2015-09-02 14:41:09 +02:00
2015-07-31 19:52:45 +02:00
end
2015-12-15 18:54:15 +01:00
if not sPhone then
kernel(...)
else
print("sPhone already started")
2015-09-09 17:31:27 +02:00
end