SuicidalSTDz

EnderAPI v2.0.0

Mar 16th, 2014
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.92 KB | None | 0 0
  1. --# USER VARIABLE(S)
  2. local shouldAutoUpdate = true
  3.  
  4. --# Files to grab when updating/installing
  5. local tFiles = {
  6.   "/EnderAPI/master/apis/color",
  7.   "/EnderAPI/master/apis/colour",
  8.   "/EnderAPI/master/apis/number",
  9.   "/EnderAPI/master/apis/pastebin",
  10.   "/EnderAPI/master/apis/text",
  11.   "/EnderAPI/master/apis/messageBox",
  12.   "/EnderAPI/master/apis/http",
  13.   "/EnderAPI/master/config"
  14.   }
  15.  
  16. --# Create directories if they are not present
  17. if not fs.exists( "/.EnderAPI" ) then
  18.   fs.makeDir( "/.EnderAPI" )
  19. end
  20. if not fs.exists( "/.EnderAPI/master" ) then
  21.     fs.makeDir( "/.EnderAPI/master" )
  22. end
  23. if not fs.exists( "/.EnderAPI/master/apis" ) then
  24.     fs.makeDir( "/.EnderAPI/master/apis" )
  25. end
  26. if not fs.exists( "/.EnderAPI/master/temp" ) then
  27.     fs.makeDir( "/.EnderAPI/master/temp" )
  28. end
  29.  
  30. local function downloadFile( url, sFile )
  31.   local response = http.get( url )
  32.   if response then
  33.     local sResponse = response.readAll()
  34.     response.close()
  35.    
  36.     local handle = io.open( sFile, "w" )
  37.     handle:write( sResponse )
  38.     handle:close()
  39.     return true
  40.   else
  41.     return false
  42.   end
  43. end
  44.  
  45. local function loadAPIs()
  46.   local oldLoad = os.loadAPI
  47.   local tAPIsLoading = {}
  48.  
  49.   function os.loadAPI( _sPath )
  50.     local sName = fs.getName( _sPath )
  51.     if tAPIsLoading[sName] == true then
  52.       return false
  53.     end
  54.     tAPIsLoading[sName] = true
  55.      
  56.     local tEnv = {}
  57.     setmetatable( tEnv, { __index = _G } )
  58.     local fnAPI, err = loadfile( _sPath )
  59.     if fnAPI then
  60.       setfenv( fnAPI, tEnv )
  61.       fnAPI()
  62.     else
  63.       tAPIsLoading[sName] = nil
  64.       return false
  65.     end
  66.    
  67.     local tAPI = {}
  68.     for k,v in pairs( tEnv ) do
  69.       tAPI[k] =  v
  70.     end
  71.    
  72.     _G[sName] = tAPI   
  73.     tAPIsLoading[sName] = nil
  74.     return true
  75.   end
  76.  
  77.   os.loadAPI( "/.EnderAPI/master/apis/text" )
  78.   os.loadAPI( "/.EnderAPI/master/apis/number" )
  79.   shell.run( "/.EnderAPI/master/apis/color" )
  80.   shell.run( "/.EnderAPI/master/apis/colour" )
  81.   os.loadAPI( "/.EnderAPI/master/apis/pastebin" )
  82.   os.loadAPI( "/.EnderAPI/masster/apis/messageBox" )
  83.   shell.run( "/.EnderAPI/master/apis/http" )
  84.   --# Still have to test the fs additions
  85.   --shell.run( "/.EnderAPI/master/apis/fs" )
  86.  
  87.   os.loadAPI = oldLoad
  88. end
  89.  
  90. --# Main Update Script
  91. for i = 1, #tFiles do
  92.  
  93.   --# Grab the file and store it's contents in a string to check against later
  94.   local sResponse
  95.   local response = http.get( "https://raw.github.com/SuicidalSTDz" .. tFiles[ i ] )
  96.   if response then
  97.     sResponse = response.readAll()
  98.     response.close()
  99.   end
  100.  
  101.   --# Open the currently installed file to check against the downloaded file
  102.   local sFile = tFiles[ i ]:sub( 1, 1 ) .. "." .. tFiles[ i ]:sub( 2, #tFiles[ i ] )
  103.   if not fs.exists( sFile ) then
  104.     local handle = io.open( sFile, "w" )
  105.     handle:write( "function help() return 'This file was improperly downloaded, please try again later!' end" )
  106.     handle:close()
  107.   end
  108.   local handle = fs.open( sFile, "r" )
  109.   local sData = handle.readAll()
  110.   handle.close()
  111.      
  112.   --# If the file does not exist or doesn't match the downloaded file, then update said file
  113.   if sResponse ~= "" then
  114.     if ( sData ~= sResponse and shouldAutoUpdate ) then
  115.       local handle = io.open( sFile, "w" )
  116.       handle:write( sResponse, "w" )
  117.       handle:close()
  118.     end
  119.   end
  120. end
  121.  
  122. --# Update the launcher, if need be
  123. local sRunning = shell.getRunningProgram()
  124. local response = http.get( "https://raw.github.com/SuicidalSTDz/EnderAPI/master/launcher" )
  125. if response then
  126.   local sResponse = response.readAll()
  127.   response.close()
  128.  
  129.   local handle = fs.open( sRunning, "r" )
  130.   local sData = handle.readAll()
  131.   handle.close()
  132.  
  133.   if sResponse ~= "" then
  134.     if ( sData ~= sResponse and shouldAutoUpdate ) then
  135.       fs.delete( sRunning )
  136.       local handle = io.open( sRunning, "w" )
  137.       handle:write( sResponse )
  138.       handle:close()
  139.     end
  140.   end
  141. end
  142.  
  143. --# Load EnderAPI
  144. loadAPIs()
Advertisement
Add Comment
Please, Sign In to add comment