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