Olacken

wirelessPeripheralComputer

Jun 1st, 2016
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.25 KB | None | 0 0
  1. --local override = true
  2. local PROTOCOL = "wlp"
  3. local native={}
  4. for n,v in ipairs({"top","bottom","front","back","left","right"}) do
  5.     if peripheral.getType(v) == "modem" and peripheral.call(v,"isWireless") then
  6.         rednet.open(v)
  7.         break
  8.     elseif v=="right" then
  9.         error("No wireless modem found",2)
  10.     end
  11. end
  12.  
  13.  
  14. local modems={}
  15. local function findHost(hostModem)
  16.  
  17.     if not modems[hostModem] then
  18.         modems[hostModem] = rednet.lookup(PROTOCOL,hostModem)
  19.         if not modems[hostModem] then
  20.             error("No specifeid modem")
  21.         end
  22.     end
  23. end
  24.  
  25. local function getPath(side)
  26.     local sep = string.find(side,"/")
  27.     if sep then
  28.         return string.sub(side,sep+1),string.sub(side,1,sep-1)
  29.     else
  30.         return side
  31.     end
  32. end
  33.  
  34. local periphoveride= {
  35. getNames = function(hostModem) 
  36.     if hostModem == nil then
  37.         return native.getNames()
  38.     else
  39.         findHost(hostModem)
  40.         rednet.send(modems[hostModem],{func="getNames",modem=hostModem},PROTOCOL)
  41.         local id, msg = rednet.receive(PROTOCOL,2)
  42.         return msg.funcr
  43.     end
  44. end,
  45.  
  46. isPresent = function(sSide)
  47.     if type( sSide ) ~= "string" then
  48.         error( "Expected string", 2 )
  49.     end
  50.     local sSide,hostModem = getPath(sSide)
  51.     if hostModem == nil then
  52.         return native.isPresent(sSide)
  53.     else
  54.         findHost(hostModem)
  55.         rednet.send(modems[hostModem],{func="isPresent",modem=hostModem,periph=sSide},PROTOCOL)
  56.         local id, msg = rednet.receive(PROTOCOL,2)
  57.         if msg.funcr then
  58.             return true
  59.         end
  60.         return false
  61.     end
  62. end,
  63.  
  64. getType = function(sSide)
  65.     if type( sSide ) ~= "string" then
  66.         error( "Expected string", 2 )
  67.     end
  68.     local sSide,hostModem = getPath(sSide)
  69.     if hostModem == nil then
  70.         return native.getType(sSide)
  71.     else
  72.         findHost(hostModem)
  73.         rednet.send(modems[hostModem],{func="getType",modem=hostModem,periph=sSide},PROTOCOL)
  74.         local id, msg = rednet.receive(PROTOCOL,2)
  75.         return msg.funcr
  76.     end
  77. end,
  78.  
  79. getMethods = function(sSide)
  80.     if type( sSide ) ~= "string" then
  81.         error( "Expected string", 2 )
  82.     end
  83.     local sSide,hostModem = getPath(sSide)
  84.     if hostModem == nil then
  85.         return native.getMethods(sSide)
  86.     else
  87.         findHost(hostModem)
  88.         rednet.send(modems[hostModem],{func="getMethods",modem=hostModem},PROTOCOL)
  89.         local id, msg = rednet.receive(PROTOCOL,2)
  90.         return msg.funcr
  91.     end
  92. end,
  93.  
  94. call = function(sSide,sMethod,...)
  95.     if type( sSide ) ~= "string" or type( sMethod ) ~= "string" then
  96.         error( "Expected string, string", 2 )
  97.     end
  98.     local sSide,hostModem = getPath(sSide)
  99.     if hostModem == nil then
  100.         return native.call(sSide,sMethod,...)
  101.     else
  102.         findHost(hostModem)
  103.         rednet.send(modems[hostModem],{func="call",modem=hostModem,periph=sSide,method=sMethod,args={...}},PROTOCOL)
  104.         local id, msg = rednet.receive(PROTOCOL,2)
  105.         if msg.err then
  106.             error(msg.funcr)
  107.         else
  108.             if msg.funcr then
  109.                 return unpack(msg.funcr)
  110.             end
  111.         end
  112.     end
  113. end,
  114.  
  115. wrap = function(_sSide)
  116.     if type( _sSide ) ~= "string" then
  117.         error( "Expected string", 2 )
  118.     end
  119.     local sSide,hostModem = getPath(_sSide)
  120.     if hostModem == nil then
  121.         return native.wrap(sSide)
  122.     else
  123.         findHost(hostModem)
  124.         rednet.send(modems[hostModem],{func="wrap",modem=hostModem,periph=sSide},PROTOCOL)
  125.         local id, msg = rednet.receive(PROTOCOL,2)
  126.         if msg.funcr then
  127.             local methods = {}
  128.             for n,sMethods in pairs(msg.funcr) do
  129.                 methods[sMethods] = function(...)
  130.                     return call(_sSide,sMethods,...)
  131.                 end
  132.             end
  133.             return methods
  134.         end
  135.     end
  136. end,
  137.  
  138. find = function(sType,fnFilter)
  139.     if type( sType ) ~= "string" or (fnFilter ~= nil and type( fnFilter ) ~= "function") then
  140.         error( "Expected string, [function]", 2 )
  141.     end
  142.     local sSide,hostModem = getPath(sSide)
  143.     if hostModem == nil then
  144.         return native.find(sType,fnFilter)
  145.     else
  146.         findHost(hostModem)
  147.         rednet.send(modems[hostModem],{func="find",modem=hostModem,ptype=sType},PROTOCOL)
  148.         local id,msg = rednet.receive(PROTOCOL,2)
  149.         if msg.funcr then
  150.             local tResult = {}
  151.             for n,sPeriph in ipairs(msg.funcr) do
  152.                 local p = wrap(hostModem,sPeriph)
  153.                 if fnFilter == nil or fnFilter( sName, p ) then
  154.                     table.insert( tResult, p )
  155.                 end
  156.             end
  157.             return unpack(tResult)
  158.         end
  159.     end
  160. end
  161. }
  162.  
  163. for k,v in pairs(periphoveride) do
  164.     native[k] = peripheral[k]
  165.     if override then
  166.         peripheral[k] = v
  167.     else
  168.         _ENV[k]=v
  169.     end
  170. end
Add Comment
Please, Sign In to add comment