Advertisement
Oeed

CommPDA

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