lua-serve/projects/updater/update.lua

188 lines
5.7 KiB
Lua
Raw Normal View History

2021-06-06 04:03:09 +00:00
args = {...}
url = "http://lua.gaywine.org"
path = "bin/"
if not fs.exists(path) then
fs.makeDir(path)
end
temp = "temp/"
if not fs.exists(temp) then
fs.makeDir(temp)
end
2021-06-06 04:42:30 +00:00
local programToInstall = nil
2021-06-06 04:03:09 +00:00
if #args > 0 then
2021-06-06 04:42:30 +00:00
programToInstall = args[1]
2021-06-06 04:03:09 +00:00
end
2021-06-06 04:42:30 +00:00
local customPath = nil
2021-06-06 04:03:09 +00:00
if #args > 1 then
print("WARNING: custom paths are currently not supported.")
2021-06-06 04:42:30 +00:00
customPath = shell.resolve(args[2])
2021-06-06 04:03:09 +00:00
end
2021-06-06 04:39:59 +00:00
local function downloadFile(program, filename)
2021-06-06 04:03:09 +00:00
print("downloading " .. filename .. "...")
2021-06-06 04:39:59 +00:00
local fileUrl = url .. "/files/" .. program .. "/" .. filename
2021-06-06 04:03:09 +00:00
local result = http.get(fileUrl)
local resultText = result.readAll()
local file = fs.open(temp .. filename, "w")
file.write(resultText)
file.close()
end
local function split(pString, pPattern)
local Table = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pPattern
local last_end = 1
local s, e, cap = pString:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(Table, cap)
end
last_end = e + 1
s, e, cap = pString:find(fpat, last_end)
end
if last_end <= #pString then
cap = pString:sub(last_end)
table.insert(Table, cap)
end
return Table
end
2021-06-06 04:39:59 +00:00
local function getAllFiles(program)
local indexesUrl = url .. "/indexes/" .. program
local updateUrl = url .. "/updates/" .. program
2021-06-06 04:03:09 +00:00
local currentFiles = ""
2021-06-06 05:08:53 +00:00
if fs.exists(path .. program .. "/indexes.dat") then
print("#DEBUG path " .. path .. program .. "/indexes.dat")
2021-06-06 04:59:12 +00:00
local indexesFile = fs.open(path .. program .. "/indexes.dat", "r")
2021-06-06 05:07:05 +00:00
local indexesFileText = indexesFile.readAll()
2021-06-06 05:08:53 +00:00
print("#DEBUG indexesFileText " .. indexesFileText)
2021-06-06 05:07:05 +00:00
currentFiles = indexesFileText
2021-06-06 04:03:09 +00:00
indexesFile.close()
end
2021-06-06 05:08:53 +00:00
print("#DEBUG currentFiles " .. currentFiles)
2021-06-06 05:05:34 +00:00
2021-06-06 06:32:52 +00:00
local updateResult = http.post(updateUrl, "content=" .. textutils.urlEncode(currentFiles))
2021-06-06 05:05:34 +00:00
if updateResult == nil then
2021-06-06 04:39:59 +00:00
print("error! getting updates for " .. program)
2021-06-06 04:33:47 +00:00
error()
2021-06-06 04:08:17 +00:00
end
2021-06-06 05:05:34 +00:00
local updateText = updateResult.readAll()
2021-06-06 05:08:53 +00:00
print("#DEBUG updateText" .. updateText)
2021-06-06 05:05:34 +00:00
local updateTable = split(updateText, "\n")
2021-06-06 04:03:09 +00:00
2021-06-06 05:05:34 +00:00
local indexesResult = http.get(indexesUrl)
if indexesResult == nil then
2021-06-06 04:39:59 +00:00
print("error! getting indexes for " .. program)
2021-06-06 04:33:47 +00:00
error()
2021-06-06 04:08:17 +00:00
end
2021-06-06 05:05:34 +00:00
local indexesText = indexesResult.readAll()
2021-06-06 05:08:53 +00:00
print("#DEBUG indexesText" .. indexesText)
2021-06-06 05:05:34 +00:00
local indexesTable = split(indexesText, "\n")
2021-06-06 04:03:09 +00:00
2021-06-06 04:59:12 +00:00
local newIndexesFile = fs.open(path .. program .. "/indexes.dat", "w")
2021-06-06 05:05:34 +00:00
newIndexesFile.write(indexesText)
2021-06-06 04:03:09 +00:00
newIndexesFile.close()
2021-06-06 04:59:12 +00:00
print("wrote new indexes for " .. program)
2021-06-06 04:03:09 +00:00
local finalResult = {}
2021-06-06 05:05:34 +00:00
for key, value in ipairs(indexesTable) do
2021-06-06 04:03:09 +00:00
local filename = split(value, " ")[1]
2021-06-06 05:05:34 +00:00
for key2, value2 in ipairs(updateTable) do
2021-06-06 04:03:09 +00:00
local valueTable = split(value2, " ")
2021-06-06 04:51:47 +00:00
if filename == valueTable[1] then
local filePath = path .. program .. "/" .. valueTable[1]
if valueTable[2] == "delete" and fs.exists(filePath) then
fs.delete(filePath)
elseif valueTable[2] == "download" then
finalResult[#finalResult + 1] = valueTable[1]
end
2021-06-06 04:03:09 +00:00
end
end
end
return finalResult
end
2021-06-06 04:39:59 +00:00
local function finishInstall(program, files)
2021-06-06 04:56:56 +00:00
if files == nil then
2021-06-06 05:05:34 +00:00
print("files var is nil")
error()
2021-06-06 04:56:56 +00:00
end
for _, file in ipairs(files) do
2021-06-06 04:03:09 +00:00
if customPath ~= nil then
print("moving " .. file .. " to " .. customPath .. "...")
2021-06-06 04:33:47 +00:00
if not fs.exists(customPath) then
fs.makeDir(customPath)
end
2021-06-06 04:31:19 +00:00
if fs.exists(customPath .. "/" .. file) then
2021-06-06 04:33:47 +00:00
fs.delete(customPath .. "/" .. file)
2021-06-06 04:31:19 +00:00
end
2021-06-06 04:03:09 +00:00
fs.move(temp .. file, customPath .. "/" .. file)
else
2021-06-06 04:39:59 +00:00
print("moving " .. file .. " to " .. path .. program .. "...")
if not fs.exists(path .. program) then
fs.makeDir(path .. program)
2021-06-06 04:33:47 +00:00
end
2021-06-06 04:39:59 +00:00
if fs.exists(path .. program .. "/" .. file) then
fs.delete(path .. program .. "/" .. file)
2021-06-06 04:31:19 +00:00
end
2021-06-06 04:39:59 +00:00
fs.move(temp .. file, path .. program .. "/" .. file)
2021-06-06 04:03:09 +00:00
end
end
if customPath ~= nil then
local program = "shell.run(" .. customPath .. "/run.lua)"
2021-06-06 04:39:59 +00:00
file = fs.open(path .. program .. "/run.lua")
2021-06-06 04:03:09 +00:00
file.write(program)
file.close()
end
fs.delete(temp)
end
if programToInstall == nil then
print("no program specified, updating all...")
local programsFile = fs.open(path .. "programs.dat", "r")
local programsText = programsFile.readAll()
local programsTable = split(programsText, "\n")
for _, value in ipairs(programsTable) do
2021-06-06 04:45:52 +00:00
if value ~= "" and value ~= nil then
print("checking for updates for " .. value)
local files = getAllFiles(value)
2021-06-06 04:56:56 +00:00
if #files == 0 then
print("no updates found.")
elseif files ~= nil then
2021-06-06 04:45:52 +00:00
for _, value2 in ipairs(files) do
2021-06-06 04:53:11 +00:00
if value2 ~= nil then
downloadFile(value, value2)
end
2021-06-06 04:45:52 +00:00
end
2021-06-06 04:56:56 +00:00
finishInstall(value, files)
2021-06-06 04:45:52 +00:00
end
2021-06-06 04:03:09 +00:00
end
end
else
2021-06-06 04:39:59 +00:00
print("getting updates for " .. programToInstall .. "...")
local files = getAllFiles(programToInstall)
2021-06-06 04:05:28 +00:00
if #files == 0 then
print("no updates found.")
else
for _, value in ipairs(files) do
2021-06-06 04:53:11 +00:00
if value ~= nil then
downloadFile(programToInstall, value)
end
2021-06-06 04:05:28 +00:00
end
2021-06-06 04:39:59 +00:00
finishInstall(programToInstall, files)
2021-06-06 04:03:09 +00:00
end
end