]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalSys.hxx
c4acd37b73d881aa3b171ab7af7275147369ed9f
[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/model.hxx>
8
9 #include <Main/globals.hxx>
10
11 #include <map>
12 SG_USING_STD(map);
13
14
15 class FGNasalScript;
16 class FGNasalListener;
17
18 class FGNasalSys : public SGSubsystem
19 {
20 public:
21     FGNasalSys();
22     virtual ~FGNasalSys();
23     virtual void init();
24     virtual void update(double dt) { /* noop */ }
25
26     // Loads a nasal script from an external file and inserts it as a
27     // global module of the specified name.
28     void loadModule(SGPath file, const char* moduleName);
29
30     // Simple hook to run arbitrary source code.  Returns a bool to
31     // indicate successful execution.  Does *not* return any Nasal
32     // values, because handling garbage-collected objects from C space
33     // is deep voodoo and violates the "simple hook" idea.
34     bool parseAndRun(const char* sourceCode);
35
36     // Slightly more complicated hook to get a handle to a precompiled
37     // Nasal script that can be invoked via a call() method.  The
38     // caller is expected to delete the FGNasalScript returned from
39     // this function.  The "name" argument specifies the "file name"
40     // for the source code that will be printed in Nasal stack traces
41     // on error.
42     FGNasalScript* parseScript(const char* src, const char* name=0);
43
44     // Implementation of the settimer extension function
45     void setTimer(int argc, naRef* args);
46
47     // Implementation of the setlistener extension function
48     naRef setListener(int argc, naRef* args);
49     naRef removeListener(int argc, naRef* args);
50
51     // Returns a ghost wrapper for the current _cmdArg
52     naRef cmdArgGhost();
53
54     // Callbacks for command and timer bindings
55     virtual bool handleCommand(const SGPropertyNode* arg);
56
57     void createModule(const char* moduleName, const char* fileName,
58                     const char* src, int len);
59
60     void deleteModule(const char* moduleName);
61
62 private:
63     friend class FGNasalScript;
64     friend class FGNasalListener;
65
66     //
67     // FGTimer subclass for handling Nasal timer callbacks.
68     // See the implementation of the settimer() extension function for
69     // more notes.
70     //
71     struct NasalTimer {
72         virtual void timerExpired();
73         naRef handler;
74         int gcKey;
75         FGNasalSys* nasal;
76     };
77
78     // Listener
79     map<int, FGNasalListener *> _listener;
80     static int _listenerId;
81
82     void loadPropertyScripts();
83     void hashset(naRef hash, const char* key, naRef val);
84     void logError(naContext);
85     naRef parse(const char* filename, const char* buf, int len);
86     naRef genPropsModule();
87     naRef propNodeGhost(SGPropertyNode* handle);
88
89     // This mechanism is here to allow naRefs to be passed to
90     // locations "outside" the interpreter.  Normally, such a
91     // reference would be garbage collected unexpectedly.  By passing
92     // it to gcSave and getting a key/handle, it can be cached in a
93     // globals.__gcsave hash.  Be sure to release it with gcRelease
94     // when done.
95     int gcSave(naRef r);
96     void gcRelease(int key);
97
98     naContext _context;
99     naRef _globals;
100
101     SGPropertyNode* _cmdArg;
102
103     int _nextGCKey;
104     naRef _gcHash;
105
106     public: void handleTimer(NasalTimer* t);
107 };
108
109
110 class FGNasalScript {
111 public:
112     ~FGNasalScript() { _nas->gcRelease(_gcKey); }
113
114     bool call() {
115         naRef n = naNil();
116         naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
117         return naGetError(_nas->_context) == 0;
118     }
119
120 private:
121     friend class FGNasalSys;
122     naRef _code;
123     int _gcKey;
124     FGNasalSys* _nas;
125 };
126
127
128 class FGNasalListener : public SGPropertyChangeListener {
129 public:
130     FGNasalListener(SGPropertyNode_ptr node, naRef handler,
131             FGNasalSys* nasal, int key)
132         : _node(node), _handler(handler), _gcKey(key), _nas(nasal) {}
133
134     ~FGNasalListener() {
135         _nas->gcRelease(_gcKey);
136     }
137
138     void valueChanged(SGPropertyNode* node) {
139         _nas->_cmdArg = node;
140         naContext c = naNewContext();
141         naModUnlock();
142         naCall(c, _handler, 0, 0, naNil(), naNil());
143         naModLock();
144         if(naGetError(c))
145             _nas->logError(c);
146         naFreeContext(c);
147     }
148
149 private:
150     friend class FGNasalSys;
151     SGPropertyNode_ptr _node;
152     naRef _handler;
153     int _gcKey;
154     FGNasalSys* _nas;
155 };
156
157
158 class FGNasalModelData : public SGModelData {
159 public:
160     FGNasalModelData() : _unload(0) {}
161     ~FGNasalModelData();
162     void modelLoaded(const string& path, SGPropertyNode *prop, ssgBranch *);
163
164 private:
165     string _module;
166     SGPropertyNode_ptr _unload;
167 };
168
169 #endif // __NASALSYS_HXX