Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.30 KB | None | 0 0
  1. --config
  2. local netComponentName = "tunnel"
  3. local address,port = "",0
  4.  
  5. --
  6.  
  7. --vcomponent
  8. local function vcomponentFactory()
  9.  
  10.     local proxylist = {}
  11.     local typelist = {}
  12.     local doclist = {}
  13.  
  14.     local oproxy = component.proxy
  15.     function component.proxy(address)
  16.         checkArg(1,address,"string")
  17.         if proxylist[address] ~= nil then
  18.             return proxylist[address]
  19.         end
  20.         return oproxy(address)
  21.     end
  22.  
  23.     local olist = component.list
  24.     function component.list(filter, exact)
  25.         checkArg(1,filter,"string","nil")
  26.         local result = {}
  27.         local data = {}
  28.         for k,v in olist(filter, exact) do
  29.             data[#data + 1] = k
  30.             data[#data + 1] = v
  31.             result[k]=v
  32.         end
  33.         for k,v in pairs(typelist) do
  34.             if filter == nil or (exact and v == filter) or (not exact and v:find(filter, nil, true)) then
  35.                 data[#data + 1] = k
  36.                 data[#data + 1] = v
  37.                 result[k]=v
  38.             end
  39.         end
  40.         local place = 1
  41.         return setmetatable(result, {__call=function()
  42.             local addr,type = data[place], data[place + 1]
  43.             place = place + 2
  44.             return addr,type
  45.         end})
  46.     end
  47.  
  48.     local otype = component.type
  49.     function component.type(address)
  50.         checkArg(1,address,"string")
  51.         if typelist[address] ~= nil then
  52.             return typelist[address]
  53.         end
  54.         return otype(address)
  55.     end
  56.  
  57.     local odoc = component.doc
  58.     function component.doc(address, method)
  59.         checkArg(1,address,"string")
  60.         checkArg(2,method,"string")
  61.         if proxylist[address] ~= nil then
  62.             if proxylist[address][method] == nil then
  63.                 error("no such method",2)
  64.             end
  65.             if doclist[address] ~= nil then
  66.                 return doclist[address][method]
  67.             end
  68.             return nil
  69.         end
  70.         return odoc(address, method)
  71.     end
  72.  
  73.     local oslot = component.slot
  74.     function component.slot(address)
  75.         checkArg(1,address,"string")
  76.         if proxylist[address] ~= nil then
  77.             return -1 -- vcomponents do not exist in a slot
  78.         end
  79.         return oslot(address)
  80.     end
  81.  
  82.     local omethods = component.methods
  83.     function component.methods(address)
  84.         checkArg(1,address,"string")
  85.         if proxylist[address] ~= nil then
  86.             local methods = {}
  87.             for k,v in pairs(proxylist[address]) do
  88.                 if type(v) == "function" then
  89.                     methods[k] = true -- All vcomponent methods are direct
  90.                 end
  91.             end
  92.             return methods
  93.         end
  94.         return omethods(address)
  95.     end
  96.  
  97.     local oinvoke = component.invoke
  98.     function component.invoke(address, method, ...)
  99.         checkArg(1,address,"string")
  100.         checkArg(2,method,"string")
  101.         if proxylist[address] ~= nil then
  102.             if proxylist[address][method] == nil then
  103.                 error("no such method",2)
  104.             end
  105.             return proxylist[address][method](...)
  106.         end
  107.         return oinvoke(address, method, ...)
  108.     end
  109.  
  110.     local ofields = component.fields
  111.     function component.fields(address)
  112.         checkArg(1,address,"string")
  113.         if proxylist[address] ~= nil then
  114.             return {} -- What even is this?
  115.         end
  116.         return ofields(address)
  117.     end
  118.  
  119.     local vcomponent = {}
  120.  
  121.     function vcomponent.register(address, ctype, proxy, doc)
  122.         checkArg(1,address,"string")
  123.         checkArg(2,ctype,"string")
  124.         checkArg(3,proxy,"table")
  125.        
  126.         if proxylist[address] ~= nil then
  127.             return nil, "component already at address"
  128.         elseif component.type(address) ~= nil then
  129.             return nil, "cannot register over real component"
  130.         end
  131.        
  132.         local p = setmetatable({address = address,type = ctype},proxy)
  133.        
  134.         proxylist[address] = p
  135.         typelist[address] = ctype
  136.         doclist[address] = doc
  137.         computer.pushSignal("component_added",address,ctype)
  138.         return true
  139.     end
  140.  
  141.     function vcomponent.unregister(address)
  142.         checkArg(1,address,"string")
  143.         if proxylist[address] == nil then
  144.             if component.type(address) ~= nil then
  145.                 return nil, "cannot unregister real component"
  146.             else
  147.                 return nil, "no component at address"
  148.             end
  149.         end
  150.         local thetype = typelist[address]
  151.         proxylist[address] = nil
  152.         typelist[address] = nil
  153.         doclist[address] = nil
  154.         computer.pushSignal("component_removed",address,thetype)
  155.         return true
  156.     end
  157.  
  158.     function vcomponent.list()
  159.         local list = {}
  160.         for k,v in pairs(proxylist) do
  161.             list[#list + 1] = {k,typelist[k],v}
  162.         end
  163.         return list
  164.     end
  165.  
  166.     function vcomponent.resolve(address, componentType)
  167.         checkArg(1, address, "string")
  168.         checkArg(2, componentType, "string", "nil")
  169.         for k,v in pairs(typelist) do
  170.             if componentType == nil or v == componentType then
  171.                 if k:sub(1, #address) == address then
  172.                     return k
  173.                 end
  174.             end
  175.         end
  176.         return nil, "no such component"
  177.     end
  178.  
  179.     local r = math.random
  180.     function vcomponent.uuid()
  181.         return string.format("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
  182.         r(0,255),r(0,255),r(0,255),r(0,255),
  183.         r(0,255),r(0,255),
  184.         r(64,79),r(0,255),
  185.         r(128,191),r(0,255),
  186.         r(0,255),r(0,255),r(0,255),r(0,255),r(0,255),r(0,255))
  187.     end
  188.  
  189.     return vcomponent
  190. end
  191. --
  192.  
  193. --serialization
  194.  
  195. -- delay loaded tables fail to deserialize cross [C] boundaries (such as when having to read files that cause yields)
  196. local local_pairs = function(tbl)
  197.   local mt = getmetatable(tbl)
  198.   return (mt and mt.__pairs or pairs)(tbl)
  199. end
  200.  
  201. --[[local function serialize(value, pretty)
  202.       local kw =  {["and"]=true, ["break"]=true, ["do"]=true, ["else"]=true,
  203.                    ["elseif"]=true, ["end"]=true, ["false"]=true, ["for"]=true,
  204.                    ["function"]=true, ["goto"]=true, ["if"]=true, ["in"]=true,
  205.                    ["local"]=true, ["nil"]=true, ["not"]=true, ["or"]=true,
  206.                    ["repeat"]=true, ["return"]=true, ["then"]=true, ["true"]=true,
  207.                    ["until"]=true, ["while"]=true}
  208.       local id = "^[%a_][%w_]*$"
  209.       local ts = {}
  210.       local result_pack = {}
  211.       local function recurse(current_value, depth)
  212.         local t = type(current_value)
  213.         if t == "number" then
  214.           if current_value ~= current_value then
  215.             table.insert(result_pack, "0/0")
  216.           elseif current_value == math.huge then
  217.             table.insert(result_pack, "math.huge")
  218.           elseif current_value == -math.huge then
  219.             table.insert(result_pack, "-math.huge")
  220.           else
  221.             table.insert(result_pack, tostring(current_value))
  222.           end
  223.         elseif t == "string" then
  224.           table.insert(result_pack, (string.format("%q", current_value):gsub("\\\n","\\n")))
  225.         elseif
  226.           t == "nil" or
  227.           t == "boolean" or
  228.           pretty and (t ~= "table" or (getmetatable(current_value) or {}).__tostring) then
  229.           table.insert(result_pack, tostring(current_value))
  230.         elseif t == "table" then
  231.           if ts[current_value] then
  232.             if pretty then
  233.               table.insert(result_pack, "recursion")
  234.               return
  235.             else
  236.               error("tables with cycles are not supported")
  237.             end
  238.           end
  239.           ts[current_value] = true
  240.           local f
  241.           if pretty then
  242.             local ks, sks, oks = {}, {}, {}
  243.             for k in local_pairs(current_value) do
  244.               if type(k) == "number" then
  245.                 table.insert(ks, k)
  246.               elseif type(k) == "string" then
  247.                 table.insert(sks, k)
  248.               else
  249.                 table.insert(oks, k)
  250.               end
  251.             end
  252.             table.sort(ks)
  253.             table.sort(sks)
  254.             for _, k in ipairs(sks) do
  255.               table.insert(ks, k)
  256.             end
  257.             for _, k in ipairs(oks) do
  258.               table.insert(ks, k)
  259.             end
  260.             local n = 0
  261.             f = table.pack(function()
  262.               n = n + 1
  263.               local k = ks[n]
  264.               if k ~= nil then
  265.                 return k, current_value[k]
  266.               else
  267.                 return nil
  268.               end
  269.             end)
  270.           else
  271.             f = table.pack(local_pairs(current_value))
  272.           end
  273.           local i = 1
  274.           local first = true
  275.           table.insert(result_pack, "{")
  276.           for k, v in table.unpack(f) do
  277.             if not first then
  278.               table.insert(result_pack, ",")
  279.               if pretty then
  280.                 table.insert(result_pack, "\n" .. string.rep(" ", depth))
  281.               end
  282.             end
  283.             first = nil
  284.             local tk = type(k)
  285.             if tk == "number" and k == i then
  286.               i = i + 1
  287.               recurse(v, depth + 1)
  288.             else
  289.               if tk == "string" and not kw[k] and string.match(k, id) then
  290.                 table.insert(result_pack, k)
  291.               else
  292.                 table.insert(result_pack, "[")
  293.                 recurse(k, depth + 1)
  294.                 table.insert(result_pack, "]")
  295.               end
  296.               table.insert(result_pack, "=")
  297.               recurse(v, depth + 1)
  298.             end
  299.           end
  300.           ts[current_value] = nil -- allow writing same table more than once
  301.           table.insert(result_pack, "}")
  302.         else
  303.           error("unsupported type: " .. t)
  304.         end
  305.       end
  306.       recurse(value, 1)
  307.       local result = table.concat(result_pack)
  308.       if pretty then
  309.         local limit = type(pretty) == "number" and pretty or 10
  310.         local truncate = 0
  311.         while limit > 0 and truncate do
  312.           truncate = string.find(result, "\n", truncate + 1, true)
  313.           limit = limit - 1
  314.         end
  315.         if truncate then
  316.           return result:sub(1, truncate) .. "..."
  317.         end
  318.       end
  319.       return result
  320.     end
  321.     ]]
  322.    
  323. function unserialize(data)
  324.   checkArg(1, data, "string")
  325.   local result, reason = load("return " .. data, "=data", nil, {math={huge=math.huge}})
  326.   if not result then
  327.     return nil, reason
  328.   end
  329.   local ok, output = pcall(result)
  330.   if not ok then
  331.     return nil, output
  332.   end
  333.   return output
  334. end
  335. --
  336.  
  337.  
  338. local function primaryComponent(name)
  339.     return component.proxy(component.list(name)())
  340. end
  341.  
  342. local gpu = primaryComponent("gpu")
  343.  
  344. local computer=computer
  345.  
  346. --net
  347. local net=primaryComponent(netComponentName)
  348.  
  349. local maxPacketSize=net.maxPacketSize()
  350.  
  351. local function send(...)
  352.     net.send(...)
  353. end
  354. --
  355.  
  356. --event
  357. local function pullFiltered(...)
  358.   local args = table.pack(...)
  359.   local seconds, filter = math.huge
  360.  
  361.   if type(args[1]) == "function" then
  362.     filter = args[1]
  363.   else
  364.     checkArg(1, args[1], "number", "nil")
  365.     checkArg(2, args[2], "function", "nil")
  366.     seconds = args[1]
  367.     filter = args[2]
  368.   end
  369.  
  370.   repeat
  371.     local signal = table.pack(computer.pullSignal(seconds))
  372.     if signal.n > 0 then
  373.       if not (seconds or filter) or filter == nil or filter(table.unpack(signal, 1, signal.n)) then
  374.         return table.unpack(signal, 1, signal.n)
  375.       end
  376.     end
  377.   until signal.n == 0
  378. end
  379.  
  380. local function createPlainFilter(name, ...)
  381.   local filter = table.pack(...)
  382.   if name == nil and filter.n == 0 then
  383.     return nil
  384.   end
  385.  
  386.   return function(...)
  387.     local signal = table.pack(...)
  388.     if name and not (type(signal[1]) == "string" and signal[1]:match(name)) then
  389.       return false
  390.     end
  391.     for i = 1, filter.n do
  392.       if filter[i] ~= nil and filter[i] ~= signal[i + 1] then
  393.         return false
  394.       end
  395.     end
  396.     return true
  397.   end
  398. end
  399.  
  400. local function pull(...)
  401.   local args = table.pack(...)
  402.   if type(args[1]) == "string" then
  403.     return pullFiltered(createPlainFilter(...))
  404.   else
  405.     checkArg(1, args[1], "number", "nil")
  406.     checkArg(2, args[2], "string", "nil")
  407.     return pullFiltered(args[1], createPlainFilter(select(2, ...)))
  408.   end
  409. end
  410. --
  411.  
  412. local function prepareArgs(...)
  413.     local args={...}
  414.    
  415.     for i=0,#args do
  416.         if type(args[i])=="table" and args[i].special then
  417.             args[i]="{special="..args[i].special.."}"  
  418.         end
  419.     end
  420.    
  421.     return table.unpack(args)
  422. end
  423.  
  424. local function test1(...)
  425.     println("test1",...)
  426.     return ...
  427. end
  428.  
  429. local function invokeNet(request, ...)
  430.     send(request,prepareArgs(...))
  431.  
  432.     println("t1")
  433.     local invokeResult = table.pack(test1(pull("modem_message")))
  434.     --request=="invokeResult"
  435.     for i=1,6 do
  436.         table.remove(invokeResult,1)
  437.     end
  438.     println("t2", table.unpack(invokeResult))
  439.    
  440.     for i =1,#invokeResult do
  441.         if type(invokeResult[i])=="string" and string.sub(invokeResult[i],1,1)=="{" and string.sub(invokeResult[i],2,2)~=" " then
  442.             invokeResult[i]=unserialize(invokeResult[i])
  443.         end
  444.     end
  445.    
  446.     return table.unpack(invokeResult)
  447. end
  448.  
  449.  
  450. local vcomponent=vcomponentFactory()
  451.  
  452. local fs_address = invokeNet("component.primary","filesystem")
  453.  
  454. local proxy = {
  455.     __index=function(t,k)
  456.         return function(...)
  457.             return invokeNet("component.invoke", fs_address,k,...)
  458.         end
  459.     end
  460. }
  461.  
  462. vcomponent.register(fs_address,"filesystem",proxy,{})
  463.  
  464. --lua bios
  465.  
  466. local init
  467. do
  468.   local component_invoke = component.invoke
  469.   local function boot_invoke(address, method, ...)
  470.     local result = table.pack(pcall(component_invoke, address, method, ...))
  471.     if not result[1] then
  472.       return nil, result[2]
  473.     else
  474.       return table.unpack(result, 2, result.n)
  475.     end
  476.   end
  477.  
  478.   -- backwards compatibility, may remove later
  479.   local eeprom = component.list("eeprom")()
  480.   computer.getBootAddress = function()
  481.     return boot_invoke(eeprom, "getData")
  482.   end
  483.   computer.setBootAddress = function(address)
  484.     return boot_invoke(eeprom, "setData", address)
  485.   end
  486.   computer.setBootAddress(fs_address)
  487.  
  488.   do
  489.     local screen = component.list("screen")()
  490.     local gpu = component.list("gpu")()
  491.     if gpu and screen then
  492.       boot_invoke(gpu, "bind", screen)
  493.     end
  494.   end
  495.   local function tryLoadFrom(address)
  496.     local handle, reason = boot_invoke(address, "open", "/init.lua")
  497.     if not handle then
  498.       return nil, reason
  499.     end
  500.     local buffer = ""
  501.     repeat
  502.       local data, reason = boot_invoke(address, "read", handle, math.huge)
  503.       if not data and reason then
  504.         return nil, reason
  505.       end
  506.      
  507.       buffer = buffer .. (data or "")
  508.     until not data
  509.     boot_invoke(address, "close", handle)
  510.     return load(buffer, "=init")
  511.   end
  512.   local reason
  513.   if computer.getBootAddress() then
  514.     init, reason = tryLoadFrom(computer.getBootAddress())
  515.   end
  516.   if not init then
  517.     computer.setBootAddress()
  518.     for address in component.list("filesystem") do
  519.       init, reason = tryLoadFrom(address)
  520.       if init then
  521.         computer.setBootAddress(address)
  522.         break
  523.       end
  524.     end
  525.   end
  526.   if not init then
  527.     error("no bootable medium found" .. (reason and (": " .. tostring(reason)) or ""), 0)
  528.   end
  529.   computer.beep(1000, 0.2)
  530. end
  531. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement