Advertisement
OfficialStamper

op.lua

Feb 8th, 2023
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 18.01 KB | Gaming | 0 0
  1. --op.lua
  2. --Uses In game documentation for computercraft peripherals and displays it in a practical way
  3. --Explicitly supports
  4.         --Open Peripherals
  5.         --Logistics Pipes
  6.        
  7. local tArgs = {...}
  8.  
  9. local fTitle = "METHODMAN: Practical display for in game documentation of computercraft + addons\n"
  10. local newFiles = {}
  11. local newFilesLookup = {}
  12. local logisticsPipeID1 = "LogisticsPipes"
  13. local logisticsPipeID2 = "LogisticsChassiePipe"
  14. local pastebinEnabled = false -- Set to true to enable automatic pastebin upload capability
  15. local fastMode = false -- Set to true to prevent the program to ask you if you want to overwrite a pre-existing file
  16.  
  17. for i,v in pairs(tArgs) do
  18.         if v == "pastebin" then
  19.                 pastebinEnabled = true
  20.                 print("Pastebin mode enabled")
  21.         elseif v == "fast" then
  22.                 fastMode = true
  23.                 print("Fast mode enabled")
  24.         end
  25. end
  26.  
  27. function advMethodHandlerUpdated(data, name)  
  28.         local file = fs.open(name, "a")
  29.         file.writeLine("\nOpen Peripherals Advanced Method Data")
  30.         local c = 0
  31.         for i,v in pairs(data) do
  32.                 c = c+1
  33.                 local name = i
  34.                 if type(v["name"]) ~= "nil" then
  35.                         name = v["name"]
  36.                 end
  37.                 file.writeLine("\n"..c..". "..name.."\n")
  38.                 local returnType = "Unknown"
  39.                 if v["returnType"] ~= nil then
  40.                         returnType = v["returnType"]
  41.                 elseif v["returnTypes"] ~= nil then
  42.                         returnType = ""
  43.                         local tLen = #v["returnTypes"]
  44.                         for j = 1,tLen do
  45.                                 local str = v["returnTypes"][j]
  46.                                 if j < tLen then
  47.                                         returnType = returnType..str..", "
  48.                                 else
  49.                                         returnType = returnType..str
  50.                                 end
  51.                         end
  52.                 end
  53.                 file.writeLine("Return Type: "..returnType)
  54.                 file.writeLine("Description: "..v["description"])
  55.                 local args = v["args"]
  56.                 local str = "("
  57.                 local len =  #args
  58.                 for k = 1, len do
  59.                         str = str..args[k]["name"]
  60.                         if k < len then
  61.                                 str = str..", "
  62.                         end
  63.                 end
  64.                 file.writeLine("\n\tArguments: "..name..str..")")
  65.                 if len >= 1 then
  66.                         file.writeLine("")
  67.                         for k = 1,len do
  68.                                 file.writeLine("\t\t"..k..". "..args[k]["name"])                      
  69.                                 file.writeLine("\t\tDescription: "..args[k]["description"])
  70.                                 file.writeLine("\t\tType: "..args[k]["type"])
  71.                         end
  72.                 else
  73.                         file.writeLine("\tNo Arguments")
  74.                 end    
  75.         end
  76.         file.close()
  77. end
  78.  
  79. --[[
  80. function advMethodHandler(data, name)
  81.         local file = fs.open(name, "a")
  82.         file.writeLine("\nOpen Peripherals Advanced Method Data")
  83.         for i,v in pairs(data) do
  84.                 file.writeLine("\n"..i..". "..v["name"].."\n")
  85.                 local returnType = "Unknown"
  86.                 if v["returnType"] ~= nil then
  87.                         returnType = v["returnType"]
  88.                 elseif v["returnTypes"] ~= nil then
  89.                         returnType = ""
  90.                         local tLen = #v["returnTypes"]
  91.                         for j = 1,tLen do
  92.                                 local str = v["returnTypes"][j]
  93.                                 if j < tLen then
  94.                                         returnType = returnType..str..", "
  95.                                 else
  96.                                         returnType = returnType..str
  97.                                 end
  98.                         end
  99.                 end
  100.                 file.writeLine("Return Type: "..returnType)
  101.                 file.writeLine("Description: "..v["description"])
  102.                 local args = v["args"]
  103.                 local str = "("
  104.                 local len =  #args
  105.                 for k = 1, len do
  106.                         str = str..args[k]["name"]
  107.                         if k < len then
  108.                                 str = str..", "
  109.                         end
  110.                 end
  111.                 file.writeLine("\n\tArguments: "..v["name"]..str..")")
  112.                 if len >= 1 then
  113.                         file.writeLine("")
  114.                         for k = 1,len do
  115.                                 file.writeLine("\t\t"..k..". "..args[k]["name"])                      
  116.                                 file.writeLine("\t\tDescription: "..args[k]["description"])
  117.                                 file.writeLine("\t\tType: "..args[k]["type"])
  118.                         end
  119.                 else
  120.                         file.writeLine("\tNo Arguments")
  121.                 end    
  122.         end
  123.         file.close()
  124. end
  125. ]]
  126.  
  127. function lpMethodHandler(data, name)
  128.         local file = fs.open(name, "a")
  129.         file.writeLine("\nLogistics Pipes Peripheral Help")
  130.         file.write(data)
  131.         file.close()
  132. end
  133.  
  134. function yesOrNo(timeout)
  135.         local timer
  136.         local answer = "timeout"
  137.         if type(timeout) == "number" then
  138.                 timer = os.startTimer(timeout)
  139.         end
  140.         while true do
  141.                 local event = {os.pullEvent()}
  142.                 if event[2] == timer then
  143.                         break
  144.                 elseif event[1] == "key" then
  145.                         local key = string.lower(keys.getName(event[2]))
  146.                         if key == "y" or key == "n" then
  147.                                 if key == "y" then
  148.                                         answer = true
  149.                                 elseif key == "n" then
  150.                                         answer = false
  151.                                 end
  152.                                 break
  153.                         end
  154.                 end
  155.         end
  156.         return answer
  157. end
  158.  
  159. function uploadToPastebin(files)
  160.         for i,v in pairs(files) do
  161.                 term.clear()
  162.                 term.setCursorPos(1,1)
  163.                 print("Uploading file: "..v)
  164.                 shell.run("pastebin", "put", v)
  165.                 print("Press any key to continue")
  166.                 os.pullEvent("key")
  167.         end
  168. end
  169.  
  170. function isValidReport(path)
  171.         local file = fs.open(path, "r")
  172.         local line = file.readLine()
  173.         file.close()
  174.         if line == fTitle or line.."\n" == fTitle then
  175.                 return true
  176.         else
  177.                 return false
  178.         end
  179. end
  180.  
  181. function newFileName(name)
  182.         local fileName = nil
  183.         local c = 2
  184.         while true do
  185.                 local testName = name.."_"..c
  186.                 if fs.exists(testName) == false then
  187.                         fileName = testName
  188.                         break
  189.                 end
  190.                 c = c+1
  191.         end
  192.         return fileName
  193. end
  194.  
  195. function mainProg(side)
  196.         local pType = peripheral.getType(side)
  197.         local isLPPipe = false
  198.         if string.match(pType, logisticsPipeID1) == logisticsPipeID1 or string.match(pType, logisticsPipeID2) == logisticsPipeID2 then
  199.                 isLPPipe = true
  200.         end
  201.         if pType == "modem" then
  202.                 local p = peripheral.wrap(side)
  203.                 if p.isWireless() == true then
  204.                         pType = "wireless_modem"
  205.                 else
  206.                         pType = "wired_modem"
  207.                 end
  208.         end
  209.         local header = "Peripheral found\n\tSide: "..side.."\n\tType: "..pType.."\n"
  210.         print(header)
  211.         local fileName
  212.         if fs.exists(pType) == true then
  213.                 local isReport = isValidReport(pType)
  214.                 if fastMode == false then
  215.                         term.clear()
  216.                         term.setCursorPos(1,1)
  217.                         if isReport == false then
  218.                                 print("A file by the name of "..pType.." was detected but it does not appear to be a report.")
  219.                                 print("If you wish to overwrite this file press \"o\", else press any other key.")
  220.                                 local event = {os.pullEvent("key")}
  221.                                 local key = string.lower(keys.getName(event[2]))
  222.                                 if key == "o" then
  223.                                         print("Are you sure? (y/n)")
  224.                                         local answer = yesOrNo()
  225.                                         if answer == true then
  226.                                                 fileName = pType
  227.                                         else
  228.                                                 fileName = newFileName(pType)
  229.                                         end
  230.                                 else
  231.                                         fileName = newFileName(pType)
  232.                                 end
  233.                         else
  234.                                 print("A report for peripheral: "..pType.." already exists.")
  235.                                 print("Do you wish to overwrite this file or generate a new filename?")
  236.                                 print("Press the \"y\" key to overwrite, press \"n\" to get a new filename")
  237.                                 local answer = yesOrNo()
  238.                                 if answer == false then
  239.                                         fileName = newFileName(pType)
  240.                                 else
  241.                                         fileName = pType
  242.                                 end
  243.                         end
  244.                         term.clear()
  245.                         sleep(0.1)
  246.                 else
  247.                         if isReport == true then
  248.                                 fileName = pType
  249.                         else
  250.                                 print("A file by the name of "..pType.." was detected but it does not appear to be a report.")
  251.                                 print("Automatically getting a new name for report file")
  252.                                 fileName = newFileName(pType)
  253.                         end
  254.                 end
  255.         else
  256.                 fileName = pType
  257.         end
  258.         if newFilesLookup[fileName] == nil then
  259.                 table.insert(newFiles, 0, fileName)
  260.         end
  261.         newFilesLookup[fileName] = true
  262.         local file = fs.open(fileName, "w")
  263.         file.writeLine(fTitle)
  264.         file.writeLine(header)
  265.         file.close()  
  266.         local basicMethods = peripheral.getMethods(side)
  267.         local openPeripheralSupport = false
  268.         local logisticsPipesSupport = false
  269.         print("Generating Basic Method List")
  270.         file = fs.open(fileName, "a")
  271.         file.writeLine(pType.." method list:\n")
  272.         file.close()  
  273.         for i,v in pairs(basicMethods) do
  274.                 file = fs.open(fileName, "a")
  275.                 file.writeLine("\t"..v)
  276.                 file.close()
  277.                 if v == "getAdvancedMethodsData" then
  278.                         print("Open Peripheral's advanced method data info found")
  279.                         openPeripheralSupport = true
  280.                 end
  281.                 if v == "help" and isLPPipe == true then
  282.                         print("Logistics Pipes help found")
  283.                         logisticsPipesSupport = true
  284.                 end
  285.         end
  286.         print("Basic Method List Complete")
  287.         if openPeripheralSupport then
  288.                 local pHandler = peripheral.wrap(side)
  289.                 print("Resolving squiggly OP nested tables")
  290.                 local advMethods = pHandler.getAdvancedMethodsData()
  291.                 advMethodHandlerUpdated(advMethods, fileName)
  292.         end
  293.         if logisticsPipesSupport then
  294.                 local pHandler = peripheral.wrap(side)
  295.                 print("Resolving LP help string")
  296.                 local lpMethods = pHandler.help()
  297.                 lpMethodHandler(lpMethods, fileName)
  298.         end
  299.         file = fs.open(fileName, "a")
  300.         file.writeLine("\n\n--------------------\n")
  301.         file.writeLine("All actual documentation is written by the mod developer who made the peripheral, not me. I just displayed it - happy computercrafting")
  302.         file.close()
  303.         print("Documentation roundup for "..pType.." complete")
  304. end
  305.  
  306. function deleteReports(safeMode)
  307.         local fileList = fs.list("")
  308.         print("Searching Root Directory for peripheral reports\n")
  309.         for i,v in pairs(fileList) do
  310.                 if fs.isDir(v) == false and isValidReport(v) == true then
  311.                         print("Found a report under the name of: "..v)
  312.                         local canDelete = true
  313.                         if safeMode == true then
  314.                                 print("\t--Do you want to delete this report? (y/n)")
  315.                                 local answer = yesOrNo()
  316.                                 if answer == false then
  317.                                         canDelete = false
  318.                                 end
  319.                         end
  320.                         if canDelete == true then
  321.                                 print("Deleting "..v)
  322.                                 fs.delete(v)
  323.                         end
  324.                         print("")
  325.                 end
  326.                 sleep(0.1)
  327.         end
  328.         print("\nSearch complete")
  329. end
  330.  
  331. function help()
  332.         print("Methodman help\n")
  333.         local helpTable = {
  334.                 [1] = "New files will be created in the root directory. Access this by typing dir into the computer.",
  335.                 [2] = "File names are based on the name of the peripheral, according to the getName method. Sometimes these are weird unlocalised names that would make Direwolf20 laugh.",
  336.                 [3] = "In order for this program to recognise a peripheral you can either place the peripheral directly adjacent to the computer, or you can use wired modems and networking cable to connect peripherals. Wireless modems will not work.",
  337.                 [4] = "This program supports some arguments to change its behaviour when you run it. When running the program, write the name of this program followed by the arguments you want. Valid arguments are listed below.",
  338.                 [5] = "help. Passing this as the first argument will bring up the help section.",
  339.                 [6] = "delete. If you have too many reports, running this program with delete as the first argument gives you a convenient tool to delete them all at once. Optionally you can pass the word, safe, as the second argument to delete in safe mode.",
  340.                 [7] = "The following arguments can be passed together and in any order. They won't do anything in help or delete mode though",
  341.                 [8] = "pastebin. This program can automatically upload your reports to pastebin. By default this is set to false. Passing this argument however will enable it at runtime.",
  342.                 [9] = "fast. Passing this argument enables fastmode. By default this mode is disabled. Enabling it will stop the program from asking if you wish to overwrite pre-existing reports. In fast mode, reports will always be overwritten (although it won't automatically delete non-reports).",
  343.                 [10] = "You can also enable pastebin and fast mode by editing the program and setting some variables to true at the top of the program. Comments guide you to these variables.",
  344.                 }
  345.         for i,v in pairs(helpTable) do
  346.                 print(i..": "..v)
  347.                 print("\t--Press any key to continue\n")
  348.                 os.pullEvent("key")
  349.         end
  350.         print("Thank you for reading and enjoy using this program.")
  351. end
  352.  
  353. function mainWrapper()
  354.         local answer = false
  355.         if fastMode == false then
  356.                 print("Methodman! Documentation for your computer peripherals\n")
  357.                 print("Make easily readable reports to display the documentation available on all those peripherals and save them in the directory.\n")
  358.                 print("Documentation was written by the mod developers, not me. I have just tried to display it nicely.\n")
  359.                 print("Do you wish to continue? (y/n)")
  360.                 answer = yesOrNo(60)
  361.         end
  362.         if answer == true or fastMode == true then
  363.                 term.clear()
  364.                 term.setCursorPos(1,1)                
  365.                 for i,v in pairs(peripheral.getNames()) do
  366.                         mainProg(v)
  367.                 end
  368.                 term.clear()
  369.                 term.setCursorPos(1,1)
  370.                 if #newFiles > 0 then
  371.                         print("The following files have been generated in this computer's directory:\n")
  372.                         for i,v in pairs(newFiles) do
  373.                                 print("\t"..i..": "..v)
  374.                         end
  375.                         if pastebinEnabled == true then
  376.                                 print("\nWould you like to upload these files automatically to pastebin? (y/n)")
  377.                                 local answer = yesOrNo(60)
  378.                                 if answer == true then
  379.                                         uploadToPastebin(newFiles)
  380.                                 end
  381.                         end
  382.                 else
  383.                         print("No new peripherals found")
  384.                 end
  385.                 print("\nHappy computercrafting")
  386.         else
  387.                 print("Okay bye")
  388.         end
  389.         sleep(0.1)
  390. end
  391.  
  392. term.clear()
  393. term.setCursorPos(1,1)
  394. if tArgs[1] == "delete" then
  395.         local safeMode = false
  396.         if tArgs[2] == "safe" then
  397.                 deleteReports(true)
  398.         else
  399.                 deleteReports(false)
  400.         end
  401. elseif tArgs[1] == "help" then
  402.         help()
  403. else
  404.         mainWrapper()
  405. end
  406.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement