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>
|
|
|
|
#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>
|
2006-03-09 09:04:03 +00:00
|
|
|
|
2006-02-28 14:55:37 +00:00
|
|
|
#include <map>
|
|
|
|
SG_USING_STD(map);
|
|
|
|
|
|
|
|
|
2003-12-05 01:54:39 +00:00
|
|
|
class FGNasalScript;
|
2006-02-28 14:55:37 +00:00
|
|
|
class FGNasalListener;
|
2003-12-05 01:54:39 +00:00
|
|
|
|
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.
|
|
|
|
void loadModule(SGPath file, const char* moduleName);
|
|
|
|
|
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
|
|
|
|
virtual bool handleCommand(const SGPropertyNode* arg);
|
|
|
|
|
2005-06-19 17:09:03 +00:00
|
|
|
void createModule(const char* moduleName, const char* fileName,
|
2006-04-27 15:56:51 +00:00
|
|
|
const char* src, int len, const SGPropertyNode* arg=0);
|
2006-02-13 20:08:04 +00:00
|
|
|
|
2006-03-08 10:35:20 +00:00
|
|
|
void deleteModule(const char* moduleName);
|
|
|
|
|
2007-10-15 16:28:40 +00:00
|
|
|
naRef call(naRef code, int argc, naRef* args, naRef locals);
|
2006-07-19 19:46:53 +00:00
|
|
|
|
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;
|
2003-12-05 01:54:39 +00:00
|
|
|
|
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
|
|
|
|
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();
|
2003-11-25 21:08:36 +00:00
|
|
|
void hashset(naRef hash, const char* key, naRef val);
|
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();
|
|
|
|
naRef propNodeGhost(SGPropertyNode* handle);
|
2003-11-25 21:08:36 +00:00
|
|
|
|
2003-12-05 01:54:39 +00:00
|
|
|
// 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
|
|
|
naContext _context;
|
|
|
|
naRef _globals;
|
|
|
|
|
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,
|
|
|
|
int key, int id, 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;
|
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
|
|
|
bool _first_call;
|
|
|
|
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
|
|
|
|
2008-03-22 09:31:06 +00:00
|
|
|
class FGNasalModelData : public simgear::SGModelData {
|
2006-03-09 09:04:03 +00:00
|
|
|
public:
|
2007-04-01 12:39:20 +00:00
|
|
|
FGNasalModelData(SGPropertyNode *props = 0) : _props(props), _unload(0) {}
|
2006-03-09 09:04:03 +00:00
|
|
|
~FGNasalModelData();
|
2006-10-29 19:30:21 +00:00
|
|
|
void modelLoaded(const string& path, SGPropertyNode *prop, osg::Node *);
|
2006-03-09 09:04:03 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
string _module;
|
2007-04-01 12:39:20 +00:00
|
|
|
SGPropertyNode_ptr _props;
|
2006-03-20 07:13:10 +00:00
|
|
|
SGConstPropertyNode_ptr _unload;
|
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
|