2003-11-25 21:08:36 +00:00
|
|
|
#ifndef __NASALSYS_HXX
|
|
|
|
#define __NASALSYS_HXX
|
|
|
|
|
|
|
|
#include <simgear/misc/sg_path.hxx>
|
|
|
|
#include <simgear/structure/subsystem_mgr.hxx>
|
2011-04-02 15:13:29 +02:00
|
|
|
#include <simgear/misc/sg_dir.hxx>
|
2003-11-25 21:08:36 +00:00
|
|
|
#include <simgear/nasal/nasal.h>
|
2008-03-22 09:31:06 +00:00
|
|
|
#include <simgear/scene/model/modellib.hxx>
|
add parsexml() function, which is a wrapper around the built-in easyxml
parser. Advantages over xml.nas: (reviewed and OK'ed by Andy)
- faster (33% ... only. I had hoped for more.)
- more standards compliant
- should support UTF
- I don't have to support it. ;-)
Usage: parsexml(<path> [, <start-tag> [, <end-tag> [, <data> [, <pi>]]]]);
<path> is an absolute file path, the rest are optional callback functions.
Example:
parsexml("/tmp/foo.xml", nil, nil, func(d) { print("DATA FOUND: ", d) });
2007-06-29 15:34:38 +00:00
|
|
|
#include <simgear/xml/easyxml.hxx>
|
2012-02-06 22:19:33 +01:00
|
|
|
#include <simgear/threads/SGQueue.hxx>
|
2006-03-09 09:04:03 +00:00
|
|
|
|
2006-02-28 14:55:37 +00:00
|
|
|
#include <map>
|
|
|
|
|
|
|
|
|
2003-12-05 01:54:39 +00:00
|
|
|
class FGNasalScript;
|
2006-02-28 14:55:37 +00:00
|
|
|
class FGNasalListener;
|
2012-08-19 21:13:31 +01:00
|
|
|
class SGCondition;
|
2012-02-06 22:19:33 +01:00
|
|
|
|
|
|
|
/** Nasal model data container.
|
|
|
|
* load and unload methods must be run in main thread (not thread-safe). */
|
|
|
|
class FGNasalModelData : public SGReferenced
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** Constructor to be run in an arbitrary thread. */
|
|
|
|
FGNasalModelData(SGPropertyNode *root, const string& path, SGPropertyNode *prop,
|
|
|
|
SGPropertyNode* load, SGPropertyNode* unload) :
|
|
|
|
_path(path),
|
|
|
|
_root(root), _prop(prop),
|
|
|
|
_load(load), _unload(unload)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Load hook. Always call from inside the main loop. */
|
|
|
|
void load();
|
|
|
|
|
|
|
|
/** Unload hook. Always call from inside the main loop. */
|
|
|
|
void unload();
|
|
|
|
|
|
|
|
private:
|
|
|
|
static unsigned int _module_id;
|
|
|
|
|
|
|
|
string _module, _path;
|
|
|
|
SGPropertyNode_ptr _root, _prop;
|
|
|
|
SGConstPropertyNode_ptr _load, _unload;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Thread-safe proxy for FGNasalModelData.
|
|
|
|
* modelLoaded/destroy methods only register the requested
|
|
|
|
* operation. Actual (un)loading of Nasal module is deferred
|
|
|
|
* and done in the main loop. */
|
|
|
|
class FGNasalModelDataProxy : public simgear::SGModelData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FGNasalModelDataProxy(SGPropertyNode *root = 0) :
|
|
|
|
_root(root), _data(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~FGNasalModelDataProxy();
|
|
|
|
|
|
|
|
void modelLoaded(const string& path, SGPropertyNode *prop, osg::Node *);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SGPropertyNode_ptr _root;
|
|
|
|
SGSharedPtr<FGNasalModelData> _data;
|
|
|
|
};
|
|
|
|
|
2012-08-19 21:13:31 +01:00
|
|
|
SGPropertyNode* ghostToPropNode(naRef ref);
|
|
|
|
SGCondition* conditionGhost(naRef r);
|
|
|
|
|
2003-11-25 21:08:36 +00:00
|
|
|
class FGNasalSys : public SGSubsystem
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FGNasalSys();
|
|
|
|
virtual ~FGNasalSys();
|
|
|
|
virtual void init();
|
2007-01-23 16:47:04 +00:00
|
|
|
virtual void update(double dt);
|
2003-11-25 21:08:36 +00:00
|
|
|
|
2005-06-14 19:57:24 +00:00
|
|
|
// Loads a nasal script from an external file and inserts it as a
|
|
|
|
// global module of the specified name.
|
2011-04-03 15:30:25 +02:00
|
|
|
bool loadModule(SGPath file, const char* moduleName);
|
2005-06-14 19:57:24 +00:00
|
|
|
|
2003-12-01 14:35:49 +00:00
|
|
|
// Simple hook to run arbitrary source code. Returns a bool to
|
|
|
|
// indicate successful execution. Does *not* return any Nasal
|
|
|
|
// values, because handling garbage-collected objects from C space
|
|
|
|
// is deep voodoo and violates the "simple hook" idea.
|
|
|
|
bool parseAndRun(const char* sourceCode);
|
|
|
|
|
2003-12-05 01:54:39 +00:00
|
|
|
// Slightly more complicated hook to get a handle to a precompiled
|
|
|
|
// Nasal script that can be invoked via a call() method. The
|
|
|
|
// caller is expected to delete the FGNasalScript returned from
|
|
|
|
// this function. The "name" argument specifies the "file name"
|
|
|
|
// for the source code that will be printed in Nasal stack traces
|
|
|
|
// on error.
|
|
|
|
FGNasalScript* parseScript(const char* src, const char* name=0);
|
|
|
|
|
2003-11-25 21:08:36 +00:00
|
|
|
// Implementation of the settimer extension function
|
2007-05-12 18:15:45 +00:00
|
|
|
void setTimer(naContext c, int argc, naRef* args);
|
2003-11-25 21:08:36 +00:00
|
|
|
|
2005-12-16 19:11:03 +00:00
|
|
|
// Implementation of the setlistener extension function
|
2007-02-03 16:46:39 +00:00
|
|
|
naRef setListener(naContext c, int argc, naRef* args);
|
2007-01-17 13:56:22 +00:00
|
|
|
naRef removeListener(naContext c, int argc, naRef* args);
|
2005-12-16 19:11:03 +00:00
|
|
|
|
2003-12-01 14:35:49 +00:00
|
|
|
// Returns a ghost wrapper for the current _cmdArg
|
|
|
|
naRef cmdArgGhost();
|
2006-02-13 20:08:04 +00:00
|
|
|
|
2003-12-05 01:54:39 +00:00
|
|
|
// Callbacks for command and timer bindings
|
2012-07-04 13:15:12 +02:00
|
|
|
virtual bool handleCommand( const char* moduleName,
|
|
|
|
const char* fileName,
|
|
|
|
const char* src,
|
|
|
|
const SGPropertyNode* arg = 0 );
|
2003-12-05 01:54:39 +00:00
|
|
|
virtual bool handleCommand(const SGPropertyNode* arg);
|
|
|
|
|
2011-04-03 15:30:25 +02:00
|
|
|
bool createModule(const char* moduleName, const char* fileName,
|
2008-10-18 19:52:18 +00:00
|
|
|
const char* src, int len, const SGPropertyNode* cmdarg=0,
|
|
|
|
int argc=0, naRef*args=0);
|
2006-02-13 20:08:04 +00:00
|
|
|
|
2006-03-08 10:35:20 +00:00
|
|
|
void deleteModule(const char* moduleName);
|
|
|
|
|
2012-08-04 00:24:26 +02:00
|
|
|
/**
|
|
|
|
* Set member of specified hash to given value
|
|
|
|
*/
|
|
|
|
void hashset(naRef hash, const char* key, naRef val);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set member of globals hash to given value
|
|
|
|
*/
|
|
|
|
void globalsSet(const char* key, naRef val);
|
|
|
|
|
2007-10-15 16:28:40 +00:00
|
|
|
naRef call(naRef code, int argc, naRef* args, naRef locals);
|
2012-05-12 17:23:17 +01:00
|
|
|
|
|
|
|
naRef callMethod(naRef code, naRef self, int argc, naRef* args, naRef locals);
|
|
|
|
|
2008-10-18 19:52:18 +00:00
|
|
|
naRef propNodeGhost(SGPropertyNode* handle);
|
2012-08-19 21:13:31 +01:00
|
|
|
|
2012-02-06 22:19:33 +01:00
|
|
|
void registerToLoad(FGNasalModelData* data) { _loadList.push(data);}
|
|
|
|
void registerToUnload(FGNasalModelData* data) { _unloadList.push(data);}
|
|
|
|
|
2012-05-12 17:23:17 +01:00
|
|
|
// can't call this 'globals' due to naming clash
|
|
|
|
naRef nasalGlobals() const
|
|
|
|
{ return _globals; }
|
|
|
|
|
|
|
|
naContext context() const
|
|
|
|
{ return _context; }
|
|
|
|
|
|
|
|
// This mechanism is here to allow naRefs to be passed to
|
|
|
|
// locations "outside" the interpreter. Normally, such a
|
|
|
|
// reference would be garbage collected unexpectedly. By passing
|
|
|
|
// it to gcSave and getting a key/handle, it can be cached in a
|
|
|
|
// globals.__gcsave hash. Be sure to release it with gcRelease
|
|
|
|
// when done.
|
|
|
|
int gcSave(naRef r);
|
|
|
|
void gcRelease(int key);
|
2003-11-25 21:08:36 +00:00
|
|
|
private:
|
2003-12-05 01:54:39 +00:00
|
|
|
friend class FGNasalScript;
|
2005-12-16 19:11:03 +00:00
|
|
|
friend class FGNasalListener;
|
2011-04-03 15:30:25 +02:00
|
|
|
friend class FGNasalModuleListener;
|
2003-12-05 01:54:39 +00:00
|
|
|
|
2012-02-06 22:19:33 +01:00
|
|
|
SGLockedQueue<SGSharedPtr<FGNasalModelData> > _loadList;
|
|
|
|
SGLockedQueue<SGSharedPtr<FGNasalModelData> > _unloadList;
|
|
|
|
|
2003-11-25 21:08:36 +00:00
|
|
|
//
|
|
|
|
// FGTimer subclass for handling Nasal timer callbacks.
|
|
|
|
// See the implementation of the settimer() extension function for
|
|
|
|
// more notes.
|
|
|
|
//
|
|
|
|
struct NasalTimer {
|
|
|
|
virtual void timerExpired();
|
2006-08-08 18:32:17 +00:00
|
|
|
virtual ~NasalTimer() {}
|
2003-11-25 21:08:36 +00:00
|
|
|
naRef handler;
|
2003-12-05 01:54:39 +00:00
|
|
|
int gcKey;
|
2003-11-25 21:08:36 +00:00
|
|
|
FGNasalSys* nasal;
|
|
|
|
};
|
|
|
|
|
2006-02-28 14:55:37 +00:00
|
|
|
// Listener
|
2012-03-31 18:19:14 +02:00
|
|
|
std::map<int, FGNasalListener *> _listener;
|
2007-10-18 11:43:38 +00:00
|
|
|
vector<FGNasalListener *> _dead_listener;
|
2006-02-28 14:55:37 +00:00
|
|
|
static int _listenerId;
|
|
|
|
|
2003-12-01 14:35:49 +00:00
|
|
|
void loadPropertyScripts();
|
2011-04-03 15:30:25 +02:00
|
|
|
void loadPropertyScripts(SGPropertyNode* n);
|
2011-04-02 15:13:29 +02:00
|
|
|
void loadScriptDirectory(simgear::Dir nasalDir);
|
|
|
|
void addModule(string moduleName, simgear::PathList scripts);
|
2006-01-27 19:51:25 +00:00
|
|
|
void logError(naContext);
|
2005-04-26 20:57:06 +00:00
|
|
|
naRef parse(const char* filename, const char* buf, int len);
|
2003-12-01 14:35:49 +00:00
|
|
|
naRef genPropsModule();
|
2003-11-25 21:08:36 +00:00
|
|
|
|
|
|
|
naContext _context;
|
2013-01-31 19:14:14 +01:00
|
|
|
naRef _globals,
|
|
|
|
_string;
|
2003-11-25 21:08:36 +00:00
|
|
|
|
2006-06-11 10:21:10 +00:00
|
|
|
SGPropertyNode_ptr _cmdArg;
|
2003-12-01 14:35:49 +00:00
|
|
|
|
2003-12-05 01:54:39 +00:00
|
|
|
int _nextGCKey;
|
|
|
|
naRef _gcHash;
|
2006-07-19 19:46:53 +00:00
|
|
|
int _callCount;
|
2003-12-05 01:54:39 +00:00
|
|
|
|
|
|
|
public: void handleTimer(NasalTimer* t);
|
|
|
|
};
|
2003-11-27 14:34:37 +00:00
|
|
|
|
2006-03-08 10:35:20 +00:00
|
|
|
|
2003-12-05 01:54:39 +00:00
|
|
|
class FGNasalScript {
|
2003-11-27 14:34:37 +00:00
|
|
|
public:
|
2003-12-05 01:54:39 +00:00
|
|
|
~FGNasalScript() { _nas->gcRelease(_gcKey); }
|
|
|
|
|
|
|
|
bool call() {
|
2006-01-06 09:50:58 +00:00
|
|
|
naRef n = naNil();
|
|
|
|
naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
|
2003-12-05 01:54:39 +00:00
|
|
|
return naGetError(_nas->_context) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class FGNasalSys;
|
|
|
|
naRef _code;
|
|
|
|
int _gcKey;
|
|
|
|
FGNasalSys* _nas;
|
2003-11-25 21:08:36 +00:00
|
|
|
};
|
|
|
|
|
2006-03-08 10:35:20 +00:00
|
|
|
|
2005-12-16 19:11:03 +00:00
|
|
|
class FGNasalListener : public SGPropertyChangeListener {
|
|
|
|
public:
|
2007-10-18 11:43:38 +00:00
|
|
|
FGNasalListener(SGPropertyNode* node, naRef code, FGNasalSys* nasal,
|
2009-03-07 17:57:20 +00:00
|
|
|
int key, int id, int init, int type);
|
2006-02-04 16:37:25 +00:00
|
|
|
|
2007-10-18 11:43:38 +00:00
|
|
|
virtual ~FGNasalListener();
|
|
|
|
virtual void valueChanged(SGPropertyNode* node);
|
|
|
|
virtual void childAdded(SGPropertyNode* parent, SGPropertyNode* child);
|
|
|
|
virtual void childRemoved(SGPropertyNode* parent, SGPropertyNode* child);
|
2005-12-16 19:11:03 +00:00
|
|
|
|
|
|
|
private:
|
2007-10-15 16:28:40 +00:00
|
|
|
bool changed(SGPropertyNode* node);
|
2007-10-16 15:15:41 +00:00
|
|
|
void call(SGPropertyNode* which, naRef mode);
|
2007-10-15 16:28:40 +00:00
|
|
|
|
2005-12-16 19:11:03 +00:00
|
|
|
friend class FGNasalSys;
|
2006-02-28 14:55:37 +00:00
|
|
|
SGPropertyNode_ptr _node;
|
2007-10-18 11:43:38 +00:00
|
|
|
naRef _code;
|
2006-02-04 16:37:25 +00:00
|
|
|
int _gcKey;
|
2007-04-27 14:30:05 +00:00
|
|
|
int _id;
|
2005-12-16 19:11:03 +00:00
|
|
|
FGNasalSys* _nas;
|
2009-03-07 17:57:20 +00:00
|
|
|
int _init;
|
2007-10-15 16:28:40 +00:00
|
|
|
int _type;
|
2006-03-20 11:01:16 +00:00
|
|
|
unsigned int _active;
|
2007-01-23 16:47:04 +00:00
|
|
|
bool _dead;
|
2007-10-12 17:24:43 +00:00
|
|
|
long _last_int;
|
|
|
|
double _last_float;
|
|
|
|
string _last_string;
|
2005-12-16 19:11:03 +00:00
|
|
|
};
|
|
|
|
|
2006-03-09 09:04:03 +00:00
|
|
|
|
add parsexml() function, which is a wrapper around the built-in easyxml
parser. Advantages over xml.nas: (reviewed and OK'ed by Andy)
- faster (33% ... only. I had hoped for more.)
- more standards compliant
- should support UTF
- I don't have to support it. ;-)
Usage: parsexml(<path> [, <start-tag> [, <end-tag> [, <data> [, <pi>]]]]);
<path> is an absolute file path, the rest are optional callback functions.
Example:
parsexml("/tmp/foo.xml", nil, nil, func(d) { print("DATA FOUND: ", d) });
2007-06-29 15:34:38 +00:00
|
|
|
class NasalXMLVisitor : public XMLVisitor {
|
|
|
|
public:
|
|
|
|
NasalXMLVisitor(naContext c, int argc, naRef* args);
|
|
|
|
virtual ~NasalXMLVisitor() { naFreeContext(_c); }
|
|
|
|
|
|
|
|
virtual void startElement(const char* tag, const XMLAttributes& a);
|
|
|
|
virtual void endElement(const char* tag);
|
|
|
|
virtual void data(const char* str, int len);
|
|
|
|
virtual void pi(const char* target, const char* data);
|
|
|
|
|
|
|
|
private:
|
2007-06-30 09:44:33 +00:00
|
|
|
void call(naRef func, int num, naRef a = naNil(), naRef b = naNil());
|
add parsexml() function, which is a wrapper around the built-in easyxml
parser. Advantages over xml.nas: (reviewed and OK'ed by Andy)
- faster (33% ... only. I had hoped for more.)
- more standards compliant
- should support UTF
- I don't have to support it. ;-)
Usage: parsexml(<path> [, <start-tag> [, <end-tag> [, <data> [, <pi>]]]]);
<path> is an absolute file path, the rest are optional callback functions.
Example:
parsexml("/tmp/foo.xml", nil, nil, func(d) { print("DATA FOUND: ", d) });
2007-06-29 15:34:38 +00:00
|
|
|
naRef make_string(const char* s, int n = -1);
|
|
|
|
|
|
|
|
naContext _c;
|
|
|
|
naRef _start_element, _end_element, _data, _pi;
|
|
|
|
};
|
|
|
|
|
2003-11-25 21:08:36 +00:00
|
|
|
#endif // __NASALSYS_HXX
|