XPMP2
X-Plane multiplayer library 2 - using instancing
CSLModels.h
Go to the documentation of this file.
1 
21 #ifndef _CSLModels_h_
22 #define _CSLModels_h_
23 
24 namespace XPMP2 {
25 
27 typedef std::map<std::string,std::string> mapCSLPackageTy;
28 
31  OLS_INVALID = -1,
36 };
37 
39 class CSLObj
40 {
41 public:
42  std::string cslKey;
43  std::string path;
44  std::string pathOrig;
45  std::string texture;
46  std::string text_lit;
47 
48 protected:
50  XPLMObjectRef xpObj = NULL;
53 
54 public:
56  CSLObj (const std::string& _cslKey,
57  const std::string& _path) : cslKey(_cslKey), path(_path) {}
59  CSLObj (CSLObj&& o) = default;
60  CSLObj& operator = (CSLObj&& o) = default;
62  ~CSLObj ();
63 
65  ObjLoadStateTy GetObjState () const { return xpObjState; }
67  bool IsInvalid () const { return xpObjState == OLS_INVALID; }
68 
71 
73  float FetchVertOfsFromObjFile () const;
74 
77  XPLMObjectRef GetAndLoadObj ();
79  void Load ();
81  void Unload ();
82 
84  bool NeedsObjCopy () const { return !pathOrig.empty(); }
85 
86 protected:
87 
90  bool TriggerCopyAndReplace ();
92  void SetCopyResult (bool bResult);
94  static void SetOtherObjCopyResult (bool bResult);
95 
97  bool CopyAndReplace ();
98 
100  static void XPObjLoadedCB (XPLMObjectRef inObject,
101  void * inRefcon);
103  void Invalidate ();
104 };
105 
107 typedef std::list<CSLObj> listCSLObjTy;
108 
109 typedef std::pair<std::string,std::string> pairOfStrTy;
110 
112 class CSLModel
113 {
114 public:
116  struct MatchCritTy {
117  protected:
119  std::string icaoAirline;
120  int relOp = 0;
121  public:
123  std::string livery;
124 
126  void setAirline (const std::string& _icaoOp)
127  { relOp = RelatedGet(REL_TXT_OP, icaoAirline = _icaoOp); }
128 
129  const std::string& getAirline () const { return icaoAirline; }
130  int getRelOp () const { return relOp; }
131 
134  bool merge (const MatchCritTy& o);
135  };
137  typedef std::vector<MatchCritTy> MatchCritVecTy;
138 public:
140  std::string shortId;
142  std::string cslId;
144  std::string modelName;
146  std::uint16_t pkgHash = 0;
152  float vertOfs = 3.0f;
154  bool bVertOfsReadFromFile = true;
155 
157  std::string xsbAircraftPath;
159  int xsbAircraftLn = 0;
160 
161 protected:
163  std::string icaoType;
165  const Doc8643* doc8643 = nullptr;
167  int related = 0;
169  unsigned refCnt = 0;
171  float refZeroTs = 0.0f;
173  std::future<float> futVertOfs;
174 
175 public:
177  CSLModel () {}
179  CSLModel (CSLModel&& o) = default;
180  CSLModel& operator = (CSLModel&& o) = default;
181 
183  virtual ~CSLModel ();
184 
190  void AddMatchCriteria (const std::string& _type,
191  const MatchCritTy& _matchCrit,
192  int lnNr);
194  void CompModelName ();
195 
197  bool IsValid () const { return !cslId.empty() && !icaoType.empty() && !listObj.empty(); }
198 
199  const std::string& GetShortId () const { return shortId; }
200  const std::string& GetId () const { return cslId; }
201  const std::string& GetModelName () const { return modelName; }
202  const std::string& GetIcaoType () const { return icaoType; }
203  const std::string& GetIcaoAirline () const { return vecMatchCrit.at(0).getAirline(); }
204  const std::string& GetLivery () const { return vecMatchCrit.at(0).livery; }
205  int GetRelatedGrp () const { return related; }
206  std::string GetKeyString () const;
207 
208  // Data from Doc8643
209  const Doc8643& GetDoc8643 () const { return *doc8643; }
210  const char* GetWTC () const { return doc8643->wtc; }
211  char GetClassType () const { return doc8643->GetClassType(); }
212  char GetClassNumEng () const { return doc8643->GetClassNumEng(); }
213  int GetNumEngines() const { return doc8643->GetNumEngines(); }
214  char GetClassEngType () const { return doc8643->GetClassEngType(); }
215  bool HasRotor () const { return doc8643->HasRotor(); }
216 
218  float GetVertOfs () const { return vertOfs; }
219 
221  ObjLoadStateTy GetObjState () const;
223  bool IsObjInvalid () const { return GetObjState() == OLS_INVALID; }
224 
227  std::list<XPLMObjectRef> GetAllObjRefs ();
228 
230  void IncRefCnt () { ++refCnt; }
232  void DecRefCnt ();
234  unsigned GetRefCnt () const { return refCnt; }
235 
237  static float GarbageCollection (float inElapsedSinceLastCall,
238  float inElapsedTimeSinceLastFlightLoop,
239  int inCounter,
240  void * inRefcon);
241 
242 protected:
244  void Unload ();
246  float FetchVertOfsFromObjFile () const;
247 };
248 
250 typedef std::map<std::string,CSLModel> mapCSLModelTy;
251 
253 typedef std::multimap<unsigned long,std::pair<CSLModel*,const CSLModel::MatchCritTy*> > mmapCSLModelPTy;
254 
255 //
256 // MARK: Global Functions
257 //
258 
260 void CSLModelsInit ();
261 
263 void CSLModelsCleanup ();
264 
269 const char* CSLModelsLoad (const std::string& _path,
270  int _maxDepth = 5);
271 
275 CSLModel* CSLModelById (const std::string& _cslId,
276  mapCSLModelTy::iterator* _pOutIter = nullptr);
277 
281 CSLModel* CSLModelByKey(const std::string& _cslKey,
282  mapCSLModelTy::iterator* _pOutIter = nullptr);
283 
291 int CSLModelMatching (const std::string& _type,
292  const std::string& _airline,
293  const std::string& _livery,
294  CSLModel* &pModel);
295 
296 } // namespace XPMP2
297 
298 #endif
Represents a CSL model as it is saved on disk.
Definition: CSLModels.h:113
bool IsObjInvalid() const
(Minimum) )State of the X-Plane object: Is it invalid?
Definition: CSLModels.h:223
std::string cslId
full id: package name / shortId, expected to be unique
Definition: CSLModels.h:142
const Doc8643 * doc8643
Proper Doc8643 entry for this model.
Definition: CSLModels.h:165
listCSLObjTy listObj
list of objects representing this model
Definition: CSLModels.h:150
int GetNumEngines() const
Definition: CSLModels.h:213
int xsbAircraftLn
Line number in the xsb_aircraft.txt file where the model definition starts.
Definition: CSLModels.h:159
unsigned refCnt
Reference counter: Number of Aircraft actively using this model.
Definition: CSLModels.h:169
char GetClassEngType() const
Definition: CSLModels.h:214
char GetClassNumEng() const
Definition: CSLModels.h:212
std::string shortId
short id, just an arbitrary label read from xsb_aircraft.txt::OBJ8_AIRCRAFT
Definition: CSLModels.h:140
const std::string & GetId() const
full id: package name / shortId, expected to be unique
Definition: CSLModels.h:200
std::uint16_t pkgHash
simple hash of package name, for easy matching in networked setup
Definition: CSLModels.h:146
const Doc8643 & GetDoc8643() const
Classification (like "L2P" or "L4J") and WTC (like "H" or "L/M")
Definition: CSLModels.h:209
virtual ~CSLModel()
Destructor frees resources.
Definition: CSLModels.cpp:360
std::list< XPLMObjectRef > GetAllObjRefs()
Try get ALL object handles, only returns anything if it is the complete list.
Definition: CSLModels.cpp:452
static float GarbageCollection(float inElapsedSinceLastCall, float inElapsedTimeSinceLastFlightLoop, int inCounter, void *inRefcon)
Unload all objects which haven't been used for a while.
Definition: CSLModels.cpp:500
std::future< float > futVertOfs
future for asynchronously reading vertOfs
Definition: CSLModels.h:173
MatchCritVecTy vecMatchCrit
further match-relevant fields like airline and livery can be a list
Definition: CSLModels.h:148
std::string icaoType
ICAO aircraft type this model represents: xsb_aircraft.txt::ICAO
Definition: CSLModels.h:163
float FetchVertOfsFromObjFile() const
Read the obj files to fill CSLModel::vertOfs.
Definition: CSLModels.cpp:526
const std::string & GetLivery() const
Livery code this model represents: xsb_aircraft.txt::LIVERY
Definition: CSLModels.h:204
ObjLoadStateTy GetObjState() const
(Minimum) )State of the X-Plane objects: Is it being loaded or available?
Definition: CSLModels.cpp:438
const std::string & GetIcaoType() const
ICAO aircraft type this model represents: xsb_aircraft.txt::ICAO
Definition: CSLModels.h:202
bool IsValid() const
Minimum requirement for using this object is: id, type, path.
Definition: CSLModels.h:197
bool HasRotor() const
Definition: CSLModels.h:215
char GetClassType() const
Definition: CSLModels.h:211
std::string modelName
name, formed by last part of path plus id
Definition: CSLModels.h:144
void CompModelName()
Puts together the model name string from a path component and shortId
Definition: CSLModels.cpp:421
const std::string & GetIcaoAirline() const
ICAO Airline code this model represents: xsb_aircraft.txt::AIRLINE
Definition: CSLModels.h:203
CSLModel()
Constructor.
Definition: CSLModels.h:177
const std::string & GetShortId() const
short id, just an arbitrary label read from xsb_aircraft.txt::OBJ8_AIRCRAFT
Definition: CSLModels.h:199
int GetRelatedGrp() const
"related" group for this model (a group of alike plane models), or 0
Definition: CSLModels.h:205
int related
"related" group for this model (a group of alike plane models), or 0
Definition: CSLModels.h:167
float GetVertOfs() const
Vertical Offset to be applied to aircraft model.
Definition: CSLModels.h:218
void Unload()
Unload all objects.
Definition: CSLModels.cpp:518
const char * GetWTC() const
Wake turbulence category.
Definition: CSLModels.h:210
std::vector< MatchCritTy > MatchCritVecTy
Vector of match-relevant fields.
Definition: CSLModels.h:137
std::string GetKeyString() const
compiles the string used as key in the CSL model map
Definition: CSLModels.cpp:432
float vertOfs
Vertical offset to be applied [m].
Definition: CSLModels.h:152
void IncRefCnt()
Increase the reference counter for Aircraft usage.
Definition: CSLModels.h:230
CSLModel & operator=(CSLModel &&o)=default
bool bVertOfsReadFromFile
Shall we try reading vertOfs from the OBJ8 file if we need this a/c?
Definition: CSLModels.h:154
void DecRefCnt()
Decrease the reference counter for Aircraft usage.
Definition: CSLModels.cpp:487
float refZeroTs
Time point when refCnt reached 0 (used in garbage collection, in terms of XP's total running time)
Definition: CSLModels.h:171
CSLModel(CSLModel &&o)=default
Generate standard move constructor.
unsigned GetRefCnt() const
Current reference counter.
Definition: CSLModels.h:234
std::string xsbAircraftPath
Path to the xsb_aircraft.txt file from where this model is loaded.
Definition: CSLModels.h:157
const std::string & GetModelName() const
name, formed by last part of path plus id (human readable, but not guaranteed to be unique)
Definition: CSLModels.h:201
void AddMatchCriteria(const std::string &_type, const MatchCritTy &_matchCrit, int lnNr)
Set the a/c type model and add other match criteria.
Definition: CSLModels.cpp:390
One .obj file of a CSL model (of which it can have multiple)
Definition: CSLModels.h:40
bool IsInvalid() const
Is invalid?
Definition: CSLModels.h:67
ObjLoadStateTy xpObjState
State of the X-Plane object: Is it being loaded or available?
Definition: CSLModels.h:52
std::string pathOrig
full path to the original .obj file IF there is the need to create a copy upon load
Definition: CSLModels.h:44
XPLMObjectRef GetAndLoadObj()
Load and return the underlying X-Plane objects.
Definition: CSLModels.cpp:98
ObjLoadStateTy GetObjState() const
State of the X-Plane object: Is it being loaded or available?
Definition: CSLModels.h:65
XPLMObjectRef xpObj
The X-Plane object reference to the loaded model as requested with XPLMLoadObjectAsync.
Definition: CSLModels.h:50
CSLObj & operator=(CSLObj &&o)=default
std::string cslKey
key of the CSL model this belongs to
Definition: CSLModels.h:42
void SetCopyResult(bool bResult)
Update with the result of the copy operation.
Definition: CSLCopy.cpp:188
void Load()
Starts loading the XP object.
Definition: CSLModels.cpp:216
bool TriggerCopyAndReplace()
Trigger a separate thread to copy the .obj file if needed.
Definition: CSLCopy.cpp:230
float FetchVertOfsFromObjFile() const
Read the obj file to calculate its vertical offset.
Definition: CSLModels.cpp:151
std::string text_lit
texture_lit file if defined, to be used in the TEXTURE_LIT line of the .obj file
Definition: CSLModels.h:46
void Unload()
Free up the object.
Definition: CSLModels.cpp:243
static void XPObjLoadedCB(XPLMObjectRef inObject, void *inRefcon)
callback function called when loading is done
Definition: CSLModels.cpp:254
~CSLObj()
Destructor makes sure the XP object is removed, too.
Definition: CSLModels.cpp:92
CSLObj(const std::string &_cslKey, const std::string &_path)
Constructor doesn't do much.
Definition: CSLModels.h:56
static void SetOtherObjCopyResult(bool bResult)
Update another CSLObj with the result of the copy operation.
Definition: CSLCopy.cpp:203
CSLObj(CSLObj &&o)=default
Generate standard move constructor.
bool NeedsObjCopy() const
Will this object require copying the .obj file upon load?
Definition: CSLModels.h:84
std::string path
full path to the (potentially copied) .obj file
Definition: CSLModels.h:43
void Invalidate()
Marks the CSL Model invalid in case object loading once failed.
Definition: CSLModels.cpp:309
void DetermineWhichObjToLoad()
Determine which file to load and if we need a copied .obj file.
Definition: CSLModels.cpp:112
bool CopyAndReplace()
Perform the copy-on-load functionality, replacing dataRefs and textures, expected to be called via st...
Definition: CSLCopy.cpp:73
std::string texture
texture file if defined, to be used in the TEXTURE line of the .obj file
Definition: CSLModels.h:45
Definition: XPMPAircraft.h:70
int CSLModelMatching(const std::string &_type, const std::string &_airline, const std::string &_livery, CSLModel *&pModel)
Find a matching model.
Definition: CSLModels.cpp:1368
void CSLModelsCleanup()
Grace cleanup.
Definition: CSLModels.cpp:1065
std::multimap< unsigned long, std::pair< CSLModel *, const CSLModel::MatchCritTy * > > mmapCSLModelPTy
Multimap of references to CSLModels and match criteria for matching purposes.
Definition: CSLModels.h:253
std::pair< std::string, std::string > pairOfStrTy
Definition: CSLModels.h:109
CSLModel * CSLModelById(const std::string &_cslId, mapCSLModelTy::iterator *_pOutIter)
Find a model by unique id.
Definition: CSLModels.cpp:1110
std::list< CSLObj > listCSLObjTy
List of objects.
Definition: CSLModels.h:107
std::map< std::string, CSLModel > mapCSLModelTy
Map of CSLModels (owning the object), ordered by related group / type.
Definition: CSLModels.h:250
void CSLModelsInit()
Initialization.
Definition: CSLModels.cpp:1046
ObjLoadStateTy
State of the X-Plane object: Is it being loaded or available?
Definition: CSLModels.h:30
@ OLS_COPYING
creating a new .obj file copy, replacing dataRefs and textures
Definition: CSLModels.h:33
@ OLS_UNAVAIL
Not yet tried loading the CSL object.
Definition: CSLModels.h:32
@ OLS_INVALID
loading once failed -> invalid!
Definition: CSLModels.h:31
@ OLS_LOADING
async load underway
Definition: CSLModels.h:34
@ OLS_AVAILABLE
X-Plane object available in xpObj
Definition: CSLModels.h:35
int RelatedGet(RelTxtTy relType, const std::string &_acType)
Find the related group for a given key, 0 if none.
Definition: RelatedDoc8643.cpp:146
std::map< std::string, std::string > mapCSLPackageTy
Map of CSLPackages: Maps an id to the base path (path ends on forward slash)
Definition: CSLModels.h:27
CSLModel * CSLModelByKey(const std::string &_cslKey, mapCSLModelTy::iterator *_pOutIter)
Find a model by (even more) unique key.
Definition: CSLModels.cpp:1134
const char * CSLModelsLoad(const std::string &_path, int _maxDepth)
Read the CSL Models found in the given path and below.
Definition: CSLModels.cpp:1081
@ REL_TXT_OP
related file for operators, relOp.txt
Definition: RelatedDoc8643.h:46
Combines match-relevant fields (beside ICAO a/c type)
Definition: CSLModels.h:116
std::string livery
Livery code this model represents: xsb_aircraft.txt::LIVERY
Definition: CSLModels.h:123
const std::string & getAirline() const
ICAO Airline code / operator.
Definition: CSLModels.h:129
int relOp
'related' group of the airline, if it exists, otherwise 0
Definition: CSLModels.h:120
bool merge(const MatchCritTy &o)
Decide which criteria is better and keep that.
Definition: CSLModels.cpp:321
int getRelOp() const
'Related' group of operator, if any, otherwise 0
Definition: CSLModels.h:130
std::string icaoAirline
ICAO Airline code this model represents: xsb_aircraft.txt::AIRLINE
Definition: CSLModels.h:119
void setAirline(const std::string &_icaoOp)
Sets airline (operator) and looks up its 'related' group.
Definition: CSLModels.h:126
Represents a line in the Doc8643.txt file, of which we use only classification and WTC.
Definition: RelatedDoc8643.h:78
bool HasRotor() const
Definition: RelatedDoc8643.h:94
int GetNumEngines() const
Definition: RelatedDoc8643.h:92
char wtc[4]
Definition: RelatedDoc8643.h:81
char GetClassNumEng() const
Definition: RelatedDoc8643.h:91
char GetClassEngType() const
Definition: RelatedDoc8643.h:93
char GetClassType() const
Definition: RelatedDoc8643.h:90