-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppConfig.cpp
More file actions
771 lines (618 loc) · 20.4 KB
/
AppConfig.cpp
File metadata and controls
771 lines (618 loc) · 20.4 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
//////////////////////////////////////////////////////////////////////////////
// Implementation of CConfig
//////////////////////////////////////////////////////////////////////////////
#include "AppConfig.h"
#include "Utils.h"
// Initialize the config singleton to NULL
CConfig* CConfig::msp_ConfigSingleton = NULL;
//////////////////////////////////////////////////////////////////////////////
// Default constructor of CConfig
//////////////////////////////////////////////////////////////////////////////
CConfig::CConfig()
{
// Set defaults
m_strLanguage = "English";
m_bPlaySound = true;
m_VideoCodec = Codec_H264_360P;
m_lRecordOption = 1;
m_iHudStyle = 1;
m_HudColor = ColorGreenLite;
m_iHudLineSize = 2;
m_bUseUSUnit = false;
m_bUseLowRes = false;
m_lSpeedLimit = 50;
m_lAccelerationLimit = 50;
m_lAltitudeLimit = 10;
m_strIpAddress = "192.168.1.1";
}
//////////////////////////////////////////////////////////////////////////////
// Default destructor of CConfig
//////////////////////////////////////////////////////////////////////////////
CConfig::~CConfig()
{
}
//////////////////////////////////////////////////////////////////////////////
// Check the file structure
//////////////////////////////////////////////////////////////////////////////
// [RETURN] : True if the configuration is alright
//////////////////////////////////////////////////////////////////////////////
bool CConfig::CheckConfig()
{
// Data directory must exist
if(!wxDirExists("Data"))
{
return false;
}
// The language directory must exist
if(!wxDirExists("Data/Lang"))
{
return false;
}
// Directory or configuration files
if(!wxDirExists("Data/Config"))
{
return false;
}
// Directory for Log files
if(!wxDirExists("Data/Log"))
{
if(!wxMkdir("Data/Log"))
{
return false;
}
}
// Directories for media files (photos-videos)
if(!wxDirExists("Media"))
{
if(!wxMkdir("Media"))
{
return false;
}
}
if(!wxDirExists("Media/Pictures"))
{
if(!wxMkdir("Media/Pictures"))
{
return false;
}
}
if(!wxDirExists("Media/Movies"))
{
if(!wxMkdir("Media/Movies"))
{
return false;
}
}
return true;
}
//////////////////////////////////////////////////////////////////////////////
// Get the singleton object
//////////////////////////////////////////////////////////////////////////////
// [RETURN] : Pointer to the singleton
//////////////////////////////////////////////////////////////////////////////
/*static*/ CConfig* CConfig::GetSingleton()
{
// If the object has not been created yet create it
if (NULL == msp_ConfigSingleton)
{
msp_ConfigSingleton = new CConfig();
}
// Return the pointer
return msp_ConfigSingleton;
}
//////////////////////////////////////////////////////////////////////////////
// Destroy the singleton object
//////////////////////////////////////////////////////////////////////////////
/*static*/ void CConfig::KillSingleton()
{
// If the object exist, delete it and set the pointer to NULL
if(NULL != msp_ConfigSingleton)
{
delete msp_ConfigSingleton;
msp_ConfigSingleton = NULL;
}
}
//////////////////////////////////////////////////////////////////////////////
// Set a new language to use
//////////////////////////////////////////////////////////////////////////////
// [IN] : Name of the language/file to use
//////////////////////////////////////////////////////////////////////////////
void CConfig::SetLanguage(const wxString &strLanguage)
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
m_strLanguage = strLanguage;
}
//////////////////////////////////////////////////////////////////////////////
// Get the current language
//////////////////////////////////////////////////////////////////////////////
// [RETURN] : Name of the language/file currently in use
//////////////////////////////////////////////////////////////////////////////
const wxString& CConfig::GetLanguage()
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
return m_strLanguage;
}
//////////////////////////////////////////////////////////////////////////////
// Set sound flag
//////////////////////////////////////////////////////////////////////////////
void CConfig::SetSoundFlag(bool bPlaySound)
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
m_bPlaySound = bPlaySound;
}
//////////////////////////////////////////////////////////////////////////////
// Get current sound flag
//////////////////////////////////////////////////////////////////////////////
bool CConfig::HasSoundFlag()
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
return m_bPlaySound;
}
//////////////////////////////////////////////////////////////////////////////
// Set video codec
//////////////////////////////////////////////////////////////////////////////
void CConfig::SetVideoCodec(eVideoCodec VideoCodec)
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
m_VideoCodec = VideoCodec;
}
//////////////////////////////////////////////////////////////////////////////
// Get configured codec
//////////////////////////////////////////////////////////////////////////////
eVideoCodec CConfig::GetVideoCodec()
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
return m_VideoCodec;
}
//////////////////////////////////////////////////////////////////////////////
// Set the record option
//////////////////////////////////////////////////////////////////////////////
void CConfig::SetRecordOption(long lRecordOption)
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
m_lRecordOption = lRecordOption;
}
//////////////////////////////////////////////////////////////////////////////
// Get the current record option
//////////////////////////////////////////////////////////////////////////////
long CConfig::GetRecordOption()
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
return m_lRecordOption;
}
//////////////////////////////////////////////////////////////////////////////
// Set HUD style to use
//////////////////////////////////////////////////////////////////////////////
void CConfig::SetHudStyle(int iHudStyle)
{
if(iHudStyle == 1 || iHudStyle == 2)
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
m_iHudStyle = iHudStyle;
}
}
//////////////////////////////////////////////////////////////////////////////
// Get current HUD style
//////////////////////////////////////////////////////////////////////////////
int CConfig::GetHudStyle()
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
return m_iHudStyle;
}
//////////////////////////////////////////////////////////////////////////////
// Set color of the HUD
//////////////////////////////////////////////////////////////////////////////
void CConfig::SetHudColor(wxColour Color)
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
m_HudColor = Color;
}
//////////////////////////////////////////////////////////////////////////////
// Get current color of the HUD
//////////////////////////////////////////////////////////////////////////////
wxColour CConfig::GetHudColor()
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
return m_HudColor;
}
//////////////////////////////////////////////////////////////////////////////
// Set the line size of the HUD
//////////////////////////////////////////////////////////////////////////////
void CConfig::SetHudLineSize(int iSize)
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
m_iHudLineSize = iSize;
}
//////////////////////////////////////////////////////////////////////////////
// Get current line size of the hud
//////////////////////////////////////////////////////////////////////////////
int CConfig::GetHudLineSize()
{
return m_iHudLineSize;
}
//////////////////////////////////////////////////////////////////////////////
// Set the flag for US units
//////////////////////////////////////////////////////////////////////////////
void CConfig::SetUsUnitFlag(bool bUseUSUnits)
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
m_bUseUSUnit = bUseUSUnits;
}
//////////////////////////////////////////////////////////////////////////////
// Get the flag for US units
//////////////////////////////////////////////////////////////////////////////
bool CConfig::HasUSUnit()
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
return m_bUseUSUnit;
}
//////////////////////////////////////////////////////////////////////////////
// Set the flag for low res
//////////////////////////////////////////////////////////////////////////////
void CConfig::SetLowResFlag(bool bUseLowRes)
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
m_bUseLowRes = bUseLowRes;
}
//////////////////////////////////////////////////////////////////////////////
// Get the flag for low res
//////////////////////////////////////////////////////////////////////////////
bool CConfig::UseLowRes()
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
return m_bUseLowRes;
}
//////////////////////////////////////////////////////////////////////////////
// Set speed limit
//////////////////////////////////////////////////////////////////////////////
void CConfig::SetSpeedLimit(long lSpeedLimit)
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
m_lSpeedLimit = lSpeedLimit;
}
//////////////////////////////////////////////////////////////////////////////
// Get current speed limit
//////////////////////////////////////////////////////////////////////////////
long CConfig::GetSpeedLimit()
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
return m_lSpeedLimit;
}
//////////////////////////////////////////////////////////////////////////////
// Set acceleration limit
//////////////////////////////////////////////////////////////////////////////
void CConfig::SetAccelerationLimit(long lAccelerationLimit)
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
m_lAccelerationLimit = lAccelerationLimit;
}
//////////////////////////////////////////////////////////////////////////////
// Get acceleration limit
//////////////////////////////////////////////////////////////////////////////
long CConfig::GetAccelerationLimit()
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
return m_lAccelerationLimit;
}
//////////////////////////////////////////////////////////////////////////////
// Set altitude limit
//////////////////////////////////////////////////////////////////////////////
void CConfig::SetAltitudeLimit(long lAltitudeLimit)
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
m_lAltitudeLimit = lAltitudeLimit;
}
//////////////////////////////////////////////////////////////////////////////
// Get altitude limit
//////////////////////////////////////////////////////////////////////////////
long CConfig::GetAltitudeLimit()
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
return m_lAltitudeLimit;
}
//////////////////////////////////////////////////////////////////////////////
// Set the Ip Address to use
//////////////////////////////////////////////////////////////////////////////
void CConfig::SetIpAddress(const wxString &strIpAddress)
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
m_strIpAddress = strIpAddress;
}
//////////////////////////////////////////////////////////////////////////////
// Get the current Ip Address
//////////////////////////////////////////////////////////////////////////////
const wxString& CConfig::GetIpAddress()
{
wxCriticalSectionLocker LockConfig(m_CSConfig);
return m_strIpAddress;
}
//////////////////////////////////////////////////////////////////////////////
// Load the saved config
//////////////////////////////////////////////////////////////////////////////
// [RETURN] : true if config could successfully be loaded from file
//////////////////////////////////////////////////////////////////////////////
bool CConfig::LoadConfig()
{
// Text file containing the config
wxTextFile ConfigFile("Data/Config/Config.ini");
// Verify if the file exist
if(ConfigFile.Exists())
{
// If the file exist, open it
if(ConfigFile.Open())
{
int iLine=0;
// Read the language
m_strLanguage = ConfigFile.GetLine(iLine++);
// Get the sound flag
if(1 == ToLong(ConfigFile.GetLine(iLine++)))
{
m_bPlaySound = true;
}
else
{
m_bPlaySound = false;
}
// Read codec
m_VideoCodec = (eVideoCodec)ToLong(ConfigFile.GetLine(iLine++));
// Record option
m_lRecordOption = ToLong(ConfigFile.GetLine(iLine++));
// Get the HUD style
m_iHudStyle = ToLong(ConfigFile.GetLine(iLine++));
// Read HUD color
m_HudColor = wxColor(ConfigFile.GetLine(iLine++));
// Get line size of the HUD
m_iHudLineSize = ToLong(ConfigFile.GetLine(iLine++));
// Get US unit flag
if(1 == ToLong(ConfigFile.GetLine(iLine++)))
{
m_bUseUSUnit = true;
}
else
{
m_bUseUSUnit = false;
}
// Low resolution flag
if(1 == ToLong(ConfigFile.GetLine(iLine++)))
{
m_bUseLowRes = true;
}
else
{
m_bUseLowRes = false;
}
// Get speed limit
m_lSpeedLimit = ToLong(ConfigFile.GetLine(iLine++));
// Get acceleration limit
m_lAccelerationLimit = ToLong(ConfigFile.GetLine(iLine++));
// Get altitude limit
m_lAltitudeLimit = ToLong(ConfigFile.GetLine(iLine++));
// Get Ip address
m_strIpAddress = ConfigFile.GetLine(iLine++);
// Close the file
ConfigFile.Close();
// Leave function
return true;
}
else
{
// File exist, but cannot be loaded !
return false;
}
}
return true;
}
//////////////////////////////////////////////////////////////////////////////
// Save the current config
//////////////////////////////////////////////////////////////////////////////
// [RETURN] : true if config could successfully be saved to file
//////////////////////////////////////////////////////////////////////////////
bool CConfig::SaveConfig()
{
// Text file containing the config
wxTextFile ConfigFile("Data/Config/Config.ini");
// Verify if the file exist
if(ConfigFile.Exists())
{
// If the file exist, open it
if(!ConfigFile.Open())
{
return false;
}
}
else
{
// Create the file
if(!ConfigFile.Create())
{
return false;
}
}
// Clear the file
ConfigFile.Clear();
m_CSConfig.Enter();
// Add the language information to the config file
ConfigFile.AddLine(m_strLanguage);
// Add sound config
ConfigFile.AddLine(m_bPlaySound ? "1":"0");
// Add Codec
ConfigFile.AddLine(ToString(m_VideoCodec));
// Add record option
ConfigFile.AddLine(ToString(m_lRecordOption));
// Add HUD style
ConfigFile.AddLine(ToString(m_iHudStyle));
// Add HUD color
ConfigFile.AddLine(m_HudColor.GetAsString());
// Add HUD line size
ConfigFile.AddLine(ToString(m_iHudLineSize));
// Add US unit flag
ConfigFile.AddLine(m_bUseUSUnit ? "1":"0");
// Add low res flag
ConfigFile.AddLine(m_bUseLowRes ? "1":"0");
// Add speed limit
ConfigFile.AddLine(ToString(m_lSpeedLimit));
// Add acceleration limit
ConfigFile.AddLine(ToString(m_lAccelerationLimit));
// Add altitude limit
ConfigFile.AddLine(ToString(m_lAltitudeLimit));
// Add Ip Adress
ConfigFile.AddLine(m_strIpAddress);
m_CSConfig.Leave();
// Save the changes
ConfigFile.Write();
// Close the file
ConfigFile.Close();
return true;
}
//////////////////////////////////////////////////////////////////////////////
// List the languages files available in the language folder
//////////////////////////////////////////////////////////////////////////////
// [OUT] : Pointer to string array containing the found languages
// [RETURN] : true on success, false if an error occur
//////////////////////////////////////////////////////////////////////////////
bool CConfig::GetPossibleLanguages(wxArrayString *parLanguages)
{
// Language directory
wxDir MyDir;
// Name of the language file
wxString strFileName;
// Flag for iteration control
bool bLoop = true;
// Clear the array
parLanguages->Clear();
// Open the dir to iterate over language files
if(!MyDir.Open("Data/Lang"))
{
return false;
}
// Get the first file
bLoop = MyDir.GetFirst(&strFileName, "*.lng", wxDIR_FILES|wxDIR_HIDDEN);
// Iterate over the folder as long a language file can be found
while(bLoop)
{
// Take only the first part of the filename (without file extension)
strFileName = strFileName.BeforeLast('.');
if(!strFileName.IsEmpty())
{
// Add it to the list of languages
parLanguages->Add(strFileName);
}
// Get next file
bLoop = MyDir.GetNext(&strFileName);
}
return true;
}
//////////////////////////////////////////////////////////////////////////////
// Load current language
//////////////////////////////////////////////////////////////////////////////
// [RETURN] : true on success, false if an error occur
//////////////////////////////////////////////////////////////////////////////
bool CConfig::LoadLanguage()
{
return this->LoadLanguage(m_strLanguage);
}
//////////////////////////////////////////////////////////////////////////////
// Load a specific language
//////////////////////////////////////////////////////////////////////////////
// [IN] : Language to use
// [RETURN] : true on success, false if an error occur
//////////////////////////////////////////////////////////////////////////////
bool CConfig::LoadLanguage(const wxString& strLanguage)
{
// Temporary value
wxString strTmp;
// Key name
wxString strKey;
// Value of the key in the selected language
wxString strValue;
// Number of lines in the file
long lCount = 0;
// Position in the file
long lPos = 0;
// Position of equality sign in the string
int iTokenPos = 0;
// Return value
bool bRet = true;
// Text file containing the language strings
wxTextFile LangFile("Data/Lang/" + strLanguage + ".lng");
// Verify if the file exist
if(!LangFile.Exists())
{
return false;
}
// If the file exist, open it
if(!LangFile.Open())
{
DoLog(wxString("Failed to open language file ") + strLanguage, MSG_ERROR);
return false;
}
// Now copy all the strings to the map
if( 0 >= (lCount = LangFile.GetLineCount()))
{
// The file is empty !
bRet = false;
}
else
{
// Clear the current map before we fill it with another language
m_LanguageMap.empty();
// Iterate over the file
for(lPos = 0; lPos < lCount; lPos++)
{
strTmp = LangFile.GetLine(lPos);
// Ignore empty strings or commented strings
if( strTmp.IsNull() || strTmp.IsEmpty() )
{
continue;
}
// Trim blanks from left
strTmp.Trim(false);
// Now check that the string does not begin with ";" or "//"
if( (strTmp.Left(1) == ";") || (strTmp.Left(2) == "//") )
{
continue;
}
// A "=" sign must be found to be a valid string
if(wxNOT_FOUND != (iTokenPos = strTmp.Find('=')))
{
// Trim only the white spaces from left
strTmp = strTmp.Trim(false);
// Get the left part of the string as key
strKey = strTmp.BeforeFirst('=');
// Get the right part of the string as key
strValue = strTmp.AfterFirst('=');
// Set the values into the map
m_LanguageMap[strKey] = strValue;
}
}
}
// Close the file
LangFile.Close();
return bRet;
}
//////////////////////////////////////////////////////////////////////////////
// Get a string in the current language
//////////////////////////////////////////////////////////////////////////////
// [IN] : Name of the string we are looking for
// [RETURN] : String in the current language
//////////////////////////////////////////////////////////////////////////////
wxString CConfig::GetText(const wxString& strTargetName)
{
// Return string
wxString strValue;
// Iterator tor the map
LanguageMap::iterator LangIt;
// Look for the specified name
LangIt = m_LanguageMap.find(strTargetName);
if(m_LanguageMap.end() != LangIt)
{
// Get the string from the map
strValue = LangIt->second;
}
else
{
// String could not be found in the map so return a default value
strValue = L"ER__" + strTargetName;
}
return strValue;
}