Advertisement
Alakazard12

aloOS

Dec 13th, 2012
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.00 KB | None | 0 0
  1. local sides = {"top", "bottom", "left", "right", "front", "back"}
  2.  
  3. local stx, sty = term.getSize()
  4. local cfile = "/.aloOS"
  5. local changel = [[
  6. * 1.3
  7.     - Added scrolling and clicking.
  8.     - More options in sertup.
  9.  
  10. * 1.2.23
  11.     - Added reset config.
  12.     - Added reformat computer.
  13.     - Added force upadte.
  14.  
  15. * 1.2
  16.     - Fixed startup message. Renamed main file.
  17.     - Added changelog :D.
  18.     - Added recycling.
  19.  
  20. * 1.1.5
  21.     - Added the "Power" commands.
  22.  
  23. * 1.1
  24.     - Added "Rednet" commands.
  25.  
  26. * 1.0.12
  27.     - Bug fixes.
  28.  
  29. * 1.0.1
  30.     - Added "System" commands.
  31.  
  32. * 1.0
  33.     - First release.
  34.    
  35. ]]
  36.  
  37. local function clear()
  38.     term.clear()
  39.     term.setCursorPos(1, 1)
  40. end
  41.  
  42. local config = {
  43.     colors = {
  44.         main = colors.lime;
  45.         second = colors.white;
  46.         bg = colors.black;
  47.     };
  48.     controls = {
  49.         click = true;
  50.         scroll = true;
  51.     };
  52.     version = "1.3";
  53. }
  54.  
  55. local function drawlist(title, listr, num)
  56.     clear()
  57.     term.setTextColor(config.colors.main)
  58.     local list = {}
  59.     print(title.."\n")
  60.     for i,v in pairs(listr) do
  61.         list[i] = v
  62.     end
  63.     local start = 1
  64.     local last = #list
  65.     if num > sty - 4 then
  66.         start = num - (sty - 4)
  67.     end
  68.     local cnt = 0
  69.     for i = start, #list do
  70.         if cnt > sty - 4 then
  71.             last = last - 1
  72.             cnt = cnt - 1
  73.         end
  74.         cnt = cnt + 1
  75.     end
  76.     for m = start, last do
  77.         local tor = true
  78.         if tor == true then
  79.             if m == num then
  80.                 term.setTextColor(config.colors.second)
  81.                 write("[")
  82.                 term.setTextColor(config.colors.main)
  83.                 write("-")
  84.                 term.setTextColor(config.colors.second)
  85.                 write("]")
  86.                 term.setTextColor(config.colors.main)
  87.                 print(" "..list[m])
  88.             else
  89.                 term.setTextColor(config.colors.second)
  90.                 print("[ ] "..list[m])
  91.             end
  92.         end
  93.     end
  94. end
  95.  
  96. local function getKey()
  97.     local rt = nil
  98.     function lop()
  99.         local e, a1 = os.pullEvent()
  100.         if e == "key" then
  101.             if a1 == 200 then
  102.                 rt = "up"
  103.             elseif a1 == 208 then
  104.                 rt = "down"
  105.             elseif a1 == 28 then
  106.                 rt = "enter"
  107.             else
  108.                 lop()
  109.             end
  110.         elseif e == "mouse_scroll" then
  111.             if config.controls.scroll == true then
  112.                 if a1 == 1 then
  113.                     rt = "down"
  114.                 elseif a1 == -1 then
  115.                     rt = "up"
  116.                 else
  117.                     lop()
  118.                 end
  119.             else
  120.                 lop()
  121.             end
  122.         elseif e == "mouse_click" then
  123.             if config.controls.click == true then
  124.                 if a1 == 1 then
  125.                     rt = "enter"
  126.                 else
  127.                     lop()
  128.                 end
  129.             else
  130.                 lop()
  131.             end
  132.         else
  133.             lop()
  134.         end
  135.     end
  136.     lop()
  137.     repeat sleep(0) until rt ~= nil
  138.     return rt
  139. end
  140.  
  141. local function ch(title, list)
  142.     clear()
  143.     local ind = 1
  144.     local lcopy = list
  145.     drawlist(title, lcopy, ind)
  146.     local rt = nil
  147.     cycle = function()
  148.         local ke = getKey()
  149.         if ke == "up" then
  150.             if ind > 1 then
  151.                 ind = ind - 1
  152.             else
  153.                 ind = #list
  154.             end
  155.             drawlist(title, lcopy, ind)
  156.             cycle()
  157.         elseif ke == "down" then
  158.             if ind < #list then
  159.                 ind = ind + 1
  160.             else
  161.                 ind = 1
  162.             end
  163.             drawlist(title, lcopy, ind)
  164.             cycle()
  165.         elseif ke == "enter" then
  166.             rt = ind
  167.         end
  168.     end
  169.     cycle()
  170.     repeat sleep(0) until rt ~= nil
  171.     return ind
  172. end
  173.  
  174. local function printcol(txt, color)
  175.     term.setTextColor(color)
  176.     print(txt)
  177.     term.setTextColor(config.colors.second)
  178. end
  179.  
  180. local function listcol(text)
  181.     local cols = {}
  182.     for i,v in pairs(colors) do
  183.         if type(v) == "number" then
  184.             table.insert(cols, i)
  185.         end
  186.     end
  187.     local re = ch(text, cols)
  188.     return colors[cols[re]]
  189. end
  190.  
  191. local function setup()
  192.     while true do
  193.         local pl = ch("Configuration", {"Choose main color", "Choose secondary color", "Choose background color", "Enable/disable click", "Enable/disable scroll", "Exit setup"})
  194.         if pl == 1 then
  195.             config.colors.main = listcol("Please choose a color")
  196.         elseif pl == 2 then
  197.             config.colors.second = listcol("Please choose a color")
  198.         elseif pl == 3 then
  199.             config.colors.bg = listcol("Please choose a color")
  200.             term.setBackgroundColor(config.colors.bg)
  201.             clear()
  202.         elseif pl == 4 then
  203.             local pd = ch("Disable or enable click?", {"Disable", "Enable"})
  204.             if pd == 1 then
  205.                 config.controls.click = false
  206.             elseif pd == 2 then
  207.                 config.controls.click = true
  208.             end
  209.         elseif pl == 5 then
  210.             local pd = ch("Disable or enable scroll?", {"Disable", "Enable"})
  211.             if pd == 1 then
  212.                 config.controls.scroll = false
  213.             elseif pd == 2 then
  214.                 config.controls.scroll = true
  215.             end
  216.         elseif pl == 6 then
  217.             break
  218.         end
  219.     end
  220. end
  221.  
  222. local function sep(list, by)
  223.     local newlist = {}
  224.     while true do
  225.         local on = nil
  226.         local done = false
  227.         for i = 1, #list do
  228.             if done == false then
  229.                 if string.sub(list, i, i) == by then
  230.                     on = i
  231.                     done = true
  232.                 end
  233.             end
  234.         end
  235.         if on then
  236.             table.insert(newlist, string.sub(list, 1, on - 1))
  237.             list = string.sub(list, on + 1)
  238.         else
  239.             break
  240.         end
  241.     end
  242.     table.insert(newlist, list)
  243.     return newlist
  244. end
  245.  
  246. local function update()
  247.     local res = http.get(
  248.         "http://pastebin.com/raw.php?i="..textutils.urlEncode( "X7M1GkNW" )
  249.     )
  250.     local sRes = res.readAll()
  251.     res.close()
  252.     if sRes ~= config.version then
  253.         local fr = ch("New update available", {"Update", "Don't update"})
  254.         if fr == 1 then
  255.             local res2 = http.get(
  256.                 "http://pastebin.com/raw.php?i="..textutils.urlEncode( "pwz9u82d" )
  257.             )
  258.             local scr = res2.readAll()
  259.             res2.close()
  260.             local bcols = textutils.serialize(config.colors)
  261.             local bc = fs.open("/bcol", "w")
  262.             bc.write(bcols)
  263.             bc.close()
  264.             local bc2 = fs.open("/bcon", "w")
  265.             bc2.write(textutils.serialize(config.controls))
  266.             bc2.close()
  267.             local sname = shell.getRunningProgram()
  268.             local pl = fs.open(sname, "w")
  269.             pl.write(scr)
  270.             pl.close()
  271.             os.reboot()
  272.         end
  273.     else
  274.         local pe = ch("No updates available", {"Close", "Force update"})
  275.         if pe == 2 then
  276.             local res2 = http.get(
  277.                 "http://pastebin.com/raw.php?i="..textutils.urlEncode( "pwz9u82d" )
  278.             )
  279.             local scr = res2.readAll()
  280.             res2.close()
  281.             local bcols = textutils.serialize(config.colors)
  282.             local bc = fs.open("/bcol", "w")
  283.             bc.write(bcols)
  284.             bc.close()
  285.             local bc2 = fs.open("/bcon", "w")
  286.             bc2.write(textutils.serialize(config.controls))
  287.             bc2.close()
  288.             local sname = shell.getRunningProgram()
  289.             local pl = fs.open(sname, "w")
  290.             pl.write(scr)
  291.             pl.close()
  292.             os.reboot()
  293.         end
  294.     end
  295. end
  296.  
  297. local function savecon()
  298.     local fl = fs.open(cfile.."/config", "w")
  299.     fl.write(textutils.serialize(config))
  300.     fl.close()
  301. end
  302.  
  303. local function loadcon()
  304.     local fl = fs.open(cfile.."/config", "r")
  305.     config = textutils.unserialize(fl.readAll())
  306.     fl.close()
  307. end
  308.  
  309. local function checkports()
  310.     local isp = false
  311.     for i,v in pairs(sides) do
  312.         if peripheral.isPresent(v) and peripheral.getType(v) == "modem" then
  313.             isp = true
  314.         end
  315.     end
  316.     return isp
  317. end
  318.  
  319. local function getsys()
  320.     local res2 = http.get(
  321.         "http://pastebin.com/raw.php?i="..textutils.urlEncode( "U537Qp6w" )
  322.     )
  323.     local prl = textutils.unserialize(res2.readAll())
  324.     res2.close()
  325.     fs.delete(cfile.."/Programs/System")
  326.     fs.makeDir(cfile.."/Programs/System")
  327.     for i,v in pairs(prl) do
  328.         printcol("Downloading "..i, config.colors.main)
  329.         local res2 = http.get(
  330.             "http://pastebin.com/raw.php?i="..textutils.urlEncode( v )
  331.         )
  332.         local cp = fs.open(cfile.."/Programs/System/"..i, "w")
  333.         cp.write(res2.readAll())
  334.         cp.close()
  335.         res2.close()
  336.     end
  337. end
  338.  
  339. local oldd = fs.delete
  340. function fs.delete(path)
  341.     path = shell.resolve(path)
  342.     pcall(function()
  343.         fs.copy(path, cfile.."/recycle")
  344.     end)
  345.     oldd(path)
  346. end
  347.  
  348. local function ports()
  349.     for i,v in pairs(sides) do
  350.         rednet.open(v)
  351.     end
  352. end
  353.  
  354. if fs.exists("/bcol") then
  355.     local bc = fs.open("/bcol", "r")
  356.     config.colors = textutils.unserialize(bc.readAll())
  357.     bc.close()
  358.     fs.delete("/bcol")
  359. end
  360. if fs.exists("/bcon") then
  361.     local bc = fs.open("/bcon", "r")
  362.     config.controls = textutils.unserialize(bc.readAll())
  363.     bc.close()
  364.     fs.delete("/bcon")
  365. end
  366.  
  367. term.setBackgroundColor(config.colors.bg)
  368. clear()
  369.  
  370. if not fs.exists(cfile) then
  371.     getsys()
  372.     printcol("Alo has detected this being the first time using aloOS", config.colors.main)
  373.     local pl = ch("Alo has detected this being the first time using alOS. Would you like to go through the setup or use default settings?", {"Setup", "Default"})
  374.     if pl == 1 then
  375.         setup()
  376.     end
  377.     printcol("Saving files...", config.colors.main)
  378.     sleep(1)
  379.     fs.makeDir(cfile)
  380.     fs.makeDir(cfile.."/recycle")
  381.     savecon()
  382. else
  383.     loadcon()
  384. end
  385.  
  386. local tob = false
  387.  
  388. local chy = {
  389.     ["System"] = {
  390.         ["Terminal"] = function() tob = true end;
  391.         ["Update"] = function() update() end;
  392.         ["Changelog"] = function()
  393.             local cul = fs.open(cfile.."/changelogChache", "w")
  394.             cul.write(changel)
  395.             cul.close()
  396.             shell.run("edit", cfile.."/changelogChache")
  397.             fs.delete(cfile.."/changelogChache")
  398.         end;
  399.         ["Reset config"] = function()
  400.             local pe = ch("Are you sure you wish to reset configuration?", {"Yes", "No"})
  401.             if pe == 1 then
  402.                 fs.delete(cfile)
  403.                 os.reboot()
  404.             end
  405.         end;
  406.         ["Reformat computer"] = function()
  407.             local pe = ch("Are you sure you wish to reformat computer?", {"Yes", "No"})
  408.             if pe == 1 then
  409.                 for i,v in pairs(fs.list("/")) do
  410.                     if v ~= shell.getRunningProgram() and v ~= "rom" then
  411.                         pcall(function()
  412.                             oldd("/"..v)
  413.                         end)
  414.                     end
  415.                 end
  416.                 os.reboot()
  417.             end
  418.            
  419.         end;
  420.     };
  421.     ["Run setup"] = function() setup() savecon() end;
  422.     ["Power"] = {
  423.         ["Shutdown"] = function() os.shutdown() end;
  424.         ["Reboot"] = function() os.reboot() end;
  425.     };
  426.     ["Rednet"] = {
  427.         ["Open ports"] = function()
  428.             function lpo1()
  429.                 if checkports() then
  430.                     ports()
  431.                 else
  432.                     local po = ch("No modem attached", {"Rety", "Cancel"})
  433.                     if po == 1 then
  434.                         lpo1()
  435.                     end
  436.                 end
  437.             end
  438.             lpo1()
  439.         end;
  440.     };
  441.     ["Programs"] = {
  442.         ["User programs"] = function()
  443.             local plist = fs.list(cfile.."/Programs/User")
  444.             table.insert(plist, 1, "..")
  445.             local ut = ch("Choose a program to execute", plist)
  446.             if ut ~= 1 then
  447.                 shell.run(cfile.."/Programs/User/"..plist[ut-1])
  448.             end
  449.         end;
  450.         ["System programs"] = function()
  451.             local plist = fs.list(cfile.."/Programs/System")
  452.             table.insert(plist, 1, "..")
  453.             local ut = ch("Choose a program to execute", plist)
  454.             if ut ~= 1 then
  455.                 shell.run(cfile.."/Programs/System/"..plist[ut], textutils.serialize(config))
  456.                 term.setCursorBlink(false)
  457.             end
  458.         end;
  459.     };
  460. }
  461.  
  462. local cind = "Main"
  463. local ctbl1 = chy
  464.  
  465. while true do
  466.     if tob == true then
  467.         clear()
  468.         break
  469.     end
  470.     local ctbl = {".."}
  471.     for i,v in pairs(ctbl1) do
  472.         table.insert(ctbl, i)
  473.     end
  474.     local fl = ch(cind, ctbl)
  475.     local tp = "nil"
  476.     local tf = nil
  477.     local ond = 1
  478.     for i,v in pairs(ctbl1) do
  479.         ond = ond + 1
  480.         if fl == ond then
  481.             tp = type(v)
  482.             tf = v
  483.         end
  484.     end
  485.     if tp == "function" then
  486.         tf()
  487.     elseif fl ~= 1 then
  488.         local ond = 1
  489.         for i,v in pairs(ctbl1) do
  490.             ond = ond + 1
  491.             if fl == ond then
  492.                 ctbl1 = v
  493.                 cind = cind.."/"..i
  494.             end
  495.         end
  496.     else
  497.         local lind = sep(cind, "/")
  498.         local newc = "Main"
  499.         table.remove(lind, 1)
  500.         for i,v in pairs(lind) do
  501.             if i ~= #lind then
  502.                 newc = newc.."/"..v
  503.             end
  504.         end
  505.         table.remove(lind, #lind)
  506.         ctbl1 = chy
  507.         for i,v in pairs(lind) do
  508.             ctbl1 = ctbl1[v]
  509.         end
  510.         cind = newc
  511.     end
  512. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement