Advertisement
Oeed

CommServer

Dec 16th, 2014
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.53 KB | None | 0 0
  1. --  Hideously Smashed Together by Compilr, a Hideous Smash-Stuff-Togetherer, (c) 2014 oeed  --
  2.  
  3. --  This file REALLLLLLLY isn't suitable to be used for anything other than being executed --
  4.  
  5. --  To extract all the files, run: "<filename> --extract" in the Shell --
  6. local files = {
  7.   r = "os.reboot()",
  8.   startup = "local bedrockPath='/' if OneOS then OneOS.LoadAPI('/System/API/Bedrock.lua', false)elseif fs.exists(bedrockPath..'/Bedrock')then os.loadAPI(bedrockPath..'/Bedrock')else if http then print('Downloading Bedrock...')local h=http.get('http://pastebin.com/raw.php?i=0MgKNqpN')if h then local f=fs.open(bedrockPath..'/Bedrock','w')f.write(h.readAll())f.close()h.close()os.loadAPI(bedrockPath..'/Bedrock')else error('Failed to download Bedrock. Is your internet working?') end else error('This program needs to download Bedrock to work. Please enable HTTP.') end end if Bedrock then Bedrock.BasePath = bedrockPath Bedrock.ProgramPath = shell.getRunningProgram() end\
  9. \
  10. local program = Bedrock:Initialise()\
  11. \
  12. local communicate = Communicate:Initialise(program)\
  13. \
  14. local info = {\
  15.     Name = 'A Server',\
  16.     Actions = {\
  17.         {\
  18.             Name = '',\
  19.             MessageType = '',\
  20.             Function = function(message)\
  21.             end\
  22.         },\
  23.         {\
  24.             Name = 'Reboot System',\
  25.             MessageType = 'reboot',\
  26.             Function = function(message)\
  27.                 os.reboot()\
  28.             end\
  29.         }\
  30.     }\
  31. }\
  32. \
  33. communicate:RegisterMessageType('discover', function(message)\
  34.     if message.content == 'Ping!' then\
  35.         local _info = {}\
  36.         _info.Name = info.Name\
  37.         _info.Actions = {}\
  38.         for i, v in ipairs(info.Actions) do\
  39.             table.insert(_info.Actions, {Name = v.Name, MessageType = v.MessageType})\
  40.         end\
  41.         return _info\
  42.     end\
  43. end)\
  44. \
  45. for i, v in ipairs(info.Actions) do\
  46.     communicate:RegisterMessageType(v.MessageType, v.Function)\
  47. end\
  48. \
  49. communicate:RegisterMessageType('ping', function(message)\
  50.     if message.content == 'Ping!' then\
  51.         return 'Pong!'\
  52.     end\
  53. end)\
  54. \
  55. program:Run(function()\
  56. end)",
  57.   Views = {
  58.     [ "main.view" ] = "{\
  59.     Children = {}\
  60.     -- BackgroundColour = 'white',\
  61.     -- Children = {\
  62.     --  {\
  63.     --      Y = 3,\
  64.     --      Width = \"100%\",\
  65.     --      Align = \"Center\",\
  66.     --      TextColour = 'blue',\
  67.     --      Type = 'Label',\
  68.     --  }\
  69.     -- }\
  70. }",
  71.   },
  72.   APIs = {
  73.     [ "Communicate.lua" ] = "\
  74. Channel = 42000\
  75. Callbacks = {}\
  76. CallbackTimeouts = {}\
  77. MessageTimeout = 0.5\
  78. MessageTypeHandlers = {}\
  79. \
  80. local getNames = peripheral.getNames or function()\
  81.     local tResults = {}\
  82.     for n,sSide in ipairs( rs.getSides() ) do\
  83.         if peripheral.isPresent( sSide ) then\
  84.             table.insert( tResults, sSide )\
  85.             local isWireless = false\
  86.             if pcall(function()isWireless = peripheral.call(sSide, 'isWireless') end) then\
  87.                 isWireless = true\
  88.             end     \
  89.             if peripheral.getType( sSide ) == \"modem\" and not isWireless then\
  90.                 local tRemote = peripheral.call( sSide, \"getNamesRemote\" )\
  91.                 for n,sName in ipairs( tRemote ) do\
  92.                     table.insert( tResults, sName )\
  93.                 end\
  94.             end\
  95.         end\
  96.     end\
  97.     return tResults\
  98. end\
  99. \
  100. GetModems = function(self, callback)\
  101.     local ok = false\
  102.     for i, name in ipairs(getNames()) do\
  103.         ok = true\
  104.         if callback and peripheral.getType( name ) == \"modem\" then\
  105.             local p = peripheral.wrap(name)\
  106.             callback(p, name)\
  107.         end\
  108.     end\
  109.     return ok\
  110. end\
  111. \
  112. Initialise = function(self, bedrock)\
  113.     local new = {}\
  114.     setmetatable(new, {__index = self})\
  115.     new.Bedrock = bedrock\
  116.     new.Bedrock:RegisterEvent('modem_message', function(_, event, name, channel, replyChannel, message, distance)\
  117.         new:OnMessage(event, name, channel, replyChannel, message, distance)\
  118.     end)\
  119.     if new:GetModems(function(modem, name)\
  120.             modem.open(new.Channel)\
  121.         end)\
  122.     then\
  123.         return new\
  124.     end\
  125.     return false\
  126. end\
  127. \
  128. RegisterMessageType = function(self, msgType, callback)\
  129.     self.MessageTypeHandlers[msgType] = callback\
  130. end\
  131. \
  132. OnMessage = function(self, event, name, channel, replyChannel, message, distance)\
  133.     if channel == self.Channel and type(message) == 'table' and message.msgType and message.thread and message.id ~= os.getComputerID() then\
  134.         if self.Callbacks[message.thread] then\
  135.             local response, callback = self.Callbacks[message.thread](message.content, message, distance)\
  136.             if response ~= nil then\
  137.                 self:Reply(response, message, callback)\
  138.             end\
  139.         elseif self.MessageTypeHandlers[message.msgType] then\
  140.             local response = self.MessageTypeHandlers[message.msgType](message, message.msgType, message.content, distance)\
  141.             if response ~= nil then\
  142.                 self:Reply(response, message)\
  143.             end\
  144.         else\
  145.         end\
  146.         return true\
  147.     else\
  148.         return false\
  149.     end\
  150. end\
  151. \
  152. Reply = function(self, content, message, callback)\
  153.     self:SendMessage(message.msgType, content, callback, message.thread)\
  154. end\
  155. \
  156. SendMessage = function(self, msgType, content, callback, thread, multiple)\
  157.     thread = thread or tostring(math.random())\
  158.     if self:GetModems(function(modem, name)\
  159.             modem.transmit(self.Channel, self.Channel, {msgType = msgType, content = content, thread = thread, id = os.getComputerID()})\
  160.         end)\
  161.     then\
  162.         if callback then\
  163.             self.Callbacks[thread] = function(...)\
  164.                 if not multiple then\
  165.                     self.Callbacks[thread] = nil\
  166.                     self.CallbackTimeouts[thread] = nil\
  167.                 end\
  168.                 return callback(...), callback\
  169.             end\
  170. \
  171.             self.CallbackTimeouts[thread] = self.Bedrock:StartTimer(function(_, timer)\
  172.                 if timer == self.CallbackTimeouts[thread] then\
  173.                     callback(false)\
  174.                 end\
  175.             end, self.MessageTimeout)\
  176.         end\
  177.         return true\
  178.     end\
  179.     return false\
  180. end",
  181.   },
  182. }
  183.  
  184. local function run(tArgs)
  185.  
  186.   local fnFile, err = loadstring(files['startup'], 'startup')
  187.   if err then
  188.     error(err)
  189.   end
  190.  
  191.   local function split(str, pat)
  192.      local t = {}
  193.      local fpat = "(.-)" .. pat
  194.      local last_end = 1
  195.      local s, e, cap = str:find(fpat, 1)
  196.      while s do
  197.         if s ~= 1 or cap ~= "" then
  198.      table.insert(t,cap)
  199.         end
  200.         last_end = e+1
  201.         s, e, cap = str:find(fpat, last_end)
  202.      end
  203.      if last_end <= #str then
  204.         cap = str:sub(last_end)
  205.         table.insert(t, cap)
  206.      end
  207.      return t
  208.   end
  209.  
  210.   local function resolveTreeForPath(path, single)
  211.     local _files = files
  212.     local parts = split(path, '/')
  213.     if parts then
  214.       for i, v in ipairs(parts) do
  215.         if #v > 0 then
  216.           if _files[v] then
  217.             _files = _files[v]
  218.           else
  219.             _files = nil
  220.             break
  221.           end
  222.         end
  223.       end
  224.     elseif #path > 0 and path ~= '/' then
  225.       _files = _files[path]
  226.     end
  227.     if not single or type(_files) == 'string' then
  228.       return _files
  229.     end
  230.   end
  231.  
  232.   local oldFs = fs
  233.   local env
  234.   env = {
  235.     fs = {
  236.       list = function(path)
  237.               local list = {}
  238.               if fs.exists(path) then
  239.             list = fs.list(path)
  240.               end
  241.         for k, v in pairs(resolveTreeForPath(path)) do
  242.           if not fs.exists(path .. '/' ..k) then
  243.             table.insert(list, k)
  244.           end
  245.         end
  246.         return list
  247.       end,
  248.  
  249.       exists = function(path)
  250.         if fs.exists(path) then
  251.           return true
  252.         elseif resolveTreeForPath(path) then
  253.           return true
  254.         else
  255.           return false
  256.         end
  257.       end,
  258.  
  259.       isDir = function(path)
  260.         if fs.isDir(path) then
  261.           return true
  262.         else
  263.           local tree = resolveTreeForPath(path)
  264.           if tree and type(tree) == 'table' then
  265.             return true
  266.           else
  267.             return false
  268.           end
  269.         end
  270.       end,
  271.  
  272.       isReadOnly = function(path)
  273.         if not fs.isReadOnly(path) then
  274.           return false
  275.         else
  276.           return true
  277.         end
  278.       end,
  279.  
  280.       getName = fs.getName,
  281.  
  282.       getSize = fs.getSize,
  283.  
  284.       getFreespace = fs.getFreespace,
  285.  
  286.       makeDir = fs.makeDir,
  287.  
  288.       move = fs.move,
  289.  
  290.       copy = fs.copy,
  291.  
  292.       delete = fs.delete,
  293.  
  294.       combine = fs.combine,
  295.  
  296.       open = function(path, mode)
  297.         if fs.exists(path) then
  298.           return fs.open(path, mode)
  299.         elseif type(resolveTreeForPath(path)) == 'string' then
  300.           local handle = {close = function()end}
  301.           if mode == 'r' then
  302.             local content = resolveTreeForPath(path)
  303.             handle.readAll = function()
  304.               return content
  305.             end
  306.  
  307.             local line = 1
  308.             local lines = split(content, '\n')
  309.             handle.readLine = function()
  310.               if line > #lines then
  311.                 return nil
  312.               else
  313.                 return lines[line]
  314.               end
  315.               line = line + 1
  316.             end
  317.                       return handle
  318.           else
  319.             error('Cannot write to read-only file (compilr archived).')
  320.           end
  321.         else
  322.           return fs.open(path, mode)
  323.         end
  324.       end
  325.     },
  326.  
  327.     io = {
  328.       input = io.input,
  329.       output = io.output,
  330.       type = io.type,
  331.       close = io.close,
  332.       write = io.write,
  333.       flush = io.flush,
  334.       lines = io.lines,
  335.       read = io.read,
  336.       open = function(path, mode)
  337.         if fs.exists(path) then
  338.           return io.open(path, mode)
  339.         elseif type(resolveTreeForPath(path)) == 'string' then
  340.           local content = resolveTreeForPath(path)
  341.           local f = fs.open(path, 'w')
  342.           f.write(content)
  343.           f.close()
  344.           if mode == 'r' then
  345.             return io.open(path, mode)
  346.           else
  347.             error('Cannot write to read-only file (compilr archived).')
  348.           end
  349.         else
  350.           return io.open(path, mode)
  351.         end
  352.       end
  353.     },
  354.  
  355.     loadfile = function( _sFile )
  356.         local file = env.fs.open( _sFile, "r" )
  357.         if file then
  358.             local func, err = loadstring( file.readAll(), fs.getName( _sFile ) )
  359.             file.close()
  360.             return func, err
  361.         end
  362.         return nil, "File not found: ".._sFile
  363.     end,
  364.  
  365.     dofile = function( _sFile )
  366.         local fnFile, e = env.loadfile( _sFile )
  367.         if fnFile then
  368.             setfenv( fnFile, getfenv(2) )
  369.             return fnFile()
  370.         else
  371.             error( e, 2 )
  372.         end
  373.     end
  374.   }
  375.  
  376.   setmetatable( env, { __index = _G } )
  377.  
  378.   local tAPIsLoading = {}
  379.   env.os.loadAPI = function( _sPath )
  380.       local sName = fs.getName( _sPath )
  381.       if tAPIsLoading[sName] == true then
  382.           printError( "API "..sName.." is already being loaded" )
  383.           return false
  384.       end
  385.       tAPIsLoading[sName] = true
  386.          
  387.       local tEnv = {}
  388.       setmetatable( tEnv, { __index = env } )
  389.       local fnAPI, err = env.loadfile( _sPath )
  390.       if fnAPI then
  391.           setfenv( fnAPI, tEnv )
  392.           fnAPI()
  393.       else
  394.           printError( err )
  395.           tAPIsLoading[sName] = nil
  396.           return false
  397.       end
  398.      
  399.       local tAPI = {}
  400.       for k,v in pairs( tEnv ) do
  401.           tAPI[k] =  v
  402.       end
  403.      
  404.       env[sName] = tAPI    
  405.       tAPIsLoading[sName] = nil
  406.       return true
  407.   end
  408.  
  409.   env.shell = shell
  410.  
  411.   setfenv( fnFile, env )
  412.   fnFile(unpack(tArgs))
  413. end
  414.  
  415. local function extract()
  416.     local function node(path, tree)
  417.         if type(tree) == 'table' then
  418.             fs.makeDir(path)
  419.             for k, v in pairs(tree) do
  420.                 node(path .. '/' .. k, v)
  421.             end
  422.         else
  423.             local f = fs.open(path, 'w')
  424.             if f then
  425.                 f.write(tree)
  426.                 f.close()
  427.             end
  428.         end
  429.     end
  430.     node('', files)
  431. end
  432.  
  433. local tArgs = {...}
  434. if #tArgs == 1 and tArgs[1] == '--extract' then
  435.   extract()
  436. else
  437.   run(tArgs)
  438. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement