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