t8rt0t

doxxinstaller.lua

Jul 6th, 2024 (edited)
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.37 KB | None | 0 0
  1. local files = {
  2.     --startup
  3.     ["startup.lua"] = [[shell.openTab("simpledoxxer")]],
  4.    
  5.     ["simpledoxxer.lua"] = [[local json = require("json")
  6. local multi = require("multiTurtle")
  7.  
  8. local pos_dir = "shared/ppos/"
  9.    
  10. local self_pos_path = "shared/"..multi:getID().."/pos.txt"
  11. if not fs.exists(self_pos_path) then
  12.     print("please enter the position of the turtle")
  13.     local pos = read()
  14.     local file = fs.open(self_pos_path,"w+")
  15.     file.write(pos)
  16.     file.close()
  17. end
  18.    
  19. if not fs.isDir(pos_dir) then fs.makeDir(pos_dir) end
  20.  
  21. local pdor = peripheral.find("playerDetector")
  22. local onlinePlayers = json("shared/onlinePlayers.json")
  23.  
  24. local players = {}
  25. for k,v in ipairs(fs.list(pos_dir)) do
  26.     players[v] = json(pos_dir..v..".json")
  27. end
  28.  
  29. local function getPositions()
  30.     local p = pdor.getOnlinePlayers()
  31.     for k,v in ipairs(p) do
  32.         local s,pos = pcall(pdor.getPlayerPos,v)
  33.         if s and pos and pos.x and pos.dimension then
  34.             pos.epoch = os.epoch("utc")
  35.             print(v,pos.x,pos.y,pos.z)
  36.             if not players[v] then players[v] = json(pos_dir..v..".json") end
  37.             players[v][pos.dimension] = pos
  38.         end
  39.     end
  40.     onlinePlayers.players = p
  41. end
  42.  
  43. local function printPlayers(players)
  44.     term.setCursorPos(1,1)
  45.     term.clear()
  46.     for k,v in ipairs(players) do
  47.         if not players[v] then players[v] = json(pos_dir..v..".json") end
  48.         local maxEpoch,pos,dim = 0
  49.         for j,w in pairs(players[v]) do
  50.             if w.epoch > maxEpoch then
  51.                 maxEpoch = w.epoch
  52.                 pos = w
  53.                 dim=j
  54.             end
  55.         end
  56.         if pos and dim then
  57.             print(string.format("%s %s: <%d,%d,%d>",dim:gsub("[^:]+:",""),v,pos.x,pos.y,pos.z))
  58.         end
  59.     end
  60. end
  61.  
  62. if pdor then
  63.     while true do
  64.         getPositions()
  65.         sleep(1)
  66.     end
  67. else
  68.     while true do
  69.         printPlayers(onlinePlayers.players or {})
  70.         sleep(1)
  71.     end
  72. end]],
  73.     --multiTurtle
  74.     ["multiTurtle.lua"] = [[--Multi turtle stuff
  75. local MULTI = {}
  76.  
  77. --Assignment
  78. local assignmentsPath = "metadata/.multi_assignment.json"
  79. local assignmentsDir = "metadata/ids/"
  80. --metadata/.multi_assignment.json layout
  81. -- {
  82. --     "custom_filters":{
  83. --         "type":"functionStr(test1,test2,test3) return true if conditionsMet else return false end"
  84. --     },
  85. --     "new_id":"functionStr() return newID,conditionTable end"
  86. --}
  87. --metadata/ids/<id>.json layout
  88. -- {
  89. --     "type": "any" | "all",
  90. --     "conditions": [
  91. --         {
  92. --             "type": "(conditionType)",
  93. --             "conditions": [
  94. --                 "test1",
  95. --                 true,
  96. --                 "test3"
  97. --             ]
  98. --         }
  99. --     ]
  100. -- }
  101.        
  102. local function between(test,a,b)
  103.     return test >= math.min(a,b) and test <= math.max(a,b)
  104. end
  105. --Define default filters
  106. MULTI.FILTERS = {
  107.     ["all"] = function(...)
  108.         for _,condition in ipairs(...) do
  109.             if not MULTI.VALIDATE(condition) then
  110.                 return false
  111.             end
  112.         end
  113.         return true
  114.     end,
  115.     ["any"] = function(...)
  116.         for _,condition in ipairs(...) do
  117.             if MULTI.VALIDATE(condition) then
  118.                 return true
  119.             end
  120.         end
  121.         return false
  122.     end,
  123.     ["not"] = function(condition)
  124.         return not MULTI.VALIDATE(condition)
  125.     end,
  126.     ["label.exact"] = function(test) return os.getComputerLabel() == test end,
  127.     ["label.contains"] = function(test,useRegex) return os.getComputerLabel() and os.getComputerLabel():find(test,1,useRegex) end,
  128.     ["label.within"] = function(test,useRegex) return os.getComputerLabel() and test:find(os.getComputerLabel(),1,useRegex) end,
  129.     ["computer.pocket"] = function() return pocket end,
  130.     ["computer.turtle"] = function() return turtle end,
  131.     ["computer.advanced"] = function() return term.isColor() end,
  132.     ["computer.command"] = function() return command end,
  133.     ["computer.normal"] = function() return not pocket and not turtle and not command end,
  134.     ["peripheral.type"] = function(pType) return peripheral.find(pType) end,
  135.     ["peripheral.name"] = function(pName) return peripheral.wrap(pName) end,
  136.     ["gps.exact"] = function(x,y,z)
  137.         local s,a,b,c = pcall(gps.locate)
  138.         return s and x == a and y == b and z == c
  139.     end,
  140.     ["gps.range"] = function(x1,y1,z1,x2,y2,z2)
  141.         local s,a,b,c = pcall(gps.locate)
  142.         return s and between(a,x1,x2) and between(b,y1,y2) and between(c,z1,z2)
  143.     end,
  144.     ["files.find"] = function(regex) return #fs.find(regex) > 0 end, --Useful for external disks
  145.     --More dangerous types
  146.     ["turtle.fuel.exact"] = function(value) return turtle and turtle.getFuelLevel() == value end,
  147.     ["turtle.fuel.range"] = function(lowerLimit,upperLimit) return turtle and between(turtle.getFuelLevel(),lowerLimit,upperLimit) end,
  148.     ["turtle.inventory.contains"] = function(name)
  149.         if not turtle then return false end
  150.         for i=1,16 do
  151.             if turtle.getItemCount(i) > 0 and turtle.getItemDetail(i).name == name then
  152.                 return true
  153.             end
  154.         end
  155.         return false
  156.     end,
  157.     ["turtle.inventory.exactSlot"] = function(slot,name,count)
  158.         if turtle and turtle.getItemCount(slot) > 0 then
  159.             return turtle.getItemDetail(slot).name == name and (not count or (turtle.getItemCount(slot) == count))
  160.         end
  161.         return false
  162.     end
  163. }
  164. function MULTI.NEW_ID()
  165.     local id = string.format("%x",os.epoch("utc")%0x1000000)
  166.     local label = string.char(167) .. string.format("%x",math.random(16)-1) .. string.char(167) .. "k" .. id
  167.     os.setComputerLabel(label)
  168.     return label,{["type"] = "label.exact",conditions = {label}}
  169. end
  170.  
  171. function MULTI.VALIDATE(condition)
  172.     local cType = condition.type
  173.     if not cType or type(MULTI.FILTERS[cType]) ~= "function" then return false end
  174.     local conditions = condition.conditions
  175.     if type(conditions or {}) ~= "table" then return false end
  176.     local s,isValid = pcall(MULTI.FILTERS[cType],unpack(conditions or {}))
  177.     return s and isValid
  178. end
  179.  
  180. --Load metadata
  181. if fs.exists(assignmentsPath) then
  182.     local file = fs.open(assignmentsPath,"r")
  183.     local data = textutils.unserialiseJSON(file.readAll()) or {}
  184.     file.close()
  185.     if type(data.custom_filters) == "table" then
  186.         for k,v in pairs(data.custom_filters) do
  187.             local f = loadstring(tostring(v))
  188.             if f then
  189.                 local _,filter = pcall(f)
  190.                 if type(filter) == "function" then
  191.                     MULTI[k] = filter
  192.                 end
  193.             end
  194.         end
  195.     end
  196.     if type(data.new_id) == "string" then
  197.         local f = loadstring(data.new_id)
  198.         if f then
  199.             local _,idFunction = pcall(f)
  200.             if type(idFunction) == "function" then
  201.                 MULTI.NEW_ID = idFunction
  202.             end
  203.         end
  204.     end
  205. end
  206.  
  207. function MULTI:getID()
  208.     if self.id then return self.id end
  209.     if fs.exists(assignmentsDir) and fs.isDir(assignmentsDir) then
  210.         for k,v in pairs(fs.list(assignmentsDir)) do
  211.             local file = fs.open(fs.combine(assignmentsDir,v),"r")
  212.             local conditions = textutils.unserialise(file.readAll())
  213.             file.close()
  214.             if MULTI.VALIDATE(conditions) then
  215.                 self.id = v
  216.                 return v
  217.             end
  218.         end
  219.     end
  220.     local id,conditions = MULTI.NEW_ID()
  221.     self.id = id
  222.     local file = fs.open(fs.combine(assignmentsDir,id),"w+")
  223.     file.write(assert(textutils.serialise(conditions),"could not serialize conditions table!"))
  224.     file.close()
  225.     return id
  226. end
  227.  
  228. function MULTI:getIDS()
  229.     return fs.list(assignmentsDir)
  230. end
  231.  
  232. local function getBroadcastFolder(id)
  233.     local folder = "shared/"..id.."/inbox/"
  234.     if not fs.exists(folder) then fs.makeDir(folder) end
  235.     return folder
  236. end
  237.  
  238. function MULTI:new()
  239.     local multi = {}
  240.     multi.message_queue = {}
  241.     setmetatable(multi,self)
  242.     self.__index = self
  243.     local id = multi:getID()
  244.     multi.personalFolder = "private/"..id
  245.     if not fs.exists(multi.personalFolder) then
  246.         fs.makeDir(multi.personalFolder)
  247.     end
  248.     multi.sharedFolder = "shared/"..id
  249.     if not fs.exists(multi.sharedFolder) then
  250.         fs.makeDir(multi.sharedFolder)
  251.     end
  252.     return multi
  253. end
  254.  
  255. function MULTI:__tostring()
  256.     return "MULTI: " .. tostring(self.id)
  257. end
  258.  
  259. --Returns senderID,message,protocol
  260. function MULTI:receive(otherID,nTimeOut)
  261.     if not self.sharedFolder then error("Multi not initialized") end
  262.     if otherID then
  263.         if not self.message_queue[otherID] then self.message_queue[otherID] = {} --Make sure theres queue for the other id
  264.         elseif #self.message_queue[otherID] > 0 then return otherID,unpack(table.remove(self.message_queue[otherID])) end --If theres already message queue for that id then return that message
  265.         local targetPath = getBroadcastFolder(self.id) .. otherID .. ".broadcast"
  266.         local endEpoch = os.epoch("utc") + (nTimeOut or 0)*1000
  267.         while not nTimeOut or os.epoch("utc") <= endEpoch do
  268.             if fs.exists(targetPath) then
  269.                 local file = fs.open(targetPath,"r")
  270.                 local data = file.readAll()
  271.                 file.close()
  272.                 fs.delete(targetPath)
  273.                 for msg in data:gsub("[^\n]+") do
  274.                     msg = textutils.unserialiseJSON(msg)
  275.                     if msg then
  276.                         table.insert(self.message_queue[otherID],msg)
  277.                     end
  278.                 end
  279.                 if #self.message_queue[otherID] > 0 then return otherID,unpack(table.remove(self.message_queue[otherID])) end
  280.             end
  281.             sleep(0)
  282.         end
  283.     else
  284.         local ids = self:getIDs()
  285.         local id = self:getID()
  286.         local funcs = {}
  287.         local output = {}
  288.         for k,v in ipairs(ids) do
  289.             if v ~= id then
  290.                 table.insert(funcs,function() output = {self:receive(v,nTimeOut)};return end)
  291.             end
  292.         end
  293.         parallel.waitForAny(unpack(funcs))
  294.         return unpack(output)
  295.     end
  296. end
  297.  
  298. function MULTI:send(otherID,msg,protocol)
  299.     if id == self:getID() then return false end
  300.     if not self.sharedFolder then error("Multi not initialized") end
  301.     local packet = {msg}
  302.     if protocol then packet = {msg,protocol} end
  303.     packet = assert(textutils.serialiseJSON(packet),"Could not serialise json for the message")
  304.     local targetPath = getBroadcastFolder(otherID) .. self:getID() .. ".broadcast"
  305.     local file = fs.open(targetPath,fs.exists(targetPath) and "a" or "w")
  306.     file.writeLine(packet)
  307.     file.close()
  308. end
  309.  
  310. function MULTI:broadcast(msg,protocol)
  311.     local ids = self:getIDs()
  312.     for k,v in ipairs(ids) do
  313.         self:send(v,msg,protocol)
  314.     end
  315. end
  316.  
  317. return MULTI:new()]],
  318.     --json
  319.     ["json.lua"] = [[--Concurrent JSON editing
  320. local JSON = {}
  321.  
  322. local loaded_files = {}
  323.  
  324. function JSON:__refresh()
  325.     local filePath = rawget(self,"__filePath")
  326.     if filePath and fs.exists(filePath) then
  327.         local file = fs.open(filePath,"r")
  328.         rawset(self,"__data",textutils.unserialiseJSON(file.readAll()) or rawget(self,"__data"))
  329.         file.close()
  330.     end
  331. end
  332.  
  333. function JSON:__new(filePath)
  334.     if loaded_files[filePath] then return loaded_files[filePath] end
  335.     local json = {__data = {},__filePath = filePath}
  336.     if fs.exists(filePath) then
  337.         local file = fs.open(filePath,"r")
  338.         json.__data = textutils.unserialiseJSON(file.readAll()) or {}
  339.         file.close()
  340.     end
  341.     json.__name = tostring(json.__data):gsub("table","json")
  342.     setmetatable(json,self)
  343.     loaded_files[filePath] = json
  344.     return json
  345. end
  346.  
  347. function JSON:__tostring()
  348.     return rawget(self,"__name") or ""
  349. end
  350.  
  351. function JSON:__pairs()
  352.     self:__refresh()
  353.     return pairs(rawget(self,"__data"))
  354. end
  355.  
  356. function JSON:__ipairs()
  357.     self:__refresh()
  358.     return ipairs(rawget(self,"__data"))
  359. end
  360.  
  361. function JSON:__len()
  362.     return #rawget(self,"__data")
  363. end
  364.  
  365. function JSON:__totable()
  366.     self:__refresh()
  367.     return rawget(self,"__data")
  368. end
  369.  
  370. function JSON:__index(idx)
  371.     if not rawget(self,"__data") then return rawget(self,idx) end
  372.     local item = rawget(JSON,idx)
  373.     if item ~= nil then return item
  374.     else
  375.         self:__refresh()
  376.         return rawget(self,"__data")[idx]
  377.     end
  378. end
  379. --Last thing defined
  380. JSON.__newindex = function(self,key,value)
  381.     if not rawget(self,"__filePath") then return rawset(self,key,value) end
  382.     self:__refresh()
  383.     rawget(self,"__data")[key] = value
  384.     local file = fs.open(rawget(self,"__filePath"),"w+")
  385.     file.write(textutils.serialiseJSON(rawget(self,"__data")))
  386.     file.close()
  387. end
  388.  
  389. local serialize,serializeJSON = textutils.serialize,textutils.serializeJSON
  390.  
  391. textutils.serialize = function( t )
  392.     if getmetatable(t) == JSON then return serialize(rawget(t,"__data")) end
  393.     return serialize(t)
  394. end
  395. textutils.serialise = textutils.serialize
  396. textutils.serializeJSON = function( t, bNBTStyle )
  397.     if getmetatable(t) == JSON then return serializeJSON(rawget(t,"__data"), bNBTStyle) end
  398.     return serializeJSON(t,bNBTStyle)
  399. end
  400. textutils.serialiseJSON = textutils.serializeJSON
  401.  
  402. return function(path) return JSON:__new(path) end]]
  403. }
  404.  
  405. for k,v in pairs(files) do
  406.     local file = fs.open(k,"w+")
  407.     file.write(v)
  408.     file.close()
  409. end
Advertisement
Add Comment
Please, Sign In to add comment