Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local source = "http://geeksmod.net/ccos/content"
- print("Fetching manifest from "..source)
- local lnk = http.get(source)
- if not lnk then
- error("Source is dead")
- end
- local contents = textutils.unserialize(lnk.readAll())
- lnk.close()
- local baseurl = contents.base
- print("Getting required files...")
- for i=1,#contents.required do
- local f = contents.required[i]
- print("Getting "..f)
- for j=1,#f do
- if f:sub(j,j)=="/" then
- local found = f:sub(1,j-1)
- if not fs.exists(found) then
- fs.makeDir(found)
- end
- end
- end
- if f:sub(-1)~="/" then
- local file = fs.open(f,"w")
- local content = http.get(baseurl..f).readAll()
- file.write(content)
- file.close()
- end
- end
- print("Done downloading required files")
- -------------------------------------------------------------------------
- -- MODULE SELECTION MENU --
- -------------------------------------------------------------------------
- local w,h = term.getSize()
- local mid = math.floor(w/2)
- local disp = h-5
- local dispw = mid-3
- local panel1 = {tab = {}, off = 0, selected = 0}
- local panel2 = {tab = {}, off = 0, selected = 0}
- local function hasn(tab,name)
- for k,v in pairs(tab) do if v.name==name then return true end end
- return false
- end
- -----------
- --PRESETS--
- -----------
- if contents.presets then
- print("Presets are available: ")
- local k=1
- for i,preset in ipairs(contents.presets) do
- k=k+1
- term.setTextColor(colors.green)
- print(""..i..": "..preset.name..":")
- term.setTextColor(colors.orange)
- print(" ->"..preset.desc)
- end
- term.setTextColor(colors.green)
- print(""..k..": Manual Config")
- term.setTextColor(colors.white)
- term.write("Mode > ")
- local c = read()
- local cn = tonumber(c)
- if cn then
- if cn>0 and cn<k and math.floor(cn)==cn then --select preset
- for id,modn in ipairs(contents.presets[cn].modules) do
- panel2.tab[#panel2.tab+1] = {name=modn}
- end
- end
- end
- end
- ----------
- --MANUAL--
- ----------
- local si = #panel2.tab==0
- local prevmods = {}
- if fs.exists(".installed_modules") then
- local f = fs.open(".installed_modules","r")
- local mods = textutils.unserialize(f.readAll())
- f.close()
- for i,m in ipairs(mods) do
- if si then
- table.insert(panel2.tab,m)
- end
- table.insert(prevmods,m)
- end
- end
- if si then
- ---
- for i=1,#contents.modules do
- if not hasn(panel2.tab,contents.modules[i].name) then
- table.insert(panel1.tab,{
- name = contents.modules[i].name,
- file = contents.modules[i].file,
- conflict = contents.modules[i].conflict,
- require = contents.modules[i].required
- })
- end
- end
- local dirty = true
- local p1d = true
- local p2d = true
- local md = true
- local popup = nil
- local function slice(txt,len)
- local t = {}
- local added = 0
- for i = 1,#txt,len do
- local max=added+len
- while txt:sub(max,max)~=" " and max>1 and added+max<#txt do
- max=max-1
- end
- t[#t+1] = txt:sub(added+1,added+max)
- added=added+max
- end
- return t
- end
- local function redraw()
- if not dirty then return end
- dirty = false
- if popup then
- paintutils.drawFilledBox(2,h/2-popup.h/2-1,3+popup.w,h/2+popup.h/2,colors.blue)
- paintutils.drawFilledBox(3,h/2-popup.h/2,2+popup.w,h/2+popup.h/2-1,colors.white)
- for i=1,#popup.els do
- local e = popup.els[i]
- if e and type(e)=="table" and #e>4 then
- term.setTextColor(e[3])
- term.setBackgroundColor(e[4])
- local txt = slice(e[5],popup.w)
- for i=1,#txt do
- term.setCursorPos(1+e[1], h/2-popup.h/2-2+e[2]+i)
- term.write(txt[i])
- end
- end
- end
- return
- end
- if md then
- md=false
- term.clear()
- paintutils.drawFilledBox(1,1,w,1,colors.gray)
- term.setCursorPos(1,1)
- term.setTextColor(colors.black)
- term.write("Modules selection")
- paintutils.drawFilledBox(1,2,w,h,colors.white)
- paintutils.drawFilledBox(2,3,mid-1,h-3,colors.gray)
- term.setCursorPos(mid-1,3)
- term.setBackgroundColor(colors.red)
- term.write("^")
- term.setCursorPos(mid-1,h-3)
- term.setBackgroundColor(colors.red)
- term.write("V")
- paintutils.drawFilledBox(mid-1,4,mid-1,h-4,colors.black)
- paintutils.drawFilledBox(mid+2,3,w-1,h-3,colors.gray)
- term.setCursorPos(w-1,3)
- term.setBackgroundColor(colors.red)
- term.write("^")
- term.setCursorPos(w-1,h-3)
- term.setBackgroundColor(colors.red)
- term.write("V")
- paintutils.drawFilledBox(w-1,4,w-1,h-4,colors.black)
- term.setCursorPos(mid,h/2)
- term.setBackgroundColor(colors.lightGray)
- term.write(">")
- term.setCursorPos(mid,h/2+2)
- term.setBackgroundColor(colors.lightGray)
- term.write("<")
- term.setCursorPos(w-9, h-1)
- term.write(" Confirm ")
- end
- if p1d then
- p1d=false
- local hi=3
- local size = math.min(1,disp/#panel1.tab)
- local c = 0
- for i=1+panel1.off,math.min(#panel1.tab,panel1.off+disp) do
- local l = panel1.tab[i]
- term.setCursorPos(2,hi)
- if i==panel1.selected or (type(panel1.selected)=="table" and panel1.selected[i]) then
- term.setBackgroundColor(colors.lightGray)
- else
- term.setBackgroundColor(colors.gray)
- end
- term.setTextColor(colors.black)
- term.write(l.name:sub(1,dispw)..string.rep(" ",dispw-#l.name))
- hi=hi+1
- c=c+1
- end
- if c<disp then
- paintutils.drawFilledBox(2,3+c,mid-2,h-3,colors.gray)
- end
- if panel1.tab[panel1.selected] or type(panel1.selected)=="table" then
- term.setCursorPos(mid,h/2)
- term.setBackgroundColor(colors.lime)
- term.write(">")
- else
- term.setCursorPos(mid,h/2)
- term.setBackgroundColor(colors.lightGray)
- term.write(">")
- end
- end
- if p2d then
- p2d=false
- local hi=3
- local size = math.min(1,disp/#panel2.tab)
- local c = 0
- for i=1+panel2.off,math.min(#panel2.tab,panel2.off+disp) do
- local l = panel2.tab[i]
- term.setCursorPos(mid+2,hi)
- if i==panel2.selected or (type(panel2.selected)=="table" and panel2.selected[i]) then
- term.setBackgroundColor(colors.lightGray)
- else
- term.setBackgroundColor(colors.gray)
- end
- term.setTextColor(colors.black)
- term.write(l.name:sub(1,dispw)..string.rep(" ",dispw-#l.name))
- if l.warnings and #l.warnings>0 then
- if panel2.selerr and panel2.selerr.tp==2 and panel2.selerr.id==i then
- for k=1,#l.warnings do
- term.setCursorPos(w-4-#l.warnings[k],hi+k-1)
- term.setBackgroundColor(colors.yellow)
- term.setTextColor(colors.black)
- term.write(l.warnings[k].."<")
- end
- c=c+#l.warnings-1
- else
- term.setCursorPos(w-4,hi)
- term.setBackgroundColor(colors.yellow)
- term.setTextColor(colors.black)
- term.write("!")
- end
- end
- if l.problems and #l.problems>0 then
- if panel2.selerr and panel2.selerr.tp==1 and panel2.selerr.id==i then
- for k=1,#l.problems do
- term.setCursorPos(w-3-#l.problems[k],hi+k-1)
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.white)
- term.write(l.problems[k].."<")
- end
- c=c+#l.problems-1
- else
- term.setCursorPos(w-3,hi)
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.white)
- term.write("!")
- end
- end
- hi=hi+1
- c=c+1
- end
- if c<disp then
- paintutils.drawFilledBox(mid+2,3+c,w-2,h-3,colors.gray)
- end
- if panel2.tab[panel2.selected] or type(panel2.selected)=="table" then
- term.setCursorPos(mid,h/2+2)
- term.setBackgroundColor(colors.lime)
- term.write("<")
- else
- term.setCursorPos(mid,h/2+2)
- term.setBackgroundColor(colors.lightGray)
- term.write("<")
- end
- end
- end
- local doloop = true
- local controld = false
- local shiftd = false
- local lastsel = nil
- while doloop do
- redraw()
- local e = {os.pullEvent()}
- if popup then
- if e[1]=="mouse_click" and e[2]==1 then
- local clk,x,y = e[2],e[3],e[4]
- for i=1,#popup.els do
- local e = popup.els[i]
- if type(e)=="table" and type(e[6])=="function" then
- if x>=math.floor(e[1]+1) and x<math.floor(e[1]+#e[5]+1) and y==e[2]+math.floor(h/2-popup.h/2-1) then
- e[6]()
- break
- end
- end
- end
- end
- else
- if e[1]=="key" then
- local key = e[2]
- if key==208 then --down
- end
- if key==200 then --up
- end
- if key==29 then --control
- controld = true
- end
- if key==42 then --shift
- shiftd = true
- end
- end
- if e[1]=="key_up" then
- local key = e[2]
- if key==29 then --control
- controld = false
- end
- if key==42 then --shift
- shiftd = false
- end
- end
- if e[1]=="mouse_click" then
- local clk,x,y = e[2],e[3],e[4]
- if clk==1 then
- --Confirm
- if x>w-10 and x<w and y==h-1 then
- local haserr = false
- local haswarn = false
- for i=1,#panel2.tab do
- local t = panel2.tab[i]
- if t.problems and #t.problems>0 then haserr=true end
- if t.warnings and #t.warnings>0 then haswarn=true end
- end
- local pw = w-4
- local txt1 = haserr and "Some modules are conflicting, they may cause issues." or ""
- local txt2 = haswarn and "Some required modules are not selected, they will be downloaded." or ""
- local ln1 = (haserr and math.floor(#txt1/(pw-2)+1) or 0)
- local ln2 = (haswarn and math.floor(#txt2/(pw-2)+1) or 0)
- local ph = 3 + ln1 + ln2
- if haserr or haswarn then ph=ph+1 end
- popup = {
- w = pw,
- h = ph,
- els = {
- {pw/2-1,1,colors.black,colors.white, "Confirm"},
- (haserr and {2,3,colors.red,colors.white,txt1} or 0),
- (haswarn and {2,(ph-ln2)-1,colors.yellow,colors.white,txt2} or 0),
- {3,ph,colors.black,colors.red,"Cancel",function()
- popup=nil dirty=true p1d=true p2d=true md=true
- end},{pw-1,ph,colors.black,colors.lime,"Ok",function()
- popup = nil
- doloop = false
- --Finish config
- end}
- }
- }
- dirty=true
- end
- --Panel 1
- if x==mid-1 and y==3 then
- panel1.off = math.max(0,panel1.off-1)
- dirty = true
- p1d=true
- end
- if x==mid-1 and y==h-3 then
- panel1.off = math.min(math.max(#panel1.tab-disp,0),panel1.off+1)
- dirty = true
- p1d=true
- end
- if x<mid-1 and x>1 and y>2 and y<h-2 then
- if shiftd then
- local base = panel1.lastsel or 1
- if panel1.tab[y-2+panel1.off] then
- panel1.selected = {}
- for i = base,y-2+panel1.off,(y-2+panel1.off<base) and -1 or 1 do
- panel1.selected[i] = true
- end
- end
- elseif controld then
- if type(panel1.selected)=="number" and panel1.tab[panel1.selected] then
- local al = panel1.selected
- panel1.selected = {}
- panel1.selected[al] = true
- if panel1.tab[y-2+panel1.off] then
- panel1.selected[y-2+panel1.off] = true
- end
- elseif type(panel1.selected)=="table" then
- if panel1.tab[y-2+panel1.off] then
- panel1.selected[y-2+panel1.off] = true
- end
- else
- panel1.selected = y-2+panel1.off
- end
- else
- panel1.selected = y-2+panel1.off
- end
- if panel1.tab[y-2+panel1.off] then
- panel1.lastsel=y-2+panel1.off
- end
- panel2.selected = 0
- panel2.lastsel = nil
- dirty = true
- p1d=true
- p2d=true
- end
- --Panel 2
- if x==w-1 and y==3 then
- panel2.off = math.max(0,panel2.off-1)
- dirty = true
- p2d=true
- end
- if x==w-1 and y==h-3 then
- panel2.off = math.min(math.max(#panel2.tab-disp,0),panel2.off+1)
- dirty = true
- p2d=true
- end
- if x==w-3 then
- local opt = y-2+panel2.off
- if panel2.tab[opt] and panel2.tab[opt] then
- panel2.selerr = {tp=1,id=opt}
- dirty = true
- p2d=true
- end
- elseif x==w-4 then
- local opt = y-2+panel2.off
- if panel2.tab[opt] and panel2.tab[opt] then
- panel2.selerr = {tp=2,id=opt}
- dirty = true
- p2d=true
- end
- elseif panel2.selerr~=nil then
- panel2.selerr=nil
- dirty = true
- p2d=true
- end
- if x<w-1 and x>mid+1 and y>2 and y<h-2 then
- if shiftd then
- local base = panel2.lastsel or 1
- if panel2.tab[y-2+panel2.off] then
- panel2.selected = {}
- for i = base,y-2+panel2.off,(y-2+panel2.off<base) and -1 or 1 do
- panel2.selected[i] = true
- end
- end
- elseif controld then
- if type(panel2.selected)=="number" and panel2.tab[panel2.selected] then
- local al = panel2.selected
- panel2.selected = {}
- panel2.selected[al] = true
- if panel2.tab[y-2+panel2.off] then
- panel2.selected[y-2+panel2.off] = true
- end
- elseif type(panel2.selected)=="table" then
- if panel2.tab[y-2+panel2.off] then
- panel2.selected[y-2+panel2.off] = true
- end
- else
- panel2.selected = y-2+panel1.off
- end
- else
- panel2.selected = y-2+panel2.off
- end
- if panel2.tab[y-2+panel2.off] then
- panel2.lastsel=y-2+panel2.off
- end
- panel1.selected = 0
- panel1.lastsel = nil
- dirty = true
- p2d=true
- p1d=true
- end
- if x==mid and y==math.floor(h/2) then
- if type(panel1.selected)=="number" then
- local data = panel1.tab[panel1.selected]
- if data then
- table.remove(panel1.tab,panel1.selected)
- table.insert(panel2.tab,data)
- panel2.selected = 0
- panel1.selected = 0
- dirty = true
- p2d=true
- p1d=true
- end
- elseif type(panel1.selected)=="table" then
- local before,remd = #panel1.tab,0
- for i,_ in pairs(panel1.selected) do
- remd=remd+1
- local data = panel1.tab[i]
- panel1.tab[i] = nil
- table.insert(panel2.tab,data)
- end
- local add=0
- for i=1,before-remd do
- while not panel1.tab[i+add] do add=add+1 end
- panel1.tab[i] = panel1.tab[i+add]
- end
- for i=before-remd+1,before do panel1.tab[i]=nil end
- panel2.selected = 0
- panel1.selected = 0
- panel1.lastsel = nil
- panel2.lastsel = nil
- dirty = true
- p2d=true
- p1d=true
- end
- end
- if x==mid and y==math.floor(h/2+2) then
- if type(panel2.selected)=="number" then
- local data = panel2.tab[panel2.selected]
- if data then
- table.remove(panel2.tab,panel2.selected)
- table.insert(panel1.tab,data)
- panel1.selected = 0
- panel2.selected = 0
- dirty = true
- p2d=true
- p1d=true
- end
- elseif type(panel2.selected)=="table" then
- local before,remd = #panel2.tab,0
- for i,_ in pairs(panel2.selected) do
- remd=remd+1
- local data = panel2.tab[i]
- panel2.tab[i] = nil
- table.insert(panel1.tab,data)
- end
- local add=0
- for i=1,before-remd do
- while not panel2.tab[i+add] do add=add+1 end
- panel2.tab[i] = panel2.tab[i+add]
- end
- for i=before-remd,before do panel2.tab[i]=nil end
- panel1.selected = 0
- panel2.selected = 0
- panel1.lastsel = nil
- panel2.lastsel = nil
- dirty = true
- p2d=true
- p1d=true
- end
- end
- end
- end
- if e[1]=="mouse_scroll" then
- local dir,x,y = e[2],e[3],e[4]
- if x<mid and x>1 and y>2 and y<h-2 then
- if dir==-1 then
- panel1.off = math.max(0,panel1.off-1)
- dirty = true
- p1d=true
- end
- if dir==1 then
- panel1.off = math.min(math.max(#panel1.tab-disp,0),panel1.off+1)
- dirty = true
- p1d=true
- end
- end
- if x<w and x>mid+1 and y>2 and y<h-2 then
- if dir==-1 then
- panel2.off = math.max(0,panel2.off-1)
- dirty = true
- p2d=true
- end
- if dir==1 then
- panel2.off = math.min(math.max(#panel2.tab-disp,0),panel2.off+1)
- dirty = true
- p2d=true
- end
- end
- end
- end
- if p2d then
- for i=1,#panel2.tab do
- local p = panel2.tab[i]
- p.problems = {}
- if p.conflict then
- for j=1,#p.conflict do
- for k=1,#panel2.tab do
- if p.conflict[j]==panel2.tab[k].name then
- table.insert(p.problems,"Conflict: "..p.conflict[j])
- end
- end
- end
- end
- if p.require then
- p.warnings = {}
- for j=1,#p.require do
- local need=true
- for k=1,#panel2.tab do
- if p.require[j]==panel2.tab[k].name then
- need=false
- end
- end
- if need then
- table.insert(p.warnings,"Requires: "..p.require[j])
- end
- end
- end
- end
- end
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
- ---
- end
- ---
- local addrequired
- addrequired = function(m)
- local req = m.require or m.required or {}
- for i=1,#req do
- for k=1,#contents.modules do
- local m2 = contents.modules[k]
- if m2==req[i] then
- table.insert(panel2.tab,{name=m2.name})
- addrequired(m2)
- end
- end
- end
- end
- for k,v in pairs(prevmods or {}) do
- fs.delete(v.file)
- end
- local newm = {}
- for i=1,#contents.modules do
- local m = contents.modules[i]
- if hasn(panel2.tab, m.name) then
- print("Downloading \""..m.name.."\"")
- local f = m.file
- table.insert(newm,m)
- for j=1,#f do
- if f:sub(j,j)=="/" then
- local found = f:sub(1,j-1)
- if not fs.exists(found) then
- fs.makeDir(found)
- end
- end
- end
- local file = fs.open(f,"w")
- local content = http.get(baseurl..f).readAll()
- file.write(content)
- file.close()
- end
- end
- local f = fs.open(".installed_modules","w")
- f.write(textutils.serialize(newm))
- f.close()
- term.setTextColor(colors.red)
- print("Done installing ToxIcOS "..(contents.version or "ALPHA"))
- term.setTextColor(colors.white)
Advertisement
Add Comment
Please, Sign In to add comment