Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Modificated bios.lua
- local tEventHandler = {}
- local tEventTypes = {}
- term.clear()
- function print( ... )
- local nLinesPrinted = 0
- for n,v in ipairs( { ... } ) do
- nLinesPrinted = nLinesPrinted + write( tostring( v ) )
- end
- nLinesPrinted = nLinesPrinted + write( "\n" )
- return nLinesPrinted
- end
- print("Setting up main functions...")
- function sleep( _nTime )
- local timer = os.startTimer( _nTime )
- repeat
- local sEvent, param = os.pullEvent( "timer" )
- until param == timer
- end
- function loadfile(sPath)
- local file = fs.open(sPath,"r")
- local func,err = loadstring(file.readAll(),fs.getName(sPath))
- file.close()
- if func then return func else p("File not found") end
- end
- function os.addEventType(sType)
- if type(sType) ~= "string" then p("string expected, got " .. type(sType)) end
- if tEventTypes[sType] then
- return
- end
- tEventTypes[sType] = true
- end
- function os.addEventHandler(sType,fHandler)
- if not tEventTypes[sType] then p("unknown event type") end
- tEventHandler[sType][#tEventHandler[sType]+1] = fHandler
- end
- --[[
- function os.run(_sPath,tAddition,...)
- tArgs = {...}
- fFile, err = loadfile(_sPath)
- if(fFile) then
- local tEnv = tAddition
- setmetatable(tEnv, {__index = _G})
- setfenv(fFile,tEnv)
- local ok,err = pcall( function fFile( unpack( tArgs ) ) end )
- if not ok then
- error(err)
- end
- end
- end
- ]]
- function read( _sReplaceChar, _tHistory )
- term.setCursorBlink( true )
- local sLine = ""
- local nHistoryPos = nil
- local nPos = 0
- if _sReplaceChar then
- _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
- end
- local w, h = term.getSize()
- local sx, sy = term.getCursorPos()
- local function redraw()
- local nScroll = 0
- if sx + nPos >= w then
- nScroll = (sx + nPos) - w
- end
- term.setCursorPos( sx, sy )
- term.write( string.rep(" ", w - sx + 1) )
- term.setCursorPos( sx, sy )
- if _sReplaceChar then
- term.write( string.rep(_sReplaceChar, string.len(sLine) - nScroll) )
- else
- term.write( string.sub( sLine, nScroll + 1 ) )
- end
- term.setCursorPos( sx + nPos - nScroll, sy )
- end
- while true do
- local sEvent, param = os.pullEvent()
- if sEvent == "char" then
- sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
- nPos = nPos + 1
- redraw()
- elseif sEvent == "key" then
- if param == keys.enter then
- -- Enter
- break
- elseif param == keys.left then
- -- Left
- if nPos > 0 then
- nPos = nPos - 1
- redraw()
- end
- elseif param == keys.right then
- -- Right
- if nPos < string.len(sLine) then
- nPos = nPos + 1
- redraw()
- end
- elseif param == keys.up or param == keys.down then
- -- Up or down
- if _tHistory then
- if param == keys.up then
- -- Up
- if nHistoryPos == nil then
- if #_tHistory > 0 then
- nHistoryPos = #_tHistory
- end
- elseif nHistoryPos > 1 then
- nHistoryPos = nHistoryPos - 1
- end
- else
- -- Down
- if nHistoryPos == #_tHistory then
- nHistoryPos = nil
- elseif nHistoryPos ~= nil then
- nHistoryPos = nHistoryPos + 1
- end
- end
- if nHistoryPos then
- sLine = _tHistory[nHistoryPos]
- nPos = string.len( sLine )
- else
- sLine = ""
- nPos = 0
- end
- redraw()
- end
- elseif param == keys.backspace then
- -- Backspace
- if nPos > 0 then
- sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
- nPos = nPos - 1
- redraw()
- end
- end
- end
- end
- term.setCursorBlink( false )
- term.setCursorPos( w + 1, sy )
- print()
- return sLine
- end
- function print( ... )
- local nLinesPrinted = 0
- for n,v in ipairs( { ... } ) do
- nLinesPrinted = nLinesPrinted + write( tostring( v ) )
- end
- nLinesPrinted = nLinesPrinted + write( "\n" )
- return nLinesPrinted
- end
- function os.version()
- return "ExtraBios 0.1"
- end
- local tAPIsLoading = {}
- function os.loadAPI( _sPath )
- local sName = fs.getName( _sPath )
- if tAPIsLoading[sName] == true then
- print( "API "..sName.." is already being loaded" )
- return false
- end
- tAPIsLoading[sName] = true
- local tEnv = {}
- setmetatable( tEnv, { __index = _G } )
- local fnAPI, err = loadfile( _sPath )
- if fnAPI then
- setfenv( fnAPI, tEnv )
- fnAPI()
- else
- print( err )
- tAPIsLoading[sName] = nil
- return false
- end
- local tAPI = {}
- for k,v in pairs( tEnv ) do
- tAPI[k] = v
- end
- _G[sName] = tAPI
- tAPIsLoading[sName] = nil
- return true
- end
- function os.unloadAPI( _sName )
- if _sName ~= "_G" and type(_G[_sName]) == "table" then
- _G[_sName] = nil
- end
- end
- local nativetype = type
- function type(_object)
- if nativetype(_object) == "table" and _object.__type then return _object.__type else return "table" end
- end
- p("Setting up system events...")
- --//Setting up system events
- os.addEventType("terminated")
- os.addEventHandler("terminated", function() os.shutdown() end)
- p("Starting OS")
- --//Running the lua prompt and the Eventhandler
- local function EventHandler()
- while true do
- local evt,p1,p2,p3,p4,p5 = os.pullEvent()
- if tEventHandler[evt] then
- for _,f in ipairs(tEventHandler[evt]) do
- f(p1,p2,p3,p4,p5)
- end
- end
- sleep(0)
- end
- end
- parallel.waitForAny(
- EventHandler,
- function() os.run("rom/programs/lua") end
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement