]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalSys.hxx
Merge branch 'next' of D:\Git_New\flightgear into next
[flightgear.git] / src / Scripting / NasalSys.hxx
1 #ifndef __NASALSYS_HXX
2 #define __NASALSYS_HXX
3
4 #include <simgear/misc/sg_path.hxx>
5 #include <simgear/structure/subsystem_mgr.hxx>
6 #include <simgear/nasal/nasal.h>
7 #include <simgear/scene/model/modellib.hxx>
8 #include <simgear/xml/easyxml.hxx>
9
10 #include <map>
11 using std::map;
12
13
14 class FGNasalScript;
15 class FGNasalListener;
16
17 class FGNasalSys : public SGSubsystem
18 {
19 public:
20     FGNasalSys();
21     virtual ~FGNasalSys();
22     virtual void init();
23     virtual void update(double dt);
24
25     // Loads a nasal script from an external file and inserts it as a
26     // global module of the specified name.
27     void loadModule(SGPath file, const char* moduleName);
28
29     // Simple hook to run arbitrary source code.  Returns a bool to
30     // indicate successful execution.  Does *not* return any Nasal
31     // values, because handling garbage-collected objects from C space
32     // is deep voodoo and violates the "simple hook" idea.
33     bool parseAndRun(const char* sourceCode);
34
35     // Slightly more complicated hook to get a handle to a precompiled
36     // Nasal script that can be invoked via a call() method.  The
37     // caller is expected to delete the FGNasalScript returned from
38     // this function.  The "name" argument specifies the "file name"
39     // for the source code that will be printed in Nasal stack traces
40     // on error.
41     FGNasalScript* parseScript(const char* src, const char* name=0);
42
43     // Implementation of the settimer extension function
44     void setTimer(naContext c, int argc, naRef* args);
45
46     // Implementation of the setlistener extension function
47     naRef setListener(naContext c, int argc, naRef* args);
48     naRef removeListener(naContext c, int argc, naRef* args);
49
50     // Returns a ghost wrapper for the current _cmdArg
51     naRef cmdArgGhost();
52
53     // Callbacks for command and timer bindings
54     virtual bool handleCommand(const SGPropertyNode* arg);
55
56     void createModule(const char* moduleName, const char* fileName,
57                       const char* src, int len, const SGPropertyNode* cmdarg=0,
58                       int argc=0, naRef*args=0);
59
60     void deleteModule(const char* moduleName);
61
62     naRef call(naRef code, int argc, naRef* args, naRef locals);
63     naRef propNodeGhost(SGPropertyNode* handle);
64
65 private:
66     friend class FGNasalScript;
67     friend class FGNasalListener;
68
69     //
70     // FGTimer subclass for handling Nasal timer callbacks.
71     // See the implementation of the settimer() extension function for
72     // more notes.
73     //
74     struct NasalTimer {
75         virtual void timerExpired();
76         virtual ~NasalTimer() {}
77         naRef handler;
78         int gcKey;
79         FGNasalSys* nasal;
80     };
81
82     // Listener
83     map<int, FGNasalListener *> _listener;
84     vector<FGNasalListener *> _dead_listener;
85     static int _listenerId;
86
87     void loadPropertyScripts();
88     void hashset(naRef hash, const char* key, naRef val);
89     void logError(naContext);
90     naRef parse(const char* filename, const char* buf, int len);
91     naRef genPropsModule();
92
93     // This mechanism is here to allow naRefs to be passed to
94     // locations "outside" the interpreter.  Normally, such a
95     // reference would be garbage collected unexpectedly.  By passing
96     // it to gcSave and getting a key/handle, it can be cached in a
97     // globals.__gcsave hash.  Be sure to release it with gcRelease
98     // when done.
99     int gcSave(naRef r);
100     void gcRelease(int key);
101
102     naContext _context;
103     naRef _globals;
104
105     SGPropertyNode_ptr _cmdArg;
106
107     int _nextGCKey;
108     naRef _gcHash;
109     int _callCount;
110
111     public: void handleTimer(NasalTimer* t);
112 };
113
114
115 class FGNasalScript {
116 public:
117     ~FGNasalScript() { _nas->gcRelease(_gcKey); }
118
119     bool call() {
120         naRef n = naNil();
121         naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
122         return naGetError(_nas->_context) == 0;
123     }
124
125 private:
126     friend class FGNasalSys;
127     naRef _code;
128     int _gcKey;
129     FGNasalSys* _nas;
130 };
131
132
133 class FGNasalListener : public SGPropertyChangeListener {
134 public:
135     FGNasalListener(SGPropertyNode* node, naRef code, FGNasalSys* nasal,
136                     int key, int id, int init, int type);
137
138     virtual ~FGNasalListener();
139     virtual void valueChanged(SGPropertyNode* node);
140     virtual void childAdded(SGPropertyNode* parent, SGPropertyNode* child);
141     virtual void childRemoved(SGPropertyNode* parent, SGPropertyNode* child);
142
143 private:
144     bool changed(SGPropertyNode* node);
145     void call(SGPropertyNode* which, naRef mode);
146
147     friend class FGNasalSys;
148     SGPropertyNode_ptr _node;
149     naRef _code;
150     int _gcKey;
151     int _id;
152     FGNasalSys* _nas;
153     int _init;
154     int _type;
155     unsigned int _active;
156     bool _dead;
157     long _last_int;
158     double _last_float;
159     string _last_string;
160 };
161
162
163 class FGNasalModelData : public simgear::SGModelData {
164 public:
165     FGNasalModelData(SGPropertyNode *root = 0) : _root(root), _unload(0) {}
166     ~FGNasalModelData();
167     void modelLoaded(const string& path, SGPropertyNode *prop, osg::Node *);
168
169 private:
170     static unsigned int _module_id;
171     string _module;
172     SGPropertyNode_ptr _root;
173     SGConstPropertyNode_ptr _unload;
174 };
175
176
177 class NasalXMLVisitor : public XMLVisitor {
178 public:
179     NasalXMLVisitor(naContext c, int argc, naRef* args);
180     virtual ~NasalXMLVisitor() { naFreeContext(_c); }
181
182     virtual void startElement(const char* tag, const XMLAttributes& a);
183     virtual void endElement(const char* tag);
184     virtual void data(const char* str, int len);
185     virtual void pi(const char* target, const char* data);
186
187 private:
188     void call(naRef func, int num, naRef a = naNil(), naRef b = naNil());
189     naRef make_string(const char* s, int n = -1);
190
191     naContext _c;
192     naRef _start_element, _end_element, _data, _pi;
193 };
194
195 #endif // __NASALSYS_HXX