Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Override the terminate event
- local oldPullEvent = os.pullEvent
- os.pullEvent = os.pullEventRaw
- base_url = "http://pause.moddednetwork.net/"
- local get_command_url = base_url .. "api/LOOK_OVER_HERE"
- local cc_id = os.getComputerLabel()
- assistant_name = "core"
- -- lib
- function Split(s, delimiter)
- result = {};
- for match in (s..delimiter):gmatch("(.-)"..delimiter) do
- table.insert(result, match);
- end
- return result;
- end
- local TIMEOUT_SECONDS = 2 -- Adjust this to the desired timeout duration
- function get_files_from_server()
- local function downloadFiles()
- repeat res = http.get(base_url .. "api/coreFileNames", {["Content-Type"] = "application/json"})
- local response = http.post(get_command_url, textutils.serializeJSON({ id = tostring(cc_id), message = "Attempt to connect to API..." }), { ["Content-Type"] = "application/json" })
- until res
- dl_files = res.readAll()
- dl_files = dl_files:sub(2, #dl_files - 1):gsub("\"", "")
- res.close()
- --print(dl_files)
- term.write("autoupdate...")
- for _, name in pairs(Split(dl_files, ",")) do
- local res = http.get(base_url .. assistant_name .. "/" .. name)
- local response = http.post(get_command_url, textutils.serializeJSON({ id = tostring(cc_id), message = "Attempt to obtain file... " .. name }), { ["Content-Type"] = "application/json" })
- if not res then
- local response = http.post(get_command_url, textutils.serializeJSON({ id = tostring(cc_id), message = "Error on file request" }), { ["Content-Type"] = "application/json" })
- print("Error on GET " .. base_url .. assistant_name .. "/" .. name)
- else
- local f = fs.open(name, "w")
- f.write(res.readAll())
- f.close()
- if name == "startup" then
- fs.makeDir("backup")
- fs.delete("backup/startup")
- fs.copy("startup", "backup/startup")
- end
- res.close()
- end
- end
- term.write("complete\n")
- local response = http.post(get_command_url, textutils.serializeJSON({ id = tostring(cc_id), message = "Attempt to obtain files... COMPLETE?" }), { ["Content-Type"] = "application/json" })
- end
- local function downloadAPI()
- -- Attempt to connect to the API (First API request)
- local res_api = nil
- repeat
- res_api = http.get(base_url .. "api/api", {["Content-Type"] = "application/json"})
- local response = http.post(get_command_url, textutils.serializeJSON({ id = tostring(cc_id), message = "Attempt to connect to API..." }), { ["Content-Type"] = "application/json" })
- until res_api -- Continue retrying until we successfully get a response
- -- Try to download the Lua file (Second request)
- local res_api_lua = http.get(base_url .. "api/api.lua")
- local response = http.post(get_command_url, textutils.serializeJSON({ id = tostring(cc_id), message = "Attempt to obtain api.lua file..." }), { ["Content-Type"] = "application/json" })
- -- Handle errors if the Lua file download fails
- if not res_api_lua then
- local response = http.post(get_command_url, textutils.serializeJSON({ id = tostring(cc_id), message = "Error on file request for api.lua" }), { ["Content-Type"] = "application/json" })
- print("Error on GET " .. base_url .. "api/api.lua")
- else
- -- Successfully received the Lua file, save it locally
- local f = fs.open("api.lua", "w")
- f.write(res_api_lua.readAll())
- f.close()
- print("Successfully downloaded api.lua")
- end
- -- Close both response objects
- if res_api then res_api.close() end
- if res_api_lua then res_api_lua.close() end
- end
- local function timeoutTimer()
- sleep(TIMEOUT_SECONDS) -- Wait for the timeout duration
- end
- if parallel.waitForAny(downloadFiles, downloadAPI, timeoutTimer) == 2 then
- -- If the timeoutTimer finishes first, we know the downloadFiles function took too long.
- print("Operation timed out. Moving on...")
- end
- end
- -- autoupdate
- get_files_from_server()
- sleep(0.5)
- shell.run("main.lua")
Advertisement
Add Comment
Please, Sign In to add comment