Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local northbridgeBuildAPI = "http://builds.northbridge.dx.am/"
- local x, y = term.getSize()
- local function putFilesFromDirArray(array, build, basePath)
- for k,v in pairs(array) do
- if (type(array[k]) == "string") then
- if (basePath ~= nil) then
- v = basePath.."/"..v
- end
- fileData = http.get(northbridgeBuildAPI..build.."/"..v)
- if (fileData ~= nil) then
- local file = fs.open(v, "w")
- file.write(fileData.readAll(), "w")
- file.close()
- end
- else
- if (basePath ~= nil) then
- k = basePath.."/"..k
- end
- fs.makeDir(k)
- putFilesFromDirArray(v, build, k)
- end
- end
- end
- local function arrayToDir(dirArray, dir)
- if (dir == nil) then dir = "" end
- for k,v in pairs(dirArray) do
- print(dir.."/"..k..": "..v.type)
- if (v.type == "file") then
- if (not fs.exists(dir)) then fs.makeDir(dir) end
- local file = fs.open(dir.."/"..k,'w')
- file.write(v.contents)
- file.close()
- elseif (v.type == "dir") then
- fs.makeDir(dir.."/"..k)
- arrayToDir(v.contents,dir.."/"..k)
- end
- end
- end
- local function firstToUpper(str)
- return (str:gsub("^%l", string.upper))
- end
- local function split(self, inSplitPattern, outResults )
- if not outResults then
- outResults = { }
- end
- local theStart = 1
- local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
- while theSplitStart do
- table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
- theStart = theSplitEnd + 1
- theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
- end
- table.insert( outResults, string.sub( self, theStart ) )
- return outResults
- end
- local function splitToLines(longString)
- if (#longString > x) then
- local chars = split(longString,"")
- local lines = (#chars+3)/x
- for i=1,#lines do
- chars:insert(i*x,"\n ")
- end
- local outString = ""
- for i=1,#chars do
- outString = outString..chars[i]
- end
- return outString
- else
- return longString
- end
- end
- local function pagedPrint(input)
- local linedinput = split(input,"\n")
- local x,y = term.getSize()
- term.setCursorPos(1,2)
- for i=1,y-2 do
- print(linedInput[i])
- end
- print("Press a key to continue")
- for i=y+1,#linedInput-y do
- term.setCursorPos(1,1)
- term.setTextColor(colors.white)
- if (term.isColor()) then term.setBackgroundColor(colors.lightGray) end
- for i=1, x do
- write(" ")
- end
- term.setCursorPos(1, 1)
- print("Northbridge Terminal OS Build Info:")
- if (term.isColor()) then term.setBackgroundColor(colors.black) end
- os.pullEvent("key")
- term.setCursorPos(1,i)
- print(linedInput[i])
- end
- end
- write("Getting build info... ")
- builds = http.get(northbridgeBuildAPI.."build.php?cmd=getBuilds&versions=true&changelogs=true")
- if (builds ~= nil) then
- builds = textutils.unserialize(builds.readAll())
- else
- error("can't access the Northbridge Network Build API")
- end
- local outer = true
- while (outer) do
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.white)
- --[=[if (term.isColor()) then term.setBackgroundColor(colors.lightGray) end
- for i=1, x do
- write(" ")
- end
- term.setCursorPos(1, 1)
- print("Northbridge Terminal OS Build Info:")
- if (term.isColor()) then term.setBackgroundColor(colors.black) end
- local info = ""
- for k, v in pairs(builds.msg) do
- if (type(k) ~= "number") then
- info = info..firstToUpper(k)..":".."\n"
- if (v.version ~= nil) then
- info = info.." Version: "..v.version.."\n"
- end
- if (v.changelog ~= nil) then
- info = info.." Changelog:".."\n"
- for i=1, #v.changelog do
- --print(" "..v.changelog[i])
- info = info.." "..splitToLines(v.changelog[i]).."\n"
- end
- end
- end
- end
- pagedPrint(info)
- --[[term.setCursorPos(1, y)
- write("Press a key to continue.")
- os.pullEvent("key")]]
- sleep(0)]=]
- local inner = true
- while (inner) do
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.white)
- if (term.isColor()) then term.setBackgroundColor(colors.lightGray) end
- for i=1, x do
- write(" ")
- end
- term.setCursorPos(1, 1)
- print("Northbridge Terminal OS Build Selection:")
- term.setBackgroundColor(colors.black)
- print("Which build would you like to install?")
- local cursorPos = {term.getCursorPos(1,1)}
- for i = 1, #builds.msg do
- print(i.."> "..firstToUpper(builds.msg[i]))
- end
- --print(1+#builds.msg.."> ".."Show previous page")
- print(2+#builds.msg.."> ".."Cancel")
- reply = tonumber(read())
- if (reply == nil) or (reply < 0) or (reply > #builds.msg+2) then
- print("Invalid reply")
- sleep(2)
- term.setCursorPos(cursorPos[1], cursorPos[2])
- elseif (reply == #builds.msg+1) then
- inner = false
- elseif (reply == #builds.msg+2) then
- term.clear()
- term.setCursorPos(1,1)
- print("Nevermind then...")
- inner = false
- outer = false
- else
- print("Getting build files...")
- buildData = http.get(northbridgeBuildAPI.."build.php?cmd=getBuildFiles&build="..builds.msg[reply])
- if (buildData == nil) then
- error("can't access the Northbridge Network Build API")
- end
- buildData = textutils.unserialize(buildData.readAll())
- if (fs.exists("/startup")) then
- fs.move("/startup","/autorun")
- end
- putFilesFromDirArray(buildData.msg, builds.msg[reply])
- if (fs.exists("files")) then
- local files = fs.open("files",'r')
- arrayToDir(textutils.unserialise(files.readAll()))
- files.close()
- fs.delete("files")
- end
- local file = fs.open("/northbridge/build", "w")
- file.write(builds.msg[reply])
- file.close()
- fs.delete("json")
- fs.delete("version")
- print("Rebooting...")
- sleep(2)
- os.reboot()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement