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>
|
2013-03-21 01:16:01 +01:00
|
|
|
#include <simgear/nasal/cppbind/NasalHash.hxx>
|
2003-11-25 21:08:36 +00:00
|
|
|
#include <simgear/nasal/nasal.h>
|
2012-02-06 22:19:33 +01:00
|
|
|
#include <simgear/threads/SGQueue.hxx>
|
2013-02-09 15:33:05 +00:00
|
|
|
#include <simgear/props/props.hxx>
|
2006-03-09 09:04:03 +00:00
|
|
|
|
2013-02-09 17:40:01 +01:00
|
|
|
// Required only for MSVC
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
# include <Scripting/NasalModelData.hxx>
|
|
|
|
#endif
|
2013-02-09 17:24:44 +01: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;
|
2013-02-09 15:33:05 +00:00
|
|
|
class FGNasalModelData;
|
2013-10-06 17:36:19 +01:00
|
|
|
class NasalCommand;
|
2012-02-06 22:19:33 +01:00
|
|
|
|
2013-02-08 19:39:41 +00:00
|
|
|
namespace simgear { class BufferedLogCallback; }
|
|
|
|
|
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();
|
2013-10-06 17:36:19 +01:00
|
|
|
virtual void shutdown();
|
|
|
|
|
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.
|
2013-02-09 15:33:05 +00:00
|
|
|
// FGNasalScript* parseScript(const char* src, const char* name=0);
|
2003-12-05 01:54:39 +00:00
|
|
|
|
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
|
|
|
|
2013-02-09 15:33:05 +00:00
|
|
|
void setCmdArg(SGPropertyNode* aNode);
|
|
|
|
|
2013-02-09 16:05:54 +00:00
|
|
|
/**
|
|
|
|
* create Nasal props.Node for an SGPropertyNode*
|
|
|
|
* This is the actual ghost, wrapped in a Nasal sugar class.
|
|
|
|
*/
|
|
|
|
naRef wrappedPropsNode(SGPropertyNode* aProps);
|
|
|
|
|
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);
|
|
|
|
|
2013-10-06 17:36:19 +01:00
|
|
|
void addCommand(naRef func, const std::string& name);
|
|
|
|
void removeCommand(const std::string& name);
|
|
|
|
|
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
|
|
|
|
2013-02-09 15:33:05 +00:00
|
|
|
void registerToLoad(FGNasalModelData* data);
|
|
|
|
void registerToUnload(FGNasalModelData* data);
|
2012-02-06 22:19:33 +01:00
|
|
|
|
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; }
|
2013-03-21 01:16:01 +01:00
|
|
|
|
|
|
|
nasal::Hash getGlobals() const
|
|
|
|
{ return nasal::Hash(_globals, _context); }
|
2012-05-12 17:23:17 +01: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);
|
2013-02-08 19:39:41 +00:00
|
|
|
|
|
|
|
/// retrive the associated log object, for displaying log
|
|
|
|
/// output somewhere (a UI, presumably)
|
|
|
|
simgear::BufferedLogCallback* log() const
|
|
|
|
{ return _log; }
|
2003-11-25 21:08:36 +00:00
|
|
|
private:
|
2013-02-09 15:33:05 +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;
|
|
|
|
|
2013-03-23 12:53:17 +01:00
|
|
|
// Delay removing items of the _loadList to ensure the are already attached
|
|
|
|
// to the scene graph (eg. enables to retrieve world position in load
|
|
|
|
// callback).
|
|
|
|
bool _delay_load;
|
|
|
|
|
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;
|
2013-02-09 15:33:05 +00:00
|
|
|
std::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);
|
2013-02-09 15:33:05 +00:00
|
|
|
void addModule(std::string moduleName, simgear::PathList scripts);
|
2013-10-15 17:48:13 +02:00
|
|
|
static 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
|
|
|
|
2013-02-08 19:39:41 +00:00
|
|
|
simgear::BufferedLogCallback* _log;
|
2013-10-06 17:36:19 +01:00
|
|
|
|
|
|
|
typedef std::map<std::string, NasalCommand*> NasalCommandDict;
|
|
|
|
NasalCommandDict _commands;
|
2013-02-08 19:39:41 +00:00
|
|
|
public:
|
|
|
|
void handleTimer(NasalTimer* t);
|
2003-12-05 01:54:39 +00:00
|
|
|
};
|
2003-11-27 14:34:37 +00:00
|
|
|
|
2013-02-09 15:33:05 +00:00
|
|
|
#if 0
|
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;
|
|
|
|
}
|
2013-02-09 15:33:05 +00:00
|
|
|
|
|
|
|
FGNasalSys* sys() const { return _nas; }
|
2005-12-16 19:11:03 +00:00
|
|
|
private:
|
|
|
|
friend class FGNasalSys;
|
2007-10-18 11:43:38 +00:00
|
|
|
naRef _code;
|
2006-02-04 16:37:25 +00:00
|
|
|
int _gcKey;
|
2005-12-16 19:11:03 +00:00
|
|
|
FGNasalSys* _nas;
|
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
|
|
|
};
|
2013-02-09 15:33:05 +00:00
|
|
|
#endif
|
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
|
|
|
|
2003-11-25 21:08:36 +00:00
|
|
|
#endif // __NASALSYS_HXX
|