2020-01-13 14:45:44 +00:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
# modules.nas - Nasal module helper for Add-ons and re-loadable modules
|
|
|
|
# author: jsb
|
|
|
|
# created: 12/2019
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
# modules.nas allowes to load and unload Nasal modules at runtime (e.g. without
|
2020-04-07 20:46:04 +00:00
|
|
|
# restarting Flightgear as a whole). It implements resource tracking for
|
|
|
|
# setlistener and maketimer to make unloading easier.
|
2020-01-13 14:45:44 +00:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
# Example - generic module load:
|
2020-04-07 20:46:04 +00:00
|
|
|
#
|
2020-01-13 14:45:44 +00:00
|
|
|
# if (modules.isAvailable("foo_bar")) {
|
|
|
|
# modules.load("foo_bar");
|
|
|
|
# }
|
2020-04-07 20:46:04 +00:00
|
|
|
#
|
|
|
|
# Example - create an aircraft nasal system as module
|
2020-01-13 14:45:44 +00:00
|
|
|
# (e.g. for rapid reload while development)
|
|
|
|
#
|
|
|
|
# var my_foo_sys = modules.Module.new("my_aircraft_foo");
|
|
|
|
# my_foo_sys.setDebug(1);
|
|
|
|
# my_foo_sys.setFilePath(getprop("/sim/aircraft-dir")~"/Nasal");
|
|
|
|
# my_foo_sys.setMainFile("foo.nas");
|
|
|
|
# my_foo_sys.load();
|
|
|
|
#-------------------------------------------------------------------------------
|
2020-02-23 18:39:42 +00:00
|
|
|
var MODULES_DIR = getprop("/sim/fg-root")~"/Nasal/modules/";
|
2020-01-13 14:45:44 +00:00
|
|
|
var MODULES_NODE = nil;
|
2020-04-07 20:46:04 +00:00
|
|
|
var MODULES_DEFAULT_FILENAME = "main.nas";
|
2020-01-13 14:45:44 +00:00
|
|
|
var _modules_available = {};
|
|
|
|
|
2020-02-23 18:39:42 +00:00
|
|
|
# Hash storing Module objects; keep this outside Module to avoid stack overflow
|
|
|
|
# when using debug.dump
|
2020-04-07 20:46:04 +00:00
|
|
|
var _instances = {};
|
2020-02-23 18:39:42 +00:00
|
|
|
|
|
|
|
# Class Module
|
|
|
|
# to handle a re-loadable Nasal module at runtime
|
2020-01-13 14:45:44 +00:00
|
|
|
var Module = {
|
|
|
|
_orig_setlistener: setlistener,
|
|
|
|
_orig_maketimer: maketimer,
|
|
|
|
_orig_settimer: settimer,
|
2020-04-07 20:46:04 +00:00
|
|
|
|
2020-01-13 14:45:44 +00:00
|
|
|
# id: must be a string without special characters or spaces
|
|
|
|
# ns: optional namespace name
|
|
|
|
# node: optional property node for module management
|
|
|
|
new: func(id, ns = "", node = nil) {
|
2020-02-23 18:39:42 +00:00
|
|
|
if (!id or typeof(id) != "scalar") {
|
2020-04-07 20:46:04 +00:00
|
|
|
logprint(LOG_ALERT, "Module.new(): id: must be a string without special characters or spaces");
|
2020-02-23 18:39:42 +00:00
|
|
|
return;
|
2020-01-13 14:45:44 +00:00
|
|
|
}
|
|
|
|
if (_instances[id] != nil) {
|
|
|
|
return _instances[id];
|
|
|
|
}
|
2020-04-07 20:46:04 +00:00
|
|
|
var obj = {
|
2020-01-13 14:45:44 +00:00
|
|
|
parents: [me],
|
|
|
|
_listeners: [],
|
|
|
|
_timers: [],
|
|
|
|
_debug: 0,
|
2020-04-24 13:52:35 +00:00
|
|
|
_setlistener_runtime_default: 1,
|
2020-01-13 14:45:44 +00:00
|
|
|
id: id,
|
|
|
|
version: 1,
|
|
|
|
file_path: MODULES_DIR,
|
2020-04-07 20:46:04 +00:00
|
|
|
main_file: MODULES_DEFAULT_FILENAME,
|
2020-01-13 14:45:44 +00:00
|
|
|
namespace: ns ? ns : id,
|
|
|
|
node: nil,
|
|
|
|
};
|
|
|
|
if (isa(node, props.Node)) {
|
|
|
|
obj.node = node
|
|
|
|
} else {
|
2020-04-07 20:46:04 +00:00
|
|
|
obj.node = MODULES_NODE.getNode(id, 1);
|
2020-01-13 14:45:44 +00:00
|
|
|
}
|
2020-04-07 20:46:04 +00:00
|
|
|
|
2020-01-13 14:45:44 +00:00
|
|
|
obj.loadedN = obj.node.initNode("loaded", 0, "BOOL");
|
|
|
|
obj.lcountN = obj.node.initNode("listeners", 0, "INT");
|
|
|
|
obj.tcountN = obj.node.initNode("timers", 0, "INT");
|
|
|
|
obj.lhitN = obj.node.initNode("listener-hits", 0, "INT");
|
2020-04-07 20:46:04 +00:00
|
|
|
|
2020-02-02 22:40:13 +00:00
|
|
|
_instances[id] = obj;
|
2020-01-13 14:45:44 +00:00
|
|
|
return obj;
|
|
|
|
},
|
2020-04-07 20:46:04 +00:00
|
|
|
|
2020-01-13 14:45:44 +00:00
|
|
|
getNode: func { return me.node; },
|
|
|
|
getNamespaceName: func { return me.namespace; },
|
|
|
|
getNamespace: func { return globals[me.namespace]; },
|
|
|
|
getFilePath: func { return me.file_path; },
|
2020-04-07 20:46:04 +00:00
|
|
|
|
2020-01-13 14:45:44 +00:00
|
|
|
#return variable from module namespace
|
|
|
|
get: func(var_name) {
|
|
|
|
return globals[me.namespace][var_name];
|
|
|
|
},
|
2020-04-07 20:46:04 +00:00
|
|
|
|
2020-01-13 14:45:44 +00:00
|
|
|
setDebug: func (debug = 1) {
|
|
|
|
me._debug = debug;
|
2020-04-07 20:46:04 +00:00
|
|
|
logprint(DEV_WARN, "Module "~me.id~" debug = "~debug);
|
2020-01-13 14:45:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setFilePath: func(path) {
|
|
|
|
if (io.is_directory(path)) {
|
|
|
|
if (substr(path, -1) != "/")
|
|
|
|
path ~= "/";
|
|
|
|
me.file_path = path;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
},
|
2020-04-07 20:46:04 +00:00
|
|
|
|
2020-01-13 14:45:44 +00:00
|
|
|
setMainFile: func(filename) {
|
|
|
|
if (typeof(filename) == "scalar") {
|
|
|
|
me.main_file = filename;
|
|
|
|
}
|
|
|
|
else {
|
2020-04-07 20:46:04 +00:00
|
|
|
logprint(LOG_WARN, "setMainFile() needs a string parameter");
|
2020-01-13 14:45:44 +00:00
|
|
|
}
|
|
|
|
},
|
2020-04-07 20:46:04 +00:00
|
|
|
|
2020-01-13 14:45:44 +00:00
|
|
|
setNamespace: func(ns) {
|
|
|
|
if (typeof(ns) == "scalar") {
|
|
|
|
me.namespace = ns;
|
|
|
|
}
|
|
|
|
else {
|
2020-04-07 20:46:04 +00:00
|
|
|
logprint(LOG_WARN, "setNamespace() needs a string parameter");
|
2020-01-13 14:45:44 +00:00
|
|
|
}
|
|
|
|
},
|
2020-04-07 20:46:04 +00:00
|
|
|
|
2020-04-24 13:52:35 +00:00
|
|
|
# to change the default setlistener behaviour regarding 'runtime' argument
|
|
|
|
# i: int 0..2 passed to setlistener as 4th parameter if not specified explicitly
|
|
|
|
setlistenerRuntimeDefault: func (i) {
|
|
|
|
me._setlistener_runtime_default = int(i);
|
|
|
|
},
|
|
|
|
|
2020-04-07 20:46:04 +00:00
|
|
|
# load module
|
2020-01-13 14:45:44 +00:00
|
|
|
# if no arguments are given, the Module object will be passed to main()
|
|
|
|
load: func(myargs...) {
|
|
|
|
me.loadedN.setBoolValue(0);
|
|
|
|
if (globals[me.namespace] == nil) {
|
|
|
|
globals[me.namespace] = {};
|
|
|
|
}
|
2020-04-07 20:46:04 +00:00
|
|
|
logprint(LOG_INFO, "Module.load() ", me.id);
|
2020-02-02 22:40:13 +00:00
|
|
|
me.lcountN.setIntValue(0);
|
|
|
|
me.tcountN.setIntValue(0);
|
|
|
|
me.lhitN.setIntValue(0);
|
2020-01-13 14:45:44 +00:00
|
|
|
me._redirect_setlistener();
|
|
|
|
me._redirect_maketimer();
|
|
|
|
me._redirect_settimer();
|
2020-04-07 20:46:04 +00:00
|
|
|
|
2020-01-13 14:45:44 +00:00
|
|
|
var filename = me.file_path~"/"~me.main_file;
|
|
|
|
if (io.load_nasal(filename, me.namespace)) {
|
|
|
|
var main = globals[me.namespace]["main"];
|
|
|
|
if (typeof(main) == "func") {
|
|
|
|
var module_args = [];
|
|
|
|
if (size(myargs) == 0) module_args = [me];
|
|
|
|
else module_args = myargs;
|
|
|
|
var errors = [];
|
|
|
|
call(main, module_args, errors);
|
|
|
|
if (size(errors)) {
|
|
|
|
debug.printerror(errors);
|
|
|
|
} else {
|
|
|
|
me.loadedN.setBoolValue(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
me.loadedN.setBoolValue(1);
|
|
|
|
}
|
|
|
|
return me;
|
2020-04-07 20:46:04 +00:00
|
|
|
}
|
|
|
|
else { # loading failed
|
|
|
|
return nil;
|
|
|
|
}
|
2020-01-13 14:45:44 +00:00
|
|
|
},
|
2020-04-07 20:46:04 +00:00
|
|
|
|
|
|
|
# unload a module and remove its tracked resources
|
2020-01-13 14:45:44 +00:00
|
|
|
unload: func() {
|
|
|
|
if (!me.loadedN.getValue()) {
|
2020-04-07 20:46:04 +00:00
|
|
|
logprint(DEV_ALERT, "! ", me.id, " was not fully loaded.");
|
2020-01-13 14:45:44 +00:00
|
|
|
}
|
|
|
|
if (globals[me.namespace] != nil
|
2020-04-07 20:46:04 +00:00
|
|
|
and typeof(globals[me.namespace]) == "hash")
|
2020-01-13 14:45:44 +00:00
|
|
|
{
|
2020-04-07 20:46:04 +00:00
|
|
|
logprint(LOG_INFO, "- Removing module ", me.id);
|
2020-01-13 14:45:44 +00:00
|
|
|
if (globals[me.namespace]["setlistener"] != nil)
|
|
|
|
globals[me.namespace]["setlistener"] = func {};
|
|
|
|
foreach (var id; me._listeners) {
|
2020-04-07 20:46:04 +00:00
|
|
|
logprint(DEV_WARN, "Removing listener "~id);
|
2020-02-02 22:40:13 +00:00
|
|
|
if (removelistener(id)) {
|
|
|
|
me.lcountN.setValue(me.lcountN.getValue() - 1);
|
|
|
|
}
|
2020-01-13 14:45:44 +00:00
|
|
|
}
|
|
|
|
me._listeners = [];
|
|
|
|
|
2020-04-07 20:46:04 +00:00
|
|
|
logprint(LOG_INFO, "Stopping timers ");
|
2020-01-13 14:45:44 +00:00
|
|
|
if (globals[me.namespace]["maketimer"] != nil)
|
|
|
|
globals[me.namespace]["maketimer"] = func {};
|
|
|
|
foreach (var t; me._timers) {
|
|
|
|
if (typeof(t.stop) == "func") {
|
|
|
|
t.stop();
|
2020-02-02 22:40:13 +00:00
|
|
|
me.tcountN.setValue(me.tcountN.getValue() - 1);
|
2020-04-07 20:46:04 +00:00
|
|
|
logprint(DEV_WARN, " .");
|
2020-01-13 14:45:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
me._timers = [];
|
|
|
|
|
|
|
|
# call clean up method if available
|
|
|
|
# module shall release resources not handled by this framework
|
|
|
|
if (globals[me.namespace]["unload"] != nil
|
|
|
|
and typeof(globals[me.namespace]["unload"]) == "func") {
|
|
|
|
var errors = [];
|
|
|
|
call(globals[me.namespace].unload, [me], errors);
|
|
|
|
if (size(errors)) {
|
|
|
|
debug.printerror(errors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
me.loadedN.setBoolValue(0);
|
2020-04-07 20:46:04 +00:00
|
|
|
#kill namespace (and hope GC will clean up behind us)
|
2020-01-13 14:45:44 +00:00
|
|
|
globals[me.namespace] = nil;
|
|
|
|
}
|
|
|
|
},
|
2020-04-07 20:46:04 +00:00
|
|
|
|
2020-01-13 14:45:44 +00:00
|
|
|
reload: func() {
|
|
|
|
me.unload();
|
|
|
|
me.load();
|
|
|
|
},
|
2020-04-07 20:46:04 +00:00
|
|
|
|
|
|
|
printTrackedResources: func(loglevel = LOG_INFO) {
|
2020-01-13 14:45:44 +00:00
|
|
|
logprint(loglevel, "Tracked resources after running the main() function of " ~
|
|
|
|
me.id~":");
|
|
|
|
logprint(loglevel, "#listeners: "~size(me._listeners));
|
|
|
|
logprint(loglevel, "#timers: "~size(me._timers));
|
|
|
|
logprint(loglevel, "Use log level DEBUG to see all calls to the " ~
|
2020-04-07 20:46:04 +00:00
|
|
|
"setlistener() and maketimer() wrappers.");
|
2020-01-13 14:45:44 +00:00
|
|
|
},
|
2020-04-07 20:46:04 +00:00
|
|
|
|
2020-01-13 14:45:44 +00:00
|
|
|
# redirect setlistener() for module
|
|
|
|
_redirect_setlistener: func() {
|
2020-04-24 13:52:35 +00:00
|
|
|
globals[me.namespace].setlistener = func(p, f, start=0, runtime=nil) {
|
2020-01-13 14:45:44 +00:00
|
|
|
if (!isa(p, props.Node)) {
|
|
|
|
p = props.getNode(p, 1).resolveAlias();
|
|
|
|
}
|
2020-04-24 13:52:35 +00:00
|
|
|
if (runtime == nil) runtime = me._setlistener_runtime_default;
|
2020-01-13 14:45:44 +00:00
|
|
|
if (me._debug) {
|
|
|
|
var f_debug = func {
|
|
|
|
me.lhitN.setValue(me.lhitN.getValue() + 1);
|
2020-02-02 22:40:13 +00:00
|
|
|
if (int(me._debug) > 1) {
|
|
|
|
print("Listener hit for: ", p.getPath());
|
|
|
|
}
|
2020-01-13 14:45:44 +00:00
|
|
|
call(f, arg);
|
|
|
|
};
|
|
|
|
append(me._listeners, Module._orig_setlistener(p, f_debug, start, runtime));
|
|
|
|
var c = caller(1);
|
|
|
|
if (c != nil) {
|
2020-04-07 20:46:04 +00:00
|
|
|
print(sprintf("[%s] setlistener for %s called from %s:%s",
|
|
|
|
me.namespace, p.getPath(), io.basename(c[2]), c[3]));
|
2020-01-13 14:45:44 +00:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
append(me._listeners, Module._orig_setlistener(p,
|
|
|
|
f, start, runtime));
|
2020-04-07 20:46:04 +00:00
|
|
|
}
|
2020-01-13 14:45:44 +00:00
|
|
|
me.lcountN.setValue(me.lcountN.getValue() + 1);
|
|
|
|
}
|
|
|
|
me.setlistener = globals[me.namespace].setlistener;
|
|
|
|
},
|
2020-04-07 20:46:04 +00:00
|
|
|
|
2020-01-13 14:45:44 +00:00
|
|
|
# redirect maketimer for module
|
|
|
|
_redirect_maketimer: func() {
|
|
|
|
globals[me.namespace].maketimer = func() {
|
|
|
|
if (size(arg) == 2) {
|
|
|
|
append(me._timers, Module._orig_maketimer(arg[0], arg[1]));
|
|
|
|
} elsif (size(arg) == 3) {
|
2020-04-07 20:46:04 +00:00
|
|
|
append(me._timers,
|
2020-01-13 14:45:44 +00:00
|
|
|
Module._orig_maketimer(arg[0], arg[1], arg[2]));
|
|
|
|
} else {
|
2020-04-07 20:46:04 +00:00
|
|
|
logprint(DEV_ALERT, "Invalid number of arguments to maketimer()");
|
2020-01-13 14:45:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (me._debug) {
|
|
|
|
var c = caller(1);
|
|
|
|
if (c != nil) {
|
2020-04-07 20:46:04 +00:00
|
|
|
print(sprintf("[%s] maketimer called from %s:%s",
|
|
|
|
me.namespace, io.basename(c[2]), c[3]));
|
2020-01-13 14:45:44 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
me.tcountN.setValue(me.tcountN.getValue() + 1);
|
|
|
|
return me._timers[-1];
|
|
|
|
}
|
|
|
|
me.maketimer = globals[me.namespace].maketimer;
|
|
|
|
},
|
2020-04-07 20:46:04 +00:00
|
|
|
|
2020-01-13 14:45:44 +00:00
|
|
|
_redirect_settimer: func() {
|
|
|
|
globals[me.namespace].settimer = func() {
|
2020-04-07 20:46:04 +00:00
|
|
|
var c = caller(1);
|
|
|
|
logprint(DEV_ALERT, sprintf("\n\Unsupported settimer() call from %s:%s. "~
|
2020-01-13 14:45:44 +00:00
|
|
|
"Use maketimer() instead.",
|
|
|
|
io.basename(c[2]), c[3]));
|
|
|
|
}
|
|
|
|
},
|
2020-02-23 18:39:42 +00:00
|
|
|
}; # end class Module
|
2020-01-13 14:45:44 +00:00
|
|
|
|
|
|
|
var isAvailable = func(name) {
|
|
|
|
return contains(_modules_available, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
var _getInstance = func(name) {
|
|
|
|
if (isAvailable(name) and _instances[name] == nil) {
|
|
|
|
var m = Module.new(name);
|
|
|
|
m.setFilePath(MODULES_DIR~name);
|
|
|
|
}
|
|
|
|
return _instances[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
var setDebug = func(name, debug=1) {
|
|
|
|
if (isAvailable(name)) {
|
|
|
|
var module = _getInstance(name);
|
|
|
|
module.setDebug(debug);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var load = func(name, ns="") {
|
|
|
|
var m = _getInstance(name);
|
|
|
|
if (m != nil) {
|
|
|
|
if (ns) { m.setNamespace(ns); }
|
|
|
|
return m.load();
|
|
|
|
}
|
2020-04-07 20:46:04 +00:00
|
|
|
else return 0;
|
2020-01-13 14:45:44 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 20:46:04 +00:00
|
|
|
# scan MODULES_DIR for subdirectories; it is assumed, that only well-formed
|
2020-01-13 14:45:44 +00:00
|
|
|
# modules are stored in that directories, so no further checks right here
|
|
|
|
var _findModules = func() {
|
|
|
|
var module_dirs = io.subdirectories(MODULES_DIR);
|
|
|
|
_modules_available = {};
|
|
|
|
foreach (var name; module_dirs) {
|
2020-04-07 20:46:04 +00:00
|
|
|
if (!io.is_regular_file(MODULES_DIR~"/"~name~"/"~MODULES_DEFAULT_FILENAME))
|
|
|
|
break;
|
2020-01-13 14:45:44 +00:00
|
|
|
_modules_available[name] = 1;
|
2020-03-08 19:56:05 +00:00
|
|
|
MODULES_NODE.getNode(name~"/available",1).setBoolValue(1);
|
2020-01-13 14:45:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 22:40:13 +00:00
|
|
|
#props.getNode is available only after nasal dir has been initialized
|
2020-01-13 14:45:44 +00:00
|
|
|
_setlistener("sim/signals/nasal-dir-initialized", func {
|
|
|
|
MODULES_NODE = props.getNode("/nasal/modules", 1);
|
|
|
|
_findModules();
|
|
|
|
});
|
2020-04-07 13:28:06 +00:00
|
|
|
|
|
|
|
var commandModuleReload = func(node)
|
|
|
|
{
|
|
|
|
var module = node.getChild("module").getValue();
|
|
|
|
var m = _getInstance(module);
|
|
|
|
if (m == nil) {
|
2020-04-07 20:46:04 +00:00
|
|
|
logprint(LOG_WARN, "Unknown module to reload: "~module);
|
2020-04-07 13:28:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m.reload();
|
|
|
|
};
|
|
|
|
|
|
|
|
addcommand("nasal-module-reload", commandModuleReload);
|