1
0
Fork 0

Add-ons: pass the addons.Addon ghost to main() instead of the base path

The addons.Addon instance (ghost) is much more interesting than its base
path. The base path is immediately accessible from the addons.Addon
instance using its 'basePath' attribute. The addons.Addon instance
allows main() to easily access essentially all of the add-on metadata,
including the add-on identifier, which makes it possible to write
main.nas without hardcoding the add-on id at all.

This is an incompatible change of course, so better do it now than
later.
This commit is contained in:
Florent Rougon 2018-01-08 09:49:27 +01:00
parent 73424c1791
commit a57aed195e
2 changed files with 19 additions and 8 deletions

View file

@ -41,9 +41,11 @@ add-on directory. Such a directory, when used as the argument of
4) The add-on directory must contain a Nasal file called main.nas.
This file will be loaded at startup too, and its main() function
run in the namespace __addon[ADDON_ID]__, where ADDON_ID is the
add-on identifier specified in the addon-metadata.xml file. This
operation is done by $FG_ROOT/Nasal/addons.nas at the time of this
writing.
add-on identifier specified in the addon-metadata.xml file. The
main() function is passed one argument: the addons.Addon object
(a Nasal ghost, see below) corresponding to the add-on being
loaded. This operation is done by $FG_ROOT/Nasal/addons.nas at the
time of this writing.
Also, the Property Tree is populated (under /addons) with information
about registered add-ons. More details will be given below.
@ -459,11 +461,11 @@ code easy access to add-on metadata, for instance like this:
print(myAddon.version.str());
foreach (var author; myAddon.authors) {
print(author.name, author.email, author.url);
print(author.name, " ", author.email, " ", author.url);
}
foreach (var maintainer; myAddon.maintainers) {
print(maintainer.name, maintainer.email, maintainer.url);
print(maintainer.name, " ", maintainer.email, " ", maintainer.url);
}
print(myAddon.shortDescription);

View file

@ -2,7 +2,8 @@
# Initialize addons configured with --addon=foobar command line switch:
# - get the list of registered add-ons
# - load the main.nas file of each add-on into namespace __addon[ADDON_ID]__
# - call function main() from every such main.nas with the add-on path as arg.
# - call function main() from every such main.nas with the add-on ghost as
# argument (an addons.Addon instance).
# Example:
#
@ -14,7 +15,15 @@
# - AddonManager.cxx adds /foo/bar/baz to the list of aircraft paths (to get
# permissions to read files from there)
# - this script loads /foo/bar/baz/main.nas into namespace __addon[ADDON_ID]__
# - this script calls main("/foo/bar/baz") from /foo/bar/baz/main.nas.
# - this script calls main(addonGhost) from /foo/bar/baz/main.nas.
# - the add-on ghost can be used to retrieve most of the add-on metadata, for
# instance:
# addonGhost.id the add-on identifier
# addonGhost.name the add-on name
# addonGhost.version.str() the add-on version as a string
# addonGhost.basePath the add-on base path (realpath() of
# "/foo/bar/baz" here)
# etc.
#
# For more details, see $FG_ROOT/Docs/README.add-ons.
@ -30,7 +39,7 @@ var id = _setlistener("/sim/signals/fdm-initialized", func {
io.load_nasal( main_nas, namespace );
var addon_main = globals[namespace]["main"];
var addon_main_args = [ addon.basePath ];
var addon_main_args = [ addon ];
call(addon_main, addon_main_args); #, object, namespace, error_vector);
# Tell the world that the add-on is now loaded.