Advertisement
Guest User

Gray

a guest
May 18th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 26.14 KB | None | 0 0
  1. os.unloadAPI("multishell")
  2. os.loadAPI("system/api/multishell")
  3. os.loadAPI("settings")
  4. --term.redirect(term.native())
  5. math.randomseed(os.time())
  6.  
  7. colors.transparent = 0
  8. local screen = {
  9.   new = function(parent, x, y, width, height)
  10.     local s = {
  11.       term = window.create(parent or term.current(), x or 1, y or 1, width or 51, height or 19, false),
  12.       sscroll = 0,
  13.      
  14.       clear = function(self, color)
  15.         self.term.setBackgroundColor(color or colors.black)
  16.         self.term.clear()
  17.       end,
  18.      
  19.       drawImage = function(self, sx, sy, width, height, image)
  20.         if width > 0 and height > 0 then
  21.           for y = 1, #image do
  22.             local line = image[y]
  23.            
  24.             for x = 1, #line do
  25.               if line[x] > 0 and x <= width and y <= height then
  26.                 self:setPixel(x + sx - 1, y + sy - 1, line[x])
  27.               end
  28.             end
  29.           end
  30.         end
  31.       end,
  32.      
  33.       drawText = function(self, x, y, text, backgroundcolor, textcolor)
  34.         self.term.setCursorPos(x, y)
  35.         self.term.setBackgroundColor(backgroundcolor or colors.white)
  36.         self.term.setTextColor(textcolor or colors.white)
  37.         if not (backgroundcolor == colors.transparent) then
  38.           if textcolor == colors.transparent then
  39.             self.term.write("")
  40.           else
  41.             self.term.write(text or "")
  42.           end
  43.         end
  44.       end,
  45.      
  46.       drawBox = function(self, sx, sy, width, height, color)
  47.         for x = sx, sx + width - 1 do
  48.           for y = sy, sy + height - 1 do
  49.             self:setPixel(x, y, color)
  50.           end
  51.         end
  52.       end,
  53.      
  54.       setPixel = function(self, x, y, color)
  55.         self.term.setCursorPos(x, y)
  56.         self.term.setBackgroundColor(color or colors.white)
  57.         if not (color == colors.transparent) then
  58.           self.term.write(" ")
  59.         end
  60.       end,
  61.      
  62.       draw = function(self)
  63.         self.term.setVisible(true)
  64.         self.term.setVisible(false)
  65.       end,
  66.      
  67.       reposition = function(self, x, y, width, height)
  68.         self.term.reposition(x, y, width, height)
  69.       end,
  70.      
  71.       scroll = function(self, direction, speed)
  72.         self.term.scroll(direction * (speed or 1))
  73.       end
  74.     }
  75.    
  76.     return s
  77.   end
  78. }
  79.  
  80.  
  81.  
  82. colors.transparent = 0
  83. local widget = {
  84.   new = function(x, y, width, height)
  85.     local w = {
  86.       x = x or 1,
  87.       y = y or 1,
  88.       width = width or 1,
  89.       height = height or 1,
  90.       visible = true,
  91.       focused = false,
  92.      
  93.       onclick = function(self, button, x, y)
  94.        
  95.       end,
  96.      
  97.       onscroll = function(self, direction, x, y)
  98.        
  99.       end,
  100.      
  101.       onkey = function(self, key)
  102.        
  103.       end,
  104.      
  105.       onchar = function(self, c)
  106.        
  107.       end,
  108.      
  109.       draw = function(self, screen)
  110.        
  111.       end
  112.     }
  113.    
  114.     return w
  115.   end
  116. }
  117.  
  118. local layout = {
  119.   new = function(x, y, width, height)
  120.     local w = widget.new(x, y, width, height)
  121.    
  122.     w.backgroundcolor = colors.black
  123.     w.widgets = {}
  124.     w.screen = screen.new(term.current(), x or 1, y or 1, width or 1, height or 1)
  125.    
  126.     w.addWidget = function(self, widget)
  127.       self.widgets[#self.widgets + 1] = widget
  128.     end
  129.    
  130.     w.handleevent = function(self, event, p1, p2, p3, p4, p5)
  131.       if not event then
  132.         event, p1, p2, p3, p4, p5 = os.pullEvent()
  133.       end
  134.      
  135.       if event == "mouse_click" then
  136.         self:onclick(p1, p2, p3)
  137.       elseif event == "mouse_scroll" then
  138.         self:onscroll(p1, p2, p3)
  139.       elseif event == "key" then
  140.         self:onkey(p1)
  141.       elseif event == "char" then
  142.         self:onchar(p1)
  143.       end
  144.     end
  145.    
  146.     w.onclick = function(self, button, x, y)
  147.       for i = 1, #self.widgets do
  148.         if (self.widgets[i].width == -1 or self.widgets[i].height == -1) or (x >= self.widgets[i].x and y >= self.widgets[i].y and x <= self.widgets[i].x + self.widgets[i].width - 1 and y <= self.widgets[i].y + self.widgets[i].height - 1) then
  149.           self.widgets[i].focused = true
  150.           self.widgets[i]:onclick(button, x, y)
  151.         else
  152.           self.widgets[i].focused = false
  153.         end
  154.       end
  155.     end
  156.    
  157.     w.onscroll = function(self, direction, x, y)
  158.       for i = 1, #self.widgets do
  159.         if (self.widgets[i].width == -1 or self.widgets[i].height == -1) or (x >= self.widgets[i].x and y >= self.widgets[i].y and x <= self.widgets[i].x + self.widgets[i].width - 1 and y <= self.widgets[i].y + self.widgets[i].height - 1) then
  160.           self.widgets[i]:onscroll(direction, x, y)
  161.         end
  162.       end
  163.     end
  164.    
  165.     w.onkey = function(self, key)
  166.       for i = 1, #self.widgets do
  167.         self.widgets[i]:onkey(key)
  168.       end
  169.     end
  170.    
  171.     w.onchar = function(self, c)
  172.       for i = 1, #self.widgets do
  173.         self.widgets[i]:onchar(c)
  174.       end
  175.     end
  176.    
  177.     w.draw = function(self, scr)
  178.       local s = self.screen
  179.      
  180.       if scr then
  181.         s = screen.new(scr.term, self.x or 1, self.y or 1, self.width or 1, self.height or 1)
  182.       end
  183.      
  184.       s:clear(self.backgroundcolor)
  185.      
  186.       if self.visible then
  187.         for i = 1, #self.widgets do
  188.           self.widgets[i]:draw(s)
  189.         end
  190.       end
  191.      
  192.       s:draw()
  193.     end
  194.    
  195.     return w
  196.   end
  197. }
  198.  
  199.  
  200. local imageview = {
  201.   new = function(x, y, width, height, image, picformat)
  202.     local w = widget.new(x, y, width, height)
  203.    
  204.     w.image = image or {}
  205.    
  206.     w.picformat = picformat or false
  207.    
  208.     w.draw = function(self, screen)
  209.       if self.visible then
  210.         if self.picformat then
  211.           local otrm = term.current()
  212.           term.redirect(screen.term)
  213.           drawpic(self.x, self.y, self.image, self.backgroundcolor)
  214.           term.redirect(otrm)
  215.         else
  216.           screen:drawImage(self.x, self.y, self.width, self.height, self.image)
  217.         end
  218.       end
  219.     end
  220.    
  221.     return w
  222.   end
  223. }
  224.  
  225.  
  226. local textbox = {
  227.   new = function(x, y, width, height, hint)
  228.     local w = widget.new(x, y, width, height)
  229.    
  230.     w.backgroundcolor = colors.black
  231.     w.textcolor = colors.white
  232.     w.expandable = false
  233.     w.centered = false
  234.     w.hint = hint or ""
  235.     w.text = ""
  236.    
  237.     w.setText = function(self, text)
  238.       self.text = text
  239.     end
  240.    
  241.     w.getText = function(self)
  242.       return self.text
  243.     end
  244.    
  245.     w.draw = function(self, screen)
  246.       if self.visible and self.width >= 1 and self.height >= 1 then
  247.         local x = self.x
  248.         local y = self.y
  249.         local txt = self.text == "" and self.hint or self.text
  250.         local txtc = self.text == "" and colors.lightGray or self.textcolor
  251.        
  252.         if self.centered then
  253.           x = self.x + math.floor(self.width / 2 - #txt / 2)
  254.           y = self.y + math.floor(self.height / 2)
  255.         end
  256.        
  257.         screen:drawBox(self.x, self.y, self.width, self.height, self.backgroundcolor)
  258.        
  259.         for i = 1, #txt, self.width do
  260.           screen:drawText(x, y, string.sub(txt, i, i + self.width - 1), self.backgroundcolor, txtc)
  261.           y = y + 1
  262.         end
  263.       end
  264.     end
  265.    
  266.     w.onkey = function(self, key)
  267.       if self.focused and key == keys.backspace then
  268.         self.text = string.sub(self.text, 1, #self.text - 1)
  269.       end
  270.     end
  271.    
  272.     w.onchar = function(self, c)
  273.       if self.focused and (self.expandable or #self.text < self.width * self.height) then
  274.         self.text = self.text .. c
  275.       end
  276.     end
  277.    
  278.     return w
  279.   end
  280. }
  281.  
  282.  
  283. local label = {
  284.   new = function(x, y, width, height, text)
  285.     local w = widget.new(x, y, width, height)
  286.    
  287.     w.multiline = false
  288.     w.centered = true
  289.     w.backgroundcolor = colors.black
  290.     w.textcolor = colors.white
  291.     w.text = text or ""
  292.    
  293.     w.setText = function(self, text)
  294.       self.text = text
  295.     end
  296.    
  297.     w.getText = function(self)
  298.       return self.text
  299.     end
  300.    
  301.     w.draw = function(self, screen)
  302.       if self.visible and self.width >= 1 and self.height >= 1 then
  303.         local x = self.x
  304.         local y = self.y
  305.        
  306.         if self.centered then
  307.           x = self.x + math.floor(self.width / 2 - #self.text / 2)
  308.           y = self.y + math.floor(self.height / 2)
  309.         end
  310.        
  311.         screen:drawBox(self.x, self.y, self.width, self.height, self.backgroundcolor)
  312.        
  313.         if self.multiline then
  314.           for i = 1, #self.text, self.width do
  315.             screen:drawText(x, y, string.sub(self.text, i, i + self.width - 1), self.backgroundcolor, self.textcolor)
  316.             y = y + 1
  317.           end
  318.         else
  319.           screen:drawText(x, y, self.text, self.backgroundcolor, self.textcolor)
  320.         end
  321.       end
  322.     end
  323.    
  324.     return w
  325.   end
  326. }
  327.  
  328.  
  329. local colorz = {
  330. ["0"] = 1,
  331. ["1"] = 2,
  332. ["2"] = 4,
  333. ["3"] = 8,
  334. ["4"] = 16,
  335. ["5"] = 32,
  336. ["6"] = 64,
  337. ["7"] = 128,
  338. ["8"] = 256,
  339. ["9"] = 512,
  340. ["a"] = 1024,
  341. ["b"] = 2048,
  342. ["c"] = 4096,
  343. ["d"] = 8192,
  344. ["e"] = 16384,
  345. ["f"] = 32768
  346. }
  347. term.clear()
  348. function split(inputstr, sep)
  349.   sep = sep or "%s"
  350.  
  351.   local t = {}
  352.   local i = 1
  353.  
  354.   for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  355.     t[i] = str
  356.     i = i + 1
  357.   end
  358.  
  359.   return t
  360. end
  361. function chars(str)
  362.   local t = {}
  363.   for i = 1, string.len(str) do
  364.     t[i] = string.sub(str, i, i)
  365.   end
  366.  
  367.   return t
  368. end
  369. function loadpic(path)
  370.   local h = fs.open(path, "r")
  371.  
  372.   if h then
  373.     local txt = h.readAll()
  374.     h.close()
  375.    
  376.     local parts = split(txt, ";")
  377.     local size = split(parts[1] or "0,0", ",")
  378.    
  379.     local pic = {
  380.       pixels = chars(parts[2] or ""),
  381.       textcolors = chars(parts[3] or ""),
  382.       text = chars(parts[4] or ""),
  383.       width = tonumber(size[1]) or 0,
  384.       height = tonumber(size[2]) or 0
  385.     }
  386.    
  387.     return pic
  388.   end
  389.  
  390.   return nil
  391. end
  392. function drawpic(sx, sy, pic, background)
  393.   local x = 0
  394.   local y = 0
  395.  
  396.   for i = 1, pic.width * pic.height do
  397.     term.setCursorPos((sx or 1) + x, (sy or 1) + y)
  398.     term.setBackgroundColor(colorz[pic.pixels[i]] or background or colors.black)
  399.    
  400.     if pic.textcolors[i] then
  401.       term.setTextColor(colorz[pic.textcolors[i]])
  402.       term.write(pic.text[i] or " ")
  403.     else
  404.       term.write(" ")
  405.     end
  406.    
  407.     x = x + 1
  408.    
  409.     if x >= pic.width then
  410.       x = 0
  411.       y = y + 1
  412.     end
  413.   end
  414. end
  415.  
  416.  
  417. local bor = bit32.bor
  418. local band = bit32.band
  419. local bxor = bit32.bxor
  420. local blshift = bit32.lshift
  421. local brshift = bit32.arshift
  422. local mod = 2^32
  423. local function rrotate(n, b)
  424.     local s = n/(2^b)
  425.     local f = s%1
  426.     return (s-f) + f*mod
  427. end
  428. local const = {[0]=
  429.     0x243F6A88, 0x85A308D3, 0x13198A2E, 0x03707344,
  430.     0xA4093822, 0x299F31D0, 0x082EFA98, 0xEC4E6C89,
  431.     0x452821E6, 0x38D01377, 0xBE5466CF, 0x34E90C6C,
  432.     0xC0AC29B7, 0xC97C50DD, 0x3F84D5B5, 0xB5470917
  433. }
  434. local iv = {[0]=
  435.     0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
  436.     0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,
  437. }
  438. local sigma = {[0]=
  439.     {[0]= 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15},
  440.     {[0]=14,10, 4, 8, 9,15,13, 6, 1,12, 0, 2,11, 7, 5, 3},
  441.     {[0]=11, 8,12, 0, 5, 2,15,13,10,14, 3, 6, 7, 1, 9, 4},
  442.     {[0]= 7, 9, 3, 1,13,12,11,14, 2, 6, 5,10, 4, 0,15, 8},
  443.     {[0]= 9, 0, 5, 7, 2, 4,10,15,14, 1,11,12, 6, 8, 3,13},
  444.     {[0]= 2,12, 6,10, 0,11, 8, 3, 4,13, 7, 5,15,14, 1, 9},
  445.     {[0]=12, 5, 1,15,14,13, 4,10, 0, 7, 6, 3, 9, 2, 8,11},
  446.     {[0]=13,11, 7,14,12, 1, 3, 9, 5, 0,15, 4, 8, 6, 2,10},
  447.     {[0]= 6,15,14, 9,11, 3, 0, 8,12, 2,13, 7, 1, 4,10, 5},
  448.     {[0]=10, 2, 8, 4, 7, 6, 1, 5,15,11, 9,14, 3,12,13, 0}
  449. }
  450. local function quarterRound(s, a, b, c, d, m, r, i)
  451.     local j, k = sigma[r%10][2*i], sigma[r%10][(2*i)+1]
  452.     s[a] = (s[a]+(s[b]+(bxor(m[j], const[k]))))%mod
  453.     s[d] = rrotate(bxor(s[d], s[a]), 16)
  454.     s[c] = (s[c]+s[d])%mod
  455.     s[b] = rrotate(bxor(s[b], s[c]), 12)
  456.     s[a] = (s[a]+(s[b]+(bxor(m[k], const[j]))))%mod
  457.     s[d] = rrotate(bxor(s[d], s[a]), 8)
  458.     s[c] = (s[c]+s[d])%mod
  459.     s[b] = rrotate(bxor(s[b], s[c]), 7)
  460.     return s
  461. end
  462. local function compress(h, m, s)
  463.     local v = {[0]=
  464.         h[ 0], h[ 1], h[ 2], h[ 3], h[ 4], h[ 5], h[ 6], h[ 7],
  465.         h[ 8], h[ 9], h[10], h[11], h[12], h[13], h[14], h[15]
  466.     }
  467.     for r = 0, 13 do
  468.         v = quarterRound(v, 0, 4, 8,12, m, r, 0)
  469.         v = quarterRound(v, 1, 5, 9,13, m, r, 1)
  470.         v = quarterRound(v, 2, 6,10,14, m, r, 2)
  471.         v = quarterRound(v, 3, 7,11,15, m, r, 3)
  472.         v = quarterRound(v, 0, 5,10,15, m, r, 4)
  473.         v = quarterRound(v, 1, 6,11,12, m, r, 5)
  474.         v = quarterRound(v, 2, 7, 8,13, m, r, 6)
  475.         v = quarterRound(v, 3, 4, 9,14, m, r, 7)
  476.     end
  477.     for i = 0, 7 do
  478.         h[i] = bxor(bxor(bxor(h[i], v[i]), v[i+8]), s[i] or s[i-4])
  479.     end
  480.     return h
  481. end
  482. local function BE_toInt(bs, i)
  483.     return blshift((bs[i+1] or 0), 24) + blshift((bs[i+2] or 0), 16) + blshift((bs[i+3] or 0), 8) + (bs[i+4] or 0)
  484. end
  485. local function incr(t, incr)
  486.     if 0xFFFFFFFF - t[0] < incr then
  487.         t[1] = t[1] + 1
  488.         t[0] = incr - (0xFFFFFFFF - t[0]) - 1      
  489.     else
  490.         t[0] = t[0] + incr
  491.     end
  492.     return t
  493. end
  494. function digest(data, salt)
  495.     local data = type(data) == "string" and {data:byte(1,-1)} or data
  496.     local salt = type(salt) == "string" and {salt:byte(1,-1)} or salt
  497.     local salt = salt or {}
  498.     local m = {}
  499.     local v = {}
  500.     local t = {[0]=0,0}
  501.     local h = {[0]=iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]}
  502.     local s = {[0]=BE_toInt(salt, 0), BE_toInt(salt, 4), BE_toInt(salt, 8), BE_toInt(salt,12)}
  503.     local mLen = #data
  504.     data[#data+1] = 0x80
  505.     while #data%64~=56 do data[#data+1] = 0 end
  506.     data[#data] = bor(data[#data], 1)
  507.     t = incr(t, mLen*8)
  508.     for i = 1, 0, -1 do
  509.         data[#data+1] = band(brshift(band(t[i], 0xFF000000), 24), 0xFF)
  510.         data[#data+1] = band(brshift(band(t[i], 0xFF0000), 16), 0xFF)
  511.         data[#data+1] = band(brshift(band(t[i], 0xFF00), 8), 0xFF)
  512.         data[#data+1] = band(t[i], 0xFF)
  513.     end
  514.     t[0], t[1] = 0, 0
  515.     local blockAmt = math.ceil(#data/64)
  516.     for i = 0, blockAmt-1 do
  517.         for j = 0, 15 do m[j] = BE_toInt(data, (64*i)+4*j) end
  518.         if mLen == 0 then t[0], t[1] = 0, 0
  519.         elseif (mLen-64) >= 0 then
  520.             t = incr(t, 512)
  521.             mLen = mLen-64
  522.         elseif (mLen-64) < 0 then
  523.             t = incr(t, mLen*8)
  524.             mLen = 0
  525.         end
  526.         v = {[0]=
  527.             h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7],
  528.             bxor(s[0], const[0]), bxor(s[1], const[1]), bxor(s[2], const[2]), bxor(s[3], const[3]),
  529.             bxor(t[0], const[4]), bxor(t[0], const[5]), bxor(t[1], const[6]), bxor(t[1], const[7]),
  530.         }
  531.         h = compress(v, m, s)
  532.     end
  533.     return (("%08x"):rep(8)):format(h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7])
  534. end
  535.  
  536.  
  537. local Chars = {}
  538. for Loop = 0, 255 do
  539.    Chars[Loop+1] = string.char(Loop)
  540. end
  541. local String = table.concat(Chars)
  542. local Built = {['.'] = Chars}
  543. local AddLookup = function(CharSet)
  544.    local Substitute = string.gsub(String, '[^'..CharSet..']', '')
  545.    local Lookup = {}
  546.    for Loop = 1, string.len(Substitute) do
  547.        Lookup[Loop] = string.sub(Substitute, Loop, Loop)
  548.    end
  549.    Built[CharSet] = Lookup
  550.    return Lookup
  551. end
  552. function string.random(Length, CharSet)
  553.    -- Length (number)
  554.    -- CharSet (string, optional); e.g. %l%d for lower case letters and digits
  555.    local CharSet = CharSet or '.'
  556.    if CharSet == '' then
  557.       return ''
  558.    else
  559.       local Result = {}
  560.       local Lookup = Built[CharSet] or AddLookup(CharSet)
  561.       local Range = table.getn(Lookup)
  562.       for Loop = 1,Length do
  563.          Result[Loop] = Lookup[math.random(1, Range)]
  564.       end
  565.       return table.concat(Result)
  566.    end
  567. end
  568.  
  569.  
  570. function __setTitle(title)
  571.   multishell.setTitle(multishell.getCurrent(), title)
  572. end
  573. __setTitle("#")
  574. fs.readFile = function(path)
  575.   local h = fs.open(path, "r")
  576.   if h then
  577.     local r = h.readAll()
  578.     h.close()
  579.    
  580.     return r
  581.   end
  582.  
  583.   return nil
  584. end
  585. function endswith(str, e)
  586.   return e == "" or string.sub(str, -string.len(e)) == e
  587. end
  588. local program = {
  589.   open = function(path)
  590.     shell.openTab(path)
  591.     multishell.setFocus(multishell.getCount())
  592.   end
  593. }
  594. local programicon = {
  595.   new = function(x, y, path, lnk)
  596.     local text = fs.getName(path)
  597.    
  598.     local w = imageview.new(x, y, 5, 5, loadpic(fs.combine(path, "icon.pic")), true)
  599.    
  600.     if lnk then
  601.       w.image.pixels[21] = "0"
  602.       w.image.textcolors[21] = "9"
  603.       w.image.text[21] = ">"
  604.       text = fs.getName(lnk)
  605.     end
  606.    
  607.     local l = label.new(x, y + 6, 9, 1, text)
  608.    
  609.     l.multiline = true
  610.     l.backgroundcolor = colors.lightGray
  611.     w.backgroundcolor = colors.lightGray
  612.     w.odraw = w.draw
  613.     w.path = path
  614.     w.l = l
  615.    
  616.     w.onclick = function(self, button, x, y)
  617.       program.open(fs.combine(self.path, "app"))
  618.     end
  619.    
  620.     w.draw = function(self, screen)
  621.       self.l:draw(screen)
  622.       self:odraw(screen)
  623.     end
  624.    
  625.     return w
  626.   end
  627. }
  628. local fileicon = {
  629.   new = function(x, y, path, pic)
  630.     local text = fs.getName(path)
  631.    
  632.     local w = imageview.new(x, y, 5, 5, pic, true)
  633.     local l = label.new(x, y + 6, 9, 1, text)
  634.    
  635.     l.multiline = true
  636.     l.backgroundcolor = colors.lightGray
  637.     w.backgroundcolor = colors.lightGray
  638.     w.odraw = w.draw
  639.     w.path = path
  640.     w.l = l
  641.    
  642.     w.onclick = function(self, button, x, y)
  643.       --program.open("system/app/Explorer.app/app", self.path)
  644.     end
  645.    
  646.     w.draw = function(self, screen)
  647.       self.l:draw(screen)
  648.       self:odraw(screen)
  649.     end
  650.    
  651.     return w
  652.   end
  653. }
  654. local icons = {
  655.   file = loadpic("system/pic/file.pic"),
  656.   folder = loadpic("system/pic/folder.pic"),
  657.   filelnk = loadpic("system/pic/filelnk.pic"),
  658.   folderlnk = loadpic("system/pic/folderlnk.pic")
  659. }
  660. local desktop = layout.new(1, 1, 51, 19)
  661. desktop.backgroundcolor = colors.lightGray
  662. local menu = layout.new(1, 1, 12, 19)
  663. menu.backgroundcolor = colors.gray
  664. local showmenu = false
  665. local menub = label.new(1, 10, 1, 1, ">")
  666. menub.backgroundcolor = desktop.backgroundcolor
  667. menub.onclick = function(self, button, x, y)
  668.   showmenu = true
  669. end
  670. local deskb = label.new(12, 10, 1, 1, "<")
  671. deskb.backgroundcolor = menu.backgroundcolor
  672. deskb.onclick = function(self, button, x, y)
  673.   showmenu = false
  674. end
  675. local name = label.new(1, 2, 12, 1)
  676. name.backgroundcolor = menu.backgroundcolor
  677. name.textcolor = colors.lightGray
  678. local explorer = label.new(1, 5, 12, 1, "Explorer")
  679. explorer.backgroundcolor = menu.backgroundcolor
  680. explorer.onclick = function(self, button, x, y)
  681.   program.open("system/apps/Explorer.app/app")
  682. end
  683. local terminal = label.new(1, 7, 12, 1, "Terminal")
  684. terminal.backgroundcolor = menu.backgroundcolor
  685. terminal.onclick = function(self, button, x, y)
  686.   program.open("system/apps/Terminal.app/app")
  687. end
  688. local settings = label.new(1, 9, 12, 1, "Settings")
  689. settings.backgroundcolor = menu.backgroundcolor
  690. settings.onclick = function(self, button, x, y)
  691.   program.open("system/apps/Settings.app/app")
  692. end
  693. local rebuild = label.new(1, 13, 12, 1, "Rebuild")
  694. rebuild.backgroundcolor = menu.backgroundcolor
  695. rebuild.onclick = function(self, button, x, y)
  696.   shell.run("make")
  697.   os.reboot()
  698. end
  699. local lock = label.new(1, 15, 12, 1, "Lock")
  700. lock.backgroundcolor = menu.backgroundcolor
  701. lock.onclick = function(self, button, x, y)
  702.   stopDesktop()
  703. end
  704. local shutdown = label.new(1, 17, 12, 1, "Shutdown")
  705. shutdown.backgroundcolor = menu.backgroundcolor
  706. shutdown.onclick = function(self, button, x, y)
  707.   os.shutdown()
  708. end
  709. desktop:addWidget(menub)
  710. function loadDesktop()
  711.   local currentuser = settings.get("name", "")
  712.   name.text = currentuser
  713.   local deskpath = "users/" .. currentuser .. "/desktop/"
  714.   local l = fs.list(deskpath)
  715.  
  716.   desktop.widgets = {}
  717.   desktop:addWidget(menub)
  718.  
  719.   for i = 1, #l do
  720.     local f = l[i]
  721.     local fp = deskpath .. f
  722.    
  723.     if endswith(f, ".lnk") then
  724.       local lnkdir = fs.readFile(fp)
  725.       fp = lnkdir or ""
  726.      
  727.       if endswith(fp, ".app") then
  728.         desktop:addWidget(programicon.new(4 + 14 * (i - 1), 2, fp, string.sub(f, 1, #f - 4)))
  729.       elseif fs.isDir(fp) then
  730.         desktop:addWidget(fileicon.new(4 + 14 * (i - 1), 2, fp, icons.folderlnk))
  731.       else
  732.         desktop:addWidget(fileicon.new(4 + 14 * (i - 1), 2, fp, icons.filelnk))
  733.       end
  734.     elseif endswith(f, ".app") then
  735.       desktop:addWidget(programicon.new(4 + 14 * (i - 1), 2, fp))
  736.     elseif fs.isDir(fp) then
  737.       desktop:addWidget(fileicon.new(4 + 14 * (i - 1), 2, fp, icons.folder))
  738.     else
  739.       desktop:addWidget(fileicon.new(4 + 14 * (i - 1), 2, fp, icons.file))
  740.     end
  741.   end
  742. end
  743. menu:addWidget(name)
  744. menu:addWidget(deskb)
  745. menu:addWidget(rebuild)
  746. menu:addWidget(lock)
  747. menu:addWidget(explorer)
  748. menu:addWidget(terminal)
  749. menu:addWidget(settings)
  750. menu:addWidget(shutdown)
  751. local desktoprunning = false
  752. function runDesktop()
  753.   desktoprunning = true
  754.   --__setTitle("#")
  755.  
  756.   while desktoprunning do
  757.     loadDesktop()
  758.    
  759.     desktop:draw()
  760.    
  761.     if showmenu then
  762.       menu:draw()
  763.       menu:handleevent()
  764.     else
  765.       desktop:handleevent()
  766.     end
  767.   end
  768.  
  769.   runLockScreen()
  770. end
  771. function stopDesktop()
  772.   desktoprunning = false
  773. end
  774.  
  775. function getusers()
  776.   local userdir = "users/"
  777.   local flist = fs.list(userdir)
  778.   local userlist = {}
  779.  
  780.   for i = 1, #flist do
  781.     if fs.isDir(userdir .. flist[i]) and fs.exists(fs.combine(userdir .. flist[i], ".user")) then
  782.       table.insert(userlist, userdir .. flist[i])
  783.     end
  784.   end
  785.  
  786.   return userlist
  787. end
  788. function newuser()
  789.   local r = true
  790.  
  791.   local lay = layout.new(1, 1, 51, 19)
  792.   lay.backgroundcolor = colors.lightGray
  793.  
  794.   local lab = label.new(1, 4, 51, 1, "New user")
  795.   lab.backgroundcolor = colors.lightGray
  796.  
  797.   local name = textbox.new(19, 9, 15, 1, "Name")
  798.   name.backgroundcolor = colors.gray
  799.  
  800.   local pass = textbox.new(19, 11, 15, 1, "Password")
  801.   pass.backgroundcolor = colors.gray
  802.  
  803.   local err = label.new(1, 18, 51, 1)
  804.   err.backgroundcolor = colors.lightGray
  805.   err.textcolor = colors.red
  806.   err.centered = true
  807.  
  808.   local done = label.new(23, 13, 6, 1, "Done")
  809.   done.backgroundcolor = colors.gray
  810.   done.onclick = function(self, x, y, button)
  811.     if string.match(name.text, "/") or string.match(name.text, "\\") then
  812.       err.text = "Your name can't contain \"/\" or \"\\\""
  813.       return
  814.     end
  815.    
  816.     if fs.exists("users/" .. name.text) then
  817.       err.text = "User already exists"
  818.       return
  819.     end
  820.    
  821.     lay.widgets = {}
  822.    
  823.     fs.makeDir("users/" .. name.text)
  824.     fs.copy("system/defaults/desktop", "users/" .. name.text .. "/desktop")
  825.    
  826.     -- Idk if this is really necessary
  827.     local f = fs.open("users/" .. name.text .. "/.user", "w")
  828.     f.write("")
  829.     f.close()
  830.    
  831.     local salt = string.random(8, "0123456789abcdef")
  832.    
  833.     settings.load("users/" .. name.text .. "/.user")
  834.     settings.set("name", name.text)
  835.     settings.set("password", digest(pass.text, salt))
  836.     settings.set("passwordsalt", salt)
  837.     settings.save("users/" .. name.text .. "/.user")
  838.    
  839.     r = false
  840.   end
  841.  
  842.   lay:addWidget(lab)
  843.   lay:addWidget(name)
  844.   lay:addWidget(pass)
  845.   lay:addWidget(err)
  846.   lay:addWidget(done)
  847.  
  848.   while r do
  849.     lay:draw()
  850.     lay:handleevent()
  851.   end
  852.  
  853.   settings.load("system/.settings")
  854.   settings.set("lastuser", "users/" .. name.text)
  855.   settings.save("system/.settings")
  856. end
  857. function selectusr(usr, info)
  858.     info.logscreen.widgets = {}
  859.    
  860.     settings.load(usr .. "/.user")
  861.    
  862.     local name = settings.get("name", "")
  863.     local password = settings.get("password", "")
  864.     local salt = settings.get("passwordsalt", "")
  865.    
  866.     local pass = textbox.new(19, 9, 15, 1, "Password")
  867.     pass.backgroundcolor = colors.gray
  868.    
  869.     local name = label.new(23, 5, 7, 1, name)
  870.     name.backgroundcolor = colors.lightGray
  871.    
  872.     local login = label.new(23, 11, 7, 1, "Login")
  873.     login.backgroundcolor = colors.gray
  874.     login.onclick = function(self, x, y, button)
  875.       info.lockscreenrunning = not (digest(pass.text, salt) == password)
  876.     end
  877.    
  878.     info.logscreen:addWidget(back)
  879.     info.logscreen:addWidget(name)
  880.     info.logscreen:addWidget(pass)
  881.     info.logscreen:addWidget(login)
  882.    
  883.     local x = 1
  884.    
  885.     for _, usr in ipairs(users) do
  886.       local initials = string.sub(usr, 7, 7)
  887.      
  888.       for _, s in ipairs(split(string.sub(usr, 7, #usr), " ")) do
  889.         initials = initials .. string.sub(s, 1, 1)
  890.       end
  891.      
  892.       local usrlab = label.new(x, 17, #initials, 3, initials)
  893.       usrlab.backgroundcolor = colors.lightGray
  894.       usrlab.onclick = function(self, x, y, button)
  895.         selectusr(usr, info)
  896.       end
  897.      
  898.       logscreen:addWidget(usrlab)
  899.      
  900.       x = x + #initials
  901.     end
  902.   end
  903. function runLockScreen()
  904.   local lockscreenrunning = true
  905.  
  906.   local lockscreen = layout.new(1, 1, 51, 19)
  907.   lockscreen.backgroundcolor = colors.gray
  908.  
  909.   local logscreen = layout.new(1, 1, 51, 19)
  910.   logscreen.backgroundcolor = colors.lightGray
  911.  
  912.   local curlay = lockscreen
  913.  
  914.   local bg = imageview.new(1, 1, 51, 19, loadpic("system/pic/lockscreen/bg01.pic"), true)
  915.   bg.backgroundcolor = lockscreen.backgroundcolor
  916.   bg.onclick = function(self, x, y, button)
  917.     term.setBackgroundColor(logscreen.backgroundcolor)
  918.    
  919.     for i = 19, 1, -3 do
  920.       sleep(0.05)
  921.       term.setCursorPos(1, i)
  922.       term.clearLine()
  923.       term.setCursorPos(1, i + 1)
  924.       term.clearLine()
  925.       term.setCursorPos(1, i + 2)
  926.       term.clearLine()
  927.     end
  928.    
  929.     curlay = logscreen
  930.   end
  931.  
  932.   lockscreen:addWidget(bg)
  933.  
  934.   local back = label.new(1, 1, 51, 1, "v")
  935.   back.backgroundcolor = logscreen.backgroundcolor
  936.   back.onclick = function(self, x, y, button)
  937.     term.setBackgroundColor(lockscreen.backgroundcolor)
  938.    
  939.     for i = 1, 19, 3 do
  940.       sleep(0.05)
  941.       term.setCursorPos(1, i)
  942.       term.clearLine()
  943.       term.setCursorPos(1, i - 1)
  944.       term.clearLine()
  945.       term.setCursorPos(1, i - 2)
  946.       term.clearLine()
  947.     end
  948.    
  949.     curlay = lockscreen
  950.   end
  951.  
  952.   local users = getusers()
  953.  
  954.   if #users == 0 then
  955.     newuser()
  956.     selectusr(getusers()[1], {["logscreen"] = logscreen, ["lockscreenrunning"] = lockscreenrunning})
  957.   else
  958.     settings.load("system/.settings")
  959.     selectusr(settings.get("lastuser", users[1]), {["logscreen"] = logscreen, ["lockscreenrunning"] = lockscreenrunning})
  960.   end
  961.  
  962.   multishell.lock()
  963.  
  964.   while lockscreenrunning do
  965.     curlay:draw()
  966.     curlay:handleevent()
  967.   end
  968.  
  969.   settings.load("system/.settings")
  970.   settings.set("lastuser", curuser)
  971.   settings.load("system/.settings")
  972.  
  973.   multishell.unlock()
  974.   runDesktop()
  975. end
  976. runLockScreen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement