Skip to content

Instantly share code, notes, and snippets.

@niklas-heer
Created November 25, 2012 19:52
Show Gist options
  • Save niklas-heer/4145064 to your computer and use it in GitHub Desktop.
Save niklas-heer/4145064 to your computer and use it in GitHub Desktop.
LUA: CCUpdater
--[[
Name: CC-Updater
Author: Niklas Heer
Version: 1.1
]]
--[[ Pastebin Stuff ]]
local pastebinWget = "mFisCzD5"
local pastebinUpdater = "AZp1TVjT"
local pastebinConfigFileURL = "http://pastebin.com/raw.php?i=GLdbuTgy"
--[[ Variables ]]
local version = "1.1"
local screenW, screenH = term.getSize()
local updaterName = "updater"
local file
--[[ Files/Paths ]]
local configFile = "/config/updater"
local md5File = "/config/md5"
local configDir = "/config"
--[[ Header Function ]]
function printHeader()
print("------------------- CC Updater ------------------")
print("| by: Niklas Heer | Version: "..version.." |")
print("| 'Strg' for options. Choose program: |")
print("-------------------------------------------------")
print("")
end
--[[ Clears everything from the screen ]]
function clear()
term.clear()
term.setCursorPos(1,1)
end
--[[ Prints the Usage of the Programm ]]
function printUsage()
print("Usage: updater <cmd> [<name>]")
print("")
print("Available <cmd>:")
print("config: updates the config file")
print("get : updates a programm, <name> is needed!")
print("list : prints a list of the available programms!")
print("help : prints this text!")
end
--[[ START MENU FUNCTIONS ]]
--[[ Returns installed if program is installed ]]
local function isInstalled(name)
if (fs.exists(name)) and (fs.isDir(name) == false) then
return "[installed]"
else
return ""
end
end
--[[ Returns installed if program is installed ]]
local function isUpdateAvailable(name)
if updateAvailable(name) then
if (fs.exists(name)) and (fs.isDir(name) == false) then
return "[updateable]"
else
return ""
end
else
return ""
end
end
--[[ Clears Options Display ]]
function clearOptions()
local clearly = ""
term.setCursorPos(1,screenH-1)
for i=1,(screenW-1) do
clearly = clearly.." "
end
print(clearly)
term.setCursorPos(1,screenH-1)
end
--[[ Adding Display names ]]
function addDisplayNames(inList)
local list = inList
for i = 1,#list do
list[i] = list[i].." "..isInstalled(list[i]).." "..isUpdateAvailable(list[i])
end
return list
end
--[[ The Menu ]]
function menu(inList) -- ver 1.1
local sel = 1
print("Loading data...")
print("Checking for Updates...")
print("Please wait, this could take a few seconds!")
local list = addDisplayNames(inList)
clear()
printHeader()
local control
local offX,offY = term.getCursorPos()
local curX,curY = term.getCursorPos()
local running = true
while running do
if sel > #list then sel = 1 end
if sel < 1 then sel = #list end
for i = 1,#list do
term.setCursorPos(offX,offY+i-1)
if sel == i then
print(">"..list[i])
else
print(" "..list[i])
end
end
while running do
local e,e1,e2,e3,e4,e5 = os.pullEvent()
if e == "key" then
if e1 == 200 then -- up key
sel = sel-1
break
end
if e1 == 208 then -- down key
sel = sel+1
break
end
if e1 == 29 then -- strg key left
term.setCursorPos(1,screenH-1)
print("[Exit] Update")
control = "exit"
local run = true
while run do
local ev,ev1 = os.pullEvent()
if ev == "key" then
if ev1 == 203 then -- left
clearOptions()
print("[Exit] Update")
control = "exit"
end
if ev1 == 205 then -- right
clearOptions()
print(" Exit [Update]")
control = "update"
end
if ev1 == 28 then -- enter
run = false
return control
end
if ev1 == 29 then -- strg key left
clearOptions()
run = false
break
end
end
end
end
if e1 == 28 then -- enter
term.setCursorPos(curX,curY)
return list[sel],sel
end
end
end
end
end
--[[ END MENU FUNCTIONS ]]
--[[ Checks if a config file exists ]]
function existsConfigFile()
if (fs.exists(configFile)) and (fs.isDir(configFile) == false) then
return true
else
return false
end
end
--[[ Checks if the Config dir exists ]]
function existsConfigDir()
if (fs.exists(configDir)) and (fs.isDir(configDir)) then
return true
else
return false
end
end
--[[ Makes the Config Directory ]]
function makeConfigDir()
fs.makeDir(configDir)
end
--[[ Downloads the wget programm and downloads the config file ]]
function downloadConfig(command, name)
downloadPastebin(pastebinWget, "wget")
sleep(1)
shell.run("wget", pastebinConfigFileURL, configFile)
if command == "" then
shell.run("updater")
else
if name == "" then
shell.run("updater", command)
else
shell.run("updater", command, name)
end
end
end
--[[ download() function to download a programm from pastebin ]]
function downloadPastebin(id, programm)
shell.run("delete", programm)
shell.run("pastebin", "get", id , programm)
end
--[[ Gets the Programm id from the config file ]]
function getProgrammIdByName(name)
local f = fs.open(configFile, "r")
local found = false
local run = true
local search = ""
local searchTmp
local result = ""
while run do
search = f.readLine()
searchTmp = string.find(search, name)
if search == NIL then
run = false
end
if searchTmp ~= NIL then
result = string.sub(search, 1, 8)
found = true
run = false
end
end
f.close()
return result
end
--[[ Makes all inital fancy stuff ]]
function initalRun(command, name)
if not existsConfigDir() then
makeConfigDir()
end
if not existsConfigFile() then
downloadConfig(command, name)
end
if not existsMD5File() then
makeMD5File()
end
end
--[[ Provides an interface for the Download ]]
function download(name)
local id = getProgrammIdByName(name)
if id ~= "" then
downloadPastebin(id, name)
return "Successfully updated "..name.."!"
else
return "Failed updating "..name..". Programm not in List!"
end
end
--[[ Updates the config file ]]
function updateConfig()
fs.delete(configFile)
shell.run("wget", pastebinConfigFileURL, configFile)
print("Successfully updated the Config File!")
end
--[[ Returns a list with the available Programmnames ]]
function getList()
local f = fs.open(configFile, "r")
local endOfFile = false
local search = ""
local i = 1
local result = {}
while not endOfFile do
search = f.readLine()
if search == NIL then
endOfFile = true
else
result[i] = string.sub(search, 10)
end
i = i+1
end
f.close()
return result
end
--[[ Prints the programm list ]]
function printList()
local list = getList()
for i=1,# list do
print(i..": "..list[i])
end
end
--[[ runs the program itself in gui mode ]]
function selfRun()
shell.run("updater")
end
--[[ gets a pastebin-urls md5 hash by id ]]
function getMD5ByID(id)
local md5_checker = "http://niklas-heer.de/cc/md5.php?"
local pastebin = "http://pastebin.com/raw.php?i="
local request = md5_checker..pastebin..id
local content = http.get(request)
local md5 = content.readAll()
content.close()
return md5
end
--[[ gets a pastebin-urls md5 hash by name ]]
function getMD5ByName(name)
return getMD5ByID(getProgrammIdByName(name))
end
--[[ makes a new md5 file ]]
function makeMD5File()
local md5 = getMD5ByID(pastebinUpdater)
local name = updaterName
local line = md5.." "..name
f = fs.open(md5File, "w")
f.writeLine(line)
sleep(1)
f.close()
sleep(1)
end
--[[ Checks if a md5 file exists ]]
function existsMD5File()
if (fs.exists(md5File)) and (fs.isDir(md5File) == false) then
return true
else
return false
end
end
--[[ checks a pastebin-urls md5 hash ]]
function updateAvailable(name)
local md5 = getMD5ByName(name)
local update = false
if not existsMD5File() then
makeMD5File()
end
if md5 ~= NIL then
local file = fs.open(md5File, "r")
local found = false
local run = true
local search2
local searchTmp2
while run do
search2 = file.readLine()
if search2 == NIL then
run = false
if not found then
update = true
end
break
end
searchTmp2 = string.find(search2, md5)
if searchTmp2 ~= NIL then
found = true
run = false
end
end
file.close()
else
update = false
end
return update
end
--[[ gets the clear selection name ]]
function clearSelectionName(inName, inList)
local name = inName
local list = inList
local search
local result = ""
for i = 1,#list do
search = string.find(name, list[i])
if search ~= NIL then
result = list[i]
end
end
return result
end
--[[ Main Programm ]]
local args = { ... }
local cmd = ""
local downloadName = ""
local run = false
local list
local withGui = false
clear()
if #args > 2 then
printUsage()
else
if #args < 1 then
withGui = true
else
if #args == 2 then
cmd = args[1]
downloadName = args[2]
else
cmd = args[1]
end
end
run = true
end
if run then
if not existsConfigFile() then
initalRun(cmd,downloadName)
else
if withGui then
list = getList()
originalList = getList()
printHeader()
local selection = menu(list)
local cleanSelection = clearSelectionName(selection, originalList)
if cleanSelection ~= "" then
if cleanSelection == "exit" then
clear()
elseif cleanSelection == "update" then
clear()
updateConfig()
selfRun()
else
clear()
download(cleanSelection)
end
end
else
if cmd == "config" then
updateConfig()
elseif cmd == "get" then
print(download(downloadName))
elseif cmd == "list" then
printList()
elseif cmd == "help" then
printUsage()
else
print("Command not found!")
printUsage()
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment