| 1 | // globals.cxx -- Global state that needs to be shared among the sim modules |
| 2 | // |
| 3 | // Written by Curtis Olson, started July 2000. |
| 4 | // |
| 5 | // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt |
| 6 | // |
| 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. |
| 11 | // |
| 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. |
| 16 | // |
| 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. |
| 20 | // |
| 21 | // $Id$ |
| 22 | |
| 23 | #include <config.h> |
| 24 | |
| 25 | #include <algorithm> |
| 26 | |
| 27 | #include <osgViewer/Viewer> |
| 28 | #include <osgDB/Registry> |
| 29 | |
| 30 | #include <simgear/structure/commands.hxx> |
| 31 | #include <simgear/structure/exception.hxx> |
| 32 | #include <simgear/misc/sg_path.hxx> |
| 33 | #include <simgear/misc/sg_dir.hxx> |
| 34 | #include <simgear/timing/sg_time.hxx> |
| 35 | #include <simgear/ephemeris/ephemeris.hxx> |
| 36 | #include <simgear/structure/subsystem_mgr.hxx> |
| 37 | #include <simgear/structure/event_mgr.hxx> |
| 38 | |
| 39 | #include <simgear/misc/ResourceManager.hxx> |
| 40 | #include <simgear/props/propertyObject.hxx> |
| 41 | #include <simgear/props/props_io.hxx> |
| 42 | #include <simgear/props/AtomicChangeListener.hxx> |
| 43 | #include <simgear/scene/model/modellib.hxx> |
| 44 | #include <simgear/package/Root.hxx> |
| 45 | |
| 46 | #include <Add-ons/AddonResourceProvider.hxx> |
| 47 | #include <Aircraft/controls.hxx> |
| 48 | #include <Airports/runways.hxx> |
| 49 | #include <Autopilot/route_mgr.hxx> |
| 50 | #include <Navaids/navlist.hxx> |
| 51 | |
| 52 | #include <GUI/gui.h> |
| 53 | #include <Main/sentryIntegration.hxx> |
| 54 | #include <Viewer/viewmgr.hxx> |
| 55 | |
| 56 | #include <Scenery/scenery.hxx> |
| 57 | #include <Scenery/tilemgr.hxx> |
| 58 | #include <Viewer/VRManager.hxx> |
| 59 | #include <Viewer/renderer.hxx> |
| 60 | #include <GUI/FGFontCache.hxx> |
| 61 | #include <GUI/MessageBox.hxx> |
| 62 | |
| 63 | #include <simgear/sound/soundmgr.hxx> |
| 64 | #include <simgear/scene/material/matlib.hxx> |
| 65 | |
| 66 | #include "globals.hxx" |
| 67 | #include "locale.hxx" |
| 68 | |
| 69 | #include "fg_props.hxx" |
| 70 | #include "fg_io.hxx" |
| 71 | |
| 72 | class AircraftResourceProvider : public simgear::ResourceProvider |
| 73 | { |
| 74 | public: |
| 75 | AircraftResourceProvider() : |
| 76 | simgear::ResourceProvider(simgear::ResourceManager::PRIORITY_HIGH) |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | virtual SGPath resolve(const std::string& aResource, SGPath&) const |
| 81 | { |
| 82 | string_list pieces(sgPathBranchSplit(aResource)); |
| 83 | if ((pieces.size() < 3) || (pieces.front() != "Aircraft")) { |
| 84 | return SGPath(); // not an Aircraft path |
| 85 | } |
| 86 | |
| 87 | // test against the aircraft-dir property |
| 88 | const std::string aircraftDir = fgGetString("/sim/aircraft-dir"); |
| 89 | string_list aircraftDirPieces(sgPathBranchSplit(aircraftDir)); |
| 90 | if (!aircraftDirPieces.empty() && (aircraftDirPieces.back() == pieces[1])) { |
| 91 | // current aircraft-dir matches resource aircraft |
| 92 | SGPath r(aircraftDir); |
| 93 | for (unsigned int i=2; i<pieces.size(); ++i) { |
| 94 | r.append(pieces[i]); |
| 95 | } |
| 96 | |
| 97 | if (r.exists()) { |
| 98 | return r; |
| 99 | } else { |
| 100 | // Stop here, otherwise we could end up returning a resource that |
| 101 | // belongs to an unrelated version of the same aircraft (from a |
| 102 | // different aircraft directory). |
| 103 | return SGPath(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // try each aircraft dir in turn |
| 108 | std::string res(aResource, 9); // resource path with 'Aircraft/' removed |
| 109 | const PathList& dirs(globals->get_aircraft_paths()); |
| 110 | PathList::const_iterator it = dirs.begin(); |
| 111 | for (; it != dirs.end(); ++it) { |
| 112 | SGPath p(*it); |
| 113 | p.append(res); |
| 114 | if (p.exists()) { |
| 115 | return p; |
| 116 | } |
| 117 | } // of aircraft path iteration |
| 118 | |
| 119 | return SGPath(); // not found |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | class CurrentAircraftDirProvider : public simgear::ResourceProvider |
| 124 | { |
| 125 | public: |
| 126 | CurrentAircraftDirProvider() : |
| 127 | simgear::ResourceProvider(simgear::ResourceManager::PRIORITY_HIGH) |
| 128 | { |
| 129 | } |
| 130 | |
| 131 | virtual SGPath resolve(const std::string& aResource, SGPath&) const |
| 132 | { |
| 133 | SGPath p = SGPath::fromUtf8(fgGetString("/sim/aircraft-dir")); |
| 134 | p.append(aResource); |
| 135 | return p.exists() ? p : SGPath(); |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | //////////////////////////////////////////////////////////////////////// |
| 140 | // Implementation of FGGlobals. |
| 141 | //////////////////////////////////////////////////////////////////////// |
| 142 | |
| 143 | // global global :-) |
| 144 | FGGlobals *globals = NULL; |
| 145 | |
| 146 | |
| 147 | // Constructor |
| 148 | FGGlobals::FGGlobals() : |
| 149 | renderer( new FGRenderer ), |
| 150 | subsystem_mgr( new SGSubsystemMgr ), |
| 151 | event_mgr( new SGEventMgr ), |
| 152 | sim_time_sec( 0.0 ), |
| 153 | fg_root( "" ), |
| 154 | fg_home( "" ), |
| 155 | time_params( NULL ), |
| 156 | commands( SGCommandMgr::instance() ), |
| 157 | channel_options_list( NULL ), |
| 158 | initial_waypoints( NULL ), |
| 159 | channellist( NULL ), |
| 160 | haveUserSettings(false) |
| 161 | { |
| 162 | SGPropertyNode* root = new SGPropertyNode; |
| 163 | props = SGPropertyNode_ptr(root); |
| 164 | locale = new FGLocale(props); |
| 165 | |
| 166 | auto resMgr = simgear::ResourceManager::instance(); |
| 167 | resMgr->addProvider(new AircraftResourceProvider()); |
| 168 | resMgr->addProvider(new CurrentAircraftDirProvider()); |
| 169 | resMgr->addProvider(new flightgear::addons::ResourceProvider()); |
| 170 | initProperties(); |
| 171 | } |
| 172 | |
| 173 | void FGGlobals::initProperties() |
| 174 | { |
| 175 | simgear::PropertyObjectBase::setDefaultRoot(props); |
| 176 | |
| 177 | positionLon = props->getNode("position/longitude-deg", true); |
| 178 | positionLat = props->getNode("position/latitude-deg", true); |
| 179 | positionAlt = props->getNode("position/altitude-ft", true); |
| 180 | |
| 181 | viewLon = props->getNode("sim/current-view/viewer-lon-deg", true); |
| 182 | viewLat = props->getNode("sim/current-view/viewer-lat-deg", true); |
| 183 | viewAlt = props->getNode("sim/current-view/viewer-elev-ft", true); |
| 184 | |
| 185 | referenceOffsetX = props->getNode("sim/model/reference-offset-x", true); |
| 186 | referenceOffsetY = props->getNode("sim/model/reference-offset-y", true); |
| 187 | referenceOffsetZ = props->getNode("sim/model/reference-offset-z", true); |
| 188 | |
| 189 | orientPitch = props->getNode("orientation/pitch-deg", true); |
| 190 | orientHeading = props->getNode("orientation/heading-deg", true); |
| 191 | orientRoll = props->getNode("orientation/roll-deg", true); |
| 192 | |
| 193 | } |
| 194 | |
| 195 | // Destructor |
| 196 | FGGlobals::~FGGlobals() |
| 197 | { |
| 198 | // save user settings (unless already saved) |
| 199 | saveUserSettings(); |
| 200 | |
| 201 | // stop OSG threading first, to avoid thread races while we tear down |
| 202 | // scene-graph pieces |
| 203 | // there are some scenarios where renderer is already gone. |
| 204 | osg::ref_ptr<osgViewer::ViewerBase> vb; |
| 205 | if (renderer) { |
| 206 | vb = renderer->getViewerBase(); |
| 207 | if (vb) { |
| 208 | // https://code.google.com/p/flightgear-bugs/issues/detail?id=1291 |
| 209 | // explicitly stop trheading before we delete the renderer or |
| 210 | // viewMgr (which ultimately holds refs to the CameraGroup, and |
| 211 | // GraphicsContext) |
| 212 | vb->stopThreading(); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | subsystem_mgr->shutdown(); |
| 217 | #ifdef ENABLE_OSGXR |
| 218 | flightgear::VRManager::destroyInstance(); |
| 219 | #endif |
| 220 | subsystem_mgr->unbind(); |
| 221 | |
| 222 | // don't cancel the pager until after shutdown, since AIModels (and |
| 223 | // potentially others) can queue delete requests on the pager. |
| 224 | osgViewer::View* v = renderer->getView(); |
| 225 | if (v && v->getDatabasePager()) { |
| 226 | v->getDatabasePager()->cancel(); |
| 227 | v->getDatabasePager()->clear(); |
| 228 | } |
| 229 | |
| 230 | osgDB::Registry::instance()->clearObjectCache(); |
| 231 | if (subsystem_mgr->get_subsystem(FGScenery::staticSubsystemClassId())) { |
| 232 | subsystem_mgr->remove(FGScenery::staticSubsystemClassId()); |
| 233 | } |
| 234 | |
| 235 | // renderer touches subsystems during its destruction |
| 236 | set_renderer(nullptr); |
| 237 | |
| 238 | FGFontCache::shutdown(); |
| 239 | fgCancelSnapShot(); |
| 240 | |
| 241 | delete subsystem_mgr; |
| 242 | subsystem_mgr = nullptr; // important so ::get_subsystem returns NULL |
| 243 | vb = nullptr; |
| 244 | set_matlib(NULL); |
| 245 | |
| 246 | delete time_params; |
| 247 | delete channel_options_list; |
| 248 | delete initial_waypoints; |
| 249 | delete channellist; |
| 250 | |
| 251 | // delete commands before we release the property root |
| 252 | // this avoids crash where commands might be storing a propery |
| 253 | // ref/pointer. |
| 254 | // see https://sentry.io/organizations/flightgear/issues/1890563449 |
| 255 | delete commands; |
| 256 | commands = nullptr; |
| 257 | |
| 258 | simgear::PropertyObjectBase::setDefaultRoot(NULL); |
| 259 | simgear::SGModelLib::resetPropertyRoot(); |
| 260 | delete locale; |
| 261 | locale = NULL; |
| 262 | |
| 263 | cleanupListeners(); |
| 264 | |
| 265 | props.clear(); |
| 266 | |
| 267 | delete simgear::ResourceManager::instance(); |
| 268 | } |
| 269 | |
| 270 | // set the fg_root path |
| 271 | void FGGlobals::set_fg_root (const SGPath &root) { |
| 272 | SGPath tmp(root); |
| 273 | fg_root = tmp.realpath(); |
| 274 | |
| 275 | // append /data to root if it exists |
| 276 | tmp.append( "data" ); |
| 277 | tmp.append( "version" ); |
| 278 | if ( tmp.exists() ) { |
| 279 | fgGetNode("BAD_FG_ROOT", true)->setStringValue(fg_root.utf8Str()); |
| 280 | fg_root.append("data"); |
| 281 | fgGetNode("GOOD_FG_ROOT", true)->setStringValue(fg_root.utf8Str()); |
| 282 | SG_LOG(SG_GENERAL, SG_ALERT, "***\n***\n*** Warning: changing bad FG_ROOT/--fg-root to '" |
| 283 | << fg_root << "'\n***\n***"); |
| 284 | } |
| 285 | |
| 286 | // deliberately not a tied property, for SGPath::validate() security |
| 287 | // write-protect to avoid accidents |
| 288 | SGPropertyNode *n = fgGetNode("/sim", true); |
| 289 | n->removeChild("fg-root", 0); |
| 290 | n = n->getChild("fg-root", 0, true); |
| 291 | n->setStringValue(fg_root.utf8Str()); |
| 292 | n->setAttribute(SGPropertyNode::WRITE, false); |
| 293 | |
| 294 | simgear::ResourceManager::instance()->addBasePath(fg_root, |
| 295 | simgear::ResourceManager::PRIORITY_DEFAULT); |
| 296 | } |
| 297 | |
| 298 | // set the fg_home path |
| 299 | void FGGlobals::set_fg_home (const SGPath &home) |
| 300 | { |
| 301 | fg_home = home.realpath(); |
| 302 | } |
| 303 | |
| 304 | void FGGlobals::set_texture_cache_dir(const SGPath &textureCache) |
| 305 | { |
| 306 | texture_cache_dir = textureCache.realpath(); |
| 307 | auto node = fgGetNode("/sim/rendering/texture-cache/dir", true); |
| 308 | node->setAttribute(SGPropertyNode::WRITE, true); |
| 309 | node->setStringValue(textureCache.utf8Str()); |
| 310 | node->setAttribute(SGPropertyNode::WRITE, false); |
| 311 | } |
| 312 | |
| 313 | |
| 314 | PathList FGGlobals::get_data_paths() const |
| 315 | { |
| 316 | PathList r(additional_data_paths); |
| 317 | r.push_back(fg_root); |
| 318 | r.insert(r.end(), _dataPathsAfterFGRoot.begin(), _dataPathsAfterFGRoot.end()); |
| 319 | return r; |
| 320 | } |
| 321 | |
| 322 | PathList FGGlobals::get_data_paths(const std::string& suffix) const |
| 323 | { |
| 324 | PathList r; |
| 325 | for (SGPath p : get_data_paths()) { |
| 326 | p.append(suffix); |
| 327 | if (p.exists()) { |
| 328 | r.push_back(p); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | return r; |
| 333 | } |
| 334 | |
| 335 | void FGGlobals::append_data_path(const SGPath& path, bool afterFGRoot) |
| 336 | { |
| 337 | if (!path.exists()) { |
| 338 | SG_LOG(SG_GENERAL, SG_WARN, "adding non-existant data path:" << path); |
| 339 | } |
| 340 | |
| 341 | using RM = simgear::ResourceManager; |
| 342 | auto resManager = RM::instance(); |
| 343 | if (afterFGRoot) { |
| 344 | _dataPathsAfterFGRoot.push_back(path); |
| 345 | // after FG_ROOT |
| 346 | resManager->addBasePath(path, static_cast<RM::Priority>(RM::PRIORITY_DEFAULT - 10)); |
| 347 | } else { |
| 348 | additional_data_paths.push_back(path); |
| 349 | // after NORMAL prioirty, but ahead of FG_ROOT |
| 350 | resManager->addBasePath(path, static_cast<RM::Priority>(RM::PRIORITY_DEFAULT + 10)); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | SGPath FGGlobals::findDataPath(const std::string& pathSuffix) const |
| 355 | { |
| 356 | for (SGPath p : additional_data_paths) { |
| 357 | p.append(pathSuffix); |
| 358 | if (p.exists()) { |
| 359 | return p; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | SGPath rootPath(fg_root); |
| 364 | rootPath.append(pathSuffix); |
| 365 | if (rootPath.exists()) { |
| 366 | return rootPath; |
| 367 | } |
| 368 | |
| 369 | for (SGPath p : _dataPathsAfterFGRoot) { |
| 370 | p.append(pathSuffix); |
| 371 | if (p.exists()) { |
| 372 | return p; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | return SGPath{}; |
| 377 | } |
| 378 | |
| 379 | void FGGlobals::append_fg_scenery (const PathList &paths) |
| 380 | { |
| 381 | for (const SGPath& path : paths) { |
| 382 | append_fg_scenery(path); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | void FGGlobals::append_fg_scenery (const SGPath &path) |
| 387 | { |
| 388 | SGPropertyNode* sim = fgGetNode("/sim", true); |
| 389 | |
| 390 | // find first unused fg-scenery property in /sim |
| 391 | int propIndex = 0; |
| 392 | while (sim->getChild("fg-scenery", propIndex) != NULL) { |
| 393 | ++propIndex; |
| 394 | } |
| 395 | |
| 396 | SGPath abspath(path.realpath()); |
| 397 | if (!abspath.exists()) { |
| 398 | SG_LOG(SG_GENERAL, SG_WARN, "scenery path not found:" << abspath); |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | // check for duplicates |
| 403 | PathList::const_iterator ex = std::find(fg_scenery.begin(), fg_scenery.end(), abspath); |
| 404 | if (ex != fg_scenery.end()) { |
| 405 | SG_LOG(SG_GENERAL, SG_INFO, "skipping duplicate add of scenery path:" << abspath); |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | // tell the ResourceManager about the scenery path |
| 410 | // needed to load Models from this scenery path |
| 411 | simgear::ResourceManager::instance()->addBasePath(abspath, simgear::ResourceManager::PRIORITY_DEFAULT); |
| 412 | |
| 413 | fg_scenery.push_back(abspath); |
| 414 | extra_read_allowed_paths.push_back(abspath); |
| 415 | |
| 416 | // make scenery dirs available to Nasal |
| 417 | SGPropertyNode* n = sim->getChild("fg-scenery", propIndex++, true); |
| 418 | n->setStringValue(abspath.utf8Str()); |
| 419 | n->setAttribute(SGPropertyNode::WRITE, false); |
| 420 | |
| 421 | // temporary fix so these values survive reset |
| 422 | n->setAttribute(SGPropertyNode::PRESERVE, true); |
| 423 | |
| 424 | // Add any sources.xml file to the property tree so we can provide attribution through the GUI |
| 425 | SGPath sources = SGPath(abspath); |
| 426 | sources.append("sources.xml"); |
| 427 | SG_LOG(SG_TERRAIN, SG_DEBUG, "Looking for source file " << sources << ". Exists ? " << sources.exists()); |
| 428 | if (sources.exists()) { |
| 429 | // Determine the next free indice under /scenery/sources |
| 430 | SGPropertyNode* sourcesProp = fgGetNode("/scenery/sources", true); |
| 431 | int propIndex = 0; |
| 432 | while (sourcesProp->getChild("directory", propIndex) != NULL) { |
| 433 | ++propIndex; |
| 434 | } |
| 435 | |
| 436 | sourcesProp = sourcesProp->getChild("directory", propIndex++, true); |
| 437 | |
| 438 | // Add a reference to the path itself |
| 439 | sourcesProp->setStringValue("path", abspath.utf8Str()); |
| 440 | |
| 441 | // Now load the sources file into /scenery/sources/source[n] |
| 442 | if(!fgLoadProps(sources.utf8Str(), sourcesProp, false)) |
| 443 | { |
| 444 | SG_LOG(SG_TERRAIN, SG_ALERT, "Unable to load sources file " << sources.utf8Str()); |
| 445 | } |
| 446 | |
| 447 | // Ensure these properties cannot be over-ridden, and preserve them across a reset. |
| 448 | sourcesProp->setAttribute(SGPropertyNode::WRITE, false); |
| 449 | sourcesProp->setAttribute(SGPropertyNode::PRESERVE, true); |
| 450 | } |
| 451 | |
| 452 | } |
| 453 | |
| 454 | void FGGlobals::append_read_allowed_paths(const SGPath &path) |
| 455 | { |
| 456 | SGPath abspath(path.realpath()); |
| 457 | if (!abspath.exists()) { |
| 458 | SG_LOG(SG_IO, SG_DEBUG, "read-allowed path not found:" << abspath); |
| 459 | return; |
| 460 | } |
| 461 | extra_read_allowed_paths.push_back(abspath); |
| 462 | } |
| 463 | |
| 464 | void FGGlobals::clear_fg_scenery() |
| 465 | { |
| 466 | fg_scenery.clear(); |
| 467 | fgGetNode("/sim", true)->removeChildren("fg-scenery"); |
| 468 | } |
| 469 | |
| 470 | // The 'path' argument to this method must come from trustworthy code, because |
| 471 | // the method grants read permissions to Nasal code for many files beneath |
| 472 | // 'path'. In particular, don't call this method with a 'path' value taken |
| 473 | // from the property tree or any other Nasal-writable place. |
| 474 | void FGGlobals::set_download_dir(const SGPath& path) |
| 475 | { |
| 476 | SGPath abspath(path.realpath()); |
| 477 | download_dir = abspath; |
| 478 | |
| 479 | append_read_allowed_paths(abspath / "Aircraft"); |
| 480 | append_read_allowed_paths(abspath / "AI"); |
| 481 | append_read_allowed_paths(abspath / "Liveries"); |
| 482 | // If in use, abspath / TerraSync will be added to 'extra_read_allowed_paths' |
| 483 | // by FGGlobals::append_fg_scenery(), as any scenery path. |
| 484 | |
| 485 | SGPropertyNode *n = fgGetNode("/sim/paths/download-dir", true); |
| 486 | n->setAttribute(SGPropertyNode::WRITE, true); |
| 487 | n->setStringValue(abspath.utf8Str()); |
| 488 | n->setAttribute(SGPropertyNode::WRITE, false); |
| 489 | } |
| 490 | |
| 491 | // The 'path' argument to this method must come from trustworthy code, because |
| 492 | // the method grants read permissions to Nasal code for all files beneath |
| 493 | // 'path'. In particular, don't call this method with a 'path' value taken |
| 494 | // from the property tree or any other Nasal-writable place. |
| 495 | void FGGlobals::set_terrasync_dir(const SGPath &path) |
| 496 | { |
| 497 | if (terrasync_dir.realpath() != SGPath(fgGetString("/sim/terrasync/scenery-dir")).realpath()) { |
| 498 | // if they don't match, /sim/terrasync/scenery-dir has been set by something else |
| 499 | SG_LOG(SG_GENERAL, SG_WARN, "/sim/terrasync/scenery-dir is no longer stored across runs: if you wish to keep using a non-standard Terrasync directory, use --terrasync-dir or the launcher's settings"); |
| 500 | } |
| 501 | SGPath abspath(path.realpath()); |
| 502 | terrasync_dir = abspath; |
| 503 | // deliberately not a tied property, for SGPath::validate() security |
| 504 | // write-protect to avoid accidents |
| 505 | SGPropertyNode *n = fgGetNode("/sim/terrasync/scenery-dir", true); |
| 506 | n->setAttribute(SGPropertyNode::WRITE, true); |
| 507 | n->setStringValue(abspath.utf8Str()); |
| 508 | n->setAttribute(SGPropertyNode::WRITE, false); |
| 509 | // don't add it to fg_scenery yet, as we want it ordered after explicit --fg-scenery |
| 510 | } |
| 511 | |
| 512 | |
| 513 | |
| 514 | void FGGlobals::set_catalog_aircraft_path(const SGPath& path) |
| 515 | { |
| 516 | catalog_aircraft_dir = path; |
| 517 | } |
| 518 | |
| 519 | PathList FGGlobals::get_aircraft_paths() const |
| 520 | { |
| 521 | PathList r; |
| 522 | if (!catalog_aircraft_dir.isNull()) { |
| 523 | r.push_back(catalog_aircraft_dir); |
| 524 | } |
| 525 | |
| 526 | r.insert(r.end(), fg_aircraft_dirs.begin(), fg_aircraft_dirs.end()); |
| 527 | return r; |
| 528 | } |
| 529 | |
| 530 | void FGGlobals::append_aircraft_path(const SGPath& path) |
| 531 | { |
| 532 | SGPath dirPath(path); |
| 533 | if (!dirPath.exists()) { |
| 534 | SG_LOG(SG_GENERAL, SG_WARN, "aircraft path not found:" << path); |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | SGPath acSubdir(dirPath); |
| 539 | acSubdir.append("Aircraft"); |
| 540 | if (acSubdir.exists()) { |
| 541 | SG_LOG( |
| 542 | SG_GENERAL, |
| 543 | SG_WARN, |
| 544 | "Specified an aircraft-dir with an 'Aircraft' subdirectory:" << dirPath |
| 545 | << ", will instead use child directory:" << acSubdir |
| 546 | ); |
| 547 | dirPath = acSubdir; |
| 548 | } |
| 549 | |
| 550 | fg_aircraft_dirs.push_back(dirPath.realpath()); |
| 551 | extra_read_allowed_paths.push_back(dirPath.realpath()); |
| 552 | } |
| 553 | |
| 554 | void FGGlobals::append_aircraft_paths(const PathList& paths) |
| 555 | { |
| 556 | for (unsigned int p = 0; p<paths.size(); ++p) { |
| 557 | append_aircraft_path(paths[p]); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | SGPath FGGlobals::resolve_aircraft_path(const std::string& branch) const |
| 562 | { |
| 563 | return simgear::ResourceManager::instance()->findPath(branch); |
| 564 | } |
| 565 | |
| 566 | SGPath FGGlobals::resolve_maybe_aircraft_path(const std::string& branch) const |
| 567 | { |
| 568 | return simgear::ResourceManager::instance()->findPath(branch); |
| 569 | } |
| 570 | |
| 571 | SGPath FGGlobals::resolve_resource_path(const std::string& branch) const |
| 572 | { |
| 573 | return simgear::ResourceManager::instance() |
| 574 | ->findPath(branch, SGPath(fgGetString("/sim/aircraft-dir"))); |
| 575 | } |
| 576 | |
| 577 | FGRenderer * |
| 578 | FGGlobals::get_renderer () const |
| 579 | { |
| 580 | return renderer; |
| 581 | } |
| 582 | |
| 583 | void FGGlobals::set_renderer(FGRenderer *render) |
| 584 | { |
| 585 | if (render == renderer) { |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | delete renderer; |
| 590 | renderer = render; |
| 591 | } |
| 592 | |
| 593 | SGSubsystemMgr * |
| 594 | FGGlobals::get_subsystem_mgr () const |
| 595 | { |
| 596 | return subsystem_mgr; |
| 597 | } |
| 598 | |
| 599 | SGSubsystem * |
| 600 | FGGlobals::get_subsystem (const char * name) const |
| 601 | { |
| 602 | if (!subsystem_mgr) { |
| 603 | return NULL; |
| 604 | } |
| 605 | |
| 606 | return subsystem_mgr->get_subsystem(name); |
| 607 | } |
| 608 | |
| 609 | SGEventMgr * |
| 610 | FGGlobals::get_event_mgr () const |
| 611 | { |
| 612 | return event_mgr; |
| 613 | } |
| 614 | |
| 615 | SGGeod |
| 616 | FGGlobals::get_aircraft_position() const |
| 617 | { |
| 618 | return SGGeod::fromDegFt(positionLon->getDoubleValue(), |
| 619 | positionLat->getDoubleValue(), |
| 620 | positionAlt->getDoubleValue()); |
| 621 | } |
| 622 | |
| 623 | SGVec3d |
| 624 | FGGlobals::get_aircraft_position_cart() const |
| 625 | { |
| 626 | return SGVec3d::fromGeod(get_aircraft_position()); |
| 627 | } |
| 628 | |
| 629 | void FGGlobals::get_aircraft_orientation(double& heading, double& pitch, double& roll) |
| 630 | { |
| 631 | heading = orientHeading->getDoubleValue(); |
| 632 | pitch = orientPitch->getDoubleValue(); |
| 633 | roll = orientRoll->getDoubleValue(); |
| 634 | } |
| 635 | |
| 636 | SGGeod |
| 637 | FGGlobals::get_view_position() const |
| 638 | { |
| 639 | return SGGeod::fromDegFt(viewLon->getDoubleValue(), |
| 640 | viewLat->getDoubleValue(), |
| 641 | viewAlt->getDoubleValue()); |
| 642 | } |
| 643 | |
| 644 | SGVec3d |
| 645 | FGGlobals::get_view_position_cart() const |
| 646 | { |
| 647 | return SGVec3d::fromGeod(get_view_position()); |
| 648 | } |
| 649 | SGVec3d FGGlobals::get_ownship_reference_position_cart() const |
| 650 | { |
| 651 | SGVec3d pos = get_aircraft_position_cart(); |
| 652 | |
| 653 | if (referenceOffsetX) |
| 654 | pos[0] += referenceOffsetX->getDoubleValue(); |
| 655 | |
| 656 | if (referenceOffsetY) |
| 657 | pos[1] += referenceOffsetY->getDoubleValue(); |
| 658 | |
| 659 | if (referenceOffsetZ) |
| 660 | pos[2] += referenceOffsetZ->getDoubleValue(); |
| 661 | |
| 662 | return pos; |
| 663 | } |
| 664 | |
| 665 | static void treeDumpRefCounts(int depth, SGPropertyNode* nd) |
| 666 | { |
| 667 | for (int i=0; i<nd->nChildren(); ++i) { |
| 668 | SGPropertyNode* cp = nd->getChild(i); |
| 669 | if (SGReferenced::count(cp) > 1) { |
| 670 | SG_LOG(SG_GENERAL, SG_INFO, "\t" << cp->getPath() << " refcount:" << SGReferenced::count(cp)); |
| 671 | } |
| 672 | |
| 673 | treeDumpRefCounts(depth + 1, cp); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | static void treeClearAliases(SGPropertyNode* nd) |
| 678 | { |
| 679 | if (nd->isAlias()) { |
| 680 | nd->unalias(); |
| 681 | } |
| 682 | |
| 683 | for (int i=0; i<nd->nChildren(); ++i) { |
| 684 | SGPropertyNode* cp = nd->getChild(i); |
| 685 | treeClearAliases(cp); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | void |
| 690 | FGGlobals::resetPropertyRoot() |
| 691 | { |
| 692 | delete locale; |
| 693 | |
| 694 | // avoid a warning when we processOptions after reset |
| 695 | terrasync_dir = SGPath{}; |
| 696 | |
| 697 | cleanupListeners(); |
| 698 | |
| 699 | // we don't strictly need to clear these (they will be reset when we |
| 700 | // initProperties again), but trying to reduce false-positives when dumping |
| 701 | // ref-counts. |
| 702 | positionLon.clear(); |
| 703 | positionLat.clear(); |
| 704 | positionAlt.clear(); |
| 705 | viewLon.clear(); |
| 706 | viewLat.clear(); |
| 707 | viewAlt.clear(); |
| 708 | orientPitch.clear(); |
| 709 | orientHeading.clear(); |
| 710 | orientRoll.clear(); |
| 711 | |
| 712 | // clear aliases so ref-counts are accurate when dumped |
| 713 | treeClearAliases(props); |
| 714 | |
| 715 | SG_LOG(SG_GENERAL, SG_INFO, "root props refcount:" << props.getNumRefs()); |
| 716 | treeDumpRefCounts(0, props); |
| 717 | |
| 718 | //BaseStackSnapshot::dumpAll(std::cout); |
| 719 | |
| 720 | props = new SGPropertyNode; |
| 721 | initProperties(); |
| 722 | locale = new FGLocale(props); |
| 723 | |
| 724 | // remove /sim/fg-root before writing to prevent hijacking |
| 725 | SGPropertyNode *n = props->getNode("/sim", true); |
| 726 | n->removeChild("fg-root", 0); |
| 727 | n = n->getChild("fg-root", 0, true); |
| 728 | n->setStringValue(fg_root.utf8Str()); |
| 729 | n->setAttribute(SGPropertyNode::WRITE, false); |
| 730 | } |
| 731 | |
| 732 | static std::string autosaveName() |
| 733 | { |
| 734 | std::ostringstream os; |
| 735 | string_list versionParts = simgear::strutils::split(VERSION, "."); |
| 736 | if (versionParts.size() < 2) { |
| 737 | return "autosave.xml"; |
| 738 | } |
| 739 | |
| 740 | os << "autosave_" << versionParts[0] << "_" << versionParts[1] << ".xml"; |
| 741 | return os.str(); |
| 742 | } |
| 743 | |
| 744 | SGPath FGGlobals::autosaveFilePath(SGPath userDataPath) const |
| 745 | { |
| 746 | if (userDataPath.isNull()) { |
| 747 | userDataPath = get_fg_home(); |
| 748 | } |
| 749 | |
| 750 | return simgear::Dir(userDataPath).file(autosaveName()); |
| 751 | } |
| 752 | |
| 753 | static void deleteProperties(SGPropertyNode* props, const string_list& blacklist) |
| 754 | { |
| 755 | const std::string path(props->getPath()); |
| 756 | auto it = std::find_if(blacklist.begin(), blacklist.end(), [path](const std::string& black) |
| 757 | { return simgear::strutils::matchPropPathToTemplate(path, black); }); |
| 758 | if (it != blacklist.end()) { |
| 759 | SGPropertyNode* pr = props->getParent(); |
| 760 | pr->removeChild(props); |
| 761 | return; |
| 762 | } |
| 763 | |
| 764 | // recurse |
| 765 | for (int c=0; c < props->nChildren(); ++c) { |
| 766 | deleteProperties(props->getChild(c), blacklist); |
| 767 | } |
| 768 | |
| 769 | } |
| 770 | |
| 771 | using VersionPair = std::pair<int, int>; |
| 772 | |
| 773 | static bool operator<(const VersionPair& a, const VersionPair& b) |
| 774 | { |
| 775 | if (a.first == b.first) { |
| 776 | return a.second < b.second; |
| 777 | } |
| 778 | |
| 779 | return a.first < b.first; |
| 780 | } |
| 781 | |
| 782 | static void tryAutosaveMigration(const SGPath& userDataPath, SGPropertyNode* props) |
| 783 | { |
| 784 | const string_list versionParts = simgear::strutils::split(VERSION, "."); |
| 785 | if (versionParts.size() < 2) { |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | simgear::Dir userDataDir(userDataPath); |
| 790 | SGPath migratePath; |
| 791 | VersionPair foundVersion(0, 0); |
| 792 | const VersionPair currentVersion(simgear::strutils::to_int(versionParts[0]), |
| 793 | simgear::strutils::to_int(versionParts[1])); |
| 794 | |
| 795 | for (auto previousSave : userDataDir.children(simgear::Dir::TYPE_FILE, ".xml")) { |
| 796 | const std::string base = previousSave.file_base(); |
| 797 | VersionPair v; |
| 798 | // extract version from name |
| 799 | const int matches = ::sscanf(base.c_str(), "autosave_%d_%d", &v.first, &v.second); |
| 800 | if (matches != 2) { |
| 801 | continue; |
| 802 | } |
| 803 | |
| 804 | if (currentVersion < v) { |
| 805 | // ignore autosaves from more recent versions; this happens when |
| 806 | // running unsable and stable at the same time |
| 807 | continue; |
| 808 | } |
| 809 | |
| 810 | if (v.first < 2000) { |
| 811 | // ignore autosaves from older versions, too much change to deal |
| 812 | // with. |
| 813 | continue; |
| 814 | } |
| 815 | |
| 816 | if (foundVersion < v) { |
| 817 | foundVersion = v; |
| 818 | migratePath = previousSave; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | if (!migratePath.exists()) { |
| 823 | return; |
| 824 | } |
| 825 | |
| 826 | SG_LOG(SG_GENERAL, SG_INFO, "Migrating old autosave:" << migratePath); |
| 827 | SGPropertyNode oldProps; |
| 828 | |
| 829 | try { |
| 830 | readProperties(migratePath, &oldProps, SGPropertyNode::USERARCHIVE); |
| 831 | } catch (sg_exception& e) { |
| 832 | SG_LOG(SG_GENERAL, SG_WARN, "failed to read previous user settings:" << e.getMessage() |
| 833 | << "(from " << e.getOrigin() << ")"); |
| 834 | return; |
| 835 | } |
| 836 | |
| 837 | // read migration blacklist |
| 838 | string_list blacklist; |
| 839 | SGPropertyNode_ptr blacklistNode = fgGetNode("/sim/autosave-migration/blacklist", true); |
| 840 | for (auto node : blacklistNode->getChildren("path")) { |
| 841 | blacklist.push_back(node->getStringValue()); |
| 842 | } |
| 843 | |
| 844 | // apply migration filters for each property in turn |
| 845 | deleteProperties(&oldProps, blacklist); |
| 846 | |
| 847 | // manual migrations |
| 848 | // migrate pre-2019.1 sense of /sim/mouse/invert-mouse-wheel |
| 849 | if (foundVersion.first < 2019) { |
| 850 | SGPropertyNode_ptr wheelNode = oldProps.getNode("/sim/mouse/invert-mouse-wheel"); |
| 851 | if (wheelNode) { |
| 852 | wheelNode->setBoolValue(!wheelNode->getBoolValue()); |
| 853 | } |
| 854 | } |
| 855 | // copy remaining props out |
| 856 | copyProperties(&oldProps, props); |
| 857 | |
| 858 | // we can't inform the user yet, becuase embedded resources and the locale |
| 859 | // are not done. So we set a flag and check it once those things are done. |
| 860 | fgSetBool("/sim/autosave-migration/did-migrate", true); |
| 861 | } |
| 862 | |
| 863 | // Load user settings from the autosave file (normally in $FG_HOME) |
| 864 | void |
| 865 | FGGlobals::loadUserSettings(SGPath userDataPath) |
| 866 | { |
| 867 | if (userDataPath.isNull()) { |
| 868 | userDataPath = get_fg_home(); |
| 869 | } |
| 870 | |
| 871 | // Remember that we have (tried) to load any existing autosave file |
| 872 | haveUserSettings = true; |
| 873 | |
| 874 | SGPath autosaveFile = autosaveFilePath(userDataPath); |
| 875 | SGPropertyNode autosave; |
| 876 | if (autosaveFile.exists()) { |
| 877 | SG_LOG(SG_INPUT, SG_INFO, |
| 878 | "Reading user settings from " << autosaveFile); |
| 879 | try { |
| 880 | readProperties(autosaveFile, &autosave, SGPropertyNode::USERARCHIVE); |
| 881 | } catch (sg_exception& e) { |
| 882 | SG_LOG(SG_INPUT, SG_WARN, "failed to read user settings:" << e.getMessage() |
| 883 | << "(from " << e.getOrigin() << ")"); |
| 884 | } |
| 885 | } else { |
| 886 | tryAutosaveMigration(userDataPath, &autosave); |
| 887 | } |
| 888 | /* Before 2020-03-10, we could save portions of the /ai/models/ tree, which |
| 889 | confuses things when loaded again. So delete any such items if they have |
| 890 | been loaded. */ |
| 891 | SGPropertyNode* ai = autosave.getNode("ai"); |
| 892 | if (ai) { |
| 893 | ai->removeChildren("models"); |
| 894 | } |
| 895 | copyProperties(&autosave, globals->get_props()); |
| 896 | } |
| 897 | |
| 898 | // Save user settings to the autosave file. |
| 899 | // |
| 900 | // When calling this method, make sure the value of 'userDataPath' is |
| 901 | // trustworthy. In particular, make sure it can't be influenced by Nasal code, |
| 902 | // not even indirectly via a Nasal-writable place such as the property tree. |
| 903 | // |
| 904 | // Note: the default value, which causes the autosave file to be written to |
| 905 | // $FG_HOME, is safe---if not, it would be a bug. |
| 906 | void |
| 907 | FGGlobals::saveUserSettings(SGPath userDataPath) |
| 908 | { |
| 909 | if (userDataPath.isNull()) userDataPath = get_fg_home(); |
| 910 | |
| 911 | // only save settings when we have (tried) to load the previous |
| 912 | // settings (otherwise user data was lost) |
| 913 | if (!haveUserSettings) |
| 914 | return; |
| 915 | |
| 916 | if (fgGetBool("/sim/startup/save-on-exit")) { |
| 917 | // don't save settings more than once on shutdown |
| 918 | haveUserSettings = false; |
| 919 | |
| 920 | SGPath autosaveFile = autosaveFilePath(userDataPath); |
| 921 | autosaveFile.create_dir( 0700 ); |
| 922 | SG_LOG(SG_IO, SG_INFO, "Saving user settings to " << autosaveFile); |
| 923 | try { |
| 924 | writeProperties(autosaveFile, globals->get_props(), false, SGPropertyNode::USERARCHIVE); |
| 925 | } catch (const sg_exception &e) { |
| 926 | guiErrorMessage("Error writing autosave:", e); |
| 927 | } |
| 928 | SG_LOG(SG_INPUT, SG_DEBUG, "Finished Saving user settings"); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | long int FGGlobals::get_warp() const |
| 933 | { |
| 934 | return fgGetInt("/sim/time/warp"); |
| 935 | } |
| 936 | |
| 937 | void FGGlobals::set_warp( long int w ) |
| 938 | { |
| 939 | fgSetInt("/sim/time/warp", w); |
| 940 | } |
| 941 | |
| 942 | long int FGGlobals::get_warp_delta() const |
| 943 | { |
| 944 | return fgGetInt("/sim/time/warp-delta"); |
| 945 | } |
| 946 | |
| 947 | void FGGlobals::set_warp_delta( long int d ) |
| 948 | { |
| 949 | fgSetInt("/sim/time/warp-delta", d); |
| 950 | } |
| 951 | |
| 952 | FGScenery* FGGlobals::get_scenery () const |
| 953 | { |
| 954 | return subsystem_mgr->get_subsystem<FGScenery>(); |
| 955 | } |
| 956 | |
| 957 | FGViewMgr *FGGlobals::get_viewmgr() const |
| 958 | { |
| 959 | return subsystem_mgr->get_subsystem<FGViewMgr>(); |
| 960 | } |
| 961 | |
| 962 | flightgear::View* FGGlobals::get_current_view () const |
| 963 | { |
| 964 | FGViewMgr* vm = get_viewmgr(); |
| 965 | return vm ? vm->get_current_view() : 0; |
| 966 | } |
| 967 | |
| 968 | void FGGlobals::set_matlib( SGMaterialLib *m ) |
| 969 | { |
| 970 | matlib = m; |
| 971 | } |
| 972 | |
| 973 | FGControls *FGGlobals::get_controls() const |
| 974 | { |
| 975 | return subsystem_mgr->get_subsystem<FGControls>(); |
| 976 | } |
| 977 | |
| 978 | void FGGlobals::addListenerToCleanup(SGPropertyChangeListener* l) |
| 979 | { |
| 980 | _listeners_to_cleanup.push_back(l); |
| 981 | } |
| 982 | |
| 983 | void FGGlobals::cleanupListeners() |
| 984 | { |
| 985 | SGPropertyChangeListenerVec::iterator i = _listeners_to_cleanup.begin(); |
| 986 | for (; i != _listeners_to_cleanup.end(); ++i) { |
| 987 | delete *i; |
| 988 | } |
| 989 | _listeners_to_cleanup.clear(); |
| 990 | |
| 991 | simgear::AtomicChangeListener::clearPendingChanges(); |
| 992 | } |
| 993 | |
| 994 | simgear::pkg::Root* FGGlobals::packageRoot() |
| 995 | { |
| 996 | return _packageRoot.get(); |
| 997 | } |
| 998 | |
| 999 | void FGGlobals::setPackageRoot(const SGSharedPtr<simgear::pkg::Root>& p) |
| 1000 | { |
| 1001 | _packageRoot = p; |
| 1002 | } |
| 1003 | |
| 1004 | bool FGGlobals::is_headless() |
| 1005 | { |
| 1006 | return flightgear::isHeadlessMode(); |
| 1007 | } |
| 1008 | |
| 1009 | void FGGlobals::set_headless(bool mode) |
| 1010 | { |
| 1011 | flightgear::setHeadlessMode(mode); |
| 1012 | } |
| 1013 | |
| 1014 | // end of globals.cxx |