RigsofRods
Soft-body Physics Simulation
LocalStorage.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5  Copyright 2013-2020 Petr Ohlidal
6 
7  For more information, see http://www.rigsofrods.org/
8 
9  Rigs of Rods is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License version 3, as
11  published by the Free Software Foundation.
12 
13  Rigs of Rods is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "LocalStorage.h"
23 
24 #include "Actor.h"
25 #include "Application.h"
26 #include "ContentManager.h"
27 #include "PlatformUtils.h"
28 
29 using namespace RoR;
30 
31 
32 /* class that implements the localStorage interface for the scripts */
33 LocalStorage::LocalStorage(std::string fileName_in, const std::string& sectionName_in, const std::string &resource_group = RGN_CACHE)
34 {
35  // inversed logic, better use a whitelist instead of a blacklist, so you are on the safe side ;) - tdev
36  std::string allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
37  for (std::string::iterator it = fileName_in.begin() ; it < fileName_in.end() ; ++it){
38  if ( allowedChars.find(*it) == std::string::npos )
39  *it = '_';
40  }
41 
42  sectionName = sectionName_in.substr(0, sectionName_in.find(".", 0));
43 
44  m_filename = fileName_in + ".asdata";
45  m_resource_group = resource_group;
46  this->separators = "=";
47  loadDict();
48 
49  saved = true;
50 }
51 
53 {
54  // save everything
55  saveDict();
56 }
57 
59 {
60  m_filename = other->getFilename();
61  sectionName = other->getSection();
62  SettingsBySection::iterator secIt;
63  SettingsBySection osettings = other->getSettings();
64  for (secIt = osettings.begin(); secIt!=osettings.end(); secIt++)
65  {
66  SettingsMultiMap::iterator setIt;
67  for (setIt = secIt->second->begin(); setIt!=secIt->second->end(); setIt++)
68  {
69  setSetting(setIt->first, setIt->second, secIt->first);
70  }
71  }
72 }
73 
74 void LocalStorage::changeSection(const std::string &section)
75 {
76  sectionName = section.substr(0, section.find(".", 0));
77 }
78 
79 // getters and setters
80 std::string LocalStorage::get(std::string key)
81 {
82  std::string sec;
83  parseKey(key, sec);
84  return getString(key, sec);
85 }
86 
87 void LocalStorage::set(std::string key, const std::string &value)
88 {
89  std::string sec;
90  parseKey(key, sec);
91  setSetting(key, value, sec);
92  saved = false;
93 }
94 
95 int LocalStorage::getInt(std::string key)
96 {
97  std::string sec;
98  parseKey(key, sec);
99  return getSettingInt(key, sec);
100 }
101 
102 void LocalStorage::set(std::string key, const int value)
103 {
104  std::string sec;
105  parseKey(key, sec);
106  setSetting(key, value, sec);
107  saved = false;
108 }
109 
110 float LocalStorage::getFloat(std::string key)
111 {
112  std::string sec;
113  parseKey(key, sec);
114  return getSettingReal(key, sec);
115 }
116 
117 void LocalStorage::set(std::string key, const float value)
118 {
119  std::string sec;
120  parseKey(key, sec);
121  setSetting(key, value, sec);
122  saved = false;
123 }
124 
125 bool LocalStorage::getBool(std::string key)
126 {
127  std::string sec;
128  parseKey(key, sec);
129  return getSettingBool(key, sec);
130 }
131 
132 void LocalStorage::set(std::string key, const bool value)
133 {
134  std::string sec;
135  parseKey(key, sec);
136  setSetting(key, value, sec);
137  saved = false;
138 }
139 
140 Ogre::Vector3 LocalStorage::getVector3(std::string key)
141 {
142  std::string sec;
143  parseKey(key, sec);
144  return getSettingVector3(key, sec);
145 }
146 
147 void LocalStorage::set(std::string key, const Ogre::Vector3 &value)
148 {
149  std::string sec;
150  parseKey(key, sec);
151  setSetting(key, value, sec);
152  saved = false;
153 }
154 
155 Ogre::Quaternion LocalStorage::getQuaternion(std::string key)
156 {
157  std::string sec;
158  parseKey(key, sec);
159  return getSettingQuaternion(key, sec);
160 }
161 
162 void LocalStorage::set(std::string key, const Ogre::Quaternion &value)
163 {
164  std::string sec;
165  parseKey(key, sec);
166  setSetting(key, value, sec);
167  saved = false;
168 }
169 
170 Ogre::Radian LocalStorage::getRadian(std::string key)
171 {
172  std::string sec;
173  parseKey(key, sec);
174  return getSettingRadian(key, sec);
175 }
176 
177 void LocalStorage::set(std::string key, const Ogre::Radian &value)
178 {
179  std::string sec;
180  parseKey(key, sec);
181  setSetting(key, value, sec);
182  saved = false;
183 }
184 
185 Ogre::Degree LocalStorage::getDegree(std::string key)
186 {
187  std::string sec;
188  parseKey(key, sec);
189  return Ogre::Degree(getSettingRadian(key, sec));
190 }
191 
192 void LocalStorage::set(std::string key, const Ogre::Degree &value)
193 {
194  std::string sec;
195  parseKey(key, sec);
196  setSetting(key, Ogre::Radian(value), sec);
197  saved = false;
198 }
199 
201 {
202  if (this->saved)
203  {
204  return;
205  }
206 
207  try
208  {
210  this->saved = true;
211  }
212  catch (std::exception& e)
213  {
214  RoR::LogFormat("[RoR|Scripting|LocalStorage]"
215  "Error saving file '%s' (resource group '%s'), message: '%s'",
216  m_filename.c_str(), RGN_CACHE, e.what());
217  }
218 }
219 
221 {
222  try
223  {
225  }
226  catch (std::exception& e)
227  {
228  RoR::LogFormat("[RoR|Scripting|LocalStorage]"
229  "Error reading file '%s' (resource group '%s'), message: '%s'",
230  m_filename.c_str(), RGN_CACHE, e.what());
231  return false;
232  }
233  saved = true;
234  return true;
235 }
236 
237 void LocalStorage::eraseKey(std::string key)
238 {
239  std::string sec;
240  parseKey(key, sec);
241  if (mSettingsPtr.find(sec) != mSettingsPtr.end() && mSettingsPtr[sec]->find(key) != mSettingsPtr[sec]->end())
242  if (mSettingsPtr[sec]->erase(key) > 0)
243  saved = false;
244 }
245 
247 {
248  // SLOG("This feature is not available yet");
249 }
250 
251 void LocalStorage::parseKey(std::string& key, std::string &section)
252 {
253  size_t dot = key.find(".", 0);
254  if ( dot != std::string::npos )
255  {
256  section = key.substr(0, dot);
257 
258  if ( !section.length() )
259  section = sectionName;
260 
261  key.erase(0, dot+1);
262  }
263  else
264  section = sectionName;
265 }
266 
267 bool LocalStorage::exists(std::string key)
268 {
269  std::string sec;
270  parseKey(key, sec);
271  return hasSetting(key, sec);
272 }
273 
ImprovedConfigFile::getSettingReal
Ogre::Real getSettingReal(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
Definition: ImprovedConfigFile.h:122
ImprovedConfigFile::getSettingInt
int getSettingInt(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
Definition: ImprovedConfigFile.h:132
RoR::LocalStorage::saved
bool saved
Inverted 'dirty flag'.
Definition: LocalStorage.h:94
RGN_CACHE
#define RGN_CACHE
Definition: Application.h:46
ImprovedConfigFile::getSettingBool
bool getSettingBool(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
Definition: ImprovedConfigFile.h:112
RoR::LocalStorage::getVector3
Ogre::Vector3 getVector3(std::string key)
Definition: LocalStorage.cpp:140
ImprovedConfigFile::separators
Ogre::String separators
Definition: ImprovedConfigFile.h:233
RoR::LocalStorage::m_filename
std::string m_filename
Definition: LocalStorage.h:92
ImprovedConfigFile::saveImprovedCfg
bool saveImprovedCfg(std::string const &filename, std::string const &resource_group_name)
Definition: ImprovedConfigFile.h:58
RoR::LocalStorage::loadDict
bool loadDict()
Definition: LocalStorage.cpp:220
RoR::LocalStorage::deleteAll
void deleteAll()
Definition: LocalStorage.cpp:246
ContentManager.h
RoR::LocalStorage::getInt
int getInt(std::string key)
Definition: LocalStorage.cpp:95
RoR::LocalStorage::getRadian
Ogre::Radian getRadian(std::string key)
Definition: LocalStorage.cpp:170
ImprovedConfigFile::getSettingRadian
Ogre::Radian getSettingRadian(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
Definition: ImprovedConfigFile.h:102
RoR::LogFormat
void LogFormat(const char *format,...)
Improved logging utility. Uses fixed 2Kb buffer.
Definition: Application.cpp:424
ImprovedConfigFile::loadImprovedCfg
void loadImprovedCfg(std::string const &filename, std::string const &resource_group_name)
Definition: ImprovedConfigFile.h:48
RoR::ConfigFile::getString
Ogre::String getString(Ogre::String const &key, Ogre::String const &section, Ogre::String const &defaultValue="")
Definition: ConfigFile.cpp:89
RefCountingObjectPtr
Definition: RefCountingObjectPtr.h:24
RoR::LocalStorage::getFloat
float getFloat(std::string key)
Definition: LocalStorage.cpp:110
Actor.h
RoR::LocalStorage::sectionName
std::string sectionName
Definition: LocalStorage.h:95
RoR::LocalStorage::saveDict
void saveDict()
Definition: LocalStorage.cpp:200
ImprovedConfigFile::setSetting
void setSetting(Ogre::String key, Ogre::String value, Ogre::String section=Ogre::BLANKSTRING)
Definition: ImprovedConfigFile.h:85
PlatformUtils.h
Platform-specific utilities. We use narrow UTF-8 encoded strings as paths. Inspired by http://utf8eve...
RoR::LocalStorage::m_resource_group
std::string m_resource_group
Definition: LocalStorage.h:93
Application.h
Central state/object manager and communications hub.
RoR::LocalStorage::get
std::string get(std::string key)
Definition: LocalStorage.cpp:80
RoR::LocalStorage::getDegree
Ogre::Degree getDegree(std::string key)
Definition: LocalStorage.cpp:185
RoR::LocalStorage::set
void set(std::string key, const std::string &value)
Definition: LocalStorage.cpp:87
RoR::LocalStorage::eraseKey
void eraseKey(std::string key)
Definition: LocalStorage.cpp:237
RoR::LocalStorage::parseKey
void parseKey(std::string &inout_key, std::string &out_section)
Definition: LocalStorage.cpp:251
LocalStorage.h
RoR::LocalStorage::getQuaternion
Ogre::Quaternion getQuaternion(std::string key)
Definition: LocalStorage.cpp:155
ImprovedConfigFile::getSettingVector3
Ogre::Vector3 getSettingVector3(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
Definition: ImprovedConfigFile.h:172
ImprovedConfigFile::hasSetting
bool hasSetting(Ogre::String key, Ogre::String section="")
Definition: ImprovedConfigFile.h:53
ImprovedConfigFile::getSettingQuaternion
Ogre::Quaternion getSettingQuaternion(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
Definition: ImprovedConfigFile.h:202
RoR::LocalStorage::changeSection
void changeSection(const std::string &section)
Definition: LocalStorage.cpp:74
RoR::LocalStorage::exists
bool exists(std::string key)
Definition: LocalStorage.cpp:267
RoR::LocalStorage::~LocalStorage
virtual ~LocalStorage() override
Definition: LocalStorage.cpp:52
RoR::LocalStorage::getBool
bool getBool(std::string key)
Definition: LocalStorage.cpp:125
RoR
Definition: AppContext.h:36
RoR::LocalStorage::copyFrom
void copyFrom(LocalStoragePtr other)
Definition: LocalStorage.cpp:58
RoR::LocalStorage::LocalStorage
LocalStorage(std::string filename, const std::string &section_name, const std::string &rg_name)
Definition: LocalStorage.cpp:33