-
Notifications
You must be signed in to change notification settings - Fork 13
/
navdatareader.cpp
260 lines (212 loc) · 9.69 KB
/
navdatareader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*****************************************************************************
* Copyright 2015-2022 Alexander Barthel [email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#include "navdatareader.h"
#include "atools.h"
#include "exception.h"
#include "fs/navdatabase.h"
#include "fs/navdatabaseerrors.h"
#include "logging/logginghandler.h"
#include "logging/loggingutil.h"
#include "settings/settings.h"
#include <QCommandLineParser>
#include <QSettings>
using atools::sql::SqlDatabase;
using atools::fs::NavDatabaseErrors;
using atools::fs::FsPaths;
void NavdataReader::run()
{
atools::logging::LoggingHandler::initialize(atools::settings::Settings::getOverloadedLocalPath(
":/navdatareader/resources/config/logging.cfg"));
// Load simulator paths =================================
FsPaths::loadAllPaths();
// Fill NavDatabaseOptions from command line
parseArgs();
if(!opts.getSourceDatabase().isEmpty())
qInfo() << "===== Input Database " << QFileInfo(opts.getSourceDatabase()).absoluteFilePath() << "=====";
qInfo() << "===== Output Database" << QFileInfo(db.databaseName()).absoluteFilePath() << "=====";
qInfo() << "===== Using source data type" << FsPaths::typeToShortName(type) << "=====";
qInfo() << "===== Configuration" << configFile << "=====";
qInfo() << "This software is licensed under the GPL3 or any later version.";
qInfo() << "See http://www.gnu.org/licenses/gpl-3.0 for more information.";
qInfo() << "The source code for this application is available at https://github.com/albar965";
qInfo() << "Copyright 2015-2023 Alexander Barthel ([email protected]).";
qInfo().nospace().noquote() << "Version " << QCoreApplication::applicationVersion()
<< " (revision " << GIT_REVISION_NAVDATAREADER << ")";
qInfo().nospace().noquote() << "atools Version " << atools::version()
<< " (revision " << atools::gitRevision() << ")";
using atools::logging::LoggingUtil;
LoggingUtil::logSystemInformation();
LoggingUtil::logStandardPaths();
FsPaths::logAllPaths();
qInfo() << opts;
QStringList DATABASE_PRAGMAS({QString("PRAGMA cache_size=-%1").arg(50000),
"PRAGMA synchronous=OFF",
"PRAGMA journal_mode=TRUNCATE",
"PRAGMA page_size=8196"
/*, "PRAGMA locking_mode=EXCLUSIVE"*/});
// FKs don't work currently
db.open(DATABASE_PRAGMAS);
atools::fs::NavDatabaseErrors errors;
atools::fs::NavDatabase nd(&opts, &db, &errors, GIT_REVISION_NAVDATAREADER);
resultFlags = nd.compileDatabase();
db.close();
if(errors.getTotalErrors() > 0)
{
qWarning() << "==================================================================";
qWarning() << "== FOUND ERRORS ==================================================";
for(const NavDatabaseErrors::SceneryErrors& errs : errors.sceneryErrors)
{
qWarning() << "Error in scenery" << errs.scenery;
for(const NavDatabaseErrors::SceneryFileError& err : errs.fileErrors)
qWarning() << "Error in file" << err.filepath << "line" << err.lineNum << ":" << err.errorMessage;
for(const QString& err : errs.sceneryErrorsMessages)
qWarning() << "Other error:" << err;
}
throw atools::Exception("Found errors while compiling. Check log file.");
}
}
QString NavdataReader::getDatabaseName() const
{
return db.databaseName();
}
void NavdataReader::parseArgs()
{
// Build command line option parser ===================================================
QCommandLineParser parser;
parser.setApplicationDescription("Flight Simulator Navdata Database Reader.\n"
"This software is licensed under the GPL3 or any later version.");
parser.addHelpOption();
parser.addVersionOption();
QCommandLineOption fstypeOpt({"f", "flight-simulator"},
QObject::tr("Required option. Flight simulator type <simulator> or other data source. "
"Either FSX, FSXSE, P3DV2, P3DV3, P3DV4, P3DV5, P3DV6, XP11, XP12, MSFS or DFD."),
QObject::tr("simulator"));
parser.addOption(fstypeOpt);
QCommandLineOption sceneryOpt({"s", "scenery"},
QObject::tr("Scenery.cfg file <scenery> for FSX and P3D. "
"Determined by registry entries if not given."),
QObject::tr("scenery"));
parser.addOption(sceneryOpt);
QCommandLineOption sourceDbOpt({"d", "source-database"},
QObject::tr("Required option for DFD compilation. "
"Source database <source-database> for DFD based compilation."),
QObject::tr("source-database"));
parser.addOption(sourceDbOpt);
QCommandLineOption basepathOpt({"b", "basepath"},
QObject::tr("Flight simulator basepath for BGL files <basepath>. "
"Determined by registry entries if not given."),
QObject::tr("basepath"));
parser.addOption(basepathOpt);
QCommandLineOption databaseOpt({"o", "output"},
QObject::tr("Output Sqlite database filename <output> "
"Default is \"navdata.sqlite\"."),
QObject::tr("output"),
"navdata.sqlite");
parser.addOption(databaseOpt);
QCommandLineOption cfgOpt({"c", "config"},
QObject::tr("Configuration file <config> for %1. "
"Default is to use integrated \"navdatareader.cfg\".").
arg(QCoreApplication::applicationName()),
QObject::tr("config"));
parser.addOption(cfgOpt);
// Process the actual command line arguments given by the user
parser.process(*QCoreApplication::instance());
// Simulator Type ===================================================
if(parser.isSet(fstypeOpt))
{
type = FsPaths::stringToType(parser.value(fstypeOpt).toUpper());
opts.setSimulatorType(type);
}
if(type == FsPaths::NONE)
{
qCritical().noquote().nospace() << "*** ERROR: Unknown type for option -f." << endl;
parser.showHelp(1);
}
// Check required options ===================================================
if(!parser.isSet(fstypeOpt))
{
qCritical().noquote().nospace() << "*** ERROR: No database type given." << endl;
parser.showHelp(1);
}
if(FsPaths::isAnyXplane(type) && !parser.isSet(basepathOpt))
{
qCritical().noquote().nospace() << "*** ERROR: No base path for X-Plane given." << endl;
parser.showHelp(1);
}
if(type == FsPaths::DFD && !parser.isSet(sourceDbOpt))
{
qCritical().noquote().nospace() << "*** ERROR: No DFD database given." << endl;
parser.showHelp(1);
}
// Base path ===================================================
if(type != FsPaths::DFD)
{
QString basepath = parser.value(basepathOpt);
if(basepath.isEmpty())
basepath = FsPaths::getBasePath(type);
if(!atools::checkDir(Q_FUNC_INFO, basepath))
exit(1);
opts.setBasepath(basepath);
}
// Source database ===================================================
if(type == FsPaths::DFD)
{
QString dbFile = parser.value(sourceDbOpt);
if(!atools::checkFile(Q_FUNC_INFO, dbFile))
exit(1);
opts.setSourceDatabase(dbFile);
}
// Scenery.cfg only FSX and P3D ===================================================
if(!FsPaths::isAnyXplane(type) && type != FsPaths::DFD && type != FsPaths::MSFS)
{
QString sceneryFile = parser.value(sceneryOpt);
if(sceneryFile.isEmpty())
sceneryFile = FsPaths::getSceneryLibraryPath(type);
if(!atools::checkFile(Q_FUNC_INFO, sceneryFile))
exit(1);
opts.setSceneryFile(sceneryFile);
}
if(type == FsPaths::MSFS)
{
opts.setMsfsCommunityPath(FsPaths::getMsfsCommunityPath(opts.getBasepath()));
opts.setMsfsOfficialPath(FsPaths::getMsfsOfficialPath(opts.getBasepath()));
}
// Configuration file ===================================================
configFile = parser.value(cfgOpt);
if(configFile.isEmpty())
// Command line overrides resource settings file
configFile = ":/navdatareader/resources/config/navdatareader.cfg";
if(!atools::checkFile(Q_FUNC_INFO, configFile))
exit(1);
QSettings settings(configFile, QSettings::IniFormat);
opts.loadFromSettings(settings);
opts.setCallDefaultCallback(true);
// Create database ===================================================
db = SqlDatabase(settings, "Database");
QString databaseName = parser.value(databaseOpt);
if(!databaseName.isEmpty())
{
// Remove any stale files
QFileInfo file(databaseName);
if(file.exists() && file.isFile())
{
if(QFile::remove(databaseName))
qInfo() << "Removed" << databaseName;
}
db.setDatabaseName(databaseName);
}
}