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