This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
wilcomega's profile picture

[Snippet] API Loading and File functions

Started by wilcomega, 19 January 2016 - 05:49 PM
wilcomega #1
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)
  1. string ReadFile(string path)
  2. nil WriteFile(string path, string data)
  3. string getpastebin(string pasteid)
  4. bool CheckAPI(string name, string pasteID, bool required, string downloadFilename)
1: it reads the file and gives back the contents
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
Dragon53535 #2
Posted 20 January 2016 - 11:36 PM
I'm going to give some advice:

1: If someone tries to load your file as an API, they will not be able to use it. Because all of your functions are local, and only globals are usable by API calls. I'm only mentioning this because I find this as more of a noticeable thing, since we are in the API and utilities section, and people might try to use it as an API. If it's not an API, I would just state so up at the top.


2: If I were to be the one to write these, I would specifically make them not able to crash a users program at all.
It doesn't take much to put in a check on your own, and then if that check fails, return nil. That way, if the user decides to use your function, they don't have to pcall it and only really care if it didn't work in the first place.

If you're going to write some simplistically stupid functions that any competent programmer can make, (And know how to do so in a way that doesn't crash their program), make them safe to use so that they don't have to worry.
For your read function, a simple if statement at the top:

if fs.exists(path) and not fs.isDir(path) then
 --# Read
else
  return nil
end
For the rest, that's up to you.

3: I will never use something that I can make better, faster, and more user friendly. Make this something that people will WANT to use, and they will as long as it is easy to use. Look at the touchpoint API, it's really easy to setup, easy to understand, and it works straight from the start. If I were to use it wrong, it would cleanly tell me that, and I would know exactly what went wrong and how to change it.

I'm not saying that you have to make your code super complex or anything, just make sure that someone can pickup and use your code without having to worry about it going wrong through some random error they're not prepared for.
Lupus590 #3
Posted 21 January 2016 - 09:40 AM
instead of returning nil I would throw an error and complain about bad arguments (where appropriate, some errors may be better handled by returning nil)
Edited on 21 January 2016 - 12:39 PM
wilcomega #4
Posted 21 January 2016 - 11:35 AM
i just wanted to throw these out there as someone might find them usefull, and i do appreciate the feedback, i might release a safe version of this later that people can just use, but for now this will be it.

OP edit: added instructions for adding it directly to the code of the program
LDDestroier #5
Posted 21 January 2016 - 01:44 PM
If you want it as an API (which I recommend), then remove the 'local's from the functions that the program is supposed to use.
wilcomega #6
Posted 21 January 2016 - 04:22 PM
its not supposed to be an api, because then you cant use the CheckAPI function to load the api, you see you add this to your code to be able to simply execute one function that attemtps to locate, get and load an API your program needs, i mean you wouldnt add a 3000 line API to your code, but this is just 4 functions that you can quickly implement and use to make sure your dependencies are loaded