Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- loader = {}
- loader.url = "http://YOURURL/TOINDEX/"
- loader.auto_update = true
- loader.update_self = false
- loader._name = "loader" --remote name of loader if you set update_self to true
- loader._ver = "0"
- loader._target = "YOURPROJECTORFILETITLE"
- loader._vtable = {}
- loader._ltable = {}
- function loader.getUrl(action, value)
- local url = loader.url
- if (action ~= nil) then
- url = url .."?"..action
- if (value ~=nil) then
- url = url .."="..value
- end
- end
- local response = http.get(url)
- if (response) then
- local cont = response.readAll()
- response.close()
- if (cont ~= nil) then
- return cont
- end
- end
- return false
- end
- function loader.getFile(name)
- local content = loader.getUrl("f", name)
- if (content ~= false) then
- local path = shell.resolve(name)
- if fs.exists( path ) then
- fs.delete( path )
- end
- local file = fs.open(path, "w" )
- file.write(content)
- file.close()
- print("Downloaded "..name)
- if (loader.getVer(name) == false) then
- return false
- end
- return true
- end
- return false
- end
- function loader.getProj(name)
- local content = loader.getUrl("p", name)
- if (fs.isDir(name) == false) then
- fs.makeDir(name)
- end
- if (content ~= false) then
- for file in string.gmatch(content, "[^%s]+") do
- local result = loader.getFile(file)
- if (result == false) then
- print("ERROR: Failed to get "..file)
- loader.quit()
- end
- end
- return tokes
- end
- return false
- end
- function loader.reqVer(name)
- if (loader.auto_update == false) then
- return 0
- end
- content = loader.getUrl('v', name)
- if (content ~= false) then
- return tonumber(content)
- end
- return false
- end
- function loader.getVer(name)
- local ver = loader.reqVer(name)
- if (ver == false) then
- print("ERROR: version request failed for "..name)
- loader.quit()
- end
- local path = shell.resolve(name .. ".ver")
- if fs.exists( path ) then
- fs.delete( path )
- end
- local file = fs.open(path, "w" )
- file.write(ver)
- file.close()
- loader._vtable[name] = ver
- return true
- end
- function loader.newVer(name, ignoreVTable)
- if ((loader._vtable[name] ~= nil) and (ignoreVTable == nil)) then
- return false
- end
- local path = shell.resolve(name..".ver")
- if (fs.exists(path) == false) then
- return true
- elseif loader.auto_update == false then
- return false
- else
- local file = fs.open(path, "r")
- local lVer = file.readLine()
- file.close()
- if (lVer ~= nil) then
- lVer = tonumber(lVer)
- rVer = loader.reqVer(name)
- if (lVer >= rVer) then
- return false
- end
- end
- end
- return true
- end
- function loader.loadProj(name)
- local res = true
- local content = loader.getUrl("p", name)
- print("wat")
- if (fs.isDir(name) == false) then
- fs.makeDir(name)
- end
- if (content ~= false) then
- for file in string.gmatch(content, "[^%s]+") do
- if (loader.loadFile(file) == false) then
- res = false
- end
- end
- else
- res = false
- end
- return res
- end
- function loader.loadFile(name)
- if (loader.newVer(name)) then
- print("Found update for "..name)
- if (loader.getFile(name) == true) then
- local ver = loader.getVer(name)
- if (ver == false) then
- print("ERROR: getVer failed for "..name)
- loader.quit()
- end
- else
- print("ERROR: getFile failed for "..name)
- loader.quit()
- end
- end
- loader._ltable[name] = true
- dofile(name)
- print("Loaded "..name)
- end
- function loader.load(...)
- local args = { ... }
- if #args == 1 then
- if (type(args[1]) == "table") then
- args = args[1]
- end
- end
- if #args >= 1 then
- for i,file in pairs(args) do
- if (loader._ltable[file] == nil) then
- if (loader.loadFile(file) == false) then
- print("ERROR: failed to getFile "..file)
- loader.quit()
- end
- end
- end
- end
- end
- function loader.quit()
- print("Aborting program.")
- os.sleep(3)
- shell.exit()
- end
- function loader.restart()
- print("Rebooting!")
- os.sleep(1)
- os.reboot()
- end
- function req(...)
- local args = { ... }
- loader.load(...)
- end
- --Check dependencies
- print("Watt framework loader v"..loader._ver.." starting...")
- if http == nil then
- print("ERROR: http api not enabled on this server")
- loader.quit()
- end
- if (os.getComputerLabel() == nil) then
- os.setComputerLabel(loader._target)
- end
- if (loader.update_self == true) then
- if (loader.newVer(loader._name) == true) then
- print("New loader version found, upgrading")
- if (loader.getFile(loader._name) == false) then
- print("Failed to get new version")
- loader.restart()
- end
- fs.delete("startup")
- fs.copy(loader._name, "startup")
- loader.restart()
- end
- end
- if (loader.loadProj(loader._target) == false) then
- if (loader.loadFile(loader._target) == false) then
- print("loadProj and loadFile failed for "..loader._target)
- end
- end
- if (start ~= nil) then
- start()
- else
- print("ERROR: No start function found.")
- loader.quit()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement