Advertisement
casillero

MethodMan - Computercraft Peripheral Documentation

Jan 22nd, 2014
1,795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.20 KB | None | 0 0
  1. --Uses In game documentation for computercraft peripherals and displays it in a practical way
  2. --Explicitly supports
  3.     --Open Peripherals
  4.     --Logistics Pipes
  5.    
  6. local fTitle = "METHODMAN: Practical display for in game documentation of computercraft + addons\n"
  7. local newFiles = {}
  8. local logisticsPipeID1 = "LogisticsPipes"
  9. local logisticsPipeID2 = "LogisticsChassiePipe"
  10.  
  11. function advMethodHandler(data, name)
  12.     local file = fs.open(name, "a")
  13.     file.writeLine("\nOpen Peripherals Advanced Method Data")
  14.     for i,v in pairs(data) do
  15.         file.writeLine("\n"..i..". "..v["name"].."\n")
  16.         local returnType = "Unknown"
  17.         if v["returnType"] ~= nil then
  18.             returnType = v["returnType"]
  19.         elseif v["returnTypes"] ~= nil then
  20.             returnType = ""
  21.             local tLen = #v["returnTypes"]
  22.             for j = 1,tLen do
  23.                 local str = v["returnTypes"][j]
  24.                 if j < tLen then
  25.                     returnType = returnType..str..", "
  26.                 else
  27.                     returnType = returnType..str
  28.                 end
  29.             end
  30.         end
  31.         file.writeLine("Return Type: "..returnType)
  32.         file.writeLine("Description: "..v["description"])
  33.         local args = v["args"]
  34.         local str = "("
  35.         local len =  #args
  36.         for k = 1, len do
  37.             str = str..args[k]["name"]
  38.             if k < len then
  39.                 str = str..", "
  40.             end
  41.         end
  42.         file.writeLine("\n\tArguments: "..v["name"]..str..")")
  43.         if len >= 1 then
  44.             file.writeLine("")
  45.             for k = 1,len do
  46.                 file.writeLine("\t\t"..k..". "..args[k]["name"])           
  47.                 file.writeLine("\t\tDescription: "..args[k]["description"])
  48.                 file.writeLine("\t\tType: "..args[k]["type"])
  49.             end
  50.         else
  51.             file.writeLine("\tNo Arguments")
  52.         end
  53.     end
  54.     file.close()
  55. end
  56.  
  57. function lpMethodHandler(data, name)
  58.     local file = fs.open(name, "a")
  59.     file.writeLine("\nLogistics Pipes Peripheral Help")
  60.     file.write(data)
  61.     file.close()
  62. end
  63.  
  64. function yesOrNo(timeout)
  65.     local timer
  66.     local answer = "timeout"
  67.     if type(timeout) == "number" then
  68.         timer = os.startTimer(timeout)
  69.     end
  70.     while true do
  71.         local event = {os.pullEvent()}
  72.         if event[2] == timer then
  73.             break
  74.         elseif event[1] == "key" then
  75.             local key = string.lower(keys.getName(event[2]))
  76.             if key == "y" or key == "n" then
  77.                 if key == "y" then
  78.                     answer = true
  79.                 elseif key == "n" then
  80.                     answer = false
  81.                 end
  82.                 break
  83.             end
  84.         end
  85.     end
  86.     return answer
  87. end
  88.  
  89. function uploadToPastebin(files)
  90.     for i,v in pairs(files) do
  91.         term.clear()
  92.         term.setCursorPos(1,1)
  93.         print("Uploading file: "..v)
  94.         shell.run("pastebin", "put", v)
  95.         print("Press any key to continue")
  96.         os.pullEvent("key")
  97.     end
  98. end
  99.  
  100. function mainProg(side)
  101.     local pType = peripheral.getType(side)
  102.     local isLPPipe = false
  103.     if string.match(pType, logisticsPipeID1) == logisticsPipeID1 or string.match(pType, logisticsPipeID2) == logisticsPipeID2 then
  104.         isLPPipe = true
  105.     end
  106.     if pType == "modem" then
  107.         local p = peripheral.wrap(side)
  108.         if p.isWireless() == true then
  109.             pType = "wireless_modem"
  110.         else
  111.             pType = "wired_modem"
  112.         end
  113.     end
  114.     local header = "Peripheral found\n\tSide: "..side.."\n\tType: "..pType.."\n"
  115.     print(header)
  116.     local fileName
  117.     if fs.exists(pType) == true then
  118.         term.clear()
  119.         term.setCursorPos(1,1)
  120.         print("A file by the name of "..pType.." already exists")
  121.         print("Do you wish to overwrite this file or generate a new filename?")
  122.         print("Press the \"y\" key to overwrite, press then \"n\" to get a new filename")
  123.         local answer = yesOrNo()
  124.         if answer == false then
  125.             local c = 2
  126.             while true do
  127.                 local testName = pType.."_"..c
  128.                 if fs.exists(testName) == false then
  129.                     fileName = testName
  130.                     break
  131.                 end
  132.                 c = c+1
  133.             end
  134.         else
  135.             fileName = pType
  136.         end
  137.     else
  138.         fileName = pType
  139.     end
  140.     table.insert(newFiles, 0, fileName)
  141.     local file = fs.open(fileName, "w")
  142.     file.writeLine(fTitle)
  143.     file.writeLine(header)
  144.     file.close()   
  145.     local basicMethods = peripheral.getMethods(side)
  146.     local openPeripheralSupport = false
  147.     local logisticsPipesSupport = false
  148.     print("Generating Basic Method List")
  149.     file = fs.open(fileName, "a")
  150.     file.writeLine(pType.." method list:\n")
  151.     file.close()   
  152.     for i,v in pairs(basicMethods) do
  153.         file = fs.open(fileName, "a")
  154.         file.writeLine("\t"..v)
  155.         file.close()
  156.         if v == "getAdvancedMethodsData" then
  157.             print("Open Peripheral's advanced method data info found")
  158.             openPeripheralSupport = true
  159.         end
  160.         if v == "help" and isLPPipe == true then
  161.             print("Logistics Pipes help found")
  162.             logisticsPipesSupport = true
  163.         end
  164.     end
  165.     print("Basic Method List Complete")
  166.     if openPeripheralSupport then
  167.         local pHandler = peripheral.wrap(side)
  168.         print("Resolving squiggly OP nested tables")
  169.         local advMethods = pHandler.getAdvancedMethodsData()
  170.         advMethodHandler(advMethods, fileName)
  171.     end
  172.     if logisticsPipesSupport then
  173.         local pHandler = peripheral.wrap(side)
  174.         print("Resolving LP help string")
  175.         local lpMethods = pHandler.help()
  176.         lpMethodHandler(lpMethods, fileName)
  177.     end
  178.     file = fs.open(fileName, "a")
  179.     file.writeLine("\n\n--------------------\n")
  180.     file.writeLine("All actual documentation is written by the mod developer who made the peripheral, not me. I just displayed it - happy computercrafting")
  181.     file.close()
  182.     print("Documentation roundup for "..pType.." complete")
  183. end
  184.  
  185. term.clear()
  186. term.setCursorPos(1,1)
  187.  
  188. print("This program will create reports documenting any peripheral attached to this computer")
  189. print("The reports can be found on the directory of this computer - type dir into the console to find them")
  190. print("File names are based on the name the peripheral")
  191. print("Documentation was written by the mod developers, not me. I have just tried to display it nicely")
  192. print("Do you wish to continue (y/n)")
  193. local answer = yesOrNo(60)
  194. if answer == true then
  195.     term.clear()
  196.     term.setCursorPos(1,1)
  197.        
  198.     for i,v in pairs(peripheral.getNames()) do
  199.         mainProg(v)
  200.     end
  201.  
  202.     if #newFiles > 0 then
  203.         print("\nThe following files have been generated in this computer's directory\n")
  204.         for i,v in pairs(newFiles) do
  205.             print("\t"..i..": "..v)
  206.         end
  207.         print("\nWould you like to upload these files automatically to pastebin (y/n)")
  208.         local answer = yesOrNo(60)
  209.         if answer == true then
  210.             uploadToPastebin(newFiles)
  211.         end
  212.     else
  213.         print("No peripherals found")
  214.     end
  215.     print("\nHappy computercrafting")
  216. else
  217.     print("Never mind, goodbye")
  218. end
  219. sleep(0.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement