Advertisement
HPWebcamAble

[CC][1.1] Config Editor (RE)

Sep 29th, 2014
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 18.01 KB | None | 0 0
  1. --[[
  2. Coded by HPWebcamAble
  3. http://pastebin.com/u/HPWebcamAble
  4.  
  5. WARNING! USE THE INSTALLER, DO NOT DOWNLOAD THIS ALONE! IT WONT WORK BY ITSELF.
  6.  
  7. **********
  8. Installer
  9. **********
  10. In a CC Computer (Advanced only!):
  11. pastebin get eMCqJEBs installer
  12.  
  13. From pastebin site:
  14. http://pastebin.com/eMCqJEBs
  15.  
  16.  
  17. **********
  18. Description
  19. **********
  20. This program creates the config files for my Redstone Engine program.
  21. If you got this by itself, run the installer program. (see above)
  22.  
  23. MORE INFO IN THE REDSTONE ENGINE (RE) PROGRAM DESCRIPTION
  24.  
  25.  
  26. **********
  27. FAQ
  28. **********
  29. Q:What is this?
  30. A:See description
  31.  
  32. Q:How does bundled/rednet cable work?
  33. A:Look it up. Probably best on youtube.
  34.  
  35. Q:The bundled cable isn't attaching to the computer correctly/it doesnt work
  36. A:At this moment, you need to use ComputerCraft 1.5, not 1.6
  37.   See http://pastebin.com/1XY12jQZ for more info
  38.  
  39. Q:Im confused
  40. A:Here's a video! (Leave a comment with questions):
  41.   http://youtu.be/zPvUVptSnro
  42.  
  43. Q:Where do I report bugs?
  44. A:On its Youtube Video (Leave it as a comment):
  45.   http://youtu.be/zPvUVptSnro
  46.  
  47.  
  48. **********
  49. Version
  50. **********
  51. |1.1| <-- This program
  52. Date: 10/4/14
  53.   -Removed a few unused functions
  54.   -Reworked config selection
  55.     *Program starts on config selection
  56.     *You can now delete configs in the program
  57.     *Configs can no longer be made with a space in the name (derp)
  58.   -You can now use 're edit <name>' to open a config from the console
  59.     *If it doesn't exist, it will allow you to create it.
  60.   -Setting sleep time is now much more logical :P
  61.  
  62. |1.0 (Release)|
  63. Date: 9/29/14
  64.   -Makes configs for my RE program!
  65.  
  66.  
  67. **********
  68. Bugs
  69. **********
  70. None! (Yet)
  71.  
  72.  
  73. **********
  74. Notes
  75. **********
  76. This program is almost identical to my config editor for
  77. Advanced Redstone Control
  78.  
  79. ARC allows you to easily control bundled cable outputs from a CC monitor
  80. ]]
  81.  
  82. --Variables--
  83. w,h = term.getSize()
  84. actions = {}
  85. local page = 1
  86. local sConfig = nil
  87. local toprint = nil
  88. args = {...}
  89. local deleteMode = false
  90.  
  91.  
  92. --Functions--
  93. function clear() shell.run("clear") end
  94. function tc(...) term.setTextColor(...) end
  95. function bc(...) term.setBackgroundColor(...) end
  96. function tw(...) term.write(...) end
  97. function cp(...) term.setCursorPos(...) end
  98. function color(textColor,backgroundColor)
  99.   if textColor then
  100.     term.setTextColor(textColor)
  101.   end
  102.   if backgroundColor then
  103.     term.setBackgroundColor(backgroundColor)
  104.   end
  105. end
  106.  
  107. function printC(text,y)
  108.   if not y then
  109.     error("printC: for text '"..text.."', expected y value")
  110.   end
  111.   tLenght = #tostring(text)
  112.   local sStart = math.ceil(w/2-tLenght/2)
  113.   local sEnd = sStart + tLenght
  114.   cp(sStart,y)
  115.   tw(text)
  116.   return sStart,sEnd
  117. end
  118.  
  119. function mainScreen(_text)
  120.   bc(colors.white)
  121.   clear()
  122.   bc(colors.blue)
  123.   cp(1,1)
  124.   tw(string.rep(" ",w))
  125.   tc(colors.orange)
  126.   if _text then
  127.     printC(_text,1)
  128.   else
  129.     printC("Redstone Engine - Config Creator",1)
  130.   end
  131. end
  132.  
  133. function drawMain()
  134.   clear()
  135.   mainScreen()
  136.   bc(colors.white)
  137.   tc(colors.black)
  138.   printC("Config '"..sConfig.."'",2)
  139.   cp(3,2)
  140.   tc(colors.white)
  141.   bc(colors.lime)
  142.   tw("Add Event")
  143.   if actions[1] == "" or actions[1] == nil then
  144.     tc(colors.red)
  145.     bc(colors.white)
  146.     printC("None",3)
  147.   else
  148.     for i = 1, #actions do
  149.       current = i+16*(page-1)
  150.       if i == 17 or not actions[current] then break end
  151.       bc(colors.white)
  152.       tc(colors.black)
  153.       cp(1,i+2)
  154.       tw(tostring(current))
  155.       cp(4,i+2)
  156.       firstChar = string.sub(actions[current],1,1)
  157.       if firstChar == "s" then --sleep
  158.         tempvar = string.sub(actions[current],2)
  159.         if not tonumber(tempvar) then
  160.           tw("sleep ")
  161.           tc(colors.red)
  162.           tw("[Not a number]")
  163.         else
  164.           tw("sleep("..tempvar..")")
  165.         end
  166.       elseif firstChar == "+" then --add color
  167.         tempvar = string.sub(actions[current],2)
  168.         if not tonumber(tempvar) then
  169.           tw("add color ")
  170.           tc(colors.red)
  171.           tw("[Not a number] ("..tempvar..")")
  172.         else
  173.           tw("add color "..tempvar.." (")
  174.           bc(tonumber(tempvar))
  175.           tw(" ")
  176.           bc(colors.white)
  177.           tw(")")          
  178.         end
  179.       elseif firstChar == "-" then --subtract color
  180.         tempvar = string.sub(actions[current],2)
  181.         if not tonumber(tempvar) then
  182.           tw("remove color ")
  183.           tc(colors.red)
  184.           tw("[Not a number] ("..tempvar..")")
  185.         else
  186.           tw("remove color "..tempvar.." (")
  187.           bc(tonumber(tempvar))
  188.           tw(" ")
  189.           bc(colors.white)
  190.           tw(")")          
  191.         end
  192.       elseif firstChar == "=" then --set output to
  193.         tempvar = string.sub(actions[current],2)
  194.         if not tonumber(tempvar) then
  195.           tw("set to ")
  196.           tc(colors.red)
  197.           tw("[Not a number] ("..tempvar..")")
  198.         else
  199.           tw("set to "..tempvar)      
  200.         end
  201.       else
  202.         tc(colors.red)
  203.         tw("[Not a recognized event] ("..actions[current]..")")
  204.       end
  205.       bc(colors.red)
  206.       tc(colors.white)
  207.       cp(w-7,i+2)
  208.       tw("Delete")
  209.     end
  210.   end
  211.   tc(colors.white)
  212.   if page > 1 then
  213.     canScrollUp = true
  214.     bc(colors.lime)
  215.   else
  216.     canScrollUp = false
  217.     bc(colors.lightGray)
  218.   end
  219.   cp(w,3)
  220.   tw("^")
  221.   if page < #actions/16 then
  222.     canScrollDown = true
  223.     bc(colors.lime)
  224.   else
  225.     canScrollDown = false
  226.     bc(colors.lightGray)
  227.   end
  228.   cp(w,h-1)
  229.   tw("v")
  230.   cp(1,h)
  231.   bc(colors.lime)
  232.   tw(string.rep(" ",w))
  233.   tc(colors.white)
  234.   printC("Save",h)
  235. end
  236.  
  237. function scrollRead(x,y,maxX,insertText) --This basically scrolls but not very well
  238.   if insertText then
  239.     cPos = #insertText+1
  240.     cInput = insertText
  241.   else
  242.     cPos = 1
  243.     cInput = ""
  244.   end
  245.   term.setCursorBlink(true)
  246.   while true do
  247.     term.setCursorPos(x,y)
  248.     term.write(string.rep(" ",maxX))
  249.     term.setCursorPos(x,y)
  250.     if string.len(cInput) > maxX-1 then
  251.       term.write(string.sub(cInput,(maxX-1)*-1))
  252.     else
  253.       term.write(cInput)
  254.     end
  255.     if cPos > maxX-1 then
  256.       term.setCursorPos(x+maxX-1,y)
  257.     else  
  258.      term.setCursorPos(x+cPos-1,y)
  259.     end
  260.     event,p1 = os.pullEvent()
  261.         if event == "char" then
  262.             cInput = string.sub(cInput,1,cPos)..p1..string.sub(cInput,cPos+1)
  263.             cPos = cPos+1          
  264.         elseif event == "key" then
  265.       if p1 == keys.enter then
  266.                 break
  267.       elseif p1 == keys.backspace then
  268.                 if cPos > 1 then
  269.                     cInput = string.sub(cInput,1,cPos-2)..string.sub(cInput,cPos)
  270.                     cPos = cPos - 1
  271.                 end
  272.             elseif p1 == keys["end"] then
  273.                 cPos = string.len(cInput)+1
  274.       end    
  275.     end
  276.   end
  277.   term.setCursorBlink(false)
  278.   return cInput
  279. end
  280.  
  281. function loadConfig()
  282.   --Try to load existing config
  283.   f = fs.open("/RED/configs/"..sConfig,"r")
  284.   actions = {}
  285.   local l = 0
  286.   repeat
  287.     l = l + 1
  288.     actions[l] = f.readLine()
  289.   until actions[l] == nil
  290.   f.close()
  291. end
  292.  
  293. function drawSelectConfig()
  294.   clear()
  295.   mainScreen()
  296.   color(colors.black,colors.white)
  297.   printC("Select config:",2)
  298.   configs = {}
  299.   if fs.isDir("/RED/configs") then
  300.     configs = fs.list("/RED/configs")
  301.     for i = 1, #configs do
  302.       cName = configs[i+16*(page-1)]
  303.       if cName == nil or i == 17 then break end
  304.       color(colors.black,colors.white)
  305.       cp(8,i+2)
  306.       if #cName > 30 then
  307.         tw(string.sub(cName,1,27).."...")
  308.       else
  309.         tw(cName)
  310.       end
  311.       cp(40,i+2)
  312.       if deleteMode then
  313.         color(colors.white,colors.red)
  314.         tw("Delete")
  315.       else
  316.         color(colors.white,colors.lightBlue)
  317.         tw("Select")
  318.       end
  319.     end
  320.   else
  321.     color(colors.red,colors.white)
  322.     printC("Missing config directory",4)
  323.   end
  324.   cp(w,3)
  325.   canScrollup = false
  326.   if page > 1 then
  327.     color(colors.white,colors.lime)
  328.     canScrollUp = true
  329.   else
  330.     color(colors.white,colors.lightGray)
  331.   end
  332.   tw("^")
  333.   cp(w,h-1)
  334.   canScrollDown = false
  335.   if page < #configs/15 then
  336.     canScrollDown = true
  337.     color(colors.white,colors.lime)
  338.   else
  339.     color(colors.white,colors.lightGray)
  340.   end
  341.   tw("v")
  342.   color(colors.white,colors.lightBlue)
  343.   cp(w-12,2)
  344.   tw("Toggle Mode")
  345.   cp(2,2)
  346.   color(colors.white,colors.lime)
  347.   tw("New Config")
  348. end
  349.  
  350. function save()
  351.   bc(colors.yellow)
  352.   tc(colors.white)
  353.   cp(1,h)
  354.   tw(string.rep(" ",w))
  355.   printC("Checking config for errors...",h)
  356.   problem = false
  357.   for i = 1, #actions do
  358.     firstChar = string.sub(actions[i],1,1)
  359.     if firstChar == "+" then
  360.       if not tonumber(string.sub(actions[i],2)) then
  361.         problem = i
  362.         break
  363.       end
  364.     elseif firstChar == "-" then
  365.       if not tonumber(string.sub(actions[i],2)) then
  366.         problem = i
  367.         break
  368.       end
  369.     elseif firstChar == "=" then
  370.       if not tonumber(string.sub(actions[i],2)) then
  371.         problem = i
  372.         break
  373.       end
  374.     elseif firstChar == "s" then
  375.       if not tonumber(string.sub(actions[i],2)) then
  376.         problem = i
  377.         break
  378.       end
  379.     else
  380.       problem = i
  381.       break
  382.     end
  383.   end
  384.   if problem then
  385.     bc(colors.red)
  386.     cp(1,h)
  387.     tw(string.rep(" ",w))
  388.     printC("Error found on line "..problem,h)
  389.     sleep(2)
  390.     return false
  391.   else
  392.     f = fs.open("/RED/configs/"..sConfig,"w")
  393.     if f then
  394.       for i = 1, #actions do
  395.         f.write(actions[i].."\n")
  396.       end
  397.       f.close()
  398.       return true
  399.     else
  400.       bc(colors.red)
  401.       cp(1,h)
  402.       tw(string.rep(" ",w))
  403.       printC("Could not open save file",h)
  404.       sleep(2)
  405.       return false
  406.     end
  407.   end
  408. end
  409.  
  410. function selectColor()
  411.   local cColor = 1
  412.   local colorPos = {}
  413.   local lastColor = 32768 --the color black
  414.   local xpos = 17
  415.   local ypos = 5
  416.   cp(xpos,ypos)
  417.   tc(colors.black)
  418.   bc(colors.white)
  419.   tw("[")
  420.   for i = 1, 20 do
  421.     bc(cColor)
  422.     tw(" ")
  423.     colorPos[i] = cColor
  424.     if cColor == lastColor then break end
  425.     cColor = cColor*2
  426.   end
  427.   tc(colors.black)
  428.   bc(colors.white)
  429.   tw("]")
  430.   while true do
  431.     event,p1,p2,p3 = os.pullEvent("mouse_click")
  432.     if p3 == ypos and p2 > xpos and p2 < xpos+17 then
  433.       return colorPos[p2-xpos]
  434.     end
  435.   end
  436. end
  437.  
  438. function addEvent()
  439.   bc(colors.lime)
  440.   tc(colors.white)
  441.   for i = 1, #actions do
  442.     cp(w-7,i+2)
  443.     tw("  ^   ")
  444.     if i == 16 then break end
  445.   end
  446.   cp(3,2)
  447.   tw("Add at end")
  448.   tc(colors.lightGray)
  449.   bc(colors.white)
  450.   cp(1,h)
  451.   tw(string.rep(" ",w))
  452.   printC("Use '^' to insert before",h)
  453.   local addBefore = false
  454.   while true do
  455.     event,p1,p2,p3 = os.pullEvent("mouse_click")
  456.     if p2 >= 3 and p2 <= 12 and p3 == 2 then --at end
  457.       break
  458.     elseif p3 > 2 and p3 < h and p2 >= w-7 and p2 <= w-3 then
  459.       if p3-2 <= #actions-16*(page-1) then --before one
  460.         addBefore = (p3-2)+16*(page-1)
  461.         break
  462.       end
  463.     end
  464.   end
  465.   mainScreen("Add Event...")
  466.   tc(colors.black)
  467.   bc(colors.white)
  468.   printC("Select an event:",2)
  469.   bc(colors.lime)
  470.   tc(colors.white)
  471.   printC("Add color",4)
  472.   printC("Remove color",6)
  473.   printC("Set output",8)
  474.   printC("Sleep",10)
  475.   local action = nil
  476.   while true do
  477.     event,p1,p2,p3 = os.pullEvent("mouse_click")
  478.     if p3 == 4 then --add
  479.       mainScreen("Add Event...")
  480.       tc(colors.black)
  481.       bc(colors.white)
  482.       printC("Select color to add:",2)
  483.       _color = selectColor()
  484.       if addBefore then
  485.         table.insert(actions,addBefore,"+".._color)
  486.       else
  487.         table.insert(actions,"+".._color)
  488.       end
  489.       break
  490.     elseif p3 == 6 then -- remove
  491.       mainScreen("Add Event...")
  492.       tc(colors.black)
  493.       bc(colors.white)
  494.       printC("Select color to remove:",2)
  495.       _color = selectColor()
  496.       if addBefore then
  497.         table.insert(actions,addBefore,"-".._color)
  498.       else
  499.         table.insert(actions,"-".._color)
  500.       end
  501.       break
  502.     elseif p3 == 8 then --set
  503.       mainScreen("Add Event...")
  504.       tc(colors.black)
  505.       bc(colors.white)
  506.       printC("Enter number to set output to:",2)
  507.       tc(colors.white)
  508.       bc(colors.black)
  509.       repeat
  510.         input = scrollRead(20,5,10)
  511.       until input~="" and tonumber(input)
  512.       if addBefore then
  513.         table.insert(actions,addBefore,"="..tonumber(input))
  514.       else
  515.         table.insert(actions,"="..tonumber(input))
  516.       end
  517.       break
  518.     elseif p3 == 10 then --sleep
  519.       local sTime = 0
  520.       mainScreen("Add Event...")
  521.       color(colors.black,colors.white)
  522.       printC("Enter sleep time:",3)
  523.       input = ""
  524.       while true do
  525.         color(colors.white,colors.black)
  526.         input = scrollRead(22,5,7,input)
  527.         if input == lastInput then break else lastInput = nil end
  528.         if not input or not tonumber(input) then
  529.           color(nil,colors.white)
  530.           cp(1,6)
  531.           tw(string.rep(" ",w))
  532.           cp(1,7)
  533.           tw(string.rep(" ",w))
  534.           color(colors.red,colors.white)
  535.           printC("Must be a number",6)
  536.         elseif tonumber(input) > 10 then
  537.           color(nil,colors.white)
  538.           cp(1,6)
  539.           tw(string.rep(" ",w))
  540.           color(colors.red,colors.white)
  541.           printC("Sleep times above 10 aren't recomended",6)
  542.           color(colors.lightGray)
  543.           printC("Hit enter again to confirm",7)
  544.           lastInput = input
  545.         else
  546.           break
  547.         end
  548.       end
  549.       sTime = tonumber(input)
  550.       if addBefore then
  551.         table.insert(actions,addBefore,"s"..sTime)
  552.       else
  553.         table.insert(actions,"s"..sTime)
  554.       end
  555.       break
  556.     end
  557.   end  
  558. end
  559.  
  560. function newConfig(name)
  561.   color(colors.orange,colors.blue)
  562.   cp(10,6)
  563.   tw(string.rep(" ",30))
  564.   cp(10,7) tw(" ")
  565.   cp(10,8) tw(" ")
  566.   cp(10,9) tw(" ")
  567.   cp(10,10)
  568.   tw(string.rep(" ",30))
  569.   cp(39,7) tw(" ")
  570.   cp(39,8) tw(" ")
  571.   cp(39,9) tw(" ")
  572.   printC("New Config",6)
  573.   color(colors.black,colors.white)
  574.   printC("Enter name:",7)
  575.   color(colors.lightGray,colors.blue)
  576.   printC("Leave blank to cancel",10)
  577.   useExisting = nil
  578.   if name then
  579.     input = tostring(name)
  580.   else
  581.     input = ""
  582.   end
  583.   while true do
  584.     color(colors.white,colors.black)
  585.     input = scrollRead(12,8,26,input)
  586.     if input == "" then
  587.       return false
  588.     elseif string.find(input," ") then
  589.       color(nil,colors.white)
  590.       cp(11,9)
  591.       tw(string.rep(" ",28))
  592.       color(colors.red,colors.white)
  593.       printC("Cannot contain spaces",9)
  594.     elseif string.find(input,"/") then
  595.       color(nil,colors.white)
  596.       cp(11,9)
  597.       tw(string.rep(" ",28))
  598.       color(colors.red,colors.white)
  599.       printC("Cannot contain '/'",9)
  600.     elseif fs.exists("/RED/configs/"..input) then
  601.       if useExisting == input then
  602.         return input
  603.       else
  604.         color(nil,colors.white)
  605.         cp(11,9)
  606.         tw(string.rep(" ",28))
  607.         color(colors.red,colors.white)
  608.         printC("Use 'enter' to edit",9)
  609.         useExisting = input
  610.       end
  611.     else
  612.       return input
  613.     end
  614.   end
  615. end
  616.  
  617. function main() --Having this as a function allows error catching
  618.   if args[1] then --Check the given args
  619.     if fs.exists("/RED/configs/"..args[1]) then
  620.       os.queueEvent("load_config")
  621.     else
  622.       os.queueEvent("create_config")
  623.     end
  624.   end
  625.  
  626.   continue = true
  627.   drawSelectConfig()
  628.   while continue do --have the user select a config (or create one)
  629.     event,p1,p2,p3 = os.pullEvent()
  630.     if event == "mouse_click" then
  631.       if p2 <= w-1 and p2 >= w-12 and p3 == 2 then --toggle mode
  632.         deleteMode = not deleteMode
  633.         drawSelectConfig()
  634.       elseif p2 >= 2 and p2 <= 12 and p3 == 2 then --new config
  635.         success = newConfig()
  636.         if success then
  637.           sConfig = success
  638.           break
  639.         else
  640.           drawSelectConfig()
  641.         end
  642.       elseif p3 > 2 and p3 < h then --delete or select config
  643.         if p3 <= #configs-16*(page-1)+2 then
  644.           if deleteMode then
  645.             fs.delete("/RED/configs/"..configs[p3-2+16*(page-1)])
  646.             drawSelectConfig()
  647.           else
  648.             sConfig = configs[p3-2+16*(page-1)]
  649.             loadConfig()
  650.             break
  651.           end
  652.         end
  653.       elseif p3 == 3 and p2 == w then --page up
  654.         if canScrollUp then
  655.           page = page-1
  656.         end
  657.       elseif p3 == h-1 and p2 == w then --page down
  658.         if canScrollDown then
  659.           page = page+1
  660.         end
  661.       end
  662.     elseif event == "load_config" then
  663.       sConfig = args[1]
  664.       loadConfig()
  665.       break
  666.     elseif event == "create_config" then
  667.       success = newConfig(args[1])
  668.         if success then
  669.           sConfig = success
  670.           break
  671.         else
  672.           drawSelectConfig()
  673.         end
  674.     end
  675.   end
  676.  
  677.   continue = true
  678.   page = 1
  679.   while continue do --edit the config
  680.     drawMain()
  681.     while true do
  682.       event,p1,p2,p3 = os.pullEvent("mouse_click")
  683.       if p2 >= 3 and p2 <= 11 and p3 == 2 then --Add
  684.         addEvent()
  685.         drawMain()
  686.       elseif  p2 >= w-7 and p2 <= w-2 and p3 > 2 and p3 < h then --Delete
  687.         if p3-2 <= #actions+16*(page-1) then
  688.           table.remove(actions,(p3-2)+16*(page-1))
  689.           drawMain()
  690.         end
  691.       elseif p3 == 3 and p2 == w and canScrollUp then --Scroll up
  692.         page = page-1
  693.         drawMain()
  694.       elseif p3 == h-1 and p2 == w and canScrollDown then --Scroll down
  695.         page = page+1
  696.         drawMain()
  697.       elseif p3 == h then --save
  698.         if save() then
  699.           continue = false
  700.           break
  701.         else
  702.           drawMain()
  703.         end
  704.       end
  705.     end
  706.   end
  707. end
  708.  
  709.  
  710. --Program--
  711. state,err = pcall(function() main() end)
  712.  
  713. if err then
  714.   if string.find(err,"Terminated") then
  715.     term.setCursorBlink(false)
  716.     cp(1,1)
  717.     bc(colors.red)
  718.     tc(colors.white)
  719.     tw(string.rep(" ",w))
  720.     printC("Terminated - Click to clear screen",1)
  721.     os.pullEvent()
  722.     bc(colors.black)
  723.     clear()
  724.     print("No changes made")
  725.   elseif string.find(err,"end") then
  726.     tc(colors.black)
  727.     print("Ended")
  728.   else
  729.     tc(colors.black)
  730.     print("CRITICAL ERROR:")
  731.     print(err)
  732.   end
  733. else
  734.   bc(colors.black)
  735.   tc(colors.white)
  736.   clear()
  737.   tc(colors.lime)
  738.   print("Config Saved")
  739. end
  740.  
  741. --Cleanup
  742. if f then
  743.   f.close()
  744. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement