Advertisement
JustGekto

rednet_tools

Oct 21st, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.35 KB | None | 0 0
  1. --[[
  2.     Rednet tools ver 2.5 made by JustGekto
  3.  
  4.     Free to use modify and distribute
  5.     ONLY IF LINK TO ORIGINAL PRESENT
  6. --]]
  7. --Varibles
  8. local pause = false
  9. local settings = {}
  10. local xc,yc = term.getCursorPos()
  11. local mode = 1
  12. local messages = {}
  13. local np = print
  14. local xp,yp = 0,0
  15. scripts = {}
  16. xm,ym = term.getSize()
  17. --Functions
  18. local function toboolean(text)
  19.     if text == "true" then
  20.         return true
  21.     else
  22.         return false
  23.     end
  24. end
  25. local function clear()
  26.     shell.run("clear")
  27. end
  28. local function open(nChanel)
  29.     peripheral.call(side, "open", nChanel )
  30. end
  31. local function close(nChanel)
  32.     peripheral.call(side, "close", nChanel )
  33. end
  34. local function stringToTable(text)
  35.     local done,result = pcall(loadstring("return "..text))
  36.     if done then
  37.         return result
  38.     else
  39.         term.setTextColor(colors.red)
  40.         print("There was an error converting table:")
  41.         print(result)
  42.         sleep(1)
  43.         return nil
  44.     end
  45. end
  46. local function isTable(text)
  47.     if string.byte(text) == 123 and string.byte(text,#text) == 125 then
  48.         return true
  49.     else
  50.         return false
  51.     end
  52. end
  53. local function mode_string()
  54.     if mode == 1 then
  55.         return "r"
  56.     else
  57.         return "s"
  58.     end
  59. end
  60. local function print(text,b)
  61.     if b ~= true then
  62.         table.insert(messages,text)
  63.     end
  64.     np(text)
  65. end
  66. local function addMessage(msg)
  67.     table.insert(messages,msg)
  68. end
  69. local function findModem()
  70.     local PeriList = peripheral.getNames()
  71.     for i=1,#PeriList do
  72.       if peripheral.getType(PeriList[i]) == "modem" then
  73.         return PeriList[i]
  74.       end
  75.     end
  76.   end
  77.   local function getn(table)
  78.     local i = 0
  79.     for key,val in pairs(table) do
  80.         i = i+1
  81.     end
  82.     return i
  83. end
  84. function table_print (tt, indent, done)
  85.     done = done or {}
  86.     indent = indent or 0
  87.     if type(tt) == "table" then
  88.         for key, value in pairs (tt) do
  89.             io.write(string.rep (" ", indent)) -- indent it
  90.             if type (value) == "table" and not done [value] then
  91.                 done [value] = true
  92.                 io.write(string.format("%s = {\n", tostring (key)));
  93.                 addMessage(string.rep (" ", indent)..string.format("%s = {", tostring (key)))
  94.                 io.write(string.rep (" ", indent)) -- indent it
  95.                 table_print (value, indent + 1, done)
  96.                 io.write(string.rep (" ", indent)) -- indent it
  97.                 io.write("}\n");
  98.                 addMessage(string.rep (" ", indent).."}")
  99.             else
  100.                 if type(value) == "string" then
  101.                     io.write(string.format("%s = \"%s\",\n", tostring (key), value))
  102.                     addMessage(string.rep (" ", indent)..string.format("%s = \"%s\",", tostring (key), value))
  103.                 else
  104.                     io.write(string.format("%s = %s,\n", tostring (key), tostring(value)))
  105.                     addMessage(string.rep (" ", indent)..string.format("%s = %s,", tostring (key), tostring(value)))
  106.                 end
  107.             end
  108.         end
  109.     else
  110.         io.write(tt .. "\n")
  111.     end
  112. end
  113. local function send( nRecipient,nSender,message)
  114.     nMessageID = math.random( 1, 2147483647 )
  115.     local tMessage = {
  116.         nMessageID = nMessageID,
  117.         nRecipient = nRecipient,
  118.         message = message,
  119.         sProtocol = sProtocol,
  120.     }
  121.     peripheral.call(side,"transmit",nRecipient,nSender,tMessage)
  122.     peripheral.call(side,"transmit",65533,nSender,tMessage)
  123. end
  124. local function DrawTopOLD()
  125.     local x,y = term.getCursorPos()
  126.     term.setCursorPos(1,1)
  127.     term.setBackgroundColor(colors.lightGray)
  128.     term.clearLine()
  129.     term.setTextColor(colors.white)
  130.     term.setCursorPos(1,1)
  131.     term.write("Mode:"..mode_string())
  132.     term.setCursorPos(8,1)
  133.     term.write("Chanel:"..chanel)
  134.     term.setCursorPos(xm-2,1)
  135.     term.write("E")
  136.     term.setCursorPos(xm-4,1)
  137.     term.write("C")
  138.     term.setCursorPos(xm-6,1)
  139.     term.write("S")
  140.     term.setTextColor(colors.white)
  141.     term.setBackgroundColor(colors.red)
  142.     term.setCursorPos(xm,1)
  143.     term.write("X")
  144.     term.setBackgroundColor(colors.white)
  145.     term.setTextColor(colors.gray)
  146.     term.setCursorPos(x,y)
  147. end
  148. local function DrawTop()
  149.     local x,y = term.getCursorPos()
  150.     term.setCursorPos(1,1)
  151.     term.setBackgroundColor(colors.lightGray)
  152.     term.clearLine()
  153.     term.setCursorPos(1,1)
  154.     term.setTextColor(colors.white)
  155.     term.write("Menu")
  156.     term.setCursorPos(6,1)
  157.     term.setTextColor(colors.white)
  158.     term.write("Chanel:"..chanel)
  159.     term.setTextColor(colors.white)
  160.     term.setCursorPos(20,1)
  161.     if pause then
  162.         term.setTextColor(colors.red)
  163.         term.write("||")
  164.     else
  165.         term.setTextColor(colors.green)
  166.         term.write(string.char(16))
  167.     end
  168.     term.setCursorPos(xm,1)
  169.     term.setBackgroundColor(colors.red)
  170.     term.setTextColor(colors.white)
  171.     term.write("X")
  172.     term.setBackgroundColor(colors.white)
  173.     term.setTextColor(colors.gray)
  174.     term.setCursorPos(x,y)
  175. end
  176. local function DrawMain()
  177.     term.setBackgroundColor(colors.white)
  178.     clear()
  179.     DrawTop()
  180.     term.setBackgroundColor(colors.white)
  181.     term.setTextColor(colors.gray)
  182.     term.setCursorPos(1,2)
  183. end
  184. local function addScript()
  185.     term.setBackgroundColor(colors.white)
  186.     clear()
  187.     DrawTop()
  188.     term.setCursorPos(1,2)
  189.     print("Name         :",true)
  190.     print("On id message:",true)
  191.     print("With message :",true)
  192.     print("To           :",true)
  193.     print("Send",true)
  194.     print("Message      :",true)
  195.     print("To           :",true)
  196.     print("From         :",true)
  197.     term.setCursorPos(15,2)
  198.     local name = read()
  199.     term.setCursorPos(15,3)
  200.     local sid = tonumber(read())
  201.     term.setCursorPos(15,4)
  202.     local sm = read()
  203.     term.setCursorPos(15,5)
  204.     local nr = tonumber(read())
  205.     term.setCursorPos(15,7)
  206.  
  207.     local fm = read() -- fake message
  208.     if isTable(fm) then
  209.         fm = stringToTable(fm)
  210.     end
  211.     term.setCursorPos(15,8)
  212.     local ri = tonumber(read()) -- fake receiver
  213.     term.setCursorPos(15,9)
  214.     local fs = tonumber(read()) -- fake sender
  215.     open(chanel)
  216.     mode = 1
  217.     local data = {sid = sid,sm = sm,nr = nr,fm = fm,ri = ri,fs = fs,name = name}
  218.     table.insert(scripts,data)
  219.     term.setTextColor(colors.green)
  220.     print("Saved!",true)
  221.     term.setTextColor(colors.gray)
  222.     sleep(0.3)
  223. end
  224. local function SaveScript()
  225.     term.setBackgroundColor(colors.white)
  226.     clear()
  227.     DrawTop()
  228.     term.setCursorPos(1,2)
  229.     term.setTextColor(colors.black)
  230.     for i=1,#scripts do
  231.         x,y = term.getCursorPos()
  232.         if x+#scripts[i].name+1 <= xm then
  233.             term.write(scripts[i].name)
  234.             if i ~= #scripts then
  235.                 term.write(",")
  236.             end
  237.         else
  238.             term.setCursorPos(1,y+1)
  239.         end
  240.     end
  241.     x,y = term.getCursorPos()
  242.     term.setCursorPos(1,y+1)
  243.     term.write("Name:")
  244.     local name = read()
  245.     for i=1,#scripts do
  246.         if scripts[i].name == name then
  247.             local fname = "RT/scripts/"..name..".rts"
  248.             local s = scripts[i]
  249.             file = fs.open(fname,"w")
  250.             file.close()
  251.             config.write(fname,"sid",s.sid)
  252.             config.write(fname,"sm",s.sm)
  253.             config.write(fname,"nr",s.nr)
  254.             config.write(fname,"fm",s.fm)
  255.             config.write(fname,"ri",s.ri)
  256.             config.write(fname,"fs",s.fs)
  257.             config.write(fname,"name",s.name)
  258.             term.setTextColor(colors.green)
  259.             print("Saved as "..fname,true)
  260.             term.setTextColor(colors.lightGray)
  261.             sleep(1)
  262.         end
  263.     end
  264. end
  265. function GetFileExtension(name)
  266.     if name~= nil then
  267.         return name:match("^.+(%..+)$")
  268.     end
  269.   end
  270. local function LoadScript()
  271.     term.setBackgroundColor(colors.white)
  272.     clear()
  273.     DrawTop()
  274.     term.setCursorPos(1,2)
  275.     term.setTextColor(colors.black)
  276.     local files = fs.list("RT/scripts")
  277.     for i=1,#files do
  278.         if GetFileExtension(files[i]) ~= ".rts" then
  279.             table.remove(files,i)
  280.         end
  281.     end
  282.     for i=1,#files do
  283.         x,y = term.getCursorPos()
  284.         if x+#files[i] <= xm then
  285.             term.write(files[i])
  286.             if i ~= #files then
  287.                 term.write(",")
  288.             end
  289.         else
  290.             term.setCursorPos(1,y+1)
  291.         end
  292.     end
  293.     x,y = term.getCursorPos()
  294.     term.setCursorPos(1,y+1)
  295.     term.write("Name:")
  296.     local name = read()
  297.     for i=1,#files do
  298.         if name == files[i] then
  299.             local data = {}
  300.             local fname = "RT/scripts/"..files[i]
  301.             data.sid = tonumber(config.read(fname,"sid"))
  302.             data.sm = config.read(fname,"sm")
  303.             data.nr = tonumber(config.read(fname,"nr"))
  304.             data.fm = config.read(fname,"fm")
  305.             data.ri = tonumber(config.read(fname,"ri"))
  306.             data.fs = tonumber(config.read(fname,"fs"))
  307.             data.name = config.read(fname,"name")
  308.             table.insert(scripts,data)
  309.             term.setTextColor(colors.green)
  310.             print("Loaded "..files[i],true)
  311.             term.setTextColor(colors.lightGray)
  312.             sleep(1)
  313.         end
  314.     end
  315. end
  316. local function ExecuteScripts(data)
  317.     id = data[4]
  318.     msg = data[5].message
  319.     receiver = data[5].nRecipient
  320.     for i=1,#scripts do
  321.         if id == scripts[i]["sid"] then
  322.             if scripts[i]["sm"] == msg then
  323.                 if receiver == scripts[i]["nr"] then
  324.                     send(scripts[i]["ri"],scripts[i]["fs"],scripts[i]["fm"])
  325.                     print("Script \""..scripts[i]["name"].."\"",true)
  326.                 end
  327.             end
  328.         end
  329.     end
  330. end
  331. local function FakeMessage()
  332.     term.setBackgroundColor(colors.white)
  333.     clear()
  334.     DrawTop()
  335.     term.setBackgroundColor(colors.white)
  336.     term.setTextColor(colors.gray)
  337.     term.setCursorPos(1,2)
  338.     print("Sender   id:",true)
  339.     print("Receiver id:",true)
  340.     print("Message    :",true)
  341.     term.setCursorPos(13,2)
  342.     local s = tonumber(read())
  343.     term.setCursorPos(13,3)
  344.     local r = tonumber(read())
  345.     term.setCursorPos(13,4)
  346.     local m = read()
  347.     if isTable(m) then
  348.         m = stringToTable(m)
  349.     end
  350.     if m == nil then
  351.         term.setTextColor(colors.red)
  352.         print("Error!",true)
  353.         term.setTextColor(colors.gray)
  354.         return
  355.     end
  356.     open(chanel)
  357.     send( r,s,m)
  358.     mode = 1
  359.     term.setTextColor(colors.green)
  360.     print("Sent!",true)
  361.     sleep(0.3)
  362.     term.setTextColor(colors.gray)
  363.     sleep(0.3)
  364. end
  365. local function RedrawMain()
  366.     term.setBackgroundColor(colors.white)
  367.     clear()
  368.     DrawTop()
  369.     term.setBackgroundColor(colors.white)
  370.     term.setTextColor(colors.gray)
  371.     term.setCursorPos(1,2)
  372.     for i in pairs(messages) do
  373.         print(messages[i],true)
  374.     end
  375. end
  376. local function export()
  377.     time = os.time()
  378.     file = fs.open("RT/exports/rednet_data_"..time,"w")
  379.     for i in pairs(messages) do
  380.         file.writeLine(messages[i])
  381.     end
  382.     file.close()
  383.     term.setTextColor(colors.green)
  384.     term.setBackgroundColor(colors.white)
  385.     print("Exported as ".."RT/exports/rednet_data_"..time,true)
  386.     term.setTextColor(colors.gray)
  387.     os.sleep(1)
  388.     RedrawMain()
  389. end
  390. local function main()
  391.     while true do
  392.         e = {os.pullEvent()}
  393.         if e[1] == "modem_message" and not pause then
  394.             if type(e[5]) == "table" then
  395.                 ExecuteScripts(e)
  396.                 if type(e[5].message) == "string" or type(e[5].message) == "number" or type(e[5].message) == "boolean" then
  397.                     local msg = "id:"..e[4].." msg:"..tostring(e[5].message).." recipient:"..e[5].nRecipient.." chanel:"..e[3]
  398.                     print(msg)
  399.                 elseif type(e[5].message) == "table" then
  400.                     print("id:"..e[4].." recipient:"..e[5].nRecipient.." chanel:"..e[3].." msg:")
  401.                     print("{")
  402.                     table_print(e[5].message,1,false)
  403.                     print("}")
  404.                 end
  405.             else
  406.                 local msg = "id:"..e[4].." msg:"..tostring(e[5]).." recipient:"..e[3]
  407.                 print(msg)
  408.             end
  409.         elseif e[1] == "mouse_click" then
  410.             local x = e[3]
  411.             local y = e[4]
  412.  
  413.             if x>=1 and x<= 4 and y == 1 then
  414.                 local ans = context.menu(1,2,{"Fake message"},{"Export"},{"Clear"},{"Set default chanel",true},"-",{"Add script"},{"Save script"},{"Load script"},"-",{"Uninstall"})
  415.                 if ans == "Fake message" then
  416.                     FakeMessage()
  417.                     RedrawMain()
  418.                 elseif ans == "Export" then
  419.                     RedrawMain()
  420.                     export()
  421.                 elseif ans == "Clear" then
  422.                     messages = {}
  423.                     DrawMain()
  424.                 elseif ans == "Add script" then
  425.                     addScript()
  426.                 elseif ans == "Save script" then
  427.                     SaveScript()
  428.                 elseif ans == "Load script" then
  429.                     LoadScript()
  430.                 elseif ans == "Set default chanel" then
  431.                     chanel = 65533
  432.                     config.write("RT/settings.cfg","chanel",chanel)
  433.                 elseif ans == "Uninstall" then
  434.                     shell.run("RT/uninstaller")
  435.                     break
  436.                 end
  437.                 RedrawMain()
  438.             elseif x == xm and y == 1 then
  439.                 break
  440.             elseif x >= 6 and x <= 12 + #tostring(chanel) and y == 1 then
  441.                 term.setCursorPos(1,1)
  442.                 term.setBackgroundColor(colors.lightGray)
  443.                 term.clearLine()
  444.                 term.setCursorPos(1,1)
  445.                 term.setTextColor(colors.white)
  446.                 term.write("Menu")
  447.                 term.setTextColor(colors.white)
  448.                 term.setBackgroundColor(colors.red)
  449.                 term.setCursorPos(xm,1)
  450.                 term.write("X")
  451.                 term.setCursorPos(6,1)
  452.                 term.setBackgroundColor(colors.lightGray)
  453.                 term.setTextColor(colors.white)
  454.                 if settings.oc then
  455.                     close(chanel)
  456.                 end
  457.                 term.write("Chanel:")
  458.                 chanel = tonumber(read())
  459.                 open(chanel)
  460.                 config.write("RT/settings.cfg","chanel",chanel)
  461.                 term.setBackgroundColor(colors.white)
  462.                 term.setTextColor(colors.gray)
  463.             elseif x >= 20  and x <= 21 and y == 1 then
  464.                 pause = not pause
  465.                 RedrawMain()
  466.             end
  467.         end
  468.         DrawTop()
  469.     end
  470. end
  471. --Pre load checkers
  472. if pocket then
  473.     term.setTextColor(colors.red)
  474.     print("Warning program not optimized for small screen",false)
  475.     sleep(1)
  476. end
  477. if not fs.exists("RT/API/config") then
  478.     print("Some files may have been corrupted!",true)
  479.     print("Running file recover...",true)
  480.     shell.run("pastebin run XZktg6DH ")
  481.     error("",0)
  482. end
  483. os.loadAPI("RT/API/config")
  484. if not fs.exists("RT/API/context") then
  485.     print("Some files may have been corrupted!",true)
  486.     print("Running file recover...",true)
  487.     shell.run("pastebin run XZktg6DH ")
  488.     error("",0)
  489. end
  490. os.loadAPI("RT/API/context")
  491. if fs.exists("RT/settings.cfg") then
  492.     chanel = tonumber(config.read("RT/settings.cfg","chanel"))
  493.     settings.oc = toboolean(config.read("RT/settings.cfg","single_chanel"))
  494. else
  495.     file = fs.open("RT/settings.cfg","w")
  496.     file.close()
  497.     config.write("RT/settings.cfg","chanel","65533")
  498.     config.write("RT/settings.cfg","single_chanel","false")
  499.     chanel = 65533
  500.     settings.oc = false
  501. end
  502. --Start
  503. side = findModem()
  504. DrawMain()  
  505. if side == nil then
  506.     error("You need modem to use this program",0)
  507. end
  508. open(65533)
  509. open(chanel)
  510. main()
  511. term.setBackgroundColor(colors.black)
  512. clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement