Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
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
29using namespace RoR;
30
31
32/* class that implements the localStorage interface for the scripts */
33LocalStorage::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
74void LocalStorage::changeSection(const std::string &section)
75{
76 sectionName = section.substr(0, section.find(".", 0));
77}
78
79// getters and setters
80std::string LocalStorage::get(std::string key)
81{
82 std::string sec;
83 parseKey(key, sec);
84 return getString(key, sec);
85}
86
87void 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
95int LocalStorage::getInt(std::string key)
96{
97 std::string sec;
98 parseKey(key, sec);
99 return getSettingInt(key, sec);
100}
101
102void 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
110float LocalStorage::getFloat(std::string key)
111{
112 std::string sec;
113 parseKey(key, sec);
114 return getSettingReal(key, sec);
115}
116
117void 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
125bool LocalStorage::getBool(std::string key)
126{
127 std::string sec;
128 parseKey(key, sec);
129 return getSettingBool(key, sec);
130}
131
132void 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
140Ogre::Vector3 LocalStorage::getVector3(std::string key)
141{
142 std::string sec;
143 parseKey(key, sec);
144 return getSettingVector3(key, sec);
145}
146
147void 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
155Ogre::Quaternion LocalStorage::getQuaternion(std::string key)
156{
157 std::string sec;
158 parseKey(key, sec);
159 return getSettingQuaternion(key, sec);
160}
161
162void 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
170Ogre::Radian LocalStorage::getRadian(std::string key)
171{
172 std::string sec;
173 parseKey(key, sec);
174 return getSettingRadian(key, sec);
175}
176
177void 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
185Ogre::Degree LocalStorage::getDegree(std::string key)
186{
187 std::string sec;
188 parseKey(key, sec);
189 return Ogre::Degree(getSettingRadian(key, sec));
190}
191
192void 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
237void 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
251void 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
267bool LocalStorage::exists(std::string key)
268{
269 std::string sec;
270 parseKey(key, sec);
271 return hasSetting(key, sec);
272}
273
Central state/object manager and communications hub.
#define RGN_CACHE
Definition Application.h:46
Platform-specific utilities. We use narrow UTF-8 encoded strings as paths. Inspired by http://utf8eve...
void loadImprovedCfg(std::string const &filename, std::string const &resource_group_name)
bool saveImprovedCfg(std::string const &filename, std::string const &resource_group_name)
Ogre::Vector3 getSettingVector3(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
Ogre::Real getSettingReal(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
Ogre::Quaternion getSettingQuaternion(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
int getSettingInt(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
void setSetting(Ogre::String key, Ogre::String value, Ogre::String section=Ogre::BLANKSTRING)
Ogre::Radian getSettingRadian(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
bool hasSetting(Ogre::String key, Ogre::String section="")
bool getSettingBool(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
Ogre::String getString(Ogre::String const &key, Ogre::String const &section, Ogre::String const &defaultValue="")
Ogre::Degree getDegree(std::string key)
void parseKey(std::string &inout_key, std::string &out_section)
std::string m_filename
int getInt(std::string key)
void eraseKey(std::string key)
Ogre::Radian getRadian(std::string key)
Ogre::Vector3 getVector3(std::string key)
Ogre::Quaternion getQuaternion(std::string key)
bool saved
Inverted 'dirty flag'.
LocalStorage(std::string filename, const std::string &section_name, const std::string &rg_name)
std::string m_resource_group
float getFloat(std::string key)
void changeSection(const std::string &section)
virtual ~LocalStorage() override
bool exists(std::string key)
std::string sectionName
void copyFrom(LocalStoragePtr other)
void set(std::string key, const std::string &value)
bool getBool(std::string key)
std::string get(std::string key)
void LogFormat(const char *format,...)
Improved logging utility. Uses fixed 2Kb buffer.