2 #include "NasalModelData.hxx"
4 #include <cstring> // for strlen
5 #include <simgear/debug/logstream.hxx>
7 #include "NasalSys.hxx"
8 #include <Main/globals.hxx>
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.
16 unsigned int FGNasalModelData::_module_id = 0;
18 void FGNasalModelData::load()
21 m << "__model" << _module_id++;
24 SG_LOG(SG_NASAL, SG_DEBUG, "Loading nasal module " << _module.c_str());
26 const char *s = _load ? _load->getStringValue() : "";
27 FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
30 arg[0] = nasalSys->propNodeGhost(_root);
31 arg[1] = nasalSys->propNodeGhost(_prop);
32 nasalSys->createModule(_module.c_str(), _path.c_str(), s, strlen(s),
36 void FGNasalModelData::unload()
41 FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
43 SG_LOG(SG_NASAL, SG_WARN, "Trying to run an <unload> script "
44 "without Nasal subsystem present.");
48 SG_LOG(SG_NASAL, SG_DEBUG, "Unloading nasal module " << _module.c_str());
52 const char *s = _unload->getStringValue();
53 nasalSys->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _root);
56 nasalSys->deleteModule(_module.c_str());
59 void FGNasalModelDataProxy::modelLoaded(const std::string& path, SGPropertyNode *prop,
62 FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
64 SG_LOG(SG_NASAL, SG_WARN, "Trying to run a <load> script "
65 "without Nasal subsystem present.");
72 SGPropertyNode *nasal = prop->getNode("nasal");
76 SGPropertyNode* load = nasal->getNode("load");
77 SGPropertyNode* unload = nasal->getNode("unload");
79 if ((!load) && (!unload))
82 _data = new FGNasalModelData(_root, path, prop, load, unload);
84 // register Nasal module to be created and loaded in the main thread.
85 nasalSys->registerToLoad(_data);
88 FGNasalModelDataProxy::~FGNasalModelDataProxy()
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);