Advertisement
ndfjay

CCatch

Feb 18th, 2013
728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.27 KB | None | 0 0
  1. --[[
  2. Author: NDFJay
  3. Title: CCatch
  4. Type: API
  5. Version: 1.0
  6. Created: 15/02/2013
  7. Last Update: 15/02/2013
  8.  
  9. API inspired by "TheOriginalBIT" through his crash handling tutorial as shown below.
  10. http://www.computercraft.info/forums2/index.php?/topic/10450-handling-errors-creating-controlled-errors-creating-a-blue-screen-of-death-bsod/
  11.  
  12. License:
  13. COPYRIGHT NOTICE
  14. Copyright © 2013 JayJay Swarts known online as NDFJay
  15.  
  16. NDFJay, herein refered to as "The author" allows you to copy,
  17. distribute and adapt the work under the following conditions:
  18.  
  19. Attribution — This license MUST be present at the top of the file.
  20. You must attribute the work in the manner specified by the author
  21.                                         (but not in any way that suggests that they endorse you or your use of the work).
  22.                                         If using parts of this code you must attribute the author above code snippet
  23.  
  24. Creative Commons — You may use this work for commercial and non-commercial purposes alike.
  25.  
  26. Share Alike — If you alter, transform, or build upon this work, you may distribute the resulting work
  27.                                         only under the same or similar license to this one.
  28.  
  29. Any of the above conditions can be waived if you get permission from the copyright holder.
  30.  
  31. This software is provided by the author "AS IS". As such the author does not take any responsibility for
  32. any damage caused to your systems, physical or virtual, especially from misuse or modification of code.
  33. ]]--
  34.  
  35.  
  36. function cwrite(path, text)
  37.     local file = assert(io.open(path, "w"))
  38.     file:write(text)
  39.     file:close()
  40. end
  41.  
  42.  
  43. function cgetText(path)
  44.     if fs.exists(path) then
  45.         local file = assert(io.open(path, "r"))
  46.         return file:read("*a")
  47.     end
  48.     return ""
  49. end
  50.  
  51.  
  52. function append(path, text)
  53.     local _text = cgetText(path)
  54.     cwrite(path, text.."\n".._text)
  55. end
  56.  
  57.  
  58. A_crash = function(func,align,bcol,tcol)
  59. local w,h = term.getSize()
  60. local ok, err = pcall(func)
  61. term.setBackgroundColor(bcol)
  62. term.setTextColor(tcol)
  63. term.clear()
  64. local tmpErr = {
  65. "Oh No! We've Crashed!";
  66. " ";
  67. "Error Code:";
  68. err;
  69. " ";
  70. " ";
  71. "Please report this code to the dev if this is";
  72. "no fault of your own";
  73. " ";
  74. " ";
  75. "Please do refrain from modifying the base code!"
  76. }
  77.     if not ok then
  78.         if align == "left" then
  79.             for i,v in pairs(tmpErr) do
  80.                 term.setCursorPos(1,i+1)
  81.                 print(v)
  82.             end
  83.                 elseif align == "center" then
  84.                 for i,v in pairs(tmpErr) do
  85.                     term.setCursorPos(math.floor(w-string.len(v))/2, i+1)
  86.                     print(v)
  87.                 end
  88.                 term.setBackgroundColor(colors.black)
  89.                 term.setCursorPos(1,math.floor(h))
  90.                 term.clearLine()
  91.         end
  92. term.setBackgroundColor(colors.black)
  93. term.setCursorPos(1,math.floor(h))
  94. term.clearLine()
  95.     end
  96. end
  97.  
  98.  
  99. A_custCrash = function(func,align,bcol,tcol,t)
  100. local w,h = term.getSize()
  101. local ok, err = pcall(func)
  102. term.setBackgroundColor(bcol)
  103. term.setTextColor(tcol)
  104. term.clear()
  105. if not ok then
  106.     if align == "left" then
  107.         for i,v in pairs(t) do
  108.             term.setCursorPos(1,i+1)
  109.             print(v)
  110.             if string.find(t[i],":error:") then
  111.                 term.setCursorPos(1, i+1)
  112.                 print(v)
  113.                 term.setCursorPos(string.find(t[i],":error:")-1, i+1)
  114.                 write(err)
  115.             end
  116.         end
  117.         write(err)
  118.             elseif align == "center" then
  119.             for i,v in pairs(t) do
  120.                 term.setCursorPos(math.floor(w-#v)/2, i+1)
  121.                 print(v)
  122.                 if string.find(t[i],":error:") then
  123.                     term.setCursorPos(math.floor(w-string.find(t[i],":error:")-#err+1)/2, i+1)
  124.                     print(v)
  125.                     term.setCursorPos(math.floor(w+string.find(t[i],":error:")-#err-2)/2, i+1)
  126.                     write(err)
  127.                 end
  128.             end
  129.         end
  130.     end
  131. term.setBackgroundColor(colors.black)
  132. term.setCursorPos(1,math.floor(h))
  133. term.clearLine()
  134. end
  135.  
  136.  
  137. A_resolveCrash = function(func,resolvefunc)
  138. local ok, err = pcall(func)
  139. if not ok then
  140. resolvefunc()
  141. end
  142. end
  143.  
  144.  
  145. A_saveCrash = function(func,align,bcol,tcol,t,x,y,sDir,sPath)
  146. local w,h = term.getSize()
  147. local drop = 1
  148. local ok, err = pcall(func)
  149. local success = {
  150. 'Error Log Saved to (/'..sDir.."/"..sPath..')';
  151. "Please show the log to the Dev"
  152. }
  153. term.setBackgroundColor(bcol)
  154. term.setTextColor(tcol)
  155. term.clear()
  156. if not ok then
  157.     if align == "left" then
  158.         for i,v in pairs(t) do
  159.             term.setCursorPos(1,i+drop)
  160.             print(v)
  161.             if string.find(t[i],":error:") then
  162.                 term.setCursorPos(1, i+1)
  163.                 print(v)
  164.                 term.setCursorPos(string.find(t[i],":error:")-1, i+1)
  165.                 write(err)
  166.             end
  167.         end
  168.             elseif align == "center" then
  169.             for i,v in pairs(t) do
  170.                 term.setCursorPos(math.floor(w-#v)/2, i+drop)
  171.                 print(v)
  172.                 if string.find(t[i],":error:") then
  173.                     term.setCursorPos(math.floor(w-string.find(t[i],":error:")-#err+1)/2, i+1)
  174.                     print(v)
  175.                     term.setCursorPos(math.floor(w+string.find(t[i],":error:")-#err-2)/2, i+1)
  176.                     write(err)
  177.                 end
  178.             end
  179.         end
  180.         if not fs.exists(sDir) then
  181.         fs.makeDir(sDir)
  182.         end
  183.         if not fs.exists(sDir.."/"..sPath) then
  184.         fwrite(sDir.."/"..sPath," ")
  185.         end
  186.         append(sDir.."/"..sPath, string.rep("=",40))
  187.         append(sDir.."/"..sPath, " ")
  188.         append(sDir.."/"..sPath, tostring(err))
  189.         append(sDir.."/"..sPath, " ")
  190.         append(sDir.."/"..sPath, string.rep("=",40))
  191.         if align == "left" then
  192.                     term.setCursorPos(x,y)
  193.                     print(success[1])
  194.                     term.setCursorPos(x,y+2)
  195.                     print(success[2])
  196.                     elseif align == "center" then
  197.                     term.setCursorPos(math.floor(w-#success[1])/2,y)
  198.                     print(success[1])
  199.                     term.setCursorPos(math.floor(w-#success[2])/2,y+2)
  200.                     print(success[2])
  201.             end
  202.         term.setBackgroundColour(colors.black)
  203.         term.setTextColor(1)
  204.         term.setCursorPos(1,math.floor(h))
  205.         term.clearLine()
  206.         return true
  207.     end
  208. end
  209.  
  210.  
  211. A_c_Crash = function(func,argc,argv,align,bcol,tcol,t)
  212. local ok, err = pcall(func, argc, argv)
  213. local w,h = term.getSize()
  214. term.setBackgroundColor(bcol)
  215. term.setTextColor(tcol)
  216. term.clear()
  217. if not ok then
  218.     if align == "left" then
  219.         for i,v in pairs(t) do
  220.             term.setCursorPos(1,i+1)
  221.             print(v)
  222.             if string.find(t[i],":error:") then
  223.                 term.setCursorPos(1, i+1)
  224.                 print(v)
  225.                 term.setCursorPos(string.find(t[i],":error:")-1, i+1)
  226.                 write(err)
  227.             end
  228.         end
  229.         write(err)
  230.             elseif align == "center" then
  231.             for i,v in pairs(t) do
  232.                 term.setCursorPos(math.floor(w-#v)/2, i+1)
  233.                 print(v)
  234.                 if string.find(t[i],":error:") then
  235.                     term.setCursorPos(math.floor(w-string.find(t[i],":error:")-#err+1)/2, i+1)
  236.                     print(v)
  237.                     term.setCursorPos(math.floor(w+string.find(t[i],":error:")-#err-2)/2, i+1)
  238.                     write(err)
  239.                 end
  240.             end
  241.         end
  242.     end
  243. term.setBackgroundColor(colors.black)
  244. term.setCursorPos(1,math.floor(h))
  245. term.clearLine()
  246. end
  247.  
  248.  
  249. A_save_c_Crash = function(func,align,bcol,tcol,t,x,y,sDir,sPath,argc,argv)
  250. local w,h = term.getSize()
  251. local drop = 1
  252. local ok, err = pcall(func, argc, argv)
  253. local success = {
  254. 'Error Log Saved to (/'..sDir.."/"..sPath..')';
  255. "Please show the log to the Dev"
  256. }
  257. term.setBackgroundColor(bcol)
  258. term.setTextColor(tcol)
  259. term.clear()
  260. if not ok then
  261.     if align == "left" then
  262.         for i,v in pairs(t) do
  263.             term.setCursorPos(1,i+drop)
  264.             print(v)
  265.             if string.find(t[i],":error:") then
  266.                 term.setCursorPos(1, i+1)
  267.                 print(v)
  268.                 term.setCursorPos(string.find(t[i],":error:")-1, i+1)
  269.                 write(err)
  270.             end
  271.         end
  272.             elseif align == "center" then
  273.             for i,v in pairs(t) do
  274.                 term.setCursorPos(math.floor(w-#v)/2, i+drop)
  275.                 print(v)
  276.                 if string.find(t[i],":error:") then
  277.                     term.setCursorPos(math.floor(w-string.find(t[i],":error:")-#err+1)/2, i+1)
  278.                     print(v)
  279.                     term.setCursorPos(math.floor(w+string.find(t[i],":error:")-#err-2)/2, i+1)
  280.                     write(err)
  281.                 end
  282.             end
  283.         end
  284.         if not fs.exists(sDir) then
  285.         fs.makeDir(sDir)
  286.         end
  287.         if not fs.exists(sDir.."/"..sPath) then
  288.         fwrite(sDir.."/"..sPath," ")
  289.         end
  290.         append(sDir.."/"..sPath, string.rep("=",40))
  291.         append(sDir.."/"..sPath, " ")
  292.         append(sDir.."/"..sPath, tostring(err))
  293.         append(sDir.."/"..sPath, " ")
  294.         append(sDir.."/"..sPath, string.rep("=",40))
  295.             if align == "left" then
  296.                     term.setCursorPos(x,y)
  297.                     print(success[1])
  298.                     term.setCursorPos(x,y+2)
  299.                     print(success[2])
  300.                     elseif align == "center" then
  301.                     term.setCursorPos(math.floor(w-#success[1])/2,y)
  302.                     print(success[1])
  303.                     term.setCursorPos(math.floor(w-#success[2])/2,y+2)
  304.                     print(success[2])
  305.             end
  306.         term.setBackgroundColour(colors.black)
  307.         term.setTextColor(1)
  308.         term.setCursorPos(1,math.floor(h))
  309.         term.clearLine()
  310.         return true
  311.     end
  312. end
  313.  
  314.  
  315. B_crash = function(func,align)
  316. local w,h = term.getSize()
  317. local ok, err = pcall(func)
  318. term.clear()
  319. local tmpErr = {
  320. "Oh No! We've Crashed!";
  321. " ";
  322. "Error Code:";
  323. err;
  324. " ";
  325. " ";
  326. "Please report this code to the dev if this is";
  327. "no fault of your own";
  328. " ";
  329. " ";
  330. "Please do refrain from modifying the base code!"
  331. }
  332.     if not ok then
  333.         if align == "left" then
  334.             for i,v in pairs(tmpErr) do
  335.                 term.setCursorPos(1,i+1)
  336.                 print(v)
  337.             end
  338.                 elseif align == "center" then
  339.                 for i,v in pairs(tmpErr) do
  340.                     term.setCursorPos(math.floor(w-string.len(v))/2, i+1)
  341.                     print(v)
  342.                 end
  343.                 term.setCursorPos(1,math.floor(h))
  344.                 term.clearLine()
  345.         end
  346. term.setCursorPos(1,math.floor(h))
  347. term.clearLine()
  348.     end
  349. end
  350.  
  351.  
  352. B_custCrash = function(func,align,t)
  353. local w,h = term.getSize()
  354. local ok, err = pcall(func)
  355. term.clear()
  356. if not ok then
  357.     if align == "left" then
  358.         for i,v in pairs(t) do
  359.             term.setCursorPos(1,i+1)
  360.             print(v)
  361.             if string.find(t[i],":error:") then
  362.                 term.setCursorPos(1, i+1)
  363.                 print(v)
  364.                 term.setCursorPos(string.find(t[i],":error:")-1, i+1)
  365.                 write(err)
  366.             end
  367.         end
  368.         write(err)
  369.             elseif align == "center" then
  370.             for i,v in pairs(t) do
  371.                 term.setCursorPos(math.floor(w-#v)/2, i+1)
  372.                 print(v)
  373.                 if string.find(t[i],":error:") then
  374.                     term.setCursorPos(math.floor(w-string.find(t[i],":error:")-#err+1)/2, i+1)
  375.                     print(v)
  376.                     term.setCursorPos(math.floor(w+string.find(t[i],":error:")-#err-2)/2, i+1)
  377.                     write(err)
  378.                 end
  379.             end
  380.         end
  381.     end
  382. term.setCursorPos(1,math.floor(h))
  383. term.clearLine()
  384. end
  385.  
  386.  
  387. B_resolveCrash = function(func,resolvefunc)
  388. local ok, err = pcall(func)
  389. if not ok then
  390. resolvefunc()
  391. end
  392. end
  393.  
  394.  
  395. B_saveCrash = function(func,align,t,x,y,sDir,sPath)
  396. local w,h = term.getSize()
  397. local drop = 1
  398. local ok, err = pcall(func)
  399. local success = {
  400. 'Error Log Saved to (/'..sDir.."/"..sPath..')';
  401. "Please show the log to the Dev"
  402. }
  403. term.clear()
  404. if not ok then
  405.     if align == "left" then
  406.         for i,v in pairs(t) do
  407.             term.setCursorPos(1,i+drop)
  408.             print(v)
  409.             if string.find(t[i],":error:") then
  410.                 term.setCursorPos(1, i+1)
  411.                 print(v)
  412.                 term.setCursorPos(string.find(t[i],":error:")-1, i+1)
  413.                 write(err)
  414.             end
  415.         end
  416.             elseif align == "center" then
  417.             for i,v in pairs(t) do
  418.                 term.setCursorPos(math.floor(w-#v)/2, i+drop)
  419.                 print(v)
  420.                 if string.find(t[i],":error:") then
  421.                     term.setCursorPos(math.floor(w-string.find(t[i],":error:")-#err+1)/2, i+1)
  422.                     print(v)
  423.                     term.setCursorPos(math.floor(w+string.find(t[i],":error:")-#err-2)/2, i+1)
  424.                     write(err)
  425.                 end
  426.             end
  427.         end
  428.         if not fs.exists(sDir) then
  429.         fs.makeDir(sDir)
  430.         end
  431.         if not fs.exists(sDir.."/"..sPath) then
  432.         fwrite(sDir.."/"..sPath," ")
  433.         end
  434.         append(sDir.."/"..sPath, string.rep("=",40))
  435.         append(sDir.."/"..sPath, " ")
  436.         append(sDir.."/"..sPath, tostring(err))
  437.         append(sDir.."/"..sPath, " ")
  438.         append(sDir.."/"..sPath, string.rep("=",40))
  439.             if align == "left" then
  440.                     term.setCursorPos(x,y)
  441.                     print(success[1])
  442.                     term.setCursorPos(x,y+2)
  443.                     print(success[2])
  444.                     elseif align == "center" then
  445.                     term.setCursorPos(math.floor(w-#success[1])/2,y)
  446.                     print(success[1])
  447.                     term.setCursorPos(math.floor(w-#success[2])/2,y+2)
  448.                     print(success[2])
  449.             end
  450.         term.setCursorPos(1,math.floor(h))
  451.         term.clearLine()
  452.         return true
  453.     end
  454. end
  455.  
  456.  
  457. B_c_Crash = function(func,argc,argv,align,t)
  458. local ok, err = pcall(func, argc, argv)
  459. local w,h = term.getSize()
  460. term.setBackgroundColor(bcol)
  461. term.setTextColor(tcol)
  462. term.clear()
  463. if not ok then
  464.     if align == "left" then
  465.         for i,v in pairs(t) do
  466.             term.setCursorPos(1,i+1)
  467.             print(v)
  468.             if string.find(t[i],":error:") then
  469.                 term.setCursorPos(1, i+1)
  470.                 print(v)
  471.                 term.setCursorPos(string.find(t[i],":error:")-1, i+1)
  472.                 write(err)
  473.             end
  474.         end
  475.         write(err)
  476.             elseif align == "center" then
  477.             for i,v in pairs(t) do
  478.                 term.setCursorPos(math.floor(w-#v)/2, i+1)
  479.                 print(v)
  480.                 if string.find(t[i],":error:") then
  481.                     term.setCursorPos(math.floor(w-string.find(t[i],":error:")-#err+1)/2, i+1)
  482.                     print(v)
  483.                     term.setCursorPos(math.floor(w+string.find(t[i],":error:")-#err-2)/2, i+1)
  484.                     write(err)
  485.                 end
  486.             end
  487.         end
  488.     end
  489. term.setBackgroundColor(colors.black)
  490. term.setCursorPos(1,math.floor(h))
  491. term.clearLine()
  492. end
  493.  
  494.  
  495. B_save_c_Crash = function(func,align,t,x,y,sDir,sPath,argc,argv)
  496. local w,h = term.getSize()
  497. local drop = 1
  498. local ok, err = pcall(func, argc, argv)
  499. local success = {
  500. 'Error Log Saved to (/'..sDir.."/"..sPath..')';
  501. "Please show the log to the Dev"
  502. }
  503. term.clear()
  504. if not ok then
  505.     if align == "left" then
  506.         for i,v in pairs(t) do
  507.             term.setCursorPos(1,i+drop)
  508.             print(v)
  509.             if string.find(t[i],":error:") then
  510.                 term.setCursorPos(1, i+1)
  511.                 print(v)
  512.                 term.setCursorPos(string.find(t[i],":error:")-1, i+1)
  513.                 write(err)
  514.             end
  515.         end
  516.             elseif align == "center" then
  517.             for i,v in pairs(t) do
  518.                 term.setCursorPos(math.floor(w-#v)/2, i+drop)
  519.                 print(v)
  520.                 if string.find(t[i],":error:") then
  521.                     term.setCursorPos(math.floor(w-string.find(t[i],":error:")-#err+1)/2, i+1)
  522.                     print(v)
  523.                     term.setCursorPos(math.floor(w+string.find(t[i],":error:")-#err-2)/2, i+1)
  524.                     write(err)
  525.                 end
  526.             end
  527.         end
  528.         if not fs.exists(sDir) then
  529.         fs.makeDir(sDir)
  530.         end
  531.         if not fs.exists(sDir.."/"..sPath) then
  532.         fwrite(sDir.."/"..sPath," ")
  533.         end
  534.         append(sDir.."/"..sPath, string.rep("=",40))
  535.         append(sDir.."/"..sPath, " ")
  536.         append(sDir.."/"..sPath, tostring(err))
  537.         append(sDir.."/"..sPath, " ")
  538.         append(sDir.."/"..sPath, string.rep("=",40))
  539.         if align == "left" then
  540.                     term.setCursorPos(x,y)
  541.                     print(success[1])
  542.                     term.setCursorPos(x,y+2)
  543.                     print(success[2])
  544.                     elseif align == "center" then
  545.                     term.setCursorPos(math.floor(w-#success[1])/2,y)
  546.                     print(success[1])
  547.                     term.setCursorPos(math.floor(w-#success[2])/2,y+2)
  548.                     print(success[2])
  549.             end
  550.         term.setCursorPos(1,math.floor(h))
  551.         term.clearLine()
  552.         return true
  553.     end
  554. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement