Posted 19 January 2016 - 06:49 PM
BTW this is NOT, NOT an API, add this code directly into your program if you wish to use it (i plan on releasing an easier, safer version in the future with more options)
So i have been using these functions for a while now and they are amazingly usefull: (they can all crash your program, use pcall to be safe)
2: it writes the given data to a file
3: downloads a pastebin paste (only use if http is enabled (if http then getpastebin() end))
4: checks if the path for the given API name is defined in the settings if not it uses the name as the path, if the file exists it loads it as an API, if it doesnt exist it will atempt to download and save the file using the pastebin id, if the path is not found in the settings it will save it the downloaded file to donwloadFileName, but only if specified, otherwise it saves it to the name, returns false on failure and true on succes
sorry for the non tabbing, textboxes on websites dont like tabs
here is the pastebin WITH tabbing: http://pastebin.com/rC6vBux6
please report back if you encouter an error
So i have been using these functions for a while now and they are amazingly usefull: (they can all crash your program, use pcall to be safe)
- string ReadFile(string path)
- nil WriteFile(string path, string data)
- string getpastebin(string pasteid)
- bool CheckAPI(string name, string pasteID, bool required, string downloadFilename)
2: it writes the given data to a file
3: downloads a pastebin paste (only use if http is enabled (if http then getpastebin() end))
4: checks if the path for the given API name is defined in the settings if not it uses the name as the path, if the file exists it loads it as an API, if it doesnt exist it will atempt to download and save the file using the pastebin id, if the path is not found in the settings it will save it the downloaded file to donwloadFileName, but only if specified, otherwise it saves it to the name, returns false on failure and true on succes
local function ReadFile(path)
local f = fs.open(path, "r")
local data = f.readAll()
f.close()
return data
end
local function WriteFile(path, data)
local f = fs.open(path, "w")
f.write(data)
f.close()
end
local function getpastebin(paste)
local response = http.get(
"http://pastebin.com/raw/"..textutils.urlEncode( paste )
)
if response then
local sResponse = response.readAll()
response.close()
return true, sResponse
else
return false
end
end
local function CheckAPI(name, pastebin, required, downloadfilename)
local path = settings.get(name, nil)
if path ~= nil then
if fs.exists(path) then
os.loadAPI(path)
return true
else
if http then
local suc, paste = getpastebin(pastebin)
if suc then
WriteFile(path, paste)
os.loadAPI(path)
return true
else
print("Failed to download")
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") end
end
else
print("Http disabled")
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") end
end
end
else
print("Settings for API path not found")
if fs.exists(name) then
os.loadAPI(name)
return true
else
if http then
local suc, paste = getpastebin(pastebin)
if suc then
if downloadfilename then
WriteFile(downloadfilename, paste)
settings.set(name, downloadfilename)
os.loadAPI(downloadfilename)
else
WriteFile(name, paste)
os.loadAPI(name)
end
return true
else
print("Failed to download")
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") end
end
else
print("Http disabled")
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") end
end
end
end
return false
end
sorry for the non tabbing, textboxes on websites dont like tabs
here is the pastebin WITH tabbing: http://pastebin.com/rC6vBux6
please report back if you encouter an error
Edited on 21 January 2016 - 10:41 AM