pass program to functions

This commit is contained in:
jane 2021-06-06 00:39:59 -04:00
parent 1d58f51ba4
commit eec87784de
2 changed files with 40 additions and 41 deletions

View file

@ -23,9 +23,9 @@ if #args > 1 then
customPath = shell.resolve(args[2])
end
local function downloadFile(filename)
local function downloadFile(program, filename)
print("downloading " .. filename .. "...")
local fileUrl = url .. "/files/" .. programToInstall .. "/" .. filename
local fileUrl = url .. "/files/" .. program .. "/" .. filename
local result = http.get(fileUrl)
local resultText = result.readAll()
@ -54,12 +54,12 @@ local function split(pString, pPattern)
return Table
end
local function getAllFiles()
local indexesUrl = url .. "/indexes/" .. programToInstall
local function getAllFiles(program)
local indexesUrl = url .. "/indexes/" .. program
local result = http.get(indexesUrl)
if result == nil then
print("error! getting indexes for " .. programToInstall)
print("error! getting indexes for " .. program)
error()
end
local resultText = result.readAll()
@ -70,7 +70,7 @@ local function getAllFiles()
indexesFile.close()
local programsFile = fs.open(path .. "programs.dat", "a")
programsFile.write(programToInstall)
programsFile.write(program)
programsFile.close()
local finalResult = {}
@ -81,7 +81,7 @@ local function getAllFiles()
return finalResult
end
local function finishInstall(files)
local function finishInstall(program, files)
for key, file in ipairs(files) do
if customPath ~= nil then
print("moving " .. file .. " to " .. customPath .. "...")
@ -93,20 +93,20 @@ local function finishInstall(files)
end
fs.move(temp .. file, customPath .. "/" .. file)
else
print("moving " .. file .. " to " .. path .. programToInstall .. "...")
if not fs.exists(path .. programToInstall) then
fs.makeDir(path .. programToInstall)
print("moving " .. file .. " to " .. path .. program .. "...")
if not fs.exists(path .. program) then
fs.makeDir(path .. program)
end
if fs.exists(path .. programToInstall .. "/" .. file) then
fs.delete(path .. programToInstall .. "/" .. file)
if fs.exists(path .. program .. "/" .. file) then
fs.delete(path .. program .. "/" .. file)
end
fs.move(temp .. file, path .. programToInstall .. "/" .. file)
fs.move(temp .. file, path .. program .. "/" .. file)
end
end
if customPath ~= nil then
local program = "shell.run(" .. customPath .. "/run.lua)"
file = fs.open(path .. programToInstall .. "/run.lua")
file = fs.open(path .. program .. "/run.lua")
file.write(program)
file.close()
end
@ -130,11 +130,11 @@ for _, value in ipairs(programsTable) do
end
end
if shouldInstall then
local files = getAllFiles()
local files = getAllFiles(programToInstall)
for _, value in ipairs(files) do
downloadFile(value)
downloadFile(programToInstall, value)
end
finishInstall(files)
finishInstall(programToInstall, files)
elseif fs.exists(path .. "updater/update.lua") then
shell.run(path .. "updater/update.lua " .. programToInstall)
end

View file

@ -19,9 +19,9 @@ if #args > 1 then
customPath = shell.resolve(args[2])
end
local function downloadFile(filename)
local function downloadFile(program, filename)
print("downloading " .. filename .. "...")
local fileUrl = url .. "/files/" .. programToInstall .. "/" .. filename
local fileUrl = url .. "/files/" .. program .. "/" .. filename
local result = http.get(fileUrl)
local resultText = result.readAll()
@ -50,9 +50,9 @@ local function split(pString, pPattern)
return Table
end
local function getAllFiles()
local indexesUrl = url .. "/indexes/" .. programToInstall
local updateUrl = url .. "/updates/" .. programToInstall
local function getAllFiles(program)
local indexesUrl = url .. "/indexes/" .. program
local updateUrl = url .. "/updates/" .. program
local currentFiles = ""
if fs.exists(path .. "indexes.dat") then
@ -63,7 +63,7 @@ local function getAllFiles()
local result = http.post(updateUrl, currentFiles)
if result == nil then
print("error! getting updates for " .. programToInstall)
print("error! getting updates for " .. program)
error()
end
local resultText = result.readAll()
@ -71,7 +71,7 @@ local function getAllFiles()
local result2 = http.get(indexesUrl)
if result2 == nil then
print("error! getting indexes for " .. programToInstall)
print("error! getting indexes for " .. program)
error()
end
local resultText2 = result2.readAll()
@ -86,7 +86,7 @@ local function getAllFiles()
local filename = split(value, " ")[1]
for key2, value2 in ipairs(resultTable) do
local valueTable = split(value2, " ")
local filePath = path .. programToInstall .. "/" .. valueTable[1]
local filePath = path .. program .. "/" .. valueTable[1]
if valueTable[2] == "delete" and fs.exists(filePath) then
fs.delete(filePath)
elseif valueTable[2] == "download" then
@ -98,7 +98,7 @@ local function getAllFiles()
return finalResult
end
local function finishInstall(files)
local function finishInstall(program, files)
for key, file in ipairs(files) do
if customPath ~= nil then
print("moving " .. file .. " to " .. customPath .. "...")
@ -110,20 +110,20 @@ local function finishInstall(files)
end
fs.move(temp .. file, customPath .. "/" .. file)
else
print("moving " .. file .. " to " .. path .. programToInstall .. "...")
if not fs.exists(path .. programToInstall) then
fs.makeDir(path .. programToInstall)
print("moving " .. file .. " to " .. path .. program .. "...")
if not fs.exists(path .. program) then
fs.makeDir(path .. program)
end
if fs.exists(path .. programToInstall .. "/" .. file) then
fs.delete(path .. programToInstall .. "/" .. file)
if fs.exists(path .. program .. "/" .. file) then
fs.delete(path .. program .. "/" .. file)
end
fs.move(temp .. file, path .. programToInstall .. "/" .. file)
fs.move(temp .. file, path .. program .. "/" .. file)
end
end
if customPath ~= nil then
local program = "shell.run(" .. customPath .. "/run.lua)"
file = fs.open(path .. programToInstall .. "/run.lua")
file = fs.open(path .. program .. "/run.lua")
file.write(program)
file.close()
end
@ -137,23 +137,22 @@ if programToInstall == nil then
local programsText = programsFile.readAll()
local programsTable = split(programsText, "\n")
for _, value in ipairs(programsTable) do
programToInstall = value
local files = getAllFiles()
for _, value in ipairs(files) do
downloadFile(value)
local files = getAllFiles(value)
for _, value2 in ipairs(files) do
downloadFile(value, value2)
end
finishInstall(files)
end
else
print("getting updates...")
local files = getAllFiles()
print("getting updates for " .. programToInstall .. "...")
local files = getAllFiles(programToInstall)
if #files == 0 then
print("no updates found.")
else
for _, value in ipairs(files) do
downloadFile(value)
downloadFile(programToInstall, value)
end
finishInstall(files)
finishInstall(programToInstall, files)
end
end