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