Advertisement
Freack100

BIOS Mod

Jan 15th, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.43 KB | None | 0 0
  1. --Modificated bios.lua
  2.  
  3. local tEventHandler = {}
  4.  
  5. local tEventTypes = {}
  6.  
  7. term.clear()
  8.  
  9. function print( ... )
  10.     local nLinesPrinted = 0
  11.     for n,v in ipairs( { ... } ) do
  12.         nLinesPrinted = nLinesPrinted + write( tostring( v ) )
  13.     end
  14.     nLinesPrinted = nLinesPrinted + write( "\n" )
  15.     return nLinesPrinted
  16. end
  17.  
  18. print("Setting up main functions...")
  19.  
  20. function sleep( _nTime )
  21.     local timer = os.startTimer( _nTime )
  22.     repeat
  23.         local sEvent, param = os.pullEvent( "timer" )
  24.     until param == timer
  25. end
  26.  
  27. function loadfile(sPath)
  28.     local file = fs.open(sPath,"r")
  29.     local func,err = loadstring(file.readAll(),fs.getName(sPath))
  30.     file.close()
  31.     if func then return func else p("File not found") end
  32. end
  33.  
  34. function os.addEventType(sType)
  35.     if type(sType) ~= "string" then p("string expected, got " .. type(sType)) end
  36.     if tEventTypes[sType] then
  37.         return
  38.     end
  39.     tEventTypes[sType] = true
  40. end
  41.  
  42. function os.addEventHandler(sType,fHandler)
  43.     if not tEventTypes[sType] then p("unknown event type") end
  44.     tEventHandler[sType][#tEventHandler[sType]+1] = fHandler
  45. end
  46.  
  47. --[[
  48. function os.run(_sPath,tAddition,...)
  49.     tArgs = {...}
  50.     fFile, err = loadfile(_sPath)
  51.     if(fFile) then
  52.         local tEnv = tAddition
  53.         setmetatable(tEnv, {__index =  _G})
  54.         setfenv(fFile,tEnv)
  55.         local ok,err = pcall( function fFile( unpack( tArgs ) ) end )
  56.         if not ok then
  57.             error(err)
  58.         end
  59.     end
  60. end
  61. ]]
  62. function read( _sReplaceChar, _tHistory )  
  63.     term.setCursorBlink( true )
  64.  
  65.     local sLine = ""
  66.     local nHistoryPos = nil
  67.     local nPos = 0
  68.     if _sReplaceChar then
  69.         _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  70.     end
  71.    
  72.     local w, h = term.getSize()
  73.     local sx, sy = term.getCursorPos() 
  74.     local function redraw()
  75.         local nScroll = 0
  76.         if sx + nPos >= w then
  77.             nScroll = (sx + nPos) - w
  78.         end
  79.            
  80.         term.setCursorPos( sx, sy )
  81.         term.write( string.rep(" ", w - sx + 1) )
  82.         term.setCursorPos( sx, sy )
  83.         if _sReplaceChar then
  84.             term.write( string.rep(_sReplaceChar, string.len(sLine) - nScroll) )
  85.         else
  86.             term.write( string.sub( sLine, nScroll + 1 ) )
  87.         end
  88.         term.setCursorPos( sx + nPos - nScroll, sy )
  89.     end
  90.    
  91.     while true do
  92.         local sEvent, param = os.pullEvent()
  93.         if sEvent == "char" then
  94.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  95.             nPos = nPos + 1
  96.             redraw()
  97.            
  98.         elseif sEvent == "key" then
  99.             if param == keys.enter then
  100.                 -- Enter
  101.                 break
  102.                
  103.             elseif param == keys.left then
  104.                 -- Left
  105.                 if nPos > 0 then
  106.                     nPos = nPos - 1
  107.                     redraw()
  108.                 end
  109.                
  110.             elseif param == keys.right then
  111.                 -- Right               
  112.                 if nPos < string.len(sLine) then
  113.                     nPos = nPos + 1
  114.                     redraw()
  115.                 end
  116.            
  117.             elseif param == keys.up or param == keys.down then
  118.                 -- Up or down
  119.                 if _tHistory then
  120.                     if param == keys.up then
  121.                         -- Up
  122.                         if nHistoryPos == nil then
  123.                             if #_tHistory > 0 then
  124.                                 nHistoryPos = #_tHistory
  125.                             end
  126.                         elseif nHistoryPos > 1 then
  127.                             nHistoryPos = nHistoryPos - 1
  128.                         end
  129.                     else
  130.                         -- Down
  131.                         if nHistoryPos == #_tHistory then
  132.                             nHistoryPos = nil
  133.                         elseif nHistoryPos ~= nil then
  134.                             nHistoryPos = nHistoryPos + 1
  135.                         end                    
  136.                     end
  137.                    
  138.                     if nHistoryPos then
  139.                         sLine = _tHistory[nHistoryPos]
  140.                         nPos = string.len( sLine )
  141.                     else
  142.                         sLine = ""
  143.                         nPos = 0
  144.                     end
  145.                     redraw()
  146.                 end
  147.             elseif param == keys.backspace then
  148.                 -- Backspace
  149.                 if nPos > 0 then
  150.                     sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  151.                     nPos = nPos - 1                
  152.                     redraw()
  153.                 end
  154.             end
  155.         end
  156.     end
  157.    
  158.     term.setCursorBlink( false )
  159.     term.setCursorPos( w + 1, sy )
  160.     print()
  161.    
  162.     return sLine
  163. end
  164.  
  165. function print( ... )
  166.     local nLinesPrinted = 0
  167.     for n,v in ipairs( { ... } ) do
  168.         nLinesPrinted = nLinesPrinted + write( tostring( v ) )
  169.     end
  170.     nLinesPrinted = nLinesPrinted + write( "\n" )
  171.     return nLinesPrinted
  172. end
  173.  
  174. function os.version()
  175.     return "ExtraBios 0.1"
  176. end
  177.  
  178. local tAPIsLoading = {}
  179. function os.loadAPI( _sPath )
  180.     local sName = fs.getName( _sPath )
  181.     if tAPIsLoading[sName] == true then
  182.         print( "API "..sName.." is already being loaded" )
  183.         return false
  184.     end
  185.     tAPIsLoading[sName] = true
  186.        
  187.     local tEnv = {}
  188.     setmetatable( tEnv, { __index = _G } )
  189.     local fnAPI, err = loadfile( _sPath )
  190.     if fnAPI then
  191.         setfenv( fnAPI, tEnv )
  192.         fnAPI()
  193.     else
  194.         print( err )
  195.         tAPIsLoading[sName] = nil
  196.         return false
  197.     end
  198.    
  199.     local tAPI = {}
  200.     for k,v in pairs( tEnv ) do
  201.         tAPI[k] =  v
  202.     end
  203.  
  204.     _G[sName] = tAPI
  205.    
  206.     tAPIsLoading[sName] = nil
  207.     return true
  208. end
  209.  
  210. function os.unloadAPI( _sName )
  211.     if _sName ~= "_G" and type(_G[_sName]) == "table" then
  212.         _G[_sName] = nil
  213.     end
  214. end
  215.  
  216. local nativetype = type
  217. function type(_object)
  218.     if nativetype(_object) == "table" and _object.__type then return _object.__type else return "table" end
  219. end
  220.  
  221. p("Setting up system events...")
  222.  
  223. --//Setting up system events
  224. os.addEventType("terminated")
  225. os.addEventHandler("terminated", function() os.shutdown() end)
  226.  
  227.  
  228. p("Starting OS")
  229. --//Running the lua prompt and the Eventhandler
  230. local function EventHandler()
  231.     while true do
  232.         local evt,p1,p2,p3,p4,p5 = os.pullEvent()
  233.         if tEventHandler[evt] then
  234.             for _,f in ipairs(tEventHandler[evt]) do
  235.                 f(p1,p2,p3,p4,p5)
  236.             end
  237.         end
  238.     sleep(0)
  239.     end
  240. end
  241.  
  242. parallel.waitForAny(
  243.         EventHandler,
  244.         function() os.run("rom/programs/lua") end
  245. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement