Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --disknet api by KingofGamesYami
- local files = { }
- local oldFiles = {}
- function open( sSide, sFile, sLabel )
- if type( sSide ) ~= "string" or type( sFile ) ~= "string" then
- error( "Expected string, string, got " .. type( sSide) .. ", " .. type( sFile ) .. ", " .. type( sLabel ), 2 )
- end
- if disk.isPresent( sSide ) then
- if disk.hasData( sSide ) then
- files[ #files + 1 ] = { side = sSide, file = sFile, label = sLabel }
- return #files
- else
- error( "No data on disk in drive " .. sSide, 2 )
- end
- else
- error( "No connected drive on the " .. sSide, 2 )
- end
- end
- function close( id )
- if not files[ id ] then
- error( "Invalid disknet ID", 2 )
- end
- files[ id ] = nil
- end
- function send( sMessage, id )
- if type( sMessage ) ~= "string" then
- error( "Expected string, number, got "..type( sMessage ) .. ", " .. type( id ), 2 )
- elseif #files == 0 then
- error( "No open disknet", 2 )
- elseif not files[ id ] then
- error( "Invalid disknet id", 2 )
- else
- disk.setLabel( files[ id ].side, files[ id ].label )
- local filePath = fs.combine( disk.getMountPath( files[ id ].side ), files[ id ].file )
- local tbl
- if fs.exists( filePath ) then
- local file = fs.open( filePath, "r" )
- tbl = textutils.unserialize( file.readAll() )
- file.close()
- else
- tbl = {}
- end
- tbl[ #tbl + 1 ] = { messsage = sMessage, sender = os.getComputerLabel() or os.getComputerID() }
- local file = fs.open( filePath, "w" )
- file.write( textutils.serialize( tbl ) )
- file.close()
- end
- end
- function broadcast( sMessage )
- if type( sMessage ) ~= "string" then
- error( "Expected string, got " .. type( sMessage), 2 )
- end
- for id, _ in pairs( files ) do
- send( sMessage, id )
- end
- end
- function receive( pFilter, nTimeout, _side, _sender )
- local id
- if nTimeout and type( nTimeout ) ~= "number" then
- error( "Expected string, number[, string], got " .. type( pFilter ) .. ", " .. type( nTimeout ) .. "," .. type( _side ), 2 )
- elseif pFilter and type( pFilter ) ~= "string" then
- error( "Expected string, number[, string], got " .. type( pFilter ) .. ", " .. type( nTimeout ) .. "," .. type( _side ), 2 )
- elseif _side and type( _side ) ~= "string" then
- error( "Expected string, number[, string], got " .. type( pFilter ) .. ", " .. type( nTimeout ) .. "," .. type( _side ), 2 )
- end
- if nTimeout then
- id = os.startTimer( nTimeout )
- end
- while true do
- local event = { os.pullEvent() }
- if event[ 1 ] == "disknet_message" then
- if event[ 2 ] == _side or not _side then
- if event[ 3 ] == pFilter or not pFilter then
- return unpack( event )
- end
- end
- elseif event[ 1 ] == "timer" and event[ 2 ] == id then
- return nil
- end
- end
- end
- function isOpen( side, file ) --attempts to find an open "port"
- for k, v in pairs( files ) do
- if v.side == side and v.file == file then
- return k
- end
- end
- return false
- end
- local function checkFile() --checks the files for new data once per tick
- while true do
- for id, tbl in pairs( files ) do
- disk.setLabel( tbl.side, tbl.label )
- local filePath = fs.combine( disk.getMountPath( tbl.side ), tbl.file )
- if fs.getFreeSpace( disk.getMountPath( tbl.side ) ) < 100 then
- local file = fs.open( filePath, "w" )
- file.write( textutils.serialize( {} ) )
- file.close()
- end
- if fs.exists( filePath ) then
- local file = fs.open( filePath, "r" )
- local data = textutils.unserialize( file.readAll() )
- file.close()
- local toSend = {}
- if not oldFiles[ id ] then
- oldFiles[ id ] = {}
- end
- for k, v in ipairs( data ) do
- if oldFiles[ id ][ k ] ~= v then
- toSend[ #toSend + 1 ] = v
- end
- end
- for k, v in pairs( toSend ) do
- os.queueEvent( "disknet_message", tbl.side, tbl.file, v.sender, v.message )
- os.pullEvent()
- oldFiles[ id ] = data
- end
- end
- end
- sleep( 0.1 )
- end
- end
- function redRepeat( side, file ) --sends/recieves disknet/rednet messages back and forth.
- while true do
- local event = { os.pullEvent() }
- if event[ 1 ] == "disknet_message" and event[ 2 ] == side and event[ 3 ] == file then
- rednet.send( event[ 3 ], file )
- elseif event[ 1 ] == "rednet_message" then
- disknet.broadcast( event[ 3 ] )
- end
- end
- end
- local cocheck = coroutine.create( checkFile )
- function os.pullEventRaw( sFilter )
- while true do
- local event = { coroutine.yield() }
- if coroutine.status( cocheck ) == "suspended" then
- coroutine.resume( cocheck, unpack( event ) )
- end
- if event[ 1 ] == sFilter or not sFilter then
- return unpack( event )
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement