SuicidalSTDz

EnderAPI v0.01

Mar 13th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 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. print( "Loaded existent APIs" )
  87. write( "EnderAPI is installed\nChecking for updates\n" )
  88. end
  89.  
  90. --# Check for any available updates, if showAutoUpdate is true
  91. if config.returnVars().shouldAutoUpdate then
  92. local response = http.get( "https://raw.github.com/SuicidalSTDz/EnderAPI/master/config" )
  93. if response then
  94. local sResponse = response.readAll()
  95. response.close()
  96.  
  97. local handle = io.open( "/EnderAPI/master/temp/config", "w" )
  98. handle:write( sResponse )
  99. handle:close()
  100. local oldVersion = config.returnVars().version
  101. os.loadAPI( "/EnderAPI/master/temp/config" )
  102. if config.returnVars().version ~= oldVersion then
  103. --# An update is available, ask user to update ( not yet implemented )
  104. --local bUpdate = false
  105. --messagebox.create( "Download available update?", downloadFiles, function() return nil end )
  106. if bShow then
  107. print( "Update Available!" )
  108. end
  109. else
  110. --# No update is available
  111. if bShow then
  112. print( "No updates avavilable" )
  113. end
  114. end
  115. else
  116. --# Error updating
  117. if bShow then
  118. print( "Error updating" )
  119. end
  120. end
  121. end
  122. else
  123. --# EnderAPI is not installed, download necessary files
  124. downloadFiles( true )
  125. end
  126.  
  127. --# Load EnderAPI
  128. loadAPIs( config.returnVars().showUpdateStatus )
Advertisement
Add Comment
Please, Sign In to add comment