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

Get list of currently loaded APIs

Started by Mystwing24, 23 April 2013 - 01:36 AM
Mystwing24 #1
Posted 23 April 2013 - 03:36 AM
I'm writing an extension to the OS api, adding a few useful functions. I want to implement a function to determine if an API is currently loaded, like this:

os.loadAPI("/rom/apis/sys")
if sys.apiLoaded("foo") then
foo.bar("foobar", "baz", "apple")
end
I know that os.loadAPI() doesn't do anything if an API is already loaded, so I can just put

os.loadAPI("foo")
at the top of my program, but I would really like to make my code look neater with this. It's not that important, but if anyone knows a way to do this, I would really appreciate it.
Lyqyd #2
Posted 23 April 2013 - 06:37 AM
Split into new topic.

Getting a list of them might be a bit trickier (without modifying ROM), but `if foo then` will determine whether something is occupying the variable "foo", which you could blindly hope is the "foo" API, or you could simply load it at the top of your program.
theoriginalbit #3
Posted 23 April 2013 - 06:46 AM
As Lyqyd said you could modify the ROM… You could also override the os.loadAPI function to keep track of it, but you wont get the ones that were loaded before your program…
But if I was you I would loop through the _G table and any other tables (except _G) that are contained, you can assume is an API.

For example:
SpoilerIf I have a file called 'tester' then run this code in another file it will print out all the loaded APIs including tester

os.loadAPI('tester')

for k,v in pairs(_G) do
  if type(v) == 'table' and k ~= '_G' then
	print(k)
	sleep(0.1)
  end
end
Edited on 23 April 2013 - 04:48 AM