Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Settings Class Reference

the settings of the client More...

#include <Settings.hxx>

+ Inheritance diagram for Settings:
+ Collaboration diagram for Settings:

Public Member Functions

void readFile ()
 Load settings from file. More...
 
void writeFile ()
 Save settings to file. More...
 
void resetSettingsToDefaults ()
 Reset settings to defaults from local settings file. More...
 

Public Attributes

friend Singleton< Settings >
 
- Public Attributes inherited from SettingsData
int settingsVersion
 the version of the Settings file. Overwrite cache settings if a newer version exists More...
 
int mapSize
 the size of the map More...
 
ScreenDimension screenWidth
 the screen width More...
 
ScreenDimension screenHeight
 the screen height More...
 
ScreenDimension currentScreenWidth
 the actual screen width (can differ from the one that's set in borderless fullscreen) More...
 
ScreenDimension currentScreenHeight
 the actual screen height (can differ from the one that's set in borderless fullscreen) More...
 
int maxElevationHeight
 the maximum elevation height More...
 
float zoneLayerTransparency
 the value of the zone layer transparency, (0 - 1.0). where 0 is full opaque and 1 for full transparency. More...
 
bool vSync
 if vSync is enabled or not More...
 
bool fullScreen
 
int fullScreenMode
 
float musicVolume
 the volume of music as float between [0, 1] More...
 
float soundEffectsVolume
 the volume of sound effects as float between [0, 1] More...
 
bool playMusic
 true if music should be played More...
 
bool playSoundEffects
 true if sound effects should be played More...
 
int audioChannels
 the number of channels used for sound playback 1=Mono,2=Stereo More...
 
bool audio3DStatus
 whether to play 3D sound or not More...
 
std::string buildMenuPosition
 location of the build menu More...
 
std::string biome
 this is used for biomedata More...
 
FilePath uiDataJSONFile
 JSONFile that contains uiData. More...
 
FilePath tileDataJSONFile
 JSONFile that contains tileData. More...
 
FilePath uiLayoutJSONFile
 The file path to the UI layout file. More...
 
FilePath audioConfigJSONFile
 file path to Audio Configuration More...
 
FilePath audioConfig3DJSONFile
 the file path to Audio Configuration 3D More...
 
std::string gameLanguage
 the code for the current game language More...
 
FilePath fontFileName
 FilePath of the Font that should be used. More...
 
int subMenuButtonWidth
 The width in pixels of the buttons used in the build sub menues on the UI. More...
 
int subMenuButtonHeight
 The height in pixels of the buttons used in the build sub menues on the UI. More...
 
uint32_t defaultFontSize
 
bool showBuildingsInBlueprint
 indicates whether we want to see buildings inside Blueprint layer or not More...
 
bool writeErrorLogFile
 Write errors to a log file. More...
 

Private Member Functions

 Settings ()
 
 ~Settings ()=default
 
json parseSettingsFile (const std::string &fileName) const
 

Additional Inherited Members

- Static Public Member Functions inherited from Singleton< Settings >
static Settingsinstance (void)
 Get an instance of the singleton. More...
 
- Protected Member Functions inherited from Singleton< Settings >
 Singleton () noexcept=default
 
 ~Singleton () noexcept=default
 

Detailed Description

the settings of the client

Definition at line 175 of file Settings.hxx.

Constructor & Destructor Documentation

◆ Settings()

Settings::Settings ( )
private

Definition at line 12 of file Settings.cxx.

12 { readFile(); }
+ Here is the call graph for this function:

◆ ~Settings()

Settings::~Settings ( )
privatedefault

Member Function Documentation

◆ parseSettingsFile()

json Settings::parseSettingsFile ( const std::string fileName) const
private

Definition at line 75 of file Settings.cxx.

76 {
77  json settingsJSONObject;
78 
79  if (fs::fileExists(fileName))
80  {
81  std::string jsonFile = fs::readFileAsString(fileName);
82  settingsJSONObject = json::parse(jsonFile, nullptr, false);
83  }
84 
85  return settingsJSONObject;
86 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ readFile()

void Settings::readFile ( )

Load settings from file.

Exceptions
ConfigurationErrorwhen loading configuration results in an error

Definition at line 14 of file Settings.cxx.

15 {
16  const std::string pathToCachedSettingsFile = CYTOPIA_DATA_DIR + SETTINGS_FILENAME;
17  const std::string pathToLocalSettingsFile = fs::getBasePath() + CYTOPIA_RESOURCES_DIR + SETTINGS_FILENAME;
18 
19  const json localJsonObject = parseSettingsFile(pathToLocalSettingsFile);
20  const json cachedJsonObject = parseSettingsFile(pathToCachedSettingsFile);
21 
22  if (localJsonObject.empty() || localJsonObject.is_discarded())
23  throw ConfigurationError(TRACE_INFO "Error parsing local JSON File " + string{pathToLocalSettingsFile});
24 
25  SettingsData settingsData;
26 
27  if (cachedJsonObject.empty() || cachedJsonObject.is_discarded())
28  {
29  settingsData = localJsonObject;
30  }
31  else
32  {
33  int cacheVersion = cachedJsonObject.value("SettingsVersion", -1);
34  int localVersion = localJsonObject.value("SettingsVersion", -1);
35  if (localVersion <= cacheVersion)
36  {
37  settingsData = cachedJsonObject;
38  }
39  else
40  {
41  LOG(LOG_INFO) << "The settings file version has changed. Overwriting local cached settings file with default settings.";
42  }
43  }
44 
45  *this = settingsData;
46 
47  // init the actual resolution with the desired resolution
50 
51 #ifdef __ANDROID__
53  subMenuButtonWidth *= 2;
54 #endif
55 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ resetSettingsToDefaults()

void Settings::resetSettingsToDefaults ( )

Reset settings to defaults from local settings file.

Exceptions
ConfigurationErrorwhen loading configuration results in an error

Definition at line 88 of file Settings.cxx.

89 {
90  const std::string pathToLocalSettingsFile = fs::getBasePath() + CYTOPIA_RESOURCES_DIR + SETTINGS_FILENAME;
91  const json localJsonObject = parseSettingsFile(pathToLocalSettingsFile);
92 
93  if (localJsonObject.empty() || localJsonObject.is_discarded())
94  throw ConfigurationError(TRACE_INFO "Error parsing local JSON File " + string{pathToLocalSettingsFile});
95 
96  SettingsData settingsData;
97  settingsData = localJsonObject;
98  *this = settingsData;
99 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ writeFile()

void Settings::writeFile ( )

Save settings to file.

Definition at line 57 of file Settings.cxx.

58 {
59  const json settingsJsonObject = *this;
60  std::string pathToDataDir;
61  if (CYTOPIA_DATA_DIR_BASE.empty())
62  {
63  LOG(LOG_ERROR) << "CYTOPIA_DATA_DIR_BASE is not set! Please report this issue on github. Falling back to cytopia base dir.";
64  pathToDataDir = fs::getBasePath() + CYTOPIA_RESOURCES_DIR;
65  }
66  else
67  {
68  pathToDataDir = CYTOPIA_DATA_DIR;
69  }
70  std::string pathToSettingsFile = pathToDataDir + SETTINGS_FILENAME;
71  fs::createDirectory(pathToDataDir);
72  fs::writeStringToFile(pathToSettingsFile, settingsJsonObject.dump());
73 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ Singleton< Settings >

Definition at line 178 of file Settings.hxx.


The documentation for this class was generated from the following files:
TRACE_INFO
#define TRACE_INFO
Definition: Exception.hxx:12
ConfigurationError
A configuration error.
Definition: Exception.hxx:36
LOG
Definition: LOG.hxx:32
fileExists
bool fileExists(const std::string &filePath)
Check if a file (or folder) exists.
Definition: Filesystem.cxx:62
LOG_INFO
@ LOG_INFO
Definition: LOG.hxx:25
writeStringToFile
void writeStringToFile(const std::string &fileName, const std::string &stringToWrite, bool binaryMode)
Write a string to a file.
Definition: Filesystem.cxx:50
SettingsData::screenHeight
ScreenDimension screenHeight
the screen height
Definition: Settings.hxx:46
SettingsData::subMenuButtonWidth
int subMenuButtonWidth
The width in pixels of the buttons used in the build sub menues on the UI.
Definition: Settings.hxx:154
LOG_ERROR
@ LOG_ERROR
Definition: LOG.hxx:28
CYTOPIA_RESOURCES_DIR
const std::string CYTOPIA_RESOURCES_DIR
Definition: Constants.hxx:21
SettingsData
Definition: Settings.hxx:23
Settings::parseSettingsFile
json parseSettingsFile(const std::string &fileName) const
Definition: Settings.cxx:75
readFileAsString
std::string readFileAsString(const std::string &fileName, bool binaryMode)
Read contents from a file as string.
Definition: Filesystem.cxx:12
SettingsData::screenWidth
ScreenDimension screenWidth
the screen width
Definition: Settings.hxx:40
getBasePath
std::string getBasePath()
Get base path (where Cytopia is being run)
Definition: Filesystem.cxx:77
SettingsData::currentScreenWidth
ScreenDimension currentScreenWidth
the actual screen width (can differ from the one that's set in borderless fullscreen)
Definition: Settings.hxx:49
SettingsData::subMenuButtonHeight
int subMenuButtonHeight
The height in pixels of the buttons used in the build sub menues on the UI.
Definition: Settings.hxx:160
SETTINGS_FILENAME
const std::string SETTINGS_FILENAME
Definition: Constants.hxx:20
createDirectory
void createDirectory(const std::string &dir)
Definition: Filesystem.cxx:69
CYTOPIA_DATA_DIR
const std::string CYTOPIA_DATA_DIR
Definition: Constants.hxx:17
SettingsData::currentScreenHeight
ScreenDimension currentScreenHeight
the actual screen height (can differ from the one that's set in borderless fullscreen)
Definition: Settings.hxx:52
CYTOPIA_DATA_DIR_BASE
const std::string CYTOPIA_DATA_DIR_BASE
Definition: Constants.hxx:14
json
nlohmann::json json
Definition: Settings.hxx:12
string
std::string string
Definition: AudioConfig.hxx:14
Settings::readFile
void readFile()
Load settings from file.
Definition: Settings.cxx:14