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