Chira Engine
A customizable MIT-licensed game engine.
JSONSettingsLoader.cpp
1 #include "JSONSettingsLoader.h"
2 
3 #include <fstream>
4 #include <iomanip>
5 
6 #include <config/Config.h>
7 
8 using namespace chira;
9 
10 JSONSettingsLoader::JSONSettingsLoader(std::string_view filename)
11  : ISettingsLoader(filename, Config::getConfigDirectory(), false) {
12  this->load();
13 }
14 
15 JSONSettingsLoader::JSONSettingsLoader(std::string_view filename, std::string_view path, bool relative /*= false*/)
16  : ISettingsLoader(filename, path, relative) {
17  this->load();
18 }
19 
20 void JSONSettingsLoader::getValue(const std::string& name, int* value) const {
21  if (this->hasValue(name)) {
22  *value = this->settings[name];
23  }
24 }
25 
26 void JSONSettingsLoader::getValue(const std::string& name, double* value) const {
27  if (this->hasValue(name)) {
28  *value = this->settings[name];
29  }
30 }
31 
32 void JSONSettingsLoader::getValue(const std::string& name, std::string* value) const {
33  if (this->hasValue(name)) {
34  *value = this->settings[name];
35  }
36 }
37 
38 void JSONSettingsLoader::getValue(const std::string& name, bool* value) const {
39  if (this->hasValue(name)) {
40  *value = this->settings[name];
41  }
42 }
43 
44 void JSONSettingsLoader::setValue(const std::string& name, int value, bool overwrite, bool save) {
45  if (!overwrite && this->hasValue(name)) {
46  return;
47  }
48  this->settings[name] = value;
49  if (save) {
50  this->save();
51  }
52 }
53 
54 void JSONSettingsLoader::setValue(const std::string& name, double value, bool overwrite, bool save) {
55  if (!overwrite && this->hasValue(name)) {
56  return;
57  }
58  this->settings[name] = value;
59  if (save) {
60  this->save();
61  }
62 }
63 
64 void JSONSettingsLoader::setValue(const std::string& name, const std::string& value, bool overwrite, bool save) {
65  if (!overwrite && this->hasValue(name)) {
66  return;
67  }
68  this->settings[name] = value;
69  if (save) {
70  this->save();
71  }
72 }
73 
74 void JSONSettingsLoader::setValue(const std::string& name, bool value, bool overwrite, bool save) {
75  if (!overwrite && this->hasValue(name)) {
76  return;
77  }
78  this->settings[name] = value;
79  if (save) {
80  this->save();
81  }
82 }
83 
84 bool JSONSettingsLoader::hasValue(const std::string& name) const {
85  return this->settings.contains(name);
86 }
87 
88 void JSONSettingsLoader::load() {
89  std::ifstream fileCheck{this->getFilePath().data()};
90  if (!fileCheck.good()) {
91  // Make new file if it doesn't exist
92  std::fstream fs{this->getFilePath().data(), std::fstream::out};
93  fs << "{}\n";
94  fs.close();
95  }
96  std::ifstream inputFile{this->getFilePath().data()};
97  nlohmann::json input;
98  inputFile >> input;
99  inputFile.close();
100  for (auto element = input.begin(); element != input.end(); ++element) {
101  this->settings[element.key()] = element.value();
102  }
103 }
104 
105 void JSONSettingsLoader::save() {
106  std::ofstream output{this->getFilePath().data()};
107  output << std::setw(4) << this->settings << std::endl;
108 }