Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Coded by Egnarts --
- -- Changelog:
- -- v5.2: some bugfixes
- -- v5.1: some bugfixes
- -- v5.0: Its now possible to change the background color of the Monitor
- -- v4.5.1: Fixed bug in simpleCenter function
- -- v4.5: Waiting some Seconds before updating to prevent Request-Bans with Pastebin.
- -- v4.4: Fixed bug where the text was not shown when computer was unloaded and pastebin unavailable
- -- v4.3: Showing Pagenumbers is now possible. Look at config.
- -- v4.2: Text Mode: Paged now available
- -- v4.1: shorter time until text is shown, some bugfixes, fixed error in updater
- -- v4.0: language fixes, fixed monitor detection, fixed update routine
- -- v3.9: fix in counting with runtimeCounter, error handling, fixed textSize
- -- v3.8: fixed a bug where the program never updates itself
- -- v3.7: some new prints and bugfixes
- -- v3.6: added branches
- -- v3.5: bugfixes
- -- v3.4: added a config-check, and two new settings and their implementation
- -- v3.3: some security updates, other standard refreshSpeed
- -- Don't Change:
- version = "5.2"
- branche = "release"
- resetInputSide = "back"
- -- download the Paste if pastebin.com is reachable
- function updateFile(pastebinCode, fileName)
- local r = math.random(7)-1
- print(string.format("Waiting %s Seconds before updating.", r))
- sleep(r)
- http.request("http://www.pastebin.com")
- local requesting = true
- while requesting do
- event, url = os.pullEvent()
- if event == "http_success" then
- if fs.exists(fileName) then
- shell.run(string.format("rm %s", fileName))
- end
- shell.run(string.format("pastebin get %s %s", pastebinCode, fileName))
- requesting = false
- return true
- elseif event == "http_failure" then
- print("Connecting to pastebin.com ... Failed. Try again later.")
- print("File not updated")
- requesting = false
- return false
- end
- end
- end -- updateFile()
- -- open file and insert into list, line per line
- function readFile (fileName, sText)
- local f = fs.open(fileName, "r")
- if f == nil then
- error(string.format("File with name >%s< not found!", fileName))
- return
- end
- local text = f.readLine()
- sText = {}
- while text ~= nil do
- table.insert(sText, text)
- text = f.readLine()
- end
- f.close()
- return sText
- end -- readFile ()
- local function printCenter(mon, t, titleColor, subtitleColor, textColor)
- local w, h = mon.getSize()
- local y = 0 -- math.floor((h / 2) - (#t / 2)) - 1
- local options = 0
- for i, line in ipairs(t) do
- if string.find(line, "<title>") ~= nil then
- options = options +1
- mon.setTextColor(titleColor)
- elseif string.find(line, "</title>") ~= nil then
- options = options +1
- mon.setTextColor(textColor)
- elseif string.find(line, "<subtitle>") ~= nil then
- options = options +1
- mon.setTextColor(subtitleColor)
- elseif string.find(line, "</subtitle>") ~= nil then
- options = options +1
- mon.setTextColor(textColor)
- else
- local x = math.floor((w / 2) - (#line / 2) + 1)
- mon.setCursorPos(x, y + i - options)
- mon.write(line)
- end
- end -- for i, line in ipairs(t) do
- end -- printCenter(mon, t)
- local function printSimpleCenter(mon, t, textColor)
- mon.setTextColor(textColor)
- local w, h = mon.getSize()
- local y = math.floor((h / 2) - (#t / 2)) -- - 1
- for i, line in ipairs(t) do
- local x = math.floor((w / 2) - (#line / 2)) + 1
- mon.setCursorPos(x, y + i)
- mon.write(line)
- end
- end
- local function printLeft(mon, t, titleColor, subtitleColor, textColor)
- local options = 0
- for i, line in ipairs(t) do
- if string.find(line, "<title>") ~= nil then
- options = options +1
- mon.setTextColor(titleColor)
- elseif string.find(line, "</title>") ~= nil then
- options = options +1
- mon.setTextColor(textColor)
- elseif string.find(line, "<subtitle>") ~= nil then
- options = options +1
- mon.setTextColor(subtitleColor)
- elseif string.find(line, "</subtitle>") ~= nil then
- options = options +1
- mon.setTextColor(textColor)
- else
- mon.setCursorPos(1, i - options)
- line = " "..line
- mon.write(line)
- end
- end -- for i, line in ipairs(t) do
- end -- printLeft(mon, t)
- local function printColumn(mon, t, titleColor, subtitleColor, textColor)
- local options = 0
- local w, h = mon.getSize()
- local max = 3 -- min distance of columns
- local x = 1
- local y = 1
- local static = 0 -- written lines per column
- local space = 0 -- place between | and text
- local table = "" -- | symbol 2. column and so on
- for i, line in ipairs(t) do
- if (#line + space) > max then
- max = #line + space
- end
- if string.find(line, "<title>") ~= nil then
- options = options +1
- mon.setTextColor(titleColor)
- elseif string.find(line, "</title>") ~= nil then
- options = options +1
- mon.setTextColor(textColor)
- elseif string.find(line, "<subtitle>") ~= nil then
- options = options +1
- mon.setTextColor(subtitleColor)
- elseif string.find(line, "</subtitle>") ~= nil then
- options = options +1
- mon.setTextColor(textColor)
- else
- y = i - options - static
- mon.setCursorPos(x, y)
- mon.write(table..line)
- if y >= h then
- static = y + static
- space = 2
- x = max + space + x
- max = 3
- table = "| "
- end
- end -- if string.find(...)
- end -- for i, line in ipairs(t) do
- end -- printColumn(mon, t)
- local function printPaged(mon, t, titleColor, subtitleColor, textColor, showPages)
- local options = 0
- local existingPages = 0
- local ignorePageLines = true
- local innerPage = false
- local pageCount = 0
- for a, search in ipairs(t) do
- if string.find(search, "<page>") ~= nil then
- existingPages = existingPages + 1
- end
- end
- if currentPage > existingPages then
- currentPage = 1
- end
- for i, line in ipairs(t) do
- if string.find(line, "<title>") ~= nil then
- options = options +1
- mon.setTextColor(titleColor)
- elseif string.find(line, "</title>") ~= nil then
- options = options +1
- mon.setTextColor(textColor)
- elseif string.find(line, "<subtitle>") ~= nil then
- options = options +1
- mon.setTextColor(subtitleColor)
- elseif string.find(line, "</subtitle>") ~= nil then
- options = options +1
- mon.setTextColor(textColor)
- elseif string.find(line, "<page>") ~= nil then
- options = options +1
- pageCount = pageCount +1
- innerPage = true
- if pageCount == currentPage then
- ignorePageLines = false
- end
- elseif string.find(line, "</page>") ~= nil then
- options = options +1
- innerPage = false
- if pageCount == currentPage then
- ignorePageLines = true
- end
- else
- if (innerPage == true) and (ignorePageLines == true) then
- options = options +1
- else
- mon.setCursorPos(1, i - options)
- line = " "..line
- mon.write(line)
- end
- end -- if string.find(...) ~= nil then
- end -- for i, line in ipairs(t) do
- if showPages == true and existingPages > 0 then
- local w, h = mon.getSize()
- mon.setCursorPos(1, h)
- mon.write(string.format("%s/%s", currentPage, existingPages))
- end
- currentPage = currentPage +1
- end -- printPaged(mon, t, titleColor, subtitleColor, textColor)
- -- returns the peripheral.wrap monitor
- function getMonitor()
- local SIDES = {"left";
- "right";
- "back";
- "front";
- "top";
- "bottom"}
- local mon
- for i = 1, #SIDES do
- if peripheral.isPresent(SIDES[i]) then
- if peripheral.getType(SIDES[i]) == "monitor" then
- mon = peripheral.wrap(SIDES[i])
- return mon
- end
- elseif i == #SIDES and peripheral.isPresent(SIDES[i]) == "false" then
- error("No Monitor found. Please check the connection the the monitor")
- return
- end
- end
- end
- -- returns colors.black etc
- function getColorApi(color)
- if color == "colors.white" then
- return colors.white
- elseif color == "colors.orange" then
- return colors.orange
- elseif color == "colors.magenta" then
- return colors.magenta
- elseif color == "colors.lightBlue" then
- return colors.lightBlue
- elseif color == "colors.yellow" then
- return colors.yellow
- elseif color == "colors.lime" then
- return colors.lime
- elseif color == "colors.pink" then
- return colors.pink
- elseif color == "colors.gray" then
- return colors.gray
- elseif color == "colors.lightGray" then
- return colors.lightGray
- elseif color == "colors.cyan" then
- return colors.cyan
- elseif color == "colors.purple" then
- return colors.purple
- elseif color == "colors.blue" then
- return colors.blue
- elseif color == "colors.brown" then
- return colors.brown
- elseif color == "colors.green" then
- return colors.green
- elseif color == "colors.red" then
- return colors.red
- elseif color == "colors.black" then
- return colors.black
- else
- error("Color unknown. Change your config.")
- return
- end
- end
- -- changes the bool string ("true") to real bool (true)
- function tobool(string)
- if string == "true" then
- return true
- else
- return false
- end
- end
- -- writes the text from the downloaded paste to the monitor
- function writeText (textMode, mText, config)
- local textColor = getColorApi(config["TextColor"])
- local titleColor = getColorApi(config["TitleColor"])
- local subtitleColor = getColorApi(config["SubtitleColor"])
- local backroundColor = getColorApi(config["BackgroundColor"])
- local textSize = tonumber(config["TextSize"])
- local showPages = tobool(config["ShowPages"])
- local mon = getMonitor()
- if mon == nil then
- error("No Monitor found. Please check the connection the the monitor")
- return
- end
- mon.clear()
- mon.setBackgroundColor(backroundColor)
- mon.clear()
- mon.setTextScale(textSize)
- mon.setTextColor(textColor)
- mon.setCursorPos(1, 1)
- if textMode == "center" then
- printCenter(mon, mText, titleColor, subtitleColor, textColor)
- elseif textMode == "simpleCenter" then
- printSimpleCenter(mon, mText, textColor)
- elseif textMode == "left" then
- printLeft(mon, mText, titleColor, subtitleColor, textColor)
- elseif textMode == "column" then
- printColumn(mon, mText, titleColor, subtitleColor, textColor)
- elseif textMode == "paged" then
- printPaged(mon, mText, titleColor, subtitleColor, textColor, showPages)
- else
- error("Given textMode is not known")
- end
- end -- writeText()
- function createConfigFile()
- local f = fs.open("config", "w")
- if f == nil then
- error("Config file can not be created.")
- return
- end
- local t = { "-- Don't forget to scroll!!! : ";
- "-- Pastebinmode: true or false, if false create";
- "-- own file with FileName und fill it with text : ";
- "UsePastebin: true";
- "-- If you use this program offline insert here : ";
- "-- the name of your file or let it so : ";
- "FileName: downloadedFile";
- "-- Insert here a pastebin code otherwhise:let it so";
- "PastebinCode: 1AnB6bbq";
- "-- Possible text modes: left, center";
- "-- simpleCenter, paged : ";
- "TextMode: left";
- "TextColor: colors.cyan";
- "TitleColor: colors.orange";
- "SubtitleColor: colors.white";
- "BackgroundColor: colors.black";
- "--Insert the Time a Page is shown and : ";
- "--if Pagenumbers should be shown: ";
- "PageFrequency: 20";
- "ShowPages: true";
- "RefreshSpeed: 300";
- "-- Change the size of the text: in steps of 0.5";
- "-- from 0.5 to 5 : ";
- "TextSize: 1";
- "ResetInputSide: top";
- "AutoCreateStartup: true";
- "AutoUpdate: true";
- "-- Select debug or release branch : ";
- "Branche: release"}
- for i, _ in pairs(t) do
- f.write(t[i].."\n")
- end
- f.close()
- print("Standard config file created.")
- print("Please edit the config if you want, save and exit it after that.")
- print("The program will run aumatically further.")
- sleep(5)
- shell.run("edit config")
- print("Loading config...")
- sleep(1)
- return
- end
- -- Checks all elements of the config and fix it if needed, does not reload config!
- function checkAndFixConfig(config)
- local configWasCorrect = true
- if not fs.exists("config") then
- createConfigFile()
- return false
- end
- local f = fs.open("config", "a")
- if config["UsePastebin"] == nil then
- f.write("UsePastebin: true\n")
- configWasCorrect = false
- end
- if config["FileName"] == nil then
- f.write("FileName: downloadedFile\n")
- configWasCorrect = false
- end
- if config["PastebinCode"] == nil then
- f.write("PastebinCode: 1AnB6bbq\n")
- configWasCorrect = false
- end
- if config["TextColor"] == nil then
- f.write("TextColor: colors.cyan\n")
- configWasCorrect = false
- end
- if config["TitleColor"] == nil then
- f.write("TitleColor: colors.orange\n")
- configWasCorrect = false
- end
- if config["SubtitleColor"] == nil then
- f.write("SubtitleColor: colors.white\n")
- configWasCorrect = false
- end
- if config["BackgroundColor"] == nil then
- f.write("BackgroundColor: colors.black\n")
- configWasCorrect = false
- end
- if config["TextSize"] == nil then
- f.write("TextSize: 1\n")
- configWasCorrect = false
- end
- if config["TextMode"] == nil then
- f.write("TextMode: left\n")
- configWasCorrect = false
- end
- if config["RefreshSpeed"] == nil then
- f.write("RefreshSpeed: 300\n")
- configWasCorrect = false
- end
- if config["PageFrequency"] == nil then
- f.write("PageFrequency: 30\n")
- configWasCorrect = false
- end
- if config["ShowPages"] == nil then
- f.write("ShowPages: true\n")
- configWasCorrect = false
- end
- if config["ResetInputSide"] == nil then
- f.write("ResetInputSide: top\n")
- configWasCorrect = false
- end
- if config["AutoCreateStartup"] == nil then
- f.write("AutoCreateStartup: true\n")
- configWasCorrect = false
- end
- if config["AutoUpdate"] == nil then
- f.write("AutoUpdate: true\n")
- configWasCorrect = false
- end
- if config["Branche"] == nil then
- f.write("Branche: release\n")
- configWasCorrect = false
- end
- f.close()
- if configWasCorrect then
- print("Config was correct.")
- else
- print("Config was incorrect but i fixed it.")
- end
- sleep(1)
- return configWasCorrect
- end
- function loadConfigFile()
- local raw = {}
- if fs.exists("config") then
- f = fs.open("config", "r")
- local line = f.readLine()
- while line ~= nil do
- table.insert(raw, line)
- line = f.readLine()
- end
- f.close()
- else
- error("Config file can not be opened. Please remove it and restart the computer.")
- return
- end
- local new = {}
- for i = 1, #raw do
- new[string.sub(raw[i], 1, string.find(raw[i], ":")-1)] = string.sub(raw[i], string.find(raw[i], ":")+2)
- end
- return new
- end
- -- Extracts the new version number
- function getNewVersionAndBranch(myBranch)
- updateFile("imtxbLkf", "newProgram")
- local newVersion = "0.0"
- local newBranch = "debug"
- local count = 0
- uText = {}
- if fs.exists("newProgram") then
- uText = readFile("newProgram", uText)
- for i, line in ipairs(uText) do
- if string.find(line, "version =") ~= nil then
- local firstQuote = string.find(line, "\"")
- local secondQuote = string.find(line, "\"", firstQuote+1)
- newVersion = string.sub(line, firstQuote+1, secondQuote-1)
- count = count + 1
- elseif string.find(line, "branche =") ~= nil then
- local firstQuote = string.find(line, "\"")
- local secondQuote = string.find(line, "\"", firstQuote+1)
- newBranch = string.sub(line, firstQuote+1, secondQuote-1)
- count = count + 1
- end
- if count == 2 then
- return newVersion, newBranch
- end
- end
- else
- print("Pastebin not available.")
- return version, myBranch
- end
- return newVersion, newBranch
- end
- -- creates an update program which will run and deleted auomatically
- function createUpdater()
- print("Creating updater...")
- sleep(1)
- local programName = shell.getRunningProgram()
- local f = fs.open("updater", "w")
- if f == nil then
- error("Updater file can not be created.")
- return
- end
- local t = { string.format("local programName = \"%s\"", programName);
- "local pastebinCode = \"imtxbLkf\"";
- "shell.run(\"clear\")";
- "print(\"Welcome to the Update-Tool, this tool will automatically update the Text to Monitor program.\")";
- "sleep(3)";
- "local movere = \" rm\"";
- "movere = string.sub(movere, 1)";
- "if fs.exists(\"oldProgram\") then";
- " shell.run(string.format(\"%s oldProgram\", movere))";
- "end";
- "if fs.exists(programName) then";
- " shell.run(string.format(\"cp %s oldProgram \\ \", programName))";
- " shell.run(string.format(\"%s %s\", movere, programName))";
- "end";
- "if fs.exists(\"newProgram\") then"; -- uses the downloaded file from getNewVersionAndBranch() if available
- " shell.run(string.format(\"cp newProgram %s \\ \", programName))";
- " shell.run(string.format(\"%s newProgram\", movere))";
- "else";
- " shell.run(string.format(\"pastebin get %s %s\", pastebinCode, programName))";
- "end";
- "if fs.exists(\"oldStartup\") then";
- " print(\"Restoring original startup file...\")";
- " sleep(2)";
- " shell.run(string.format(\"%s startup\", movere))";
- " shell.run(\"cp oldStartup startup \\ \")"; -- restore the original Startup
- " shell.run(string.format(\"%s oldStartup\", movere))";
- "else";
- " shell.run(string.format(\"%s startup\", movere))";
- "end";
- "print(\"Updater finished. Rebooting...\")";
- "local f = fs.open(\"updated\", \"w\")";
- "f.write(\"This file should deleted automatically\")";
- "f.close()";
- "sleep(2)";
- "os.reboot()"}
- for i, _ in pairs(t) do
- f.write(t[i].."\n")
- end
- f.close()
- return
- end
- -- changes the startup file to start the updater
- function modifyStartup()
- print("Changing startup for update")
- shell.run("rm oldStartup")
- if fs.exists("startup") then
- shell.run("cp startup oldStartup \\") -- Backuping original startup
- shell.run("rm startup")
- end
- local f = fs.open("startup", "w")
- if f == nil then
- error("Startup file can not be created.")
- return
- end
- local t = { "shell.run(\"updater\")";
- ""}
- for i, _ in pairs(t) do
- f.write(t[i].."\n")
- end
- f.close()
- return
- end
- -- updates the program itself
- function updateProgram(myBranch)
- print("Checking if new update is available...")
- sleep(1)
- local newVersion
- local newBranch
- newVersion, newBranch = getNewVersionAndBranch(myBranch)
- print(string.format("Your version: %s", version))
- print(string.format("Your branch: %s", myBranch))
- sleep(2)
- if version == newVersion then
- print("No new update is available.")
- shell.run("rm newProgram")
- sleep(2)
- return
- end
- if ((myBranch == newBranch) or (myBranch == "debug")) then
- print(string.format("Update available. New version: %s", newVersion))
- sleep(2)
- createUpdater()
- modifyStartup()
- print("Rebooting CraftOS for updating...")
- sleep(2)
- os.reboot()
- else
- print("No new update is available")
- shell.run("rm newProgram")
- end
- end
- -- removes all files which where needed for update
- function clearUpdate()
- if fs.exists("updated") then
- print("Cleaning update.")
- sleep(1)
- if fs.exists("updater") then
- shell.run("rm updater")
- end
- if fs.exists("oldProgram") then
- shell.run("rm oldProgram")
- end
- if fs.exists("updatenow") then
- shell.run("rm updatenow")
- end
- if fs.exists("updated") then
- shell.run("rm updated")
- end
- end
- return
- end
- function createStartup()
- if not fs.exists("startup") then
- print("Creating startup...")
- sleep(1)
- local f = fs.open("startup", "w")
- f.write(string.format("shell.run(\"%s\") \n", shell.getRunningProgram()))
- f.close()
- print("Startup created.")
- sleep(1)
- end
- return
- end
- function readAndWrite(fileName, mText, textMode, config)
- if fs.exists(fileName) then
- mText = readFile(fileName, mText)
- writeText(textMode, mText, config)
- else
- print("File not found, ty again later.")
- end
- end
- function main ()
- local forceUpdate = false
- local mText = {}
- local runtimeCounter = 0
- local updateCounter = 0
- local config
- local lastRefresh = os.time()
- shell.run("clear")
- clearUpdate()
- if fs.exists("config") then -- all about checking and loading the config
- config = loadConfigFile()
- else
- createConfigFile()
- config = loadConfigFile()
- end
- if not checkAndFixConfig(config) then -- checks if all configs which are needed are available, otherwhise it will fix it and reload config
- config = loadConfigFile()
- end
- local usePastebin = tobool(config["UsePastebin"])
- local fileName = config["FileName"]
- local pastebinCode = config["PastebinCode"]
- local textMode = config["TextMode"]
- local pageSpeed = tonumber(config["PageFrequency"])
- local refreshSpeed = tonumber(config["RefreshSpeed"])
- resetInputSide = config["ResetInputSide"]
- local autoCreateStartup = tobool(config["AutoCreateStartup"])
- local autoUpdate = tobool(config["AutoUpdate"])
- local myBranch = config["Branche"]
- if fs.exists("updatenow") then
- forceUpdate = true
- end
- if autoCreateStartup then -- creates a startup file
- createStartup()
- end
- -- mainloop
- if textMode == "paged" then
- while true do -- depends on pageSpeed
- shell.run("clear")
- if forceUpadte or (((runtimeCounter + 1) % 100) == 0) then
- forceUpdate = false
- updateProgram(myBranch)
- end
- if usePastebin == true then -- download the file or use the local file
- print("Program startet: Running in Pastebin-Mode.")
- if (runtimeCounter % 100) == 0 then
- updateFile(pastebinCode, fileName)
- end
- else
- print("Program startet: Running in Local-Mode (no Pastebin).")
- end
- readAndWrite(fileName, mText, textMode, config)
- if runtimeCounter == 1000 then
- shell.run("rm backup")
- shell.run(string.format("cp %s backup \\", fileName))
- print(string.format("%s backuped to >backup<", fileName))
- end
- runtimeCounter = runtimeCounter +1
- if runtimeCounter > 1000000000 then
- runtimeCounter = 1
- end
- sleep(pageSpeed)
- end -- while true do
- else
- while true do -- depends on refreshSpeed
- shell.run("clear")
- if forceUpdate or (updateCounter > 5) then
- forceUpdate = false
- updateProgram(myBranch)
- if updateCounter > 5 then
- updateCounter = 0
- end
- elseif refreshSpeed > 1000 then
- updateCounter = updateCounter + 6
- else
- updateCounter = updateCounter + 1
- end
- if usePastebin == true then -- download the file or use the local file
- print("Program startet: Running in Pastebin-Mode.")
- updateFile(pastebinCode, fileName)
- else
- print("Program startet: Running in Local-Mode (no Pastebin).")
- end
- readAndWrite(fileName, mText, textMode, config)
- if (runtimeCounter == 10) then -- after 10 complete runs do a backup
- runtimeCounter = runtimeCounter + 1
- shell.run("rm backup")
- shell.run(string.format("cp %s backup \\", fileName))
- print(string.format("%s backuped to >backup<", fileName))
- elseif runtimeCounter < 25 then
- runtimeCounter = runtimeCounter + 1
- elseif runtimeCounter == 25 then -- 25 complete runs until the refreshSpeed ist set to 1800 (30min)
- refreshSpeed = 1800
- runtimeCounter = runtimeCounter + 1
- print("refreshSpeed set to 1800 (30min)")
- end
- sleep(refreshSpeed)
- end -- while true do
- end -- if textMode == "paged" then
- end -- main ()
- currentPage = 1
- -- reset computer by redstone input
- function listener()
- while true do
- if rs.getInput(resetInputSide) then
- os.reboot()
- end
- sleep(1)
- end
- end -- listener()
- -- Runs main and listener parallel
- parallel.waitForAll(listener, main )
- --Coded by Egnarts --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement