SuicidalSTDz

EnderAPI v0.1

Mar 14th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.09 KB | None | 0 0
  1. --# Files to grab when updating/installing
  2. local tFiles = {
  3.   "/EnderAPI/master/apis/color",
  4.   "/EnderAPI/master/apis/colour",
  5.   "/EnderAPI/master/apis/number",
  6.   "/EnderAPI/master/apis/pastebin",
  7.   "/EnderAPI/master/apis/text",
  8.   "/EnderAPI/master/apis/messageBox",
  9.   "/EnderAPI/master/config"
  10.   }
  11.  
  12. --# Create directories if they are not present
  13. if not fs.exists( "/EnderAPI" ) then
  14.   fs.makeDir( "/EnderAPI" )
  15. end
  16. if not fs.exists( "/EnderAPI/master" ) then
  17.     fs.makeDir( "/EnderAPI/master" )
  18. end
  19. if not fs.exists( "/EnderAPI/master/apis" ) then
  20.     fs.makeDir( "/EnderAPI/master/apis" )
  21. end
  22. if not fs.exists( "/EnderAPI/master/temp" ) then
  23.     fs.makeDir( "/EnderAPI/master/temp" )
  24. end
  25.  
  26. local function downloadFiles( bShow )
  27.     for i = 1, #tFiles do
  28.         local response = http.get( "https://raw.github.com/SuicidalSTDz" .. tFiles[ i ] )
  29.         if response then
  30.             local sResponse = response.readAll()
  31.             response.close()
  32.            
  33.             local handle = fs.open( tFiles[ i ], "w" )
  34.             handle.write( sResponse )
  35.             handle.close()
  36.       --# Successfully downloaded file
  37.       if bShow then
  38.         print( "Downloaded " .. tFiles[ i ] )
  39.       end
  40.         else
  41.       --# Error downloading file
  42.             if bShow then
  43.         print( "Failed to download " .. tFiles[ i ] )
  44.       end
  45.         end
  46.     end
  47. end
  48.  
  49. local function loadAPIs( bShow )
  50.   if bShow then
  51.     print( "Loading APIs.." )
  52.   end
  53.   local tAPIs = {
  54.   TextAPI = { success = os.loadAPI( "/EnderAPI/master/apis/text" ) },
  55.   NumberAPI = { success = os.loadAPI( "/EnderAPI/master/apis/number" ) },
  56.   ColorAPI = { success = os.loadAPI( "/EnderAPI/master/apis/color" ) },
  57.   ColourAPI = { success = os.loadAPI( "/EnderAPI/master/apis/colour" ) },
  58.   PastebinAPI = { success = os.loadAPI( "/EnderAPI/master/apis/pastebin" ) },
  59.   MessageBoxAPI = { success = os.loadAPI( "/EnderAPI/master/apis/messageBox" ) }
  60.   }
  61.   for k, v in pairs( tAPIs ) do
  62.     if not v.success then
  63.       if bShow then
  64.         printError( "Failed to load " .. tostring( k ) )
  65.       end
  66.     else
  67.       if bShow then
  68.         term.setTextColour( colours.lime )
  69.         print( "Loaded " .. tostring( k ) )
  70.       end
  71.     end
  72.   end
  73.   if bShow then
  74.     term.setTextColour( colours.white )
  75.   end
  76. end
  77.  
  78. --# Main Update Script
  79. if fs.exists( "/EnderAPI/master/config" ) then
  80.  
  81.     --# EnderAPI is installed (at least enough to download core files and load APIs)
  82.   --# Load the EnderAPI config file
  83.   os.loadAPI( "/EnderAPI/master/config" )
  84.   local bShow = config.returnVars().showUpdateStatus
  85.   if bShow then
  86.     if config.returnVars().shouldAutoUpdate then
  87.       write( "EnderAPI is installed\nChecking for updates\n" )
  88.     end
  89.   end
  90.  
  91.   --# Check for any available updates, if showAutoUpdate is true
  92.   if config.returnVars().shouldAutoUpdate then
  93.     local response = http.get( "https://raw.github.com/SuicidalSTDz/EnderAPI/master/config" )
  94.     if response then
  95.       local sResponse = response.readAll()
  96.       response.close()
  97.      
  98.       local handle = io.open( "/EnderAPI/master/temp/config", "w" )
  99.       handle:write( sResponse )
  100.       handle:close()
  101.       local oldVersion = config.returnVars().version
  102.       os.loadAPI( "/EnderAPI/master/temp/config" )
  103.       if config.returnVars().version ~= oldVersion then
  104.         --# An update is available, ask user to update ( not yet implemented )
  105.         write( "An update is available!\nDownload Update? Y/N?\n" )
  106.         local sInput = read():lower()
  107.         if sInput == ( "yes" or "y" ) then
  108.           if bShow then
  109.             print( "Updating.." )
  110.           end
  111.           downloadFiles( bShow )
  112.         else
  113.           if bShow then
  114.             print( "Skipping update" )
  115.           end
  116.         end
  117.       else
  118.         --# No update is available
  119.         if bShow then
  120.           print( "No updates avavilable" )
  121.         end
  122.       end
  123.     else
  124.       --# Error updating
  125.       if bShow then
  126.         print( "Error updating" )
  127.       end
  128.     end
  129.   end
  130. else
  131.     --# EnderAPI is not installed, download necessary files
  132.     downloadFiles( true )
  133. end
  134.  
  135. --# Load EnderAPI
  136. os.loadAPI( "/EnderAPI/master/config" )
  137. loadAPIs( config.returnVars().showUpdateStatus )
Advertisement
Add Comment
Please, Sign In to add comment