Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function githubUrl(owner,repo,path)
- local path = path or ""
- return 'https://api.github.com/repos/'..owner..'/'..repo..'/contents/'..path
- end
- local function download(url,GITHUB_TOKEN)
- local handle, err, err_handle
- if GITHUB_TOKEN then
- handle, err, err_handle = http.get(url,{
- ["Authorization"] = "token " .. GITHUB_TOKEN,
- ["User-Agent"] = "mcNet-Installer"
- })
- else
- handle, err, err_handle = http.get(url)
- end
- if not handle then
- printError(err_handle and err_handle.readAll() or "-")
- error(err, 0)
- end
- return handle.readAll()
- end
- function downloadRepo(path,url,GITHUB_TOKEN)
- path = path or ""
- local paths = textutils.unserialiseJSON(download(url,GITHUB_TOKEN))
- for _,p in pairs(paths) do
- print(p.path)
- if p.download_url then
- local h = fs.open(path..p.path,"w")
- h.write(download(p.download_url, GITHUB_TOKEN))
- h.close()
- else
- downloadRepo(path,p.url)
- end
- end
- end
- local function readInput(strings,arrow)
- arrow = arrow or ">"
- for i=1,#strings do
- print(tostring(strings[i]))
- end
- local _,y =term.getCursorPos()
- term.write(tostring(arrow))
- local input = io.read()
- term.setCursorPos(1,y+1)
- return input
- end
- local function programEnd(token)
- if token then
- local input = readInput({"do you want me to remember this token: "..string.sub(token, 1, 4).."..."..string.sub(token, -4).."? under setting 'github.token'","this setting will be deleted together with the installer"},"y/n:")
- if string.find(input,"y",nil,true) then
- settings.set("github.token",token)
- settings.save()
- end
- end
- if token then
- print("to protect you secrete token this chat will auto wipe in 1 seconds")
- sleep(1)
- term.clear()
- term.setCursorPos(1,1)
- end
- end
- print("This is the mcNET installer to install certain assets please select what asset to install")
- print("-'D' : DNSServer (will delete all other files)")
- print("-'C' : Client")
- print("-'S' : Server (will delete all other files)")
- print("-'E' : deinstall this Installer")
- print("- other: exits installer")
- local _,y =term.getCursorPos()
- term.write("D/C/S/E:")
- local input = io.read()
- term.setCursorPos(1,y+1)
- input = string.lower(input)
- local confirmed, token
- if settings.get("github.token") and input ~= "e" then
- token = settings.get("github.token")
- local input = readInput({"you have provided a token:"..string.sub(token, 1, 4).."..."..string.sub(token, -4),"do you want to reset it?"},"y/n:")
- if string.find(input,"y",nil,true) then
- settings.unset("github.token")
- settings.save()
- end
- token = nil
- print()
- end
- while not (confirmed or token)and input ~= "e" do
- token = readInput({"please enter your github token for a higher rate limit (5,000 requests/hour)","press enter if you don't want to do this"})
- if #token < 1 then
- print("warning you are using unauthenticated download requests")
- print("this means you can only do 60 requests / hour")
- confirmed = true
- token = nil
- else
- local input = readInput({"is this your token? "..string.sub(token, 1, 4).."..."..string.sub(token, -4)},"y/n:")
- if string.find(input,"y",nil,true) then
- confirmed = true
- end
- end
- end
- if token and input ~= "e" then
- print()
- print("using github token: "..string.sub(token, 1, 4).."..."..string.sub(token, -4))
- end
- print()
- if input == "d" then
- for _,i in pairs(fs.list("")) do
- print(i)
- if i ~= "rom" then
- fs.delete(i)
- end
- end
- downloadRepo("",githubUrl("Redtech0inc","MCNet","mcNet/dnsServer-Stuff"),token)
- for _,i in pairs(fs.list("mcNet/dnsServer-Stuff")) do
- print(i)
- local lasPathDiv = 0
- while string.find(i,"/",lasPathDiv+1,true) do
- local pathDiv = string.find(i,"/",lasPathDiv+1,true)
- if pathDiv then lasPathDiv = pathDiv end
- end
- local path = string.sub(i,lasPathDiv+1,#i)
- fs.move("mcNet/dnsServer-Stuff/"..path,path)
- end
- fs.delete("mcNet/")
- fs.delete(shell.getRunningProgram())
- shell.run("initDns.lua")
- elseif input == "c" then
- print("enter the name of the directory that you want the client to be installed in")
- local _,y =term.getCursorPos()
- term.write("> ")
- local name = io.read()
- term.setCursorPos(1,y+1)
- if #name < 1 then
- name = "mcNet"
- end
- downloadRepo("temporary/",githubUrl("Redtech0inc","MCNet","mcNet/client-stuff"),token)
- fs.delete(name.."/")
- fs.move("temporary/mcNet/client-stuff/*",name.."/")
- fs.delete("temporary/")
- fs.makeDir(name.."/libs")
- fs.makeDir(name.."/pages")
- fs.move(name.."/openUILib.lua",name.."/libs/openUILib.lua")
- fs.move(name.."/logo.nfp",name.."/libs/logo.nfp")
- print("do you want to deinstall the installer?")
- local _,y =term.getCursorPos()
- term.write("y/n:")
- local input = io.read()
- term.setCursorPos(1,y+1)
- if string.find(input,"y",nil,true) then
- fs.delete(shell.getRunningProgram())
- end
- programEnd(token)
- print("do you want to run the program now?")
- local _,y =term.getCursorPos()
- term.write("y/n:")
- local input = io.read()
- term.setCursorPos(1,y+1)
- if string.find(input,"y",nil,true) then
- shell.run(name.."/mcNet-Client.lua")
- end
- elseif input == "s" then
- for _,i in pairs(fs.list("")) do
- print(i)
- if i ~= "rom" then
- fs.delete(i)
- end
- end
- downloadRepo("",githubUrl("Redtech0inc","MCNet","mcNet/serverFramework"),token)
- for _,i in pairs(fs.list("mcNet/serverFramework")) do
- print(i)
- local lasPathDiv = 0
- while string.find(i,"/",lasPathDiv+1,true) do
- local pathDiv = string.find(i,"/",lasPathDiv+1,true)
- if pathDiv then lasPathDiv = pathDiv end
- end
- local path = string.sub(i,lasPathDiv+1,#i)
- fs.move("mcNet/serverFramework/"..path,path)
- end
- fs.delete("mcNet/")
- fs.delete(shell.getRunningProgram())
- print("do you want to run the program now?")
- local _,y =term.getCursorPos()
- term.write("y/n:")
- local input = io.read()
- term.setCursorPos(1,y+1)
- if string.find(input,"y",nil,true) then
- shell.run("initServer.lua")
- end
- elseif input == "e" then
- fs.delete(shell.getRunningProgram())
- if token then
- settings.unset("github.token")
- settings.save()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment