AquaJD

[A'sP] SkySHELL

Nov 2nd, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.08 KB | None | 0 0
  1. local parentShell = shell
  2. local bExit = false
  3. local sDir = (parentShell and parentShell.dir()) or ""
  4. local sPath = (parentShell and parentShell.path()) or ".:/rom/programs"
  5. local tAliases = (parentShell and parentShell.aliases()) or {}
  6. local tProgramStack = {}
  7.  
  8. local w,h = term.getSize()
  9. local version = "0.8"
  10.  
  11. local shell = {}
  12. local tCommands = {
  13.     ["disk"] = function() if fs.exists("/disk") and fs.isDir("/disk") then sDir = "/disk" else print("Disk not found") end end,
  14.     ["appstore"] = function() if term.isColor then launchAppStore() else print("Adv. Computer needed") end end,
  15.     ["version"] = function() print(version) end,
  16.     ["about"] = function() print("SkySHELL was made by AutoLocK") end,
  17.     ["msg"] = function() write("Message> ") local msg = read() write("ID> ") local id = tonumber(read()) write("Modem Side> ") local side = read() rednet.open(side) rednet.send(id,msg) end,
  18.     ["broadcast"] = function() write("Message> ") local msg = read() write("Modem Side> ") local side = read() rednet.open(side) rednet.broadcast(msg) end,
  19.     ["list"] = function() print("/disk - Goes to Disk's directory") print("/appstore - Runs the Appstore") print("/version - Prints version") print("/about - About SkySHELL") print("/msg - Sends a message to an ID") print("/broadcast - Broadcasts a message") print("/list - Lists all commands") end,
  20. }
  21. local appCodes = {
  22.     ["FileManager"] = { code = "pnzdr8FB" },
  23.     ["NPaintPro"] = { code = "udDhEHeV" },
  24.     ["LuaIDE"] = { code = "vyAZc6tJ" },
  25.     ["SavviAnimation"] = { code = "ePY9eCDH" },
  26.     ["LightShot"] = { code = "g0nVKvgr" },
  27.     ["FireWolf"] = { code = "bY8YDUJ1" },
  28.     ["Thunderhawk"] = { code = "R9Eb4XuG" },
  29.     ["RPGame"] = { code = "COMING" }
  30. }
  31. local tEnv = {
  32.     ["shell"] = shell,
  33. }
  34.  
  35. -- GUI Colors
  36. if term.isColor() then
  37.     promptColor = colors.lime
  38.     textColor = colors.lime
  39.     bgColor = colors.black
  40.     tBarTCol = colors.white
  41.     tBarBCol = colors.lime
  42.     highlightColour = colours.white
  43.     keywordColour = colours.lime
  44.     commentColour = colours.magenta
  45.     stringColour = colours.orange
  46. else
  47.     promptColor = colors.white
  48.     textColor = colors.white
  49.     bgColor = colors.black
  50.     tBarTCol = colors.black
  51.     tBarBCol = colors.white
  52.     highlightColour = colours.white
  53.     keywordColour = colours.white
  54.     commentColour = colours.white
  55.     stringColour = colours.white
  56. end
  57.  
  58. -- Neutral Functions
  59. local function run( _sCommand, ... )
  60.     local sPath = shell.resolveProgram( _sCommand )
  61.     if sPath ~= nil then
  62.         tProgramStack[#tProgramStack + 1] = sPath
  63.         local result = os.run( tEnv, sPath, ... )
  64.         tProgramStack[#tProgramStack] = nil
  65.         return result
  66.     else
  67.         printError( "No such program" )
  68.         return false
  69.     end
  70. end
  71.  
  72. local function runLine( _sLine )
  73.     local tWords = {}
  74.     for match in string.gmatch( _sLine, "[^ \t]+" ) do
  75.         table.insert( tWords, match )
  76.     end
  77.  
  78.     local sCommand = tWords[1]
  79.     if sCommand then
  80.         return run( sCommand, unpack( tWords, 2 ) )
  81.     end
  82.     return false
  83. end
  84.  
  85. function shell.run( ... )
  86.     return runLine( table.concat( { ... }, " " ) )
  87. end
  88.  
  89. function shell.exit()
  90.     bExit = true
  91. end
  92.  
  93. function shell.dir()
  94.     return sDir
  95. end
  96.  
  97. function shell.setDir( _sDir )
  98.     sDir = _sDir
  99. end
  100.  
  101. function shell.path()
  102.     return sPath
  103. end
  104.  
  105. function shell.setPath( _sPath )
  106.     sPath = _sPath
  107. end
  108.  
  109. function shell.resolve( _sPath )
  110.     local sStartChar = string.sub( _sPath, 1, 1 )
  111.     if sStartChar == "/" or sStartChar == "\\" then
  112.         return fs.combine( "", _sPath )
  113.     else
  114.         return fs.combine( sDir, _sPath )
  115.     end
  116. end
  117.  
  118. function shell.resolveProgram( _sCommand )
  119.     if tAliases[ _sCommand ] ~= nil then
  120.         _sCommand = tAliases[ _sCommand ]
  121.     end
  122.     local sStartChar = string.sub( _sCommand, 1, 1 )
  123.     if sStartChar == "/" or sStartChar == "\\" then
  124.         local sPath = fs.combine( "", _sCommand )
  125.         if fs.exists( sPath ) and not fs.isDir( sPath ) then
  126.             return sPath
  127.         end
  128.         return nil
  129.     end
  130.     for sPath in string.gmatch(sPath, "[^:]+") do
  131.         sPath = fs.combine( shell.resolve( sPath ), _sCommand )
  132.         if fs.exists( sPath ) and not fs.isDir( sPath ) then
  133.             return sPath
  134.         end
  135.     end
  136.     return nil
  137. end
  138.  
  139. function shell.programs( _bIncludeHidden )
  140.     local tItems = {}
  141.     for sPath in string.gmatch(sPath, "[^:]+") do
  142.         sPath = shell.resolve( sPath )
  143.         if fs.isDir( sPath ) then
  144.             local tList = fs.list( sPath )
  145.             for n,sFile in pairs( tList ) do
  146.                 if not fs.isDir( fs.combine( sPath, sFile ) ) and
  147.                    (_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= ".") then
  148.                     tItems[ sFile ] = true
  149.                 end
  150.             end
  151.         end
  152.     end
  153.  
  154.     -- Sort and return
  155.     local tItemList = {}
  156.     for sItem, b in pairs( tItems ) do
  157.         table.insert( tItemList, sItem )
  158.     end
  159.     table.sort( tItemList )
  160.     return tItemList
  161. end
  162.  
  163. function shell.getRunningProgram()
  164.     if #tProgramStack > 0 then
  165.         return tProgramStack[#tProgramStack]
  166.     end
  167.     return nil
  168. end
  169.  
  170. function shell.setAlias( _sCommand, _sProgram )
  171.     tAliases[ _sCommand ] = _sProgram
  172. end
  173.  
  174. function shell.clearAlias( _sCommand )
  175.     tAliases[ _sCommand ] = nil
  176. end
  177.  
  178. function shell.aliases()
  179.     local tCopy = {}
  180.     for sAlias, sCommand in pairs( tAliases ) do
  181.         tCopy[sAlias] = sCommand
  182.     end
  183.     return tCopy
  184. end
  185.  
  186. function draw_tBar()
  187.     term.setTextColor(tBarTCol)
  188.     term.setBackgroundColor(tBarBCol)
  189.     term.setCursorPos(1,1)
  190.     term.clearLine()
  191.     term.setCursorPos(1,1)
  192.     print("SkySHELL "..version)
  193.     term.setTextColor(tBarTCol)
  194.     term.setBackgroundColor(tBarBCol)
  195.     term.setCursorPos(51-#sDir,1)
  196.     print(sDir)
  197. end
  198.  
  199. function as_drawBox(X,Y,W,H,title,color1,color2,bClosable)
  200.     moveY = Y+1
  201.     term.setCursorPos(X,Y)
  202.     term.setBackgroundColor(color1)
  203.     term.setTextColor(color2)
  204.     print(string.rep(" ",W))
  205.     term.setCursorPos(X,Y)
  206.     print(title)
  207.     term.setCursorPos(X,Y+1)
  208.     term.setBackgroundColor(color2)
  209.     for i=1,H-1 do
  210.         term.setCursorPos(X,moveY)
  211.         print(string.rep(" ",W))
  212.         moveY = moveY + 1
  213.     end
  214.     if bClosable == true then
  215.         term.setCursorPos(X+W-1,Y)
  216.         term.setTextColor(colors.red)
  217.         term.setBackgroundColor(color1)
  218.         print("X")
  219.     end
  220. end
  221.  
  222. function as_addApp(guiname,name,code,x,y,color1,color2)
  223.     as_drawBox(x,y,10,7,guiname,color1,color2,false)
  224.     term.setCursorPos(x+1,y+2)
  225.     term.setTextColor(textColor)
  226.     print("Code:")
  227.     term.setCursorPos(x+1,y+3)
  228.     print(code)
  229.     term.setTextColor(color2)
  230.     term.setBackgroundColor(color1)
  231.     if fs.exists("/Skyrom/Apps/"..name) then
  232.         term.setCursorPos(x+1,y+5)
  233.         print(" Launch ")
  234.     else
  235.         term.setCursorPos(x,y+5)
  236.         print(" Download ")
  237.     end
  238. end
  239.  
  240. function as_downloadApp(name,code)
  241.     local appFile = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(code))
  242.     local appCont = appFile.readAll()
  243.     appFile.close()
  244.     local file = fs.open("/Skyrom/Apps/"..name,"w")
  245.     file.write(appCont)
  246.     file.close()
  247.     return true
  248. end
  249.  
  250. function as_drawGUI()
  251.     term.setBackgroundColor(bgColor)
  252.     term.clear()
  253.     as_addApp("FileManag.","FileManager",appCodes["FileManager"].code,2,2,colors.red,colors.white)
  254.     as_addApp("NPaintPro","NPaintPro",appCodes["NPaintPro"].code,2,10,colors.green,colors.white)
  255.     as_addApp("LuaIDE","LuaIDE",appCodes["LuaIDE"].code,15,2,colors.blue,colors.white)
  256.     as_addApp("SavviAnim.","SavviAnimation",appCodes["SavviAnimation"].code,15,10,colors.orange,colors.white)
  257.     as_addApp("LightShot","LightShot",appCodes["LightShot"].code,28,2,colors.blue,colors.white)
  258.     as_addApp("FireWolf","FireWolf",appCodes["FireWolf"].code,28,10,colors.blue,colors.white)
  259.     as_addApp("Thunderha.","Thunderhawk",appCodes["Thunderhawk"].code,41,2,colors.blue,colors.white)
  260.     as_addApp("RPGame","RPGame",appCodes["RPGame"].code,41,10,colors.orange,colors.white)
  261.     term.setCursorPos(16,18)
  262.     term.setBackgroundColor(bgColor)
  263.     term.setTextColor(promptColor)
  264.     print("Press 'Enter' to Exit")
  265. end
  266.  
  267. function launchAppStore()
  268.     as_drawGUI()
  269.     while true do
  270.     local event,button,X,Y = os.pullEvent()
  271.     if event == "mouse_click" then
  272.         if X >= 2 and X <= 12 and Y == 7 and button == 1 then
  273.             if fs.exists("/Skyrom/Apps/FileManager") then
  274.                 shell.run("/Skyrom/Apps/FileManager")
  275.                 sleep(.1)
  276.             else
  277.                 local status = as_downloadApp("FileManager",appCodes["FileManager"].code)
  278.                 if status == true then
  279.                     term.setCursorPos(4,6)
  280.                     term.setBackgroundColor(colors.black)
  281.                     term.setTextColor(Colors.red)
  282.                     print("Complete!")
  283.                     sleep(1)
  284.                     as_drawGUI()
  285.                 else
  286.                     term.setCursorPos(4,6)
  287.                     term.setBackgroundColor(colors.black)
  288.                     term.setTextColor(colors.red)
  289.                     print("Failure!")
  290.                     sleep(1)
  291.                     as_drawGUI()
  292.                 end
  293.             end
  294.         elseif X >= 2 and X <= 12 and Y == 15 and button == 1 then
  295.             if fs.exists("/Skyrom/Apps/NPaintPro") then
  296.                 shell.run("/Skyrom/Apps/NPaintPro")
  297.                 sleep(.1)
  298.             else
  299.                 local status = as_downloadApp("NPaintPro",appCodes["NPaintPro"].code)
  300.                 if status == true then
  301.                     term.setCursorPos(4,12)
  302.                     term.setBackgroundColor(colors.black)
  303.                     term.setTextColor(colors.lime)
  304.                     print("Complete!")
  305.                     sleep(1)
  306.                     as_drawGUI()
  307.                 else
  308.                     term.setCursorPos(4,12)
  309.                     term.setBackgroundColor(colors.black)
  310.                     term.setTextColor(colors.lime)
  311.                     print("Failure!")
  312.                     sleep(1)
  313.                     as_drawGUI()
  314.                 end
  315.             end
  316.         elseif X >= 15 and X <= 25 and Y == 7 and button == 1 then
  317.             if fs.exists("/Skyrom/Apps/LuaIDE") then
  318.                 shell.run("/Skyrom/Apps/LuaIDE")
  319.                 sleep(.1)
  320.             else
  321.                 local status = as_downloadApp("LuaIDE",appCodes["LuaIDE"].code)
  322.                 if status == true then
  323.                     term.setCursorPos(15,7)
  324.                     term.setBackgroundColor(colors.black)
  325.                     term.setTextColor(colors.blue)
  326.                     print("Complete!")
  327.                     sleep(1)
  328.                     as_drawGUI()
  329.                 else
  330.                     term.setCursorPos(15,12)
  331.                     term.setBackgroundColor(colors.black)
  332.                     term.setTextColor(colors.blue)
  333.                     sleep(1)
  334.                     as_drawGUI()
  335.                 end
  336.             end
  337.         elseif X >= 15 and X <= 25 and Y == 15 and button == 1 then
  338.             if fs.exists("/Skyrom/Apps/SavviAnimation") then
  339.                 shell.run("/Skyrom/Apps/SavviAnimation")
  340.                 sleep(.1)
  341.             else
  342.                 local status = as_downloadApp("SavviAnimation",appCodes["SavviAnimation"].code)
  343.                 if status == true then
  344.                     term.setCursorPos(15,15)
  345.                     term.setBackgroundColor(colors.black)
  346.                     term.setTextColor(colors.orange)
  347.                     print("Complete!")
  348.                     sleep(1)
  349.                     as_drawGUI()
  350.                 else
  351.                     term.setCursorPos(15,15)
  352.                     term.setBackgroundColor(colors.black)
  353.                     term.setTextColor(colors.orange)
  354.                     print("Failure!")
  355.                     sleep(1)
  356.                     as_drawGUI()
  357.                 end
  358.             end
  359.         elseif X >= 28 and X <= 38 and Y == 7 and button == 1 then
  360.             if fs.exists("/Skyrom/Apps/LightShot") then
  361.                 shell.run("/Skyrom/Apps/LightShot")
  362.                 sleep(.1)
  363.             else
  364.                 local status = as_downloadApp("LightShot",appCodes["LightShot"].code)
  365.                 if status == true then
  366.                     term.setCursorPos(15,15)
  367.                     term.setBackgroundColor(colors.black)
  368.                     term.setTextColor(colors.blue)
  369.                     print("Complete!")
  370.                     sleep(1)
  371.                     as_drawGUI()
  372.                 else
  373.                     term.setCursorPos(15,15)
  374.                     term.setBackgroundColor(colors.black)
  375.                     term.setTextColor(colors.blue)
  376.                     print("Failure!")
  377.                     sleep(1)
  378.                     as_drawGUI()
  379.                 end
  380.             end
  381.         elseif X >= 28 and X <= 38 and Y == 15 and button == 1 then
  382.             if fs.exists("/Skyrom/Apps/FireWolf") then
  383.                 shell.run("/Skyrom/Apps/FireWolf")
  384.                 sleep(.1)
  385.             else
  386.                 local status = as_downloadApp("FireWolf",appCodes["FireWolf"].code)
  387.                 if status == true then
  388.                     term.setCursorPos(15,15)
  389.                     term.setBackgroundColor(colors.black)
  390.                     term.setTextColor(colors.blue)
  391.                     print("Complete!")
  392.                     sleep(1)
  393.                     as_drawGUI()
  394.                 else
  395.                     term.setCursorPos(15,15)
  396.                     term.setBackgroundColor(colors.black)
  397.                     term.setTextColor(colors.blue)
  398.                     print("Failure!")
  399.                     sleep(1)
  400.                     as_drawGUI()
  401.                 end
  402.             end
  403.         elseif X >= 40 and X <= 50 and Y == 7 and button == 1 then
  404.             if fs.exists("/Skyrom/Apps/Thunderhawk") then
  405.                 shell.run("/Skyrom/Apps/Thunderhawk")
  406.                 sleep(.1)
  407.             else
  408.                 local status = as_downloadApp("Thunderhawk",appCodes["Thunderhawk"].code)
  409.                 if status == true then
  410.                     term.setCursorPos(15,15)
  411.                     term.setBackgroundColor(colors.black)
  412.                     term.setTextColor(colors.blue)
  413.                     print("Complete!")
  414.                     sleep(1)
  415.                     as_drawGUI()
  416.                 else
  417.                     term.setCursorPos(15,15)
  418.                     term.setBackgroundColor(colors.black)
  419.                     term.setTextColor(colors.blue)
  420.                     print("Failure!")
  421.                     sleep(1)
  422.                     as_drawGUI()
  423.                 end
  424.             end
  425.         elseif X >= 40 and X <= 50 and Y == 15 and button == 1 then
  426.             if fs.exists("/Skyrom/Apps/RPGame") then
  427.                 shell.run("/Skyrom/Apps/RPGame")
  428.                 sleep(.1)
  429.             else
  430.                 local status = as_downloadApp("RPGame",appCodes["RPGame"].code)
  431.                 if status == true then
  432.                     term.setCursorPos(15,15)
  433.                     term.setBackgroundColor(colors.black)
  434.                     term.setTextColor(colors.orange)
  435.                     print("Complete!")
  436.                     sleep(1)
  437.                     as_drawGUI()
  438.                 else
  439.                     term.setCursorPos(15,15)
  440.                     term.setBackgroundColor(colors.black)
  441.                     term.setTextColor(colors.orange)
  442.                     print("Failure!")
  443.                     sleep(1)
  444.                     as_drawGUI()
  445.                 end
  446.             end
  447.         else
  448.             as_drawGUI()
  449.         end
  450.     elseif event == "key" then
  451.         if button == keys.enter then
  452.             term.setBackgroundColor(bgColor)
  453.             term.clear()
  454.             draw_tBar()
  455.             break
  456.         else
  457.             as_drawGUI()
  458.         end
  459.     end
  460. end
  461. end
  462.  
  463.  
  464. term.clear()
  465. draw_tBar()
  466. term.setTextColor(textColor)
  467. term.setBackgroundColor(bgColor)
  468.  
  469. if not fs.exists("/Skyrom") and not fs.isDir("/Skyrom") then
  470.     fs.makeDir("/Skyrom")
  471.     fs.makeDir("/Skyrom/Apps")
  472. elseif fs.exists("/Skyrom") and not fs.isDir("/Skyrom") then
  473.     fs.move("/Skyrom","/OLD.Skyrom")
  474.     fs.makeDir("/Skyrom")
  475.     fs.makeDir("/Skyrom/Apps")
  476. end
  477.  
  478. -- Run DISK Startup
  479. local sDiskStartup = shell.resolveProgram( "/disk/startup" )
  480. if sDiskStartup then
  481.     print("Launch disk?")
  482.     term.setTextColor( promptColor )
  483.     write("> ")
  484.     term.setTextColor( textColor )
  485.     local answer = string.lower(read())
  486.     if answer == "yes" then
  487.         shell.run("/disk/startup")
  488.         sleep(1)
  489.         term.clear()
  490.         draw_tBar()
  491.     elseif answer == "y" then
  492.         shell.run("/disk/startup")
  493.         sleep(1)
  494.         term.clear()
  495.         draw_tBar()
  496.     elseif answer == "no" then
  497.         print("Ignored.")
  498.         sleep(1)
  499.         term.clear()
  500.         draw_tBar()
  501.     elseif answer == "n" then
  502.         print("Ignored.")
  503.         sleep(1)
  504.         term.clear()
  505.         draw_tBar()
  506.     else
  507.         if sUserStartup then
  508.             shell.run( sUserStartup )
  509.             sleep(1)
  510.             term.clear()
  511.             draw_tBar()
  512.         else
  513.             term.clear()
  514.             draw_tBar()
  515.         end
  516.     end
  517. -- If not DISK Startup then run USER Startup
  518. elseif sUserStartup then
  519.     shell.run( sUserStartup )
  520.     sleep(1)
  521.     term.clear()
  522.     draw_tBar()
  523. end
  524.  
  525. -- Reads commands and runs them
  526. local tCommandHistory = {}
  527. draw_tBar()
  528. while not bExit do
  529.     term.setBackgroundColor( bgColor )
  530.     term.setTextColor( promptColor )
  531.     write("> ")
  532.     term.setTextColor(textColor)
  533.     local sLine = read(nil,tCommandHistory)
  534.     table.insert(tCommandHistory,sLine)
  535.     if string.lower(sLine) == "clear" then
  536.         runLine(sLine)
  537.         draw_tBar()
  538.     elseif string.lower(sLine:sub(1,1)) == "/" then
  539.         if tCommands[sLine:sub(2,#sLine)] then
  540.             tCommands[sLine:sub(2,#sLine)]()
  541.         else
  542.             printError("Command unknown")
  543.         end
  544.     elseif string.lower(sLine:sub(1,4)) == "edit" then
  545.         runLine(sLine)
  546.         draw_tBar()
  547.     else
  548.         runLine(sLine)
  549.         local x,y = term.getCursorPos()
  550.         draw_tBar()
  551.         term.setCursorPos(x,y)
  552.     end
  553. end
  554.  
  555. if parentShell == nil then
  556.     if shell.resolveProgram( "shutdown" ) then
  557.         shell.run( "shutdown" )
  558.     end
  559.     os.shutdown()
  560. end
Advertisement
Add Comment
Please, Sign In to add comment