ClientOS/installer/installer.lua
2025-10-20 00:19:13 +02:00

216 lines
6.3 KiB
Lua

local function dothething()
local function wordwrap(str)
local x,y = term.getCursorPos()
local tW,tH = term.getSize()
for w in str:gmatch("%S+") do
local x1,y1 = term.getCursorPos()
if x1+(#w*3) >= tW then
bigfont.bigPrint(" ")
local x2,y2 = term.getCursorPos()
term.setCursorPos(x,y2)
end
bigfont.bigWrite(w.." ")
end
end
--local ico = lUtils.asset.load("User/Cloud/Images/New_Loading.limg") -- replace with actual filepath later
local f = http.post("https://os.leveloper.cc/sGet.php","path="..textutils.urlEncode("LevelOS/assets/loading.limg").."&code="..textutils.urlEncode("lSlb8kZq"),nil).readAll()
local ico = textutils.unserialize(f)
for t=1,#ico do
for l=1,#ico[t] do
ico[t][l][2] = string.gsub(ico[t][l][2],"T","9")
ico[t][l][3] = string.gsub(ico[t][l][3],"T","9")
end
end
local frame = 1
local prog = 0
local icoX,icoY
local function renderIco(frame)
local f = ico[frame]
for l=1,#f do
term.setCursorPos(icoX,icoY+(l-1))
term.blit(unpack(f[l]))
end
end
local function progress()
while true do
renderIco(frame)
term.setCursorPos(icoX+5,icoY+1)
term.write(math.floor(prog + 0.5).."% complete")
if badConn then
term.setCursorPos(x, icoY+5)
print("Having trouble connecting, please hang on...")
end
os.sleep(0.1)
frame = frame+1
if frame > #ico then
frame = 1
end
end
end
local function fread(file)
local f = fs.open(file,"r")
local o = f.readAll()
f.close()
return o
end
local function fwrite(file,content)
local f = fs.open(file,"w")
f.write(content)
f.close()
return true
end
local badConn = false
local function render()
term.setBackgroundColor(colors.cyan)
term.setTextColor(colors.white)
term.clear()
local w,h = term.getSize()
local x = 2
if w > 51 then
x = math.floor(x+((w-51)/4))
end
term.setCursorPos(x,3)
wordwrap("Installing LevelOS")
bigfont.bigPrint(" ")
local x1,y1 = term.getCursorPos()
term.setCursorPos(x,y1+2)
print("This might take a while. Don't turn off your computer.")
local x1,y1 = term.getCursorPos()
icoX,icoY = x,y1+1
-- perhaps a cancel button if i ever feel like it
end
render()
local function events()
while true do
local e = {os.pullEvent()}
-- process things if buttons
if e[1] == "term_resize" then
render()
end
end
end
local hpost = function(...)
while true do
local ret = table.pack(http.post(...))
if not ret[1] then
badConn = true
render()
os.sleep(0.5)
else
badConn = false
return table.unpack(ret, 1, ret.n)
end
end
end
local function getField(thing,fieldname)
if string.find(thing,"<"..fieldname..">",1,true) ~= nil and string.find(thing,"</"..fieldname..">",1,true) ~= nil then
begin = nil
ending = nil
trash,begin = string.find(thing,"<"..fieldname..">",1,true)
ending,ending2 = string.find(thing,"</"..fieldname..">",begin+1,true)
if begin ~= nil and ending ~= nil then
return string.sub(thing,begin+1,ending-1),string.sub(thing,1,trash-1)..string.sub(thing,ending2+1,string.len(thing))
end
end
return nil
end
local function download(pth)
local f = hpost("https://os.leveloper.cc/sGet.php","path="..textutils.urlEncode(pth).."&code="..textutils.urlEncode("lSlb8kZq"),{Cookie=userID}).readAll()
if f ~= "409" and f ~= "403" and f ~= "401" then
fwrite(pth,f)
return true
else
return false
end
end
local function downloader()
local package = shapescape.getSlides().package
local tFiles = 0
local pack = package.name
if fs.exists("LevelOS/data/settings.lconf") then
local set = textutils.unserialize(fread("LevelOS/data/settings.lconf"))
if set.package then
pack = set.package
end
end
local tree = {}
local folders = {}
local function searchFolder(folder)
--print("Searching folder root/"..folder)
local f = hpost("https://os.leveloper.cc/sGet.php","path="..textutils.urlEncode(folder).."&code="..textutils.urlEncode("lSlb8kZq"),nil).readAll()
--print(f)
local f2 = f
while true do
local file = nil
file,f = getField(f,"file")
if not file then
break
else
local name = getField(file,"name")
local timestamp = getField(file,"timestamp")
timestamp = tonumber(timestamp)
if pack == "Full" or fs.exists(fs.combine(folder,name)) then
tree[fs.combine(folder,name)] = {timestamp=timestamp}
tFiles = tFiles+1
end
--print("Found "..fs.combine(folder,name))
end
end
f = f2
while true do
local file = nil
file,f = getField(f,"folder")
if not file then
break
else
local name = getField(file,"name")
if not fs.exists(fs.combine(folder,name)) then
fs.makeDir(fs.combine(folder,name))
end
folders[fs.combine(folder,name)] = ""
searchFolder(fs.combine(folder,name))
end
end
return true
end
if not searchFolder("") then return false end
if pack == "Full" then
for k,v in pairs(tree) do
table.insert(package,k)
end
end
-- check if name is full, if it is then first list the dir then add all the files to the package
for p=1,#package do
prog = ((p-1)/#package)*100
download(package[p])
prog = ((p)/#package)*100
end
local temp = hpost("https://os.leveloper.cc/sGet.php","path="..textutils.urlEncode("").."&code="..textutils.urlEncode("lSlb8kZq"),{Cookie=userID}).readAll()
local ver = getField(temp,"version")
fwrite("LevelOS/data/version.txt",ver)
fwrite("LevelOS/data/settings.lconf",textutils.serialize({package=pack}))
os.sleep(2)
os.reboot()
end
parallel.waitForAny(events,progress,downloader)
end
local ok,err = pcall(dothething)
if not ok then
term.setCursorPos(1,1)
term.setTextColor(colors.red)
term.write(err)
end