]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalModelData.cxx
Update for nasal::Ghost changes
[flightgear.git] / src / Scripting / NasalModelData.cxx
1
2 #include "NasalModelData.hxx"
3
4 #include <cstring> // for strlen
5 #include <simgear/debug/logstream.hxx>
6
7 #include "NasalSys.hxx"
8 #include <Main/globals.hxx>
9
10
11 // FGNasalModelData class.  If sgLoad3DModel() is called with a pointer to
12 // such a class, then it lets modelLoaded() run the <load> script, and the
13 // destructor the <unload> script. The latter happens when the model branch
14 // is removed from the scene graph.
15
16 unsigned int FGNasalModelData::_module_id = 0;
17
18 void FGNasalModelData::load()
19 {
20     std::stringstream m;
21     m << "__model" << _module_id++;
22     _module = m.str();
23     
24     SG_LOG(SG_NASAL, SG_DEBUG, "Loading nasal module " << _module.c_str());
25     
26     const char *s = _load ? _load->getStringValue() : "";
27     FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
28     
29     naRef arg[2];
30     arg[0] = nasalSys->propNodeGhost(_root);
31     arg[1] = nasalSys->propNodeGhost(_prop);
32     nasalSys->createModule(_module.c_str(), _path.c_str(), s, strlen(s),
33                            _root, 2, arg);
34 }
35
36 void FGNasalModelData::unload()
37 {
38     if (_module.empty())
39         return;
40     
41     FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
42     if(!nasalSys) {
43         SG_LOG(SG_NASAL, SG_WARN, "Trying to run an <unload> script "
44                "without Nasal subsystem present.");
45         return;
46     }
47     
48     SG_LOG(SG_NASAL, SG_DEBUG, "Unloading nasal module " << _module.c_str());
49     
50     if (_unload)
51     {
52         const char *s = _unload->getStringValue();
53         nasalSys->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _root);
54     }
55     
56     nasalSys->deleteModule(_module.c_str());
57 }
58
59 void FGNasalModelDataProxy::modelLoaded(const std::string& path, SGPropertyNode *prop,
60                                         osg::Node *)
61 {
62     FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
63     if(!nasalSys) {
64         SG_LOG(SG_NASAL, SG_WARN, "Trying to run a <load> script "
65                "without Nasal subsystem present.");
66         return;
67     }
68     
69     if(!prop)
70         return;
71     
72     SGPropertyNode *nasal = prop->getNode("nasal");
73     if(!nasal)
74         return;
75     
76     SGPropertyNode* load   = nasal->getNode("load");
77     SGPropertyNode* unload = nasal->getNode("unload");
78     
79     if ((!load) && (!unload))
80         return;
81     
82     _data = new FGNasalModelData(_root, path, prop, load, unload);
83     
84     // register Nasal module to be created and loaded in the main thread.
85     nasalSys->registerToLoad(_data);
86 }
87
88 FGNasalModelDataProxy::~FGNasalModelDataProxy()
89 {
90     FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
91     // when necessary, register Nasal module to be destroyed/unloaded
92     // in the main thread.
93     if ((_data.valid())&&(nasalSys))
94         nasalSys->registerToUnload(_data);
95 }