sPhone/src/init.lua

198 lines
4.6 KiB
Lua
Raw Normal View History

2016-11-01 16:29:35 +01:00
os.oldPullEvent = os.pullEvent
2016-10-30 21:47:53 +01:00
os.pullEvent = os.pullEventRaw
2016-11-01 16:29:35 +01:00
2015-12-15 18:53:44 +01:00
local function crash(err)
if not sPhone then
sPhone = {
devMode = false,
}
end
term.setCursorBlink(false)
term.setBackgroundColor(colors.white)
term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.black)
if not err then
err = "Undefined Error"
end
print("sPhone got an error :(\n")
term.setTextColor(colors.red)
print(err)
term.setTextColor(colors.black)
print("")
if sPhone.version then
print("sPhone "..sPhone.version)
end
print("Computer ID: "..os.getComputerID())
if _CC_VERSION then
print("CC Version: ".._CC_VERSION)
print("MC Version: ".._MC_VERSION)
2016-01-12 11:39:52 +01:00
elseif _HOST then
print("Host: ".._HOST)
2015-12-15 18:53:44 +01:00
else
print("CC Version: Under 1.74")
print("MC Version: Undefined")
term.setTextColor(colors.red)
print("Update CC to 1.74 or higher")
term.setTextColor(colors.black)
end
print("LUA Version: ".._VERSION)
2016-01-12 11:39:52 +01:00
if _LUAJ_VERSION then
print("LUAJ Version: ".._LUAJ_VERSION)
end
2016-01-12 11:42:54 +01:00
print("Contact sPhone devs:")
2016-06-04 21:36:21 +02:00
print("GitHub: BeaconNet/sPhone")
2015-12-15 18:53:44 +01:00
print("Thanks for using sPhone")
print("Press any key")
repeat
sleep(0)
until os.pullEvent("key")
if not sPhone.devMode then
_G.term = nil
end
term.setBackgroundColor(colors.black)
term.clear()
term.setCursorPos(1,1)
sleep(0.1)
shell.run("/rom/programs/shell")
end
local function recovery()
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.clear()
term.setCursorPos(1,1)
print("sPhone Recovery")
print("[1] Hard Reset")
print("[2] Update sPhone")
print("[3] Reset User Config")
print("[4] Continue Booting")
print("[5] Boot in safe mode")
while true do
local _, k = os.pullEvent("key")
if k == 2 then
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
for k, v in pairs(fs.list("/")) do
if not fs.isReadOnly(v) then
if fs.isDir(v) then
shell.setDir(v)
for k, v in pairs(fs.list("/"..v)) do
fs.delete(v)
print("Removed "..shell.dir().."/"..v)
end
shell.setDir(shell.resolve(".."))
end
fs.delete(v)
print("Removed "..v)
end
end
print("Installing sPhone...")
sleep(0.5)
2016-06-04 21:36:21 +02:00
setfenv(loadstring(http.get("https://raw.githubusercontent.com/BeaconNet/sPhone/master/src/installer.lua").readAll()),getfenv())()
2015-12-15 18:53:44 +01:00
elseif k == 3 then
2016-06-04 21:36:21 +02:00
setfenv(loadstring(http.get("https://raw.githubusercontent.com/BeaconNet/sPhone/master/src/installer.lua").readAll()),getfenv())()
2015-12-15 18:53:44 +01:00
elseif k == 4 then
fs.delete("/.sPhone/config")
fs.delete("/.sPhone/cache")
2016-11-20 17:07:19 +01:00
fs.delete("/.sPhone/apps/spk")
fs.delete("/.sPhone/autorun")
2015-12-15 18:53:44 +01:00
os.reboot()
elseif k == 5 then
2016-11-15 23:19:35 +01:00
safemode = false
2015-12-15 18:53:44 +01:00
break
elseif k == 6 then
2016-11-15 23:19:35 +01:00
safemode = true
2015-12-15 18:53:44 +01:00
break
end
end
end
term.setBackgroundColor(colors.white)
term.setCursorPos(1,1)
term.clear()
term.setTextColor(colors.black)
2016-06-04 21:36:21 +02:00
print("BeaconNet")
2015-12-15 18:53:44 +01:00
if fs.exists("/.sPhone/interfaces/bootImage") then
local bootImage = paintutils.loadImage("/.sPhone/interfaces/bootImage")
paintutils.drawImage(bootImage, 11,7)
else
print("Missing bootImage")
end
local w, h = term.getSize()
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.setCursorPos(1,h)
write("Press ALT to recovery mode")
local bootTimer = os.startTimer(1)
while true do
local e,k = os.pullEvent()
if e == "key" and k == 56 then
recovery()
break
2016-11-20 17:07:19 +01:00
elseif e == "timer" and k == bootTimer then
safemode = false
2015-12-15 18:53:44 +01:00
break
end
end
if not fs.exists("/.sPhone/sPhone") then
2016-11-20 17:07:19 +01:00
printError("sPhone not installed")
shell.run("/.sPhone/init -u")
return
2015-12-15 18:53:44 +01:00
end
local runningOnStartup
term.setBackgroundColor(colors.black)
term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.white)
if sPhone then
printError("sPhone already started")
return
end
if not pocket or not term.isColor() then
printError("Computer not supported: use an Advanced Pocket Computer or an Advanced Wireless Pocket Computer")
return
end
local tArgs = {...}
local argData = {
["-u"] = false,
["-s"] = false,
}
if #tArgs > 0 then
while #tArgs > 0 do
local tArgs = table.remove(tArgs, 1)
if argData[tArgs] ~= nil then
argData[tArgs] = true
end
end
end
if argData["-u"] then
print("Getting installer...")
2016-06-04 21:36:21 +02:00
setfenv(loadstring(http.get("https://raw.githubusercontent.com/BeaconNet/sPhone/master/src/installer.lua").readAll()),getfenv())()
2015-12-15 18:53:44 +01:00
end
2016-11-01 16:29:35 +01:00
os.pullEvent = os.oldPullEvent
2016-10-30 21:47:53 +01:00
2016-11-15 23:19:35 +01:00
local ok, err = pcall(function()
setfenv(loadfile("/.sPhone/sPhone"), setmetatable({
crash = crash,
safemode = safemode,
}, {__index = getfenv()}))()
end)
2015-12-15 18:53:44 +01:00
if not ok then
crash(err)
end
_G.term = nil -- The OS ends here - This string force to crash the pda to shutdown