Advertisement
margeoComputerCraft

[ccraft]FacSetup

Dec 4th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.11 KB | None | 0 0
  1. --  Title: FacSetup
  2. --
  3. --  Version: 1.0 - useless
  4. --
  5. --  Description: Installs Factionet (downloads the
  6. --  main Factionet script). After I got rid of the
  7. --  image files and just put them in the main program
  8. --  this actually became kind of useless... Oh well,
  9. --  who doesn't like useless programs?!
  10. --
  11. --  Author: margeobur
  12.  
  13. -- 'global' variables
  14. local rootDir = shell.dir()
  15. local installPath = rootDir .. "/Factionet"
  16. local stringInPathbox
  17. local done = false
  18.  
  19. -- functions
  20.  
  21. local function eWrite(pstring, xCoord, yCoord) --saves so many lines!
  22.     term.setCursorPos(xCoord,yCoord)
  23.     term.write(pstring)
  24. end
  25.  
  26. local function displayHomeScreen()
  27.     term.setBackgroundColour(colours.white)
  28.     term.clear()
  29.     term.setTextColour(colours.green)
  30.     eWrite("Factionet Setup",18,6)
  31.  
  32.     for i=12, 16 do
  33.         paintutils.drawLine(14,i,37,i,colours.lightGrey)
  34.     end
  35.     term.setTextColour(colours.black)
  36.     paintutils.drawLine(16,13,35,13,colours.white)
  37.     eWrite("Install",23,13)
  38.     paintutils.drawLine(16,15,35,15,colours.white)
  39.     eWrite("Update",23,15)
  40.  
  41.     paintutils.drawPixel(51,1,colours.green)
  42.     term.setTextColour(colours.red)
  43.     eWrite("X",51,1)
  44. end
  45.  
  46. local function displayInstallScreen()
  47.     term.setBackgroundColour(colours.white)
  48.     term.setTextColour(colours.black)
  49.     term.clear()
  50.  
  51.     paintutils.drawLine(50,1,51,1,colours.green)
  52.     term.setTextColour(colours.red)
  53.     eWrite("<-",50,1)
  54.  
  55.     term.setBackgroundColour(colours.white)
  56.     term.setTextColour(colours.black)
  57.     eWrite("Factionet will be installed in",12,6)
  58.     eWrite("   the following directory:   ",12,7)
  59.     for i=12,16 do
  60.         paintutils.drawLine(9,i,42,i,colours.lightGrey)
  61.     end
  62.     paintutils.drawLine(10,13,41,13,colours.white)
  63.     if string.len(installPath) > 32 then
  64.         local temp = string.sub(installPath,1,32)
  65.         eWrite(temp,10,13)
  66.     else
  67.         eWrite(installPath,10,13)
  68.     end
  69.  
  70.     paintutils.drawLine(16,15,35,15,colours.white)
  71.     eWrite("Install",23,15)
  72. end
  73.  
  74. local function editInstallPath()
  75.     local event, param1, param2, param3 = nil,nil,nil,nil
  76.     local currentPos = 0
  77.     local displayString = ""
  78.     if string.len(installPath) > 32 then
  79.         displayString = string.sub(installPath,-32,-1)
  80.         currentPos = 41
  81.     else
  82.         displayString = installPath
  83.         currentPos = string.len(installPath) + 10
  84.     end
  85.     eWrite(displayString,10,13)
  86.     term.setCursorPos(currentPos,13)
  87.     term.setCursorBlink(true)
  88.  
  89.     while true do
  90.         event, param1, param2, param3 = os.pullEvent()
  91.         if event == "mouse_click" then
  92.             if param2 > 9 and param2 < 42 and param3 == 13 then
  93.  
  94.             else
  95.                 break
  96.             end
  97.  
  98.         elseif event == "key" then
  99.             if param1 == keys.enter then
  100.                 break
  101.             elseif param1 == keys.backspace then
  102.                 if string.len(installPath) < 33 then
  103.                     if currentPos < 11 then
  104.                         -- do nothing because we can't backspace
  105.                     else
  106.                         local temp = string.sub(installPath, 1 , -2)
  107.                         installPath = temp
  108.                         eWrite(" ",currentPos - 1,13)
  109.                         currentPos = currentPos - 1
  110.                         term.setCursorPos(currentPos,13)
  111.                     end
  112.                 end
  113.             end
  114.         elseif event == "char" then
  115.             if currentPos < 31 + 11 then
  116.                 eWrite(param1,currentPos,13)
  117.                 currentPos = currentPos + 1
  118.                 installPath = installPath .. param1
  119.             end
  120.         end
  121.     end
  122.  
  123.     term.setCursorBlink(false)
  124. end
  125.  
  126. local function askBox(string1,string2,string3,string4)
  127.     for i=10,16 do
  128.         paintutils.drawLine(14,i,37,i,colours.green)
  129.     end
  130.     term.setTextColour(colours.magenta)
  131.     term.setBackgroundColour(colours.green)
  132.     eWrite(string1,15,11)
  133.     eWrite(string2,15,12)
  134.     eWrite(string3,15,13)
  135.     eWrite(string4,15,14)
  136.  
  137.     term.setTextColour(colours.black)
  138.     term.setBackgroundColour(colours.white)
  139.     paintutils.drawLine(16,15,25,15,colours.white)
  140.     eWrite("No",20,15)
  141.     paintutils.drawLine(27,15,35,15,colours.white)
  142.     eWrite("Yes",30,15)
  143.  
  144.     while true do
  145.         local event, button, xCoord, yCoord = os.pullEvent("mouse_click")
  146.         if xCoord > 15 and xCoord < 26 and yCoord == 15 then
  147.             return false
  148.         elseif xCoord > 26 and xCoord < 36 and yCoord == 15 then
  149.             return true
  150.         end
  151.     end
  152.  
  153.     return true
  154. end
  155.  
  156. -- install: downloads the main script and creates
  157. -- the image files
  158.  
  159. local function install()
  160.     if not fs.isDir(installPath) then
  161.         if not askBox("That filepath does not","exist, should I create","    it?","") then
  162.             return false
  163.         else
  164.             fs.makeDir(installPath)
  165.         end
  166.     end
  167.  
  168.     if not http then
  169.         for i=9,16 do
  170.             paintutils.drawLine(14,i,37,i,colours.green)
  171.         end
  172.         term.setTextColour(colours.magenta)
  173.         term.setBackgroundColour(colours.green)
  174.         eWrite("The http API is not",16,10)
  175.         eWrite("enabled on this map/",16,11)
  176.         eWrite("server. It needs to be",15,12)
  177.         eWrite("enabled to connect to",15,13)
  178.         eWrite("pastebin.",21,14)
  179.  
  180.         paintutils.drawLine(15,15,35,15,colours.white)
  181.         term.setTextColour(colours.black)
  182.         term.setBackgroundColour(colours.white)
  183.         eWrite("ok",26,15)
  184.  
  185.         while true do
  186.             local event, button, xCoord, yCoord = os.pullEvent("mouse_click")
  187.             if xCoord > 15 and xCoord < 36 and yCoord == 15 then
  188.                 return false
  189.             end
  190.         end
  191.     end
  192.  
  193.     term.clear()
  194.     for i=9,11 do
  195.         paintutils.drawLine(11,i,40,i,colours.lightGrey)
  196.     end
  197.     term.setTextColour(colours.black)
  198.     term.setBackgroundColour(colours.lightGrey)
  199.     eWrite("connecting to pastebin...",13,10)
  200.  
  201.     local response = http.get(
  202.         "http://pastebin.com/raw.php?i="..textutils.urlEncode( "8Uhx0G2C" )
  203.         )
  204.  
  205.     if response then
  206.         eWrite("installing main script...",13,10)
  207.         sleep(1)
  208.  
  209.         local sResponse = response.readAll()
  210.         response.close()
  211.  
  212.         local mainFile = fs.open( installPath .. "/Factionet" , "w" )
  213.         mainFile.write( sResponse )
  214.         mainFile.close()
  215.  
  216.     else
  217.         for i=10,15 do
  218.             paintutils.drawLine(14,i,37,i,colours.lightGrey)
  219.         end
  220.         term.setTextColour(colours.black)
  221.         term.setBackgroundColour(colours.lightGrey)
  222.         eWrite("Could not connect to",16,11)
  223.         eWrite("pastebin",16,12)
  224.  
  225.         paintutils.drawLine(15,14,35,14,colours.white)
  226.         term.setTextColour(colours.black)
  227.         term.setBackgroundColour(colours.white)
  228.         eWrite("ok",26,14)
  229.  
  230.         while true do
  231.             local event, button, xCoord, yCoord = os.pullEvent("mouse_click")
  232.             if xCoord > 15 and xCoord < 36 and yCoord == 14 then
  233.                 return false
  234.             end
  235.         end
  236.         return false
  237.     end
  238.  
  239.     if not fs.isDir(installPath .. "/sessions") then
  240.         fs.makeDir(installPath .. "/sessions")
  241.     end
  242.  
  243.     term.setBackgroundColour(colours.white)
  244.     term.clear()
  245.  
  246.     for i=9,15 do
  247.         paintutils.drawLine(14,i,37,i,colours.lightGrey)
  248.     end
  249.     term.setTextColour(colours.black)
  250.     term.setBackgroundColour(colours.lightGrey)
  251.     eWrite("Success. Thank you for",15,10)
  252.     eWrite("choosing to install",16,11)
  253.     eWrite("Factionet, enjoy!",17,12)
  254.  
  255.     paintutils.drawLine(15,14,35,14,colours.white)
  256.     term.setTextColour(colours.black)
  257.     term.setBackgroundColour(colours.white)
  258.     eWrite("ok",26,14)
  259.  
  260.     while true do
  261.         local event, button, xCoord, yCoord = os.pullEvent("mouse_click")
  262.         if xCoord > 15 and xCoord < 36 and yCoord == 14 then
  263.             return true
  264.         end
  265.     end
  266.     return 0
  267. end
  268.  
  269. local function displayUpdateScreen()
  270.     term.setBackgroundColour(colours.white)
  271.     term.setTextColour(colours.black)
  272.     term.clear()
  273.  
  274.     paintutils.drawLine(50,1,51,1,colours.green)
  275.     term.setTextColour(colours.red)
  276.     eWrite("<-",50,1)
  277.  
  278.     term.setBackgroundColour(colours.white)
  279.     term.setTextColour(colours.black)
  280.     eWrite("Directory of your Factionet",12,6)
  281.     eWrite("   installation:   ",15,7)
  282.     for i=12,16 do
  283.         paintutils.drawLine(9,i,42,i,colours.lightGrey)
  284.     end
  285.     paintutils.drawLine(10,13,41,13,colours.white)
  286.     if string.len(installPath) > 32 then
  287.         local temp = string.sub(installPath,1,32)
  288.         eWrite(temp,10,13)
  289.     else
  290.         eWrite(installPath,10,13)
  291.     end
  292.  
  293.     paintutils.drawLine(16,15,35,15,colours.white)
  294.     eWrite("Update",23,15)
  295. end
  296.  
  297. local function Update()
  298.     if not fs.isDir(installPath) then
  299.         if not askBox("That filepath does not","exist, would you like","to create it and","install Factionet there?") then
  300.             return false
  301.         else
  302.             fs.makeDir(installPath)
  303.             return 2
  304.         end
  305.     elseif not fs.exists(installPath .. "/Factionet") or not fs.exists(installPath .. "/sessions") then
  306.         if not askBox("  Factionet is not"," installed properly at"," this location. Do you","want to install it there?") then
  307.             return false
  308.         else
  309.             return 2
  310.         end
  311.     end
  312.     if not http then
  313.         for i=9,16 do
  314.             paintutils.drawLine(14,i,37,i,colours.green)
  315.         end
  316.         term.setTextColour(colours.magenta)
  317.         term.setBackgroundColour(colours.green)
  318.         eWrite("The http API is not",16,10)
  319.         eWrite("enabled on this map/",16,11)
  320.         eWrite("server. It needs to be",15,12)
  321.         eWrite("enabled to connect to",15,13)
  322.         eWrite("pastebin.",21,14)
  323.  
  324.         paintutils.drawLine(15,15,35,15,colours.white)
  325.         term.setTextColour(colours.black)
  326.         term.setBackgroundColour(colours.white)
  327.         eWrite("ok",26,15)
  328.  
  329.         while true do
  330.             local event, button, xCoord, yCoord = os.pullEvent("mouse_click")
  331.             if xCoord > 15 and xCoord < 36 and yCoord == 15 then
  332.                 return false
  333.             end
  334.         end
  335.     end
  336.  
  337.     --otherwise...
  338.  
  339.     term.clear()
  340.     for i=9,11 do
  341.         paintutils.drawLine(11,i,40,i,colours.lightGrey)
  342.     end
  343.     term.setTextColour(colours.black)
  344.     term.setBackgroundColour(colours.lightGrey)
  345.     eWrite("connecting to pastebin...",13,10)
  346.  
  347.     local response = http.get(
  348.         "http://pastebin.com/raw.php?i="..textutils.urlEncode( "8Uhx0G2C" )
  349.         )
  350.  
  351.     if response then
  352.         eWrite("replacing...",13,10)
  353.  
  354.         local sResponse = response.readAll()
  355.         response.close()
  356.  
  357.         local mainFile = fs.open( installPath .. "/Factionet" , "w" )
  358.         mainFile.write( sResponse )
  359.         mainFile.close()
  360.  
  361.     else
  362.         for i=10,15 do
  363.             paintutils.drawLine(14,i,37,i,colours.lightGrey)
  364.         end
  365.         term.setTextColour(colours.black)
  366.         term.setBackgroundColour(colours.lightGrey)
  367.         eWrite("Could not connect to",16,11)
  368.         eWrite("pastebin",16,12)
  369.  
  370.         paintutils.drawLine(15,14,35,14,colours.white)
  371.         term.setTextColour(colours.black)
  372.         term.setBackgroundColour(colours.white)
  373.         eWrite("ok",26,14)
  374.  
  375.         while true do
  376.             local event, button, xCoord, yCoord = os.pullEvent("mouse_click")
  377.             if xCoord > 15 and xCoord < 36 and yCoord == 14 then
  378.                 return false
  379.             end
  380.         end
  381.         return false
  382.     end
  383.  
  384.     term.setBackgroundColour(colours.white)
  385.     term.clear()
  386.  
  387.     for i=9,15 do
  388.         paintutils.drawLine(14,i,37,i,colours.lightGrey)
  389.     end
  390.     term.setTextColour(colours.black)
  391.     term.setBackgroundColour(colours.lightGrey)
  392.     eWrite("Success. Factionet is",15,10)
  393.     eWrite("up to date. :)",16,11)
  394.     eWrite("",17,12)
  395.  
  396.     paintutils.drawLine(15,14,35,14,colours.white)
  397.     term.setTextColour(colours.black)
  398.     term.setBackgroundColour(colours.white)
  399.     eWrite("ok",26,14)
  400.  
  401.     while true do
  402.         local event, button, xCoord, yCoord = os.pullEvent("mouse_click")
  403.         if xCoord > 15 and xCoord < 36 and yCoord == 14 then
  404.             return 1
  405.         end
  406.     end
  407.  
  408.     return 0
  409. end
  410.  
  411. -- functionality begins here
  412. local event, param1, param2, param3 = "",0,0,0
  413.  
  414. while true do
  415.     displayHomeScreen()
  416.     event, param1, param2, param3 = os.pullEvent()
  417.  
  418.     if event == "mouse_click" then
  419.         if param2 == 51 and param3 == 1 then
  420.             term.setBackgroundColour(colours.black)
  421.             term.clear()
  422.             term.setCursorPos(1,1)
  423.             return 0
  424.         elseif param2 > 15 and param2 < 36 and param3 == 13 then
  425.             while true do
  426.                 displayInstallScreen()
  427.                 event, param1, param2, param3 = os.pullEvent()
  428.                 if event == "mouse_click" then
  429.                     if param2 > 49 and param3 == 1 then
  430.                         break
  431.                     elseif param2 > 9 and param2 < 42 and param3 == 13 then
  432.                         editInstallPath()
  433.                     elseif param2 > 15 and param2 < 36 and param3 == 15 then
  434.                         if install() then
  435.                             done = true
  436.                             break
  437.                         end
  438.                     end
  439.                 end
  440.             end
  441.         elseif param2 > 15 and param2 < 36 and param3 == 15 then
  442.             while true do
  443.                 displayUpdateScreen()
  444.                 event, param1, param2, param3 = os.pullEvent()
  445.                 if event == "mouse_click" then
  446.                     if param2 > 49 and param3 == 1 then
  447.                         break
  448.                     elseif param2 > 9 and param2 < 42 and param3 == 13 then
  449.                         editInstallPath()
  450.                     elseif param2 > 15 and param2 < 36 and param3 == 15 then
  451.                         local temp = Update()
  452.                         if temp == 1 then
  453.                             done = true
  454.                             break
  455.                         elseif temp == 2 then
  456.                             displayInstallScreen()
  457.                             if install() then
  458.                                 done = true
  459.                                 break
  460.                             end
  461.                         end
  462.                     end
  463.                 end
  464.             end
  465.         end
  466.     end
  467.  
  468.     if done then
  469.         break
  470.     end
  471. end
  472.  
  473. term.setBackgroundColour(colours.black)
  474. term.clear()
  475. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement