R-Type
R-Type project
PlayerPrefs.hpp
1 /*
2 ** EPITECH PROJECT, 2022
3 ** engine
4 ** File description:
5 ** PlayerPrefs
6 */
7 
8 #ifndef PLAYERPREFS_HPP_
9 #define PLAYERPREFS_HPP_
10 
11 #include <string>
12 #include <iostream>
13 #include <fstream>
14 #include <sstream>
15 #include <vector>
16 
17 #include "KapEngineDebug.hpp"
18 
19 namespace KapEngine {
20 
26  class PlayerPrefs {
27  public:
28  struct PlayerPrefIntel {
29  std::string key;
30  std::string content;
31  };
32 
40  static void setBool(std::string const& name, bool const& val) {
41  if (val) {
42  setString(name, "true");
43  } else {
44  setString(name, "false");
45  }
46  }
47 
55  static void setInt(std::string const& name, int const& val) {
56  setString(name, std::to_string(val));
57  }
58 
66  static void setString(std::string const& name, std::string const& val) {
67  std::vector<PlayerPrefIntel> intels = readPlayerPref();
68  bool newVal = true;
69 
70  for (std::size_t i = 0; i < intels.size(); i++) {
71  if (intels.at(i).key == name) {
72  intels.at(i).content = val;
73  newVal = false;
74  break;
75  }
76  }
77  if (!newVal) {
78  writePlayerPrefs(intels);
79  return;
80  }
81  PlayerPrefIntel nIntel;
82  nIntel.content = val;
83  nIntel.key = name;
84  intels.push_back(nIntel);
85  writePlayerPrefs(intels);
86  }
87 
96  static std::string getString(std::string const& name) {
97  std::vector<PlayerPrefIntel> intels = readPlayerPref();
98 
99  for (std::size_t i = 0; i < intels.size(); i++) {
100  if (name == intels.at(i).key)
101  return intels.at(i).content;
102  }
103  DEBUG_ERROR("no content with key: " + name);
104  return "";
105  }
106 
115  static int getInt(std::string const& name) {
116  std::stringstream sstr;
117  std::string str = getString(name);
118  int res = 0;
119 
120  sstr << str;
121  sstr >> res;
122 
123  return res;
124 
125  }
126 
135  static bool getBool(std::string const& name) {
136  std::string strVal = getString(name);
137 
138  if (strVal == "true" || strVal == "1")
139  return true;
140  return false;
141  }
142 
143  static bool hasKey(std::string const& name) {
144  std::vector<PlayerPrefIntel> intels = readPlayerPref();
145 
146  for (std::size_t i = 0; i < intels.size(); i++) {
147  if (name == intels.at(i).key)
148  return true;
149  }
150  return false;
151  }
152 
153  protected:
154  private:
155 
156  inline static const std::string filePlayerPrefName = "playerPrefs.keDat";
157 
158  static std::vector<PlayerPrefIntel> readPlayerPref() {
159  std::string line;
160  std::ifstream myfile (filePlayerPrefName);
161  std::vector<std::string> lines;
162  std::vector<PlayerPrefIntel> res;
163  if (myfile.is_open())
164  {
165  while ( getline (myfile,line) ) {
166  lines.push_back(line);
167  }
168  myfile.close();
169  } else {
170  DEBUG_ERROR("PlayerPref file does not exists. Please save something with PlayerPref to create this file");
171  return res;
172  }
173 
174  for (std::size_t i = 0; i < lines.size(); i++) {
175  std::size_t posEqu = lines.at(i).find(":");
176  PlayerPrefIntel nIntel;
177 
178  nIntel.key = lines.at(i).substr(0, posEqu);
179  nIntel.content = lines.at(i).substr(posEqu + 1, lines.at(i).size());
180  res.push_back(nIntel);
181  }
182  return res;
183  }
184 
185  static void writePlayerPrefs(std::vector<PlayerPrefIntel> const& list) {
186  std::ofstream myfile;
187  std::string content = "";
188 
189  for (std::size_t i = 0; i < list.size(); i++) {
190  std::string lineCont;
191 
192  lineCont = list.at(i).key + ":" + list.at(i).content;
193  if (i < list.size() - 1) {
194  lineCont += "\n";
195  }
196  content += lineCont;
197  }
198 
199  myfile.open (filePlayerPrefName);
200  if (!myfile.is_open()) {
201  DEBUG_ERROR("PlayerPrefs can't save anything. File does not open");
202  return;
203  }
204  myfile << content;
205  myfile.close();
206  }
207  };
208 
209 }
210 
211 #endif /* !PLAYERPREFS_HPP_ */
KapEngine::PlayerPrefs::setString
static void setString(std::string const &name, std::string const &val)
Enregistrer une std::string.
Definition: PlayerPrefs.hpp:66
KapEngine::PlayerPrefs::PlayerPrefIntel
Definition: PlayerPrefs.hpp:28
KapEngine::PlayerPrefs::getInt
static int getInt(std::string const &name)
avoir la valeur int
Definition: PlayerPrefs.hpp:115
KapEngine::PlayerPrefs::setBool
static void setBool(std::string const &name, bool const &val)
Enregistrer une bool.
Definition: PlayerPrefs.hpp:40
KapEngine::PlayerPrefs
Definition: PlayerPrefs.hpp:26
KapEngine::PlayerPrefs::getBool
static bool getBool(std::string const &name)
avoir la valeur bool
Definition: PlayerPrefs.hpp:135
KapEngine
main namespace
Definition: Component.hpp:17
KapEngine::PlayerPrefs::setInt
static void setInt(std::string const &name, int const &val)
Enregistrer une int.
Definition: PlayerPrefs.hpp:55
KapEngine::PlayerPrefs::getString
static std::string getString(std::string const &name)
avoir la valeur string
Definition: PlayerPrefs.hpp:96