Advertisement
HPWebcamAble

[CC][1.1.1] Config Editor (ARC)

Aug 15th, 2014
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.97 KB | None | 0 0
  1. --[[
  2. WARNING! USE THE INSTALLER, DO NOT DOWNLOAD THIS ALONE! IT WONT WORK BY ITSELF.
  3.  
  4. **********
  5. Installer
  6. **********
  7. In a CC Computer (Advanced only!):
  8. pastebin get pVyr7LjW installer
  9.  
  10. From pastebin site:
  11. http://pastebin.com/pVyr7LjW
  12.  
  13. **********
  14. Description
  15. **********
  16. This program ceates the config file for my Advanced Redstone Control (ARC) program. This
  17. program really isn't helpful if used by itself (But you can use it alone if you want I guess).
  18. To download it correctly, use the installer
  19.  
  20. **********
  21. FAQ
  22. **********
  23. Q:What is this?
  24. A:See description
  25.  
  26. Q:Im confused
  27. A:http://youtu.be/o_mzKx-7dS0
  28.  
  29. Q:Auto-Update?
  30. A:Not yet, and not planned
  31.  
  32. Q:Where do I report bugs?
  33. A:On it's Youtube video
  34.   http://youtu.be/o_mzKx-7dS0
  35.  
  36. **********
  37. Version
  38. **********
  39. |1.1.1| <-- This program
  40. Date: 9/12/2014
  41.   Bug fixes:
  42.   -Error message would be put over text when loading bad configs
  43.  
  44. |1.1|
  45. Date: ??? (Forgot to record...)
  46.   Changes:
  47.   -When editing buttons, the old name is automatically put into the dialog box
  48.   -Colors are displayed in order by value (finally :P)
  49.   -Now displayes colors by multiplying by 2, not an internal table
  50.  
  51.  
  52. |1.0 (Release)|
  53. Date: 8/15/2014
  54. Date: mm/dd/yyyy
  55.   -Makes configs for my ARC program!
  56.   Bugs:
  57.   -None so far
  58.   -Altho side note this program is HUGE
  59.     *Will simplify soon. Maybe.
  60. ]]
  61.  
  62. --Variables--
  63. local version = "1.1.1"
  64. local cableSide
  65. local monitorName
  66. w,h = term.getSize()
  67. local problem = true
  68. local lastColor = 32768 --the color black
  69.  
  70. --Functions--
  71. function clear() shell.run("clear") end
  72. function tc(...) term.setTextColor(tonumber(...)) end
  73. function bc(...) term.setBackgroundColor(...) end
  74. function tw(...) term.write(...) end
  75. function cp(...) term.setCursorPos(...) end
  76.  
  77. function printC(text,y)
  78.   if not y then
  79.     error("printC:No Y value specified")
  80.   end
  81.   tLenght = #tostring(text)
  82.   local sStart = math.ceil(w/2-tLenght/2)
  83.   local sEnd = sStart + tLenght
  84.   cp(sStart,y)
  85.   tw(text)
  86.   return sStart,sEnd
  87. end
  88.  
  89. function isColor(color)
  90.   for a,b in pairs(colors) do
  91.     if b == tonumber(color) then
  92.       return true
  93.     end
  94.   end
  95.   return false
  96. end
  97.  
  98. function isSide(side)
  99.   for a,b in pairs(rs.getSides()) do
  100.     if b == side then
  101.       return true
  102.     end
  103.   end
  104.   return false
  105. end
  106.  
  107. function mainScreen(_text)
  108.   bc(colors.white)
  109.   clear()
  110.   bc(colors.blue)
  111.   cp(1,1)
  112.   tw(string.rep(" ",w))
  113.   tc(colors.orange)
  114.   if _text then
  115.     printC(_text,1)
  116.   else
  117.     printC("Advanced Redstone Control - Config Creator",1)
  118.   end
  119. end
  120.  
  121. function drawMain()
  122.   problem = false
  123.   clear()
  124.   mainScreen()
  125.   tc(colors.black)
  126.   --monitor
  127.   cp(2,3)
  128.   bc(colors.white)
  129.   tw("Monitor: ")
  130.   tc(colors.white)
  131.   if not monitorName then
  132.     monitorName = "Not set"
  133.   end
  134.   if peripheral.getType(monitorName) ~= "monitor" then
  135.     tc(colors.orange)
  136.   else
  137.     tc(colors.lime)
  138.   end
  139.   if monitorName == "Not set" then
  140.     problem = true
  141.     tc(colors.red)
  142.     tw(monitorName)
  143.   else
  144.     tw(monitorName)
  145.   end
  146.   tw("  ")
  147.   bc(colors.lightBlue)
  148.   tc(colors.white)
  149.   tw("Edit")
  150.   --cable
  151.   cp(2,5)
  152.   bc(colors.white)
  153.   tc(colors.black)
  154.   tw("Bundled Cable: ")
  155.   if not isSide(cableSide) or not cableSide then
  156.     problem = true
  157.     tc(colors.red)
  158.   else
  159.     tc(colors.lime)
  160.   end
  161.   if cableSide then
  162.     tw(cableSide)
  163.   else
  164.     tw("Not set")
  165.   end
  166.   tw("  ")
  167.   tc(colors.white)
  168.   bc(colors.lightBlue)
  169.   tw("Edit")
  170.   --buttons
  171.   cp(2,7)
  172.   tc(colors.black)
  173.   bc(colors.white)
  174.   tw("Buttons:  ")
  175.   bc(colors.lightBlue)
  176.   tc(colors.white)
  177.   print("Edit")
  178.   bc(colors.white)
  179.   if #buttons < 1 then
  180.     cp(2,8)
  181.     tc(colors.red)
  182.     tw("None")
  183.     problem = true
  184.   else
  185.     for i = 1, #buttons do
  186.       bc(colors.white)
  187.       tw(" ")
  188.       if i > h-9 then
  189.         bc(colors.white)
  190.         tc(colors.black)
  191.         tw("Plus "..#buttons+1-i.." more")
  192.         break
  193.       end
  194.       if not isColor(buttons[i]["color"]) then
  195.         bc(colors.red)
  196.         tc(colors.white)
  197.         problem = true
  198.       else
  199.         bColor = buttons[i]["color"]
  200.         if bColor == colors.white then
  201.           bc(colors.lightGray)
  202.           tc(colors.white)
  203.         else
  204.           bc(colors.white)
  205.           tc(bColor)
  206.         end
  207.       end
  208.       print(buttons[i]["name"])
  209.     end
  210.   end
  211.   if problem then
  212.     bc(colors.red)
  213.   else
  214.     bc(colors.lime)
  215.   end
  216.   tc(colors.white)
  217.   cp(1,h)
  218.   tw(string.rep(" ",w))
  219.   printC("Save and Exit",h)
  220. end
  221.  
  222. function scrollRead(x,y,maxX,insertText) --This basically scrolls but not very well
  223.   if insertText then cInput = insertText cPos = #insertText+1 else cPos = 1 cInput = "" end
  224.   term.setCursorBlink(true)
  225.   while true do
  226.     term.setCursorPos(x,y)
  227.     term.write(string.rep(" ",maxX))
  228.     term.setCursorPos(x,y)
  229.     if string.len(cInput) > maxX-1 then
  230.       term.write(string.sub(cInput,(maxX-1)*-1))
  231.     else
  232.       term.write(cInput)
  233.     end
  234.     if cPos > maxX-1 then
  235.       term.setCursorPos(x+maxX-1,y)
  236.     else  
  237.      term.setCursorPos(x+cPos-1,y)
  238.     end
  239.     event,p1 = os.pullEvent()
  240.         if event == "char" then
  241.             cInput = string.sub(cInput,1,cPos)..p1..string.sub(cInput,cPos+1)
  242.             cPos = cPos+1          
  243.         elseif event == "key" then
  244.       if p1 == keys.enter then
  245.                 break
  246.       elseif p1 == keys.backspace then
  247.                 if cPos > 1 then
  248.                     cInput = string.sub(cInput,1,cPos-2)..string.sub(cInput,cPos)
  249.                     cPos = cPos - 1
  250.                 end
  251.             elseif p1 == keys["end"] then
  252.                 cPos = string.len(cInput)+1
  253.       end    
  254.     end
  255.   end
  256.   term.setCursorBlink(false)
  257.   return cInput
  258. end
  259.  
  260. function loadConfig()
  261.   --Try to load existing config
  262.   f = fs.open("/ARC/config","r")
  263.   local tLines = {}
  264.   local l = 0
  265.   repeat
  266.     l = l + 1
  267.     tLines[l] = f.readLine()
  268.   until tLines[l] == nil
  269.   if tLines[1] ~= "This is the Advanced Redstone Control config file" or tLines[2] ~= "Do not change anything in it!" or tLines[3] ~= "Use the provided config editor" then
  270.     f.close()
  271.     return "File has incorrect formatting"
  272.   end
  273.   --Monitor
  274.   monitorName = tLines[4]
  275.   --Bundled cable
  276.   if not isSide(tLines[5]) then
  277.     f.close()
  278.     return "Invalid side"
  279.   end
  280.   cableSide = tLines[5]
  281.   --Load the buttons
  282.   local equalPos
  283.   if not tonumber(tLines[6]) then
  284.     f.close()
  285.     return "Incorrect formating"
  286.   end
  287.   buttons = {}
  288.   for i = 1, tonumber(tLines[6]) do
  289.     c = i+6
  290.     equalPos = string.find(tLines[c],"=")
  291.     if not equalPos then
  292.       f.close()
  293.       return "Problem with line "..c
  294.     end
  295.     bName = string.sub(tLines[c],1,equalPos-1)
  296.     bColor = tonumber(string.sub(tLines[c],equalPos+1))
  297.     buttons[i] = {}
  298.     buttons[i]["name"] = bName
  299.     buttons[i]["color"] = bColor
  300.     buttons[i]["state"] = false
  301.   end
  302.   f.close()
  303.   return false
  304. end
  305.  
  306. function editMonitor()
  307.   clear()
  308.   mainScreen("Edit monitor...")
  309.   tc(colors.black)
  310.   bc(colors.white)
  311.   printC("Current name:",3)
  312.   if monitorName then
  313.     printC(monitorName,4)
  314.   else
  315.     printC("Not set")
  316.   end
  317.   printC("Enter name/side:",6)
  318.   printC("Monitor touching computer - use side",h-1)
  319.   printC("Monitor connected by network cable - use name",h)
  320.   tStart,tEnd = printC(string.rep(" ",20),7)
  321.   while true do
  322.     bc(colors.black)
  323.     tc(colors.white)
  324.     input = scrollRead(tStart,7,20)
  325.     if string.find(input," ") then
  326.       tc(colors.red)
  327.       bc(colors.white)
  328.       printC("Cannot contain spaces",8)
  329.     elseif input ~= "" then
  330.       break
  331.     end
  332.   end
  333.   monitorName = input
  334. end
  335.  
  336. function editCable()
  337.   clear()
  338.   mainScreen("Edit cable side...")
  339.   tc(colors.black)
  340.   bc(colors.white)
  341.   printC("Current side:",3)
  342.   if cableSide then
  343.     printC(cableSide,4)
  344.   else
  345.     printC("Not set",4)
  346.   end
  347.   printC("Enter side:",6)
  348.   tStart,tEnd = printC(string.rep(" ",10),7)
  349.   while true do
  350.     bc(colors.black)
  351.     tc(colors.white)
  352.     input = scrollRead(tStart,7,10)
  353.     if not isSide(input) then
  354.       tc(colors.red)
  355.       bc(colors.white)
  356.       printC("Not a side",8)
  357.       printC("top, bottom, left, right, back, front",9)
  358.     else
  359.       break
  360.     end
  361.   end
  362.   cableSide = input
  363. end
  364.  
  365. function redrawButtonEdit()
  366.   clear()
  367.   mainScreen("Edit buttons...")
  368.   cp(2,2)
  369.   tc(colors.white)
  370.   bc(colors.lightBlue)
  371.   tw("Add")
  372.   bc(colors.white)
  373.   tw(" ")
  374.   bc(colors.lime)
  375.   tw("Done")
  376.   tc(colors.black)
  377.   bc(colors.white)
  378.   printC("Buttons:",2)
  379.   if #buttons > 0  then
  380.     if #buttons >= h-3 then
  381.       times = h-3
  382.       bc(colors.white)
  383.       tc(colors.red)
  384.       printC("Max buttons reached",h)
  385.     else
  386.       times = #buttons
  387.     end
  388.     for i = 1, times do
  389.       cp(2,i+2)
  390.       if not isColor(buttons[i]["color"]) then
  391.         bc(colors.red)
  392.         tc(colors.white)
  393.       else
  394.         bColor = buttons[i]["color"]
  395.         if bColor == colors.white then
  396.           bc(colors.lightGray)
  397.           tc(colors.white)
  398.         else
  399.           bc(colors.white)
  400.           tc(bColor)
  401.         end
  402.       end
  403.       tw(buttons[i]["name"])
  404.       cp(w-12,i+2)
  405.       tc(colors.white)
  406.       bc(colors.lightBlue)
  407.       tw("Edit")
  408.       bc(colors.white)
  409.       tw(" ")
  410.       bc(colors.red)
  411.       tw("Delete")
  412.     end
  413.   else
  414.     tc(colors.red)
  415.     bc(colors.white)
  416.     printC("None",3)
  417.   end
  418. end
  419.  
  420. function editButtons()
  421.   continue = true
  422.   while continue do
  423.     redrawButtonEdit()
  424.     while true do
  425.       event,p1,p2,p3 = os.pullEvent("mouse_click")
  426.       if p3 == 2 then
  427.         if p2 >= 2 and p2 <= 4 then --add
  428.           if #buttons < h-3 then
  429.             bc(colors.black)
  430.             tc(colors.white)
  431.             repeat
  432.               input = scrollRead(2,#buttons+3,15)
  433.             until input ~= ""
  434.             cp(2,#buttons+3)
  435.             local cColor = 1
  436.             local colorPos = {}
  437.             for i = 1, 20 do
  438.               bc(cColor)
  439.               tw(" ")
  440.               colorPos[i] = cColor
  441.               if cColor == lastColor then break end
  442.               cColor = cColor*2
  443.             end
  444.             continue2 = true
  445.             while continue2 do
  446.               event,par1,par2,par3 = os.pullEvent("mouse_click")
  447.               if par3 == #buttons+3 then
  448.                 if par2 >= 2 and par2 <= 18 then
  449.                   buttons[#buttons+1] = {}
  450.                   buttons[#buttons]["name"] = input
  451.                   buttons[#buttons]["color"] = colorPos[par2 - 1]
  452.                   continue2 = false
  453.                   redrawButtonEdit()
  454.                 end
  455.               end
  456.             end
  457.           end
  458.         elseif p2 >= 6 and p2 <= 9 then --Done
  459.           continue = false
  460.           break
  461.         end
  462.       elseif p3 > 2 and #buttons > 0 then
  463.         if p3-2 <= #buttons then
  464.           if p2 >= w-13 and p2 <= w-10 then --Edit
  465.             bc(colors.black)
  466.             tc(colors.white)
  467.             repeat
  468.               input = scrollRead(2,p3,15,buttons[p3-2]["name"])
  469.             until input ~= ""
  470.             cp(2,p3)
  471.             local cColor = 1
  472.             local colorPos = {}
  473.             for i = 1, 20 do
  474.               bc(cColor)
  475.               tw(" ")
  476.               colorPos[i] = cColor
  477.               if cColor == lastColor then break end
  478.               cColor = cColor*2
  479.             end
  480.             continue2 = true
  481.             while continue2 do
  482.               event,par1,par2,par3 = os.pullEvent("mouse_click")
  483.               if par3 == p3 then
  484.                 if par2 >= 2 and par2 <= 18 then
  485.                   buttons[p3-2]["name"] = input
  486.                   buttons[p3-2]["color"] = colorPos[par2 - 1]
  487.                   continue2 = false
  488.                   redrawButtonEdit()
  489.                 end
  490.               end
  491.             end
  492.           elseif p2 >= w-8 and p2 <= w-2 then --delete
  493.             --For now thers no confirmation
  494.             table.remove(buttons,p3-2)
  495.             redrawButtonEdit()
  496.           end  
  497.         end
  498.       end
  499.     end
  500.   end
  501. end
  502.  
  503. function save()
  504.   cp(1,h)
  505.   bc(colors.orange)
  506.   tw(string.rep(" ",w))
  507.   tc(colors.white)
  508.   printC("Saving...",h)
  509.   f = fs.open("/arc/config","w")
  510.   if f then
  511.     f.write("This is the Advanced Redstone Control config file\nDo not change anything in it!\nUse the provided config editor\n")
  512.     f.write(monitorName.."\n")
  513.     f.write(cableSide.."\n")
  514.     f.write(#buttons.."\n")
  515.     for i = 1, #buttons do
  516.       f.write(buttons[i]["name"].."="..buttons[i]["color"].."\n")
  517.     end
  518.     cp(1,h)
  519.     bc(colors.lime)
  520.     tw(string.rep(" ",w))
  521.     tc(colors.white)
  522.     printC("Saved to /arc/config",h)
  523.     sleep(2)
  524.   else
  525.     cp(1,1)
  526.     bc(colors.orange)
  527.     tw(string.rep(" ",w))
  528.     tc(colors.white)
  529.     printC("Cannot open config file",1)
  530.     sleep(1)
  531.   end
  532.   f.close()
  533. end
  534.  
  535. function main() --Having this as a function allows error catching
  536.   pos = {}
  537.   mainScreen()
  538.   bc(colors.lime)
  539.   tc(colors.black)
  540.   pos["New"] = {}
  541.   pos["New"]["start"],pos["New"]["end"] = printC("Create New...",6)
  542.   fExists = fs.exists("/ARC/config")
  543.   if fExists then
  544.     bc(colors.lime)
  545.   else
  546.     bc(colors.lightGray)
  547.   end
  548.   pos["Load"] = {}
  549.   pos["Load"]["start"],pos["Load"]["end"] = printC("Load Existing",8)
  550.   tc(colors.lightGray)
  551.   bc(colors.white)
  552.   printC("Use ctrl+t to end the program",h-1)
  553.   printC("However, changes are not saved",h)
  554.   while true do
  555.     event,p1,p2,p3 = os.pullEvent("mouse_click")
  556.     if p3 == 6 and p2 >= pos["New"]["start"] and p2 <= pos["New"]["end"] then
  557.       monitorSide = nil
  558.       cableSide = nil
  559.       buttons = {}
  560.       break
  561.     elseif p3 == 8 and p2 >= pos["Load"]["start"] and p2 <= pos["New"]["end"] and fExists then
  562.       tError = loadConfig()
  563.       if tError then
  564.         bc(colors.white)
  565.         tc(colors.red)
  566.         printC(tError,h-3)
  567.         printC("Cannot load",h-2)
  568.       else
  569.         break    
  570.       end
  571.     end
  572.   end
  573.   continue3 = true
  574.   while continue3 do
  575.     drawMain()
  576.     while true do
  577.       event,p1,p2,p3 = os.pullEvent("mouse_click")
  578.       if p3 == 3 then --Edit Monitor name
  579.         editMonitor()
  580.         break
  581.       elseif p3 == 5 then --Edit bundled cable side
  582.         editCable()
  583.         break
  584.       elseif p3 == 7 then --Edit buttons
  585.         editButtons()
  586.         break
  587.       elseif p3 == h and not problem then
  588.         save()
  589.         continue3 = false
  590.         break
  591.       end
  592.     end
  593.   end
  594. end
  595.  
  596.  
  597. --Program--
  598. state,err = pcall(function() main() end)
  599.  
  600. if err then
  601.   if string.find(err,"Terminated") then
  602.     term.setCursorBlink(false)
  603.     cp(1,1)
  604.     bc(colors.red)
  605.     tc(colors.white)
  606.     tw(string.rep(" ",w))
  607.     printC("Terminated",1)
  608.     sleep(2)
  609.     printC("Click to clear screen",1)
  610.     os.pullEvent()
  611.     bc(colors.black)
  612.     clear()
  613.   elseif string.find(err,"end") then
  614.     tc(colors.black)
  615.     print("Ended")
  616.   else
  617.     tc(colors.black)
  618.     print("CRITICAL ERROR:")
  619.     print(err)
  620.     for i = 1, #buttons do
  621.       print(i..":")
  622.       print("  Name:"..buttons[i]["name"])
  623.       print("  Color:"..buttons[i]["color"])
  624.     end
  625.   end
  626. else
  627.   bc(colors.black)
  628.   tc(colors.white)
  629.   clear()
  630.   print("Sucesss. Type 'rc run' to run Advanced Redstone Control program")
  631. end
  632.  
  633. --Cleanup
  634. if f then
  635.   f.close()
  636. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement