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