Auto update
This commit is contained in:
@ -72,8 +72,8 @@ config.logFile = "stargate.log"
|
||||
-- AUTO-UPDATE
|
||||
---------------------------------------------
|
||||
|
||||
-- Auto-update addresses file from git repository
|
||||
config.autoUpdateAddresses = true
|
||||
config.addressesRepoUrl = "https://git.munebase.dev/Munelit/StargateControl/raw/branch/master/addresses.lua"
|
||||
-- Auto-update all program files from git repository on startup
|
||||
config.autoUpdate = true
|
||||
config.repoBaseUrl = "https://git.munebase.dev/Munelit/StargateControl/raw/branch/master/"
|
||||
|
||||
return config
|
||||
|
||||
92
startup.lua
92
startup.lua
@ -17,37 +17,81 @@
|
||||
|
||||
local config = require("config")
|
||||
|
||||
-- Auto-update addresses file if enabled
|
||||
local function updateAddressesFile()
|
||||
if config.autoUpdateAddresses and http then
|
||||
print("Checking for addresses file update...")
|
||||
local response = http.get(config.addressesRepoUrl)
|
||||
if response then
|
||||
local content = response.readAll()
|
||||
response.close()
|
||||
---------------------------------------------
|
||||
-- AUTO-UPDATE SYSTEM
|
||||
---------------------------------------------
|
||||
|
||||
local file = fs.open("addresses.lua", "w")
|
||||
if file then
|
||||
file.write(content)
|
||||
file.close()
|
||||
print("Addresses file updated successfully")
|
||||
return true
|
||||
else
|
||||
print("Failed to write addresses file")
|
||||
local function autoUpdate()
|
||||
if not (config.autoUpdate and http) then
|
||||
return false
|
||||
end
|
||||
|
||||
print("Checking for program updates...")
|
||||
|
||||
local filesToUpdate = {
|
||||
"startup.lua",
|
||||
"addresses.lua",
|
||||
"utils.lua",
|
||||
"display.lua",
|
||||
"events.lua",
|
||||
"handlers.lua"
|
||||
}
|
||||
|
||||
local updated = false
|
||||
|
||||
for _, filename in ipairs(filesToUpdate) do
|
||||
local url = config.repoBaseUrl .. filename
|
||||
local response = http.get(url)
|
||||
|
||||
if response then
|
||||
local newContent = response.readAll()
|
||||
response.close()
|
||||
|
||||
-- Check if file exists and compare content
|
||||
local needsUpdate = true
|
||||
if fs.exists(filename) then
|
||||
local file = fs.open(filename, "r")
|
||||
if file then
|
||||
local currentContent = file.readAll()
|
||||
file.close()
|
||||
|
||||
-- Only update if content is different
|
||||
if currentContent == newContent then
|
||||
needsUpdate = false
|
||||
print(" [SKIP] " .. filename .. " (unchanged)")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if needsUpdate then
|
||||
-- Delete old file if it exists
|
||||
if fs.exists(filename) then
|
||||
fs.delete(filename)
|
||||
end
|
||||
|
||||
local file = fs.open(filename, "w")
|
||||
if file then
|
||||
file.write(newContent)
|
||||
file.close()
|
||||
print(" [UPDATE] " .. filename)
|
||||
updated = true
|
||||
else
|
||||
print(" [FAIL] Could not write " .. filename)
|
||||
end
|
||||
end
|
||||
else
|
||||
print("Failed to download addresses file - using local version")
|
||||
print(" [SKIP] Could not download " .. filename)
|
||||
end
|
||||
end
|
||||
return false
|
||||
|
||||
return updated
|
||||
end
|
||||
|
||||
-- Try to update addresses file
|
||||
local addressesUpdated = updateAddressesFile()
|
||||
|
||||
-- Reload addresses module if updated
|
||||
if addressesUpdated then
|
||||
package.loaded["addresses"] = nil
|
||||
-- Perform auto-update
|
||||
if autoUpdate() then
|
||||
print("Program updated! Restarting...")
|
||||
sleep(2)
|
||||
os.reboot()
|
||||
end
|
||||
|
||||
local addresses = require("addresses")
|
||||
|
||||
Reference in New Issue
Block a user