Advertisement
es01

aa_Text to Monitor with Pastebin

Dec 9th, 2014
76,882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 25.31 KB | None | 0 0
  1. -- Coded by Egnarts --
  2.  
  3. -- Changelog:
  4. -- v5.2: some bugfixes
  5. -- v5.1: some bugfixes
  6. -- v5.0: Its now possible to change the background color of the Monitor
  7. -- v4.5.1: Fixed bug in simpleCenter function
  8. -- v4.5: Waiting some Seconds before updating to prevent Request-Bans with Pastebin.
  9. -- v4.4: Fixed bug where the text was not shown when computer was unloaded and pastebin unavailable
  10. -- v4.3: Showing Pagenumbers is now possible. Look at config.
  11. -- v4.2: Text Mode: Paged now available
  12. -- v4.1: shorter time until text is shown, some bugfixes, fixed error in updater
  13. -- v4.0: language fixes, fixed monitor detection, fixed update routine
  14. -- v3.9: fix in counting with runtimeCounter, error handling, fixed textSize
  15. -- v3.8: fixed a bug where the program never updates itself
  16. -- v3.7: some new prints and bugfixes
  17. -- v3.6: added branches
  18. -- v3.5: bugfixes
  19. -- v3.4: added a config-check, and two new settings and their implementation
  20. -- v3.3: some security updates, other standard refreshSpeed
  21.  
  22. -- Don't Change:
  23. version = "5.2"
  24. branche = "release"
  25. resetInputSide = "back"
  26.  
  27. -- download the Paste if pastebin.com is reachable
  28. function updateFile(pastebinCode, fileName)
  29.   local r = math.random(7)-1
  30.   print(string.format("Waiting %s Seconds before updating.", r))
  31.   sleep(r)  
  32.   http.request("http://www.pastebin.com")
  33.   local requesting = true
  34.   while requesting do
  35.     event, url = os.pullEvent()
  36.     if event == "http_success" then
  37.       if fs.exists(fileName) then
  38.         shell.run(string.format("rm %s", fileName))
  39.       end
  40.       shell.run(string.format("pastebin get %s %s", pastebinCode, fileName))
  41.       requesting = false
  42.       return true
  43.     elseif event == "http_failure" then
  44.       print("Connecting to pastebin.com ... Failed. Try again later.")
  45.       print("File not updated")
  46.       requesting = false
  47.       return false
  48.     end
  49.   end
  50. end -- updateFile()        
  51.  
  52. -- open file and insert into list, line per line
  53. function readFile (fileName, sText)
  54.   local f = fs.open(fileName, "r")
  55.   if f == nil then
  56.     error(string.format("File with name >%s< not found!", fileName))
  57.     return
  58.   end
  59.   local text = f.readLine()
  60.   sText = {}
  61.     while text ~= nil do
  62.       table.insert(sText, text)
  63.       text = f.readLine()
  64.     end
  65.   f.close()
  66.   return sText
  67. end -- readFile ()
  68.  
  69. local function printCenter(mon, t, titleColor, subtitleColor, textColor)
  70.     local w, h = mon.getSize()
  71.     local y = 0 -- math.floor((h / 2) - (#t / 2)) - 1
  72.     local options = 0
  73.     for i, line in ipairs(t) do
  74.       if string.find(line, "<title>") ~= nil then
  75.         options = options +1
  76.         mon.setTextColor(titleColor)
  77.       elseif string.find(line, "</title>") ~= nil then
  78.         options = options +1
  79.         mon.setTextColor(textColor)
  80.       elseif string.find(line, "<subtitle>") ~= nil then
  81.         options = options +1
  82.         mon.setTextColor(subtitleColor)
  83.       elseif string.find(line, "</subtitle>") ~= nil then
  84.         options = options +1
  85.         mon.setTextColor(textColor)
  86.       else
  87.         local x = math.floor((w / 2) - (#line / 2) + 1)
  88.         mon.setCursorPos(x, y + i - options)
  89.         mon.write(line)
  90.       end
  91.     end -- for i, line in ipairs(t) do
  92. end -- printCenter(mon, t)
  93.  
  94. local function printSimpleCenter(mon, t, textColor)
  95.   mon.setTextColor(textColor)
  96.   local w, h = mon.getSize()
  97.   local y = math.floor((h / 2) - (#t / 2)) -- - 1
  98.   for i, line in ipairs(t) do
  99.         local x = math.floor((w / 2) - (#line / 2)) + 1
  100.         mon.setCursorPos(x, y + i)
  101.         mon.write(line)
  102.   end
  103. end
  104.  
  105. local function printLeft(mon, t, titleColor, subtitleColor, textColor)
  106.   local options = 0
  107.   for i, line in ipairs(t) do
  108.     if string.find(line, "<title>") ~= nil then
  109.       options = options +1
  110.       mon.setTextColor(titleColor)
  111.     elseif string.find(line, "</title>") ~= nil then
  112.       options = options +1
  113.       mon.setTextColor(textColor)
  114.     elseif string.find(line, "<subtitle>") ~= nil then
  115.       options = options +1
  116.       mon.setTextColor(subtitleColor)
  117.     elseif string.find(line, "</subtitle>") ~= nil then
  118.       options = options +1
  119.       mon.setTextColor(textColor)
  120.     else
  121.       mon.setCursorPos(1, i - options)
  122.       line = " "..line
  123.       mon.write(line)
  124.     end
  125.   end -- for i, line in ipairs(t) do
  126. end -- printLeft(mon, t)
  127.  
  128. local function printColumn(mon, t, titleColor, subtitleColor, textColor)
  129.   local options = 0
  130.   local w, h = mon.getSize()
  131.   local max = 3    -- min distance of columns
  132.   local x = 1
  133.   local y = 1
  134.   local static = 0 -- written lines per column
  135.   local space = 0  -- place between | and text
  136.   local table = "" -- | symbol 2. column and so on
  137.   for i, line in ipairs(t) do
  138.     if (#line + space) > max then
  139.       max = #line + space
  140.     end
  141.     if string.find(line, "<title>") ~= nil then
  142.       options = options +1
  143.       mon.setTextColor(titleColor)
  144.     elseif string.find(line, "</title>") ~= nil then
  145.       options = options +1
  146.       mon.setTextColor(textColor)
  147.     elseif string.find(line, "<subtitle>") ~= nil then
  148.       options = options +1
  149.       mon.setTextColor(subtitleColor)
  150.     elseif string.find(line, "</subtitle>") ~= nil then
  151.       options = options +1
  152.       mon.setTextColor(textColor)
  153.     else
  154.       y = i - options - static
  155.       mon.setCursorPos(x, y)
  156.       mon.write(table..line)
  157.       if y >= h then
  158.         static = y + static
  159.         space = 2
  160.         x = max + space + x
  161.         max = 3
  162.         table = "| "
  163.       end
  164.     end -- if string.find(...)
  165.   end -- for i, line in ipairs(t) do
  166. end -- printColumn(mon, t)
  167.  
  168. local function printPaged(mon, t, titleColor, subtitleColor, textColor, showPages)
  169.   local options = 0  
  170.   local existingPages = 0
  171.   local ignorePageLines = true
  172.   local innerPage = false
  173.   local pageCount = 0
  174.  
  175.   for a, search in ipairs(t) do
  176.     if string.find(search, "<page>") ~= nil then
  177.       existingPages = existingPages + 1
  178.     end
  179.   end
  180.   if currentPage > existingPages then
  181.     currentPage = 1
  182.   end
  183.   for i, line in ipairs(t) do
  184.     if string.find(line, "<title>") ~= nil then
  185.       options = options +1
  186.       mon.setTextColor(titleColor)
  187.     elseif string.find(line, "</title>") ~= nil then
  188.       options = options +1
  189.       mon.setTextColor(textColor)
  190.     elseif string.find(line, "<subtitle>") ~= nil then
  191.       options = options +1
  192.       mon.setTextColor(subtitleColor)
  193.     elseif string.find(line, "</subtitle>") ~= nil then
  194.       options = options +1
  195.       mon.setTextColor(textColor)
  196.     elseif string.find(line, "<page>") ~= nil then
  197.       options = options +1
  198.       pageCount = pageCount +1
  199.       innerPage = true
  200.       if pageCount == currentPage then
  201.         ignorePageLines = false
  202.       end
  203.     elseif string.find(line, "</page>") ~= nil then
  204.       options = options +1
  205.       innerPage = false
  206.       if pageCount == currentPage then
  207.         ignorePageLines = true
  208.       end
  209.     else
  210.       if (innerPage == true) and (ignorePageLines == true) then
  211.         options = options +1  
  212.       else
  213.         mon.setCursorPos(1, i - options)
  214.         line = " "..line
  215.         mon.write(line)
  216.       end
  217.     end -- if string.find(...) ~= nil then
  218.   end -- for i, line in ipairs(t) do
  219.   if showPages == true and existingPages > 0 then
  220.     local w, h = mon.getSize()
  221.     mon.setCursorPos(1, h)
  222.     mon.write(string.format("%s/%s", currentPage, existingPages))
  223.   end
  224.   currentPage = currentPage +1
  225. end -- printPaged(mon, t, titleColor, subtitleColor, textColor)
  226.  
  227. -- returns the peripheral.wrap monitor
  228. function getMonitor()
  229.   local SIDES = {"left";
  230.                  "right";
  231.                  "back";
  232.                  "front";
  233.                  "top";
  234.                  "bottom"}
  235.   local mon
  236.   for i = 1, #SIDES do
  237.     if peripheral.isPresent(SIDES[i]) then
  238.       if peripheral.getType(SIDES[i]) == "monitor" then
  239.         mon = peripheral.wrap(SIDES[i])
  240.         return mon
  241.       end
  242.     elseif i == #SIDES and peripheral.isPresent(SIDES[i]) == "false" then
  243.       error("No Monitor found. Please check the connection the the monitor")
  244.       return
  245.     end
  246.   end
  247. end
  248.  
  249. -- returns colors.black etc
  250. function getColorApi(color)
  251.   if color == "colors.white" then
  252.     return colors.white
  253.   elseif color == "colors.orange" then
  254.     return colors.orange
  255.   elseif color == "colors.magenta" then
  256.     return colors.magenta
  257.   elseif color == "colors.lightBlue" then
  258.     return colors.lightBlue
  259.   elseif color == "colors.yellow" then
  260.     return colors.yellow
  261.   elseif color == "colors.lime" then
  262.     return colors.lime
  263.   elseif color == "colors.pink" then
  264.     return colors.pink
  265.   elseif color == "colors.gray" then
  266.     return colors.gray
  267.   elseif color == "colors.lightGray" then
  268.     return colors.lightGray
  269.   elseif color == "colors.cyan" then
  270.     return colors.cyan
  271.   elseif color == "colors.purple" then
  272.     return colors.purple
  273.   elseif color == "colors.blue" then
  274.     return colors.blue
  275.   elseif color == "colors.brown" then
  276.     return colors.brown
  277.   elseif color == "colors.green" then
  278.     return colors.green
  279.   elseif color == "colors.red" then
  280.     return colors.red
  281.   elseif color == "colors.black" then
  282.     return colors.black
  283.   else
  284.     error("Color unknown. Change your config.")
  285.     return
  286.   end
  287. end
  288.  
  289. -- changes the bool string ("true") to real bool (true)
  290. function tobool(string)
  291.   if string == "true" then
  292.     return true
  293.   else
  294.     return false
  295.   end
  296. end
  297.  
  298. -- writes the text from the downloaded paste to the monitor
  299. function writeText (textMode, mText, config)
  300.   local textColor      = getColorApi(config["TextColor"])
  301.   local titleColor     = getColorApi(config["TitleColor"])
  302.   local subtitleColor  = getColorApi(config["SubtitleColor"])
  303.   local backroundColor = getColorApi(config["BackgroundColor"])
  304.   local textSize       = tonumber(config["TextSize"])
  305.   local showPages      = tobool(config["ShowPages"])
  306.   local mon = getMonitor()
  307.   if mon == nil then
  308.     error("No Monitor found. Please check the connection the the monitor")
  309.     return
  310.   end
  311.   mon.clear()
  312.   mon.setBackgroundColor(backroundColor)
  313.   mon.clear()
  314.   mon.setTextScale(textSize)
  315.   mon.setTextColor(textColor)
  316.   mon.setCursorPos(1, 1)
  317.   if textMode == "center" then
  318.     printCenter(mon, mText, titleColor, subtitleColor, textColor)
  319.   elseif textMode == "simpleCenter" then
  320.     printSimpleCenter(mon, mText, textColor)
  321.   elseif textMode == "left" then
  322.     printLeft(mon, mText, titleColor, subtitleColor, textColor)
  323.   elseif textMode == "column" then
  324.     printColumn(mon, mText, titleColor, subtitleColor, textColor)
  325.   elseif textMode == "paged" then
  326.     printPaged(mon, mText, titleColor, subtitleColor, textColor, showPages)
  327.   else
  328.     error("Given textMode is not known")
  329.   end
  330. end -- writeText()
  331.  
  332. function createConfigFile()
  333.   local f = fs.open("config", "w")
  334.   if f == nil then
  335.     error("Config file can not be created.")
  336.     return
  337.   end  
  338.   local t = { "-- Don't forget to scroll!!!                            : ";
  339.               "-- Pastebinmode: true or false, if false create";
  340.               "-- own file with FileName und fill it with text         : ";
  341.               "UsePastebin: true";
  342.               "-- If you use this program offline insert here          : ";
  343.               "-- the name of your file or let it so                   : ";
  344.               "FileName: downloadedFile";
  345.               "-- Insert here a pastebin code otherwhise:let it so";
  346.               "PastebinCode: 1AnB6bbq";
  347.               "-- Possible text modes: left, center";
  348.               "-- simpleCenter, paged                                  : ";
  349.               "TextMode: left";
  350.               "TextColor: colors.cyan";
  351.               "TitleColor: colors.orange";
  352.               "SubtitleColor: colors.white";
  353.               "BackgroundColor: colors.black";
  354.               "--Insert the Time a Page is shown and                     : ";
  355.               "--if Pagenumbers should be shown: ";
  356.               "PageFrequency: 20";
  357.               "ShowPages: true";
  358.               "RefreshSpeed: 300";
  359.               "-- Change the size of the text: in steps of 0.5";
  360.               "-- from 0.5 to 5                                        : ";
  361.               "TextSize: 1";
  362.               "ResetInputSide: top";
  363.               "AutoCreateStartup: true";
  364.               "AutoUpdate: true";
  365.               "-- Select debug or release branch                       : ";
  366.               "Branche: release"}
  367.   for i, _ in pairs(t) do
  368.       f.write(t[i].."\n")
  369.   end
  370.   f.close()
  371.   print("Standard config file created.")
  372.   print("Please edit the config if you want, save and exit it after that.")
  373.   print("The program will run aumatically further.")
  374.   sleep(5)
  375.   shell.run("edit config")
  376.   print("Loading config...")
  377.   sleep(1)  
  378.   return
  379. end
  380.  
  381. -- Checks all elements of the config and fix it if needed, does not reload config!
  382. function checkAndFixConfig(config)
  383.   local configWasCorrect = true
  384.   if not fs.exists("config") then
  385.     createConfigFile()
  386.     return false
  387.   end
  388.   local f = fs.open("config", "a")
  389.   if config["UsePastebin"] == nil then
  390.     f.write("UsePastebin: true\n")
  391.     configWasCorrect = false
  392.   end
  393.   if config["FileName"] == nil then
  394.     f.write("FileName: downloadedFile\n")
  395.     configWasCorrect = false
  396.   end
  397.   if config["PastebinCode"] == nil then
  398.     f.write("PastebinCode: 1AnB6bbq\n")
  399.     configWasCorrect = false
  400.   end  
  401.   if config["TextColor"] == nil then
  402.     f.write("TextColor: colors.cyan\n")
  403.     configWasCorrect = false
  404.   end
  405.   if config["TitleColor"] == nil then
  406.     f.write("TitleColor: colors.orange\n")
  407.     configWasCorrect = false
  408.   end
  409.   if config["SubtitleColor"] == nil then
  410.     f.write("SubtitleColor: colors.white\n")
  411.     configWasCorrect = false
  412.   end
  413.   if config["BackgroundColor"] == nil then
  414.     f.write("BackgroundColor: colors.black\n")
  415.     configWasCorrect = false
  416.   end
  417.   if config["TextSize"] == nil then
  418.     f.write("TextSize: 1\n")
  419.     configWasCorrect = false
  420.   end
  421.   if config["TextMode"] == nil then
  422.     f.write("TextMode: left\n")
  423.     configWasCorrect = false
  424.   end
  425.   if config["RefreshSpeed"] == nil then
  426.     f.write("RefreshSpeed: 300\n")
  427.     configWasCorrect = false
  428.   end
  429.   if config["PageFrequency"] == nil then
  430.     f.write("PageFrequency: 30\n")
  431.     configWasCorrect = false
  432.   end
  433.   if config["ShowPages"] == nil then
  434.     f.write("ShowPages: true\n")
  435.     configWasCorrect = false
  436.   end
  437.   if config["ResetInputSide"] == nil then
  438.     f.write("ResetInputSide: top\n")
  439.     configWasCorrect = false
  440.   end
  441.   if config["AutoCreateStartup"] == nil then
  442.     f.write("AutoCreateStartup: true\n")
  443.     configWasCorrect = false
  444.   end
  445.   if config["AutoUpdate"] == nil then
  446.     f.write("AutoUpdate: true\n")
  447.     configWasCorrect = false
  448.   end
  449.   if config["Branche"] == nil then
  450.     f.write("Branche: release\n")
  451.     configWasCorrect = false
  452.   end
  453.   f.close()
  454.   if configWasCorrect then
  455.     print("Config was correct.")
  456.   else
  457.     print("Config was incorrect but i fixed it.")
  458.   end
  459.   sleep(1)
  460.   return configWasCorrect
  461. end
  462.  
  463. function loadConfigFile()
  464.   local raw = {}
  465.   if fs.exists("config") then
  466.     f = fs.open("config", "r")
  467.     local line = f.readLine()
  468.       while line ~= nil do
  469.         table.insert(raw, line)
  470.         line = f.readLine()
  471.       end
  472.     f.close()
  473.   else
  474.     error("Config file can not be opened. Please remove it and restart the computer.")
  475.     return
  476.   end
  477.   local new = {}
  478.   for i = 1, #raw do
  479.     new[string.sub(raw[i], 1, string.find(raw[i], ":")-1)] = string.sub(raw[i], string.find(raw[i], ":")+2)
  480.   end
  481.   return new
  482. end
  483.  
  484. -- Extracts the new version number
  485. function getNewVersionAndBranch(myBranch)
  486.   updateFile("imtxbLkf", "newProgram")
  487.   local newVersion = "0.0"
  488.   local newBranch = "debug"
  489.   local count = 0
  490.   uText = {}
  491.   if fs.exists("newProgram") then
  492.     uText = readFile("newProgram", uText)
  493.     for i, line in ipairs(uText) do
  494.       if string.find(line, "version =") ~= nil then
  495.         local firstQuote = string.find(line, "\"")
  496.         local secondQuote = string.find(line, "\"", firstQuote+1)
  497.         newVersion = string.sub(line, firstQuote+1, secondQuote-1)
  498.         count = count + 1
  499.       elseif string.find(line, "branche =") ~= nil then
  500.         local firstQuote = string.find(line, "\"")
  501.         local secondQuote = string.find(line, "\"", firstQuote+1)
  502.         newBranch = string.sub(line, firstQuote+1, secondQuote-1)
  503.         count = count + 1
  504.       end
  505.       if count == 2 then
  506.         return newVersion, newBranch
  507.       end
  508.     end
  509.   else
  510.     print("Pastebin not available.")
  511.     return version, myBranch
  512.   end
  513.   return newVersion, newBranch
  514. end
  515.  
  516. -- creates an update program which will run and deleted auomatically
  517. function createUpdater()
  518.   print("Creating updater...")
  519.   sleep(1)
  520.   local programName = shell.getRunningProgram()
  521.   local f = fs.open("updater", "w")
  522.   if f == nil then
  523.     error("Updater file can not be created.")
  524.     return
  525.   end  
  526.   local t = { string.format("local programName = \"%s\"", programName);
  527.               "local pastebinCode = \"imtxbLkf\"";
  528.               "shell.run(\"clear\")";
  529.               "print(\"Welcome to the Update-Tool, this tool will automatically update the Text to Monitor program.\")";
  530.               "sleep(3)";
  531.               "local movere = \" rm\"";
  532.               "movere = string.sub(movere, 1)";
  533.               "if fs.exists(\"oldProgram\") then";
  534.               "  shell.run(string.format(\"%s oldProgram\", movere))";
  535.               "end";
  536.               "if fs.exists(programName) then";
  537.               "  shell.run(string.format(\"cp %s oldProgram \\ \", programName))";
  538.               "  shell.run(string.format(\"%s %s\", movere, programName))";
  539.               "end";
  540.               "if fs.exists(\"newProgram\") then"; -- uses the downloaded file from getNewVersionAndBranch() if available
  541.               "  shell.run(string.format(\"cp newProgram %s \\ \", programName))";
  542.               "  shell.run(string.format(\"%s newProgram\", movere))";
  543.               "else";
  544.               "  shell.run(string.format(\"pastebin get %s %s\", pastebinCode, programName))";
  545.               "end";
  546.               "if fs.exists(\"oldStartup\") then";
  547.               "  print(\"Restoring original startup file...\")";
  548.               "  sleep(2)";
  549.               "  shell.run(string.format(\"%s startup\", movere))";
  550.               "  shell.run(\"cp oldStartup startup \\ \")"; -- restore the original Startup
  551.               "  shell.run(string.format(\"%s oldStartup\", movere))";
  552.               "else";
  553.               "  shell.run(string.format(\"%s startup\", movere))";
  554.               "end";
  555.               "print(\"Updater finished. Rebooting...\")";
  556.               "local f = fs.open(\"updated\", \"w\")";
  557.               "f.write(\"This file should deleted automatically\")";
  558.               "f.close()";
  559.               "sleep(2)";
  560.               "os.reboot()"}
  561.   for i, _ in pairs(t) do
  562.       f.write(t[i].."\n")
  563.   end
  564.   f.close()  
  565.   return
  566. end
  567.  
  568. -- changes the startup file to start the updater
  569. function modifyStartup()
  570.   print("Changing startup for update")
  571.   shell.run("rm oldStartup")
  572.   if fs.exists("startup") then
  573.     shell.run("cp startup oldStartup \\") -- Backuping original startup
  574.     shell.run("rm startup")
  575.   end
  576.   local f = fs.open("startup", "w")
  577.   if f == nil then
  578.     error("Startup file can not be created.")
  579.     return
  580.   end  
  581.   local t = { "shell.run(\"updater\")";
  582.               ""}
  583.   for i, _ in pairs(t) do
  584.       f.write(t[i].."\n")
  585.   end
  586.   f.close()  
  587.   return
  588. end
  589.  
  590. -- updates the program itself
  591. function updateProgram(myBranch)
  592.   print("Checking if new update is available...")
  593.   sleep(1)
  594.   local newVersion
  595.   local newBranch
  596.   newVersion, newBranch = getNewVersionAndBranch(myBranch)
  597.   print(string.format("Your version: %s", version))
  598.   print(string.format("Your branch: %s", myBranch))
  599.   sleep(2)
  600.   if version == newVersion then
  601.     print("No new update is available.")
  602.     shell.run("rm newProgram")
  603.     sleep(2)
  604.     return
  605.   end
  606.   if ((myBranch == newBranch) or (myBranch == "debug")) then
  607.     print(string.format("Update available. New version: %s", newVersion))
  608.     sleep(2)
  609.     createUpdater()
  610.     modifyStartup()
  611.     print("Rebooting CraftOS for updating...")
  612.     sleep(2)
  613.     os.reboot()
  614.   else
  615.     print("No new update is available")
  616.     shell.run("rm newProgram")
  617.   end
  618. end
  619.  
  620. -- removes all files which where needed for update
  621. function clearUpdate()
  622.   if fs.exists("updated") then
  623.     print("Cleaning update.")
  624.     sleep(1)
  625.     if fs.exists("updater") then
  626.       shell.run("rm updater")
  627.     end
  628.     if fs.exists("oldProgram") then
  629.       shell.run("rm oldProgram")
  630.     end
  631.     if fs.exists("updatenow") then
  632.       shell.run("rm updatenow")
  633.     end
  634.     if fs.exists("updated") then
  635.       shell.run("rm updated")
  636.     end
  637.   end
  638.   return
  639. end
  640.  
  641. function createStartup()
  642.   if not fs.exists("startup") then
  643.     print("Creating startup...")
  644.     sleep(1)
  645.     local f = fs.open("startup", "w")
  646.     f.write(string.format("shell.run(\"%s\") \n", shell.getRunningProgram()))
  647.     f.close()
  648.     print("Startup created.")
  649.     sleep(1)
  650.   end
  651.   return
  652. end
  653.  
  654. function readAndWrite(fileName, mText, textMode, config)
  655.   if fs.exists(fileName) then
  656.     mText = readFile(fileName, mText)
  657.     writeText(textMode, mText, config)
  658.   else
  659.     print("File not found, ty again later.")
  660.   end
  661. end
  662.  
  663. function main ()
  664.   local forceUpdate = false
  665.   local mText = {}
  666.   local runtimeCounter = 0
  667.   local updateCounter = 0
  668.   local config
  669.   local lastRefresh = os.time()
  670.  
  671.   shell.run("clear")
  672.   clearUpdate()
  673.  
  674.   if fs.exists("config") then -- all about checking and loading the config
  675.     config = loadConfigFile()
  676.   else
  677.     createConfigFile()
  678.     config = loadConfigFile()
  679.   end  
  680.   if not checkAndFixConfig(config) then -- checks if all configs which are needed are available, otherwhise it will fix it and reload config
  681.     config = loadConfigFile()
  682.   end
  683.  
  684.   local usePastebin       = tobool(config["UsePastebin"])
  685.   local fileName          = config["FileName"]
  686.   local pastebinCode      = config["PastebinCode"]
  687.   local textMode          = config["TextMode"]
  688.   local pageSpeed         = tonumber(config["PageFrequency"])
  689.   local refreshSpeed      = tonumber(config["RefreshSpeed"])
  690.   resetInputSide          = config["ResetInputSide"]
  691.   local autoCreateStartup = tobool(config["AutoCreateStartup"])
  692.   local autoUpdate        = tobool(config["AutoUpdate"])
  693.   local myBranch          = config["Branche"]
  694.  
  695.   if fs.exists("updatenow") then
  696.     forceUpdate = true
  697.   end
  698.  
  699.   if autoCreateStartup then -- creates a startup file
  700.     createStartup()
  701.   end  
  702.  
  703.   -- mainloop
  704.   if textMode == "paged" then
  705.     while true do -- depends on pageSpeed
  706.       shell.run("clear")
  707.      
  708.       if forceUpadte or (((runtimeCounter + 1) % 100) == 0) then
  709.         forceUpdate = false
  710.         updateProgram(myBranch)
  711.       end      
  712.      
  713.       if usePastebin == true then -- download the file or use the local file
  714.         print("Program startet: Running in Pastebin-Mode.")
  715.         if (runtimeCounter % 100) == 0 then
  716.           updateFile(pastebinCode, fileName)
  717.         end
  718.       else
  719.         print("Program startet: Running in Local-Mode (no Pastebin).")
  720.       end
  721.      
  722.       readAndWrite(fileName, mText, textMode, config)
  723.  
  724.       if runtimeCounter == 1000 then
  725.         shell.run("rm backup")
  726.         shell.run(string.format("cp %s backup \\", fileName))
  727.         print(string.format("%s backuped to >backup<", fileName))
  728.       end  
  729.      
  730.       runtimeCounter = runtimeCounter +1
  731.       if runtimeCounter > 1000000000 then
  732.         runtimeCounter = 1
  733.       end
  734.       sleep(pageSpeed)
  735.     end -- while true do
  736.   else  
  737.     while true do -- depends on refreshSpeed
  738.       shell.run("clear")
  739.      
  740.       if forceUpdate or (updateCounter > 5) then
  741.         forceUpdate = false
  742.         updateProgram(myBranch)
  743.         if updateCounter > 5 then
  744.           updateCounter = 0
  745.         end
  746.       elseif refreshSpeed > 1000 then
  747.         updateCounter = updateCounter + 6
  748.       else
  749.         updateCounter = updateCounter + 1
  750.       end
  751.      
  752.       if usePastebin == true then -- download the file or use the local file
  753.         print("Program startet: Running in Pastebin-Mode.")
  754.         updateFile(pastebinCode, fileName)
  755.       else
  756.         print("Program startet: Running in Local-Mode (no Pastebin).")
  757.       end
  758.      
  759.       readAndWrite(fileName, mText, textMode, config)
  760.      
  761.       if (runtimeCounter == 10) then -- after 10 complete runs do a backup
  762.         runtimeCounter = runtimeCounter + 1
  763.         shell.run("rm backup")
  764.         shell.run(string.format("cp %s backup \\", fileName))
  765.         print(string.format("%s backuped to >backup<", fileName))
  766.       elseif runtimeCounter < 25 then
  767.         runtimeCounter = runtimeCounter + 1
  768.       elseif runtimeCounter == 25 then -- 25 complete runs until the refreshSpeed ist set to 1800 (30min)
  769.         refreshSpeed = 1800
  770.         runtimeCounter = runtimeCounter + 1
  771.         print("refreshSpeed set to 1800 (30min)")
  772.       end    
  773.      
  774.       sleep(refreshSpeed)
  775.     end -- while true do
  776.   end -- if textMode == "paged" then
  777. end -- main ()
  778.  
  779. currentPage = 1
  780.  
  781. -- reset computer by redstone input
  782. function listener()
  783.    while true do
  784.       if rs.getInput(resetInputSide) then
  785.         os.reboot()
  786.       end
  787.       sleep(1)
  788.    end
  789. end -- listener()
  790.  
  791. -- Runs main and listener parallel
  792. parallel.waitForAll(listener, main )
  793.  
  794. --Coded by Egnarts --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement