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

API Dump?

Started by HometownPotato, 18 January 2015 - 10:14 AM
HometownPotato #1
Posted 18 January 2015 - 11:14 AM
Is there a function to dump everything to a file? I don't want to download the website because I don't need all the stuff on it, just a list of all the functions of ComputerCraft. In certain games that allow user-code, they have something like a DumpFuncToXML function that you could call which would write everything to an XML.

I ask this so I can create a parse it into a small and compact couple of HTML files that should work with all versions of CC (assuming a function like this exists and existed). I don't really want to go manually over the wiki since it might change in the future without me knowing.
Edited on 18 January 2015 - 10:22 AM
Exerro #2
Posted 18 January 2015 - 11:23 AM
Don't exactly know what you're trying to achieve here, but there are a couple of things I can suggest regarding writing to files and saving data.

1) The FS API.

Using fs.open( path, "w" ) you can modify the contents of a file by writing strings to it.

2) The Textutils API.

Using textutils.serialize( variable ) you can turn pretty much every Lua datatype into a string that can be converted back using textutils.unserialize().

3) string.dump()

This will return the string representation of a function (in Lua bytecode) that can be converted back to its (near) original state using loadstring().


Let's say you have a table…

local player = {
   name = "foo";
   x = 0;
   y = 0;
   health = 100;
}

You want to save this player table to a file. Using textutils.serialize() you can turn it into a single string.

local str_representation = textutils.serialize( player )

Then you can write the string to a file like this.

local h = fs.open( file, "w" )
h.write( str_representation )
h.close()

If you want to get it back into a table, you need to read the file then unserialize it.

local h = fs.open( file, "r" )
local content = h.readAll()
h.close()
local player = textutils.unserialize( content )
HometownPotato #3
Posted 18 January 2015 - 11:27 AM
I'm wondering if there is an internal function that exists that will generate a list of all the functions available in CC. Not like dumping a function with string.dump or converting back to JSON but like, a way for CC to create a list of all available functions.
Exerro #4
Posted 18 January 2015 - 12:14 PM
Ah, sorry for the long wait.

You can use the pairs() function to iterate through all variables in a table. As _G (the global environment) is actually just a table, you can list all the functions that you have access to.


for k, v in pairs( _G ) do
   -- k is the name, i.e. _G[k]
   -- v is the value
   if type( v ) == "function" then
	  print( k .. " is a function" )
   end
end
HometownPotato #5
Posted 18 January 2015 - 05:30 PM
Oh yeah, for some reason that crossed my mind.
Of course I would have to do a deep scan but thanks :)/>
Exerro #6
Posted 18 January 2015 - 07:32 PM
Just watch out for causing a stack overflow because of looping tables. _G contains itself, so it would keep on looping and iterating through itself. Make sure to track all the tables you've listed and not go through them more than once.