HoverRace  2.0
Config.h
Go to the documentation of this file.
1 
2 // Config.h
3 //
4 // Copyright (c) 2008-2010, 2012-2016 Michael Imamura.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 
22 #pragma once
23 
24 #ifndef _WIN32
25  // Use XDG base directories on non-Win32.
26 # include <basedir.h>
27 #endif
28 
29 #include "OS.h"
30 
31 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
32 # ifdef MR_ENGINE
33 # define MR_DllDeclare __declspec( dllexport )
34 # else
35 # define MR_DllDeclare __declspec( dllimport )
36 # endif
37 #else
38 # define MR_DllDeclare
39 #endif
40 
41 // Forward declarations.
42 namespace HoverRace {
43  namespace Parcel {
44  class Bundle;
45  class ResBundle;
46  class TrackBundle;
47  }
48  namespace Util {
49  class Locale;
50  namespace yaml {
51  class Emitter;
52  class MapNode;
53  }
54  }
55 }
56 
57 namespace HoverRace {
58 namespace Util {
59 
60 class ConfigExn : public std::exception
61 {
62  using SUPER = std::exception;
63 
64 public:
65  ConfigExn() : SUPER() { }
66  ConfigExn(const char* const &msg) : SUPER(), msg(msg) { }
67  ConfigExn(const std::string &msg) : SUPER(), msg(msg) { }
68  virtual ~ConfigExn() noexcept { }
69 
70  const char* what() const noexcept override { return msg.c_str(); }
71 
72 private:
73  std::string msg;
74 };
75 
80 {
81 private:
82  Config(const std::string &packageName,
83  int verMajor, int verMinor, int verPatch, int verBuild,
84  bool prerelease, const OS::path_t &mediaPath,
85  const OS::path_t &sysCfgPath,
86  const OS::path_t &path = { });
87 public:
88  ~Config();
89 
90  static Config &Init(const std::string &packageName,
91  int verMajor, int verMinor, int verPatch, int verBuild,
92  bool prerelease, const OS::path_t &mediaPath,
93  const OS::path_t &sysCfgPath,
94  const OS::path_t &path = { });
95 
96  bool IsUnlinked() const;
97  void SetUnlinked(bool unlinked);
98 
99  bool IsPrerelease() const;
100  const std::string &GetVersion() const;
101  const std::string &GetFullVersion() const;
102  const int &GetBuild() const;
103  const std::string &GetUserAgentId() const;
104 
105 private:
106  static OS::path_t GetDefaultBasePath();
107  void AppendPackageSubdir(OS::path_t &path) const;
108 
109 public:
110  OS::path_t GetBaseDataPath() const;
111  OS::path_t GetBaseConfigPath() const;
112  OS::path_t GetConfigFilename() const;
113 
114  OS::path_t GetDefaultMediaPath();
115  const OS::path_t &GetMediaPath() const;
116  OS::path_t GetMediaPath(const std::string &file) const;
117 
118  const OS::path_t &GetUserTrackPath() const;
119  OS::path_t GetUserTrackPath(const std::string &name) const;
120 
121  const OS::path_t &GetProfilePath() const;
122  OS::path_t GetProfilePath(const std::string &uid) const;
123 
124  Parcel::ResBundle &GetResBundle() const { return *resBundle; }
125  std::shared_ptr<Parcel::ResBundle> ShareResBundle() const { return resBundle; }
126 
127  Parcel::TrackBundle &GetTrackBundle() const { return *trackBundle; }
128 
133  std::shared_ptr<Parcel::TrackBundle> ShareTrackBundle() const { return trackBundle; }
134 
135  Locale &GetLocale() { return *locale; }
136 
137  OS::path_t GetScriptHelpPath(const std::string &className) const;
138 
139  OS::path_t GenerateScreenshotPath(const std::string &ext) const;
140 
141  static std::string GetDefaultRoomListUrl();
142  static std::string GetDefaultUpdateServerUrl();
143  static OS::path_t GetDefaultChatLogPath();
144  static OS::path_t GetDefaultScreenshotPath();
145 
146  const std::string &GetDefaultFontName() const;
147  const std::string &GetDefaultMonospaceFontName() const;
148  const std::string &GetDefaultSymbolFontName() const;
149 
150 public:
151  static Config *GetInstance()
152  {
153  assert(instance);
154  return instance.get();
155  }
156 
157  void ResetToDefaults();
158 
159 private:
160  void LoadSystem();
161  void LoadSystem(const OS::path_t &path);
162 public:
163  void Load();
164  void Save() const;
165 
166 private:
167  void SaveVersion(yaml::Emitter&) const;
168 
169 public:
170  static const std::string TRACK_EXT;
171 
172 private:
173  static std::unique_ptr<Config> instance;
174  std::string packageName;
175  bool unlinked;
182  std::shared_ptr<Parcel::ResBundle> resBundle;
183  std::shared_ptr<Parcel::TrackBundle> trackBundle;
184  std::unique_ptr<Locale> locale;
185  int verBuild;
187  std::string shortVersion;
188  std::string fullVersion;
189  std::string userAgentId;
190  std::string defaultFontName;
193 # ifndef _WIN32
194  xdgHandle *xdg;
195 # endif
196 
197 public:
198  struct app_t
199  {
202 
203  void Load(yaml::MapNode*);
204  } app;
205 
206  struct video_t
207  {
208  double gamma;
209  double contrast;
210  double brightness;
211 
212  double textScale;
213 
214  int xPos;
215  int yPos;
216  int xRes;
217  int yRes;
223 
225 
226  void ResetToDefaults();
227  void Load(yaml::MapNode*);
228  void Save(yaml::Emitter&) const;
229  } video;
230 
231  struct audio_t
232  {
233  double sfxVolume;
234 
235  void ResetToDefaults();
236  void Load(yaml::MapNode*);
237  void Save(yaml::Emitter&) const;
238  } audio;
239 
240  struct i18n_t
241  {
242  std::string preferredLocale;
243 
244  void ResetToDefaults();
245  void Load(yaml::MapNode*);
246  void Save(yaml::Emitter&) const;
247  } i18n;
248 
249  struct misc_t
250  {
252 
253  void ResetToDefaults();
254  void Load(yaml::MapNode*);
255  void Save(yaml::Emitter&) const;
256  } misc;
257 
258  struct player_t
259  {
260  std::string defaultProfile;
261 
262  void ResetToDefaults();
263  void Load(yaml::MapNode*);
264  void Save(yaml::Emitter&) const;
265  } player;
266 
267  struct net_t
268  {
269  std::string mainServer;
270  std::string updateServer;
274  bool logChats;
279  static const int DEFAULT_UDP_RECV_PORT = 9531;
280  static const int DEFAULT_TCP_RECV_PORT = 9531;
281  static const int DEFAULT_TCP_SERV_PORT = 9530;
282  //TODO: Proxy server settings.
283 
284  void ResetToDefaults();
285  void Load(yaml::MapNode*);
286  void Save(yaml::Emitter&) const;
287  } net;
288 
289  static const int MAX_PLAYERS = 4;
290 
293  {
294  int motorOn;
295  int right;
296  int left;
297  int jump;
298  int fire;
299  int brake;
300  int weapon;
301  int lookBack;
302 
303  void Load(yaml::MapNode*);
304  void Save(yaml::Emitter&) const;
305  } controlsHash[MAX_PLAYERS];
306 
308  {
309  int zoomIn;
310  int zoomOut;
311  int panUp;
312  int panDown;
313  int reset;
314 
315  void Load(yaml::MapNode*);
316  void Save(yaml::Emitter&) const;
317  } cameraHash;
318 
319  struct ui_t
320  {
329  int menuOk;
332  int menuUp;
333  int menuDown;
334  int menuLeft;
336  int menuNext;
337  int menuPrev;
338 
339  void Load(yaml::MapNode*);
340  void Save(yaml::Emitter&) const;
341  } ui;
342 
343  struct runtime_t
344  {
345  bool silent;
349  bool enableHud;
350  bool noAccel;
352  bool profiling;
353  std::vector<OS::path_t> initScripts;
354  } runtime;
355 };
356 
357 } // namespace Util
358 } // namespace HoverRace
359 
360 #undef MR_DllDeclare
int yResFullscreen
Definition: Config.h:221
int udpRecvPort
Definition: Config.h:276
int left
Definition: Config.h:296
int consoleBottom
Definition: Config.h:325
xdgHandle * xdg
Definition: Config.h:194
std::locale locale
Global reference to the current locale.
Definition: OS.cpp:98
bool enableConsole
Definition: Config.h:347
boost::filesystem::path path_t
Definition: OS.h:57
int menuUp
Definition: Config.h:332
int panDown
Definition: Config.h:312
bool fullscreen
Definition: Config.h:218
int tcpRecvPort
Definition: Config.h:277
int yRes
Definition: Config.h:217
std::exception SUPER
Definition: Config.h:62
ConfigExn(const char *const &msg)
Definition: Config.h:66
int jump
Definition: Config.h:297
bool enableDebugOverlay
Definition: Config.h:348
bool prerelease
Definition: Config.h:186
int menuPrev
Definition: Config.h:337
OS::path_t userTrackPath
Definition: Config.h:180
MR_DllDeclare void Init()
Definition: ColorTools.cpp:142
std::string userAgentId
Definition: Config.h:189
Definition: Config.h:319
std::string mainServer
Definition: Config.h:269
Definition: Config.h:258
OS::path_t mediaPath
Definition: Config.h:178
Definition: Config.h:60
int motorOn
Definition: Config.h:294
int consoleDown
Definition: Config.h:323
ConfigExn(const std::string &msg)
Definition: Config.h:67
double brightness
Definition: Config.h:210
Definition: Config.h:343
Global configuration manager, shared by all aspects of the system.
Definition: Config.h:79
Parcel::ResBundle & GetResBundle() const
Definition: Config.h:124
double sfxVolume
Definition: Config.h:233
OS::path_t dataPath
Base path for data files.
Definition: Config.h:176
int tcpServPort
Definition: Config.h:278
Parcel::TrackBundle & GetTrackBundle() const
Definition: Config.h:127
int panUp
Definition: Config.h:311
bool showFramerate
Definition: Config.h:346
bool messageReceivedSoundOnlyBg
Definition: Config.h:273
OS::path_t sysCfgPath
Definition: Config.h:179
int consoleHelp
Definition: Config.h:328
int verBuild
Definition: Config.h:185
std::string shortVersion
Definition: Config.h:187
Definition: Config.h:249
int brake
Definition: Config.h:299
int xResFullscreen
Definition: Config.h:220
OS::path_t mediaPath
Definition: Config.h:201
OS::path_t localePath
Definition: Config.h:200
int menuRight
Definition: Config.h:335
std::string defaultProfile
Definition: Config.h:260
OS::path_t logChatsPath
Definition: Config.h:275
double textScale
Definition: Config.h:212
bool noAccel
Disable accelerated (OpenGL) rendering.
Definition: Config.h:350
bool profiling
Definition: Config.h:352
Abstraction for loading resources.
Definition: ResBundle.h:49
#define MR_DllDeclare
Definition: Config.h:38
double gamma
Definition: Config.h:208
int menuNext
Definition: Config.h:336
int xPos
Definition: Config.h:214
int fire
Definition: Config.h:298
OS::path_t screenshotPath
Definition: Config.h:251
OS::path_t profilePath
Definition: Config.h:181
bool autoUpdates
Definition: Config.h:271
int yPos
Definition: Config.h:215
Definition: Config.h:267
int lookBack
Definition: Config.h:301
int fullscreenMonitorIndex
Definition: Config.h:219
int consoleUp
Definition: Config.h:322
int menuOk
Definition: Config.h:329
virtual ~ConfigExn() noexcept
Definition: Config.h:68
static const std::string TRACK_EXT
Definition: Config.h:170
static std::unique_ptr< Config > instance
Definition: Config.h:173
Definition: Config.h:206
const char * what() const noexceptoverride
Definition: Config.h:70
std::shared_ptr< Parcel::ResBundle > ShareResBundle() const
Definition: Config.h:125
std::string fullVersion
Definition: Config.h:188
std::shared_ptr< Parcel::ResBundle > resBundle
Definition: Config.h:182
std::vector< OS::path_t > initScripts
Definition: Config.h:353
double contrast
Definition: Config.h:209
int menuLeft
Definition: Config.h:334
std::shared_ptr< Parcel::TrackBundle > trackBundle
Definition: Config.h:183
std::string msg
Definition: Config.h:73
static Config * GetInstance()
Definition: Config.h:151
int fullscreenRefreshRate
Definition: Config.h:222
int xRes
Definition: Config.h:216
bool skipStartupWarning
Definition: Config.h:351
int consoleNextCmd
Definition: Config.h:327
std::string defaultFontName
Definition: Config.h:190
int menuCancel
Definition: Config.h:330
bool messageReceivedSound
Definition: Config.h:272
Definition: MapNode.h:34
OS::path_t cfgPath
Base path for config files.
Definition: Config.h:177
Definition: Config.h:198
int menuDown
Definition: Config.h:333
int consoleToggle
Definition: Config.h:321
int right
Definition: Config.h:295
Locale & GetLocale()
Definition: Config.h:135
Definition: Announcement.h:24
int reset
Definition: Config.h:313
Definition: Config.h:231
std::string defaultMonospaceFontName
Definition: Config.h:191
bool unlinked
if true, will prevent saving config.
Definition: Config.h:175
std::shared_ptr< Parcel::TrackBundle > ShareTrackBundle() const
Retrieve the track bundle.
Definition: Config.h:133
A source of track parcels.
Definition: TrackBundle.h:58
Definition: Config.h:240
std::string updateServer
Definition: Config.h:270
Wrapper for the LibYAML emitter.
Definition: Emitter.h:48
std::string preferredLocale
Definition: Config.h:242
int zoomIn
Definition: Config.h:309
std::unique_ptr< Locale > locale
Definition: Config.h:184
std::string defaultSymbolFontName
Definition: Config.h:192
int consolePrevCmd
Definition: Config.h:326
int consoleTop
Definition: Config.h:324
A library of available locales.
Definition: Locale.h:54
int menuExtra
Definition: Config.h:331
bool stackedSplitscreen
Definition: Config.h:224
int zoomOut
Definition: Config.h:310
See Client/Control/Controller.h for the hash function.
Definition: Config.h:292
std::string packageName
Definition: Config.h:174
ConfigExn()
Definition: Config.h:65
bool logChats
Definition: Config.h:274
bool silent
Definition: Config.h:345
bool enableHud
Definition: Config.h:349
int weapon
Definition: Config.h:300