Advertisement
King0fGamesYami

disknet

Aug 4th, 2014
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.56 KB | None | 0 0
  1. --disknet api by KingofGamesYami
  2. local files = { }
  3. local oldFiles = {}
  4.  
  5. function open( sSide, sFile, sLabel )
  6.     if type( sSide ) ~= "string" or type( sFile ) ~= "string" then
  7.         error( "Expected string, string, got " .. type( sSide) .. ", " .. type( sFile ) .. ", " .. type( sLabel ), 2 )
  8.     end
  9.     if disk.isPresent( sSide ) then
  10.         if disk.hasData( sSide ) then
  11.             files[ #files + 1 ] = { side = sSide, file = sFile, label = sLabel }
  12.             return #files
  13.         else
  14.             error( "No data on disk in drive " .. sSide, 2 )
  15.         end
  16.     else
  17.         error( "No connected drive on the " .. sSide, 2 )
  18.     end
  19. end
  20.  
  21. function close( id )
  22.     if not files[ id ] then
  23.         error( "Invalid disknet ID", 2 )
  24.     end
  25.     files[ id ] = nil
  26. end
  27.  
  28. function send( sMessage, id )
  29.     if type( sMessage ) ~= "string" then
  30.         error( "Expected string, number, got "..type( sMessage ) .. ", " .. type( id ), 2 )
  31.     elseif #files == 0 then
  32.         error( "No open disknet", 2 )
  33.     elseif not files[ id ] then
  34.         error( "Invalid disknet id", 2 )
  35.     else
  36.         disk.setLabel( files[ id ].side, files[ id ].label )
  37.         local filePath = fs.combine( disk.getMountPath( files[ id ].side ), files[ id ].file )
  38.         local tbl
  39.         if fs.exists( filePath ) then
  40.             local file = fs.open( filePath, "r" )
  41.             tbl = textutils.unserialize( file.readAll() )
  42.             file.close()
  43.         else
  44.             tbl = {}
  45.         end
  46.         tbl[ #tbl + 1 ] = { messsage = sMessage, sender = os.getComputerLabel() or os.getComputerID() }
  47.         local file = fs.open( filePath, "w" )
  48.         file.write( textutils.serialize( tbl ) )
  49.         file.close()
  50.     end
  51. end
  52.  
  53. function broadcast( sMessage )
  54.     if type( sMessage ) ~= "string" then
  55.         error( "Expected string, got " .. type( sMessage), 2 )
  56.     end
  57.     for id, _ in pairs( files ) do
  58.         send( sMessage, id )
  59.     end
  60. end
  61.  
  62. function receive( pFilter, nTimeout, _side, _sender )
  63.     local id
  64.     if nTimeout and type( nTimeout ) ~= "number" then
  65.         error( "Expected string, number[, string], got " .. type( pFilter ) .. ", " .. type( nTimeout ) .. "," .. type( _side ), 2 )
  66.     elseif pFilter and type( pFilter ) ~= "string" then
  67.         error( "Expected string, number[, string], got " .. type( pFilter ) .. ", " .. type( nTimeout ) .. "," .. type( _side ), 2 )
  68.     elseif _side and type( _side ) ~= "string" then
  69.         error( "Expected string, number[, string], got " .. type( pFilter ) .. ", " .. type( nTimeout ) .. "," .. type( _side ), 2 )
  70.     end
  71.     if nTimeout then
  72.         id = os.startTimer( nTimeout )
  73.     end
  74.     while true do
  75.         local event = { os.pullEvent() }
  76.         if event[ 1 ] == "disknet_message" then
  77.             if event[ 2 ] == _side or not _side then
  78.                 if event[ 3 ] == pFilter or not pFilter then
  79.                     return unpack( event )
  80.                 end
  81.             end
  82.         elseif event[ 1 ] == "timer" and event[ 2 ] == id then
  83.             return nil
  84.         end
  85.     end
  86. end
  87.  
  88. function isOpen( side, file ) --attempts to find an open "port"
  89.     for k, v in pairs( files ) do
  90.         if v.side == side and v.file == file then
  91.             return k
  92.         end
  93.     end
  94.     return false
  95. end
  96.  
  97. local function checkFile()  --checks the files for new data once per tick
  98.     while true do
  99.         for id, tbl in pairs( files ) do
  100.             disk.setLabel( tbl.side, tbl.label )
  101.             local filePath = fs.combine( disk.getMountPath( tbl.side ), tbl.file )
  102.             if fs.getFreeSpace( disk.getMountPath( tbl.side ) ) < 100 then
  103.                 local file = fs.open( filePath, "w" )
  104.                 file.write( textutils.serialize( {} ) )
  105.                 file.close()
  106.             end
  107.             if fs.exists( filePath ) then
  108.                 local file = fs.open( filePath, "r" )
  109.                 local data = textutils.unserialize( file.readAll() )
  110.                 file.close()
  111.                 local toSend = {}
  112.                 if not oldFiles[ id ] then
  113.                     oldFiles[ id ] = {}
  114.                 end
  115.                 for k, v in ipairs( data ) do
  116.                     if oldFiles[ id ][ k ] ~= v then
  117.                         toSend[ #toSend + 1 ] = v
  118.                     end
  119.                 end
  120.                 for k, v in pairs( toSend ) do
  121.                     os.queueEvent( "disknet_message", tbl.side, tbl.file, v.sender, v.message )
  122.                     os.pullEvent()
  123.                     oldFiles[ id ] = data
  124.                 end
  125.             end
  126.         end
  127.         sleep( 0.1 )
  128.     end
  129. end
  130.  
  131. function redRepeat( side, file ) --sends/recieves disknet/rednet messages back and forth.
  132.     while true do
  133.         local event = { os.pullEvent() }
  134.         if event[ 1 ] == "disknet_message" and event[ 2 ] == side and event[ 3 ] == file then
  135.             rednet.send( event[ 3 ], file )
  136.         elseif event[ 1 ] == "rednet_message" then
  137.             disknet.broadcast( event[ 3 ] )
  138.         end
  139.     end
  140. end
  141.  
  142. local cocheck = coroutine.create( checkFile )
  143.  
  144. function os.pullEventRaw( sFilter )
  145.     while true do
  146.         local event = { coroutine.yield() }
  147.         if coroutine.status( cocheck ) == "suspended" then
  148.             coroutine.resume( cocheck, unpack( event ) )
  149.         end
  150.         if event[ 1 ] == sFilter or not sFilter then
  151.             return unpack( event )
  152.         end
  153.     end
  154. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement