1 // globals.cxx -- Global state that needs to be shared among the sim modules
3 // Written by Curtis Olson, started July 2000.
5 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software Foundation,
19 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <boost/foreach.hpp>
30 #include <simgear/structure/commands.hxx>
31 #include <simgear/misc/sg_path.hxx>
32 #include <simgear/misc/sg_dir.hxx>
33 #include <simgear/timing/sg_time.hxx>
34 #include <simgear/ephemeris/ephemeris.hxx>
35 #include <simgear/magvar/magvar.hxx>
36 #include <simgear/scene/material/matlib.hxx>
37 #include <simgear/structure/subsystem_mgr.hxx>
38 #include <simgear/structure/event_mgr.hxx>
39 #include <simgear/sound/soundmgr_openal.hxx>
40 #include <simgear/misc/ResourceManager.hxx>
41 #include <simgear/props/propertyObject.hxx>
42 #include <simgear/props/props_io.hxx>
43 #include <simgear/scene/model/placement.hxx>
45 #include <Aircraft/controls.hxx>
46 #include <Airports/runways.hxx>
47 #include <ATCDCL/ATISmgr.hxx>
48 #include <Autopilot/route_mgr.hxx>
49 #include <GUI/FGFontCache.hxx>
51 #include <Model/acmodel.hxx>
52 #include <Model/modelmgr.hxx>
53 #include <MultiPlayer/multiplaymgr.hxx>
54 #include <Scenery/scenery.hxx>
55 #include <Scenery/tilemgr.hxx>
56 #include <Navaids/navlist.hxx>
57 #include <Viewer/renderer.hxx>
58 #include <Viewer/viewmgr.hxx>
60 #include "globals.hxx"
63 #include "fg_props.hxx"
66 class AircraftResourceProvider : public simgear::ResourceProvider
69 AircraftResourceProvider() :
70 simgear::ResourceProvider(simgear::ResourceManager::PRIORITY_HIGH)
74 virtual SGPath resolve(const std::string& aResource, SGPath&) const
76 string_list pieces(sgPathBranchSplit(aResource));
77 if ((pieces.size() < 3) || (pieces.front() != "Aircraft")) {
78 return SGPath(); // not an Aircraft path
81 // test against the aircraft-dir property
82 const char* aircraftDir = fgGetString("/sim/aircraft-dir");
83 string_list aircraftDirPieces(sgPathBranchSplit(aircraftDir));
84 if (!aircraftDirPieces.empty() && (aircraftDirPieces.back() == pieces[1])) {
85 // current aircraft-dir matches resource aircraft
86 SGPath r(aircraftDir);
87 for (unsigned int i=2; i<pieces.size(); ++i) {
92 SG_LOG(SG_IO, SG_DEBUG, "found path:" << aResource << " via /sim/aircraft-dir: " << r.str());
97 // try each aircraft dir in turn
98 std::string res(aResource, 9); // resource path with 'Aircraft/' removed
99 const string_list& dirs(globals->get_aircraft_paths());
100 string_list::const_iterator it = dirs.begin();
101 for (; it != dirs.end(); ++it) {
104 SG_LOG(SG_IO, SG_DEBUG, "found path:" << aResource << " in aircraft dir: " << *it);
107 } // of aircraft path iteration
109 return SGPath(); // not found
113 ////////////////////////////////////////////////////////////////////////
114 // Implementation of FGGlobals.
115 ////////////////////////////////////////////////////////////////////////
122 FGGlobals::FGGlobals() :
123 props( new SGPropertyNode ),
124 initial_state( NULL ),
125 locale( new FGLocale(props) ),
126 renderer( new FGRenderer ),
127 subsystem_mgr( new SGSubsystemMgr ),
128 event_mgr( new SGEventMgr ),
140 commands( SGCommandMgr::instance() ),
143 channel_options_list( NULL ),
144 initial_waypoints( NULL ),
147 fontcache ( new FGFontCache ),
155 haveUserSettings(false)
157 simgear::ResourceManager::instance()->addProvider(new AircraftResourceProvider());
158 simgear::PropertyObjectBase::setDefaultRoot(props);
162 FGGlobals::~FGGlobals()
164 // save user settings (unless already saved)
167 // The AIModels manager performs a number of actions upon
168 // Shutdown that implicitly assume that other subsystems
169 // are still operational (Due to the dynamic allocation and
170 // deallocation of AIModel objects. To ensure we can safely
171 // shut down all subsystems, make sure we take down the
172 // AIModels system first.
173 SGSubsystem* ai = subsystem_mgr->remove("ai-model");
178 SGSubsystem* sound = subsystem_mgr->remove("sound");
180 subsystem_mgr->shutdown();
181 subsystem_mgr->unbind();
182 delete subsystem_mgr;
200 delete channel_options_list;
201 delete initial_waypoints;
218 // set the fg_root path
219 void FGGlobals::set_fg_root (const string &root) {
221 fg_root = tmp.realpath();
223 // append /data to root if it exists
224 tmp.append( "data" );
225 tmp.append( "version" );
226 if ( tmp.exists() ) {
227 fgGetNode("BAD_FG_ROOT", true)->setStringValue(fg_root);
229 fgGetNode("GOOD_FG_ROOT", true)->setStringValue(fg_root);
230 SG_LOG(SG_GENERAL, SG_ALERT, "***\n***\n*** Warning: changing bad FG_ROOT/--fg-root to '"
231 << fg_root << "'\n***\n***");
234 // remove /sim/fg-root before writing to prevent hijacking
235 SGPropertyNode *n = fgGetNode("/sim", true);
236 n->removeChild("fg-root", 0, false);
237 n = n->getChild("fg-root", 0, true);
238 n->setStringValue(fg_root.c_str());
239 n->setAttribute(SGPropertyNode::WRITE, false);
241 simgear::ResourceManager::instance()->addBasePath(fg_root,
242 simgear::ResourceManager::PRIORITY_DEFAULT);
245 // set the fg_home path
246 void FGGlobals::set_fg_home (const string &home) {
248 fg_home = tmp.realpath();
251 void FGGlobals::append_fg_scenery (const string &paths)
253 // fg_scenery.clear();
254 SGPropertyNode* sim = fgGetNode("/sim", true);
256 // find first unused fg-scenery property in /sim
258 while (sim->getChild("fg-scenery", propIndex) != NULL) {
262 BOOST_FOREACH(const SGPath& path, sgPathSplit( paths )) {
263 SGPath abspath(path.realpath());
264 if (!abspath.exists()) {
265 SG_LOG(SG_GENERAL, SG_WARN, "scenery path not found:" << abspath.str());
269 // check for duplicates
270 string_list::const_iterator ex = std::find(fg_scenery.begin(), fg_scenery.end(), abspath.str());
271 if (ex != fg_scenery.end()) {
272 SG_LOG(SG_GENERAL, SG_INFO, "skipping duplicate add of scenery path:" << abspath.str());
276 simgear::Dir dir(abspath);
277 SGPath terrainDir(dir.file("Terrain"));
278 SGPath objectsDir(dir.file("Objects"));
280 // this code used to add *either* the base dir, OR add the
281 // Terrain and Objects subdirs, but the conditional logic was commented
282 // out, such that all three dirs are added. Unfortunately there's
283 // no information as to why the change was made.
284 fg_scenery.push_back(abspath.str());
286 if (terrainDir.exists()) {
287 fg_scenery.push_back(terrainDir.str());
290 if (objectsDir.exists()) {
291 fg_scenery.push_back(objectsDir.str());
294 // insert a marker for FGTileEntry::load(), so that
295 // FG_SCENERY=A:B becomes list ["A/Terrain", "A/Objects", "",
296 // "B/Terrain", "B/Objects", ""]
297 fg_scenery.push_back("");
299 // make scenery dirs available to Nasal
300 SGPropertyNode* n = sim->getChild("fg-scenery", propIndex++, true);
301 n->setStringValue(abspath.str());
302 n->setAttribute(SGPropertyNode::WRITE, false);
303 } // of path list iteration
306 void FGGlobals::append_aircraft_path(const std::string& path)
308 SGPath dirPath(path);
309 if (!dirPath.exists()) {
310 SG_LOG(SG_GENERAL, SG_WARN, "aircraft path not found:" << path);
313 std::string abspath = dirPath.realpath();
315 unsigned int index = fg_aircraft_dirs.size();
316 fg_aircraft_dirs.push_back(abspath);
318 // make aircraft dirs available to Nasal
319 SGPropertyNode* sim = fgGetNode("/sim", true);
320 sim->removeChild("fg-aircraft", index, false);
321 SGPropertyNode* n = sim->getChild("fg-aircraft", index, true);
322 n->setStringValue(abspath);
323 n->setAttribute(SGPropertyNode::WRITE, false);
326 void FGGlobals::append_aircraft_paths(const std::string& path)
328 string_list paths = sgPathSplit(path);
329 for (unsigned int p = 0; p<paths.size(); ++p) {
330 append_aircraft_path(paths[p]);
334 SGPath FGGlobals::resolve_aircraft_path(const std::string& branch) const
336 return simgear::ResourceManager::instance()->findPath(branch);
339 SGPath FGGlobals::resolve_maybe_aircraft_path(const std::string& branch) const
341 return simgear::ResourceManager::instance()->findPath(branch);
344 SGPath FGGlobals::resolve_ressource_path(const std::string& branch) const
346 return simgear::ResourceManager::instance()
347 ->findPath(branch, SGPath(fgGetString("/sim/aircraft-dir")));
351 FGGlobals::get_renderer () const
357 FGGlobals::get_subsystem_mgr () const
359 return subsystem_mgr;
363 FGGlobals::get_subsystem (const char * name)
365 return subsystem_mgr->get_subsystem(name);
369 FGGlobals::add_subsystem (const char * name,
370 SGSubsystem * subsystem,
371 SGSubsystemMgr::GroupType type,
374 subsystem_mgr->add(name, subsystem, type, min_time_sec);
378 FGGlobals::get_soundmgr () const
381 return (SGSoundMgr*) subsystem_mgr->get_subsystem("sound");
387 FGGlobals::get_event_mgr () const
393 FGGlobals::get_aircraft_position() const
395 if( acmodel != NULL ) {
396 SGModelPlacement * mp = acmodel->get3DModel();
398 return mp->getPosition();
400 throw sg_exception("Can't get aircraft position", "FGGlobals::get_aircraft_position()" );
404 FGGlobals::get_aircraft_positon_cart() const
406 return SGVec3d::fromGeod(get_aircraft_position());
410 // Save the current state as the initial state.
412 FGGlobals::saveInitialState ()
414 initial_state = new SGPropertyNode();
416 // copy properties which are READ/WRITEable - but not USERARCHIVEd or PRESERVEd
417 int checked = SGPropertyNode::READ+SGPropertyNode::WRITE+
418 SGPropertyNode::USERARCHIVE+SGPropertyNode::PRESERVE;
419 int expected = SGPropertyNode::READ+SGPropertyNode::WRITE;
420 if (!copyProperties(props, initial_state, expected, checked))
421 SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
423 // delete various properties from the initial state, since we want to
424 // preserve their values even if doing a restore
425 // => Properties should now use the PRESERVE flag to protect their values
426 // on sim-reset. Remove some specific properties for backward compatibility.
427 SGPropertyNode* sim = initial_state->getChild("sim");
428 SGPropertyNode* cameraGroupNode = sim->getNode("rendering/camera-group");
429 if (cameraGroupNode) {
430 cameraGroupNode->removeChild("camera");
431 cameraGroupNode->removeChild("gui");
436 // Restore the saved initial state, if any
438 FGGlobals::restoreInitialState ()
440 if ( initial_state == 0 ) {
441 SG_LOG(SG_GENERAL, SG_ALERT,
442 "No initial state available to restore!!!");
445 // copy properties which are READ/WRITEable - but not USERARCHIVEd or PRESERVEd
446 int checked = SGPropertyNode::READ+SGPropertyNode::WRITE+
447 SGPropertyNode::USERARCHIVE+SGPropertyNode::PRESERVE;
448 int expected = SGPropertyNode::READ+SGPropertyNode::WRITE;
449 if ( copyProperties(initial_state, props, expected, checked)) {
450 SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
452 SG_LOG( SG_GENERAL, SG_INFO,
453 "Some errors restoring initial state (read-only props?)" );
458 // Load user settings from autosave.xml
460 FGGlobals::loadUserSettings(const SGPath& dataPath)
462 // remember that we have (tried) to load any existing autsave.xml
463 haveUserSettings = true;
465 SGPath autosaveFile = simgear::Dir(dataPath).file("autosave.xml");
466 SGPropertyNode autosave;
467 if (autosaveFile.exists()) {
468 SG_LOG(SG_INPUT, SG_INFO, "Reading user settings from " << autosaveFile.str());
470 readProperties(autosaveFile.str(), &autosave, SGPropertyNode::USERARCHIVE);
471 } catch (sg_exception& e) {
472 SG_LOG(SG_INPUT, SG_WARN, "failed to read user settings:" << e.getMessage()
473 << "(from " << e.getOrigin() << ")");
476 copyProperties(&autosave, globals->get_props());
479 // Save user settings in autosave.xml
481 FGGlobals::saveUserSettings()
483 // only save settings when we have (tried) to load the previous
484 // settings (otherwise user data was lost)
485 if (!haveUserSettings)
488 if (fgGetBool("/sim/startup/save-on-exit")) {
489 // don't save settings more than once on shutdown
490 haveUserSettings = false;
492 SGPath autosaveFile(globals->get_fg_home());
493 autosaveFile.append( "autosave.xml" );
494 autosaveFile.create_dir( 0700 );
495 SG_LOG(SG_IO, SG_INFO, "Saving user settings to " << autosaveFile.str());
497 writeProperties(autosaveFile.str(), globals->get_props(), false, SGPropertyNode::USERARCHIVE);
498 } catch (const sg_exception &e) {
499 guiErrorMessage("Error writing autosave.xml: ", e);
501 SG_LOG(SG_INPUT, SG_DEBUG, "Finished Saving user settings");
506 FGGlobals::get_current_view () const
508 return viewmgr->get_current_view();
511 long int FGGlobals::get_warp() const
513 return fgGetInt("/sim/time/warp");
516 void FGGlobals::set_warp( long int w )
518 fgSetInt("/sim/time/warp", w);
521 long int FGGlobals::get_warp_delta() const
523 return fgGetInt("/sim/time/warp-delta");
526 void FGGlobals::set_warp_delta( long int d )
528 fgSetInt("/sim/time/warp-delta", d);
531 // end of globals.cxx