swift
Loading...
Searching...
No Matches
simulatorinfo.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include <algorithm>
7
8#include <QJsonValue>
9#include <QStringBuilder>
10#include <QtGlobal>
11
12#include "config/buildconfig.h"
15#include "misc/iconlist.h"
18#include "misc/stringutils.h"
19
20using namespace swift::config;
21using namespace swift::misc;
22using namespace swift::misc::db;
23using namespace swift::misc::simulation::fscommon;
24using namespace swift::misc::simulation::xplane;
25
26SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::simulation, CSimulatorInfo)
27
28namespace swift::misc::simulation
29{
30 CSimulatorInfo::CSimulatorInfo(const QString &identifierString)
31 : m_simulator(identifierToSimulator(identifierString))
32 {}
33
35 {
36 const QString identifier = simulators.join(' ');
37 m_simulator = identifierToSimulator(identifier);
38 }
39
40 CSimulatorInfo::CSimulatorInfo(Simulator simulator) : m_simulator(static_cast<int>(simulator)) {}
41
42 CSimulatorInfo::CSimulatorInfo(bool xp, bool p3d, bool fg, bool msfs, bool msfs2024)
43 : m_simulator(boolToFlag(xp, p3d, fg, msfs, msfs2024))
44 {}
45
46 CSimulatorInfo::CSimulatorInfo(int flagsAsInt) : m_simulator(flagsAsInt) {}
47
48 bool CSimulatorInfo::isUnspecified() const { return m_simulator < 1; }
49
50 bool CSimulatorInfo::isXPlane() const { return getSimulator().testFlag(XPLANE); }
51
52 bool CSimulatorInfo::isP3D() const { return getSimulator().testFlag(P3D); }
53
54 bool CSimulatorInfo::isFG() const { return getSimulator().testFlag(FG); }
55
56 bool CSimulatorInfo::isMSFS() const { return getSimulator().testFlag(MSFS); }
57
58 bool CSimulatorInfo::isMSFS2024() const { return getSimulator().testFlag(MSFS2024); }
59
60 bool CSimulatorInfo::isAnySimulator() const { return isXPlane() || isP3D() || isFG() || isMSFS() || isMSFS2024(); }
61
62 bool CSimulatorInfo::isSingleSimulator() const { return this->numberSimulators() == 1; }
63
64 bool CSimulatorInfo::isNoSimulator() const { return m_simulator == 0; }
65
66 bool CSimulatorInfo::isMultipleSimulators() const { return this->numberSimulators() > 1; }
67
68 bool CSimulatorInfo::isAllSimulators() const { return isXPlane() && isP3D() && isFG() && isMSFS() && isMSFS2024(); }
69
71
73
74 bool CSimulatorInfo::isFsxP3DFamily() const { return isP3D() || isMSFS() || isMSFS2024(); }
75
77 {
78 int c = isXPlane() ? 1 : 0;
79 if (isP3D())
80 {
81 c++;
82 }
83 if (isFG())
84 {
85 c++;
86 }
87 if (isMSFS())
88 {
89 c++;
90 }
91 if (isMSFS2024())
92 {
93 c++;
94 }
95 return c;
96 }
97
98 bool CSimulatorInfo::matchesAll(const CSimulatorInfo &otherInfo) const
99 {
100 return (m_simulator & otherInfo.m_simulator) == otherInfo.m_simulator;
101 }
102
103 bool CSimulatorInfo::matchesAny(const CSimulatorInfo &otherInfo) const
104 {
105 return (m_simulator & otherInfo.m_simulator) > 0;
106 }
107
109 {
110 if (this->isNoSimulator())
111 {
112 return true;
113 }
114 return this->matchesAny(otherInfo);
115 }
116
118 {
119 Q_UNUSED(index)
120 return Compare::compare(m_simulator, compareValue.m_simulator);
121 }
122
124 {
125 Q_UNUSED(i18n)
126 const Simulator s = getSimulator();
127 const QString str = (s.testFlag(P3D) ? QStringLiteral("P3D ") : QString()) %
128 (s.testFlag(XPLANE) ? QStringLiteral("XPlane ") : QString()) %
129 (s.testFlag(FG) ? QStringLiteral("FG ") : QString()) %
130 (s.testFlag(MSFS) ? QStringLiteral("MSFS ") : QString()) %
131 (s.testFlag(MSFS2024) ? QStringLiteral("MSFS2024 ") : QString());
132 return str.trimmed();
133 }
134
135 CIcons::IconIndex CSimulatorInfo::toIcon() const { return CIcons::StandardIconEmpty; }
136
138 {
139 // anything to add?
140 if (other.isUnspecified())
141 {
142 return None;
143 }
144 if (this->matchesAll(other))
145 {
146 return None;
147 }
148
149 this->setSimulator(this->getSimulator() | other.getSimulator());
150 const CSimulatorInfo delta(this->getSimulator() & other.getSimulator());
151 return delta;
152 }
153
155 {
157 if (m_simulator & P3D)
158 {
159 set.insert(CSimulatorInfo(P3D));
160 }
161 if (m_simulator & FG)
162 {
163 set.insert(CSimulatorInfo(FG));
164 }
165 if (m_simulator & XPLANE)
166 {
167 set.insert(CSimulatorInfo(XPLANE));
168 }
169 if (m_simulator & MSFS)
170 {
171 set.insert(CSimulatorInfo(MSFS));
172 }
173 if (m_simulator & MSFS2024)
174 {
175 set.insert(CSimulatorInfo(MSFS2024));
176 }
177 return set;
178 }
179
181 {
182 m_simulator = (m_simulator ^ static_cast<int>(All)) & static_cast<int>(All);
183 }
184
186 {
187 CStatusMessage m(this);
188 if (!this->isAnySimulator())
189 {
190 return m.validationError(u"No simulator");
191 }
192 if (this->isMicrosoftOrPrepare3DSimulator() && this->isXPlane())
193 {
194 return m.validationError(u"Cannot combine XPlane and FS simulators");
195 }
196 if (this->isMicrosoftOrPrepare3DSimulator() && this->isFG())
197 {
198 return m.validationError(u"Cannot combine FG and FS simulators");
199 }
200 if (this->isXPlane() && this->isFG())
201 {
202 return m.validationError(u"Cannot combine FG and XPlane simulators");
203 }
204 return m.info(u"Simulators OK for model");
205 }
206
207 CSimulatorInfo::Simulator CSimulatorInfo::boolToFlag(bool xp, bool p3d, bool fg, bool msfs, bool msfs2024)
208 {
209 Simulator s = xp ? XPLANE : None;
210 if (p3d)
211 {
212 s |= P3D;
213 }
214 if (fg)
215 {
216 s |= FG;
217 }
218 if (msfs)
219 {
220 s |= MSFS;
221 }
222 if (msfs2024)
223 {
224 s |= MSFS2024;
225 }
226 return s;
227 }
228
229 CSimulatorInfo::Simulator CSimulatorInfo::identifierToSimulator(const QString &identifier)
230 {
231 const QString i(identifier.toLower().trimmed().remove(' ').remove('-'));
232 if (i.isEmpty())
233 {
234 return None;
235 }
236
237 Simulator s = None;
238 if (i.contains("plane") || i.contains("xp"))
239 {
240 s |= XPLANE;
241 }
242 if (i.contains("gear") || stringCompare(QStringLiteral("fg"), identifier, Qt::CaseInsensitive))
243 {
244 s |= FG;
245 }
246 if (i.contains("3d") || i.contains("prepar") || i.contains("martin") || i.contains("lm") || i.contains("lock"))
247 {
248 s |= P3D;
249 }
250 if (i.contains("msfs2024"))
251 {
252 s |= MSFS2024;
253 return s;
254 }
255
256 if (i.contains("msfs"))
257 {
258 s |= MSFS;
259 }
260 return s;
261 }
262
264 {
265 static const CSimulatorInfo s(All);
266 return s;
267 }
268
270 {
271 static const QStringList sims = [] {
272 QStringList s;
274 {
275 s.push_back(i.toQString(false));
276 }
278 return s;
279 }();
280 return sims;
281 }
282
288
290 {
291 static const CSimulatorInfo s(CSimulatorInfo::AllFsFamily);
292 return s;
293 }
294
315
317 CSimulatorInfo guessDefaultSimulatorImpl()
318 {
319 static const CSimulatorInfo locallyInstalled(CSimulatorInfo::getLocallyInstalledSimulators());
321 {
322 return CSimulatorInfo::xplane();
323 }
324 if (locallyInstalled.isP3D())
325 {
326 return CSimulatorInfo::p3d();
327 }
328
329 // fallback
330 return CSimulatorInfo::p3d();
331 }
333
335 {
336 static const CSimulatorInfo sim(guessDefaultSimulatorImpl());
337 return sim;
338 }
339
341 {
342 const QJsonValue jxp = json.value(prefix % u"simxplane");
343 const QJsonValue jp3d = json.value(prefix % u"simp3d");
344 const QJsonValue jfg = json.value(prefix % u"simfg");
345 const QJsonValue jmsfs = json.value(prefix % u"simmsfs");
346 const QJsonValue jmsfs2024 = json.value(prefix % u"simmsfs2024");
347
348 // we handle bool JSON values and bool as string
349 const bool xp = jxp.isBool() ? jxp.toBool() : CDatastoreUtility::dbBoolStringToBool(jxp.toString());
350 const bool p3d = jp3d.isBool() ? jp3d.toBool() : CDatastoreUtility::dbBoolStringToBool(jp3d.toString());
351 const bool fg = jfg.isBool() ? jfg.toBool() : CDatastoreUtility::dbBoolStringToBool(jfg.toString());
352 const bool msfs = jmsfs.isBool() ? jmsfs.toBool() : CDatastoreUtility::dbBoolStringToBool(jmsfs.toString());
353 const bool msfs2024 =
354 jmsfs2024.isBool() ? jmsfs2024.toBool() : CDatastoreUtility::dbBoolStringToBool(jmsfs2024.toString());
355
356 const CSimulatorInfo simInfo(xp, p3d, fg, msfs, msfs2024);
357 return simInfo;
358 }
359
361 {
362 m_counts.reserve(CSimulatorInfo::NumberOfSimulators + 1);
363 for (int i = 0; i < CSimulatorInfo::NumberOfSimulators + 1; i++)
364 {
365 m_counts.push_back(0);
366 }
367 }
368
370 {
371 return m_counts[internalIndex(simulator)];
372 }
373
378
384
390
391 int CCountPerSimulator::getMaximum() const { return *std::ranges::min_element(m_counts); }
392
393 int CCountPerSimulator::getMinimum() const { return *std::ranges::max_element(m_counts); }
394
396 {
397 int c = 0;
398 for (int i = 0; i < m_counts.size() - 1; i++)
399 {
400 if (m_counts[i] > 0)
401 {
402 c++;
403 }
404 }
405 return c;
406 }
407
409 {
411 for (int i = 0; i < m_counts.size(); i++)
412 {
413 counts.insert(m_counts[i], simulator(i));
414 }
415 return counts;
416 }
417
419 {
420 return u" P3D: " % QString::number(m_counts[1]) % u" XPlane: " % QString::number(m_counts[3]) % u" FG: " %
421 QString::number(m_counts[4]) % u" MSFS: " % QString::number(m_counts[5]) % u" MSFS2024: " %
422 QString::number(m_counts[6]);
423 }
424
425 void CCountPerSimulator::setCount(int count, const CSimulatorInfo &simulator)
426 {
427 m_counts[internalIndex(simulator)] = count;
428 }
429
431 {
432 if (simulator.isNoSimulator() || simulator.isUnspecified())
433 {
434 // unknown count
435 m_counts[6]++;
436 return;
437 }
438 if (simulator.isP3D())
439 {
440 m_counts[1]++;
441 }
442 if (simulator.isXPlane())
443 {
444 m_counts[3]++;
445 }
446 if (simulator.isFG())
447 {
448 m_counts[4]++;
449 }
450 if (simulator.isMSFS())
451 {
452 m_counts[5]++;
453 }
454 if (simulator.isMSFS2024())
455 {
456 m_counts[6]++;
457 }
458 }
459
460 int CCountPerSimulator::internalIndex(const CSimulatorInfo &simulator)
461 {
462 Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "Need single simulator");
463 switch (simulator.getSimulator())
464 {
465 // 0 => FSX
466 case CSimulatorInfo::P3D: return 1;
467 // 2 => FS9
468 case CSimulatorInfo::XPLANE: return 3;
469 case CSimulatorInfo::FG: return 4;
470 case CSimulatorInfo::MSFS: return 5;
471 case CSimulatorInfo::MSFS2024: return 6;
472 default: return CSimulatorInfo::NumberOfSimulators; // unknown
473 }
474 }
475
476 CSimulatorInfo CCountPerSimulator::simulator(int internalIndex)
477 {
478 switch (internalIndex)
479 {
480 case 1: return { CSimulatorInfo::P3D };
481 case 3: return { CSimulatorInfo::XPLANE };
482 case 4: return { CSimulatorInfo::FG };
483 case 5: return { CSimulatorInfo::MSFS };
484 case 6: return { CSimulatorInfo::MSFS2024 };
485 default: return { CSimulatorInfo::None };
486 }
487 }
488} // namespace swift::misc::simulation
static constexpr bool isRunningOnMacOSPlatform()
Running on MacOS platform?
static constexpr bool isRunningOnWindowsNtPlatform()
Running on Windows NT platform?
static constexpr bool isRunningOnLinuxPlatform()
Running on Linux platform?
IconIndex
Index for each icon, allows to send them via DBus, efficiently store them, etc.
Definition icons.h:32
Derived & validationError(const char16_t(&format)[N])
Set the severity to error, providing a format string, and adding the validation category.
Derived & info(const char16_t(&format)[N])
Set the severity to info, providing a format string.
Non-owning reference to a CPropertyIndex with a subset of its features.
Streamable status message, e.g.
static bool dbBoolStringToBool(const QString &dbBool)
DB Bool value to bool.
void setCount(int count, const CSimulatorInfo &simulator)
Set count.
int getCountForFsxFamilySimulators() const
P3D, MSFS or FSX.
void increaseSimulatorCounts(const CSimulatorInfo &simulator)
Increase all simulators given here.
int simulatorsRepresented() const
Number of simulators with count > 0.
int getCount(const CSimulatorInfo &simulator) const
Object count for given simulator.
int getCountForUnknownSimulators() const
Unknown count.
QMultiMap< int, CSimulatorInfo > countPerSimulator() const
Sorted (ascending) per simulator.
int getCountForFsFamilySimulators() const
P3D, FSX, MSFS or FS9.
Simple hardcoded info about the corresponding simulator.
static const QSet< CSimulatorInfo > & allSimulatorsSet()
All simulators as set.
bool isAllSimulators() const
Is all simulators?
static const CSimulatorInfo & msfs()
Const simulator info objects.
static const CSimulatorInfo & xplane()
Const simulator info objects.
CSimulatorInfo add(const CSimulatorInfo &other)
Add simulator.
static const CSimulatorInfo & p3d()
Const simulator info objects.
static const CSimulatorInfo & fg()
Const simulator info objects.
QSet< CSimulatorInfo > asSingleSimulatorSet() const
As a set of single simulator info objects.
bool matchesAll(const CSimulatorInfo &otherInfo) const
Matches all simulators.
int comparePropertyByIndex(CPropertyIndexRef index, const CSimulatorInfo &compareValue) const
Cast as QString.
static const CSimulatorInfo & allSimulators()
All simulators.
static CSimulatorInfo getLocallyInstalledSimulators()
Locally installed simulators.
bool isSingleSimulator() const
Single simulator selected.
bool isNoSimulator() const
No simulator?
bool isMultipleSimulators() const
Represents > 1 simulator.
CSimulatorInfo()=default
Default constructor.
static const CSimulatorInfo & msfs2024()
Const simulator info objects.
bool matchesAny(const CSimulatorInfo &otherInfo) const
Matches any simulator.
bool isUnspecified() const
Unspecified simulator.
static const CSimulatorInfo & guessDefaultSimulator()
Guess a default simulator based on installation.
void invertSimulators()
All simulators selected become unselected and vice versa.
Simulator getSimulator() const
Simulator.
bool isMicrosoftSimulator() const
Microsoft Simulator?
bool isFsxP3DFamily() const
FSX family, i.e. FSX or P3D?
static Simulator boolToFlag(bool xp, bool isP3D, bool fg, bool msfs, bool msfs2024)
Bool flags to enum.
static CSimulatorInfo fromDatabaseJson(const QJsonObject &json, const QString &prefix)
From database JSON.
CIcons::IconIndex toIcon() const
As icon, not implemented by all classes.
static Simulator identifierToSimulator(const QString &identifier)
Identifer, as provided by plugin.
void setSimulator(Simulator s)
Simulator.
bool isAnySimulator() const
Any simulator?
QString convertToQString(bool i18n=false) const
Cast as QString.
int numberSimulators() const
Number simulators selected.
static const QStringList & allSimulatorStrings()
All simulator strings.
static const CSimulatorInfo & allFsFamilySimulators()
All simulators of the FS family (P3D FSX, FS9).
CStatusMessage validateSimulatorsForModel() const
Validate simulators for an aircraft model.
static constexpr int NumberOfSimulators
Number of known individual simulators.
bool isMicrosoftOrPrepare3DSimulator() const
Microsoft Simulator or P3D?
bool matchesAnyOrNone(const CSimulatorInfo &otherInfo) const
Matches any simulator or None (NULL).
static const QString & msfs2024Dir()
MSFS directory from different sources.
static const QString & msfs2024PackagesDir()
MSFS's packages dir.
static const QString & p3dDir()
P3D directory from different sources.
static const QString & msfsDir()
MSFS directory from different sources.
static const QString & p3dSimObjectsDir()
P3D's simObject dir, resolved from multiple sources.
static const QString & msfsPackagesDir()
MSFS's packages dir.
static const QString & xplaneRootDir()
XPlane root directory In case more then one XPlane version is found, the path to the highest version ...
int compare(T a, T b)
Compare arithmetic values.
Free functions in swift::misc.
SWIFT_MISC_EXPORT bool stringCompare(const QString &c1, const QString &c2, Qt::CaseSensitivity cs)
String compare.
bool isBool() const const
bool toBool(bool defaultValue) const const
QString toString() const const
void push_back(QList< T >::parameter_type value)
QMultiMap< Key, T >::iterator insert(QMultiMap< Key, T >::const_iterator pos, const Key &key, const T &value)
QSet< T >::iterator insert(QSet< T >::const_iterator it, const T &value)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString number(double n, char format, int precision)
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QString toLower() const const
QString trimmed() const const
QString join(QChar separator) const const
void sort(Qt::CaseSensitivity cs)
CaseInsensitive
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition valueobject.h:67