Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Nitrosoft Games Catalogue
- A listing of all games released and available for installation by Nitrogen Fingers
- Part installer, part advertising tool...
- ]]--
- local catalogueCode = "KBApP73v"
- local entries = {}
- local _w,_h = term.getSize()
- local function writeC(msg, col, x, y)
- if x and y then term.setCursorPos(x,y) end
- if term.isColour() and col then
- term.setTextColour(col)
- end
- term.write(msg)
- end
- function wprintOffCenter(msg, height, width, offset)
- local inc = 0
- local ops = 1
- while #msg - ops > width do
- local nextspace = 0
- while string.find(msg, " ", ops + nextspace) and
- string.find(msg, " ", ops + nextspace) - ops < width do
- nextspace = string.find(msg, " ", nextspace + ops) + 1 - ops
- end
- local ox,oy = term.getCursorPos()
- term.setCursorPos(width/2 - (nextspace)/2 + offset, height + inc)
- inc = inc + 1
- term.write(string.sub(msg, ops, nextspace + ops - 1))
- ops = ops + nextspace
- end
- term.setCursorPos(width/2 - #string.sub(msg, ops)/2 + offset, height + inc)
- term.write(string.sub(msg, ops))
- return inc + 1
- end
- if not http then
- print("The catalogue requires HTTP to run. Please ensure it is enabled.")
- end
- local function getCatalogue()
- write("Downloading catalogue, please wait...")
- local httprep = http.get(
- "http://pastebin.com/raw.php?i="..textutils.urlEncode(catalogueCode)
- )
- if httprep then
- local line = httprep.readLine()
- local currEntry = nil
- while line do
- if string.find(line, "ENTRY ") == 1 then
- if currEntry then table.insert(entries, currEntry) end
- currEntry = { title = string.sub(line, 7); updates = {} }
- elseif string.find(line, "V ") == 1 then
- currEntry.version = tonumber(string.sub(line, 3))
- elseif string.find(line, "COL ") == 1 then
- currEntry.mono = string.find(line, "yes") == 5
- elseif string.find(line, "CODE ") == 1 then
- currEntry.pastecode = string.sub(line, 6)
- if #currEntry.pastecode ~= 8 then pastecode = nil end
- elseif string.find(line, "DESC ") == 1 then
- currEntry.desc = string.sub(line, 6)
- elseif string.find(line, "UPDATE ") == 1 then
- line = string.sub(line, 7)
- local _n = string.sub(line, 1, string.find(line, " "))
- table.insert(currEntry.updates, {
- version = tonumber(_n);
- desc = string.sub(line, #_n+1)
- })
- end
- line = httprep.readLine()
- end
- table.insert(entries, currEntry)
- return true
- else
- print("Unable to access the catalogue: "..http.getResponseCode())
- return false
- end
- end
- local states = { "SELECT", "DISPLAY", "GET", "QUIT", "UPDATE" }
- local cstate = states[1]
- local mensel = 1
- local selEntry = nil
- local function displayNitrosoftTitle()
- shell.run("clear")
- local _t1,_t2,_t3 = "nitrosoft", "games", "Game Catalogue"
- term.setCursorPos(math.floor(_w/2-#_t1), 4)
- if term.isColour() then term.setTextColour(colours.blue) end
- term.write(_t1)
- term.setCursorPos(math.floor(_w/2-#_t2/2),5)
- term.setTextColour(colours.white)
- term.write(_t2)
- if term.isColour() then term.setTextColour(colours.red) end
- term.setCursorPos(math.floor(_w/2-#_t1), 6)
- term.write(string.rep("-",#_t1*2))
- if term.isColour() then
- term.setBackgroundColour(colours.green)
- term.setCursorPos(_w/2+#_t1-4, 2)
- term.write(" ")
- term.setCursorPos(_w/2+#_t1-4, 3)
- term.write(" ")
- term.setBackgroundColour(colours.lime)
- term.setCursorPos(_w/2+#_t1-3, 3)
- term.write(" ")
- term.setCursorPos(_w/2+#_t1-3, 4)
- term.write(" ")
- end
- term.setBackgroundColour(colours.black)
- term.setTextColour(colours.white)
- end
- local function updateTitleMenu()
- term.setBackgroundColour(colours.black)
- term.setTextColour(colours.white)
- for i=1,#entries do
- term.setCursorPos(_w/2-#entries[i].title/2-2, 7+i)
- if mensel == i then
- term.setTextColour(colours.white)
- term.write("> ")
- else term.write(" ") end
- term.setTextColour(colours.yellow)
- term.write(entries[i].title)
- if mensel == i then
- term.setTextColour(colours.white)
- term.write(" <")
- else term.write(" ") end
- end
- local _t4 = "Exit Catalogue"
- term.setCursorPos(_w/2-#_t4/2-2, 9+#entries)
- if mensel == #entries + 1 then
- term.setTextColour(colours.white)
- term.write("> ")
- else term.write(" ") end
- term.setTextColour(colours.red)
- term.write(_t4)
- if mensel == #entries + 1 then
- term.setTextColour(colours.white)
- term.write(" <")
- else term.write(" ") end
- end
- local function runTitle()
- displayNitrosoftTitle()
- while true do
- updateTitleMenu()
- local _,key = os.pullEvent("key")
- if key == keys.up then
- if mensel > 1 then mensel = mensel - 1 end
- elseif key == keys.down then
- if mensel <= #entries then mensel = mensel + 1 end
- elseif key == keys.enter or key == keys.space then
- if mensel == #entries + 1 then cstate = states[4]
- else
- selEntry = entries[mensel]
- cstate = states[2]
- end
- return
- end
- end
- end
- local function downloadEntry()
- shell.run("clear")
- term.setTextColour(colours.white)
- print("Beginning installation\n*******************")
- local lpath = string.gsub(selEntry.title.."_installer"," ","")
- shell.run("pastebin", "get", selEntry.pastecode, lpath)
- print(" ")
- if fs.exists(shell.resolve(lpath)) then
- print("running "..shell.resolve(lpath))
- local _result = shell.run("/"..shell.resolve(lpath))
- fs.delete(shell.resolve(lpath))
- sleep(1)
- shell.run("clear")
- else
- shell.run("clear")
- displayNitrosoftTitle()
- _msg = selEntry.title.." was unable to download."
- writeC(_msg, colours.white, _w/2-#_msg/2,8)
- _msg = "Press a key to continue"
- writeC(_msg, colours.yellow, _w/2-#_msg/2,_h-3)
- os.pullEvent("key")
- end
- cstate = states[1]
- term.clear()
- end
- local function updateEntryMenu()
- local _opt = { "Install", "Updates", "Back" }
- mensel = 1
- repeat
- term.setCursorPos(1,_h-2)
- term.clearLine()
- if mensel == 1 then
- writeC("> "..string.rep(" ",#_opt[mensel]).." <",
- colours.white, 2,_h-2)
- elseif mensel == 2 then
- writeC("> "..string.rep(" ",#_opt[mensel]).." <",
- colours.white,_w/2-#_opt[mensel]/2,_h-2)
- else
- writeC("> "..string.rep(" ",#_opt[mensel]).." <",
- colours.white,_w-#_opt-5,_h-2)
- end
- writeC(_opt[1],colours.yellow,4,_h-2)
- --writeC(_opt[2],colours.yellow,_w/2-#_opt/2,_h-2)
- writeC(_opt[3],colours.yellow,_w-#_opt-3,_h-2)
- local _,key = os.pullEvent("key")
- if key == keys.left then
- if mensel > 1 then mensel = mensel - 2 end
- elseif key == keys.right then
- if mensel < #_opt then mensel = mensel + 2 end
- elseif key == keys.enter then
- if mensel == 1 then cstate = states[3]
- elseif mensel == 2 then cstate = states[4]
- elseif mensel == 3 then cstate = states[1]
- end
- break
- end
- until false
- term.clear()
- mensel = 1
- end
- local function runEntry()
- term.setBackgroundColour(colours.black)
- term.clear()
- displayNitrosoftTitle()
- writeC(selEntry.title, colours.yellow, _w/2-#selEntry.title/2,7)
- term.setTextColour(colours.white)
- local inc = 0
- wprintOffCenter(selEntry.desc, 9, _w-3, 3)
- writeC("Version "..selEntry.version, colours.white, 3,_h-4)
- local cmsg = "Monochrome "
- if selEntry.mono then cmsg = cmsg.."supported"
- else cmsg = cmsg.."not supported" end
- writeC(cmsg, colours.white, _w-3-#cmsg,_h-4)
- updateEntryMenu()
- end
- if not getCatalogue() then return end
- shell.run("clear")
- repeat
- if cstate == states[1] then
- runTitle()
- elseif cstate == states[2] then
- runEntry()
- elseif cstate == states[3] then
- downloadEntry()
- end
- until cstate == states[4]
- shell.run("clear")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement