BrainStone

API Startup

Dec 25th, 2015
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.37 KB | None | 0 0
  1. -- BrainStone's APIs autoupdater
  2. -- Version 3.0
  3. --
  4. -- If you need your own startup script name it "startup.custom"
  5. --
  6. -- This script will automatically install and update BrainStone's APIs
  7.  
  8. if not http then
  9.     printError( "Pastebin requires http API" )
  10.     printError( "Set http_enable to true in ComputerCraft.cfg" )
  11.    
  12.     return
  13. end
  14.  
  15. function pastebin(id, name)
  16.   local URI = "http://pastebin.com/raw.php?i=" .. textutils.urlEncode(id)
  17.   local path = "api/" .. name
  18.  
  19.   local response = http.get(URI)
  20.        
  21.   if response then
  22.     local sResponse = response.readAll()
  23.     response.close()
  24.    
  25.     local file = fs.open( path, "w" )
  26.     file.write( sResponse )
  27.     file.close()
  28.   else
  29.     printError( "Failed." )
  30.   end
  31. end
  32.  
  33. function split(str)
  34.   local t = {}
  35.   local function helper(word) table.insert(t, word) return "" end
  36.   if not str:gsub("[%w\\.]+", helper):find"%S" then return t end
  37. end
  38.  
  39. function parseList()
  40.   local list = {}
  41.   local file = fs.open("api/list", "r")
  42.  
  43.   if file ~= nil then
  44.     local line = file.readLine()
  45.     local id, name, version
  46.    
  47.     while line do
  48.       id, name, version = unpack(split(line) or {})
  49.      
  50.       if id ~= nil and id ~= "--" and name ~= nil and version ~= nil then
  51.         list[name] = {id = id, version = version}
  52.       end
  53.      
  54.       line = file.readLine()
  55.     end
  56.    
  57.     file.close()
  58.   end
  59.  
  60.   return list
  61. end
  62.  
  63. if not fs.exists("updateBSAPIs") then
  64.   local handle = fs.open("updateBSAPIs", "w")
  65.  
  66.   handle.writeLine("-- This script makes it easy to update")
  67.   handle.writeLine("-- the BrainStone APIs")
  68.   handle.writeLine("")
  69.   handle.writeLine("shell.run(\"pastebin run V2uDhBFU update\")")
  70.  
  71.   handle.close()
  72. end
  73.  
  74. local oldList = parseList()
  75.  
  76. fs.delete("api/list")
  77.  
  78. print("Fetching API list")
  79. pastebin("H2s81Dd0", "list")
  80.  
  81. local newList = parseList()
  82.  
  83. for name, val in pairs(newList) do
  84.   if
  85.     oldList[name] == nil or
  86.     val.version ~= oldList[name].version or
  87.     not fs.exists("api/" .. name) then
  88.    
  89.     print("Fetching " .. name)
  90.    
  91.     pastebin(val.id, name)
  92.   end
  93.  
  94.   oldList[name] = nil
  95. end
  96.  
  97. for name, _ in pairs(oldList) do
  98.   print("Deleting " .. name)
  99.  
  100.   fs.delete("api/" .. name)
  101. end
  102.  
  103. if fs.exists("startup.custom") then
  104.   shell.run("startup.custom")
  105. end
  106.  
  107. -- DO NOT REMOVE THIS THESE TWO LINES
  108. -- 4024a1f38967b6f8d01e32a34688b43a
Advertisement
Add Comment
Please, Sign In to add comment