]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/fg_init.cxx
set /sim/fg-current to current working directory; getcwd() is defined in
[flightgear.git] / src / Main / fg_init.cxx
index cc4556733a65c186d68e967a7cf5f4549623ddab..626f05291cba152a68244a4728efec445c2db771 100644 (file)
 #if defined( unix ) || defined( __CYGWIN__ )
 #  include <unistd.h>           // for gethostname()
 #endif
+#if defined( _MSC_VER) || defined(__MINGW32__)
+#  include <direct.h>           // for getcwd()
+#  define getcwd _getcwd
+#endif
 
 // work around a stdc++ lib bug in some versions of linux, but doesn't
 // seem to hurt to have this here for all versions of Linux.
@@ -67,7 +71,6 @@
 #include <Airports/runways.hxx>
 #include <Airports/simple.hxx>
 #include <AIModel/AIManager.hxx>
-#include <ATC/ATCdisplay.hxx>
 #include <ATC/ATCmgr.hxx>
 #include <ATC/AIMgr.hxx>
 #include <Autopilot/route_mgr.hxx>
@@ -506,8 +509,11 @@ do_options (int argc, char ** argv)
 }
 
 
-#define MAXDEPTH 1
-static string fgFindAircraftPath( const SGPath &path, const string &aircraft, int depth = 0 ) {
+static string fgFindAircraftPath( const SGPath &path, const string &aircraft,
+                                  SGPropertyNode *cache, int depth = 0 )
+{
+    const int MAXDEPTH = 1;
+
     ulDirEnt* dire;
     ulDir *dirp = ulOpenDir(path.str().c_str());
     if (dirp == NULL) {
@@ -517,21 +523,46 @@ static string fgFindAircraftPath( const SGPath &path, const string &aircraft, in
 
     string result;
     while ((dire = ulReadDir(dirp)) != NULL) {
-        if (dire->d_isdir && depth < MAXDEPTH) {
-            if ( strcmp("CVS", dire->d_name) && strcmp(".", dire->d_name)
-                 && strcmp("..", dire->d_name) && strcmp("AI", dire->d_name))
-            {
-                SGPath next = path;
-                next.append(dire->d_name);
-
-                result = fgFindAircraftPath( next, aircraft, depth + 1 );
-                if ( ! result.empty() ) {
+        if (dire->d_isdir) {
+            if (depth > MAXDEPTH)
+                continue;
+
+            if (!strcmp("CVS", dire->d_name) || !strcmp(".", dire->d_name)
+                    || !strcmp("..", dire->d_name) || !strcmp("AI", dire->d_name))
+                continue;
+
+            SGPath next = path;
+            next.append(dire->d_name);
+
+            result = fgFindAircraftPath( next, aircraft, cache, depth + 1 );
+            if ( ! result.empty() )
+                break;
+
+        } else {
+            int len = strlen(dire->d_name);
+            if (len < 9 || strcmp(dire->d_name + len - 8, "-set.xml"))
+                continue;
+
+            // create cache node
+            int i = 0;
+            while (1)
+                if (!cache->getChild("aircraft", i++, false))
                     break;
-                }
+
+            SGPropertyNode *n, *entry = cache->getChild("aircraft", --i, true);
+
+            n = entry->getNode("file", true);
+            n->setStringValue(dire->d_name);
+            n->setAttribute(SGPropertyNode::USERARCHIVE, true);
+
+            n = entry->getNode("path", true);
+            n->setStringValue(path.str().c_str());
+            n->setAttribute(SGPropertyNode::USERARCHIVE, true);
+
+            if ( !strcmp(dire->d_name, aircraft.c_str()) ) {
+                result = path.str();
+                break;
             }
-        } else if ( !strcmp(dire->d_name, aircraft.c_str()) ) {
-            result = path.str();
-            break;
         }
     }
 
@@ -556,6 +587,30 @@ bool fgInitConfig ( int argc, char **argv ) {
         return false;
     }
 
+    SGPropertyNode autosave;
+#ifdef _MSC_VER
+    char *envp = ::getenv( "APPDATA" );
+    if (envp != NULL ) {
+        SGPath config( envp );
+        config.append( "flightgear.org" );
+#else
+    if ( homedir != NULL ) {
+        SGPath config( homedir );
+        config.append( ".fgfs" );
+#endif
+        fgSetString("/sim/fg-home", config.c_str());
+        config.append( "autosave.xml" );
+        SG_LOG(SG_INPUT, SG_INFO, "Reading user settings from " << config.str());
+        try {
+            readProperties(config.str(), &autosave, SGPropertyNode::USERARCHIVE);
+        } catch (...) {
+            SG_LOG(SG_INPUT, SG_DEBUG, "First time reading user settings");
+        }
+        SG_LOG(SG_INPUT, SG_DEBUG, "Finished Reading user settings");
+    }
+    SGPropertyNode *cache_root = autosave.getNode("sim/startup/path-cache", true);
+
+
     // Scan user config files and command line for a specified aircraft.
     fgInitFGAircraft(argc, argv);
 
@@ -565,8 +620,34 @@ bool fgInitConfig ( int argc, char **argv ) {
         aircraft_search.append( "Aircraft" );
 
         string aircraft_set = aircraft + "-set.xml";
+        string result;
+
+        // check if the *-set.xml file is already in the cache
+        if (globals->get_fg_root() == cache_root->getStringValue("fg-root", "")) {
+            vector<SGPropertyNode_ptr> cache = cache_root->getChildren("aircraft");
+            for (unsigned int i = 0; i < cache.size(); i++) {
+                const char *name = cache[i]->getStringValue("file", "");
+                if (aircraft_set == name) {
+                    const char *path = cache[i]->getStringValue("path", "");
+                    SGPath xml(path);
+                    xml.append(name);
+                    if (xml.exists())
+                        result = path;
+                    break;
+                }
+            }
+        }
+
+        if (result.empty()) {
+            // prepare cache for rescan
+            SGPropertyNode *n = cache_root->getNode("fg-root", true);
+            n->setStringValue(globals->get_fg_root().c_str());
+            n->setAttribute(SGPropertyNode::USERARCHIVE, true);
+            cache_root->removeChildren("aircraft");
+
+            result = fgFindAircraftPath( aircraft_search, aircraft_set, cache_root );
+        }
 
-        string result = fgFindAircraftPath( aircraft_search, aircraft_set );
         if ( !result.empty() ) {
             fgSetString( "/sim/aircraft-dir", result.c_str() );
             SGPath full_name( result );
@@ -592,26 +673,7 @@ bool fgInitConfig ( int argc, char **argv ) {
         SG_LOG( SG_INPUT, SG_ALERT, "No default aircraft specified" );
     }
 
-#ifdef _MSC_VER
-    char *envp = ::getenv( "APPDATA" );
-    if (envp != NULL ) {
-        SGPath config( envp );
-        config.append( "flightgear.org" );
-#else
-    if ( homedir != NULL ) {
-        SGPath config( homedir );
-        config.append( ".fgfs" );
-#endif
-        fgSetString("/sim/fg-home", config.c_str());
-        config.append( "autosave.xml" );
-        SG_LOG(SG_INPUT, SG_INFO, "Reading user settings from " << config.str());
-        try {
-            readProperties(config.str(), globals->get_props(), SGPropertyNode::USERARCHIVE);
-        } catch (...) {
-            SG_LOG(SG_INPUT, SG_DEBUG, "First time reading user settings");
-        }
-        SG_LOG(SG_INPUT, SG_DEBUG, "Finished Reading user settings");
-    }
+    copyProperties(&autosave, globals->get_props());
 
     // parse options after loading aircraft to ensure any user
     // overrides of defaults are honored.
@@ -689,9 +751,9 @@ struct FGTowerLocationListener : SGPropertyChangeListener {
     }
 };
 
-static void fgInitTowerLocationListener() {
+void fgInitTowerLocationListener() {
     fgGetNode("/sim/tower/airport-id",  true)
-       ->addChangeListener( new FGTowerLocationListener() );
+        ->addChangeListener( new FGTowerLocationListener(), true );
 }
 
 // Set current_options lon/lat given an airport id and heading (degrees)
@@ -1143,7 +1205,6 @@ bool fgInitPosition() {
     string fix = fgGetString("/sim/presets/fix");
 
     fgSetDouble( "/orientation/heading-deg", hdg );
-    fgInitTowerLocationListener();
 
     if ( !set_pos && !apt.empty() && !rwy_no.empty() ) {
         // An airport + runway is requested
@@ -1252,6 +1313,8 @@ bool fgInitGeneral() {
     }
 #endif
 
+    char buf[256], *cwd = getcwd(buf, 256);
+    fgSetString("/sim/fg-current", cwd ? cwd : "");
     return true;
 }
 
@@ -1270,90 +1333,83 @@ void fgInitFDM() {
     double dt = 1.0 / fgGetInt("/sim/model-hz");
     string model = fgGetString("/sim/flight-model");
 
-    try {
-        if ( model == "larcsim" ) {
-            cur_fdm_state = new FGLaRCsim( dt );
-        } else if ( model == "jsb" ) {
-            cur_fdm_state = new FGJSBsim( dt );
+    if ( model == "larcsim" ) {
+        cur_fdm_state = new FGLaRCsim( dt );
+    } else if ( model == "jsb" ) {
+        cur_fdm_state = new FGJSBsim( dt );
 #if ENABLE_SP_FDM
-        } else if ( model == "ada" ) {
-            cur_fdm_state = new FGADA( dt );
-        } else if ( model == "acms" ) {
-            cur_fdm_state = new FGACMS( dt );
+    } else if ( model == "ada" ) {
+        cur_fdm_state = new FGADA( dt );
+    } else if ( model == "acms" ) {
+        cur_fdm_state = new FGACMS( dt );
 #endif
-        } else if ( model == "balloon" ) {
-            cur_fdm_state = new FGBalloonSim( dt );
-        } else if ( model == "magic" ) {
-            cur_fdm_state = new FGMagicCarpet( dt );
-        } else if ( model == "ufo" ) {
-            cur_fdm_state = new FGUFO( dt );
-        } else if ( model == "external" ) {
-            // external is a synonym for "--fdm=null" and is
-            // maintained here for backwards compatibility
-            cur_fdm_state = new FGNullFDM( dt );
-        } else if ( model.find("network") == 0 ) {
-            string host = "localhost";
-            int port1 = 5501;
-            int port2 = 5502;
-            int port3 = 5503;
-            string net_options = model.substr(8);
-            string::size_type begin, end;
-            begin = 0;
-            // host
-            end = net_options.find( ",", begin );
-            if ( end != string::npos ) {
-                host = net_options.substr(begin, end - begin);
-                begin = end + 1;
-            }
-            // port1
-            end = net_options.find( ",", begin );
-            if ( end != string::npos ) {
-                port1 = atoi( net_options.substr(begin, end - begin).c_str() );
-                begin = end + 1;
-            }
-            // port2
-            end = net_options.find( ",", begin );
-            if ( end != string::npos ) {
-                port2 = atoi( net_options.substr(begin, end - begin).c_str() );
-                begin = end + 1;
-            }
-            // port3
-            end = net_options.find( ",", begin );
-            if ( end != string::npos ) {
-                port3 = atoi( net_options.substr(begin, end - begin).c_str() );
-                begin = end + 1;
-            }
-            cur_fdm_state = new FGExternalNet( dt, host, port1, port2, port3 );
-        } else if ( model.find("pipe") == 0 ) {
-            // /* old */ string pipe_path = model.substr(5);
-            // /* old */ cur_fdm_state = new FGExternalPipe( dt, pipe_path );
-            string pipe_path = "";
-            string pipe_protocol = "";
-            string pipe_options = model.substr(5);
-            string::size_type begin, end;
-            begin = 0;
-            // pipe file path
-            end = pipe_options.find( ",", begin );
-            if ( end != string::npos ) {
-                pipe_path = pipe_options.substr(begin, end - begin);
-                begin = end + 1;
-            }
-            // protocol (last option)
-            pipe_protocol = pipe_options.substr(begin);
-            cur_fdm_state = new FGExternalPipe( dt, pipe_path, pipe_protocol );
-        } else if ( model == "null" ) {
-            cur_fdm_state = new FGNullFDM( dt );
-        } else if ( model == "yasim" ) {
-            cur_fdm_state = new YASim( dt );
-        } else {
-            SG_LOG(SG_GENERAL, SG_ALERT,
-                   "Unrecognized flight model '" << model
-                   << "', cannot init flight dynamics model.");
-            exit(-1);
+    } else if ( model == "balloon" ) {
+        cur_fdm_state = new FGBalloonSim( dt );
+    } else if ( model == "magic" ) {
+        cur_fdm_state = new FGMagicCarpet( dt );
+    } else if ( model == "ufo" ) {
+        cur_fdm_state = new FGUFO( dt );
+    } else if ( model == "external" ) {
+        // external is a synonym for "--fdm=null" and is
+        // maintained here for backwards compatibility
+        cur_fdm_state = new FGNullFDM( dt );
+    } else if ( model.find("network") == 0 ) {
+        string host = "localhost";
+        int port1 = 5501;
+        int port2 = 5502;
+        int port3 = 5503;
+        string net_options = model.substr(8);
+        string::size_type begin, end;
+        begin = 0;
+        // host
+        end = net_options.find( ",", begin );
+        if ( end != string::npos ) {
+            host = net_options.substr(begin, end - begin);
+            begin = end + 1;
         }
-    } catch ( ... ) {
-        SG_LOG(SG_GENERAL, SG_ALERT, "FlightGear aborting\n\n");
-        exit(-1);
+        // port1
+        end = net_options.find( ",", begin );
+        if ( end != string::npos ) {
+            port1 = atoi( net_options.substr(begin, end - begin).c_str() );
+            begin = end + 1;
+        }
+        // port2
+        end = net_options.find( ",", begin );
+        if ( end != string::npos ) {
+            port2 = atoi( net_options.substr(begin, end - begin).c_str() );
+            begin = end + 1;
+        }
+        // port3
+        end = net_options.find( ",", begin );
+        if ( end != string::npos ) {
+            port3 = atoi( net_options.substr(begin, end - begin).c_str() );
+            begin = end + 1;
+        }
+        cur_fdm_state = new FGExternalNet( dt, host, port1, port2, port3 );
+    } else if ( model.find("pipe") == 0 ) {
+        // /* old */ string pipe_path = model.substr(5);
+        // /* old */ cur_fdm_state = new FGExternalPipe( dt, pipe_path );
+        string pipe_path = "";
+        string pipe_protocol = "";
+        string pipe_options = model.substr(5);
+        string::size_type begin, end;
+        begin = 0;
+        // pipe file path
+        end = pipe_options.find( ",", begin );
+        if ( end != string::npos ) {
+            pipe_path = pipe_options.substr(begin, end - begin);
+            begin = end + 1;
+        }
+        // protocol (last option)
+        pipe_protocol = pipe_options.substr(begin);
+        cur_fdm_state = new FGExternalPipe( dt, pipe_path, pipe_protocol );
+    } else if ( model == "null" ) {
+        cur_fdm_state = new FGNullFDM( dt );
+    } else if ( model == "yasim" ) {
+        cur_fdm_state = new YASim( dt );
+    } else {
+        throw sg_throwable(string("Unrecognized flight model '") + model
+               + "', cannot init flight dynamics model.");
     }
 }
 
@@ -1521,9 +1577,10 @@ bool fgInitSubsystems() {
     globals->get_event_mgr()->setRealtimeProperty(fgGetNode("/sim/time/delta-realtime-sec", true));
 
     ////////////////////////////////////////////////////////////////////
-    // Initialize the property interpolator subsystem
+    // Initialize the property interpolator subsystem. Put into the INIT
+    // group because the "nasal" subsystem may need it at GENERAL take-down.
     ////////////////////////////////////////////////////////////////////
-    globals->add_subsystem("interpolator", new SGInterpolator);
+    globals->add_subsystem("interpolator", new SGInterpolator, SGSubsystemMgr::INIT);
 
 
     ////////////////////////////////////////////////////////////////////
@@ -1659,14 +1716,6 @@ bool fgInitSubsystems() {
     globals->add_subsystem("voice", new FGVoiceMgr);
 #endif
 
-    ////////////////////////////////////////////////////////////////////
-    // Initialise ATC display system
-    ////////////////////////////////////////////////////////////////////
-
-    SG_LOG(SG_GENERAL, SG_INFO, "  ATC Display");
-    globals->set_ATC_display(new FGATCDisplay);
-    globals->get_ATC_display()->init(); 
-
     ////////////////////////////////////////////////////////////////////
     // Initialise the ATC Manager 
     ////////////////////////////////////////////////////////////////////
@@ -1691,16 +1740,10 @@ bool fgInitSubsystems() {
     globals->add_subsystem("submodel_mgr", new FGSubmodelMgr);
 
 
-     // It's probably a good idea to initialize the top level traffic manager
-     // After the AI and ATC systems have been initialized properly.
-     // AI Traffic manager
-     globals->add_subsystem("Traffic Manager", new FGTrafficManager);
-     //FGTrafficManager *dispatcher = 
-     //  (FGTrafficManager*) globals->get_subsystem("Traffic Manager");
-     //SGPath path = globals->get_fg_root();
-     //path.append("/Traffic/fgtraffic.xml");
-     //readXML(path.str(),
-     //        *dispatcher);
+    // It's probably a good idea to initialize the top level traffic manager
+    // After the AI and ATC systems have been initialized properly.
+    // AI Traffic manager
+    globals->add_subsystem("Traffic Manager", new FGTrafficManager);
 
 
     ////////////////////////////////////////////////////////////////////