]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalSys.hxx
Scripting: expose flight history as aircraft.history()
[flightgear.git] / src / Scripting / NasalSys.hxx
1 #ifndef __NASALSYS_HXX
2 #define __NASALSYS_HXX
3
4 #include <simgear/math/SGMath.hxx> // keep before any cppbind include to enable
5                                    // SGVec2<T> conversion.
6 #include <simgear/misc/sg_dir.hxx>
7 #include <simgear/misc/sg_path.hxx>
8 #include <simgear/nasal/cppbind/NasalHash.hxx>
9 #include <simgear/nasal/nasal.h>
10 #include <simgear/props/props.hxx>
11 #include <simgear/structure/subsystem_mgr.hxx>
12 #include <simgear/threads/SGQueue.hxx>
13
14 // Required only for MSVC
15 #ifdef _MSC_VER
16 #   include <Scripting/NasalModelData.hxx>
17 #endif
18
19 #include <map>
20
21
22 class FGNasalScript;
23 class FGNasalListener;
24 class SGCondition;
25 class FGNasalModelData;
26 class NasalCommand;
27 class FGNasalModuleListener;
28
29 namespace simgear { class BufferedLogCallback; }
30
31 SGPropertyNode* ghostToPropNode(naRef ref);
32 SGCondition* conditionGhost(naRef r);
33
34 class FGNasalSys : public SGSubsystem
35 {
36 public:
37     FGNasalSys();
38     virtual ~FGNasalSys();
39     virtual void init();
40     virtual void shutdown();
41     
42     virtual void update(double dt);
43
44     // Loads a nasal script from an external file and inserts it as a
45     // global module of the specified name.
46     bool loadModule(SGPath file, const char* moduleName);
47
48     // Simple hook to run arbitrary source code.  Returns a bool to
49     // indicate successful execution.  Does *not* return any Nasal
50     // values, because handling garbage-collected objects from C space
51     // is deep voodoo and violates the "simple hook" idea.
52     bool parseAndRun(const char* sourceCode);
53
54     // Slightly more complicated hook to get a handle to a precompiled
55     // Nasal script that can be invoked via a call() method.  The
56     // caller is expected to delete the FGNasalScript returned from
57     // this function.  The "name" argument specifies the "file name"
58     // for the source code that will be printed in Nasal stack traces
59     // on error.
60  //   FGNasalScript* parseScript(const char* src, const char* name=0);
61
62     // Implementation of the settimer extension function
63     void setTimer(naContext c, int argc, naRef* args);
64
65     // Implementation of the setlistener extension function
66     naRef setListener(naContext c, int argc, naRef* args);
67     naRef removeListener(naContext c, int argc, naRef* args);
68
69     // Returns a ghost wrapper for the current _cmdArg
70     naRef cmdArgGhost();
71
72     void setCmdArg(SGPropertyNode* aNode);
73     
74     /**
75      * create Nasal props.Node for an SGPropertyNode*
76      * This is the actual ghost, wrapped in a Nasal sugar class.
77      */
78     naRef wrappedPropsNode(SGPropertyNode* aProps);
79     
80     // Callbacks for command and timer bindings
81     virtual bool handleCommand( const char* moduleName,
82                                 const char* fileName,
83                                 const char* src,
84                                 const SGPropertyNode* arg = 0 );
85     virtual bool handleCommand(const SGPropertyNode* arg);
86
87     bool createModule(const char* moduleName, const char* fileName,
88                       const char* src, int len, const SGPropertyNode* cmdarg=0,
89                       int argc=0, naRef*args=0);
90
91     void deleteModule(const char* moduleName);
92
93     void addCommand(naRef func, const std::string& name);
94     void removeCommand(const std::string& name);
95     
96     /**
97      * Set member of specified hash to given value
98      */
99     void hashset(naRef hash, const char* key, naRef val);
100
101     /**
102      * Set member of globals hash to given value
103      */
104     void globalsSet(const char* key, naRef val);
105
106     naRef call(naRef code, int argc, naRef* args, naRef locals);
107     naRef callWithContext(naContext ctx, naRef code, int argc, naRef* args, naRef locals);
108   
109     naRef callMethod(naRef code, naRef self, int argc, naRef* args, naRef locals);
110     naRef callMethodWithContext(naContext ctx, naRef code, naRef self, int argc, naRef* args, naRef locals);
111   
112     naRef propNodeGhost(SGPropertyNode* handle);
113   
114     void registerToLoad(FGNasalModelData* data);
115     void registerToUnload(FGNasalModelData* data);
116
117     // can't call this 'globals' due to naming clash
118     naRef nasalGlobals() const
119     { return _globals; }
120
121     nasal::Hash getGlobals() const
122     { return nasal::Hash(_globals, _context); }
123   
124     // This mechanism is here to allow naRefs to be passed to
125     // locations "outside" the interpreter.  Normally, such a
126     // reference would be garbage collected unexpectedly.  By passing
127     // it to gcSave and getting a key/handle, it can be cached in a
128     // globals.__gcsave hash.  Be sure to release it with gcRelease
129     // when done.
130     int gcSave(naRef r);
131     void gcRelease(int key);
132     
133     /// retrive the associated log object, for displaying log
134     /// output somewhere (a UI, presumably)
135     simgear::BufferedLogCallback* log() const
136     { return _log; }
137 private:
138     //friend class FGNasalScript;
139     friend class FGNasalListener;
140     friend class FGNasalModuleListener;
141
142     SGLockedQueue<SGSharedPtr<FGNasalModelData> > _loadList;
143     SGLockedQueue<SGSharedPtr<FGNasalModelData> > _unloadList;
144
145     // Delay removing items of the _loadList to ensure the are already attached
146     // to the scene graph (eg. enables to retrieve world position in load
147     // callback).
148     bool _delay_load;
149
150     //
151     // FGTimer subclass for handling Nasal timer callbacks.
152     // See the implementation of the settimer() extension function for
153     // more notes.
154     //
155     struct NasalTimer {
156         virtual void timerExpired();
157         virtual ~NasalTimer() {}
158         naRef handler;
159         int gcKey;
160         FGNasalSys* nasal;
161     };
162
163     // Listener
164     std::map<int, FGNasalListener *> _listener;
165     std::vector<FGNasalListener *> _dead_listener;
166     
167     std::vector<FGNasalModuleListener*> _moduleListeners;
168     
169     static int _listenerId;
170
171     void loadPropertyScripts();
172     void loadPropertyScripts(SGPropertyNode* n);
173     void loadScriptDirectory(simgear::Dir nasalDir);
174     void addModule(std::string moduleName, simgear::PathList scripts);
175     static void logError(naContext);
176     naRef parse(naContext ctx, const char* filename, const char* buf, int len);
177     naRef genPropsModule();
178
179     bool _inited;
180     naContext _context;
181     naRef _globals,
182           _string;
183
184     SGPropertyNode_ptr _cmdArg;
185
186     simgear::BufferedLogCallback* _log;
187     
188     typedef std::map<std::string, NasalCommand*> NasalCommandDict;
189     NasalCommandDict _commands;
190     
191     naRef _wrappedNodeFunc;
192 public:
193     void handleTimer(NasalTimer* t);
194 };
195
196 #if 0
197 class FGNasalScript {
198 public:
199     ~FGNasalScript() { _nas->gcRelease(_gcKey); }
200
201     bool call() {
202         naRef n = naNil();
203         naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
204         return naGetError(_nas->_context) == 0;
205     }
206     
207     FGNasalSys* sys() const { return _nas; }
208 private:
209     friend class FGNasalSys;
210     naRef _code;
211     int _gcKey;
212     FGNasalSys* _nas;
213 };
214 #endif
215
216 #endif // __NASALSYS_HXX