Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --local override = true
- local PROTOCOL = "wlp"
- local native={}
- for n,v in ipairs({"top","bottom","front","back","left","right"}) do
- if peripheral.getType(v) == "modem" and peripheral.call(v,"isWireless") then
- rednet.open(v)
- break
- elseif v=="right" then
- error("No wireless modem found",2)
- end
- end
- local modems={}
- local function findHost(hostModem)
- if not modems[hostModem] then
- modems[hostModem] = rednet.lookup(PROTOCOL,hostModem)
- if not modems[hostModem] then
- error("No specifeid modem")
- end
- end
- end
- local function getPath(side)
- local sep = string.find(side,"/")
- if sep then
- return string.sub(side,sep+1),string.sub(side,1,sep-1)
- else
- return side
- end
- end
- local periphoveride= {
- getNames = function(hostModem)
- if hostModem == nil then
- return native.getNames()
- else
- findHost(hostModem)
- rednet.send(modems[hostModem],{func="getNames",modem=hostModem},PROTOCOL)
- local id, msg = rednet.receive(PROTOCOL,2)
- return msg.funcr
- end
- end,
- isPresent = function(sSide)
- if type( sSide ) ~= "string" then
- error( "Expected string", 2 )
- end
- local sSide,hostModem = getPath(sSide)
- if hostModem == nil then
- return native.isPresent(sSide)
- else
- findHost(hostModem)
- rednet.send(modems[hostModem],{func="isPresent",modem=hostModem,periph=sSide},PROTOCOL)
- local id, msg = rednet.receive(PROTOCOL,2)
- if msg.funcr then
- return true
- end
- return false
- end
- end,
- getType = function(sSide)
- if type( sSide ) ~= "string" then
- error( "Expected string", 2 )
- end
- local sSide,hostModem = getPath(sSide)
- if hostModem == nil then
- return native.getType(sSide)
- else
- findHost(hostModem)
- rednet.send(modems[hostModem],{func="getType",modem=hostModem,periph=sSide},PROTOCOL)
- local id, msg = rednet.receive(PROTOCOL,2)
- return msg.funcr
- end
- end,
- getMethods = function(sSide)
- if type( sSide ) ~= "string" then
- error( "Expected string", 2 )
- end
- local sSide,hostModem = getPath(sSide)
- if hostModem == nil then
- return native.getMethods(sSide)
- else
- findHost(hostModem)
- rednet.send(modems[hostModem],{func="getMethods",modem=hostModem},PROTOCOL)
- local id, msg = rednet.receive(PROTOCOL,2)
- return msg.funcr
- end
- end,
- call = function(sSide,sMethod,...)
- if type( sSide ) ~= "string" or type( sMethod ) ~= "string" then
- error( "Expected string, string", 2 )
- end
- local sSide,hostModem = getPath(sSide)
- if hostModem == nil then
- return native.call(sSide,sMethod,...)
- else
- findHost(hostModem)
- rednet.send(modems[hostModem],{func="call",modem=hostModem,periph=sSide,method=sMethod,args={...}},PROTOCOL)
- local id, msg = rednet.receive(PROTOCOL,2)
- if msg.err then
- error(msg.funcr)
- else
- if msg.funcr then
- return unpack(msg.funcr)
- end
- end
- end
- end,
- wrap = function(_sSide)
- if type( _sSide ) ~= "string" then
- error( "Expected string", 2 )
- end
- local sSide,hostModem = getPath(_sSide)
- if hostModem == nil then
- return native.wrap(sSide)
- else
- findHost(hostModem)
- rednet.send(modems[hostModem],{func="wrap",modem=hostModem,periph=sSide},PROTOCOL)
- local id, msg = rednet.receive(PROTOCOL,2)
- if msg.funcr then
- local methods = {}
- for n,sMethods in pairs(msg.funcr) do
- methods[sMethods] = function(...)
- return call(_sSide,sMethods,...)
- end
- end
- return methods
- end
- end
- end,
- find = function(sType,fnFilter)
- if type( sType ) ~= "string" or (fnFilter ~= nil and type( fnFilter ) ~= "function") then
- error( "Expected string, [function]", 2 )
- end
- local sSide,hostModem = getPath(sSide)
- if hostModem == nil then
- return native.find(sType,fnFilter)
- else
- findHost(hostModem)
- rednet.send(modems[hostModem],{func="find",modem=hostModem,ptype=sType},PROTOCOL)
- local id,msg = rednet.receive(PROTOCOL,2)
- if msg.funcr then
- local tResult = {}
- for n,sPeriph in ipairs(msg.funcr) do
- local p = wrap(hostModem,sPeriph)
- if fnFilter == nil or fnFilter( sName, p ) then
- table.insert( tResult, p )
- end
- end
- return unpack(tResult)
- end
- end
- end
- }
- for k,v in pairs(periphoveride) do
- native[k] = peripheral[k]
- if override then
- peripheral[k] = v
- else
- _ENV[k]=v
- end
- end
Add Comment
Please, Sign In to add comment