xXm0dzXx

CraftOS 2.0 - WIP

Sep 29th, 2012
1,244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.10 KB | None | 0 0
  1.  
  2. local parentShell = shell
  3.  
  4. local bExit = false
  5. local sDir = (parentShell and parentShell.dir()) or ""
  6. local sPath = (parentShell and parentShell.path()) or ".:/rom/programs"
  7. local tAliases = (parentShell and parentShell.aliases()) or {}
  8. local tProgramStack = {}
  9.  
  10. local shell = {}
  11. local tEnv = {
  12.     ["shell"] = shell,
  13. }
  14.  
  15. local function cPrint(text)
  16.    local x,y = term.getSize()
  17.    local x2,y2 = term.getCursorPos()
  18.    term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
  19.    write(text.. "\n")
  20. end
  21.  
  22. -- Install shell API
  23. function shell.run( _sCommand, ... )
  24.     local sPath = shell.resolveProgram( _sCommand )
  25.     if sPath ~= nil then
  26.         tProgramStack[#tProgramStack + 1] = sPath
  27.         local result = os.run( tEnv, sPath, ... )
  28.         tProgramStack[#tProgramStack] = nil
  29.         return result
  30.     else
  31.         print( "No such program" )
  32.         return false
  33.     end
  34. end
  35.  
  36. function shell.exit()
  37.     bExit = true
  38. end
  39.  
  40. function shell.dir()
  41.     return sDir
  42. end
  43.  
  44. function shell.setDir( _sDir )
  45.     sDir = _sDir
  46. end
  47.  
  48. function shell.path()
  49.     return sPath
  50. end
  51.  
  52. function shell.setPath( _sPath )
  53.     sPath = _sPath
  54. end
  55.  
  56. function shell.resolve( _sPath )
  57.     local sStartChar = string.sub( _sPath, 1, 1 )
  58.     if sStartChar == "/" or sStartChar == "\\" then
  59.         return fs.combine( "", _sPath )
  60.     else
  61.         return fs.combine( sDir, _sPath )
  62.     end
  63. end
  64.  
  65. function shell.resolveProgram( _sCommand )
  66.     -- Substitute aliases firsts
  67.     if tAliases[ _sCommand ] ~= nil then
  68.         _sCommand = tAliases[ _sCommand ]
  69.     end
  70.  
  71.     -- If the path is a global path, use it directly
  72.     local sStartChar = string.sub( _sCommand, 1, 1 )
  73.     if sStartChar == "/" or sStartChar == "\\" then
  74.         local sPath = fs.combine( "", _sCommand )
  75.         if fs.exists( sPath ) and not fs.isDir( sPath ) then
  76.             return sPath
  77.         end
  78.         return nil
  79.     end
  80.    
  81.     -- Otherwise, look on the path variable
  82.     for sPath in string.gmatch(sPath, "[^:]+") do
  83.         sPath = fs.combine( shell.resolve( sPath ), _sCommand )
  84.         if fs.exists( sPath ) and not fs.isDir( sPath ) then
  85.             return sPath
  86.         end
  87.     end
  88.    
  89.     -- Not found
  90.     return nil
  91. end
  92.  
  93. function shell.programs( _bIncludeHidden )
  94.     local tItems = {}
  95.    
  96.     -- Add programs from the path
  97.     for sPath in string.gmatch(sPath, "[^:]+") do
  98.         sPath = shell.resolve( sPath )
  99.         if fs.isDir( sPath ) then
  100.             local tList = fs.list( sPath )
  101.             for n,sFile in pairs( tList ) do
  102.                 if not fs.isDir( fs.combine( sPath, sFile ) ) and
  103.                    (_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= ".") then
  104.                     tItems[ sFile ] = true
  105.                 end
  106.             end
  107.         end
  108.     end
  109.  
  110.     -- Sort and return
  111.     local tItemList = {}
  112.     for sItem, b in pairs( tItems ) do
  113.         table.insert( tItemList, sItem )
  114.     end
  115.     table.sort( tItemList )
  116.     return tItemList
  117. end
  118.  
  119. function shell.getRunningProgram()
  120.     if #tProgramStack > 0 then
  121.         return tProgramStack[#tProgramStack]
  122.     end
  123.     return nil
  124. end
  125.  
  126. function shell.setAlias( _sCommand, _sProgram )
  127.     tAliases[ _sCommand ] = _sProgram
  128. end
  129.  
  130. function shell.clearAlias( _sCommand )
  131.     tAliases[ _sCommand ] = nil
  132. end
  133.  
  134. function shell.aliases()
  135.     -- Add aliases
  136.     local tCopy = {}
  137.     for sAlias, sCommand in pairs( tAliases ) do
  138.         tCopy[sAlias] = sCommand
  139.     end
  140.     return tCopy
  141. end
  142.  
  143. local function fixedRead( _sReplaceChar, _tHistory )   
  144.     term.setCursorBlink( true )
  145.  
  146.     local sLine = ""
  147.     local nHistoryPos = nil
  148.     local nPos = 0
  149.     if _sReplaceChar then
  150.         _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  151.     end
  152.    
  153.     local w, h = term.getSize()
  154.     local sx, sy = term.getCursorPos() 
  155.     local function redraw()
  156.         local nScroll = 0
  157.         if sx + nPos >= w then
  158.             nScroll = (sx + nPos) - w
  159.         end
  160.            
  161.         term.setCursorPos( sx, sy )
  162.         term.write( string.rep(" ", 14) )
  163.         term.setCursorPos( sx, sy )
  164.         if _sReplaceChar then
  165.             term.write( string.rep(_sReplaceChar, string.len(sLine) - nScroll) )
  166.         else
  167.             term.write( string.sub( sLine, nScroll + 1 ) )
  168.         end
  169.         term.setCursorPos( sx + nPos - nScroll, sy )
  170.     end
  171.    
  172.     while true do
  173.         local sEvent, param = os.pullEvent()
  174.         if sEvent == "char" then
  175.             if nPos < 14 then
  176.                 sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  177.                 nPos = nPos + 1
  178.             end
  179.             redraw()
  180.            
  181.         elseif sEvent == "key" then
  182.             if param == keys.enter then
  183.                 -- Enter
  184.                 break
  185.                
  186.             elseif param == keys.left then
  187.                 -- Left
  188.                 if nPos > 0 then
  189.                     nPos = nPos - 1
  190.                     redraw()
  191.                 end
  192.                
  193.             elseif param == keys.right then
  194.                 -- Right               
  195.                 if nPos < string.len(sLine) then
  196.                     nPos = nPos + 1
  197.                     redraw()
  198.                 end
  199.            
  200.             elseif param == keys.up or param == keys.down then
  201.                 -- Up or down
  202.                 if _tHistory then
  203.                     if param == keys.up then
  204.                         -- Up
  205.                         if nHistoryPos == nil then
  206.                             if #_tHistory > 0 then
  207.                                 nHistoryPos = #_tHistory
  208.                             end
  209.                         elseif nHistoryPos > 1 then
  210.                             nHistoryPos = nHistoryPos - 1
  211.                         end
  212.                     else
  213.                         -- Down
  214.                         if nHistoryPos == #_tHistory then
  215.                             nHistoryPos = nil
  216.                         elseif nHistoryPos ~= nil then
  217.                             nHistoryPos = nHistoryPos + 1
  218.                         end                    
  219.                     end
  220.                    
  221.                     if nHistoryPos then
  222.                         sLine = _tHistory[nHistoryPos]
  223.                         nPos = string.len( sLine )
  224.                     else
  225.                         sLine = ""
  226.                         nPos = 0
  227.                     end
  228.                     redraw()
  229.                 end
  230.             elseif param == keys.backspace then
  231.                 -- Backspace
  232.                 if nPos > 0 then
  233.                     sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  234.                     nPos = nPos - 1                
  235.                     redraw()
  236.                 end
  237.             end
  238.         end
  239.     end
  240.    
  241.     term.setCursorBlink( false )
  242.     term.setCursorPos( w + 1, sy )
  243.     print()
  244.    
  245.     return sLine
  246. end
  247.  
  248. local function newVersion()
  249.     return "CraftOS 2.0"
  250. end
  251.    
  252. os.version = newVersion
  253. if not fs.exists(".accounts") then
  254.     term.clear()
  255.     term.setCursorPos(1,3)
  256.     cPrint( "+-----| " ..os.version().. " |-----+")
  257.     cPrint( "|>Welcome to " ..os.version().. "!<|")
  258.     cPrint( "|Since this is your first,|")
  259.     cPrint( "|You will have to setup ur|")
  260.     cPrint( "|    computer.. Enjoy!    |")
  261.     cPrint( "|                         |")
  262.     cPrint( "| Press enter to continue |")
  263.     cPrint( "|                         |")
  264.     cPrint( "+-------------------------+")
  265.     repeat
  266.         event, key = os.pullEventRaw("key")
  267.     until key == keys.enter
  268.     term.clear()
  269.     term.setCursorPos(1,3)
  270.     cPrint( "+-----| Setup : Pg1 |-----+")
  271.     cPrint( "| Register account:       |")
  272.     cPrint( "|                         |")
  273.     cPrint( "| Username:               |")
  274.     cPrint( "|                         |")
  275.     cPrint( "| Password:               |")
  276.     cPrint( "| Leave password blank to |")
  277.     cPrint( "| not ask for pass on boot|")
  278.     cPrint( "+-------------------------+")
  279.     repeat
  280.         term.setCursorPos(24,6)
  281.         username = fixedRead()
  282.     until username ~= ""
  283.    
  284.     term.setCursorPos(24,8)
  285.     password = fixedRead()
  286.    
  287.     term.clear()
  288.     term.setCursorPos(1,3)
  289.     cPrint( "+-----| Setup : Pg2 |-----+")
  290.     cPrint( "| Label computer (opt):   |")
  291.     cPrint( "|                         |")
  292.     cPrint( "| The computer label is   |")
  293.     cPrint( "| like the name of the PC,|")
  294.     cPrint( "| it lets you keep ur file|")
  295.     cPrint( "| after your PC is broken.|")
  296.     cPrint( "| This step is optional.  |")
  297.     cPrint( "+-------------------------+")
  298.     pcID = read()
  299.     os.setComputerLabel(pcID)
  300.     fs.makeDir(".accounts")
  301.     theAccount = fs.open(".accounts/" ..username, "w")
  302.     theAccount.write(password)
  303.     theAccount.close()
  304.    
  305.     fs.makeDir(".permissions")
  306.     fs.makeDir(".permissions/" ..username)
  307. end
  308.    
  309.  
  310. -- If this is the toplevel shell, run the startup programs
  311. if parentShell == nil then
  312.     -- Run the startup from the ROM first
  313.     local sRomStartup = shell.resolveProgram( "/rom/startup" )
  314.     if sRomStartup then
  315.         shell.run( sRomStartup )
  316.     end
  317.    
  318.     -- Then run the user created startup, from the disks or the root
  319.     local sUserStartup = shell.resolveProgram( "/startup" )
  320.     for n,sSide in pairs( redstone.getSides() ) do
  321.         if disk.isPresent( sSide ) and disk.hasData( sSide ) then
  322.             local sDiskStartup = shell.resolveProgram( fs.combine(disk.getMountPath( sSide ), "startup") )
  323.             if sDiskStartup then
  324.                 sUserStartup = sDiskStartup
  325.                 break
  326.             end
  327.         end
  328.     end
  329.    
  330.     if sUserStartup then
  331.         shell.run( sUserStartup )
  332.     end
  333. end
  334.  
  335. -- Run any programs passed in as arguments
  336. local tArgs = { ... }
  337. if #tArgs > 0 then
  338.     shell.run( ... )
  339. end
  340.  
  341. -- Read commands and execute them
  342. term.clear()
  343. term.setCursorPos(1,3)
  344. local oldevv = os.pullEvent
  345. os.pullEvent = os.pullEventRaw
  346. cPrint( "+-----| " ..os.version().. " |-----+")
  347. cPrint( "|   Login to your CC2.0   |")
  348. cPrint( "|         Account         |")
  349. cPrint( "| Username:               |")
  350. cPrint( "|                         |")
  351. cPrint( "| Password:               |")
  352. cPrint( "| Password not required if|")
  353. cPrint( "| blank when registered.  |")
  354. cPrint( "+-------------------------+")
  355. repeat
  356.     term.setCursorPos(24,6)
  357.     username = fixedRead()
  358. until username ~= ""
  359.    
  360. term.setCursorPos(24,8)
  361. password = fixedRead()
  362.  
  363. tPass = fs.open(".accounts/" ..username, "r")
  364. if tPass then
  365.     contents = tPass.readAll()
  366.     tPass.close()
  367. else
  368.     term.clear()
  369.     term.setCursorPos(1,5)
  370.     cPrint( "+-----| Login Failz |-----+")
  371.     cPrint( "|   Incorrect user/pass   |")
  372.     cPrint( "+-------------------------+")
  373.     sleep(1)
  374.     os.reboot()
  375. end
  376.  
  377. if contents ~= password then
  378.     term.clear()
  379.     term.setCursorPos(1,5)
  380.     cPrint( "+-----| Login Failz |-----+")
  381.     cPrint( "|   Incorrect user/pass   |")
  382.     cPrint( "+-------------------------+")
  383.     sleep(1)
  384.     os.reboot()
  385. end
  386. os.pullEvent = oldevv
  387.  
  388. ----------------------------------------------
  389. --Begin the OS rendering and everything else--
  390. ----------------------------------------------
  391. term.clear()
  392. term.setCursorPos(1,1)
  393. local x,y = term.getSize()
  394.  
  395. local tCommandHistory = {}
  396. local currentX = 1
  397. local currentY = 1
  398. local x,y = term.getSize()
  399. local tProgramsRunning = {}
  400. local isEnter = false;
  401. local tCursor = ">";
  402. local startMenu = false;
  403. local startExtend = false;
  404.  
  405. function setCursor( cursortype )
  406.     tCursor = cursortype
  407. end
  408.  
  409. function getX()
  410.     return currentX
  411. end
  412.  
  413. function getY()
  414.     return currentY
  415. end
  416.  
  417. function newButton( text, pos1, pos2, functor )
  418.     local xx,yy = term.getCursorPos()
  419.     term.setCursorPos(pos1,pos2)
  420.     write(text)
  421.     term.setCursorPos(xx,yy)
  422.  
  423.     if isEnter and currentX == pos1-1 and currentY == pos2 then
  424.         if tostring(functor) == functor then
  425.             if fs.isDir(functor) then
  426.                 error() --Gonna open file browser
  427.             else
  428.                 addRunningProgram("Error")
  429.                 shell.run(functor)
  430.                 delRunningProgram("Error")
  431.             end
  432.         else
  433.             functor()
  434.         end
  435.     end
  436. end
  437.  
  438. function runStart()
  439.     if startMenu then
  440.         startMenu = false
  441.     else
  442.         startMenu = true
  443.     end
  444. end
  445.  
  446. function extStart()
  447.     if startExtend then
  448.         startExtend = false
  449.     else
  450.         startExtend = true
  451.     end
  452. end
  453.  
  454. function newAccount()
  455.     term.setCursorPos(1,3)
  456.     cPrint( "+-----| Setup : Pg1 |-----+")
  457.     cPrint( "| Register account:       |")
  458.     cPrint( "|                         |")
  459.     cPrint( "| Username:               |")
  460.     cPrint( "|                         |")
  461.     cPrint( "| Password:               |")
  462.     cPrint( "| Leave password blank to |")
  463.     cPrint( "| not ask for pass on boot|")
  464.     cPrint( "+-------------------------+")
  465.     repeat
  466.         term.setCursorPos(24,6)
  467.         username = fixedRead()
  468.     until username ~= ""
  469.    
  470.     term.setCursorPos(24,8)
  471.     password = fixedRead()
  472.    
  473.     theAccount = fs.open(".accounts/" ..username, "w")
  474.     theAccount.write(password)
  475.     theAccount.close()
  476.    
  477.     fs.makeDir(".permissions/" ..username)
  478. end
  479.  
  480. function isRunning( tProgramz )
  481.     for i=1,#tProgramsRunning do
  482.         if tProgramsRunning[i] == tProgramz then
  483.             return true
  484.         end
  485.     end
  486.     return false
  487. end
  488.  
  489. function addRunningProgram( pName )
  490.     if isRunning( pName ) ~= true then
  491.         tProgramsRunning[#tProgramsRunning+1] = pName
  492.     end
  493. end
  494.  
  495. function delRunningProgram( pName )
  496.     for i=1,#tProgramsRunning do
  497.         if tProgramsRunning[i] == pName then
  498.             tProgramsRunning[i] = nil
  499.             return true
  500.         end
  501.     end
  502.     return false
  503. end
  504.  
  505. function start()
  506.     term.setCursorPos(1, y-10)
  507.     write("----------+")
  508.     local files = fs.list("/")
  509.     function clearHiddenFiles( files )
  510.         local tItems = {}
  511.         for i=1,#files do
  512.             if (string.sub( files[i], 1, 1 ) ~= ".") then
  513.                 if files[i] ~= "rom" then
  514.                     tItems[#tItems+1] = files[i]
  515.                 end
  516.             end
  517.         end
  518.        
  519.         return tItems
  520.     end
  521.    
  522.     local tFiles = clearHiddenFiles( files )
  523.     for n=1,#tFiles do
  524.         if n < 6 then
  525.             newButton(string.sub(tFiles[n],1, 10), 2, (y-10)+n, tFiles[n])
  526.         elseif startExtend then
  527.             newButton(string.sub(tFiles[n],1, 10), 13, 1+(n-6), tFiles[n])
  528.         end
  529.     end
  530.     if #tFiles > 6 then
  531.         newButton("More",2, y-2, extStart)
  532.     end
  533.    
  534.     if startExtend then
  535.         for n=1,y-2 do
  536.             term.setCursorPos(11, n)
  537.             if(n == y-10)then
  538.                 write("+")
  539.             else
  540.                 write("|")
  541.             end
  542.             term.setCursorPos(23, n)
  543.             write("|")
  544.         end
  545.     else
  546.         for n=1,8 do
  547.             term.setCursorPos(11, 9+n )
  548.             write("|")
  549.         end
  550.     end
  551. end
  552.  
  553. function redraw()
  554.     term.clear()
  555.     welcomeMenu()
  556.     newButton("Start | ", 2, y, runStart)
  557.     for i=1,#tProgramsRunning do
  558.         term.setCursorPos(#("Start |  ")+i, y)
  559.         write(tProgramsRunning[i].. " | ")
  560.     end
  561.     term.setCursorPos(x-#("| " ..textutils.formatTime(os.time()).. "  "), y)
  562.     write("| " ..textutils.formatTime(os.time()).. " ")
  563.    
  564.     for i=1,x-1 do
  565.         term.setCursorPos(i,y-1)
  566.         if i==8 then
  567.             write("+")
  568.         elseif i==(x-11) then
  569.             write("+")
  570.         else
  571.             write("-")
  572.         end
  573.     end
  574.    
  575.     if startMenu then start() end
  576.    
  577.     term.setCursorPos(currentX,currentY)
  578.     write(tCursor)
  579. end
  580.    
  581. function welcomeMenu()
  582.     if not fs.exists(".hidewelcome") then
  583.         term.setCursorPos(1,3)
  584.         cPrint( "+-| Welcome |-------------+")
  585.         cPrint( "| Welcome to your desktop!|")
  586.         cPrint( "| Use arrow keys to nav.  |")
  587.         cPrint( "| Use enter to select     |")
  588.         cPrint( "| OS by xXm0dzXx          |")
  589.         cPrint( "|                         |")
  590.         cPrint( "|     [Enter to exit]     |")
  591.         cPrint( "|                         |")
  592.         cPrint( "+-------------------------+")
  593.         addRunningProgram("Welcome")
  594.     end
  595. end
  596.  
  597. while not bExit do
  598.     redraw()
  599.     os.startTimer(1)
  600.     local event, key = os.pullEvent()
  601.     if event == "timer" and key == isEnterlol then
  602.         isEnter = false;
  603.     elseif event == "key" then
  604.         if key == keys.up then
  605.             if currentY > 1 then
  606.                 currentY = currentY -1
  607.             end
  608.         elseif key == keys.down then
  609.             if currentY < y then
  610.                 currentY = currentY +1
  611.             end
  612.         elseif key == keys.left then
  613.             if currentX > 1 then
  614.                 currentX = currentX -1
  615.             end
  616.         elseif key == keys.right then
  617.             if currentX < x-1 then
  618.                 currentX = currentX +1
  619.             end
  620.         elseif key == keys.enter then
  621.             isEnterlol = os.startTimer(0)
  622.             isEnter = true;
  623.             if not fs.exists(".hidewelcome") then
  624.                 fs.makeDir(".hidewelcome")
  625.                 delRunningProgram("Welcome")
  626.             end
  627.         end
  628.     end
  629. end
  630.  
  631. while not bExit do
  632.     write( shell.dir() .. "> " )
  633.  
  634.     local sLine = read( nil, tCommandHistory )
  635.     table.insert( tCommandHistory, sLine )
  636.    
  637.     local tWords = {}
  638.     for match in string.gmatch(sLine, "[^ \t]+") do
  639.         table.insert( tWords, match )
  640.     end
  641.  
  642.     local sCommand = tWords[1]
  643.     if sCommand then
  644.         shell.run( sCommand, unpack( tWords, 2 ) )
  645.     end
  646. end
  647.  
  648. -- If this is the toplevel shell, run the shutdown program
  649. if parentShell == nil then
  650.     if shell.resolveProgram( "shutdown" ) then
  651.         shell.run( "shutdown" )
  652.     end
  653.     os.shutdown() -- just in case
  654. end
Advertisement
Add Comment
Please, Sign In to add comment