8 #ifndef PLAYERPREFS_HPP_
9 #define PLAYERPREFS_HPP_
17 #include "KapEngineDebug.hpp"
40 static void setBool(std::string
const& name,
bool const& val) {
55 static void setInt(std::string
const& name,
int const& val) {
66 static void setString(std::string
const& name, std::string
const& val) {
67 std::vector<PlayerPrefIntel> intels = readPlayerPref();
70 for (std::size_t i = 0; i < intels.size(); i++) {
71 if (intels.at(i).key == name) {
72 intels.at(i).content = val;
78 writePlayerPrefs(intels);
84 intels.push_back(nIntel);
85 writePlayerPrefs(intels);
96 static std::string
getString(std::string
const& name) {
97 std::vector<PlayerPrefIntel> intels = readPlayerPref();
99 for (std::size_t i = 0; i < intels.size(); i++) {
100 if (name == intels.at(i).key)
101 return intels.at(i).content;
103 DEBUG_ERROR(
"no content with key: " + name);
115 static int getInt(std::string
const& name) {
116 std::stringstream sstr;
135 static bool getBool(std::string
const& name) {
138 if (strVal ==
"true" || strVal ==
"1")
143 static bool hasKey(std::string
const& name) {
144 std::vector<PlayerPrefIntel> intels = readPlayerPref();
146 for (std::size_t i = 0; i < intels.size(); i++) {
147 if (name == intels.at(i).key)
156 inline static const std::string filePlayerPrefName =
"playerPrefs.keDat";
158 static std::vector<PlayerPrefIntel> readPlayerPref() {
160 std::ifstream myfile (filePlayerPrefName);
161 std::vector<std::string> lines;
162 std::vector<PlayerPrefIntel> res;
163 if (myfile.is_open())
165 while ( getline (myfile,line) ) {
166 lines.push_back(line);
170 DEBUG_ERROR(
"PlayerPref file does not exists. Please save something with PlayerPref to create this file");
174 for (std::size_t i = 0; i < lines.size(); i++) {
175 std::size_t posEqu = lines.at(i).find(
":");
176 PlayerPrefIntel nIntel;
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);
185 static void writePlayerPrefs(std::vector<PlayerPrefIntel>
const& list) {
186 std::ofstream myfile;
187 std::string content =
"";
189 for (std::size_t i = 0; i < list.size(); i++) {
190 std::string lineCont;
192 lineCont = list.at(i).key +
":" + list.at(i).content;
193 if (i < list.size() - 1) {
199 myfile.open (filePlayerPrefName);
200 if (!myfile.is_open()) {
201 DEBUG_ERROR(
"PlayerPrefs can't save anything. File does not open");