Advertisement
MC403

SJNOS-Apps

May 29th, 2015
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.42 KB | None | 0 0
  1. --SJNOS by MC403. applications.sys (gNEADRX9)
  2.  
  3. local tArgs = {...}
  4.  
  5. local COLOR_TEXT = colors.cyan
  6. local COLOR_BG = colors.white
  7. local COLOR_HEAD_TEXT = colors.orange
  8. local COLOR_HEAD_BG = colors.cyan
  9. local COLOR_SIDE_TEXT = colors.cyan
  10. local COLOR_SIDE_BG = colors.lightGray
  11. local COLOR_SIDE_SEL_TEXT = colors.white
  12. local COLOR_SIDE_SEL_BG = colors.orange
  13.  
  14. local function start()
  15.     if (not term.isColor()) then
  16.         return true
  17.     elseif (tArgs[1] == nil) then
  18.         error("Invalid Parapeters.")
  19.     end
  20.  
  21.     os.loadAPI("SJNOS/system/SJNOS/sjn")
  22.  
  23.     local cpos, clear, tcolor, bcolor, getTime = sjn.getStandardFunctions()
  24.     local width, height = term.getSize()
  25.  
  26.     local USERNAME = tArgs[1]
  27.     local USERRANK = sjn.getConfigFileContent("SJNOS/users/"..USERNAME.."/config/.config").d4
  28.  
  29.     local function showDialog(head, texts, buttons, enterButton, delay)
  30.         return sjn.showDialog(head, texts, colors.orange, colors.gray,
  31.             colors.gray, colors.orange, buttons, enterButton, delay)
  32.     end
  33.  
  34.     local function button(text, x, y)
  35.         cpos(x, y)
  36.         tcolor(colors.cyan)
  37.         bcolor(colors.orange)
  38.         write(text)
  39.         tcolor(COLOR_TEXT)
  40.         bcolor(COLOR_BG)
  41.     end
  42.  
  43.     local apps = {}
  44.     local selected = 1
  45.     local VIEW_IMAGE, VIEW_LIST = 1, 2
  46.     local view = VIEW_IMAGE
  47.  
  48.     local function refresh()
  49.         local list = fs.list("SJNOS/users/"..USERNAME.."/home/apps")
  50.         apps = {}
  51.         for i=1,#list do
  52.             local data = sjn.getApp("SJNOS/users/"..USERNAME.."/home/apps".."/"..list[i])
  53.             if (data ~= nil) then
  54.                 data.root = "SJNOS/users/"..USERNAME.."/home/apps/appdata/"..data.header.name.."/"
  55.                 apps[#apps+1] = data
  56.             end
  57.         end
  58.  
  59.         if (selected > #apps) then
  60.             selected = 1
  61.         end
  62.     end
  63.  
  64.     refresh()
  65.  
  66.     local DLG_STANDARD, DLG_MENU, DLG_APP_INFO = 0, 1, 2
  67.     local dlg = DLG_STANDARD
  68.  
  69.     local function drawTopBar()
  70.         bcolor(COLOR_BG)
  71.         clear()
  72.         cpos(1, 1)
  73.         bcolor(COLOR_HEAD_BG)
  74.         tcolor(COLOR_HEAD_TEXT)
  75.         term.clearLine()
  76.         write("Apps")
  77.         cpos(47, 1)
  78.         write(getTime())
  79.     end
  80.  
  81.     local function drawMain()
  82.         tcolor(COLOR_TEXT)
  83.         bcolor(COLOR_BG)
  84.  
  85.         if (#apps > 0) then
  86.             local app = apps[selected]
  87.             cpos(1, 5)
  88.            
  89.             local imagetype = app.header.imagetype
  90.             local imagedata = app.header.imagedata
  91.             local name = app.header.name
  92.  
  93.             local image = {{}}
  94.  
  95.             local function getPath(file)
  96.                 return app.root .. file
  97.             end
  98.  
  99.             if (imagetype ~= nil and imagedata ~= nil) then
  100.                 if (imagetype == "data") then
  101.                     image = sjn.getPaintableFromData(imagedata, 12, 8)
  102.                 elseif (imagetype == "source") then
  103.                     if (fs.exists(getPath(imagedata))) then
  104.                         image = paintutils.loadImage(getPath(imagedata))
  105.                     end
  106.                 elseif (imagetype == "pastebin") then
  107.                     local temp = "SJNOS/users/"..USERNAME.."/config/.temp"
  108.                     fs.delete(temp)
  109.                     shell.run("pastebin", "get", imagedata, temp)
  110.                     image = paintutils.loadImage(temp)
  111.                     fs.delete(temp)
  112.                 end
  113.             end
  114.  
  115.             local x = math.floor((width-12) / 2 + 1)
  116.             local y = 6
  117.             bcolor(colors.lightGray)
  118.             for i=1,8 do
  119.                 cpos(x, y-1+i)
  120.                 write("            ")
  121.             end
  122.  
  123.             for i=1, #image do
  124.                 if (i > 8) then
  125.                     image[i] = {}
  126.                 end
  127.                 for j=1, #image[i] do
  128.                     if (j > 12) then
  129.                         image[i][j] = 0
  130.                     end
  131.                 end
  132.             end
  133.  
  134.             paintutils.drawImage(image, x, y)
  135.  
  136.             tcolor(COLOR_TEXT)
  137.             bcolor(COLOR_BG)
  138.             cpos((width - #name) / 2 + 1, 15)
  139.             write(name)
  140.  
  141.             button("+", 1, 19)
  142.  
  143.             local text = selected .. "/" .. #apps
  144.             cpos(52-#text, 19)
  145.             write(text)
  146.  
  147.             cpos(17, y+4)
  148.             write("<")
  149.  
  150.             cpos(34, y+4)
  151.             write(">")
  152.         else
  153.             local text = "No Apps installed."
  154.             cpos((width - #text) / 2 + 1, 7)
  155.             write(text)
  156.         end
  157.     end
  158.  
  159.     local function draw()
  160.         refresh()
  161.         drawTopBar()
  162.         drawMain()
  163.  
  164.         if (dlg == DLG_MENU) then
  165.             cpos(1, 1)
  166.             bcolor(colors.gray)
  167.             tcolor(colors.orange)
  168.             write("Apps ")
  169.  
  170.             local menu = {
  171.                 " List View   ",
  172.                 " Search      ",
  173.                 " Install     ",
  174.                 " Deinstall   ",
  175.                 " Help        ",
  176.                 " Quit        "
  177.             }
  178.  
  179.             for i=1,#menu do
  180.                 cpos(1, i + 1)
  181.                 write(menu[i])
  182.             end
  183.         elseif (dlg == DLG_APP_INFO) then
  184.             for y=2,19 do
  185.                 cpos(1, y)
  186.                 term.clearLine()
  187.             end
  188.  
  189.             local app = apps[selected]
  190.  
  191.             local files, storage = 1, fs.getSize(app.path)
  192.  
  193.             local function getFolderSize(folderPath)
  194.                 local list = fs.list(folderPath)
  195.                 for i=1, #list do
  196.                     local path = folderPath.."/"..list[i]
  197.                     if (fs.isDir(path)) then
  198.                         getFolderSize(path)
  199.                     else
  200.                         files = files + 1
  201.                         storage = storage + fs.getSize(path)
  202.                     end
  203.                 end
  204.             end
  205.  
  206.             getFolderSize(app.root)
  207.  
  208.             local function advanced_write(para1, para2, line)
  209.                 para1 = tostring(para1)
  210.                 para2 = tostring(para2)
  211.                 cpos(17 - #para1/2, line)
  212.                 write(para1)
  213.                 cpos(34 - #para2/2, line)
  214.                 tcolor(colors.orange)
  215.                 write(para2)
  216.                 tcolor(COLOR_TEXT)
  217.             end
  218.  
  219.             tcolor(COLOR_TEXT)
  220.             bcolor(COLOR_BG)
  221.  
  222.             advanced_write("Name", app.header.name, 6)
  223.             advanced_write("Author", app.header.author, 7)
  224.             advanced_write("Lines", app.program.lines, 8)
  225.             advanced_write("Files", files, 9)
  226.             advanced_write("Storage", storage, 10)
  227.  
  228.             button("CLOSE", 24, 4)
  229.         end
  230.     end
  231.  
  232.     local function change(up)
  233.         if (up) then
  234.             selected = selected + 1
  235.             if (selected > #apps) then
  236.                 selected = 1
  237.             end
  238.         else
  239.             selected = selected - 1
  240.             if (selected < 1) then
  241.                 selected = #apps
  242.             end
  243.         end
  244.     end
  245.  
  246.     local running = true
  247.     local timer = os.startTimer(1)
  248.  
  249.     while running do
  250.         draw()
  251.         local apps_installed = #apps > 0
  252.         local event, btn, x, y = os.pullEvent()
  253.         if (dlg == DLG_STANDARD) then
  254.             if (event == "mouse_click") then
  255.                 if (y == 1 and x <= 5) then
  256.                     --Menu
  257.                     dlg = DLG_MENU
  258.                 elseif (x == 17 and y == 10 and apps_installed) then
  259.                     --Left Arrow
  260.                     change(false)
  261.                 elseif (x == 34 and y == 10 and apps_installed) then
  262.                     --Right Arrow
  263.                     change(true)
  264.                 elseif (x == 1 and y == 19 and apps_installed) then
  265.                     --More Infos
  266.                     dlg = DLG_APP_INFO
  267.                 elseif (x >= 20 and x <= 32 and y >= 6 and y <= 17 and apps_installed) then
  268.                     --Click on the App
  269.                     local app = apps[selected]
  270.                     if (btn == 1) then
  271.                         --Run
  272.                         local temp = "SJNOS/users/"..USERNAME.."/config/.temp"
  273.                         local f = fs.open(temp, "w")
  274.  
  275.                         for i=1, #app.program.code do
  276.                             f.writeLine(app.program.code[i])
  277.                         end
  278.                         f.close()
  279.  
  280.                         tcolor(colors.white)
  281.                         bcolor(colors.black)
  282.                         cpos(1, 1)
  283.                         clear()
  284.                         shell.run(temp)
  285.  
  286.                         tcolor(colors.orange)
  287.                         bcolor(colors.gray)
  288.                         cpos(1, 19)
  289.                         term.clearLine()
  290.                         write("[SJNOS: Press any key to exit the application.]")
  291.                         local event = os.pullEvent()
  292.                         while (event ~= "key" and event ~= "mouse_click") do
  293.                             event = os.pullEvent()
  294.                         end
  295.                        
  296.                         local f = fs.open(temp, "w")
  297.                         f.close()
  298.                     elseif (btn == 2) then
  299.                         local action = showDialog(app.header.name, {"Action?","                                               "}, {{colors.lime, colors.gray, "Deinstall"},{colors.lime, colors.gray, "Path"},{colors.red, colors.gray, "Close"}}, 3)
  300.                         draw()
  301.                         if (action == 1) then
  302.                             --Deinstall
  303.                             if (showDialog(app.header.name, {"Do you really want to deinstall","'"..app.header.name.."'?"}, {{colors.lime, colors.gray, "Deinstall"},{colors.red, colors.gray, "Close"}}, 2)==1) then
  304.                                 sjn.deinstallApp(app.path)
  305.                             end
  306.                         elseif (action == 2) then
  307.                             --Get Path
  308.                             showDialog(app.header.name, {"C:/"..app.path}, {{colors.lime, colors.gray, "Okay"}})
  309.                         end
  310.                     end
  311.                 end
  312.             elseif (event == "key") then
  313.                 if (btn == 203) then
  314.                     change(false)
  315.                 elseif (btn == 205) then
  316.                     change(true)
  317.                 end
  318.             end
  319.         elseif (dlg == DLG_MENU) then
  320.             if (event == "mouse_click") then
  321.                 if (x <= 13 and y > 1 and y <= 7) then
  322.                     if (y == 2) then
  323.                         --List View
  324.                         showDialog("List View", {"Sorry, this part of SJNOS is", "not programmed yet."}, {{colors.lime, colors.gray, "Okay"}})
  325.                     elseif (y == 3) then
  326.                         --Search
  327.                         showDialog("Search", {"Sorry, this part of SJNOS is", "not programmed yet."}, {{colors.lime, colors.gray, "Okay"}})
  328.                     elseif (y == 4) then
  329.                         --Install
  330.                         local result, path = sjn.getUserFile(USERNAME, "Select a file to be installed.")
  331.                         if (result) then
  332.                             local result, e = sjn.installApp(path, USERNAME)
  333.                             if (result) then
  334.                                 showDialog("Installing", {"Installed!"}, {{colors.lime, colors.gray, "Okay"}})
  335.                             else
  336.                                 showDialog("Installing", {"Error!", e}, {{colors.lime, colors.gray, "Okay"}})
  337.                             end
  338.                         end
  339.                     elseif (y == 5) then
  340.                         --Deinstall
  341.                         showDialog("Deinstall", {"Sorry, this part of SJNOS is", "not programmed yet."}, {{colors.lime, colors.gray, "Okay"}})
  342.                     elseif (y == 6) then
  343.                         --Help
  344.                         showDialog("Help", {"Sorry, this part of SJNOS is", "not programmed yet."}, {{colors.lime, colors.gray, "Okay"}})
  345.                     elseif (y == 7) then
  346.                         --Quit
  347.                         running = false
  348.                     end
  349.                 end
  350.                 dlg = DLG_STANDARD
  351.             end
  352.         elseif (dlg == DLG_APP_INFO) then
  353.             if (event == "mouse_click") then
  354.                 if (x >= 24 and x <= 28 and y == 4) then
  355.                     --Close
  356.                     dlg = DLG_STANDARD
  357.                 end
  358.             end
  359.         end
  360.         timer = os.startTimer(1)
  361.     end
  362.    
  363.     tcolor(colors.white)
  364.     bcolor(colors.black)
  365.     clear()
  366.     cpos(1, 1)
  367.  
  368.     return true
  369. end
  370.  
  371. local ok, err = pcall(start)
  372. if not ok then
  373.     if (not sjn.drawErrorScreen(err, tArgs[1])) then
  374.         shell.run("SJNOS/start.exe")
  375.     end
  376. end
  377. os.unloadAPI("SJNOS/system/SJNOS/sjn")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement