Update sPhone.lua

This commit is contained in:
Ale32bit 2016-11-01 17:13:41 +01:00 committed by GitHub
parent a382ad4efb
commit d67043ebef

View file

@ -40,6 +40,16 @@ local function kernel()
end end
end end
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
local sPath = shell.path() local sPath = shell.path()
sPath = sPath..":/bin" sPath = sPath..":/bin"
shell.setPath(sPath) shell.setPath(sPath)
@ -164,6 +174,26 @@ local function kernel()
sPhone.setDefaultApp("home","/.sPhone/apps/home") sPhone.setDefaultApp("home","/.sPhone/apps/home")
end end
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
function sPhone.list(path, opt) function sPhone.list(path, opt)
opt = opt or {} opt = opt or {}
opt.bg1 = opt.bg1 or sPhone.getTheme("backgroundColor") opt.bg1 = opt.bg1 or sPhone.getTheme("backgroundColor")
@ -654,34 +684,118 @@ end
sPhone.colourPicker = sPhone.colorPicker -- For UK sPhone.colourPicker = sPhone.colorPicker -- For UK
function sPhone.run(rApp, ...) function sPhone.install(spk)
if not fs.exists(rApp) or fs.isDir(rApp) then if string.getExtension(spk) == "spk" then
sPhone.winOk("App not found") if fs.exists(spk) and not fs.isDir(spk) then
return false 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 end
if sPhone.inHome then
local sPhoneWasInHome = true local function writeFile(patha,contenta)
sPhone.inHome = false local file = fs.open(patha,"w")
file.write(contenta)
file.close()
end end
os.pullEvent = os.oldPullEvent function writeDown(inputa,dira)
local ok, err = pcall(function(...) setfenv(loadfile(rApp),getfenv())(...) end, ...) 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)
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))
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
lists[config.id] = true
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()
end
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 lists[spk] then
return false, "not installed"
end
local f = fs.open("/.sPhone/apps/spk/"..spk.."/.spk","r")
local script = f.readAll()
f.close()
config = textutils.unserialize(script)
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({
spk = {
getName = function()
return config.name
end,
getID = function()
return config.id
end,
getPath = function()
return "/.sPhone/apps/spk/"..config.id
end,
getAuthor = function()
return config.author
end,
getVersion = function()
return config.version
end,
},
string = string,
sPhone = sPhone,
}, {__index = getfenv()}))()
end)
if not ok then if not ok then
os.pullEvent = os.pullEventRaw return false, err
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.clear()
term.setCursorPos(1,2)
visum.align("center"," "..fs.getName(rApp).." crashed",false,2)
term.setCursorPos(1,4)
print(err)
print("")
visum.align("center"," Press Any Key")
os.pullEvent("key")
end
os.pullEvent = os.pullEventRaw
if sPhoneWasInHome then
sPhone.inHome = true
end end
return true
end end
local function home() local function home()
@ -707,6 +821,8 @@ end
end end
function login() function login()
local old = os.pullEvent
os.pullEvent = os.pullEventRaw
sPhone.locked = true sPhone.locked = true
if fs.exists("/.sPhone/config/.password") then if fs.exists("/.sPhone/config/.password") then
while true do while true do
@ -734,6 +850,7 @@ end
local fpw = fs.open("/.sPhone/config/.password","r") local fpw = fs.open("/.sPhone/config/.password","r")
if sha256.sha256(passwordLogin) == fpw.readLine() then if sha256.sha256(passwordLogin) == fpw.readLine() then
sPhone.wrongPassword = false sPhone.wrongPassword = false
os.pullEvent = old
return return
else else
sPhone.wrongPassword = true sPhone.wrongPassword = true
@ -830,6 +947,7 @@ end
sPhone.locked = false sPhone.locked = false
sPhone.inHome = true sPhone.inHome = true
sPhone.firstBoot = false sPhone.firstBoot = false
os.pullEvent = old
return return
end end
end end