Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JsonSerialization.hxx
Go to the documentation of this file.
1 #ifndef JSON_SERIALIZATION_HXX_
2 #define JSON_SERIALIZATION_HXX_
3 
4 #include "json.hxx"
5 #include "Point.hxx"
7 #include "TerrainGenerator.hxx"
8 #include "Settings.hxx"
9 #ifdef USE_AUDIO
10 #include "../../services/AudioMixer.hxx"
11 #endif // USE_AUDIO
12 
14 
15 // ************** DE-SERIALIZER **************
16 
18 inline void from_json(const json &j, Point &point)
19 {
20  point.x = j.at("x").get<int>();
21  point.y = j.at("y").get<int>();
22  point.z = j.at("z").get<int>();
23  point.height = j.at("height").get<int>();
24 }
25 
27 inline void from_json(const json &j, MapNodeData &mapNodeData)
28 {
29  mapNodeData.tileID = j.at("tileID").get<std::string>();
30  mapNodeData.tileIndex = j.at("tileIndex").get<std::int32_t>();
31  mapNodeData.origCornerPoint = j.at("origCornerPoint").get<Point>();
32 }
33 
35 inline void from_json(const json &j, SettingsData &s)
36 {
37  s.settingsVersion = j.value("SettingsVersion", 0);
38  s.screenWidth = j["Graphics"]["Resolution"].value("Screen_Width", 800);
39  s.screenHeight = j["Graphics"]["Resolution"].value("Screen_Height", 600);
40  s.vSync = j["Graphics"].value("VSYNC", false);
41  s.fullScreen = j["Graphics"].value("FullScreen", false);
42  s.fullScreenMode = j["Graphics"].value("FullScreenMode", 0);
43  s.mapSize = j["Game"].value("MapSize", 64);
44  s.gameLanguage = j["Game"].value("Language", "en");
45  s.biome = j["Game"].value("Biome", "GrassLands");
46  s.maxElevationHeight = j["Game"].value("MaxElevationHeight", 32);
47  s.showBuildingsInBlueprint = j["Game"].value("ShowBuildingsInBlueprint", false);
48  s.zoneLayerTransparency = j["Game"].value("ZoneLayerTransparency", 0.5f);
49  s.uiDataJSONFile = j["ConfigFiles"].value("UIDataJSONFile", "resources/data/TileData.json");
50  s.tileDataJSONFile = j["ConfigFiles"].value("TileDataJSONFile", "resources/data/UIData.json");
51  s.uiLayoutJSONFile = j["ConfigFiles"].value("UILayoutJSONFile", "resources/data/UILayout.json");
52  s.audioConfigJSONFile = j["ConfigFiles"].value("AudioConfigJSONFile", "resources/data/AudioConfig.json");
53  s.audio3DStatus = j["Audio"].value("Audio3DStatus", true);
54  s.playMusic = j["Audio"].value("PlayMusic", true);
55  s.playSoundEffects = j["Audio"].value("PlaySoundEffects", false);
56  s.audioChannels = j["Audio"].value("AudioChannels", 2);
57  s.musicVolume = j["Audio"].value("MusicVolume", 0.5f);
58  s.soundEffectsVolume = j["Audio"].value("SoundEffectsVolume", 0.5f);
59  s.buildMenuPosition = j["User Interface"].value("BuildMenuPosition", "BOTTOM");
60  s.fontFileName = j["User Interface"].value("FontFilename", "resources/fonts/arcadeclassics.ttf");
61  s.subMenuButtonWidth = j["User Interface"].value("SubMenuButtonWidth", 32);
62  s.subMenuButtonHeight = j["User Interface"].value("SubMenuButtonHeight", 32);
63  s.defaultFontSize = j["User Interface"].value("defaultFontSize", 20);
64  s.writeErrorLogFile = j["Debug"].value("WriteErrorLogToFile", false);
65 }
66 
68 inline void from_json(const json &j, BiomeData &b)
69 {
70  if (j.find("trees") != j.end())
71  {
72  for (const auto &it : j["trees"].items())
73  {
74  if (it.key() == "light")
75  {
76  std::vector<std::string> temp = it.value();
77  b.treesLight = temp;
78  }
79  if (it.key() == "medium")
80  {
81  std::vector<std::string> temp = it.value();
82  b.treesMedium = temp;
83  }
84  if (it.key() == "dense")
85  {
86  std::vector<std::string> temp = it.value();
87  b.treesDense = temp;
88  }
89  }
90  }
91 
92  if (j.find("terrainFlora") != j.end())
93  {
94  for (const auto &it : j["terrainFlora"].items())
95  {
96  if (it.key() == "light")
97  {
98  std::vector<std::string> temp = it.value();
99  b.terrainFloraLight = temp;
100  }
101  if (it.key() == "medium")
102  {
103  std::vector<std::string> temp = it.value();
104  b.terrainFloraMedium = temp;
105  }
106  if (it.key() == "dense")
107  {
108  std::vector<std::string> temp = it.value();
109  b.terrainFloraDense = temp;
110  }
111  }
112  }
113 
114  if (j.find("bushes") != j.end())
115  {
116  for (const auto &it : j["bushes"].items())
117  {
118  if (it.key() == "light")
119  {
120  std::vector<std::string> temp = it.value();
121  b.bushesLight = temp;
122  }
123  if (it.key() == "medium")
124  {
125  std::vector<std::string> temp = it.value();
126  b.bushesMedium = temp;
127  }
128  if (it.key() == "dense")
129  {
130  std::vector<std::string> temp = it.value();
131  b.bushesDense = temp;
132  }
133  }
134  }
135 
136  if (j.find("waterFlora") != j.end())
137  {
138  for (const auto &it : j["waterFlora"].items())
139  {
140  if (it.key() == "light")
141  {
142  std::vector<std::string> temp = it.value();
143  b.waterFloraLight = temp;
144  }
145  if (it.key() == "medium")
146  {
147  std::vector<std::string> temp = it.value();
148  b.waterFloraMedium = temp;
149  }
150  if (it.key() == "dense")
151  {
152  std::vector<std::string> temp = it.value();
153  b.waterFloraDense = temp;
154  }
155  }
156  }
157 
158  if (j.find("terrain") != j.end())
159  {
160  std::vector<std::string> temp = j["terrain"];
161  b.terrain = temp;
162  }
163  if (j.find("water") != j.end())
164  {
165  std::vector<std::string> temp = j["water"];
166  b.water = temp;
167  }
168  if (j.find("waterdecoration") != j.end())
169  {
170  std::vector<std::string> temp = j["waterdecoration"];
171  b.waterDecoration = temp;
172  }
173  if (j.find("terrainRocks") != j.end())
174  {
175  std::vector<std::string> temp = j["terrainRocks"];
176  b.terrainRocks = temp;
177  }
178  if (j.find("terrainDecoration") != j.end())
179  {
180  std::vector<std::string> temp = j["terrainDecoration"];
181  b.terrainDecoration = temp;
182  }
183 }
184 
185 #ifdef USE_AUDIO
186 inline void from_json(const json &j, AudioTrigger &trigger) { trigger = AudioTrigger::_from_string(j.get<string>().c_str()); }
188 
190 inline void from_json(const json &j, AudioConfig::SoundtrackConfiguration &config)
191 {
192  j["path"].get_to(config.stereoFilePath);
193  j["monopath"].get_to(config.monoFilePath);
194  std::vector<string> triggers;
195  j["triggers"].get_to(triggers);
196  std::transform(triggers.begin(), triggers.end(), std::back_inserter(config.triggers),
197  [](const string &trigger) { return AudioTrigger::_from_string(trigger.c_str()); });
198 }
199 
201 inline void from_json(const json &j, AudioConfig &config)
202 {
203  j["Music"].get_to(config.Music);
204  j["Sound"].get_to(config.Sound);
205 }
206 #endif // USE_AUDIO
207 
208 // ************** SERIALIZER **************
209 
211 inline void to_json(json &j, const Point &point)
212 {
213  j = json{{"x", point.x}, {"y", point.y}, {"z", point.z}, {"height", point.height}};
214 }
215 
217 inline void to_json(json &j, const MapNodeData &mapNodeData)
218 {
219  j = json{
220  {"tileID", mapNodeData.tileID}, {"tileIndex", mapNodeData.tileIndex}, {"origCornerPoint", mapNodeData.origCornerPoint}};
221 }
222 
224 inline void to_json(json &j, const MapNode &m)
225 {
226  j = json{{"coordinates", m.getCoordinates()}, {"mapNodeData", m.getMapNodeData()}};
227 }
228 
230 inline void to_json(json &j, const SettingsData &s)
231 {
232  j = {
233  {std::string("SettingsVersion"), s.settingsVersion},
234  {std::string("Graphics"),
235  {
236  {std::string("VSYNC"), s.vSync},
237  {std::string("FullScreen"), s.fullScreen},
238  {std::string("FullScreenMode"), s.fullScreenMode},
239  {std::string("Resolution"),
240  {{std::string("Screen_Width"), s.screenWidth}, {std::string("Screen_Height"), s.screenHeight}}},
241  }},
242  {std::string("Game"),
243  {{std::string("MapSize"), s.mapSize},
244  {std::string("Language"), s.gameLanguage},
245  {std::string("Biome"), s.biome},
246  {std::string("MaxElevationHeight"), s.maxElevationHeight},
247  {std::string("ZoneLayerTransparency"), s.zoneLayerTransparency},
248  {std::string("ShowBuildingsInBlueprint"), s.showBuildingsInBlueprint}}},
249  {std::string("User Interface"),
250  {{std::string("BuildMenuPosition"), s.buildMenuPosition},
251  {std::string("FontFilename"), s.fontFileName.get()},
252  {std::string("SubMenuButtonWidth"), s.subMenuButtonWidth},
253  {std::string("SubMenuButtonHeight"), s.subMenuButtonHeight},
254  {std::string("DefaultFontSize"), s.defaultFontSize}}},
255  {std::string("ConfigFiles"),
256  {{std::string("UIDataJSONFile"), s.uiDataJSONFile.get()},
257  {std::string("TileDataJSONFile"), s.tileDataJSONFile.get()},
258  {std::string("UILayoutJSONFile"), s.uiLayoutJSONFile.get()},
259  {std::string("AudioConfigJSONFile"), s.audioConfigJSONFile.get()}}},
260  {std::string("Audio"),
261  {
262  {std::string("Audio3DStatus"), s.audio3DStatus},
263  {std::string("PlayMusic"), s.playMusic},
264  {std::string("PlaySoundEffects"), s.playSoundEffects},
265  {std::string("AudioChannels"), s.audioChannels},
266  {std::string("MusicVolume"), s.musicVolume},
267  {std::string("SoundEffectsVolume"), s.soundEffectsVolume},
268  }},
269  {std::string("Debug"),
270  {
271  {std::string("WriteErrorLogToFile"), s.writeErrorLogFile},
272  }},
273  };
274 }
275 
276 #endif
BiomeData::bushesLight
std::vector< std::string > bushesLight
Bushes Light IDs.
Definition: TerrainGenerator.hxx:22
BiomeData::terrain
std::vector< std::string > terrain
Terrain IDs.
Definition: TerrainGenerator.hxx:11
SettingsData::soundEffectsVolume
float soundEffectsVolume
the volume of sound effects as float between [0, 1]
Definition: Settings.hxx:88
BiomeData::treesMedium
std::vector< std::string > treesMedium
Trees Medium IDs.
Definition: TerrainGenerator.hxx:26
SettingsData::vSync
bool vSync
if vSync is enabled or not
Definition: Settings.hxx:70
AudioConfig
Definition: AudioConfig.hxx:22
BiomeData::treesDense
std::vector< std::string > treesDense
Trees Dense IDs.
Definition: TerrainGenerator.hxx:27
SettingsData::fontFileName
FilePath fontFileName
FilePath of the Font that should be used.
Definition: Settings.hxx:148
SettingsData::fullScreen
bool fullScreen
Definition: Settings.hxx:76
AudioConfig::SoundtrackConfiguration::stereoFilePath
string stereoFilePath
Definition: AudioConfig.hxx:26
BiomeData::waterFloraLight
std::vector< std::string > waterFloraLight
Water Flora Light IDs.
Definition: TerrainGenerator.hxx:14
SettingsData::buildMenuPosition
std::string buildMenuPosition
location of the build menu
Definition: Settings.hxx:111
MapNode::getCoordinates
const Point & getCoordinates() const
get iso coordinates of this node
Definition: MapNode.hxx:59
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
SettingsData::maxElevationHeight
int maxElevationHeight
the maximum elevation height
Definition: Settings.hxx:58
Point::z
int z
The z coordinate.
Definition: Point.hxx:23
BiomeData::terrainRocks
std::vector< std::string > terrainRocks
Terrain Rocks IDs.
Definition: TerrainGenerator.hxx:17
MapNodeData::origCornerPoint
Point origCornerPoint
Definition: MapNode.hxx:22
SettingsData::zoneLayerTransparency
float zoneLayerTransparency
the value of the zone layer transparency, (0 - 1.0). where 0 is full opaque and 1 for full transparen...
Definition: Settings.hxx:64
BiomeData::water
std::vector< std::string > water
Water IDs.
Definition: TerrainGenerator.hxx:12
to_json
void to_json(json &j, const Point &point)
JSON serializer for Point class.
Definition: JsonSerialization.hxx:211
SettingsData::audioChannels
int audioChannels
the number of channels used for sound playback 1=Mono,2=Stereo
Definition: Settings.hxx:100
MapNode::getMapNodeData
const std::vector< MapNodeData > & getMapNodeData() const
Definition: MapNode.hxx:96
BiomeData::treesLight
std::vector< std::string > treesLight
Trees Light IDs.
Definition: TerrainGenerator.hxx:25
Point::y
int y
The y coordinate.
Definition: Point.hxx:20
SettingsData::mapSize
int mapSize
the size of the map
Definition: Settings.hxx:34
SettingsData
Definition: Settings.hxx:23
Point::x
int x
The x coordinate.
Definition: Point.hxx:14
SettingsData::gameLanguage
std::string gameLanguage
the code for the current game language
Definition: Settings.hxx:145
SettingsData::uiDataJSONFile
FilePath uiDataJSONFile
JSONFile that contains uiData.
Definition: Settings.hxx:124
json
nlohmann::json json
Definition: JsonSerialization.hxx:13
SettingsData::tileDataJSONFile
FilePath tileDataJSONFile
JSONFile that contains tileData.
Definition: Settings.hxx:130
SettingsData::screenWidth
ScreenDimension screenWidth
the screen width
Definition: Settings.hxx:40
MapNode
Class that holds map nodes.
Definition: MapNode.hxx:30
MapNode.hxx
BiomeData::waterDecoration
std::vector< std::string > waterDecoration
Water Decoration IDs.
Definition: TerrainGenerator.hxx:13
SettingsData::playSoundEffects
bool playSoundEffects
true if sound effects should be played
Definition: Settings.hxx:94
BiomeData::terrainFloraDense
std::vector< std::string > terrainFloraDense
Terrain Flora (Flowers) Dense IDs.
Definition: TerrainGenerator.hxx:21
SettingsData::fullScreenMode
int fullScreenMode
Definition: Settings.hxx:82
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.hxx
SettingsData::uiLayoutJSONFile
FilePath uiLayoutJSONFile
The file path to the UI layout file.
Definition: Settings.hxx:136
SettingsData::showBuildingsInBlueprint
bool showBuildingsInBlueprint
indicates whether we want to see buildings inside Blueprint layer or not
Definition: Settings.hxx:165
SettingsData::settingsVersion
int settingsVersion
the version of the Settings file. Overwrite cache settings if a newer version exists
Definition: Settings.hxx:28
SettingsData::audio3DStatus
bool audio3DStatus
whether to play 3D sound or not
Definition: Settings.hxx:103
BiomeData::waterFloraMedium
std::vector< std::string > waterFloraMedium
Water Flora Medium IDs.
Definition: TerrainGenerator.hxx:15
AudioConfig::SoundtrackConfiguration::triggers
Vector< AudioTrigger > triggers
Definition: AudioConfig.hxx:28
SettingsData::defaultFontSize
uint32_t defaultFontSize
Definition: Settings.hxx:162
Point::height
int height
The height level.
Definition: Point.hxx:26
MapNodeData::tileIndex
int32_t tileIndex
Definition: MapNode.hxx:21
BiomeData::bushesMedium
std::vector< std::string > bushesMedium
Bushes Medium IDs.
Definition: TerrainGenerator.hxx:23
BiomeData::terrainDecoration
std::vector< std::string > terrainDecoration
Terrain Decoration IDs.
Definition: TerrainGenerator.hxx:18
BiomeData::terrainFloraMedium
std::vector< std::string > terrainFloraMedium
Terrain Flora (Flowers) Medium IDs.
Definition: TerrainGenerator.hxx:20
SettingsData::musicVolume
float musicVolume
the volume of music as float between [0, 1]
Definition: Settings.hxx:85
AudioConfig::SoundtrackConfiguration
Definition: AudioConfig.hxx:24
AudioConfig::Sound
Mapping< string, SoundtrackConfiguration > Sound
Definition: AudioConfig.hxx:31
Point
Definition: Point.hxx:7
SettingsData::playMusic
bool playMusic
true if music should be played
Definition: Settings.hxx:91
MapNodeData::tileID
std::string tileID
Definition: MapNode.hxx:19
BiomeData::terrainFloraLight
std::vector< std::string > terrainFloraLight
Terrain Flora (Flowers) Light IDs.
Definition: TerrainGenerator.hxx:19
TerrainGenerator.hxx
from_json
void from_json(const json &j, Point &point)
JSON deserializer for Point class.
Definition: JsonSerialization.hxx:18
SettingsData::audioConfigJSONFile
FilePath audioConfigJSONFile
file path to Audio Configuration
Definition: Settings.hxx:139
BiomeData
Definition: TerrainGenerator.hxx:9
json
nlohmann::json json
Definition: Settings.hxx:12
string
std::string string
Definition: AudioConfig.hxx:14
SettingsData::writeErrorLogFile
bool writeErrorLogFile
Write errors to a log file.
Definition: Settings.hxx:168
SettingsData::biome
std::string biome
this is used for biomedata
Definition: Settings.hxx:118
AudioConfig::Music
Mapping< string, SoundtrackConfiguration > Music
Definition: AudioConfig.hxx:30
BiomeData::waterFloraDense
std::vector< std::string > waterFloraDense
Water Flora Dense IDs.
Definition: TerrainGenerator.hxx:16
MapNodeData
Definition: MapNode.hxx:17
AudioConfig::SoundtrackConfiguration::monoFilePath
string monoFilePath
Definition: AudioConfig.hxx:27
BiomeData::bushesDense
std::vector< std::string > bushesDense
Bushes Dense IDs.
Definition: TerrainGenerator.hxx:24
StrongType::get
WeakType & get() noexcept
Definition: Meta.hxx:126
Point.hxx