| 1 | // options.cxx -- class to handle command line options |
| 2 | // |
| 3 | // Written by Curtis Olson, started April 1998. |
| 4 | // |
| 5 | // Copyright (C) 1998 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 |
| 19 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | // |
| 21 | // $Id$ |
| 22 | |
| 23 | |
| 24 | #include <config.h> |
| 25 | |
| 26 | #include <simgear/compiler.h> |
| 27 | #include <simgear/structure/exception.hxx> |
| 28 | #include <simgear/debug/logstream.hxx> |
| 29 | #include <simgear/timing/sg_time.hxx> |
| 30 | #include <simgear/misc/sg_dir.hxx> |
| 31 | |
| 32 | #include <cJSON.h> |
| 33 | |
| 34 | #include <cmath> // rint() |
| 35 | #include <cstdio> |
| 36 | #include <cstdlib> // atof(), atoi() |
| 37 | #include <cstring> // strcmp() |
| 38 | #include <algorithm> |
| 39 | #include <map> |
| 40 | |
| 41 | #include <iostream> |
| 42 | #include <string> |
| 43 | #include <sstream> |
| 44 | |
| 45 | #include <simgear/io/HTTPClient.hxx> |
| 46 | #include <simgear/io/HTTPFileRequest.hxx> |
| 47 | #include <simgear/math/sg_random.hxx> |
| 48 | #include <simgear/props/props_io.hxx> |
| 49 | #include <simgear/io/iostreams/sgstream.hxx> |
| 50 | #include <simgear/misc/sg_path.hxx> |
| 51 | #include <simgear/misc/sg_dir.hxx> |
| 52 | #include <simgear/scene/material/mat.hxx> |
| 53 | #include <simgear/sound/soundmgr.hxx> |
| 54 | #include <simgear/misc/strutils.hxx> |
| 55 | #include <simgear/timing/timestamp.hxx> |
| 56 | |
| 57 | #include <Autopilot/route_mgr.hxx> |
| 58 | #include <Aircraft/replay.hxx> |
| 59 | #include <Aircraft/initialstate.hxx> |
| 60 | |
| 61 | #include <GUI/gui.h> |
| 62 | #include <GUI/MessageBox.hxx> |
| 63 | |
| 64 | #if defined(HAVE_QT) |
| 65 | #include <GUI/QtLauncher.hxx> |
| 66 | #endif |
| 67 | |
| 68 | #include <AIModel/AIManager.hxx> |
| 69 | #include <Add-ons/AddonManager.hxx> |
| 70 | #include <Main/locale.hxx> |
| 71 | #include <Main/sentryIntegration.hxx> |
| 72 | #include <Navaids/NavDataCache.hxx> |
| 73 | #include "globals.hxx" |
| 74 | #include "fg_init.hxx" |
| 75 | #include "fg_os.hxx" |
| 76 | #include "fg_props.hxx" |
| 77 | #include "options.hxx" |
| 78 | #include "main.hxx" |
| 79 | #include "locale.hxx" |
| 80 | #include <Viewer/view.hxx> |
| 81 | #include <Viewer/viewmgr.hxx> |
| 82 | #include <Environment/presets.hxx> |
| 83 | #include <Network/http/httpd.hxx> |
| 84 | #include <Network/HTTPClient.hxx> |
| 85 | #include "AircraftDirVisitorBase.hxx" |
| 86 | |
| 87 | #include <osg/Version> |
| 88 | #include <flightgearBuildId.h> |
| 89 | #include <simgear/version.h> |
| 90 | |
| 91 | using std::string; |
| 92 | using std::sort; |
| 93 | using std::cout; |
| 94 | using std::cerr; |
| 95 | using std::endl; |
| 96 | using std::vector; |
| 97 | using std::cin; |
| 98 | |
| 99 | using namespace flightgear; |
| 100 | using namespace std::string_literals; |
| 101 | |
| 102 | #define NEW_DEFAULT_MODEL_HZ 120 |
| 103 | |
| 104 | static flightgear::Options* shared_instance = nullptr; |
| 105 | |
| 106 | static double |
| 107 | atof( const string& str ) |
| 108 | { |
| 109 | return ::atof( str.c_str() ); |
| 110 | } |
| 111 | |
| 112 | static int |
| 113 | atoi( const string& str ) |
| 114 | { |
| 115 | return ::atoi( str.c_str() ); |
| 116 | } |
| 117 | |
| 118 | static int fgSetupProxy( const char *arg ); |
| 119 | |
| 120 | /** |
| 121 | * Set a few fail-safe default property values. |
| 122 | * |
| 123 | * These should all be set in $FG_ROOT/defaults.xml, but just |
| 124 | * in case, we provide some initial sane values here. This method |
| 125 | * should be invoked *before* reading any init files. |
| 126 | */ |
| 127 | void fgSetDefaults () |
| 128 | { |
| 129 | |
| 130 | // Position (deliberately out of range) |
| 131 | fgSetDouble("/position/longitude-deg", 9999.0); |
| 132 | fgSetDouble("/position/latitude-deg", 9999.0); |
| 133 | fgSetDouble("/position/altitude-ft", -9999.0); |
| 134 | |
| 135 | // Orientation |
| 136 | fgSetDouble("/orientation/heading-deg", 9999.0); |
| 137 | fgSetDouble("/orientation/roll-deg", 0.0); |
| 138 | fgSetDouble("/orientation/pitch-deg", 0.424); |
| 139 | |
| 140 | // Velocities |
| 141 | fgSetDouble("/velocities/uBody-fps", 0.0); |
| 142 | fgSetDouble("/velocities/vBody-fps", 0.0); |
| 143 | fgSetDouble("/velocities/wBody-fps", 0.0); |
| 144 | fgSetDouble("/velocities/speed-north-fps", 0.0); |
| 145 | fgSetDouble("/velocities/speed-east-fps", 0.0); |
| 146 | fgSetDouble("/velocities/speed-down-fps", 0.0); |
| 147 | fgSetDouble("/velocities/airspeed-kt", 0.0); |
| 148 | fgSetDouble("/velocities/mach", 0.0); |
| 149 | |
| 150 | // Presets |
| 151 | fgSetDouble("/sim/presets/longitude-deg", 9999.0); |
| 152 | fgSetDouble("/sim/presets/latitude-deg", 9999.0); |
| 153 | fgSetDouble("/sim/presets/altitude-ft", -9999.0); |
| 154 | |
| 155 | fgSetDouble("/sim/presets/heading-deg", 9999.0); |
| 156 | fgSetDouble("/sim/presets/roll-deg", 0.0); |
| 157 | fgSetDouble("/sim/presets/pitch-deg", 0.424); |
| 158 | |
| 159 | fgSetString("/sim/presets/speed-set", "knots"); |
| 160 | fgSetDouble("/sim/presets/airspeed-kt", 0.0); |
| 161 | fgSetDouble("/sim/presets/mach", 0.0); |
| 162 | fgSetDouble("/sim/presets/uBody-fps", 0.0); |
| 163 | fgSetDouble("/sim/presets/vBody-fps", 0.0); |
| 164 | fgSetDouble("/sim/presets/wBody-fps", 0.0); |
| 165 | fgSetDouble("/sim/presets/speed-north-fps", 0.0); |
| 166 | fgSetDouble("/sim/presets/speed-east-fps", 0.0); |
| 167 | fgSetDouble("/sim/presets/speed-down-fps", 0.0); |
| 168 | fgSetDouble("/sim/presets/offset-distance-nm", 0.0); |
| 169 | |
| 170 | fgSetBool("/sim/presets/runway-requested", false); |
| 171 | |
| 172 | fgSetBool("/sim/presets/onground", true); |
| 173 | fgSetBool("/sim/presets/trim", false); |
| 174 | |
| 175 | // Miscellaneous |
| 176 | fgSetBool("/sim/startup/splash-screen", true); |
| 177 | // we want mouse-pointer to have an undefined value if nothing is |
| 178 | // specified so we can do the right thing for voodoo-1/2 cards. |
| 179 | // fgSetString("/sim/startup/mouse-pointer", "false"); |
| 180 | fgSetBool("/controls/flight/auto-coordination", false); |
| 181 | fgSetString("/sim/logging/priority", "alert"); |
| 182 | |
| 183 | // Features |
| 184 | fgSetBool("/sim/hud/color/antialiased", false); |
| 185 | fgSetBool("/sim/hud/enable3d[1]", true); |
| 186 | fgSetBool("/sim/hud/visibility[1]", false); |
| 187 | fgSetBool("/sim/panel/visibility", true); |
| 188 | fgSetBool("/sim/sound/enabled", true); |
| 189 | fgSetBool("/sim/sound/working", true); |
| 190 | fgSetBool("/sim/fgcom/enabled", false); |
| 191 | |
| 192 | // Flight Model options |
| 193 | fgSetString("/sim/flight-model", "jsb"); |
| 194 | fgSetString("/sim/aero", "c172"); |
| 195 | fgSetInt("/sim/model-hz", NEW_DEFAULT_MODEL_HZ); |
| 196 | fgSetDouble("/sim/speed-up", 1.0); |
| 197 | |
| 198 | // Scenery |
| 199 | fgSetString("/sim/scenery/engine", "tilecache"); |
| 200 | |
| 201 | // ( scenery = pagedLOD ) |
| 202 | fgSetString("/sim/scenery/lod-levels", "1 3 5 7 9"); |
| 203 | fgSetString("/sim/scenery/lod-res", "1"); |
| 204 | fgSetString("/sim/scenery/lod-texturing", "bluemarble"); |
| 205 | |
| 206 | // Rendering options |
| 207 | fgSetString("/sim/rendering/fog", "nicest"); |
| 208 | fgSetBool("/environment/clouds/status", true); |
| 209 | fgSetBool("/sim/startup/fullscreen", false); |
| 210 | fgSetBool("/sim/rendering/shading", true); |
| 211 | fgTie( "/sim/rendering/filtering", SGGetTextureFilter, SGSetTextureFilter, false); |
| 212 | fgSetInt("/sim/rendering/filtering", 1); |
| 213 | fgSetBool("/sim/rendering/wireframe", false); |
| 214 | fgSetBool("/sim/rendering/horizon-effect", false); |
| 215 | fgSetBool("/sim/rendering/distance-attenuation", false); |
| 216 | fgSetBool("/sim/rendering/specular-highlight", true); |
| 217 | fgSetString("/sim/rendering/materials-file", "materials.xml"); |
| 218 | fgSetInt("/sim/startup/xsize", 1024); |
| 219 | fgSetInt("/sim/startup/ysize", 768); |
| 220 | fgSetInt("/sim/rendering/bits-per-pixel", 32); |
| 221 | fgSetString("/sim/view-mode", "pilot"); |
| 222 | fgSetDouble("/sim/current-view/heading-offset-deg", 0); |
| 223 | |
| 224 | // HUD options |
| 225 | fgSetString("/sim/startup/units", "feet"); |
| 226 | fgSetString("/sim/hud/frame-stat-type", "tris"); |
| 227 | |
| 228 | // Time options |
| 229 | fgSetInt("/sim/startup/time-offset", 0); |
| 230 | fgSetString("/sim/startup/time-offset-type", "system-offset"); |
| 231 | fgSetLong("/sim/time/cur-time-override", 0); |
| 232 | |
| 233 | // Freeze options |
| 234 | fgSetBool("/sim/freeze/master", false); |
| 235 | fgSetBool("/sim/freeze/position", false); |
| 236 | fgSetBool("/sim/freeze/clock", false); |
| 237 | fgSetBool("/sim/freeze/fuel", false); |
| 238 | |
| 239 | fgSetString("/sim/multiplay/callsign", "callsign"); |
| 240 | fgSetString("/sim/multiplay/rxhost", ""); |
| 241 | fgSetString("/sim/multiplay/txhost", ""); |
| 242 | fgSetInt("/sim/multiplay/rxport", 0); |
| 243 | fgSetInt("/sim/multiplay/txport", 0); |
| 244 | |
| 245 | SGPropertyNode* v = globals->get_props()->getNode("/sim/version", true); |
| 246 | v->setValueReadOnly("flightgear", FLIGHTGEAR_VERSION); |
| 247 | v->setValueReadOnly("simgear", SG_STRINGIZE(SIMGEAR_VERSION)); |
| 248 | v->setValueReadOnly("openscenegraph", osgGetVersion()); |
| 249 | #if OSG_VERSION_LESS_THAN(3,5,2) |
| 250 | v->setValueReadOnly("openscenegraph-thread-safe-reference-counting", |
| 251 | osg::Referenced::getThreadSafeReferenceCounting()); |
| 252 | #endif |
| 253 | v->setValueReadOnly("revision", REVISION); |
| 254 | v->setValueReadOnly("build-number", JENKINS_BUILD_NUMBER); |
| 255 | v->setValueReadOnly("build-id", JENKINS_BUILD_ID); |
| 256 | v->setValueReadOnly("hla-support", bool(FG_HAVE_HLA)); |
| 257 | v->setValueReadOnly("build-type", FG_BUILD_TYPE); |
| 258 | |
| 259 | char* envp = ::getenv( "http_proxy" ); |
| 260 | if( envp != nullptr ) |
| 261 | fgSetupProxy( envp ); |
| 262 | } |
| 263 | |
| 264 | /////////////////////////////////////////////////////////////////////////////// |
| 265 | // helper object to implement the --show-aircraft command. |
| 266 | // resides here so we can share the fgFindAircraftInDir template above, |
| 267 | // and hence ensure this command lists exectly the same aircraft as the normal |
| 268 | // loading path. |
| 269 | class ShowAircraft : public AircraftDirVistorBase |
| 270 | { |
| 271 | public: |
| 272 | ShowAircraft() |
| 273 | { |
| 274 | _minStatus = getNumMaturity(fgGetString("/sim/aircraft-min-status", "all")); |
| 275 | } |
| 276 | |
| 277 | |
| 278 | void show(const vector<SGPath> & path_list) |
| 279 | { |
| 280 | for (vector<SGPath>::const_iterator p = path_list.begin(); |
| 281 | p != path_list.end(); ++p) |
| 282 | visitDir(*p, 0); |
| 283 | |
| 284 | simgear::requestConsole(false); // ensure console is shown on Windows |
| 285 | |
| 286 | std::sort(_aircraft.begin(), _aircraft.end(), |
| 287 | [](const std::string& lhs, const std::string& rhs) { |
| 288 | return strcasecmp(lhs.c_str(), rhs.c_str()) < 0 ? 1 : 0; |
| 289 | }); |
| 290 | |
| 291 | cout << "Available aircraft:" << endl; |
| 292 | for ( unsigned int i = 0; i < _aircraft.size(); i++ ) { |
| 293 | cout << _aircraft[i] << endl; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | private: |
| 298 | virtual VisitResult visit(const SGPath& path) |
| 299 | { |
| 300 | SGPropertyNode root; |
| 301 | try { |
| 302 | readProperties(path, &root); |
| 303 | } catch (sg_exception& ) { |
| 304 | return VISIT_CONTINUE; |
| 305 | } |
| 306 | |
| 307 | int maturity = 0; |
| 308 | string descStr(" "); |
| 309 | descStr += path.file(); |
| 310 | // trim common suffix from file names |
| 311 | int nPos = descStr.rfind("-set.xml"); |
| 312 | if (nPos == (int)(descStr.size() - 8)) { |
| 313 | descStr.resize(nPos); |
| 314 | } |
| 315 | |
| 316 | SGPropertyNode *node = root.getNode("sim"); |
| 317 | if (node) { |
| 318 | SGPropertyNode* desc = node->getNode("description"); |
| 319 | // if a status tag is found, read it in |
| 320 | if (node->hasValue("status")) { |
| 321 | maturity = getNumMaturity(node->getStringValue("status")); |
| 322 | } |
| 323 | |
| 324 | if (desc) { |
| 325 | if (descStr.size() <= 27+3) { |
| 326 | descStr.append(29+3-descStr.size(), ' '); |
| 327 | } else { |
| 328 | descStr += '\n'; |
| 329 | descStr.append( 32, ' '); |
| 330 | } |
| 331 | descStr += desc->getStringValue(); |
| 332 | } |
| 333 | } // of have 'sim' node |
| 334 | |
| 335 | if (maturity >= _minStatus) { |
| 336 | _aircraft.push_back(descStr); |
| 337 | } |
| 338 | |
| 339 | return VISIT_CONTINUE; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | int getNumMaturity(const std::string& str) |
| 344 | { |
| 345 | // Changes should also be reflected in $FG_ROOT/options.xml |
| 346 | const char* levels[] = {"alpha","beta","early-production","production"}; |
| 347 | |
| 348 | if (str == "all") { |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | for (size_t i=0; i<(sizeof(levels)/sizeof(levels[0]));i++) |
| 353 | if (str == levels[i]) |
| 354 | return i; |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | int _minStatus; |
| 360 | string_list _aircraft; |
| 361 | }; |
| 362 | |
| 363 | /* |
| 364 | * Search in the current directory, and in on directory deeper |
| 365 | * for <aircraft>-set.xml configuration files and show the aircaft name |
| 366 | * and the contents of the<description> tag in a sorted manner. |
| 367 | * |
| 368 | * @parampath the directory to search for configuration files |
| 369 | */ |
| 370 | void fgShowAircraft(const vector<SGPath> &path_list) |
| 371 | { |
| 372 | ShowAircraft s; |
| 373 | s.show(path_list); |
| 374 | |
| 375 | #ifdef _MSC_VER |
| 376 | cout << "Hit a key to continue..." << endl; |
| 377 | std::cin.get(); |
| 378 | #endif |
| 379 | } |
| 380 | |
| 381 | |
| 382 | static bool |
| 383 | parse_wind (const string &wind, double * min_hdg, double * max_hdg, |
| 384 | double * speed, double * gust) |
| 385 | { |
| 386 | string::size_type pos = wind.find('@'); |
| 387 | if (pos == string::npos) |
| 388 | return false; |
| 389 | string dir = wind.substr(0, pos); |
| 390 | string spd = wind.substr(pos+1); |
| 391 | pos = dir.find(':'); |
| 392 | if (pos == string::npos) { |
| 393 | *min_hdg = *max_hdg = atof(dir.c_str()); |
| 394 | } else { |
| 395 | *min_hdg = atof(dir.substr(0,pos).c_str()); |
| 396 | *max_hdg = atof(dir.substr(pos+1).c_str()); |
| 397 | } |
| 398 | pos = spd.find(':'); |
| 399 | if (pos == string::npos) { |
| 400 | *speed = *gust = atof(spd.c_str()); |
| 401 | } else { |
| 402 | *speed = atof(spd.substr(0,pos).c_str()); |
| 403 | *gust = atof(spd.substr(pos+1).c_str()); |
| 404 | } |
| 405 | return true; |
| 406 | } |
| 407 | |
| 408 | static bool |
| 409 | parseIntValue(char** ppParserPos, int* pValue,int min, int max, const char* field, const char* argument) |
| 410 | { |
| 411 | if ( !strlen(*ppParserPos) ) |
| 412 | return true; |
| 413 | |
| 414 | char num[256]; |
| 415 | int i = 0; |
| 416 | |
| 417 | while ( isdigit((*ppParserPos)[0]) && (i<255) ) |
| 418 | { |
| 419 | num[i] = (*ppParserPos)[0]; |
| 420 | (*ppParserPos)++; |
| 421 | i++; |
| 422 | } |
| 423 | num[i] = '\0'; |
| 424 | |
| 425 | switch ((*ppParserPos)[0]) |
| 426 | { |
| 427 | case 0: |
| 428 | break; |
| 429 | case ':': |
| 430 | (*ppParserPos)++; |
| 431 | break; |
| 432 | default: |
| 433 | SG_LOG(SG_GENERAL, SG_ALERT, "Illegal character in time string for " << field << ": '" << |
| 434 | (*ppParserPos)[0] << "'."); |
| 435 | // invalid field - skip rest of string to avoid further errors |
| 436 | while ((*ppParserPos)[0]) |
| 437 | (*ppParserPos)++; |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | if (i<=0) |
| 442 | return true; |
| 443 | |
| 444 | int value = atoi(num); |
| 445 | if ((value < min)||(value > max)) |
| 446 | { |
| 447 | SG_LOG(SG_GENERAL, SG_ALERT, "Invalid " << field << " in '" << argument << |
| 448 | "'. Valid range is " << min << "-" << max << "."); |
| 449 | return false; |
| 450 | } |
| 451 | else |
| 452 | { |
| 453 | *pValue = value; |
| 454 | return true; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | // parse a time string ([+/-]%f[:%f[:%f]]) into hours |
| 459 | static double |
| 460 | parse_time(const string& time_in) { |
| 461 | char *time_str, num[256]; |
| 462 | double hours, minutes, seconds; |
| 463 | double result = 0.0; |
| 464 | int sign = 1; |
| 465 | int i; |
| 466 | |
| 467 | time_str = (char *)time_in.c_str(); |
| 468 | |
| 469 | // printf("parse_time(): %s\n", time_str); |
| 470 | |
| 471 | // check for sign |
| 472 | if ( strlen(time_str) ) { |
| 473 | if ( time_str[0] == '+' ) { |
| 474 | sign = 1; |
| 475 | time_str++; |
| 476 | } else if ( time_str[0] == '-' ) { |
| 477 | sign = -1; |
| 478 | time_str++; |
| 479 | } |
| 480 | } |
| 481 | // printf("sign = %d\n", sign); |
| 482 | |
| 483 | // get hours |
| 484 | if ( strlen(time_str) ) { |
| 485 | i = 0; |
| 486 | while ( (time_str[0] != ':') && (time_str[0] != '\0') ) { |
| 487 | num[i] = time_str[0]; |
| 488 | time_str++; |
| 489 | i++; |
| 490 | } |
| 491 | if ( time_str[0] == ':' ) { |
| 492 | time_str++; |
| 493 | } |
| 494 | num[i] = '\0'; |
| 495 | hours = atof(num); |
| 496 | // printf("hours = %.2lf\n", hours); |
| 497 | |
| 498 | result += hours; |
| 499 | } |
| 500 | |
| 501 | // get minutes |
| 502 | if ( strlen(time_str) ) { |
| 503 | i = 0; |
| 504 | while ( (time_str[0] != ':') && (time_str[0] != '\0') ) { |
| 505 | num[i] = time_str[0]; |
| 506 | time_str++; |
| 507 | i++; |
| 508 | } |
| 509 | if ( time_str[0] == ':' ) { |
| 510 | time_str++; |
| 511 | } |
| 512 | num[i] = '\0'; |
| 513 | minutes = atof(num); |
| 514 | // printf("minutes = %.2lf\n", minutes); |
| 515 | |
| 516 | result += minutes / 60.0; |
| 517 | } |
| 518 | |
| 519 | // get seconds |
| 520 | if ( strlen(time_str) ) { |
| 521 | i = 0; |
| 522 | while ( (time_str[0] != ':') && (time_str[0] != '\0') ) { |
| 523 | num[i] = time_str[0]; |
| 524 | time_str++; |
| 525 | i++; |
| 526 | } |
| 527 | num[i] = '\0'; |
| 528 | seconds = atof(num); |
| 529 | // printf("seconds = %.2lf\n", seconds); |
| 530 | |
| 531 | result += seconds / 3600.0; |
| 532 | } |
| 533 | |
| 534 | SG_LOG( SG_GENERAL, SG_INFO, " parse_time() = " << sign * result ); |
| 535 | |
| 536 | return(sign * result); |
| 537 | } |
| 538 | |
| 539 | // parse a date string (yyyy:mm:dd:hh:mm:ss) into a time_t (seconds) |
| 540 | static long int |
| 541 | parse_date( const string& date, const char* timeType) |
| 542 | { |
| 543 | struct tm gmt,*pCurrentTime; |
| 544 | int year,month,day,hour,minute,second; |
| 545 | char *argument, *date_str; |
| 546 | |
| 547 | SGTime CurrentTime; |
| 548 | CurrentTime.update(SGGeod(),0,0); |
| 549 | |
| 550 | // FIXME This should obtain system/aircraft/GMT time depending on timeType |
| 551 | pCurrentTime = CurrentTime.getGmt(); |
| 552 | |
| 553 | // initialize all fields with current time |
| 554 | year = pCurrentTime->tm_year + 1900; |
| 555 | month = pCurrentTime->tm_mon + 1; |
| 556 | day = pCurrentTime->tm_mday; |
| 557 | hour = pCurrentTime->tm_hour; |
| 558 | minute = pCurrentTime->tm_min; |
| 559 | second = pCurrentTime->tm_sec; |
| 560 | |
| 561 | argument = (char *)date.c_str(); |
| 562 | date_str = argument; |
| 563 | |
| 564 | // start with parsing year |
| 565 | if (!strlen(date_str) || |
| 566 | !parseIntValue(&date_str,&year,0,9999,"year",argument)) |
| 567 | { |
| 568 | return -1; |
| 569 | } |
| 570 | |
| 571 | if (year < 1970) |
| 572 | { |
| 573 | SG_LOG(SG_GENERAL, SG_ALERT, "Invalid year '" << year << "'. Use 1970 or later."); |
| 574 | return -1; |
| 575 | } |
| 576 | |
| 577 | parseIntValue(&date_str, &month, 1, 12, "month", argument); |
| 578 | parseIntValue(&date_str, &day, 1, 31, "day", argument); |
| 579 | parseIntValue(&date_str, &hour, 0, 23, "hour", argument); |
| 580 | parseIntValue(&date_str, &minute, 0, 59, "minute", argument); |
| 581 | parseIntValue(&date_str, &second, 0, 59, "second", argument); |
| 582 | |
| 583 | gmt.tm_sec = second; |
| 584 | gmt.tm_min = minute; |
| 585 | gmt.tm_hour = hour; |
| 586 | gmt.tm_mday = day; |
| 587 | gmt.tm_mon = month - 1; |
| 588 | gmt.tm_year = year -1900; |
| 589 | gmt.tm_isdst = 0; // ignore daylight savings time for the moment |
| 590 | |
| 591 | time_t theTime = sgTimeGetGMT( gmt.tm_year, gmt.tm_mon, gmt.tm_mday, |
| 592 | gmt.tm_hour, gmt.tm_min, gmt.tm_sec ); |
| 593 | |
| 594 | SG_LOG(SG_GENERAL, SG_INFO, "Configuring startup time to " << ctime(&theTime)); |
| 595 | |
| 596 | return (theTime); |
| 597 | } |
| 598 | |
| 599 | |
| 600 | // parse angle in the form of [+/-]ddd:mm:ss into degrees |
| 601 | static double |
| 602 | parse_degree( const string& degree_str) { |
| 603 | double result = parse_time( degree_str ); |
| 604 | |
| 605 | // printf("Degree = %.4f\n", result); |
| 606 | |
| 607 | return(result); |
| 608 | } |
| 609 | |
| 610 | |
| 611 | // parse time offset string into seconds |
| 612 | static long int |
| 613 | parse_time_offset( const string& time_str) { |
| 614 | long int result; |
| 615 | |
| 616 | // printf("time offset = %s\n", time_str); |
| 617 | |
| 618 | #ifdef HAVE_RINT |
| 619 | result = (int)rint(parse_time(time_str) * 3600.0); |
| 620 | #else |
| 621 | result = (int)(parse_time(time_str) * 3600.0); |
| 622 | #endif |
| 623 | |
| 624 | // printf("parse_time_offset(): %d\n", result); |
| 625 | |
| 626 | return( result ); |
| 627 | } |
| 628 | |
| 629 | |
| 630 | // Parse --fov=x.xx type option |
| 631 | static double |
| 632 | parse_fov( const string& arg ) { |
| 633 | double fov = atof(arg); |
| 634 | |
| 635 | if ( fov < FG_FOV_MIN ) { fov = FG_FOV_MIN; } |
| 636 | if ( fov > FG_FOV_MAX ) { fov = FG_FOV_MAX; } |
| 637 | |
| 638 | fgSetDouble("/sim/view[0]/config/default-field-of-view-deg", fov); |
| 639 | |
| 640 | // printf("parse_fov(): result = %.4f\n", fov); |
| 641 | |
| 642 | return fov; |
| 643 | } |
| 644 | |
| 645 | |
| 646 | // Parse I/O channel option |
| 647 | // |
| 648 | // Format is "--protocol=medium,direction,hz,medium_options,..." |
| 649 | // |
| 650 | // protocol = { native, nmea, garmin, AV400, AV400Sim, fgfs, rul, pve, etc. } |
| 651 | // medium = { serial, socket, file, etc. } |
| 652 | // direction = { in, out, bi } |
| 653 | // hz = number of times to process channel per second (floating |
| 654 | // point values are ok. |
| 655 | // |
| 656 | // Serial example "--nmea=serial,dir,hz,device,baud" where |
| 657 | // |
| 658 | // device = OS device name of serial line to be open()'ed |
| 659 | // baud = {300, 1200, 2400, ..., 230400} |
| 660 | // |
| 661 | // Socket example "--native=socket,dir,hz,machine,port,style" where |
| 662 | // |
| 663 | // machine = machine name or ip address if client (leave empty if server) |
| 664 | // port = port, leave empty to let system choose |
| 665 | // style = tcp or udp |
| 666 | // |
| 667 | // File example "--garmin=file,dir,hz,filename" where |
| 668 | // |
| 669 | // filename = file system file name |
| 670 | |
| 671 | static bool |
| 672 | add_channel( const string& type, const string& channel_str ) { |
| 673 | // This check is necessary to prevent fgviewer from segfaulting when given |
| 674 | // weird options. (It doesn't run the full initialization) |
| 675 | if(!globals->get_channel_options_list()) |
| 676 | { |
| 677 | SG_LOG(SG_GENERAL, SG_ALERT, "Option " << type << "=" << channel_str |
| 678 | << " ignored."); |
| 679 | return false; |
| 680 | } |
| 681 | SG_LOG(SG_GENERAL, SG_INFO, "Channel string = " << channel_str ); |
| 682 | globals->get_channel_options_list()->push_back( type + "," + channel_str ); |
| 683 | return true; |
| 684 | } |
| 685 | |
| 686 | static void |
| 687 | clearLocation () |
| 688 | { |
| 689 | fgSetString("/sim/presets/airport-id", ""); |
| 690 | fgSetString("/sim/presets/vor-id", ""); |
| 691 | fgSetString("/sim/presets/ndb-id", ""); |
| 692 | fgSetString("/sim/presets/carrier", ""); |
| 693 | fgSetString("/sim/presets/parkpos", ""); |
| 694 | fgSetString("/sim/presets/carrier-position", ""); |
| 695 | fgSetString("/sim/presets/fix", ""); |
| 696 | fgSetString("/sim/presets/tacan-id", ""); |
| 697 | } |
| 698 | |
| 699 | /* |
| 700 | Using --addon=/foo/bar does: |
| 701 | - register the add-on with the AddonManager (enabling, among other things, |
| 702 | add-on-specific resources for simgear::ResourceManager); |
| 703 | - load /foo/bar/addon-config.xml into the Global Property Tree; |
| 704 | - add /foo/bar to the list of aircraft paths to provide read access: |
| 705 | - set various properties related to the add-on under /addons; |
| 706 | - load /foo/bar/addon-main.nas into namespace __addon[ADDON_ID]__ |
| 707 | (see $FG_ROOT/Nasal/addons.nas); |
| 708 | - call the main() function defined in that file. |
| 709 | |
| 710 | For more details, see $FG_ROOT/Docs/README.add-ons. |
| 711 | */ |
| 712 | static int |
| 713 | fgOptAddon(const char *arg) |
| 714 | { |
| 715 | const SGPath addonPath = SGPath::fromUtf8(arg); |
| 716 | const auto& addonManager = addons::AddonManager::instance(); |
| 717 | |
| 718 | try { |
| 719 | addonManager->registerAddon(addonPath); |
| 720 | } catch (const sg_exception &e) { |
| 721 | string msg = "Error registering an add-on: " + e.getFormattedMessage(); |
| 722 | SG_LOG(SG_GENERAL, SG_ALERT, msg); |
| 723 | flightgear::fatalMessageBoxThenExit( |
| 724 | "FlightGear", "Unable to register an add-on.", msg); |
| 725 | } |
| 726 | |
| 727 | return FG_OPTIONS_OK; |
| 728 | } |
| 729 | |
| 730 | static int |
| 731 | fgOptAdditionalDataDir(const char* arg) |
| 732 | { |
| 733 | const SGPath dataPath = SGPath::fromUtf8(arg); |
| 734 | if (!dataPath.exists()) { |
| 735 | SG_LOG(SG_GENERAL, SG_ALERT, "--data path not found:'" << dataPath << "'"); |
| 736 | flightgear::fatalMessageBoxWithoutExit( |
| 737 | "FlightGear", "Data path not found: '" + dataPath.utf8Str() + "'."); |
| 738 | return FG_OPTIONS_EXIT; |
| 739 | } |
| 740 | |
| 741 | globals->append_data_path(dataPath, false /* = before FG_ROOT */); |
| 742 | return FG_OPTIONS_OK; |
| 743 | } |
| 744 | |
| 745 | static int |
| 746 | fgOptVOR( const char * arg ) |
| 747 | { |
| 748 | clearLocation(); |
| 749 | fgSetString("/sim/presets/vor-id", arg); |
| 750 | return FG_OPTIONS_OK; |
| 751 | } |
| 752 | |
| 753 | static int |
| 754 | fgOptNDB( const char * arg ) |
| 755 | { |
| 756 | clearLocation(); |
| 757 | fgSetString("/sim/presets/ndb-id", arg); |
| 758 | return FG_OPTIONS_OK; |
| 759 | } |
| 760 | |
| 761 | static int |
| 762 | fgOptCarrier( const char * arg ) |
| 763 | { |
| 764 | clearLocation(); |
| 765 | fgSetString("/sim/presets/carrier", arg); |
| 766 | return FG_OPTIONS_OK; |
| 767 | } |
| 768 | |
| 769 | static int |
| 770 | fgOptCarrierPos( const char * arg ) |
| 771 | { |
| 772 | fgSetString("/sim/presets/carrier-position", arg); |
| 773 | return FG_OPTIONS_OK; |
| 774 | } |
| 775 | |
| 776 | static int |
| 777 | fgOptParkpos( const char * arg ) |
| 778 | { |
| 779 | fgSetString("/sim/presets/parkpos", arg); |
| 780 | fgSetBool("/sim/presets/parking-requested", true); |
| 781 | return FG_OPTIONS_OK; |
| 782 | } |
| 783 | |
| 784 | static int |
| 785 | fgOptFIX( const char * arg ) |
| 786 | { |
| 787 | clearLocation(); |
| 788 | fgSetString("/sim/presets/fix", arg); |
| 789 | return FG_OPTIONS_OK; |
| 790 | } |
| 791 | |
| 792 | static int |
| 793 | fgOptLon( const char *arg ) |
| 794 | { |
| 795 | clearLocation(); |
| 796 | fgSetDouble("/sim/presets/longitude-deg", parse_degree( arg )); |
| 797 | fgSetDouble("/position/longitude-deg", parse_degree( arg )); |
| 798 | return FG_OPTIONS_OK; |
| 799 | } |
| 800 | |
| 801 | static int |
| 802 | fgOptLat( const char *arg ) |
| 803 | { |
| 804 | clearLocation(); |
| 805 | fgSetDouble("/sim/presets/latitude-deg", parse_degree( arg )); |
| 806 | fgSetDouble("/position/latitude-deg", parse_degree( arg )); |
| 807 | return FG_OPTIONS_OK; |
| 808 | } |
| 809 | |
| 810 | static int |
| 811 | fgOptTACAN(const char* arg) |
| 812 | { |
| 813 | clearLocation(); |
| 814 | fgSetString("/sim/presets/tacan-id", arg); |
| 815 | return FG_OPTIONS_OK; |
| 816 | } |
| 817 | |
| 818 | static int |
| 819 | fgOptAltitude( const char *arg ) |
| 820 | { |
| 821 | fgSetBool("/sim/presets/onground", false); |
| 822 | if ( fgGetString("/sim/startup/units") == "feet" ) |
| 823 | fgSetDouble("/sim/presets/altitude-ft", atof( arg )); |
| 824 | else |
| 825 | fgSetDouble("/sim/presets/altitude-ft", |
| 826 | atof( arg ) * SG_METER_TO_FEET); |
| 827 | return FG_OPTIONS_OK; |
| 828 | } |
| 829 | |
| 830 | static int |
| 831 | fgOptUBody( const char *arg ) |
| 832 | { |
| 833 | fgSetString("/sim/presets/speed-set", "UVW"); |
| 834 | if ( fgGetString("/sim/startup/units") == "feet" ) |
| 835 | fgSetDouble("/sim/presets/uBody-fps", atof( arg )); |
| 836 | else |
| 837 | fgSetDouble("/sim/presets/uBody-fps", |
| 838 | atof( arg ) * SG_METER_TO_FEET); |
| 839 | return FG_OPTIONS_OK; |
| 840 | } |
| 841 | |
| 842 | static int |
| 843 | fgOptVBody( const char *arg ) |
| 844 | { |
| 845 | fgSetString("/sim/presets/speed-set", "UVW"); |
| 846 | if ( fgGetString("/sim/startup/units") == "feet" ) |
| 847 | fgSetDouble("/sim/presets/vBody-fps", atof( arg )); |
| 848 | else |
| 849 | fgSetDouble("/sim/presets/vBody-fps", |
| 850 | atof( arg ) * SG_METER_TO_FEET); |
| 851 | return FG_OPTIONS_OK; |
| 852 | } |
| 853 | |
| 854 | static int |
| 855 | fgOptWBody( const char *arg ) |
| 856 | { |
| 857 | fgSetString("/sim/presets/speed-set", "UVW"); |
| 858 | if ( fgGetString("/sim/startup/units") == "feet" ) |
| 859 | fgSetDouble("/sim/presets/wBody-fps", atof(arg)); |
| 860 | else |
| 861 | fgSetDouble("/sim/presets/wBody-fps", |
| 862 | atof(arg) * SG_METER_TO_FEET); |
| 863 | return FG_OPTIONS_OK; |
| 864 | } |
| 865 | |
| 866 | static int |
| 867 | fgOptVNorth( const char *arg ) |
| 868 | { |
| 869 | fgSetString("/sim/presets/speed-set", "NED"); |
| 870 | if ( fgGetString("/sim/startup/units") == "feet" ) |
| 871 | fgSetDouble("/sim/presets/speed-north-fps", atof( arg )); |
| 872 | else |
| 873 | fgSetDouble("/sim/presets/speed-north-fps", |
| 874 | atof( arg ) * SG_METER_TO_FEET); |
| 875 | return FG_OPTIONS_OK; |
| 876 | } |
| 877 | |
| 878 | static int |
| 879 | fgOptVEast( const char *arg ) |
| 880 | { |
| 881 | fgSetString("/sim/presets/speed-set", "NED"); |
| 882 | if ( fgGetString("/sim/startup/units") == "feet" ) |
| 883 | fgSetDouble("/sim/presets/speed-east-fps", atof(arg)); |
| 884 | else |
| 885 | fgSetDouble("/sim/presets/speed-east-fps", |
| 886 | atof(arg) * SG_METER_TO_FEET); |
| 887 | return FG_OPTIONS_OK; |
| 888 | } |
| 889 | |
| 890 | static int |
| 891 | fgOptVDown( const char *arg ) |
| 892 | { |
| 893 | fgSetString("/sim/presets/speed-set", "NED"); |
| 894 | if ( fgGetString("/sim/startup/units") == "feet" ) |
| 895 | fgSetDouble("/sim/presets/speed-down-fps", atof(arg)); |
| 896 | else |
| 897 | fgSetDouble("/sim/presets/speed-down-fps", |
| 898 | atof(arg) * SG_METER_TO_FEET); |
| 899 | return FG_OPTIONS_OK; |
| 900 | } |
| 901 | |
| 902 | static int |
| 903 | fgOptVc( const char *arg ) |
| 904 | { |
| 905 | // fgSetString("/sim/presets/speed-set", "knots"); |
| 906 | // fgSetDouble("/velocities/airspeed-kt", atof(arg.substr(5))); |
| 907 | fgSetString("/sim/presets/speed-set", "knots"); |
| 908 | fgSetDouble("/sim/presets/airspeed-kt", atof(arg)); |
| 909 | return FG_OPTIONS_OK; |
| 910 | } |
| 911 | |
| 912 | static int |
| 913 | fgOptMach( const char *arg ) |
| 914 | { |
| 915 | fgSetString("/sim/presets/speed-set", "mach"); |
| 916 | fgSetDouble("/sim/presets/mach", atof(arg)); |
| 917 | return FG_OPTIONS_OK; |
| 918 | } |
| 919 | |
| 920 | static int |
| 921 | fgOptRoc( const char *arg ) |
| 922 | { |
| 923 | fgSetDouble("/sim/presets/vertical-speed-fps", atof(arg)/60); |
| 924 | return FG_OPTIONS_OK; |
| 925 | } |
| 926 | |
| 927 | static int |
| 928 | fgOptFgScenery( const char *arg ) |
| 929 | { |
| 930 | globals->append_fg_scenery(SGPath::pathsFromUtf8(arg)); |
| 931 | return FG_OPTIONS_OK; |
| 932 | } |
| 933 | |
| 934 | static int |
| 935 | fgOptAllowNasalRead( const char *arg ) |
| 936 | { |
| 937 | PathList paths = SGPath::pathsFromUtf8(arg); |
| 938 | if(paths.size() == 0) { |
| 939 | SG_LOG(SG_GENERAL, SG_WARN, "--allow-nasal-read requires a list of directories to allow"); |
| 940 | } |
| 941 | for( PathList::const_iterator it = paths.begin(); it != paths.end(); ++it ) { |
| 942 | globals->append_read_allowed_paths(*it); |
| 943 | } |
| 944 | return FG_OPTIONS_OK; |
| 945 | } |
| 946 | |
| 947 | static int |
| 948 | fgOptFov( const char *arg ) |
| 949 | { |
| 950 | parse_fov( arg ); |
| 951 | return FG_OPTIONS_OK; |
| 952 | } |
| 953 | |
| 954 | static int |
| 955 | fgOptGeometry( const char *arg ) |
| 956 | { |
| 957 | bool geometry_ok = true; |
| 958 | int xsize = 0, ysize = 0; |
| 959 | string geometry = arg; |
| 960 | string::size_type i = geometry.find('x'); |
| 961 | |
| 962 | if (i != string::npos) { |
| 963 | xsize = atoi(geometry.substr(0, i)); |
| 964 | ysize = atoi(geometry.substr(i+1)); |
| 965 | } else { |
| 966 | geometry_ok = false; |
| 967 | } |
| 968 | |
| 969 | if ( xsize <= 0 || ysize <= 0 ) { |
| 970 | xsize = 640; |
| 971 | ysize = 480; |
| 972 | geometry_ok = false; |
| 973 | } |
| 974 | |
| 975 | if ( !geometry_ok ) { |
| 976 | SG_LOG( SG_GENERAL, SG_ALERT, "Unknown geometry: " << geometry ); |
| 977 | SG_LOG( SG_GENERAL, SG_ALERT, |
| 978 | "Setting geometry to " << xsize << 'x' << ysize << '\n'); |
| 979 | } else { |
| 980 | SG_LOG( SG_GENERAL, SG_INFO, |
| 981 | "Setting geometry to " << xsize << 'x' << ysize << '\n'); |
| 982 | fgSetInt("/sim/startup/xsize", xsize); |
| 983 | fgSetInt("/sim/startup/ysize", ysize); |
| 984 | } |
| 985 | return FG_OPTIONS_OK; |
| 986 | } |
| 987 | |
| 988 | static int |
| 989 | fgOptBpp( const char *arg ) |
| 990 | { |
| 991 | string bits_per_pix = arg; |
| 992 | if ( bits_per_pix == "16" ) { |
| 993 | fgSetInt("/sim/rendering/bits-per-pixel", 16); |
| 994 | } else if ( bits_per_pix == "24" ) { |
| 995 | fgSetInt("/sim/rendering/bits-per-pixel", 24); |
| 996 | } else if ( bits_per_pix == "32" ) { |
| 997 | fgSetInt("/sim/rendering/bits-per-pixel", 32); |
| 998 | } else { |
| 999 | SG_LOG(SG_GENERAL, SG_ALERT, "Unsupported bpp " << bits_per_pix); |
| 1000 | } |
| 1001 | return FG_OPTIONS_OK; |
| 1002 | } |
| 1003 | |
| 1004 | static int |
| 1005 | fgOptTimeOffset( const char *arg ) |
| 1006 | { |
| 1007 | fgSetLong("/sim/startup/time-offset", |
| 1008 | parse_time_offset( arg )); |
| 1009 | fgSetString("/sim/startup/time-offset-type", "system-offset"); |
| 1010 | return FG_OPTIONS_OK; |
| 1011 | } |
| 1012 | |
| 1013 | static int |
| 1014 | fgOptStartDateSys( const char *arg ) |
| 1015 | { |
| 1016 | long int theTime = parse_date( arg, "system" ); |
| 1017 | if (theTime>=0) |
| 1018 | { |
| 1019 | fgSetLong("/sim/startup/time-offset", theTime); |
| 1020 | fgSetString("/sim/startup/time-offset-type", "system"); |
| 1021 | } |
| 1022 | return FG_OPTIONS_OK; |
| 1023 | } |
| 1024 | |
| 1025 | static int |
| 1026 | fgOptStartDateLat( const char *arg ) |
| 1027 | { |
| 1028 | long int theTime = parse_date( arg, "latitude" ); |
| 1029 | if (theTime>=0) |
| 1030 | { |
| 1031 | fgSetLong("/sim/startup/time-offset", theTime); |
| 1032 | fgSetString("/sim/startup/time-offset-type", "latitude"); |
| 1033 | } |
| 1034 | return FG_OPTIONS_OK; |
| 1035 | } |
| 1036 | |
| 1037 | static int |
| 1038 | fgOptStartDateGmt( const char *arg ) |
| 1039 | { |
| 1040 | long int theTime = parse_date( arg, "gmt" ); |
| 1041 | if (theTime>=0) |
| 1042 | { |
| 1043 | fgSetLong("/sim/startup/time-offset", theTime); |
| 1044 | fgSetString("/sim/startup/time-offset-type", "gmt"); |
| 1045 | } |
| 1046 | return FG_OPTIONS_OK; |
| 1047 | } |
| 1048 | |
| 1049 | static int |
| 1050 | fgOptJpgHttpd( const char * arg ) |
| 1051 | { |
| 1052 | SG_LOG(SG_ALL,SG_ALERT, |
| 1053 | "the option --jpg-httpd is no longer supported! Please use --httpd instead." |
| 1054 | " URL for the screenshot within the new httpd is http://YourFgServer:xxxx/screenshot"); |
| 1055 | return FG_OPTIONS_EXIT; |
| 1056 | } |
| 1057 | |
| 1058 | static int |
| 1059 | fgOptHttpd( const char * arg ) |
| 1060 | { |
| 1061 | // port may be any valid address:port notation |
| 1062 | // like 127.0.0.1:8080 |
| 1063 | // or just the port 8080 |
| 1064 | string port = simgear::strutils::strip(string(arg)); |
| 1065 | if( port.empty() ) return FG_OPTIONS_ERROR; |
| 1066 | fgSetString( string(flightgear::http::PROPERTY_ROOT).append("/options/listening-port").c_str(), port ); |
| 1067 | return FG_OPTIONS_OK; |
| 1068 | } |
| 1069 | |
| 1070 | static int |
| 1071 | fgSetupProxy( const char *arg ) |
| 1072 | { |
| 1073 | string options = simgear::strutils::strip( arg ); |
| 1074 | string host, port, auth; |
| 1075 | string::size_type pos; |
| 1076 | |
| 1077 | // this is NURLP - NURLP is not an url parser |
| 1078 | if( simgear::strutils::starts_with( options, "http://" ) ) |
| 1079 | options = options.substr( 7 ); |
| 1080 | if( simgear::strutils::ends_with( options, "/" ) ) |
| 1081 | options = options.substr( 0, options.length() - 1 ); |
| 1082 | |
| 1083 | host = port = auth = ""; |
| 1084 | if ((pos = options.find("@")) != string::npos) |
| 1085 | auth = options.substr(0, pos++); |
| 1086 | else |
| 1087 | pos = 0; |
| 1088 | |
| 1089 | host = options.substr(pos, options.size()); |
| 1090 | if ((pos = host.find(":")) != string::npos) { |
| 1091 | port = host.substr(++pos, host.size()); |
| 1092 | host.erase(--pos, host.size()); |
| 1093 | } |
| 1094 | |
| 1095 | fgSetString("/sim/presets/proxy/host", host.c_str()); |
| 1096 | fgSetString("/sim/presets/proxy/port", port.c_str()); |
| 1097 | fgSetString("/sim/presets/proxy/authentication", auth.c_str()); |
| 1098 | |
| 1099 | return FG_OPTIONS_OK; |
| 1100 | } |
| 1101 | |
| 1102 | static int |
| 1103 | fgOptTraceRead( const char *arg ) |
| 1104 | { |
| 1105 | string name = arg; |
| 1106 | SG_LOG(SG_GENERAL, SG_INFO, "Tracing reads for property " << name); |
| 1107 | fgGetNode(name.c_str(), true) |
| 1108 | ->setAttribute(SGPropertyNode::TRACE_READ, true); |
| 1109 | return FG_OPTIONS_OK; |
| 1110 | } |
| 1111 | |
| 1112 | static int |
| 1113 | fgOptLogLevel( const char *arg ) |
| 1114 | { |
| 1115 | fgSetString("/sim/logging/priority", arg); |
| 1116 | setLoggingPriority(arg); |
| 1117 | |
| 1118 | return FG_OPTIONS_OK; |
| 1119 | } |
| 1120 | |
| 1121 | static int |
| 1122 | fgOptLogClasses( const char *arg ) |
| 1123 | { |
| 1124 | fgSetString("/sim/logging/classes", arg); |
| 1125 | setLoggingClasses (arg); |
| 1126 | |
| 1127 | return FG_OPTIONS_OK; |
| 1128 | } |
| 1129 | |
| 1130 | static int |
| 1131 | fgOptLogDir(const char* arg) |
| 1132 | { |
| 1133 | SGPath dirPath; |
| 1134 | if (!strcmp(arg, "desktop")) { |
| 1135 | dirPath = SGPath::desktop(); |
| 1136 | } else { |
| 1137 | dirPath = SGPath::fromUtf8(arg); |
| 1138 | } |
| 1139 | |
| 1140 | if (!dirPath.isDir()) { |
| 1141 | SG_LOG(SG_GENERAL, SG_ALERT, "cannot find logging location " << dirPath); |
| 1142 | return FG_OPTIONS_ERROR; |
| 1143 | } |
| 1144 | |
| 1145 | if (!dirPath.canWrite()) { |
| 1146 | SG_LOG(SG_GENERAL, SG_ALERT, "cannot write to logging location " << dirPath); |
| 1147 | return FG_OPTIONS_ERROR; |
| 1148 | } |
| 1149 | |
| 1150 | // generate the log file name |
| 1151 | SGPath logFile; |
| 1152 | { |
| 1153 | char fileNameBuffer[100]; |
| 1154 | time_t now; |
| 1155 | time(&now); |
| 1156 | strftime(fileNameBuffer, 99, "FlightGear_%F", localtime(&now)); |
| 1157 | |
| 1158 | unsigned int logsTodayCount = 0; |
| 1159 | while (true) { |
| 1160 | std::ostringstream os; |
| 1161 | os << fileNameBuffer << "_" << logsTodayCount++ << ".log"; |
| 1162 | logFile = dirPath / os.str(); |
| 1163 | if (!logFile.exists()) { |
| 1164 | break; |
| 1165 | } |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | sglog().logToFile(logFile, sglog().get_log_classes(), sglog().get_log_priority()); |
| 1170 | |
| 1171 | return FG_OPTIONS_OK; |
| 1172 | } |
| 1173 | |
| 1174 | static int |
| 1175 | fgOptTraceWrite( const char *arg ) |
| 1176 | { |
| 1177 | string name = arg; |
| 1178 | SG_LOG(SG_GENERAL, SG_INFO, "Tracing writes for property " << name); |
| 1179 | fgGetNode(name.c_str(), true) |
| 1180 | ->setAttribute(SGPropertyNode::TRACE_WRITE, true); |
| 1181 | return FG_OPTIONS_OK; |
| 1182 | } |
| 1183 | |
| 1184 | static int |
| 1185 | fgOptViewOffset( const char *arg ) |
| 1186 | { |
| 1187 | // $$$ begin - added VS Renganathan, 14 Oct 2K |
| 1188 | // for multi-window outside window imagery |
| 1189 | string woffset = arg; |
| 1190 | double default_view_offset = 0.0; |
| 1191 | if ( woffset == "LEFT" ) { |
| 1192 | default_view_offset = SGD_PI * 0.25; |
| 1193 | } else if ( woffset == "RIGHT" ) { |
| 1194 | default_view_offset = SGD_PI * 1.75; |
| 1195 | } else if ( woffset == "CENTER" ) { |
| 1196 | default_view_offset = 0.00; |
| 1197 | } else { |
| 1198 | default_view_offset = atof( woffset.c_str() ) * SGD_DEGREES_TO_RADIANS; |
| 1199 | } |
| 1200 | /* apparently not used (CLO, 11 Jun 2002) |
| 1201 | FGViewer *pilot_view = |
| 1202 | (FGViewer *)globals->get_viewmgr()->get_view( 0 ); */ |
| 1203 | // this will work without calls to the viewer... |
| 1204 | fgSetDouble("/sim/view[0]/config/heading-offset-deg", |
| 1205 | default_view_offset * SGD_RADIANS_TO_DEGREES); |
| 1206 | // $$$ end - added VS Renganathan, 14 Oct 2K |
| 1207 | return FG_OPTIONS_OK; |
| 1208 | } |
| 1209 | |
| 1210 | static int |
| 1211 | fgOptVisibilityMeters( const char *arg ) |
| 1212 | { |
| 1213 | Environment::Presets::VisibilitySingleton::instance()->preset( atof( arg ) ); |
| 1214 | return FG_OPTIONS_OK; |
| 1215 | } |
| 1216 | |
| 1217 | static int |
| 1218 | fgOptVisibilityMiles( const char *arg ) |
| 1219 | { |
| 1220 | Environment::Presets::VisibilitySingleton::instance()->preset( atof( arg ) * 5280.0 * SG_FEET_TO_METER ); |
| 1221 | return FG_OPTIONS_OK; |
| 1222 | } |
| 1223 | |
| 1224 | static int |
| 1225 | fgOptMetar(const char *arg) |
| 1226 | { |
| 1227 | // The given METAR string cannot be effective without disabling |
| 1228 | // real weather fetching. |
| 1229 | fgSetBool("/environment/realwx/enabled", false); |
| 1230 | // The user-supplied METAR string |
| 1231 | fgSetString("/environment/metar/data", arg); |
| 1232 | |
| 1233 | return FG_OPTIONS_OK; |
| 1234 | } |
| 1235 | static int |
| 1236 | fgOptConsole(const char *arg) |
| 1237 | { |
| 1238 | static bool already_done = false; |
| 1239 | if (!already_done && Options::paramToBool(arg)) |
| 1240 | { |
| 1241 | already_done = true; |
| 1242 | simgear::requestConsole(false); |
| 1243 | } |
| 1244 | return FG_OPTIONS_OK; |
| 1245 | } |
| 1246 | |
| 1247 | static int |
| 1248 | fgOptRandomWind( const char *arg ) |
| 1249 | { |
| 1250 | double min_hdg = sg_random() * 360.0; |
| 1251 | double max_hdg = min_hdg + (20 - sqrt(sg_random() * 400)); |
| 1252 | double speed = sg_random() * sg_random() * 40; |
| 1253 | double gust = speed + (10 - sqrt(sg_random() * 100)); |
| 1254 | Environment::Presets::WindSingleton::instance()->preset(min_hdg, max_hdg, speed, gust); |
| 1255 | return FG_OPTIONS_OK; |
| 1256 | } |
| 1257 | |
| 1258 | static int |
| 1259 | fgOptWind( const char *arg ) |
| 1260 | { |
| 1261 | double min_hdg = 0.0, max_hdg = 0.0, speed = 0.0, gust = 0.0; |
| 1262 | if (!parse_wind( arg, &min_hdg, &max_hdg, &speed, &gust)) { |
| 1263 | SG_LOG( SG_GENERAL, SG_ALERT, "bad wind value " << arg ); |
| 1264 | return FG_OPTIONS_ERROR; |
| 1265 | } |
| 1266 | Environment::Presets::WindSingleton::instance()->preset(min_hdg, max_hdg, speed, gust); |
| 1267 | return FG_OPTIONS_OK; |
| 1268 | } |
| 1269 | |
| 1270 | static int |
| 1271 | fgOptTurbulence( const char *arg ) |
| 1272 | { |
| 1273 | Environment::Presets::TurbulenceSingleton::instance()->preset( atof(arg) ); |
| 1274 | return FG_OPTIONS_OK; |
| 1275 | } |
| 1276 | |
| 1277 | static int |
| 1278 | fgOptCeiling( const char *arg ) |
| 1279 | { |
| 1280 | double elevation, thickness; |
| 1281 | string spec = arg; |
| 1282 | string::size_type pos = spec.find(':'); |
| 1283 | if (pos == string::npos) { |
| 1284 | elevation = atof(spec.c_str()); |
| 1285 | thickness = 2000; |
| 1286 | } else { |
| 1287 | elevation = atof(spec.substr(0, pos).c_str()); |
| 1288 | thickness = atof(spec.substr(pos + 1).c_str()); |
| 1289 | } |
| 1290 | Environment::Presets::CeilingSingleton::instance()->preset( elevation, thickness ); |
| 1291 | return FG_OPTIONS_OK; |
| 1292 | } |
| 1293 | |
| 1294 | static int |
| 1295 | fgOptWp( const char *arg ) |
| 1296 | { |
| 1297 | string_list *waypoints = globals->get_initial_waypoints(); |
| 1298 | if (!waypoints) { |
| 1299 | waypoints = new string_list; |
| 1300 | globals->set_initial_waypoints(waypoints); |
| 1301 | } |
| 1302 | waypoints->push_back(arg); |
| 1303 | return FG_OPTIONS_OK; |
| 1304 | } |
| 1305 | |
| 1306 | static bool |
| 1307 | parse_colon (const string &s, double * val1, double * val2) |
| 1308 | { |
| 1309 | string::size_type pos = s.find(':'); |
| 1310 | if (pos == string::npos) { |
| 1311 | *val2 = atof(s); |
| 1312 | return false; |
| 1313 | } else { |
| 1314 | *val1 = atof(s.substr(0, pos).c_str()); |
| 1315 | *val2 = atof(s.substr(pos+1).c_str()); |
| 1316 | return true; |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | |
| 1321 | static int |
| 1322 | fgOptFailure( const char * arg ) |
| 1323 | { |
| 1324 | string a = arg; |
| 1325 | if (a == "pitot") { |
| 1326 | fgSetBool("/systems/pitot/serviceable", false); |
| 1327 | } else if (a == "static") { |
| 1328 | fgSetBool("/systems/static/serviceable", false); |
| 1329 | } else if (a == "vacuum") { |
| 1330 | fgSetBool("/systems/vacuum/serviceable", false); |
| 1331 | } else if (a == "electrical") { |
| 1332 | fgSetBool("/systems/electrical/serviceable", false); |
| 1333 | } else { |
| 1334 | SG_LOG(SG_INPUT, SG_ALERT, "Unknown failure mode: " << a); |
| 1335 | return FG_OPTIONS_ERROR; |
| 1336 | } |
| 1337 | |
| 1338 | return FG_OPTIONS_OK; |
| 1339 | } |
| 1340 | |
| 1341 | |
| 1342 | static int |
| 1343 | fgOptNAV1( const char * arg ) |
| 1344 | { |
| 1345 | double radial, freq; |
| 1346 | if (parse_colon(arg, &radial, &freq)) |
| 1347 | fgSetDouble("/instrumentation/nav[0]/radials/selected-deg", radial); |
| 1348 | fgSetDouble("/instrumentation/nav[0]/frequencies/selected-mhz", freq); |
| 1349 | return FG_OPTIONS_OK; |
| 1350 | } |
| 1351 | |
| 1352 | static int |
| 1353 | fgOptNAV2( const char * arg ) |
| 1354 | { |
| 1355 | double radial, freq; |
| 1356 | if (parse_colon(arg, &radial, &freq)) |
| 1357 | fgSetDouble("/instrumentation/nav[1]/radials/selected-deg", radial); |
| 1358 | fgSetDouble("/instrumentation/nav[1]/frequencies/selected-mhz", freq); |
| 1359 | return FG_OPTIONS_OK; |
| 1360 | } |
| 1361 | |
| 1362 | static int |
| 1363 | fgOptADF( const char * arg ) |
| 1364 | { |
| 1365 | SG_LOG(SG_ALL,SG_ALERT, |
| 1366 | "the option --adf is no longer supported! Please use --adf1 " |
| 1367 | "instead."); |
| 1368 | return FG_OPTIONS_EXIT; |
| 1369 | } |
| 1370 | |
| 1371 | static int |
| 1372 | fgOptADF1( const char * arg ) |
| 1373 | { |
| 1374 | double rot, freq; |
| 1375 | if (parse_colon(arg, &rot, &freq)) |
| 1376 | fgSetDouble("/instrumentation/adf[0]/rotation-deg", rot); |
| 1377 | fgSetDouble("/instrumentation/adf[0]/frequencies/selected-khz", freq); |
| 1378 | return FG_OPTIONS_OK; |
| 1379 | } |
| 1380 | |
| 1381 | static int |
| 1382 | fgOptADF2( const char * arg ) |
| 1383 | { |
| 1384 | double rot, freq; |
| 1385 | if (parse_colon(arg, &rot, &freq)) |
| 1386 | fgSetDouble("/instrumentation/adf[1]/rotation-deg", rot); |
| 1387 | fgSetDouble("/instrumentation/adf[1]/frequencies/selected-khz", freq); |
| 1388 | return FG_OPTIONS_OK; |
| 1389 | } |
| 1390 | |
| 1391 | static int |
| 1392 | fgOptDME( const char *arg ) |
| 1393 | { |
| 1394 | string opt = arg; |
| 1395 | if (opt == "nav1") { |
| 1396 | fgSetInt("/instrumentation/dme/switch-position", 1); |
| 1397 | fgSetString("/instrumentation/dme/frequencies/source", |
| 1398 | "/instrumentation/nav[0]/frequencies/selected-mhz"); |
| 1399 | } else if (opt == "nav2") { |
| 1400 | fgSetInt("/instrumentation/dme/switch-position", 3); |
| 1401 | fgSetString("/instrumentation/dme/frequencies/source", |
| 1402 | "/instrumentation/nav[1]/frequencies/selected-mhz"); |
| 1403 | } else { |
| 1404 | double frequency = atof(arg); |
| 1405 | if (frequency==0.0) |
| 1406 | { |
| 1407 | SG_LOG(SG_INPUT, SG_ALERT, "Invalid DME frequency: '" << arg << "'."); |
| 1408 | return FG_OPTIONS_ERROR; |
| 1409 | } |
| 1410 | fgSetInt("/instrumentation/dme/switch-position", 2); |
| 1411 | fgSetString("/instrumentation/dme/frequencies/source", |
| 1412 | "/instrumentation/dme/frequencies/selected-mhz"); |
| 1413 | fgSetDouble("/instrumentation/dme/frequencies/selected-mhz", frequency); |
| 1414 | } |
| 1415 | return FG_OPTIONS_OK; |
| 1416 | } |
| 1417 | |
| 1418 | static int |
| 1419 | fgOptLivery( const char *arg ) |
| 1420 | { |
| 1421 | string opt = arg; |
| 1422 | string livery_path = "livery/" + opt; |
| 1423 | fgSetString("/sim/model/texture-path", livery_path.c_str() ); |
| 1424 | return FG_OPTIONS_OK; |
| 1425 | } |
| 1426 | |
| 1427 | static int |
| 1428 | fgOptScenario( const char *arg ) |
| 1429 | { |
| 1430 | SGPath path(arg); |
| 1431 | std::string name(arg); |
| 1432 | if (path.exists()) { |
| 1433 | if (path.isRelative()) { |
| 1434 | // make absolute |
| 1435 | path = simgear::Dir::current().path() / arg; |
| 1436 | } |
| 1437 | |
| 1438 | // create description node |
| 1439 | auto n = FGAIManager::registerScenarioFile(globals->get_props(), path); |
| 1440 | if (!n) { |
| 1441 | SG_LOG(SG_GENERAL, SG_WARN, "failed to read scenario file at:" << path); |
| 1442 | return FG_OPTIONS_ERROR; |
| 1443 | } |
| 1444 | |
| 1445 | // also set the /sim/ai/scenario entry so we load it on startup |
| 1446 | name = path.file_base(); |
| 1447 | } |
| 1448 | |
| 1449 | // add the 'load it' node |
| 1450 | SGPropertyNode_ptr ai_node = fgGetNode( "/sim/ai", true ); |
| 1451 | ai_node->addChild("scenario")->setStringValue(name); |
| 1452 | |
| 1453 | return FG_OPTIONS_OK; |
| 1454 | } |
| 1455 | |
| 1456 | static int |
| 1457 | fgOptAirport( const char *arg ) |
| 1458 | { |
| 1459 | fgSetString("/sim/presets/airport-id", simgear::strutils::uppercase({arg}) ); |
| 1460 | fgSetBool("/sim/presets/airport-requested", true ); |
| 1461 | return FG_OPTIONS_OK; |
| 1462 | } |
| 1463 | |
| 1464 | static int |
| 1465 | fgOptRunway( const char *arg ) |
| 1466 | { |
| 1467 | fgSetString("/sim/presets/runway", simgear::strutils::uppercase({arg}) ); |
| 1468 | fgSetBool("/sim/presets/runway-requested", true ); |
| 1469 | return FG_OPTIONS_OK; |
| 1470 | } |
| 1471 | |
| 1472 | static int |
| 1473 | fgOptCallSign(const char * arg) |
| 1474 | { |
| 1475 | int i; |
| 1476 | char callsign[11]; |
| 1477 | strncpy(callsign,arg,10); |
| 1478 | callsign[10]=0; |
| 1479 | for (i=0;callsign[i];i++) |
| 1480 | { |
| 1481 | char c = callsign[i]; |
| 1482 | if (c >= 'A' && c <= 'Z') continue; |
| 1483 | if (c >= 'a' && c <= 'z') continue; |
| 1484 | if (c >= '0' && c <= '9') continue; |
| 1485 | if (c == '-' || c == '_') continue; |
| 1486 | // convert any other illegal characters |
| 1487 | callsign[i]='-'; |
| 1488 | } |
| 1489 | fgSetString("sim/multiplay/callsign", callsign ); |
| 1490 | return FG_OPTIONS_OK; |
| 1491 | } |
| 1492 | |
| 1493 | static int |
| 1494 | fgOptIgnoreAutosave(const char* arg) |
| 1495 | { |
| 1496 | const bool param = Options::paramToBool(arg); |
| 1497 | |
| 1498 | fgSetBool("/sim/startup/ignore-autosave", param); |
| 1499 | // don't overwrite autosave on exit |
| 1500 | fgSetBool("/sim/startup/save-on-exit", !param); |
| 1501 | return FG_OPTIONS_OK; |
| 1502 | } |
| 1503 | |
| 1504 | static int |
| 1505 | fgOptFreeze(const char* arg) |
| 1506 | { |
| 1507 | const bool param = Options::paramToBool(arg); |
| 1508 | |
| 1509 | fgSetBool("/sim/freeze/master", param); |
| 1510 | fgSetBool("/sim/freeze/clock", param); |
| 1511 | return FG_OPTIONS_OK; |
| 1512 | } |
| 1513 | |
| 1514 | // Set a property for the --prop: option. Syntax: --prop:[<type>:]<name>=<value> |
| 1515 | // <type> can be "double" etc. but also only the first letter "d". |
| 1516 | // Examples: --prop:alpha=1 --prop:bool:beta=true --prop:d:gamma=0.123 |
| 1517 | static int |
| 1518 | fgOptSetProperty(const char* raw) |
| 1519 | { |
| 1520 | string arg(raw); |
| 1521 | string::size_type pos = arg.find('='); |
| 1522 | if (pos == arg.npos || pos == 0 || pos + 1 == arg.size()) |
| 1523 | return FG_OPTIONS_ERROR; |
| 1524 | |
| 1525 | string name = arg.substr(0, pos); |
| 1526 | string value = arg.substr(pos + 1); |
| 1527 | string type; |
| 1528 | pos = name.find(':'); |
| 1529 | |
| 1530 | if (pos != name.npos && pos != 0 && pos + 1 != name.size()) { |
| 1531 | type = name.substr(0, pos); |
| 1532 | name = name.substr(pos + 1); |
| 1533 | } |
| 1534 | SGPropertyNode *n = fgGetNode(name.c_str(), true); |
| 1535 | |
| 1536 | bool writable = n->getAttribute(SGPropertyNode::WRITE); |
| 1537 | if (!writable) |
| 1538 | n->setAttribute(SGPropertyNode::WRITE, true); |
| 1539 | |
| 1540 | bool ret = false; |
| 1541 | if (type.empty()) |
| 1542 | ret = n->setUnspecifiedValue(value.c_str()); |
| 1543 | else if (type == "s" || type == "string") |
| 1544 | ret = n->setStringValue(value.c_str()); |
| 1545 | else if (type == "d" || type == "double") |
| 1546 | ret = n->setDoubleValue(strtod(value.c_str(), 0)); |
| 1547 | else if (type == "f" || type == "float") |
| 1548 | ret = n->setFloatValue(atof(value.c_str())); |
| 1549 | else if (type == "l" || type == "long") |
| 1550 | ret = n->setLongValue(strtol(value.c_str(), 0, 0)); |
| 1551 | else if (type == "i" || type == "int") |
| 1552 | ret = n->setIntValue(atoi(value.c_str())); |
| 1553 | else if (type == "b" || type == "bool") |
| 1554 | ret = n->setBoolValue(value == "true" || atoi(value.c_str()) != 0); |
| 1555 | |
| 1556 | if (!writable) |
| 1557 | n->setAttribute(SGPropertyNode::WRITE, false); |
| 1558 | return ret ? FG_OPTIONS_OK : FG_OPTIONS_ERROR; |
| 1559 | } |
| 1560 | |
| 1561 | /* If <url> is a URL, return suitable name for downloaded file. */ |
| 1562 | static std::string urlToLocalPath(const char* url) |
| 1563 | { |
| 1564 | bool http = simgear::strutils::starts_with(url, "http://"); |
| 1565 | bool https = simgear::strutils::starts_with(url, "https://"); |
| 1566 | if (!http && !https) { |
| 1567 | return ""; |
| 1568 | } |
| 1569 | // e.g. http://fg.com/foo/bar/wibble.fgtape |
| 1570 | const char* s2 = (http) ? url+7 : url+8; // fg.com/foo/bar/wibble.fgtape |
| 1571 | const char* s3 = strchr(s2, '/'); // /foo/bar/wibble.fgtape |
| 1572 | const char* s4 = (s3) ? strrchr(s3, '/') : NULL; // /wibble.fgtape |
| 1573 | std::string path = "url_"; |
| 1574 | if (s3) path += std::string(s2, s3-s2); // url_fg.com |
| 1575 | path += '_'; // url_fg.com_ |
| 1576 | if (s3 && s4 > s3) { |
| 1577 | path += simgear::strutils::md5(s3, s4-s3).substr(0, 8); |
| 1578 | path += '_'; // url_fg.com_12345678_ |
| 1579 | } |
| 1580 | if (s4) path += (s4+1); // url_fg.com_12345678_wibble.fgtape |
| 1581 | if (!simgear::strutils::ends_with(path, ".fgtape")) path += ".fgtape"; |
| 1582 | std::string dir = fgGetString("/sim/replay/tape-directory"); |
| 1583 | if (dir != "") { |
| 1584 | SGPath path2 = dir; |
| 1585 | path2.append(path); |
| 1586 | path = path2.str(); |
| 1587 | } |
| 1588 | return path; |
| 1589 | } |
| 1590 | |
| 1591 | // When loading a Continuous recording at startup, we need to override the |
| 1592 | // aircraft and airport. Unfortunately we can't simply set /sim/aircraft |
| 1593 | // because there may be --aircraft options later on in the command line. Also |
| 1594 | // fgMainInit() ends up calling Options::initAircraft() after we have processed |
| 1595 | // all options, and Options::initAircraft() seems to look directly at the |
| 1596 | // options again, instead of using /sim/aircraft. |
| 1597 | // |
| 1598 | // So we store any aircraft/airport override here, so that |
| 1599 | // Options::initAircraft() can use them if they are set, instead of going back |
| 1600 | // to any user-specified aircraft. |
| 1601 | // |
| 1602 | static std::string g_load_tape_aircraft; |
| 1603 | static std::string g_load_tape_airport; |
| 1604 | |
| 1605 | static int |
| 1606 | fgOptLoadTape(const char* arg) |
| 1607 | { |
| 1608 | // load a flight recorder tape but wait until the fdm is initialized. |
| 1609 | // |
| 1610 | struct DelayedTapeLoader : SGPropertyChangeListener { |
| 1611 | |
| 1612 | DelayedTapeLoader( const char * tape, simgear::HTTP::FileRequestRef filerequest) : |
| 1613 | _tape(SGPath::fromUtf8(tape)), |
| 1614 | _filerequest(filerequest) |
| 1615 | { |
| 1616 | fgGetNode("/sim/signals/fdm-initialized", true)->addChangeListener( this ); |
| 1617 | } |
| 1618 | |
| 1619 | virtual ~ DelayedTapeLoader() {} |
| 1620 | |
| 1621 | void valueChanged(SGPropertyNode* node) override |
| 1622 | { |
| 1623 | if (!fgGetBool("/sim/signals/fdm-initialized")) { |
| 1624 | return; |
| 1625 | } |
| 1626 | fgGetNode("/sim/signals/fdm-initialized", true)->removeChangeListener( this ); |
| 1627 | |
| 1628 | // tell the replay subsystem to load the tape |
| 1629 | auto replay = globals->get_subsystem<FGReplay>(); |
| 1630 | assert(replay); |
| 1631 | SGPropertyNode_ptr arg = new SGPropertyNode(); |
| 1632 | arg->setStringValue("tape", _tape.utf8Str() ); |
| 1633 | arg->setBoolValue( "same-aircraft", 0 ); |
| 1634 | if (!replay->loadTape( |
| 1635 | _tape, |
| 1636 | false /*preview*/, |
| 1637 | fgGetBool("/sim/startup/load-tape/create-video"), |
| 1638 | fgGetDouble("/sim/startup/load-tape/fixed-dt", 0), |
| 1639 | *arg, |
| 1640 | _filerequest |
| 1641 | )) |
| 1642 | { |
| 1643 | // Force shutdown if we can't load tape specified on command-line. |
| 1644 | SG_LOG(SG_GENERAL, SG_POPUP, "Exiting because unable to load fgtape: " << _tape.str()); |
| 1645 | flightgear::modalMessageBox("Exiting because unable to load fgtape", _tape.str(), ""); |
| 1646 | fgOSExit(1); |
| 1647 | } |
| 1648 | delete this; // commence suicide |
| 1649 | } |
| 1650 | private: |
| 1651 | SGPath _tape; |
| 1652 | simgear::HTTP::FileRequestRef _filerequest; |
| 1653 | }; |
| 1654 | |
| 1655 | SGPropertyNode_ptr properties(new SGPropertyNode); |
| 1656 | simgear::HTTP::FileRequestRef filerequest; |
| 1657 | SGTimeStamp timeout; |
| 1658 | |
| 1659 | std::string path = urlToLocalPath(arg); |
| 1660 | if (path.empty()) { |
| 1661 | // <arg> is a local file. |
| 1662 | // |
| 1663 | // Load the recording's header if it is a Continuous recording. |
| 1664 | // |
| 1665 | path = FGReplay::makeTapePath(arg); |
| 1666 | (void) FGReplay::loadContinuousHeader(path.c_str(), nullptr /*in*/, properties); |
| 1667 | } else { |
| 1668 | // <arg> is a URL. Start download. |
| 1669 | // |
| 1670 | // Load the recording's header if it is a Continuous recording. |
| 1671 | // |
| 1672 | // This is a little messy - we need to create a FGHTTPClient subsystem |
| 1673 | // in order to do the download, and we call its update() method |
| 1674 | // directly in order to download at least the header. |
| 1675 | // |
| 1676 | const char* url = arg; |
| 1677 | FGHTTPClient* http = FGHTTPClient::getOrCreate(); |
| 1678 | SG_LOG(SG_GENERAL, SG_MANDATORY_INFO, "Replaying url " << url << " using local path: " << path); |
| 1679 | filerequest.reset(new simgear::HTTP::FileRequest(url, path, true /*append*/)); |
| 1680 | filerequest->setAcceptEncoding(""); // "" means request any supported compression. |
| 1681 | |
| 1682 | long max_download_speed = fgGetLong("/sim/replay/download-max-bytes-per-sec"); |
| 1683 | if (max_download_speed != 0) { |
| 1684 | // Can be useful to limite download speed for testing background |
| 1685 | // download. |
| 1686 | // |
| 1687 | SG_LOG(SG_GENERAL, SG_MANDATORY_INFO, "Limiting download speed" |
| 1688 | << " /sim/replay/download-max-bytes-per-sec=" << max_download_speed); |
| 1689 | filerequest->setMaxBytesPerSec(max_download_speed); |
| 1690 | } |
| 1691 | http->client()->makeRequest(filerequest); |
| 1692 | SG_LOG(SG_GENERAL, SG_DEBUG, "" |
| 1693 | << " filerequest->responseCode()=" << filerequest->responseCode() |
| 1694 | << " filerequest->responseReason()=" << filerequest->responseReason() |
| 1695 | ); |
| 1696 | |
| 1697 | // Load recording header, looping so that we wait for the initial |
| 1698 | // portion of the recording to be downloaded. We give up after a fixed |
| 1699 | // timeout. |
| 1700 | // |
| 1701 | timeout.stamp(); |
| 1702 | for(;;) { |
| 1703 | // Run http client's update() to download any pending data. |
| 1704 | http->update(0); |
| 1705 | |
| 1706 | // Try to load properties from recording header. |
| 1707 | int e = FGReplay::loadContinuousHeader(path, nullptr /*in*/, properties); |
| 1708 | if (e == 0) { |
| 1709 | // Success. We leave <filerequest> active - it will carry |
| 1710 | // on downloading when the main update loop gets going |
| 1711 | // later. Hopefully the delay before that happens will not |
| 1712 | // cause a server timeout. |
| 1713 | // |
| 1714 | break; |
| 1715 | } |
| 1716 | if (e == -1) { |
| 1717 | SG_LOG(SG_GENERAL, SG_POPUP, "Not a Continuous recording: url=" << url << " local filename=" << path); |
| 1718 | // Replay from URL only works with Continuous recordings. |
| 1719 | return FG_OPTIONS_EXIT; |
| 1720 | } |
| 1721 | |
| 1722 | // If we get here, need to download some more. |
| 1723 | if (timeout.elapsedMSec() > (30 * 1000)) { |
| 1724 | SG_LOG(SG_GENERAL, SG_POPUP, "Timeout while reading downloaded recording from " << url << ". local path=" << path); |
| 1725 | return FG_OPTIONS_EXIT; |
| 1726 | } |
| 1727 | SGTimeStamp::sleepForMSec(1000); |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | // Set aircraft from recording header if we loaded it above; this has to |
| 1732 | // happen now, before the FDM is initialised. Also set the airport; we |
| 1733 | // don't actually have to do this because the replay doesn't need terrain |
| 1734 | // to work, but we might as well load the correct terrain. |
| 1735 | // |
| 1736 | std::string aircraft = properties->getStringValue("meta/aircraft-type"); |
| 1737 | std::string airport = properties->getStringValue("meta/closest-airport-id"); |
| 1738 | SG_LOG(SG_GENERAL, SG_MANDATORY_INFO, "From recording header: aircraft=" << aircraft << " airport=" << airport); |
| 1739 | // Override aircraft and airport settings to match what is in the |
| 1740 | // recording. |
| 1741 | // |
| 1742 | g_load_tape_aircraft = aircraft; |
| 1743 | g_load_tape_airport = airport; |
| 1744 | |
| 1745 | // Arrange to load the recording after FDM has initialised. |
| 1746 | new DelayedTapeLoader(path.c_str(), filerequest); |
| 1747 | |
| 1748 | return FG_OPTIONS_OK; |
| 1749 | } |
| 1750 | |
| 1751 | static int fgOptGUI(const char* arg) |
| 1752 | { |
| 1753 | const bool param = Options::paramToBool(arg); |
| 1754 | |
| 1755 | // Reverse logic, headless is enabled when --gui is false |
| 1756 | globals->set_headless(!param); |
| 1757 | return FG_OPTIONS_OK; |
| 1758 | } |
| 1759 | |
| 1760 | static int fgOptHoldShort(const char* arg) |
| 1761 | { |
| 1762 | const bool param = Options::paramToBool(arg); |
| 1763 | |
| 1764 | // Reverse logic, this property set to true disables hold short |
| 1765 | fgSetBool("/sim/presets/mp-hold-short-override", !param); |
| 1766 | return FG_OPTIONS_OK; |
| 1767 | } |
| 1768 | |
| 1769 | static int fgOptNoTrim(const char* arg) |
| 1770 | { |
| 1771 | const bool param = Options::paramToBool(arg); |
| 1772 | |
| 1773 | // Reverse logic, param = true means NO trim |
| 1774 | fgSetBool("/sim/presets/trim", !param); |
| 1775 | return FG_OPTIONS_OK; |
| 1776 | } |
| 1777 | |
| 1778 | static int fgOptInAir(const char* arg) |
| 1779 | { |
| 1780 | const bool param = Options::paramToBool(arg); |
| 1781 | |
| 1782 | // Reverse logic, param = true means on ground = false |
| 1783 | fgSetBool("/sim/presets/onground", !param); |
| 1784 | return FG_OPTIONS_OK; |
| 1785 | } |
| 1786 | |
| 1787 | /* |
| 1788 | option param_type type property b_param s_param func |
| 1789 | |
| 1790 | where: |
| 1791 | option : name of the option |
| 1792 | param_type: ParamType::NONE - option has not parameter: --option |
| 1793 | ParamType::VAL_BOOL - option accept only boolean params true/false/1/0/yes/no |
| 1794 | or no parameter (default true): --option=true |
| 1795 | ParamType::REGULAR - option requires a parameter but it is none of the |
| 1796 | above cases: --option=value |
| 1797 | type : OptionType::OPT_BOOL - property is a boolean |
| 1798 | OptionType::OPT_STRING - property is a string |
| 1799 | OptionType::OPT_DOUBLE - property is a double |
| 1800 | OptionType::OPT_INT - property is an integer |
| 1801 | OptionType::OPT_CHANNEL - name of option is the name of a channel |
| 1802 | OptionType::OPT_FUNC - the option trigger a function |
| 1803 | property : |
| 1804 | b_param : if type==OptionType::OPT_BOOL, |
| 1805 | value set to the property (param_type is ParamType::NONE for boolean) |
| 1806 | s_param : if type==OptionType::OPT_STRING, |
| 1807 | value set to the property if param_type is ParamType::NONE |
| 1808 | func : function called if type==OptionType::OPT_FUNC. if param_type is ParamType::VAL_BOOL |
| 1809 | or ParamType::REGULAR, the value is passed to the function as a string, |
| 1810 | otherwise, s_param is passed. |
| 1811 | |
| 1812 | For OptionType::OPT_DOUBLE and OptionType::OPT_INT, the parameter value is converted into a |
| 1813 | double or an integer and set to the property. |
| 1814 | |
| 1815 | For OptionType::OPT_CHANNEL, add_channel is called with the parameter value as the |
| 1816 | argument. |
| 1817 | */ |
| 1818 | |
| 1819 | enum ParamType { NONE = 0, VAL_BOOL, REGULAR }; |
| 1820 | enum OptionType { OPT_BOOL = 0, OPT_STRING, OPT_DOUBLE, OPT_INT, OPT_CHANNEL, OPT_FUNC, OPT_IGNORE }; |
| 1821 | const int OPTION_MULTI = 1 << 17; |
| 1822 | |
| 1823 | struct OptionDesc { |
| 1824 | const char *option; |
| 1825 | int param_type; |
| 1826 | int type; |
| 1827 | const char *property; |
| 1828 | bool b_param; |
| 1829 | const char *s_param; |
| 1830 | int (*func)( const char * ); |
| 1831 | }; |
| 1832 | |
| 1833 | const std::initializer_list<OptionDesc> fgOptionArray = { |
| 1834 | // clang-format off |
| 1835 | {"language", ParamType::REGULAR, OptionType::OPT_IGNORE, "", false, "", 0 }, |
| 1836 | {"console", ParamType::VAL_BOOL, OptionType::OPT_FUNC, "", false, "true", fgOptConsole }, |
| 1837 | {"compositor", ParamType::REGULAR, OptionType::OPT_STRING, "/sim/rendering/default-compositor", false, "", 0 }, |
| 1838 | {"metar", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptMetar }, |
| 1839 | {"browser-app", ParamType::REGULAR, OptionType::OPT_STRING, "/sim/startup/browser-app", false, "", 0 }, |
| 1840 | {"sound-device", ParamType::REGULAR, OptionType::OPT_STRING, "/sim/sound/device-name", false, "", 0 }, |
| 1841 | {"airport", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptAirport }, |
| 1842 | {"runway", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptRunway }, |
| 1843 | {"vor", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptVOR }, |
| 1844 | {"vor-frequency", ParamType::REGULAR, OptionType::OPT_DOUBLE, "/sim/presets/vor-freq", false, "", fgOptVOR }, |
| 1845 | {"ndb", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptNDB }, |
| 1846 | {"ndb-frequency", ParamType::REGULAR, OptionType::OPT_DOUBLE, "/sim/presets/ndb-freq", false, "", fgOptVOR }, |
| 1847 | {"carrier", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptCarrier }, |
| 1848 | {"carrier-position", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptCarrierPos }, |
| 1849 | {"fix", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptFIX }, |
| 1850 | {"tacan", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptTACAN }, |
| 1851 | {"offset-distance", ParamType::REGULAR, OptionType::OPT_DOUBLE, "/sim/presets/offset-distance-nm", false, "", 0 }, |
| 1852 | {"offset-azimuth", ParamType::REGULAR, OptionType::OPT_DOUBLE, "/sim/presets/offset-azimuth-deg", false, "", 0 }, |
| 1853 | {"lon", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptLon }, |
| 1854 | {"lat", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptLat }, |
| 1855 | {"altitude", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptAltitude }, |
| 1856 | {"uBody", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptUBody }, |
| 1857 | {"vBody", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptVBody }, |
| 1858 | {"wBody", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptWBody }, |
| 1859 | {"vNorth", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptVNorth }, |
| 1860 | {"vEast", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptVEast }, |
| 1861 | {"vDown", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptVDown }, |
| 1862 | {"vc", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptVc }, |
| 1863 | {"mach", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptMach }, |
| 1864 | {"heading", ParamType::REGULAR, OptionType::OPT_DOUBLE, "/sim/presets/heading-deg", false, "", 0 }, |
| 1865 | {"roll", ParamType::REGULAR, OptionType::OPT_DOUBLE, "/sim/presets/roll-deg", false, "", 0 }, |
| 1866 | {"pitch", ParamType::REGULAR, OptionType::OPT_DOUBLE, "/sim/presets/pitch-deg", false, "", 0 }, |
| 1867 | {"glideslope", ParamType::REGULAR, OptionType::OPT_DOUBLE, "/sim/presets/glideslope-deg", false, "", 0 }, |
| 1868 | {"roc", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptRoc }, |
| 1869 | {"fg-root", ParamType::REGULAR, OptionType::OPT_IGNORE, "", false, "", 0 }, |
| 1870 | {"fg-scenery", ParamType::REGULAR, OptionType::OPT_FUNC | OPTION_MULTI, "", false, "", fgOptFgScenery }, |
| 1871 | {"terrain-engine", ParamType::REGULAR, OptionType::OPT_STRING, "/sim/scenery/engine", false, "tilecache", 0 }, |
| 1872 | {"lod-levels", ParamType::REGULAR, OptionType::OPT_STRING, "/sim/scenery/lod-levels", false, "1 3 5 7", 0 }, |
| 1873 | {"lod-res", ParamType::REGULAR, OptionType::OPT_STRING, "/sim/scenery/lod-res", false, "1", 0 }, |
| 1874 | {"lod-texturing", ParamType::REGULAR, OptionType::OPT_STRING, "/sim/scenery/lod-texturing", false, "bluemarble", 0 }, |
| 1875 | {"lod-range-mult", ParamType::REGULAR, OptionType::OPT_STRING, "/sim/scenery/lod-range-mult", false, "2", 0 }, |
| 1876 | {"fg-aircraft", ParamType::REGULAR, OptionType::OPT_IGNORE | OPTION_MULTI, "", false, "", 0 }, |
| 1877 | {"fdm", ParamType::REGULAR, OptionType::OPT_STRING, "/sim/flight-model", false, "", 0 }, |
| 1878 | {"aero", ParamType::REGULAR, OptionType::OPT_STRING, "/sim/aero", false, "", 0 }, |
| 1879 | {"aircraft-dir", ParamType::REGULAR, OptionType::OPT_IGNORE, "", false, "", 0 }, |
| 1880 | {"state", ParamType::REGULAR, OptionType::OPT_IGNORE, "", false, "", 0 }, |
| 1881 | {"model-hz", ParamType::REGULAR, OptionType::OPT_INT, "/sim/model-hz", false, "", 0 }, |
| 1882 | {"max-fps", ParamType::REGULAR, OptionType::OPT_DOUBLE, "/sim/frame-rate-throttle-hz", false, "", 0 }, |
| 1883 | {"speed", ParamType::REGULAR, OptionType::OPT_DOUBLE, "/sim/speed-up", false, "", 0 }, |
| 1884 | {"trim", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/presets/trim", true, "", 0 }, |
| 1885 | {"notrim", ParamType::VAL_BOOL, OptionType::OPT_FUNC, "", false, "true", fgOptNoTrim }, |
| 1886 | {"on-ground", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/presets/onground", true, "", 0 }, |
| 1887 | {"in-air", ParamType::VAL_BOOL, OptionType::OPT_FUNC, "", false, "true", fgOptInAir }, |
| 1888 | {"fog-disable", ParamType::NONE, OptionType::OPT_STRING, "/sim/rendering/fog", false, "disabled", 0 }, |
| 1889 | {"fog-fastest", ParamType::NONE, OptionType::OPT_STRING, "/sim/rendering/fog", false, "fastest", 0 }, |
| 1890 | {"fog-nicest", ParamType::NONE, OptionType::OPT_STRING, "/sim/rendering/fog", false, "nicest", 0 }, |
| 1891 | {"fov", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptFov }, |
| 1892 | {"aspect-ratio-multiplier", ParamType::REGULAR, OptionType::OPT_DOUBLE, "/sim/current-view/aspect-ratio-multiplier", false, "", 0 }, |
| 1893 | {"shading-flat", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/shading", false, "", 0 }, |
| 1894 | {"shading-smooth", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/shading", true, "", 0 }, |
| 1895 | {"texture-filtering", ParamType::NONE, OptionType::OPT_INT, "/sim/rendering/filtering", 1, "", 0 }, |
| 1896 | {"materials-file", ParamType::REGULAR, OptionType::OPT_STRING, "/sim/rendering/materials-file", false, "", 0 }, |
| 1897 | {"terrasync-dir", ParamType::REGULAR, OptionType::OPT_IGNORE, "", false, "", 0 }, |
| 1898 | {"download-dir", ParamType::REGULAR, OptionType::OPT_IGNORE, "", false, "", 0 }, |
| 1899 | {"texture-cache-dir", ParamType::REGULAR, OptionType::OPT_IGNORE, "", false, "", 0 }, |
| 1900 | {"allow-nasal-read", ParamType::REGULAR, OptionType::OPT_FUNC | OPTION_MULTI, "", false, "", fgOptAllowNasalRead }, |
| 1901 | {"geometry", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptGeometry }, |
| 1902 | {"bpp", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptBpp }, |
| 1903 | {"units-feet", ParamType::NONE, OptionType::OPT_STRING, "/sim/startup/units", false, "feet", 0 }, |
| 1904 | {"units-meters", ParamType::NONE, OptionType::OPT_STRING, "/sim/startup/units", false, "meters", 0 }, |
| 1905 | {"timeofday", ParamType::REGULAR, OptionType::OPT_STRING, "/sim/startup/time-offset-type", false, "noon", 0 }, |
| 1906 | {"time-offset", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptTimeOffset }, |
| 1907 | {"time-match-real", ParamType::NONE, OptionType::OPT_STRING, "/sim/startup/time-offset-type", false, "system-offset", 0 }, |
| 1908 | {"time-match-local", ParamType::NONE, OptionType::OPT_STRING, "/sim/startup/time-offset-type", false, "latitude-offset", 0 }, |
| 1909 | {"start-date-sys", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptStartDateSys }, |
| 1910 | {"start-date-lat", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptStartDateLat }, |
| 1911 | {"start-date-gmt", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptStartDateGmt }, |
| 1912 | {"hud-tris", ParamType::NONE, OptionType::OPT_STRING, "/sim/hud/frame-stat-type", false, "tris", 0 }, |
| 1913 | {"hud-culled", ParamType::NONE, OptionType::OPT_STRING, "/sim/hud/frame-stat-type", false, "culled", 0 }, |
| 1914 | {"atcsim", ParamType::REGULAR, OptionType::OPT_CHANNEL, "", false, "dummy", 0 }, |
| 1915 | {"atlas", ParamType::REGULAR, OptionType::OPT_CHANNEL | OPTION_MULTI, "", false, "", 0 }, |
| 1916 | {"httpd", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptHttpd }, |
| 1917 | {"jpg-httpd", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptJpgHttpd }, |
| 1918 | {"native", ParamType::REGULAR, OptionType::OPT_CHANNEL | OPTION_MULTI, "", false, "", 0 }, |
| 1919 | {"native-ctrls", ParamType::REGULAR, OptionType::OPT_CHANNEL | OPTION_MULTI, "", false, "", 0 }, |
| 1920 | {"native-fdm", ParamType::REGULAR, OptionType::OPT_CHANNEL | OPTION_MULTI, "", false, "", 0 }, |
| 1921 | {"native-gui", ParamType::REGULAR, OptionType::OPT_CHANNEL | OPTION_MULTI, "", false, "", 0 }, |
| 1922 | {"dds-props", ParamType::REGULAR, OptionType::OPT_CHANNEL | OPTION_MULTI, "", false, "", 0 }, |
| 1923 | {"opengc", ParamType::REGULAR, OptionType::OPT_CHANNEL | OPTION_MULTI, "", false, "", 0 }, |
| 1924 | {"AV400", ParamType::REGULAR, OptionType::OPT_CHANNEL | OPTION_MULTI, "", false, "", 0 }, |
| 1925 | {"AV400Sim", ParamType::REGULAR, OptionType::OPT_CHANNEL | OPTION_MULTI, "", false, "", 0 }, |
| 1926 | {"AV400WSimA", ParamType::REGULAR, OptionType::OPT_CHANNEL | OPTION_MULTI, "", false, "", 0 }, |
| 1927 | {"AV400WSimB", ParamType::REGULAR, OptionType::OPT_CHANNEL | OPTION_MULTI, "", false, "", 0 }, |
| 1928 | {"flarm", ParamType::REGULAR, OptionType::OPT_CHANNEL | OPTION_MULTI, "", false, "", 0 }, |
| 1929 | {"garmin", ParamType::REGULAR, OptionType::OPT_CHANNEL | OPTION_MULTI, "", false, "", 0 }, |
| 1930 | {"igc", ParamType::REGULAR, OptionType::OPT_CHANNEL | OPTION_MULTI, "", false, "", 0 }, |
| 1931 | {"nmea", ParamType::REGULAR, OptionType::OPT_CHANNEL | OPTION_MULTI, "", false, "", 0 }, |
| 1932 | {"generic", ParamType::REGULAR, OptionType::OPT_CHANNEL | OPTION_MULTI, "", false, "", 0 }, |
| 1933 | {"props", ParamType::REGULAR, OptionType::OPT_CHANNEL | OPTION_MULTI, "", false, "", 0 }, |
| 1934 | {"telnet", ParamType::REGULAR, OptionType::OPT_CHANNEL | OPTION_MULTI, "", false, "", 0 }, |
| 1935 | {"pve", ParamType::REGULAR, OptionType::OPT_CHANNEL, "", false, "", 0 }, |
| 1936 | {"ray", ParamType::REGULAR, OptionType::OPT_CHANNEL, "", false, "", 0 }, |
| 1937 | {"rul", ParamType::REGULAR, OptionType::OPT_CHANNEL, "", false, "", 0 }, |
| 1938 | {"joyclient", ParamType::REGULAR, OptionType::OPT_CHANNEL, "", false, "", 0 }, |
| 1939 | {"jsclient", ParamType::REGULAR, OptionType::OPT_CHANNEL, "", false, "", 0 }, |
| 1940 | {"proxy", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgSetupProxy }, |
| 1941 | {"callsign", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptCallSign}, |
| 1942 | {"multiplay", ParamType::REGULAR, OptionType::OPT_CHANNEL | OPTION_MULTI, "", false, "", 0 }, |
| 1943 | #if FG_HAVE_HLA |
| 1944 | {"hla", ParamType::REGULAR, OptionType::OPT_CHANNEL, "", false, "", 0 }, |
| 1945 | {"hla-local", ParamType::REGULAR, OptionType::OPT_CHANNEL, "", false, "", 0 }, |
| 1946 | #endif |
| 1947 | {"trace-read", ParamType::REGULAR, OptionType::OPT_FUNC | OPTION_MULTI, "", false, "", fgOptTraceRead }, |
| 1948 | {"trace-write", ParamType::REGULAR, OptionType::OPT_FUNC | OPTION_MULTI, "", false, "", fgOptTraceWrite }, |
| 1949 | {"log-level", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptLogLevel }, |
| 1950 | {"log-class", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptLogClasses }, |
| 1951 | {"log-dir", ParamType::REGULAR, OptionType::OPT_FUNC | OPTION_MULTI, "", false, "", fgOptLogDir }, |
| 1952 | {"view-offset", ParamType::REGULAR, OptionType::OPT_FUNC | OPTION_MULTI, "", false, "", fgOptViewOffset }, |
| 1953 | {"visibility", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptVisibilityMeters }, |
| 1954 | {"visibility-miles", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptVisibilityMiles }, |
| 1955 | {"random-wind", ParamType::NONE, OptionType::OPT_FUNC, "", false, "", fgOptRandomWind }, |
| 1956 | {"wind", ParamType::REGULAR, OptionType::OPT_FUNC | OPTION_MULTI, "", false, "", fgOptWind }, |
| 1957 | {"turbulence", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptTurbulence }, |
| 1958 | {"ceiling", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptCeiling }, |
| 1959 | {"wp", ParamType::REGULAR, OptionType::OPT_FUNC | OPTION_MULTI, "", false, "", fgOptWp }, |
| 1960 | {"flight-plan", ParamType::REGULAR, OptionType::OPT_STRING, "/autopilot/route-manager/file-path", false, "", NULL }, |
| 1961 | {"config", ParamType::REGULAR, OptionType::OPT_IGNORE | OPTION_MULTI, "", false, "", 0 }, |
| 1962 | {"addon", ParamType::REGULAR, OptionType::OPT_FUNC | OPTION_MULTI, "", false, "", fgOptAddon }, |
| 1963 | {"data", ParamType::REGULAR, OptionType::OPT_FUNC | OPTION_MULTI, "", false, "", fgOptAdditionalDataDir }, |
| 1964 | {"aircraft", ParamType::REGULAR, OptionType::OPT_STRING, "/sim/aircraft", false, "", 0 }, |
| 1965 | {"vehicle", ParamType::REGULAR, OptionType::OPT_STRING, "/sim/aircraft", false, "", 0 }, |
| 1966 | {"failure", ParamType::REGULAR, OptionType::OPT_FUNC | OPTION_MULTI, "", false, "", fgOptFailure }, |
| 1967 | {"com1", ParamType::REGULAR, OptionType::OPT_DOUBLE, "/instrumentation/comm[0]/frequencies/selected-mhz", false, "", 0 }, |
| 1968 | {"com2", ParamType::REGULAR, OptionType::OPT_DOUBLE, "/instrumentation/comm[1]/frequencies/selected-mhz", false, "", 0 }, |
| 1969 | {"nav1", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptNAV1 }, |
| 1970 | {"nav2", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptNAV2 }, |
| 1971 | {"adf", /*legacy*/ ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptADF }, |
| 1972 | {"adf1", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptADF1 }, |
| 1973 | {"adf2", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptADF2 }, |
| 1974 | {"dme", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptDME }, |
| 1975 | {"min-status", ParamType::REGULAR, OptionType::OPT_STRING, "/sim/aircraft-min-status", false, "all", 0 }, |
| 1976 | {"livery", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptLivery }, |
| 1977 | {"ai-scenario", ParamType::REGULAR, OptionType::OPT_FUNC | OPTION_MULTI, "", false, "", fgOptScenario }, |
| 1978 | {"parking-id", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptParkpos }, |
| 1979 | {"parkpos", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptParkpos }, |
| 1980 | {"version", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "", true, "", nullptr }, |
| 1981 | {"info", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "", true, "", nullptr }, |
| 1982 | {"json-report", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "", true, "", nullptr }, |
| 1983 | {"fgviewer", ParamType::NONE, OptionType::OPT_IGNORE, "", false, "", 0}, |
| 1984 | {"no-default-config", ParamType::VAL_BOOL, OptionType::OPT_IGNORE, "", false, "", 0}, |
| 1985 | {"prop", ParamType::REGULAR, OptionType::OPT_FUNC | OPTION_MULTI, "", false, "", fgOptSetProperty}, |
| 1986 | {"load-tape", ParamType::REGULAR, OptionType::OPT_FUNC, "", false, "", fgOptLoadTape }, |
| 1987 | {"load-tape-fixed-dt", ParamType::REGULAR, OptionType::OPT_DOUBLE, "/sim/startup/load-tape/fixed-dt", false, "", nullptr }, |
| 1988 | {"jsbsim-output-directive-file", ParamType::REGULAR, OptionType::OPT_STRING, "/sim/jsbsim/output-directive-file", false, "", nullptr }, |
| 1989 | {"graphics-preset", ParamType::REGULAR, OptionType::OPT_STRING, "/sim/rendering/preset", false, "", nullptr}, |
| 1990 | {"show-aircraft", ParamType::VAL_BOOL, OptionType::OPT_IGNORE, "", true, "", nullptr }, |
| 1991 | {"show-sound-devices", ParamType::VAL_BOOL, OptionType::OPT_IGNORE, "", true, "", nullptr }, |
| 1992 | |
| 1993 | // Enable/disable options that can be used in many ways, |
| 1994 | // with enable/disable prefixes as well as without, but with a value of 1/0 or true/false or yes/no. |
| 1995 | // Examples of use: |
| 1996 | // --enable-fullscreen (enable) |
| 1997 | // --disable-fullscreen (disable) |
| 1998 | // --fullscreen (enable) |
| 1999 | // --fullscreen=true (enable) |
| 2000 | // --fullscreen=false (disable) |
| 2001 | // --fullscreen=1 (enable) |
| 2002 | // --fullscreen=0 (disable) |
| 2003 | // --fullscreen=yes (enable) |
| 2004 | // --fullscreen=no (disable) |
| 2005 | // --fullscreen true (enable) |
| 2006 | // --fullscreen false (disable) |
| 2007 | // --fullscreen 1 (enable) |
| 2008 | // --fullscreen 0 (disable) |
| 2009 | // --fullscreen yes (enable) |
| 2010 | // --fullscreen no (disable) |
| 2011 | |
| 2012 | {"ai-models", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/ai/enabled", true, "", nullptr }, |
| 2013 | {"disable-ai-models", ParamType::NONE, OptionType::OPT_BOOL, "/sim/ai/enabled", false, "", nullptr }, |
| 2014 | {"enable-ai-models", ParamType::NONE, OptionType::OPT_BOOL, "/sim/ai/enabled", true, "", nullptr }, |
| 2015 | {"ai-traffic", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/traffic-manager/enabled", true, "", nullptr }, |
| 2016 | {"disable-ai-traffic", ParamType::NONE, OptionType::OPT_BOOL, "/sim/traffic-manager/enabled", false, "", nullptr }, |
| 2017 | {"enable-ai-traffic", ParamType::NONE, OptionType::OPT_BOOL, "/sim/traffic-manager/enabled", true, "", nullptr }, |
| 2018 | {"allow-nasal-from-sockets", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "", true, "", nullptr }, |
| 2019 | {"disable-allow-nasal-from-sockets", ParamType::NONE, OptionType::OPT_BOOL, "", false, "", nullptr }, |
| 2020 | {"enable-allow-nasal-from-sockets", ParamType::NONE, OptionType::OPT_BOOL, "", true, "", nullptr }, |
| 2021 | {"anti-alias-hud", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/hud/color/antialiased", true, "", nullptr }, |
| 2022 | {"disable-anti-alias-hud", ParamType::NONE, OptionType::OPT_BOOL, "/sim/hud/color/antialiased", false, "", nullptr }, |
| 2023 | {"enable-anti-alias-hud", ParamType::NONE, OptionType::OPT_BOOL, "/sim/hud/color/antialiased", true, "", nullptr }, |
| 2024 | {"auto-coordination", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/controls/flight/auto-coordination", true, "", nullptr }, |
| 2025 | {"disable-auto-coordination", ParamType::NONE, OptionType::OPT_BOOL, "/controls/flight/auto-coordination", false, "", nullptr }, |
| 2026 | {"enable-auto-coordination", ParamType::NONE, OptionType::OPT_BOOL, "/controls/flight/auto-coordination", true, "", nullptr }, |
| 2027 | {"clock-freeze", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/freeze/clock", true, "", nullptr }, |
| 2028 | {"disable-clock-freeze", ParamType::NONE, OptionType::OPT_BOOL, "/sim/freeze/clock", false, "", nullptr }, |
| 2029 | {"enable-clock-freeze", ParamType::NONE, OptionType::OPT_BOOL, "/sim/freeze/clock", true, "", nullptr }, |
| 2030 | {"clouds", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/environment/clouds/status", true, "", nullptr }, |
| 2031 | {"disable-clouds", ParamType::NONE, OptionType::OPT_BOOL, "/environment/clouds/status", false, "", nullptr }, |
| 2032 | {"enable-clouds", ParamType::NONE, OptionType::OPT_BOOL, "/environment/clouds/status", true, "", nullptr }, |
| 2033 | {"clouds3d", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/rendering/clouds3d-enable", true, "", nullptr }, |
| 2034 | {"disable-clouds3d", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/clouds3d-enable", false, "", nullptr }, |
| 2035 | {"enable-clouds3d", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/clouds3d-enable", true, "", nullptr }, |
| 2036 | {"composite-viewer", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/rendering/composite-viewer-enabled", true, "", nullptr}, |
| 2037 | {"disable-composite-viewer", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/composite-viewer-enabled", false, "", nullptr}, |
| 2038 | {"enable-composite-viewer", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/composite-viewer-enabled", true, "", nullptr}, |
| 2039 | {"developer", ParamType::VAL_BOOL, OptionType::OPT_IGNORE | OptionType::OPT_BOOL, "", true, "", nullptr }, |
| 2040 | {"disable-developer", ParamType::NONE, OptionType::OPT_IGNORE | OptionType::OPT_BOOL, "", false, "", nullptr }, |
| 2041 | {"enable-developer", ParamType::NONE, OptionType::OPT_IGNORE | OptionType::OPT_BOOL, "", true, "", nullptr }, |
| 2042 | {"distance-attenuation", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/rendering/distance-attenuation", true, "", nullptr }, |
| 2043 | {"disable-distance-attenuation", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/distance-attenuation", false, "", nullptr }, |
| 2044 | {"enable-distance-attenuation", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/distance-attenuation", true, "", nullptr }, |
| 2045 | #ifdef ENABLE_IAX |
| 2046 | {"fgcom", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/fgcom/enabled", true, "", nullptr }, |
| 2047 | {"enable-fgcom", ParamType::NONE, OptionType::OPT_BOOL, "/sim/fgcom/enabled", true, "", nullptr }, |
| 2048 | {"disable-fgcom", ParamType::NONE, OptionType::OPT_BOOL, "/sim/fgcom/enabled", false, "", nullptr }, |
| 2049 | #endif |
| 2050 | {"fpe", ParamType::VAL_BOOL, OptionType::OPT_IGNORE, "", true, "", nullptr }, |
| 2051 | {"disable-fpe", ParamType::NONE, OptionType::OPT_IGNORE, "", false, "", nullptr }, |
| 2052 | {"enable-fpe", ParamType::NONE, OptionType::OPT_IGNORE, "", true, "", nullptr }, |
| 2053 | {"freeze", ParamType::VAL_BOOL, OptionType::OPT_FUNC, "", false, "true", fgOptFreeze }, |
| 2054 | {"disable-freeze", ParamType::NONE, OptionType::OPT_FUNC, "", false, "false", fgOptFreeze }, |
| 2055 | {"enable-freeze", ParamType::NONE, OptionType::OPT_FUNC, "", false, "true", fgOptFreeze }, |
| 2056 | {"fuel-freeze", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/freeze/fuel", true, "", nullptr }, |
| 2057 | {"disable-fuel-freeze", ParamType::NONE, OptionType::OPT_BOOL, "/sim/freeze/fuel", false, "", nullptr }, |
| 2058 | {"enable-fuel-freeze", ParamType::NONE, OptionType::OPT_BOOL, "/sim/freeze/fuel", true, "", nullptr }, |
| 2059 | {"fullscreen", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/startup/fullscreen", true, "", nullptr }, |
| 2060 | {"disable-fullscreen", ParamType::NONE, OptionType::OPT_BOOL, "/sim/startup/fullscreen", false, "", nullptr }, |
| 2061 | {"enable-fullscreen", ParamType::NONE, OptionType::OPT_BOOL, "/sim/startup/fullscreen", true, "", nullptr }, |
| 2062 | {"gui", ParamType::VAL_BOOL, OptionType::OPT_FUNC, "", false, "true", fgOptGUI }, |
| 2063 | {"disable-gui", ParamType::NONE, OptionType::OPT_FUNC, "", false, "false", fgOptGUI }, |
| 2064 | {"enable-gui", ParamType::NONE, OptionType::OPT_FUNC, "", false, "true", fgOptGUI }, |
| 2065 | {"hold-short", ParamType::VAL_BOOL, OptionType::OPT_FUNC, "", false, "true", fgOptHoldShort }, |
| 2066 | {"disable-hold-short", ParamType::NONE, OptionType::OPT_FUNC, "", false, "false", fgOptHoldShort }, |
| 2067 | {"enable-hold-short", ParamType::NONE, OptionType::OPT_FUNC, "", false, "true", fgOptHoldShort }, |
| 2068 | {"hud", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/hud/visibility[1]", true, "", nullptr }, |
| 2069 | {"disable-hud", ParamType::NONE, OptionType::OPT_BOOL, "/sim/hud/visibility[1]", false, "", nullptr }, |
| 2070 | {"enable-hud", ParamType::NONE, OptionType::OPT_BOOL, "/sim/hud/visibility[1]", true, "", nullptr }, |
| 2071 | {"hud-3d", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/hud/enable3d[1]", true, "", nullptr }, |
| 2072 | {"disable-hud-3d", ParamType::NONE, OptionType::OPT_BOOL, "/sim/hud/enable3d[1]", false, "", nullptr }, |
| 2073 | {"enable-hud-3d", ParamType::NONE, OptionType::OPT_BOOL, "/sim/hud/enable3d[1]", true, "", nullptr }, |
| 2074 | {"horizon-effect", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/rendering/horizon-effect", true, "", nullptr }, |
| 2075 | {"disable-horizon-effect", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/horizon-effect", false, "", nullptr }, |
| 2076 | {"enable-horizon-effect", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/horizon-effect", true, "", nullptr }, |
| 2077 | {"ignore-autosave", ParamType::VAL_BOOL, OptionType::OPT_FUNC, "", false, "true", fgOptIgnoreAutosave }, |
| 2078 | {"disable-ignore-autosave", ParamType::NONE, OptionType::OPT_FUNC, "", false, "false", fgOptIgnoreAutosave }, |
| 2079 | {"enable-ignore-autosave", ParamType::NONE, OptionType::OPT_FUNC, "", false, "true", fgOptIgnoreAutosave }, |
| 2080 | {"launcher", ParamType::VAL_BOOL, OptionType::OPT_IGNORE, "", true, "", nullptr }, |
| 2081 | {"disable-launcher", ParamType::NONE, OptionType::OPT_IGNORE, "", false, "", nullptr }, |
| 2082 | {"enable-launcher", ParamType::NONE, OptionType::OPT_IGNORE, "", true, "", nullptr }, |
| 2083 | {"load-tape-create-video", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/startup/load-tape/create-video", true, "", nullptr }, |
| 2084 | {"disable-load-tape-create-video", ParamType::NONE, OptionType::OPT_BOOL, "/sim/startup/load-tape/create-video", false, "", nullptr }, |
| 2085 | {"enable-load-tape-create-video", ParamType::NONE, OptionType::OPT_BOOL, "/sim/startup/load-tape/create-video", true, "", nullptr }, |
| 2086 | {"mouse-pointer", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/startup/mouse-pointer", true, "", nullptr }, |
| 2087 | {"disable-mouse-pointer", ParamType::NONE, OptionType::OPT_BOOL, "/sim/startup/mouse-pointer", false, "", nullptr }, |
| 2088 | {"enable-mouse-pointer", ParamType::NONE, OptionType::OPT_BOOL, "/sim/startup/mouse-pointer", true, "", nullptr }, |
| 2089 | {"panel", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/panel/visibility", true, "", nullptr }, |
| 2090 | {"disable-panel", ParamType::NONE, OptionType::OPT_BOOL, "/sim/panel/visibility", false, "", nullptr }, |
| 2091 | {"enable-panel", ParamType::NONE, OptionType::OPT_BOOL, "/sim/panel/visibility", true, "", nullptr }, |
| 2092 | {"random-buildings", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/rendering/random-buildings", true, "", nullptr }, |
| 2093 | {"disable-random-buildings", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/random-buildings", false, "", nullptr }, |
| 2094 | {"enable-random-buildings", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/random-buildings", true, "", nullptr }, |
| 2095 | {"random-objects", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/rendering/random-objects", true, "", nullptr }, |
| 2096 | {"disable-random-objects", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/random-objects", false, "", nullptr }, |
| 2097 | {"enable-random-objects", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/random-objects", true, "", nullptr }, |
| 2098 | {"random-vegetation", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/rendering/random-vegetation", true, "", nullptr }, |
| 2099 | {"disable-random-vegetation", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/random-vegetation", false, "", nullptr }, |
| 2100 | {"enable-random-vegetation", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/random-vegetation", true, "", nullptr }, |
| 2101 | {"read-only", ParamType::VAL_BOOL, OptionType::OPT_IGNORE, "", true, "", nullptr }, |
| 2102 | {"real-weather-fetch", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/environment/realwx/enabled", true, "", nullptr }, |
| 2103 | {"disable-real-weather-fetch", ParamType::NONE, OptionType::OPT_BOOL, "/environment/realwx/enabled", false, "", nullptr }, |
| 2104 | {"enable-real-weather-fetch", ParamType::NONE, OptionType::OPT_BOOL, "/environment/realwx/enabled", true, "", nullptr }, |
| 2105 | {"restart-launcher", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/restart-launcher-on-exit", true, "", nullptr }, |
| 2106 | {"disable-restart-launcher", ParamType::NONE, OptionType::OPT_BOOL, "/sim/restart-launcher-on-exit", false, "", nullptr }, |
| 2107 | {"enable-restart-launcher", ParamType::NONE, OptionType::OPT_BOOL, "/sim/restart-launcher-on-exit", true, "", nullptr }, |
| 2108 | {"restore-defaults", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/startup/restore-defaults", true, "", nullptr }, |
| 2109 | {"disable-restore-defaults", ParamType::NONE, OptionType::OPT_BOOL, "/sim/startup/restore-defaults", false, "", nullptr }, |
| 2110 | {"enable-restore-defaults", ParamType::NONE, OptionType::OPT_BOOL, "/sim/startup/restore-defaults", true, "", nullptr }, |
| 2111 | {"save-on-exit", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/startup/save-on-exit", true, "", nullptr }, |
| 2112 | {"disable-save-on-exit", ParamType::NONE, OptionType::OPT_BOOL, "/sim/startup/save-on-exit", false, "", nullptr }, |
| 2113 | {"enable-save-on-exit", ParamType::NONE, OptionType::OPT_BOOL, "/sim/startup/save-on-exit", true, "", nullptr }, |
| 2114 | {"sentry", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/startup/sentry-crash-reporting-enabled", true, "", nullptr }, |
| 2115 | {"enable-sentry", ParamType::NONE, OptionType::OPT_BOOL, "/sim/startup/sentry-crash-reporting-enabled", true, "", nullptr }, |
| 2116 | {"disable-sentry", ParamType::NONE, OptionType::OPT_BOOL, "/sim/startup/sentry-crash-reporting-enabled", false, "", nullptr }, |
| 2117 | {"sound", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/sound/working", true, "", nullptr }, |
| 2118 | {"disable-sound", ParamType::NONE, OptionType::OPT_BOOL, "/sim/sound/working", false, "", nullptr }, |
| 2119 | {"enable-sound", ParamType::NONE, OptionType::OPT_BOOL, "/sim/sound/working", true, "", nullptr }, |
| 2120 | {"specular-highlight", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/rendering/specular-highlight", true, "", nullptr }, |
| 2121 | {"disable-specular-highlight", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/specular-highlight", false, "", nullptr }, |
| 2122 | {"enable-specular-highlight", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/specular-highlight", true, "", nullptr }, |
| 2123 | {"splash-screen", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/startup/splash-screen", true, "", nullptr }, |
| 2124 | {"disable-splash-screen", ParamType::NONE, OptionType::OPT_BOOL, "/sim/startup/splash-screen", false, "", nullptr }, |
| 2125 | {"enable-splash-screen", ParamType::NONE, OptionType::OPT_BOOL, "/sim/startup/splash-screen", true, "", nullptr }, |
| 2126 | {"terrasync", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/terrasync/enabled", true, "", nullptr }, |
| 2127 | {"disable-terrasync", ParamType::NONE, OptionType::OPT_BOOL, "/sim/terrasync/enabled", false, "", nullptr }, |
| 2128 | {"enable-terrasync", ParamType::NONE, OptionType::OPT_BOOL, "/sim/terrasync/enabled", true, "", nullptr }, |
| 2129 | {"texture-cache", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/rendering/texture-cache/cache-enabled", true, "", nullptr }, |
| 2130 | {"enable-texture-cache", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/texture-cache/cache-enabled", true, "", nullptr }, |
| 2131 | {"disable-texture-cache", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/texture-cache/cache-enabled", false, "", nullptr }, |
| 2132 | #ifdef ENABLE_OSGXR |
| 2133 | {"vr", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/vr/enabled", true, "", nullptr }, |
| 2134 | {"disable-vr", ParamType::NONE, OptionType::OPT_BOOL, "/sim/vr/enabled", false, "", nullptr }, |
| 2135 | {"enable-vr", ParamType::NONE, OptionType::OPT_BOOL, "/sim/vr/enabled", true, "", nullptr }, |
| 2136 | #endif |
| 2137 | {"wireframe", ParamType::VAL_BOOL, OptionType::OPT_BOOL, "/sim/rendering/wireframe", true, "", nullptr }, |
| 2138 | {"disable-wireframe", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/wireframe", false, "", nullptr }, |
| 2139 | {"enable-wireframe", ParamType::NONE, OptionType::OPT_BOOL, "/sim/rendering/wireframe", true, "", nullptr }, |
| 2140 | }; |
| 2141 | // clang-format on |
| 2142 | |
| 2143 | namespace flightgear |
| 2144 | { |
| 2145 | |
| 2146 | /** |
| 2147 | * internal storage of a value->option binding |
| 2148 | */ |
| 2149 | class OptionValue |
| 2150 | { |
| 2151 | public: |
| 2152 | OptionValue(const OptionDesc* d, const string& v) : |
| 2153 | desc(d), value(v) |
| 2154 | {;} |
| 2155 | |
| 2156 | const OptionDesc* desc; |
| 2157 | string value; |
| 2158 | }; |
| 2159 | |
| 2160 | typedef std::vector<OptionValue> OptionValueVec; |
| 2161 | typedef std::map<string, const OptionDesc*> OptionDescDict; |
| 2162 | |
| 2163 | class Options::OptionsPrivate |
| 2164 | { |
| 2165 | public: |
| 2166 | |
| 2167 | OptionValueVec::const_iterator findValue(const string& key) const |
| 2168 | { |
| 2169 | OptionValueVec::const_iterator it = values.begin(); |
| 2170 | for (; it != values.end(); ++it) { |
| 2171 | if (!it->desc) { |
| 2172 | continue; // ignore markers |
| 2173 | } |
| 2174 | |
| 2175 | if (it->desc->option == key) { |
| 2176 | return it; |
| 2177 | } |
| 2178 | } // of set values iteration |
| 2179 | |
| 2180 | return it; // not found |
| 2181 | } |
| 2182 | |
| 2183 | OptionValueVec::iterator findValue(const string& key) |
| 2184 | { |
| 2185 | OptionValueVec::iterator it = values.begin(); |
| 2186 | for (; it != values.end(); ++it) { |
| 2187 | if (!it->desc) { |
| 2188 | continue; // ignore markers |
| 2189 | } |
| 2190 | |
| 2191 | if (it->desc->option == key) { |
| 2192 | return it; |
| 2193 | } |
| 2194 | } // of set values iteration |
| 2195 | |
| 2196 | return it; // not found |
| 2197 | } |
| 2198 | |
| 2199 | const OptionDesc* findOption(const string& key) const |
| 2200 | { |
| 2201 | OptionDescDict::const_iterator it = options.find(key); |
| 2202 | if (it == options.end()) { |
| 2203 | return NULL; |
| 2204 | } |
| 2205 | |
| 2206 | return it->second; |
| 2207 | } |
| 2208 | |
| 2209 | int processOption(const OptionDesc* desc, const string& arg_value) |
| 2210 | { |
| 2211 | if (!desc) { |
| 2212 | return FG_OPTIONS_OK; // tolerate marker options |
| 2213 | } |
| 2214 | |
| 2215 | switch ( desc->type & 0xffff ) { |
| 2216 | case OptionType::OPT_BOOL: |
| 2217 | if ( desc->param_type != ParamType::NONE && !arg_value.empty() ) { |
| 2218 | fgSetBool( desc->property, paramToBool(arg_value) ); |
| 2219 | } |
| 2220 | else { |
| 2221 | fgSetBool( desc->property, desc->b_param ); |
| 2222 | } |
| 2223 | break; |
| 2224 | case OptionType::OPT_STRING: |
| 2225 | if ( desc->param_type != ParamType::NONE && !arg_value.empty() ) { |
| 2226 | fgSetString( desc->property, arg_value.c_str() ); |
| 2227 | } else if ( desc->param_type == ParamType::NONE && arg_value.empty() ) { |
| 2228 | fgSetString( desc->property, desc->s_param ); |
| 2229 | } else if ( desc->param_type != ParamType::NONE ) { |
| 2230 | SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' needs a parameter" ); |
| 2231 | return FG_OPTIONS_ERROR; |
| 2232 | } else { |
| 2233 | SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' does not have a parameter" ); |
| 2234 | return FG_OPTIONS_ERROR; |
| 2235 | } |
| 2236 | break; |
| 2237 | case OptionType::OPT_DOUBLE: |
| 2238 | if ( !arg_value.empty() ) { |
| 2239 | fgSetDouble( desc->property, atof( arg_value ) ); |
| 2240 | } else { |
| 2241 | SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' needs a parameter" ); |
| 2242 | return FG_OPTIONS_ERROR; |
| 2243 | } |
| 2244 | break; |
| 2245 | case OptionType::OPT_INT: |
| 2246 | if ( !arg_value.empty() ) { |
| 2247 | fgSetInt( desc->property, atoi( arg_value ) ); |
| 2248 | } else { |
| 2249 | SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' needs a parameter" ); |
| 2250 | return FG_OPTIONS_ERROR; |
| 2251 | } |
| 2252 | break; |
| 2253 | case OptionType::OPT_CHANNEL: |
| 2254 | // XXX return value of add_channel should be checked? |
| 2255 | if ( desc->param_type != ParamType::NONE && !arg_value.empty() ) { |
| 2256 | add_channel( desc->option, arg_value ); |
| 2257 | } else if ( desc->param_type == ParamType::NONE && arg_value.empty() ) { |
| 2258 | add_channel( desc->option, desc->s_param ); |
| 2259 | } else if ( desc->param_type != ParamType::NONE ) { |
| 2260 | SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' needs a parameter" ); |
| 2261 | return FG_OPTIONS_ERROR; |
| 2262 | } else { |
| 2263 | SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' does not have a parameter" ); |
| 2264 | return FG_OPTIONS_ERROR; |
| 2265 | } |
| 2266 | break; |
| 2267 | case OptionType::OPT_FUNC: |
| 2268 | if ( desc->param_type != ParamType::NONE && !arg_value.empty() ) { |
| 2269 | return desc->func( arg_value.c_str() ); |
| 2270 | } else if ( arg_value.empty() && strlen(desc->s_param) ) { |
| 2271 | // It doesn't matter if the option requires a parameter or not, |
| 2272 | // always as there is no parameter but s_param is set, then call the function with s_param. |
| 2273 | return desc->func( desc->s_param ); |
| 2274 | } else if ( desc->param_type != ParamType::NONE ) { |
| 2275 | // The option requires a parameter, but arg_value and s_param are empty. |
| 2276 | SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' needs a parameter" ); |
| 2277 | return FG_OPTIONS_ERROR; |
| 2278 | } |
| 2279 | |
| 2280 | SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' does not have a parameter" ); |
| 2281 | return FG_OPTIONS_ERROR; |
| 2282 | |
| 2283 | case OptionType::OPT_IGNORE: |
| 2284 | break; |
| 2285 | } |
| 2286 | |
| 2287 | return FG_OPTIONS_OK; |
| 2288 | } |
| 2289 | |
| 2290 | /** |
| 2291 | * insert a marker value into the values vector. This is necessary |
| 2292 | * when processing options, to ensure the correct ordering, where we scan |
| 2293 | * for marker values in reverse, and then forwards within each group. |
| 2294 | */ |
| 2295 | void insertGroupMarker() |
| 2296 | { |
| 2297 | values.push_back(OptionValue(NULL, "-")); |
| 2298 | } |
| 2299 | |
| 2300 | /** |
| 2301 | * given a current iterator into the values, find the preceding group marker, |
| 2302 | * or return the beginning of the value vector. |
| 2303 | */ |
| 2304 | OptionValueVec::const_iterator rfindGroup(OptionValueVec::const_iterator pos) const |
| 2305 | { |
| 2306 | while (--pos != values.begin()) { |
| 2307 | if (pos->desc == NULL) { |
| 2308 | return pos; // found a marker, we're done |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | return pos; |
| 2313 | } |
| 2314 | |
| 2315 | // Return a pointer to a new JSON array node |
| 2316 | // (["/foo/bar", "/other/path", ...]) created from the given PathList. |
| 2317 | cJSON *createJSONArrayFromPathList(const PathList& pl) const |
| 2318 | { |
| 2319 | cJSON *resultNode = cJSON_CreateArray(); |
| 2320 | cJSON *prevNode = nullptr; |
| 2321 | bool isFirst = true; |
| 2322 | |
| 2323 | for (const SGPath& path : pl) { |
| 2324 | cJSON *pathNode = cJSON_CreateString(path.utf8Str().c_str()); |
| 2325 | |
| 2326 | if (isFirst) { |
| 2327 | isFirst = false; |
| 2328 | resultNode->child = pathNode; |
| 2329 | } else { |
| 2330 | prevNode->next = pathNode; |
| 2331 | pathNode->prev = prevNode; |
| 2332 | } |
| 2333 | |
| 2334 | prevNode = pathNode; |
| 2335 | } |
| 2336 | |
| 2337 | return resultNode; |
| 2338 | } |
| 2339 | |
| 2340 | bool showHelp, |
| 2341 | verbose, |
| 2342 | showAircraft, |
| 2343 | shouldLoadDefaultConfig; |
| 2344 | |
| 2345 | OptionDescDict options; |
| 2346 | OptionValueVec values; |
| 2347 | simgear::PathList configFiles; |
| 2348 | simgear::PathList propertyFiles; |
| 2349 | SGPath customDownloadDir; |
| 2350 | }; |
| 2351 | |
| 2352 | Options* Options::sharedInstance() |
| 2353 | { |
| 2354 | if (shared_instance == NULL) { |
| 2355 | shared_instance = new Options; |
| 2356 | } |
| 2357 | |
| 2358 | return shared_instance; |
| 2359 | } |
| 2360 | |
| 2361 | void Options::reset() |
| 2362 | { |
| 2363 | if (shared_instance != nullptr) { |
| 2364 | delete shared_instance; |
| 2365 | shared_instance = nullptr; |
| 2366 | } |
| 2367 | } |
| 2368 | |
| 2369 | Options::Options() : |
| 2370 | p(new OptionsPrivate()) |
| 2371 | { |
| 2372 | p->showHelp = false; |
| 2373 | p->verbose = false; |
| 2374 | p->showAircraft = false; |
| 2375 | p->shouldLoadDefaultConfig = true; |
| 2376 | |
| 2377 | // build option map |
| 2378 | for (auto it = fgOptionArray.begin(); it != fgOptionArray.end(); ++it) { |
| 2379 | // REVIEW: Memory Leak - 15,768 bytes in 219 blocks are still reachable |
| 2380 | p->options[ it->option ] = it; |
| 2381 | } |
| 2382 | } |
| 2383 | |
| 2384 | Options::~Options() |
| 2385 | { |
| 2386 | } |
| 2387 | |
| 2388 | OptionResult Options::init(int argc, char** argv, const SGPath& appDataPath) |
| 2389 | { |
| 2390 | // first, process the command line |
| 2391 | bool inOptions = true; |
| 2392 | simgear::optional<std::string> value; |
| 2393 | |
| 2394 | for (int i=1; i<argc; ++i) { |
| 2395 | // important : on first run after the Gatekeeper quarantine flag is |
| 2396 | // cleared, launchd passes us a null argument here. Avoid dying in |
| 2397 | // that case. |
| 2398 | if (!argv[i]) { |
| 2399 | continue; |
| 2400 | } |
| 2401 | |
| 2402 | if (inOptions && (argv[i][0] == '-')) { |
| 2403 | if (strcmp(argv[i], "--") == 0) { // end of options delimiter |
| 2404 | inOptions = false; |
| 2405 | value = {}; |
| 2406 | continue; |
| 2407 | } |
| 2408 | |
| 2409 | const string currentOption(argv[i]); |
| 2410 | // Get the next string from the list if it's a value for current option |
| 2411 | if (currentOption.find("=") == string::npos) { |
| 2412 | value = getValueFromNextParam(i, argc, argv); |
| 2413 | } |
| 2414 | else { // we have the = sign so we have the value in one string |
| 2415 | value = {}; |
| 2416 | } |
| 2417 | |
| 2418 | int result = parseOption(currentOption, value, /* fromConfigFile */ false); |
| 2419 | processArgResult(result); |
| 2420 | } else if (value.has_value()) { |
| 2421 | // Skip the value for previous option |
| 2422 | value = {}; |
| 2423 | } else { |
| 2424 | // XML properties file |
| 2425 | SGPath f = SGPath::fromUtf8(argv[i]); |
| 2426 | if (!f.exists()) { |
| 2427 | SG_LOG(SG_GENERAL, SG_ALERT, "config file not found:" << f); |
| 2428 | } else { |
| 2429 | p->propertyFiles.push_back(f); |
| 2430 | } |
| 2431 | } |
| 2432 | } // of arguments iteration |
| 2433 | p->insertGroupMarker(); // command line is one group |
| 2434 | |
| 2435 | if (isOptionSet("log-level")) { |
| 2436 | // establish log-level before anything else - otherwise it is not possible |
| 2437 | // to show extra (debug/info/warning) messages for the start-up phase. |
| 2438 | // Leave the simgear logstream default value of SG_ALERT if the argument is |
| 2439 | // not supplied. |
| 2440 | |
| 2441 | fgOptLogLevel(valueForOption("log-level").c_str()); |
| 2442 | } |
| 2443 | |
| 2444 | simgear::PathList::const_iterator i; |
| 2445 | for (i = p->configFiles.begin(); i != p->configFiles.end(); ++i) { |
| 2446 | readConfig(*i); |
| 2447 | } |
| 2448 | |
| 2449 | if (!p->shouldLoadDefaultConfig) { |
| 2450 | return setupRoot(argc, argv); |
| 2451 | } |
| 2452 | |
| 2453 | // then config files |
| 2454 | SGPath config; |
| 2455 | |
| 2456 | if( !hostname.empty() ) { |
| 2457 | // Check for ~/.fgfsrc.hostname |
| 2458 | config = SGPath::home(); |
| 2459 | config.append(".fgfsrc"); |
| 2460 | config.concat( "." ); |
| 2461 | config.concat( hostname ); |
| 2462 | readConfig(config); |
| 2463 | } |
| 2464 | |
| 2465 | // Check for ~/.fgfsrc |
| 2466 | config = SGPath::home(); |
| 2467 | config.append(".fgfsrc"); |
| 2468 | readConfig(config); |
| 2469 | |
| 2470 | // check for a config file in app data |
| 2471 | SGPath appDataConfig(appDataPath); |
| 2472 | appDataConfig.append("fgfsrc"); |
| 2473 | if (appDataConfig.exists()) { |
| 2474 | readConfig(appDataConfig); |
| 2475 | } |
| 2476 | |
| 2477 | // setup FG_ROOT |
| 2478 | auto res = setupRoot(argc, argv); |
| 2479 | if (res != FG_OPTIONS_OK) { |
| 2480 | return res; |
| 2481 | } |
| 2482 | |
| 2483 | // system.fgfsrc is disabled, as we no longer allow anything in fgdata to set |
| 2484 | // fg-root/fg-home/fg-aircraft and hence control what files Nasal can access |
| 2485 | std::string nameForError = config.utf8Str(); |
| 2486 | if( ! hostname.empty() ) { |
| 2487 | config = globals->get_fg_root(); |
| 2488 | config.append( "system.fgfsrc" ); |
| 2489 | config.concat( "." ); |
| 2490 | config.concat( hostname ); |
| 2491 | if (config.exists()) { |
| 2492 | flightgear::fatalMessageBoxThenExit( |
| 2493 | "Unsupported configuration", |
| 2494 | "You have a '" + config.utf8Str() + "' file, which is no longer " |
| 2495 | "processed for security reasons.", |
| 2496 | "If you created this file intentionally, please move it to '" + |
| 2497 | nameForError + "'."); |
| 2498 | } |
| 2499 | } |
| 2500 | |
| 2501 | config = globals->get_fg_root(); |
| 2502 | config.append( "system.fgfsrc" ); |
| 2503 | if (config.exists()) { |
| 2504 | flightgear::fatalMessageBoxThenExit( |
| 2505 | "Unsupported configuration", |
| 2506 | "You have a '" + config.utf8Str() + "' file, which is no longer " |
| 2507 | "processed for security reasons.", |
| 2508 | "If you created this file intentionally, please move it to '" + |
| 2509 | nameForError + "'."); |
| 2510 | } |
| 2511 | |
| 2512 | return FG_OPTIONS_OK; |
| 2513 | } |
| 2514 | |
| 2515 | static int |
| 2516 | getOptionParamType(string option) |
| 2517 | { |
| 2518 | if (simgear::strutils::starts_with(option, "--prop:") || |
| 2519 | simgear::strutils::starts_with(option, "prop:") |
| 2520 | ) { |
| 2521 | // The --prop option comes with the whole string with the property name, |
| 2522 | // we need to truncate it only to the option name. |
| 2523 | option = "prop"; |
| 2524 | } |
| 2525 | |
| 2526 | const bool withDashes = simgear::strutils::starts_with(option, "--"); |
| 2527 | const string dashes("--"); |
| 2528 | |
| 2529 | const auto desc = std::find_if(fgOptionArray.begin(), fgOptionArray.end(), [&withDashes, &dashes, &option](const OptionDesc& opDesc) { |
| 2530 | return (withDashes && dashes + opDesc.option == option) || (!withDashes && opDesc.option == option); |
| 2531 | }); |
| 2532 | |
| 2533 | if (desc != fgOptionArray.end()) { |
| 2534 | return desc->param_type; |
| 2535 | } |
| 2536 | |
| 2537 | return ParamType::NONE; |
| 2538 | } |
| 2539 | |
| 2540 | simgear::optional<std::string> Options::getValueFromNextParam(int index, int argc, char** argv) |
| 2541 | { |
| 2542 | if (index + 1 >= argc) { |
| 2543 | // No more arguments, return empty value |
| 2544 | return {}; |
| 2545 | } |
| 2546 | |
| 2547 | const string currentOption(argv[index]); |
| 2548 | |
| 2549 | // Get param type of current option |
| 2550 | const int paramType = getOptionParamType(currentOption); |
| 2551 | |
| 2552 | if (paramType == ParamType::NONE) { |
| 2553 | // We know that the option does not take parameters so return empty value |
| 2554 | return {}; |
| 2555 | } |
| 2556 | |
| 2557 | const string value(argv[index + 1]); |
| 2558 | |
| 2559 | if (paramType == ParamType::VAL_BOOL && simgear::strutils::is_bool(value)) { |
| 2560 | // We know that the option takes a bool parameter and the value is of |
| 2561 | // type boolean, so assign a value to the option |
| 2562 | return value; |
| 2563 | } |
| 2564 | |
| 2565 | if (simgear::strutils::starts_with(value, "-")) { |
| 2566 | // It's not a value but an option (including -c, -h, -v, -psn), |
| 2567 | // return empty value |
| 2568 | return {}; |
| 2569 | } |
| 2570 | |
| 2571 | if (paramType == ParamType::REGULAR) { |
| 2572 | return value; |
| 2573 | } |
| 2574 | |
| 2575 | return {}; |
| 2576 | } |
| 2577 | |
| 2578 | void Options::initPaths() |
| 2579 | { |
| 2580 | for (const string& pathOpt : valuesForOption("fg-aircraft")) { |
| 2581 | PathList paths = SGPath::pathsFromUtf8(pathOpt); |
| 2582 | globals->append_aircraft_paths(paths); |
| 2583 | } |
| 2584 | |
| 2585 | const char* envp = ::getenv("FG_AIRCRAFT"); |
| 2586 | if (envp) { |
| 2587 | globals->append_aircraft_paths(SGPath::pathsFromEnv("FG_AIRCRAFT")); |
| 2588 | } |
| 2589 | |
| 2590 | } |
| 2591 | |
| 2592 | OptionResult Options::initAircraft() |
| 2593 | { |
| 2594 | string aircraft; |
| 2595 | if (g_load_tape_aircraft != "") { |
| 2596 | // Use Continuous recording's aircraft if we are replaying on startup. |
| 2597 | aircraft = g_load_tape_aircraft; |
| 2598 | } |
| 2599 | else if (isOptionSet("aircraft")) { |
| 2600 | aircraft = valueForOption("aircraft"); |
| 2601 | } else if (isOptionSet("vehicle")) { |
| 2602 | aircraft = valueForOption("vehicle"); |
| 2603 | } |
| 2604 | |
| 2605 | if (!aircraft.empty()) { |
| 2606 | fgSetString("/sim/aircraft-id", aircraft); |
| 2607 | const auto lastDotPos = aircraft.rfind('.'); |
| 2608 | if (lastDotPos != string::npos) { |
| 2609 | // ensure /sim/aircraft is only the local ID, not the fully-qualified ID |
| 2610 | // otherwise some existing logic gets confused. |
| 2611 | fgSetString("/sim/aircraft", aircraft.substr(lastDotPos + 1)); |
| 2612 | } else { |
| 2613 | fgSetString("/sim/aircraft", aircraft); |
| 2614 | } |
| 2615 | |
| 2616 | SG_LOG(SG_INPUT, SG_INFO, "aircraft = " << aircraft ); |
| 2617 | } else { |
| 2618 | SG_LOG(SG_INPUT, SG_INFO, "No user specified aircraft, using default" ); |
| 2619 | // ensure aircraft-id is valid |
| 2620 | fgSetString("/sim/aircraft-id", fgGetString("/sim/aircraft").c_str()); |
| 2621 | } |
| 2622 | |
| 2623 | if (p->showAircraft) { |
| 2624 | PathList path_list; |
| 2625 | |
| 2626 | fgOptLogLevel( "alert" ); |
| 2627 | |
| 2628 | // First place to check is the 'Aircraft' sub-directory in $FG_ROOT |
| 2629 | |
| 2630 | SGPath rootAircraft = globals->get_fg_root(); |
| 2631 | rootAircraft.append("Aircraft"); |
| 2632 | path_list.push_back(rootAircraft); |
| 2633 | |
| 2634 | // Additionally, aircraft may also be found in user-defined places |
| 2635 | // (via $FG_AIRCRAFT or with the '--fg-aircraft' option) |
| 2636 | PathList aircraft_paths = globals->get_aircraft_paths(); |
| 2637 | |
| 2638 | path_list.insert(path_list.end(), aircraft_paths.begin(), |
| 2639 | aircraft_paths.end()); |
| 2640 | |
| 2641 | fgShowAircraft(path_list); |
| 2642 | // this is to indicate that we did show it |
| 2643 | return FG_OPTIONS_SHOW_AIRCRAFT; |
| 2644 | } |
| 2645 | |
| 2646 | if (isOptionSet("aircraft-dir")) { |
| 2647 | SGPath aircraftDirPath = SGPath::fromUtf8(valueForOption("aircraft-dir")); |
| 2648 | SGPath realAircraftPath = aircraftDirPath.realpath(); |
| 2649 | globals->append_read_allowed_paths(realAircraftPath); |
| 2650 | |
| 2651 | // Set this now, so it's available in FindAndCacheAircraft. Use realpath() |
| 2652 | // as in FGGlobals::append_aircraft_path(), otherwise SGPath::validate() |
| 2653 | // will deny access to resources under this path if one of its components |
| 2654 | // is a symlink (which is not a problem, since it was given as is by the |
| 2655 | // user---this is very different from a symlink *under* the aircraft dir |
| 2656 | // or a scenery dir). |
| 2657 | fgSetString("/sim/aircraft-dir", realAircraftPath.utf8Str()); |
| 2658 | } |
| 2659 | |
| 2660 | if (isOptionSet("state")) { |
| 2661 | std::string stateName = valueForOption("state"); |
| 2662 | // can't validate this until the -set.xml is parsed |
| 2663 | fgSetString("/sim/aircraft-state", stateName); |
| 2664 | } |
| 2665 | |
| 2666 | return FG_OPTIONS_OK; |
| 2667 | } |
| 2668 | |
| 2669 | void Options::processArgResult(int result) |
| 2670 | { |
| 2671 | if ((result == FG_OPTIONS_HELP) || (result == FG_OPTIONS_ERROR)) |
| 2672 | p->showHelp = true; |
| 2673 | else if (result == FG_OPTIONS_VERBOSE_HELP) |
| 2674 | p->verbose = true; |
| 2675 | else if (result == FG_OPTIONS_SHOW_AIRCRAFT) { |
| 2676 | p->showAircraft = true; |
| 2677 | } else if (result == FG_OPTIONS_NO_DEFAULT_CONFIG) { |
| 2678 | p->shouldLoadDefaultConfig = false; |
| 2679 | } else if (result == FG_OPTIONS_SHOW_SOUND_DEVICES) { |
| 2680 | SGSoundMgr smgr; |
| 2681 | |
| 2682 | smgr.init(); |
| 2683 | string vendor = smgr.get_vendor(); |
| 2684 | string renderer = smgr.get_renderer(); |
| 2685 | cout << renderer << " provided by " << vendor << endl; |
| 2686 | cout << endl << "No. Device" << endl; |
| 2687 | |
| 2688 | vector <std::string>devices = smgr.get_available_devices(); |
| 2689 | for (vector <std::string>::size_type i=0; i<devices.size(); i++) { |
| 2690 | cout << i << ". \"" << devices[i] << "\"" << endl; |
| 2691 | } |
| 2692 | devices.clear(); |
| 2693 | smgr.stop(); |
| 2694 | exit(0); |
| 2695 | } else if (result == FG_OPTIONS_EXIT) { |
| 2696 | exit(0); |
| 2697 | } |
| 2698 | } |
| 2699 | |
| 2700 | void Options::readConfig(const SGPath& path) |
| 2701 | { |
| 2702 | using namespace simgear; |
| 2703 | |
| 2704 | sg_gzifstream in(path); |
| 2705 | if (!in.is_open()) { |
| 2706 | return; |
| 2707 | } |
| 2708 | |
| 2709 | SG_LOG(SG_GENERAL, SG_INFO, "Processing config files: " << path); |
| 2710 | |
| 2711 | in >> skipcomment; |
| 2712 | while ( ! in.eof() ) { |
| 2713 | string line; |
| 2714 | getline( in, line, '\n' ); |
| 2715 | |
| 2716 | // remove leading and trailing whitespace including tabs, newlines |
| 2717 | line = strutils::strip(line); |
| 2718 | |
| 2719 | // avoid processing empty lines |
| 2720 | // https://sourceforge.net/p/flightgear/codetickets/2927/ |
| 2721 | if (line.empty()) { |
| 2722 | in >> skipcomment; |
| 2723 | continue; |
| 2724 | } |
| 2725 | |
| 2726 | simgear::optional<std::string> value; |
| 2727 | const size_t space = line.find(' '); |
| 2728 | const size_t equal = line.find('='); |
| 2729 | if (space != string::npos && space < equal) { |
| 2730 | // We assume that the value is separated by a space from the option name, like: |
| 2731 | // --metar XXXX 280900Z 28007KT 9999 20/16 Q1010 instead of |
| 2732 | // --metar=XXXX 280900Z 28007KT 9999 20/16 Q1010 |
| 2733 | value = strutils::strip(line.substr(space + 1)); |
| 2734 | line = line.substr(0, space); |
| 2735 | } |
| 2736 | |
| 2737 | if ( parseOption(line, value, /* fromConfigFile */ true) == FG_OPTIONS_ERROR ) { |
| 2738 | cerr << endl << "Config file parse error: " << path << " '" |
| 2739 | << line << "'" << endl; |
| 2740 | p->showHelp = true; |
| 2741 | } |
| 2742 | in >> skipcomment; |
| 2743 | } |
| 2744 | |
| 2745 | p->insertGroupMarker(); // each config file is a group |
| 2746 | } |
| 2747 | |
| 2748 | bool Options::paramToBool(const std::string& param) |
| 2749 | { |
| 2750 | if (simgear::strutils::is_bool(param)) { |
| 2751 | return simgear::strutils::to_bool(param); |
| 2752 | } |
| 2753 | |
| 2754 | return true; |
| 2755 | } |
| 2756 | |
| 2757 | /** |
| 2758 | * @param optional<string> val Contains a value when the user has specified a value for |
| 2759 | * the option separated by a space instead of the '=' character. |
| 2760 | * Otherwise val has no value. |
| 2761 | */ |
| 2762 | int Options::parseOption(const string& s, const simgear::optional<std::string>& val, bool fromConfigFile) |
| 2763 | { |
| 2764 | if ((s == "--help") || (s=="-h")) { |
| 2765 | return FG_OPTIONS_HELP; |
| 2766 | } else if ( (s == "--verbose") || (s == "-v") ) { |
| 2767 | // verbose help/usage request |
| 2768 | return FG_OPTIONS_VERBOSE_HELP; |
| 2769 | } else if (simgear::strutils::starts_with(s, "--console") || s == "-c") { |
| 2770 | return fgOptConsole(getValueForBooleanOption(s, "--console", val).c_str()); |
| 2771 | } else if (simgear::strutils::starts_with(s, "-psn")) { |
| 2772 | // on Mac, when launched from the GUI, we are passed the ProcessSerialNumber |
| 2773 | // as an argument (and no others). Silently ignore the argument here. |
| 2774 | return FG_OPTIONS_OK; |
| 2775 | } else if (simgear::strutils::starts_with(s, "--show-aircraft")) { |
| 2776 | return paramToBool(getValueForBooleanOption(s, "--show-aircraft", val)) |
| 2777 | ? FG_OPTIONS_SHOW_AIRCRAFT |
| 2778 | : FG_OPTIONS_OK; |
| 2779 | } else if (simgear::strutils::starts_with(s, "--show-sound-devices")) { |
| 2780 | return paramToBool(getValueForBooleanOption(s, "--show-sound-devices", val)) |
| 2781 | ? FG_OPTIONS_SHOW_SOUND_DEVICES |
| 2782 | : FG_OPTIONS_OK; |
| 2783 | } else if (simgear::strutils::starts_with(s, "--no-default-config")) { |
| 2784 | return paramToBool(getValueForBooleanOption(s, "--no-default-config", val)) |
| 2785 | ? FG_OPTIONS_NO_DEFAULT_CONFIG |
| 2786 | : FG_OPTIONS_OK; |
| 2787 | } else if (simgear::strutils::starts_with(s, "--prop:")) { |
| 2788 | // property setting has a slightly different syntax, so fudge things |
| 2789 | const OptionDesc* desc = p->findOption("prop"); |
| 2790 | |
| 2791 | const size_t optLen = strlen("--prop:"); |
| 2792 | |
| 2793 | if (s.find('=', optLen) != string::npos) { |
| 2794 | p->values.push_back(OptionValue(desc, s.substr(optLen))); |
| 2795 | return FG_OPTIONS_OK; |
| 2796 | } |
| 2797 | |
| 2798 | if (val.has_value()) { |
| 2799 | p->values.push_back(OptionValue(desc, s.substr(optLen) + "=" + val.value())); |
| 2800 | return FG_OPTIONS_OK; |
| 2801 | } |
| 2802 | |
| 2803 | SG_LOG(SG_GENERAL, SG_ALERT, "malformed property option: " << s); |
| 2804 | return FG_OPTIONS_ERROR; |
| 2805 | } else if (simgear::strutils::starts_with(s, "--config=")) { |
| 2806 | return parseConfigOption(s.substr(9), fromConfigFile); |
| 2807 | } else if (simgear::strutils::starts_with(s, "--config") && val.has_value() ) { |
| 2808 | return parseConfigOption(val.value(), fromConfigFile); |
| 2809 | } else if (simgear::strutils::starts_with(s, "--")) { |
| 2810 | size_t eqPos = s.find( '=' ); |
| 2811 | string key, value; |
| 2812 | if (eqPos == string::npos) { |
| 2813 | key = s.substr(2); |
| 2814 | |
| 2815 | if (val.has_value()) { |
| 2816 | value = val.value(); |
| 2817 | } |
| 2818 | } else { |
| 2819 | key = s.substr( 2, eqPos - 2 ); |
| 2820 | value = s.substr( eqPos + 1); |
| 2821 | } |
| 2822 | |
| 2823 | return addOption(key, value); |
| 2824 | } else if (s.empty()) { |
| 2825 | return FG_OPTIONS_OK; |
| 2826 | } else { |
| 2827 | flightgear::modalMessageBox("Unknown option", "Unknown command-line option: " + s); |
| 2828 | return FG_OPTIONS_ERROR; |
| 2829 | } |
| 2830 | } |
| 2831 | |
| 2832 | string Options::getValueForBooleanOption(const string& str, const string& option, const simgear::optional<std::string>& value) |
| 2833 | { |
| 2834 | if (simgear::strutils::starts_with(str, option + "=")) { |
| 2835 | // We have option with "=", get value after "=" sign |
| 2836 | return str.substr((option + "=").size()); |
| 2837 | } |
| 2838 | else if (value.has_value()) { |
| 2839 | // Get value after " " sign |
| 2840 | return value.value(); |
| 2841 | } |
| 2842 | |
| 2843 | // The option has no value, return "true" as default |
| 2844 | return "true"; |
| 2845 | } |
| 2846 | |
| 2847 | int Options::parseConfigOption(const SGPath &path, bool fromConfigFile) |
| 2848 | { |
| 2849 | if (path.extension() == "xml") { |
| 2850 | p->propertyFiles.push_back(path); |
| 2851 | } |
| 2852 | else if (fromConfigFile) { |
| 2853 | flightgear::fatalMessageBoxThenExit( |
| 2854 | "FlightGear", |
| 2855 | "Invalid use of the --config option.", |
| 2856 | "Sorry, it is currently not supported to load a configuration file " |
| 2857 | "using --config from another configuration file.\n\n" |
| 2858 | "Note: this does not apply to loading of XML PropertyList files " |
| 2859 | "with --config." |
| 2860 | ); |
| 2861 | } |
| 2862 | else { // the --config option comes from the command line |
| 2863 | p->configFiles.push_back(path); |
| 2864 | } |
| 2865 | |
| 2866 | return FG_OPTIONS_OK; |
| 2867 | } |
| 2868 | |
| 2869 | int Options::addOption(const string &key, const string &value) |
| 2870 | { |
| 2871 | if (key == "config") { |
| 2872 | // occurs when the launcher adds --config options |
| 2873 | SGPath path(value); |
| 2874 | if (!path.exists()) { |
| 2875 | return FG_OPTIONS_ERROR; |
| 2876 | } |
| 2877 | |
| 2878 | if (path.extension() == "xml") { |
| 2879 | p->propertyFiles.push_back(path); |
| 2880 | } else { |
| 2881 | p->insertGroupMarker(); // begin a group for the config file |
| 2882 | readConfig(path); |
| 2883 | } |
| 2884 | |
| 2885 | return FG_OPTIONS_OK; |
| 2886 | } |
| 2887 | |
| 2888 | const OptionDesc* desc = p->findOption(key); |
| 2889 | if (!desc) { |
| 2890 | flightgear::modalMessageBox("Unknown option", "Unknown command-line option: " + key); |
| 2891 | return FG_OPTIONS_ERROR; |
| 2892 | } |
| 2893 | |
| 2894 | if (!(desc->type & OPTION_MULTI)) { |
| 2895 | OptionValueVec::const_iterator it = p->findValue(key); |
| 2896 | if (it != p->values.end()) { |
| 2897 | SG_LOG(SG_GENERAL, SG_WARN, "multiple values forbidden for option:" << key << ", ignoring:" << value); |
| 2898 | return FG_OPTIONS_OK; |
| 2899 | } |
| 2900 | } |
| 2901 | |
| 2902 | p->values.push_back(OptionValue(desc, value)); |
| 2903 | return FG_OPTIONS_OK; |
| 2904 | } |
| 2905 | |
| 2906 | int Options::setOption(const string &key, const string &value) |
| 2907 | { |
| 2908 | const OptionDesc* desc = p->findOption(key); |
| 2909 | if (!desc) { |
| 2910 | flightgear::modalMessageBox("Unknown option", "Unknown command-line option: " + key); |
| 2911 | return FG_OPTIONS_ERROR; |
| 2912 | } |
| 2913 | |
| 2914 | if (!(desc->type & OPTION_MULTI)) { |
| 2915 | OptionValueVec::iterator it = p->findValue(key); |
| 2916 | if (it != p->values.end()) { |
| 2917 | // remove existing valye |
| 2918 | p->values.erase(it); |
| 2919 | } |
| 2920 | } |
| 2921 | |
| 2922 | p->values.push_back(OptionValue(desc, value)); |
| 2923 | return FG_OPTIONS_OK; |
| 2924 | } |
| 2925 | |
| 2926 | void Options::clearOption(const std::string& key) |
| 2927 | { |
| 2928 | OptionValueVec::iterator it = p->findValue(key); |
| 2929 | for (; it != p->values.end(); it = p->findValue(key)) { |
| 2930 | p->values.erase(it); |
| 2931 | } |
| 2932 | } |
| 2933 | |
| 2934 | bool Options::isOptionSet(const string &key) const |
| 2935 | { |
| 2936 | if (getOptionParamType(key) == ParamType::VAL_BOOL) { |
| 2937 | return isBoolOptionEnable(key); |
| 2938 | } |
| 2939 | |
| 2940 | OptionValueVec::const_iterator it = p->findValue(key); |
| 2941 | return (it != p->values.end()); |
| 2942 | } |
| 2943 | |
| 2944 | string Options::valueForOption(const string& key, const string& defValue) const |
| 2945 | { |
| 2946 | OptionValueVec::const_iterator it = p->findValue(key); |
| 2947 | if (it == p->values.end()) { |
| 2948 | return defValue; |
| 2949 | } |
| 2950 | |
| 2951 | return it->value; |
| 2952 | } |
| 2953 | |
| 2954 | string_list Options::valuesForOption(const std::string& key) const |
| 2955 | { |
| 2956 | string_list result; |
| 2957 | OptionValueVec::const_iterator it = p->values.begin(); |
| 2958 | for (; it != p->values.end(); ++it) { |
| 2959 | if (!it->desc) { |
| 2960 | continue; // ignore marker values |
| 2961 | } |
| 2962 | |
| 2963 | if (it->desc->option == key) { |
| 2964 | result.push_back(it->value); |
| 2965 | } |
| 2966 | } |
| 2967 | |
| 2968 | return result; |
| 2969 | } |
| 2970 | |
| 2971 | simgear::optional<bool> Options::checkBoolOptionSet(const string &key) const |
| 2972 | { |
| 2973 | if (isOptionSet("enable-" + key)) { |
| 2974 | return true; // explicitly enabled |
| 2975 | } |
| 2976 | |
| 2977 | if (isOptionSet("disable-" + key)) { |
| 2978 | return false; // explicitly disabled |
| 2979 | } |
| 2980 | |
| 2981 | OptionValueVec::const_iterator it = p->findValue(key); |
| 2982 | if (it == p->values.end()) { |
| 2983 | return {}; // option not found |
| 2984 | } |
| 2985 | |
| 2986 | if (it->value.empty()) { |
| 2987 | // The user used a boolean option but without passing a value, such as e.g. `--fullscreen`. |
| 2988 | // Then return the enabled flag. |
| 2989 | return true; // enabled by default |
| 2990 | } |
| 2991 | |
| 2992 | // The user used a boolean option with a passed value, such as `--fullscreen false`. |
| 2993 | // Convert the value to boolean. |
| 2994 | return paramToBool(it->value) |
| 2995 | ? true // explicitly enabled |
| 2996 | : false; // explicitly disabled |
| 2997 | } |
| 2998 | |
| 2999 | bool Options::isBoolOptionEnable(const string &key) const |
| 3000 | { |
| 3001 | const simgear::optional<bool> value = checkBoolOptionSet(key); |
| 3002 | return value.has_value() && value.value() == true; |
| 3003 | } |
| 3004 | |
| 3005 | bool Options::isBoolOptionDisable(const string &key) const |
| 3006 | { |
| 3007 | const simgear::optional<bool> value = checkBoolOptionSet(key); |
| 3008 | return value.has_value() && value.value() == false; |
| 3009 | } |
| 3010 | |
| 3011 | SGPath defaultDownloadDir() |
| 3012 | { |
| 3013 | #if defined(SG_WINDOWS) |
| 3014 | const SGPath p = SGPath::home() / "FlightGear" / "Downloads"; |
| 3015 | return p; |
| 3016 | #endif |
| 3017 | return globals->get_fg_home(); |
| 3018 | } |
| 3019 | |
| 3020 | SGPath Options::actualDownloadDir() const |
| 3021 | { |
| 3022 | // explicitly set download-dir on the command line always takes |
| 3023 | // precedent |
| 3024 | SGPath downloadDir = SGPath::fromUtf8(valueForOption("download-dir")); |
| 3025 | if (!downloadDir.isNull()) { |
| 3026 | return downloadDir; |
| 3027 | } |
| 3028 | |
| 3029 | if (!p->customDownloadDir.isNull()) { |
| 3030 | return p->customDownloadDir; |
| 3031 | } |
| 3032 | |
| 3033 | return defaultDownloadDir(); |
| 3034 | } |
| 3035 | |
| 3036 | SGPath Options::actualTerrasyncDir() const |
| 3037 | { |
| 3038 | SGPath explicitTSyncDir = SGPath::fromUtf8(valueForOption("terrasync-dir")); |
| 3039 | if (!explicitTSyncDir.isNull()) { |
| 3040 | return explicitTSyncDir; |
| 3041 | } |
| 3042 | |
| 3043 | return actualDownloadDir() / "TerraSync"; |
| 3044 | } |
| 3045 | |
| 3046 | void Options::setCustomDownloadDir(const SGPath& path) |
| 3047 | { |
| 3048 | p->customDownloadDir = path; |
| 3049 | } |
| 3050 | |
| 3051 | SGPath defaultTextureCacheDir() |
| 3052 | { |
| 3053 | return Options::sharedInstance()->actualDownloadDir() / "TextureCache"; |
| 3054 | } |
| 3055 | |
| 3056 | OptionResult Options::processOptions() |
| 3057 | { |
| 3058 | // establish locale before showing help (this selects the default locale, |
| 3059 | // when no explicit option was set) |
| 3060 | globals->get_locale()->selectLanguage(valueForOption("language").c_str()); |
| 3061 | |
| 3062 | // now FG_ROOT is setup, process various command line options that bail us |
| 3063 | // out quickly, but rely on aircraft / root settings |
| 3064 | if (p->showHelp) { |
| 3065 | showUsage(); |
| 3066 | return FG_OPTIONS_EXIT; |
| 3067 | } |
| 3068 | |
| 3069 | // processing order is complicated. We must process groups LIFO, but the |
| 3070 | // values *within* each group in FIFO order, to retain consistency with |
| 3071 | // older versions of FG, and existing user configs. |
| 3072 | // in practice this means system.fgfsrc must be *processed* before |
| 3073 | // .fgfsrc, which must be processed before the command line args, and so on. |
| 3074 | OptionValueVec::const_iterator groupEnd = p->values.end(); |
| 3075 | |
| 3076 | while (groupEnd != p->values.begin()) { |
| 3077 | OptionValueVec::const_iterator groupBegin = p->rfindGroup(groupEnd); |
| 3078 | // run over the group in FIFO order |
| 3079 | OptionValueVec::const_iterator it; |
| 3080 | for (it = groupBegin; it != groupEnd; ++it) { |
| 3081 | int result = p->processOption(it->desc, it->value); |
| 3082 | switch(result) |
| 3083 | { |
| 3084 | case FG_OPTIONS_ERROR: |
| 3085 | showUsage(); |
| 3086 | return FG_OPTIONS_ERROR; |
| 3087 | |
| 3088 | case FG_OPTIONS_EXIT: |
| 3089 | return FG_OPTIONS_EXIT; |
| 3090 | |
| 3091 | default: |
| 3092 | break; |
| 3093 | } |
| 3094 | if (it->desc) { |
| 3095 | SG_LOG(SG_GENERAL, SG_INFO, "\toption:" << it->desc->option << " = " << it->value); |
| 3096 | } |
| 3097 | } |
| 3098 | |
| 3099 | groupEnd = groupBegin; |
| 3100 | } |
| 3101 | |
| 3102 | for (const SGPath& file : p->propertyFiles) { |
| 3103 | SG_LOG(SG_GENERAL, SG_INFO, |
| 3104 | "Reading command-line property file " << file); |
| 3105 | readProperties(file, globals->get_props()); |
| 3106 | } |
| 3107 | |
| 3108 | // now options are process, do supplemental fixup |
| 3109 | const char *envp = ::getenv( "FG_SCENERY" ); |
| 3110 | if (envp) { |
| 3111 | globals->append_fg_scenery(SGPath::pathsFromEnv("FG_SCENERY")); |
| 3112 | } |
| 3113 | |
| 3114 | // Download dir fix-up |
| 3115 | SGPath downloadDir = SGPath::fromUtf8(valueForOption("download-dir")); |
| 3116 | if (downloadDir.isNull()) { |
| 3117 | downloadDir = defaultDownloadDir(); |
| 3118 | SG_LOG(SG_GENERAL, SG_INFO, |
| 3119 | "Using default download dir: " << downloadDir); |
| 3120 | } else { |
| 3121 | SG_LOG(SG_GENERAL, SG_INFO, |
| 3122 | "Using explicit download dir: " << downloadDir); |
| 3123 | } |
| 3124 | |
| 3125 | simgear::Dir d(downloadDir); |
| 3126 | if (!d.exists()) { |
| 3127 | SG_LOG(SG_GENERAL, SG_INFO, |
| 3128 | "Creating download dir: " << downloadDir); |
| 3129 | d.create(0755); |
| 3130 | } |
| 3131 | |
| 3132 | // This is safe because the value of 'downloadDir' is trustworthy. In |
| 3133 | // particular, it can't be influenced by Nasal code, not even indirectly |
| 3134 | // via a Nasal-writable place such as the property tree. |
| 3135 | globals->set_download_dir(downloadDir); |
| 3136 | |
| 3137 | // Texture Cache directory handling |
| 3138 | SGPath textureCacheDir = SGPath::fromUtf8(valueForOption("texture-cache-dir")); |
| 3139 | if (textureCacheDir.isNull()) { |
| 3140 | textureCacheDir = defaultTextureCacheDir(); |
| 3141 | SG_LOG(SG_GENERAL, SG_INFO, |
| 3142 | "Using default texture cache directory: " << textureCacheDir); |
| 3143 | } |
| 3144 | else { |
| 3145 | SG_LOG(SG_GENERAL, SG_INFO, |
| 3146 | "Using explicit texture cache directory: " << textureCacheDir); |
| 3147 | } |
| 3148 | |
| 3149 | simgear::Dir tcd(textureCacheDir); |
| 3150 | if (!tcd.exists()) { |
| 3151 | SG_LOG(SG_GENERAL, SG_INFO, |
| 3152 | "Creating texture cache directory: " << textureCacheDir); |
| 3153 | tcd.create(0755); |
| 3154 | } |
| 3155 | |
| 3156 | globals->set_texture_cache_dir(textureCacheDir); |
| 3157 | |
| 3158 | |
| 3159 | // TerraSync directory fixup |
| 3160 | SGPath terrasyncDir = SGPath::fromUtf8(valueForOption("terrasync-dir")); |
| 3161 | if (terrasyncDir.isNull()) { |
| 3162 | terrasyncDir = downloadDir / "TerraSync"; |
| 3163 | // No “default” qualifier here, because 'downloadDir' may be non-default |
| 3164 | SG_LOG(SG_GENERAL, SG_INFO, |
| 3165 | "Using TerraSync dir: " << terrasyncDir); |
| 3166 | } else { |
| 3167 | SG_LOG(SG_GENERAL, SG_INFO, |
| 3168 | "Using explicit TerraSync dir: " << terrasyncDir); |
| 3169 | } |
| 3170 | |
| 3171 | d = simgear::Dir(terrasyncDir); |
| 3172 | if (!d.exists()) { |
| 3173 | SG_LOG(SG_GENERAL, SG_INFO, |
| 3174 | "Creating TerraSync dir: " << terrasyncDir); |
| 3175 | d.create(0755); |
| 3176 | } |
| 3177 | |
| 3178 | // This is safe because the value of 'terrasyncDir' is trustworthy. In |
| 3179 | // particular, it can't be influenced by Nasal code, not even indirectly |
| 3180 | // via a Nasal-writable place such as the property tree. |
| 3181 | globals->set_terrasync_dir(terrasyncDir); |
| 3182 | |
| 3183 | // check if we setup a scenery path so far |
| 3184 | bool addFGDataScenery = globals->get_fg_scenery().empty(); |
| 3185 | |
| 3186 | // always add the terrasync location, regardless of whether terrasync |
| 3187 | // is enabled or not. This allows us to toggle terrasync on/off at |
| 3188 | // runtime and have things work as expected |
| 3189 | const PathList& scenery_paths(globals->get_fg_scenery()); |
| 3190 | if (std::find(scenery_paths.begin(), scenery_paths.end(), terrasyncDir) == scenery_paths.end()) { |
| 3191 | // terrasync dir is not in the scenery paths, add it |
| 3192 | globals->append_fg_scenery(terrasyncDir); |
| 3193 | } |
| 3194 | |
| 3195 | if (addFGDataScenery) { |
| 3196 | // no scenery paths set at all, use the data in FG_ROOT |
| 3197 | // ensure this path is added last |
| 3198 | SGPath root(globals->get_fg_root()); |
| 3199 | root.append("Scenery"); |
| 3200 | globals->append_fg_scenery(root); |
| 3201 | } |
| 3202 | |
| 3203 | if (!g_load_tape_aircraft.empty()) { |
| 3204 | // This might not be necessary, because we always end up calling |
| 3205 | // Options::initAircraft() later on, which also knows to use |
| 3206 | // g_load_tape_aircraft if it is not "". |
| 3207 | // |
| 3208 | SG_LOG(SG_GENERAL, SG_MANDATORY_INFO, "overriding aircraft from " << fgGetString("/sim/aircraft") << " to " << g_load_tape_aircraft); |
| 3209 | fgSetString("/sim/aircraft", g_load_tape_aircraft); |
| 3210 | } |
| 3211 | if (!g_load_tape_airport.empty()) { |
| 3212 | SG_LOG(SG_GENERAL, SG_MANDATORY_INFO, "overriding airport from " << fgGetString("/sim/presets/airport-id") << " to " << g_load_tape_airport); |
| 3213 | fgOptAirport(g_load_tape_airport.c_str()); |
| 3214 | } |
| 3215 | |
| 3216 | if (isOptionSet("json-report")) { |
| 3217 | printJSONReport(); |
| 3218 | return FG_OPTIONS_EXIT; |
| 3219 | } else if (isOptionSet("version")) { |
| 3220 | showVersion(); |
| 3221 | return FG_OPTIONS_EXIT; |
| 3222 | } else if (isOptionSet("info")) { |
| 3223 | showInfo(); |
| 3224 | return FG_OPTIONS_EXIT; |
| 3225 | } |
| 3226 | |
| 3227 | return FG_OPTIONS_OK; |
| 3228 | } |
| 3229 | |
| 3230 | void Options::showUsage() const |
| 3231 | { |
| 3232 | fgOptLogLevel( "alert" ); |
| 3233 | |
| 3234 | FGLocale *locale = globals->get_locale(); |
| 3235 | SGPropertyNode options_root; |
| 3236 | |
| 3237 | simgear::requestConsole(false); // ensure console is shown on Windows |
| 3238 | cout << endl; |
| 3239 | |
| 3240 | try { |
| 3241 | fgLoadProps("options.xml", &options_root); |
| 3242 | } catch (const sg_exception &) { |
| 3243 | cout << "Unable to read the help file." << endl; |
| 3244 | cout << "Make sure the file options.xml is located in the FlightGear base directory," << endl; |
| 3245 | cout << "and the location of the base directory is specified by setting $FG_ROOT or" << endl; |
| 3246 | cout << "by adding --fg-root=path as a program argument." << endl; |
| 3247 | |
| 3248 | exit(-1); |
| 3249 | } |
| 3250 | |
| 3251 | SGPropertyNode *options = options_root.getNode("options"); |
| 3252 | if (!options) { |
| 3253 | SG_LOG( SG_GENERAL, SG_ALERT, |
| 3254 | "Error reading options.xml: <options> element not found." ); |
| 3255 | exit(-1); |
| 3256 | } |
| 3257 | |
| 3258 | if (!locale->loadResource("options")) |
| 3259 | { |
| 3260 | cout << "Unable to read the language resource." << endl; |
| 3261 | exit(-1); |
| 3262 | } |
| 3263 | |
| 3264 | std::string usage = locale->getLocalizedString(options->getStringValue("usage"), "options"); |
| 3265 | if (!usage.empty()) { |
| 3266 | cout << usage << endl; |
| 3267 | } |
| 3268 | |
| 3269 | vector<SGPropertyNode_ptr>section = options->getChildren("section"); |
| 3270 | for (unsigned int j = 0; j < section.size(); j++) { |
| 3271 | string msg = ""; |
| 3272 | |
| 3273 | vector<SGPropertyNode_ptr>option = section[j]->getChildren("option"); |
| 3274 | for (unsigned int k = 0; k < option.size(); k++) { |
| 3275 | |
| 3276 | SGPropertyNode *name = option[k]->getNode("name"); |
| 3277 | SGPropertyNode *short_name = option[k]->getNode("short"); |
| 3278 | SGPropertyNode *key = option[k]->getNode("key"); |
| 3279 | SGPropertyNode *arg = option[k]->getNode("arg"); |
| 3280 | SGPropertyNode *optionalArg = option[k]->getNode("optional-arg"); |
| 3281 | bool brief = option[k]->getNode("brief") != 0; |
| 3282 | |
| 3283 | if ((brief || p->verbose) && name) { |
| 3284 | string tmp = name->getStringValue(); |
| 3285 | |
| 3286 | if (key){ |
| 3287 | tmp.append(":"); |
| 3288 | tmp.append(key->getStringValue()); |
| 3289 | } |
| 3290 | if (arg) { |
| 3291 | tmp.append("="); |
| 3292 | tmp.append(arg->getStringValue()); |
| 3293 | } else if (optionalArg) { |
| 3294 | tmp.append("[="); |
| 3295 | tmp.append(optionalArg->getStringValue()); |
| 3296 | tmp.append("]"); |
| 3297 | } |
| 3298 | |
| 3299 | if (short_name) { |
| 3300 | tmp.append(", -"); |
| 3301 | tmp.append(short_name->getStringValue()); |
| 3302 | } |
| 3303 | |
| 3304 | if (tmp.size() <= 25) { |
| 3305 | msg+= " --"; |
| 3306 | msg += tmp; |
| 3307 | msg.append( 27-tmp.size(), ' '); |
| 3308 | } else { |
| 3309 | msg += "\n --"; |
| 3310 | msg += tmp + '\n'; |
| 3311 | msg.append(32, ' '); |
| 3312 | } |
| 3313 | // There may be more than one <description> tag associated |
| 3314 | // with one option |
| 3315 | |
| 3316 | vector<SGPropertyNode_ptr> desc; |
| 3317 | desc = option[k]->getChildren("description"); |
| 3318 | if (! desc.empty()) { |
| 3319 | for ( unsigned int l = 0; l < desc.size(); l++) { |
| 3320 | string t = desc[l]->getStringValue(); |
| 3321 | |
| 3322 | // There may be more than one translation line. |
| 3323 | vector<SGPropertyNode_ptr>trans_desc = locale->getLocalizedStrings(t.c_str(),"options"); |
| 3324 | for ( unsigned int m = 0; m < trans_desc.size(); m++ ) { |
| 3325 | string t_str = trans_desc[m]->getStringValue(); |
| 3326 | |
| 3327 | if ((m > 0) || ((l > 0) && m == 0)) { |
| 3328 | msg.append( 32, ' '); |
| 3329 | } |
| 3330 | |
| 3331 | // If the string is too large to fit on the screen, |
| 3332 | // then split it up in several pieces. |
| 3333 | |
| 3334 | while ( t_str.size() > 47 ) { |
| 3335 | string::size_type m = t_str.rfind(' ', 47); |
| 3336 | |
| 3337 | if (m == string::npos) { |
| 3338 | m = t_str.find(' '); // fallback: find the first space |
| 3339 | } |
| 3340 | |
| 3341 | if (m == string::npos) { |
| 3342 | // No line wrapping at all. Maybe this is not the best for |
| 3343 | // some languages like Chinese, but at least this will |
| 3344 | // prevent FG from eating all memory. |
| 3345 | break; |
| 3346 | } else { |
| 3347 | msg += t_str.substr(0, m) + '\n'; |
| 3348 | msg.append( 32, ' '); |
| 3349 | t_str.erase(t_str.begin(), t_str.begin() + m + 1); |
| 3350 | } |
| 3351 | } |
| 3352 | msg += t_str + '\n'; |
| 3353 | } |
| 3354 | } |
| 3355 | } |
| 3356 | } |
| 3357 | } |
| 3358 | |
| 3359 | std::string name = locale->getLocalizedString(section[j]->getStringValue("name"),"options"); |
| 3360 | if (!msg.empty() && !name.empty()) { |
| 3361 | cout << endl << name << ":" << endl; |
| 3362 | cout << msg; |
| 3363 | msg.erase(); |
| 3364 | } |
| 3365 | } |
| 3366 | |
| 3367 | if ( !p->verbose ) { |
| 3368 | std::string verbose_help = locale->getLocalizedString(options->getStringValue("verbose-help"),"options"); |
| 3369 | if (!verbose_help.empty()) |
| 3370 | cout << endl << verbose_help << endl; |
| 3371 | } |
| 3372 | #ifdef _MSC_VER |
| 3373 | std::cout << "Hit a key to continue..." << std::endl; |
| 3374 | std::cin.get(); |
| 3375 | #endif |
| 3376 | } |
| 3377 | |
| 3378 | void Options::showVersion() const |
| 3379 | { |
| 3380 | cout << "FlightGear version: " << FLIGHTGEAR_VERSION << endl; |
| 3381 | cout << "Revision: " << REVISION << endl; |
| 3382 | cout << "Build-Id: " << JENKINS_BUILD_ID << endl; |
| 3383 | cout << "Build-Type: " << FG_BUILD_TYPE << endl; |
| 3384 | cout << "FG_ROOT=" << globals->get_fg_root().utf8Str() << endl; |
| 3385 | cout << "FG_HOME=" << globals->get_fg_home().utf8Str() << endl; |
| 3386 | cout << "FG_SCENERY="; |
| 3387 | |
| 3388 | PathList scn = globals->get_fg_scenery(); |
| 3389 | cout << SGPath::join(scn, SGPath::pathListSep) << endl; |
| 3390 | cout << "SimGear version: " << SG_STRINGIZE(SIMGEAR_VERSION) << endl; |
| 3391 | cout << "OSG version: " << osgGetVersion() << endl; |
| 3392 | cout << "PLIB version: " << PLIB_VERSION << endl; |
| 3393 | |
| 3394 | const auto fgRootPath = globals->get_fg_root(); |
| 3395 | cout << "Base Package (FGData) at " << fgRootPath << " is version:" << fgBasePackageVersion(fgRootPath) << endl; |
| 3396 | const auto fgDataInfo = fgBasePackageInfo(fgRootPath); |
| 3397 | if (fgDataInfo) { |
| 3398 | cout << "\tbuilt on " << fgDataInfo.value().buildDate << endl; |
| 3399 | cout << "\tfrom FGData Git revision: " << fgDataInfo.value().gitRevision << endl; |
| 3400 | } |
| 3401 | } |
| 3402 | |
| 3403 | void Options::showInfo() const |
| 3404 | { |
| 3405 | cout << "FlightGear version: " << FLIGHTGEAR_VERSION << endl; |
| 3406 | |
| 3407 | cout << "Sentry.io UUID: " << flightgear::sentryUserId() << endl; |
| 3408 | |
| 3409 | // paths |
| 3410 | cout << "FG_ROOT=" << globals->get_fg_root() << endl; |
| 3411 | cout << "FG_HOME=" << globals->get_fg_home() << endl; |
| 3412 | cout << "FG_SCENERY="; |
| 3413 | PathList scn = globals->get_fg_scenery(); |
| 3414 | cout << SGPath::join(scn, SGPath::pathListSep) << endl; |
| 3415 | |
| 3416 | cout << "Download-directory: " << globals->get_download_dir() << endl; |
| 3417 | cout << "TerraSync-directory: " << globals->get_terrasync_dir() << endl; |
| 3418 | } |
| 3419 | |
| 3420 | // Print a report using JSON syntax on the standard output, encoded in UTF-8. |
| 3421 | // |
| 3422 | // The report format is versioned, don't forget to update it when making |
| 3423 | // changes (see below). |
| 3424 | void Options::printJSONReport() const |
| 3425 | { |
| 3426 | cJSON *rootNode = cJSON_CreateObject(); |
| 3427 | |
| 3428 | cJSON *metaNode = cJSON_CreateObject(); |
| 3429 | cJSON_AddItemToObject(rootNode, "meta", metaNode); |
| 3430 | cJSON_AddStringToObject(metaNode, "type", "FlightGear JSON report"); |
| 3431 | // When making compatible changes to the format (e.g., adding members to |
| 3432 | // JSON objects), only the minor version number should be increased. |
| 3433 | // Increase the major version number when a change is backward-incompatible |
| 3434 | // (such as the removal, renaming or semantic change of a member). Of |
| 3435 | // course, incompatible changes should only be considered as a last |
| 3436 | // recourse. |
| 3437 | cJSON_AddNumberToObject(metaNode, "format major version", 1); |
| 3438 | cJSON_AddNumberToObject(metaNode, "format minor version", 0); |
| 3439 | |
| 3440 | cJSON *generalNode = cJSON_CreateObject(); |
| 3441 | cJSON_AddItemToObject(rootNode, "general", generalNode); |
| 3442 | cJSON_AddStringToObject(generalNode, "name", "FlightGear"); |
| 3443 | cJSON_AddStringToObject(generalNode, "version", FLIGHTGEAR_VERSION); |
| 3444 | cJSON_AddStringToObject(generalNode, "build ID", JENKINS_BUILD_ID); |
| 3445 | cJSON_AddStringToObject(generalNode, "build type", FG_BUILD_TYPE); |
| 3446 | cJSON_AddStringToObject(generalNode, "build revision", REVISION); |
| 3447 | |
| 3448 | cJSON *configNode = cJSON_CreateObject(); |
| 3449 | cJSON_AddItemToObject(rootNode, "config", configNode); |
| 3450 | cJSON_AddStringToObject(configNode, "FG_ROOT", |
| 3451 | globals->get_fg_root().utf8Str().c_str()); |
| 3452 | cJSON_AddStringToObject(configNode, "FG_HOME", |
| 3453 | globals->get_fg_home().utf8Str().c_str()); |
| 3454 | |
| 3455 | cJSON *sceneryPathsNode = p->createJSONArrayFromPathList(globals->get_fg_scenery()); |
| 3456 | cJSON_AddItemToObject(configNode, "scenery paths", sceneryPathsNode); |
| 3457 | |
| 3458 | cJSON *aircraftPathsNode = p->createJSONArrayFromPathList( |
| 3459 | globals->get_aircraft_paths()); |
| 3460 | cJSON_AddItemToObject(configNode, "aircraft paths", aircraftPathsNode); |
| 3461 | |
| 3462 | cJSON_AddStringToObject(configNode, "TerraSync directory", |
| 3463 | globals->get_terrasync_dir().utf8Str().c_str()); |
| 3464 | |
| 3465 | cJSON_AddStringToObject(configNode, "download directory", |
| 3466 | globals->get_download_dir().utf8Str().c_str()); |
| 3467 | |
| 3468 | cJSON_AddStringToObject(configNode, "autosave file", |
| 3469 | globals->autosaveFilePath().utf8Str().c_str()); |
| 3470 | |
| 3471 | const auto sentryUid = sentryUserId(); |
| 3472 | cJSON_AddStringToObject(configNode, "Sentry.io UUID", sentryUid.c_str()); |
| 3473 | |
| 3474 | // Get the ordered lists of apt.dat, fix.dat and nav.dat files used by the |
| 3475 | // NavCache |
| 3476 | NavDataCache* cache = NavDataCache::instance(); |
| 3477 | if (!cache) { |
| 3478 | cache = NavDataCache::createInstance(); |
| 3479 | } |
| 3480 | |
| 3481 | cJSON *navDataNode = cJSON_CreateObject(); |
| 3482 | cJSON_AddItemToObject(rootNode, "navigation data", navDataNode); |
| 3483 | |
| 3484 | // Write each list to the JSON tree |
| 3485 | for (const auto& datType: {NavDataCache::DATFILETYPE_APT, |
| 3486 | NavDataCache::DATFILETYPE_FIX, |
| 3487 | NavDataCache::DATFILETYPE_NAV}) { |
| 3488 | // For this method, it doesn't matter if the cache is out-of-date |
| 3489 | const NavDataCache::DatFilesGroupInfo& datFilesInfo = |
| 3490 | cache->getDatFilesInfo(datType); |
| 3491 | |
| 3492 | // Create a list of SGPath instances (for the .dat files) from the list of |
| 3493 | // NavDataCache::SceneryLocation structs that datFilesInfo.paths is. |
| 3494 | PathList datFiles(datFilesInfo.paths.size()); |
| 3495 | const auto map = [](const auto& e) { return e.datPath; }; |
| 3496 | std::transform(std::cbegin(datFilesInfo.paths), |
| 3497 | std::cend(datFilesInfo.paths), std::begin(datFiles), map); |
| 3498 | |
| 3499 | cJSON *datPathsNode = p->createJSONArrayFromPathList(datFiles); |
| 3500 | string key = NavDataCache::datTypeStr[datType] + ".dat files"; |
| 3501 | cJSON_AddItemToObject(navDataNode, key.c_str(), datPathsNode); |
| 3502 | } |
| 3503 | |
| 3504 | // Print the JSON tree to the standard output |
| 3505 | char *report = cJSON_Print(rootNode); |
| 3506 | cout << report << endl; |
| 3507 | cJSON_Delete(rootNode); |
| 3508 | } |
| 3509 | |
| 3510 | |
| 3511 | SGPath Options::downloadedDataRoot() const |
| 3512 | { |
| 3513 | const auto fgdataDirName = "fgdata_" + std::to_string(FLIGHTGEAR_MAJOR_VERSION) + "_" + std::to_string(FLIGHTGEAR_MINOR_VERSION); |
| 3514 | return actualDownloadDir() / fgdataDirName; |
| 3515 | } |
| 3516 | |
| 3517 | SGPath Options::platformDefaultRoot() const |
| 3518 | { |
| 3519 | return SGPath::fromUtf8(PKGLIBDIR); |
| 3520 | } |
| 3521 | |
| 3522 | string_list Options::extractOptions() const |
| 3523 | { |
| 3524 | string_list result; |
| 3525 | for (auto opt : p->values) { |
| 3526 | if (opt.desc == nullptr) { |
| 3527 | continue; |
| 3528 | } |
| 3529 | |
| 3530 | if (!strcmp(opt.desc->option,"prop")) { |
| 3531 | result.push_back("prop:" + opt.value); |
| 3532 | } else if (opt.value.empty()) { |
| 3533 | result.push_back(opt.desc->option); |
| 3534 | } else { |
| 3535 | result.push_back(std::string(opt.desc->option) + "=" + opt.value); |
| 3536 | } |
| 3537 | } |
| 3538 | |
| 3539 | return result; |
| 3540 | } |
| 3541 | |
| 3542 | bool Options::isFGData(const SGPath& p) |
| 3543 | { |
| 3544 | // check assorted files exist in the root location, to avoid any chance of |
| 3545 | // selecting an incomplete base package. This is probably overkill but does |
| 3546 | // no harm |
| 3547 | const string_list files{ |
| 3548 | "version"s, |
| 3549 | "defaults.xml"s, |
| 3550 | "Materials/base/materials-base.xml"s, |
| 3551 | "gui/menubar.xml"s, |
| 3552 | "Timezone/zone.tab"s}; |
| 3553 | |
| 3554 | if (!p.exists()) { |
| 3555 | return false; |
| 3556 | } |
| 3557 | |
| 3558 | for (const auto& f : files) { |
| 3559 | const auto path = p / f; |
| 3560 | if (!path.exists()) { |
| 3561 | return false; |
| 3562 | } |
| 3563 | } |
| 3564 | |
| 3565 | return true; |
| 3566 | } |
| 3567 | |
| 3568 | OptionResult Options::setupRoot(int argc, char** argv) |
| 3569 | { |
| 3570 | SGPath root(globals->get_fg_root()); |
| 3571 | bool usingDefaultRoot = false; |
| 3572 | |
| 3573 | // root has already been set, so skip the fg_root setting and validation. |
| 3574 | if (!root.isNull()) { |
| 3575 | return FG_OPTIONS_OK; |
| 3576 | } |
| 3577 | |
| 3578 | if (isOptionSet("fg-root")) { |
| 3579 | const auto v = valueForOption("fg-root"); |
| 3580 | if (v == "!ask") { |
| 3581 | #if defined(HAVE_QT) |
| 3582 | bool ok = flightgear::showSetupRootDialog(flightgear::SetupRootReason::ManualChoiceRequested); |
| 3583 | if (!ok) { |
| 3584 | return FG_OPTIONS_EXIT; |
| 3585 | } |
| 3586 | |
| 3587 | root = globals->get_fg_root(); |
| 3588 | #else |
| 3589 | SG_LOG(SG_GENERAL, SG_ALERT, "interactively asking for fg-root requires a build with Qt GUI support."); |
| 3590 | return FG_OPTIONS_EXIT; |
| 3591 | #endif |
| 3592 | } else { |
| 3593 | root = SGPath::fromUtf8(v); // easy! |
| 3594 | SG_LOG(SG_GENERAL, SG_INFO, "set from command-line argument: fg_root = " << root); |
| 3595 | } |
| 3596 | } else { |
| 3597 | // Next check if fg-root is set as an env variable |
| 3598 | char *envp = ::getenv( "FG_ROOT" ); |
| 3599 | if ( envp != nullptr ) { |
| 3600 | root = SGPath::fromEnv("FG_ROOT"); |
| 3601 | SG_LOG(SG_GENERAL, SG_INFO, "set from FG_ROOT env var: fg_root = " << root ); |
| 3602 | } else { |
| 3603 | #if defined(HAVE_QT) |
| 3604 | auto restoreResult = restoreUserSelectedRoot(root); |
| 3605 | if (restoreResult == SetupRootResult::UserExit) { |
| 3606 | return FG_OPTIONS_EXIT; |
| 3607 | } else if (restoreResult == SetupRootResult::ExitDueToReadOnly) { |
| 3608 | flightgear::fatalMessageBoxWithoutExit( |
| 3609 | "Base package not found", |
| 3610 | "The base package data files were not found, and this copy of FlightGear is running in read-only mode.", "", |
| 3611 | false); |
| 3612 | return FG_OPTIONS_EXIT; |
| 3613 | } else if (restoreResult == SetupRootResult::UseDefault) { |
| 3614 | root = SGPath{}; // clear any value, so we use the default location |
| 3615 | } else { |
| 3616 | SG_LOG(SG_GENERAL, SG_INFO, "Qt launcher set fg_root = " << root ); |
| 3617 | } |
| 3618 | #endif |
| 3619 | } |
| 3620 | } |
| 3621 | |
| 3622 | if (root.isNull()) { |
| 3623 | usingDefaultRoot = true; |
| 3624 | root = platformDefaultRoot(); |
| 3625 | if (!isFGData(root)) { |
| 3626 | root = downloadedDataRoot(); |
| 3627 | } |
| 3628 | SG_LOG(SG_GENERAL, SG_INFO, "Using default fg_root = " << root ); |
| 3629 | } |
| 3630 | |
| 3631 | globals->set_fg_root(root); |
| 3632 | string base_version = fgBasePackageVersion(root); |
| 3633 | |
| 3634 | #if defined(HAVE_QT) |
| 3635 | // only compare major and minor version, not the patch level. |
| 3636 | const int versionComp = simgear::strutils::compare_versions(FLIGHTGEAR_VERSION, base_version, 2); |
| 3637 | |
| 3638 | // note we never end up here if restoring a user selected root via |
| 3639 | // the Qt GUI, since that code pre-validates the path. But if we're using |
| 3640 | // a command-line, env-var or default root this check can fail and |
| 3641 | // we still want to use the GUI in that case. |
| 3642 | if (versionComp != 0) { |
| 3643 | // check for read-only mode, since in that case we can't run SetupRootDialog |
| 3644 | // https://gitlab.com/flightgear/flightgear/-/issues/3286 |
| 3645 | const auto readOnly = fgGetBool("/sim/fghome-readonly", false); |
| 3646 | if (readOnly) { |
| 3647 | flightgear::fatalMessageBoxWithoutExit( |
| 3648 | "Base package not found", |
| 3649 | "The base package data files were not found, and this copy of FlightGear is running in read-only mode.", |
| 3650 | "Looking for base-package files at: '" + root.str() + "'", |
| 3651 | false); |
| 3652 | return FG_OPTIONS_EXIT; |
| 3653 | } |
| 3654 | |
| 3655 | const auto r = usingDefaultRoot ? flightgear::SetupRootReason::DefaultRootInvalid |
| 3656 | : flightgear::SetupRootReason::ExplicitRootInvalid; |
| 3657 | bool ok = flightgear::showSetupRootDialog(r); |
| 3658 | if (!ok) { |
| 3659 | return FG_OPTIONS_EXIT; |
| 3660 | } |
| 3661 | } |
| 3662 | #else |
| 3663 | SG_UNUSED(usingDefaultRoot); |
| 3664 | |
| 3665 | // validate it |
| 3666 | if (base_version.empty()) { |
| 3667 | flightgear::fatalMessageBoxThenExit( |
| 3668 | "Base package not found", |
| 3669 | "Required data files not found, please check your installation.", |
| 3670 | "Looking for base-package files at: '" + root.str() + "'"); |
| 3671 | } |
| 3672 | |
| 3673 | // only compare major and minor version, not the patch level. |
| 3674 | const int versionComp = simgear::strutils::compare_versions(FLIGHTGEAR_VERSION, base_version, 2); |
| 3675 | if (versionComp != 0) { |
| 3676 | flightgear::fatalMessageBoxThenExit( |
| 3677 | "Base package version mismatch", |
| 3678 | "Version check failed, please check your installation.", |
| 3679 | "Found data files for version '" + base_version + "' at '" + |
| 3680 | globals->get_fg_root().str() + "', version '" + |
| 3681 | std::string(FLIGHTGEAR_VERSION) + "' is required."); |
| 3682 | } |
| 3683 | #endif |
| 3684 | return FG_OPTIONS_OK; |
| 3685 | } |
| 3686 | |
| 3687 | bool Options::shouldLoadDefaultConfig() const |
| 3688 | { |
| 3689 | return p->shouldLoadDefaultConfig; |
| 3690 | } |
| 3691 | |
| 3692 | void Options::setShouldLoadDefaultConfig(bool load) |
| 3693 | { |
| 3694 | p->shouldLoadDefaultConfig = load; |
| 3695 | } |
| 3696 | |
| 3697 | bool Options::checkForArg(int argc, char* argv[], const char* checkArg) |
| 3698 | { |
| 3699 | for (int i = 0; i < argc; ++i) { |
| 3700 | char* arg = argv[i]; |
| 3701 | if (arg == nullptr) { |
| 3702 | continue; |
| 3703 | } |
| 3704 | |
| 3705 | if (*arg != '-') { // we only care about args with a leading hypen |
| 3706 | continue; |
| 3707 | } |
| 3708 | |
| 3709 | arg++; |
| 3710 | if (*arg == '-') { // skip double hypens |
| 3711 | arg++; |
| 3712 | } |
| 3713 | |
| 3714 | if (strcmp(arg, checkArg) == 0) { |
| 3715 | return true; |
| 3716 | } |
| 3717 | } |
| 3718 | |
| 3719 | return false; |
| 3720 | } |
| 3721 | |
| 3722 | bool Options::checkForEarlyExitArg(int argc, char* argv[]) |
| 3723 | { |
| 3724 | return checkForArg(argc, argv, "help") || |
| 3725 | checkForArg(argc, argv, "version") || |
| 3726 | checkForArg(argc, argv, "info") || |
| 3727 | checkForArg(argc, argv, "show-aircraft") || |
| 3728 | checkForArg(argc, argv, "show-sound-devices") || |
| 3729 | checkForArg(argc, argv, "json-report"); |
| 3730 | } |
| 3731 | |
| 3732 | |
| 3733 | simgear::optional<bool> Options::checkForBoolArg(int argc, char* argv[], const string& checkArg) |
| 3734 | { |
| 3735 | for (int i = 0; i < argc; ++i) { |
| 3736 | char* arg = argv[i]; |
| 3737 | if (arg == nullptr) { |
| 3738 | continue; |
| 3739 | } |
| 3740 | |
| 3741 | if (*arg != '-') { // we only care about args with a leading hypen |
| 3742 | continue; |
| 3743 | } |
| 3744 | |
| 3745 | arg++; |
| 3746 | if (*arg == '-') { // skip double hypens |
| 3747 | arg++; |
| 3748 | } |
| 3749 | |
| 3750 | string option(arg); |
| 3751 | |
| 3752 | if (option == "enable-" + checkArg) { |
| 3753 | return true; // explicitly enabled |
| 3754 | } |
| 3755 | |
| 3756 | if (option == "disable-" + checkArg) { |
| 3757 | return false; // explicitly disabled |
| 3758 | } |
| 3759 | |
| 3760 | simgear::optional<std::string> value; |
| 3761 | const size_t equal = option.find("="); |
| 3762 | if (equal == string::npos) { |
| 3763 | value = getValueFromNextParam(i, argc, argv); |
| 3764 | } |
| 3765 | else { |
| 3766 | value = option.substr(equal + 1); |
| 3767 | option = option.substr(0, equal); |
| 3768 | } |
| 3769 | |
| 3770 | if (option != checkArg) { |
| 3771 | continue; |
| 3772 | } |
| 3773 | |
| 3774 | if (!value.has_value()) { |
| 3775 | return true; // enabled by default |
| 3776 | } |
| 3777 | |
| 3778 | return paramToBool(value.value()) |
| 3779 | ? true // explicitly enabled |
| 3780 | : false; // explicitly disabled |
| 3781 | } |
| 3782 | |
| 3783 | return {}; // option not found |
| 3784 | } |
| 3785 | |
| 3786 | bool Options::checkForArgEnable(int argc, char* argv[], const string& checkArg) |
| 3787 | { |
| 3788 | const simgear::optional<bool> value = checkForBoolArg(argc, argv, checkArg); |
| 3789 | return value.has_value() && value.value() == true; |
| 3790 | } |
| 3791 | |
| 3792 | bool Options::checkForArgDisable(int argc, char* argv[], const string& checkArg) |
| 3793 | { |
| 3794 | const simgear::optional<bool> value = checkForBoolArg(argc, argv, checkArg); |
| 3795 | return value.has_value() && value.value() == false; |
| 3796 | } |
| 3797 | |
| 3798 | std::string Options::getArgValue(int argc, char* argv[], const char* checkArg) |
| 3799 | { |
| 3800 | const auto len = strlen(checkArg); |
| 3801 | for (int i = 0; i < argc; ++i) { |
| 3802 | char* arg = argv[i]; |
| 3803 | if (arg == nullptr) { |
| 3804 | continue; |
| 3805 | } |
| 3806 | |
| 3807 | if (strncmp(arg, checkArg, len) == 0) { |
| 3808 | const auto alen = strlen(arg); |
| 3809 | if ((alen - len) < 2) { |
| 3810 | // no value after the =, or missing = entirely |
| 3811 | simgear::optional<std::string> value = getValueFromNextParam(i, argc, argv); |
| 3812 | return value.has_value() ? value.value() : ""; |
| 3813 | } |
| 3814 | |
| 3815 | return std::string(arg + len + 1); |
| 3816 | } |
| 3817 | } // of args iteration |
| 3818 | |
| 3819 | return {}; |
| 3820 | } |
| 3821 | |
| 3822 | } // of namespace flightgear |