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

API based plugin system

Started by marczone3, 06 August 2013 - 07:42 AM
marczone3 #1
Posted 06 August 2013 - 09:42 AM
Hello pro's,

I am part of a mapmaking team which, obviously, makes a map based around computercraft. After we playtested some of the code we realised that there might be people who wanted to turn off special features or even add some of there own.

To achieve this we want to implement a API Based plugin system. But we ran into a problem. We are able to load all the plugins as API's by calling a for-loop on a table returned by fs.list("Plugins"). But we donnot know how to call the functions in those API's cause we donnot know there name directly so somelike userAPI.callFunction is not possible.

Is there a way to still call the functions if you only have the name of the API in string form stored in the table that we used to register the API's?

Greets,

Marczone3
theoriginalbit #2
Posted 07 August 2013 - 05:57 PM
There definitely is… you can use the name of this file in conjunction with the global table (where APIs are loaded) to loop through the functions, for example something like this


for funcName, func in pairs( _G["api_name_you_have_here"] ) do
  if type(func) == "function" then
    print( funcName )
  end
end

however then the problem arises that, well, which function do you call? they could have 6 functions, so which would you decide to call…

the suggestion I would make to you (and that I use myself in projects like this, also used in professionally developed software) is to force the developers of the plugins to conform to your standards and implement a required minimum of functions which you call… then you know exactly which functions to call… Then when loading the plugins just make sure the required functions are found, something like this would work


os.loadAPI( "api_name" )
if not _G["api_name"].initialize or type(_G["api_name"].initialize) ~= "function" then
 error("Plugin ".."api_name".." is missing the initialize function")
end
marczone3 #3
Posted 08 August 2013 - 03:49 AM
Thanks did not know about that table.
Yeah i was planning to use an initialize function or something that returns a table with all the functions inside the API and add them to the database so the main functions learn them.

Thanks for your response,

marc