Guest User

usefull code

a guest
Jan 19th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.07 KB | None | 0 0
  1. local function ReadFile(path)
  2.     local f = fs.open(path, "r")
  3.     local data = f.readAll()
  4.     f.close()
  5.     return data
  6. end
  7.  
  8. local function WriteFile(path, data)
  9.     local f = fs.open(path, "w")
  10.     f.write(data)
  11.     f.close()
  12. end
  13.  
  14. local function getpastebin(paste)
  15.     local response = http.get(
  16.         "http://pastebin.com/raw/"..textutils.urlEncode( paste )
  17.     )
  18.        
  19.     if response then
  20.         local sResponse = response.readAll()
  21.         response.close()
  22.         return true, sResponse
  23.     else
  24.         return false
  25.     end
  26. end
  27.  
  28. local function CheckAPI(name, pastebin, required, downloadfilename)
  29.     local path = settings.get(name, nil)
  30.     if path ~= nil then
  31.         if fs.exists(path) then
  32.             os.loadAPI(path)
  33.             return true
  34.         else
  35.             if http then
  36.                 local suc, paste = getpastebin(pastebin)
  37.                 if suc then
  38.                     WriteFile(path, paste)
  39.                     os.loadAPI(path)
  40.                     return true
  41.                 else
  42.                     if required then error(name .. " API not found and was not able to download! specify path to " .. name .. " API in settings, do 'set " .. name .. " <path>' to set path")
  43.                 end
  44.             else
  45.                 if required then error(name .. " API not found and was not able to download! specify path to " .. name .. " API in settings, do 'set " .. name .. " <path>' to set path")
  46.             end
  47.         end
  48.     else
  49.         if fs.exists(name) then
  50.             os.loadAPI(name)
  51.             return true
  52.         else
  53.             if http then
  54.                 local suc, paste = getpastebin(pastebin)
  55.                 if suc then
  56.                     if downloadfilename then
  57.                         WriteFile(downloadfilename, paste)
  58.                         settings.set(name, downloadfilename)
  59.                         os.loadAPI(downloadfilename)
  60.                     else
  61.                         WriteFile(name, paste)
  62.                         os.loadAPI(name)
  63.                     end
  64.                     return true
  65.                 else
  66.                     if required then error(name .. " API not found and was not able to download! specify path to " .. name .. " API in settings, do 'set " .. name .. " <path>' to set path")
  67.                 end
  68.             else
  69.                 if required then error(name .. " API not found and was not able to download! specify path to " .. name .. " API in settings, do 'set " .. name .. " <path>' to set path")
  70.             end
  71.         end
  72.     end
  73.     return false
  74. end
Advertisement
Add Comment
Please, Sign In to add comment