Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# USER VARIABLE(S)
- local shouldAutoUpdate = true
- --# Files to grab when updating/installing
- local tFiles = {
- "/EnderAPI/master/apis/color",
- "/EnderAPI/master/apis/colour",
- "/EnderAPI/master/apis/number",
- "/EnderAPI/master/apis/pastebin",
- "/EnderAPI/master/apis/text",
- "/EnderAPI/master/apis/messageBox",
- "/EnderAPI/master/apis/http",
- "/EnderAPI/master/config"
- }
- --# Create directories if they are not present
- if not fs.exists( "/.EnderAPI" ) then
- fs.makeDir( "/.EnderAPI" )
- end
- if not fs.exists( "/.EnderAPI/master" ) then
- fs.makeDir( "/.EnderAPI/master" )
- end
- if not fs.exists( "/.EnderAPI/master/apis" ) then
- fs.makeDir( "/.EnderAPI/master/apis" )
- end
- if not fs.exists( "/.EnderAPI/master/temp" ) then
- fs.makeDir( "/.EnderAPI/master/temp" )
- end
- local function downloadFile( url, sFile )
- local response = http.get( url )
- if response then
- local sResponse = response.readAll()
- response.close()
- local handle = io.open( sFile, "w" )
- handle:write( sResponse )
- handle:close()
- return true
- else
- return false
- end
- end
- local function loadAPIs()
- local oldLoad = os.loadAPI
- local tAPIsLoading = {}
- function os.loadAPI( _sPath )
- local sName = fs.getName( _sPath )
- if tAPIsLoading[sName] == true then
- return false
- end
- tAPIsLoading[sName] = true
- local tEnv = {}
- setmetatable( tEnv, { __index = _G } )
- local fnAPI, err = loadfile( _sPath )
- if fnAPI then
- setfenv( fnAPI, tEnv )
- fnAPI()
- else
- tAPIsLoading[sName] = nil
- return false
- end
- local tAPI = {}
- for k,v in pairs( tEnv ) do
- tAPI[k] = v
- end
- _G[sName] = tAPI
- tAPIsLoading[sName] = nil
- return true
- end
- os.loadAPI( "/.EnderAPI/master/apis/text" )
- os.loadAPI( "/.EnderAPI/master/apis/number" )
- shell.run( "/.EnderAPI/master/apis/color" )
- shell.run( "/.EnderAPI/master/apis/colour" )
- os.loadAPI( "/.EnderAPI/master/apis/pastebin" )
- os.loadAPI( "/.EnderAPI/masster/apis/messageBox" )
- shell.run( "/.EnderAPI/master/apis/http" )
- --# Still have to test the fs additions
- --shell.run( "/.EnderAPI/master/apis/fs" )
- os.loadAPI = oldLoad
- end
- --# Main Update Script
- for i = 1, #tFiles do
- --# Grab the file and store it's contents in a string to check against later
- local sResponse
- local response = http.get( "https://raw.github.com/SuicidalSTDz" .. tFiles[ i ] )
- if response then
- sResponse = response.readAll()
- response.close()
- end
- --# Open the currently installed file to check against the downloaded file
- local sFile = tFiles[ i ]:sub( 1, 1 ) .. "." .. tFiles[ i ]:sub( 2, #tFiles[ i ] )
- if not fs.exists( sFile ) then
- local handle = io.open( sFile, "w" )
- handle:write( "function help() return 'This file was improperly downloaded, please try again later!' end" )
- handle:close()
- end
- local handle = fs.open( sFile, "r" )
- local sData = handle.readAll()
- handle.close()
- --# If the file does not exist or doesn't match the downloaded file, then update said file
- if sResponse ~= "" then
- if ( sData ~= sResponse and shouldAutoUpdate ) then
- local handle = io.open( sFile, "w" )
- handle:write( sResponse, "w" )
- handle:close()
- end
- end
- end
- --# Update the launcher, if need be
- local sRunning = shell.getRunningProgram()
- local response = http.get( "https://raw.github.com/SuicidalSTDz/EnderAPI/master/launcher" )
- if response then
- local sResponse = response.readAll()
- response.close()
- local handle = fs.open( sRunning, "r" )
- local sData = handle.readAll()
- handle.close()
- if sResponse ~= "" then
- if ( sData ~= sResponse and shouldAutoUpdate ) then
- fs.delete( sRunning )
- local handle = io.open( sRunning, "w" )
- handle:write( sResponse )
- handle:close()
- end
- end
- end
- --# Load EnderAPI
- loadAPIs()
Advertisement
Add Comment
Please, Sign In to add comment