1
0
Fork 0
fgdata/Nasal/addons.nas
Florent Rougon c17d9377f8 Add-ons: more specific file names for the interface between FG core and add-ons
The add-on framework now uses the following files in each add-on
directory:
  - addon-config.xml (previously: config.xml)
  - addon-main.nas   (previously: main.nas)

This is consistent with the addon-metadata.xml file that is already part
of the interface between FG core and add-ons. The goal is to make it
clearer, when browsing an add-on directory, which files belong to the
"FG core <-> add-on" interface and which files belong to the add-on
proper. This will be beneficial also when more files are added to the
"FG core <-> add-on" interface, such as possibly addon-events.xml in the
future.

This change is incompatible, thus it is the right time to do *before*
2018.2.1 is out, especially considering that this upcoming release
already has incompatible changes in the add-on API, namely the
requirement of the addon-metadata.xml file and the type of the argument
passed to each add-on's main() function. We'll try harder not to break
compatibility in the add-on API once 2018.2.1 is out. For now, it is
still a good time to try to get the API as clean as possible.
2018-02-10 19:50:00 +01:00

50 lines
2.1 KiB
Text

##
# Initialize addons configured with --addon=foobar command line switch:
# - get the list of registered add-ons
# - load the addon-main.nas file of each add-on into namespace
# __addon[ADDON_ID]__
# - call function main() from every such addon-main.nas with the add-on ghost
# as argument (an addons.Addon instance).
# Example:
#
# fgfs --addon=/foo/bar/baz
#
# - AddonManager.cxx parses /foo/bar/baz/addon-metadata.xml
# - AddonManager.cxx creates prop nodes under /addons containing add-on metadata
# - AddonManager.cxx loads /foo/bar/baz/addon-config.xml into the Property Tree
# - 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/addon-main.nas into namespace
# __addon[ADDON_ID]__
# - this script calls main(addonGhost) from /foo/bar/baz/addon-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.
var id = _setlistener("/sim/signals/fdm-initialized", func {
removelistener(id);
foreach (var addon; addons.registeredAddons()) {
var main_nas = addon.basePath ~ "/addon-main.nas";
var namespace = "__addon" ~ "[" ~ addon.id ~ "]__";
logprint(5, "Initializing addon '" ~ addon.name ~
"' version " ~ addon.version.str() ~ " from " ~ main_nas ~
" in " ~ namespace);
io.load_nasal( main_nas, namespace );
var addon_main = globals[namespace]["main"];
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.
addon.node.getChild("loaded", 0, 1).setBoolValue(1);
}
})