| 1 | // This program is free software; you can redistribute it and/or |
| 2 | // modify it under the terms of the GNU General Public License as |
| 3 | // published by the Free Software Foundation; either version 2 of the |
| 4 | // License, or (at your option) any later version. |
| 5 | // |
| 6 | // This program is distributed in the hope that it will be useful, but |
| 7 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 9 | // General Public License for more details. |
| 10 | // |
| 11 | // You should have received a copy of the GNU General Public License |
| 12 | // along with this program; if not, write to the Free Software |
| 13 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 14 | // |
| 15 | |
| 16 | #ifdef HAVE_CONFIG_H |
| 17 | # include "config.h" |
| 18 | #endif |
| 19 | |
| 20 | #include <cstdio> |
| 21 | #include <utility> |
| 22 | |
| 23 | #include <simgear/misc/sg_path.hxx> |
| 24 | #include <simgear/xml/easyxml.hxx> |
| 25 | #include <simgear/misc/strutils.hxx> |
| 26 | #include <simgear/timing/timestamp.hxx> |
| 27 | |
| 28 | #include <Main/globals.hxx> |
| 29 | #include <Main/fg_props.hxx> |
| 30 | |
| 31 | #include "xmlloader.hxx" |
| 32 | #include "dynamicloader.hxx" |
| 33 | #include "runwayprefloader.hxx" |
| 34 | |
| 35 | #include "dynamics.hxx" |
| 36 | #include "airport.hxx" |
| 37 | #include "runwayprefs.hxx" |
| 38 | |
| 39 | #include <Navaids/NavDataCache.hxx> |
| 40 | #include <Main/sentryIntegration.hxx> |
| 41 | |
| 42 | using std::string; |
| 43 | |
| 44 | XMLLoader::XMLLoader() {} |
| 45 | XMLLoader::~XMLLoader() {} |
| 46 | |
| 47 | void XMLLoader::load(FGGroundNetwork* net) |
| 48 | { |
| 49 | SGPath path; |
| 50 | if (!findAirportData(net->airport()->ident(), "groundnet", path)) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | SG_LOG(SG_NAVAID, SG_DEBUG, "reading groundnet data from " << path); |
| 55 | SGTimeStamp t; |
| 56 | t.stamp(); |
| 57 | try { |
| 58 | FGGroundNetXMLLoader visitor(net); |
| 59 | readXML(path, visitor); |
| 60 | |
| 61 | if (visitor.hasErrors() && fgGetBool("/sim/terrasync/enabled")) { |
| 62 | flightgear::updateSentryTag("ground-net", net->airport()->ident()); |
| 63 | |
| 64 | // disabled for 2024.1.4 to reduce Sentry quota spam |
| 65 | //flightgear::sentryReportException("Ground-net load error", path.utf8Str()); |
| 66 | } |
| 67 | } catch (sg_exception& e) { |
| 68 | SG_LOG(SG_NAVAID, SG_DEV_WARN, "parsing groundnet XML failed:" << e.getFormattedMessage()); |
| 69 | } |
| 70 | |
| 71 | SG_LOG(SG_NAVAID, SG_DEBUG, "parsing groundnet XML took " << t.elapsedMSec()); |
| 72 | } |
| 73 | |
| 74 | void XMLLoader::loadFromStream(FGGroundNetwork* net, std::istream& inData) |
| 75 | { |
| 76 | try { |
| 77 | FGGroundNetXMLLoader visitor(net); |
| 78 | readXML(inData, visitor); |
| 79 | |
| 80 | if (visitor.hasErrors()) { |
| 81 | // disabled for 2024.1.4 to reduce Sentry quota spam |
| 82 | // flightgear::sentryReportException("Ground-net load error", {}); |
| 83 | } |
| 84 | } catch (sg_exception& e) { |
| 85 | SG_LOG(SG_NAVAID, SG_DEV_WARN, "parsing groundnet XML failed:" << e.getFormattedMessage()); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void XMLLoader::loadFromPath(FGGroundNetwork* net, const SGPath& path) |
| 90 | { |
| 91 | try { |
| 92 | FGGroundNetXMLLoader visitor(net); |
| 93 | readXML(path, visitor); |
| 94 | |
| 95 | if (visitor.hasErrors() && fgGetBool("/sim/terrasync/enabled")) { |
| 96 | flightgear::updateSentryTag("ground-net", net->airport()->ident()); |
| 97 | // disabled for 2024.1.4 to reduce Sentry quota spam |
| 98 | // flightgear::sentryReportException("Ground-net load error", path.utf8Str()); |
| 99 | } |
| 100 | } catch (sg_exception& e) { |
| 101 | SG_LOG(SG_NAVAID, SG_DEV_WARN, "parsing groundnet XML failed:" << e.getFormattedMessage()); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void XMLLoader::load(FGRunwayPreference* p) { |
| 106 | FGRunwayPreferenceXMLLoader visitor(p); |
| 107 | loadAirportXMLDataIntoVisitor(p->getId(), "rwyuse", visitor); |
| 108 | } |
| 109 | |
| 110 | bool XMLLoader::findAirportData(const std::string& aICAO, |
| 111 | const std::string& aFileName, SGPath& aPath) |
| 112 | { |
| 113 | FGAirportRef airport = FGAirport::findByIdent(aICAO); |
| 114 | if (!airport) { |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | string fileName(aFileName); |
| 119 | if (!simgear::strutils::ends_with(aFileName, ".xml")) { |
| 120 | fileName.append(".xml"); |
| 121 | } |
| 122 | |
| 123 | const bool performFullTraversal = airport->sceneryPath().isNull() || |
| 124 | !fileName.compare("procedures.xml"); |
| 125 | |
| 126 | PathList sc = globals->get_fg_scenery(); |
| 127 | char buffer[128]; |
| 128 | ::snprintf(buffer, 128, "%c/%c/%c/%s.%s", |
| 129 | aICAO[0], aICAO[1], aICAO[2], |
| 130 | aICAO.c_str(), fileName.c_str()); |
| 131 | |
| 132 | for (PathList::const_iterator it = sc.begin(); it != sc.end(); ++it) { |
| 133 | // fg_senery contains empty strings as "markers" (see FGGlobals::set_fg_scenery) |
| 134 | if (!it->isNull()) { |
| 135 | const SGPath path = *it / "Airports" / buffer; |
| 136 | if (path.exists()) { |
| 137 | aPath = std::move(path); |
| 138 | return true; |
| 139 | } // of path exists |
| 140 | |
| 141 | // Unless we are in “full traversal mode”, don't look in scenery paths |
| 142 | // that come after the one which contributed the apt.dat file for the |
| 143 | // airport. |
| 144 | if (!performFullTraversal && *it == airport->sceneryPath()) { |
| 145 | return false; |
| 146 | } |
| 147 | } |
| 148 | } // of scenery path iteration |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | bool XMLLoader::loadAirportXMLDataIntoVisitor(const string& aICAO, |
| 153 | const string& aFileName, XMLVisitor& aVisitor) |
| 154 | { |
| 155 | SGPath path; |
| 156 | if (!findAirportData(aICAO, aFileName, path)) { |
| 157 | SG_LOG(SG_NAVAID, SG_DEBUG, "loadAirportXMLDataIntoVisitor: failed to find data for " << aICAO << "/" << aFileName); |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | bool readXMLOk = true; |
| 162 | try { |
| 163 | SG_LOG(SG_NAVAID, SG_DEBUG, "loadAirportXMLDataIntoVisitor: loading from " << path); |
| 164 | readXML(path, aVisitor); |
| 165 | } catch (sg_exception& e) { |
| 166 | readXMLOk = false; |
| 167 | SG_LOG(SG_NAVAID, SG_WARN, "XML errors trying to read:" << path); |
| 168 | } |
| 169 | |
| 170 | return readXMLOk; |
| 171 | } |
| 172 |