Skip to content

Commit f0ff575

Browse files
committed
Delayed load of settings (fixes default value bug)
1 parent 451e939 commit f0ff575

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

Diff for: Sources/Core/Settings.cpp

+30-11
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,7 @@ namespace spades {
6464
if(it == items.end()){
6565
Item *item = new Item();
6666
item->name = name;
67-
68-
char buf[2048];
69-
buf[2047] = 0;
70-
SPAssert(pref != NULL);
71-
pref->get(name.c_str(), buf, def.c_str(), 2047);
72-
SPAssert(buf);
73-
74-
item->string = buf;
75-
76-
item->value = static_cast<float>(atof(item->string.c_str()));
77-
item->intValue = atoi(item->string.c_str());
67+
item->loaded = false;
7868

7969
item->desc = desc;
8070
item->defaultValue = def;
@@ -85,14 +75,35 @@ namespace spades {
8575
return it->second;
8676
}
8777

78+
void Settings::Item::Load() {
79+
if(this->loaded){
80+
return;
81+
}
82+
83+
char buf[2048];
84+
buf[2047] = 0;
85+
SPAssert(pref != NULL);
86+
pref->get(name.c_str(), buf, defaultValue.c_str(), 2047);
87+
SPAssert(buf);
88+
89+
this->string = buf;
90+
91+
this->value = static_cast<float>(atof(this->string.c_str()));
92+
this->intValue = atoi(this->string.c_str());
93+
94+
this->loaded = true;
95+
}
96+
8897
void Settings::Item::Set(const std::string &str) {
8998
string = str;
9099
value = static_cast<float>(atof(str.c_str()));
91100
intValue = atoi(str.c_str());
92101

93102
pref->set(name.c_str(), string.c_str());
103+
loaded = true;
94104
}
95105

106+
96107
void Settings::Item::Set(int v) {
97108
char buf[256];
98109
sprintf(buf, "%d", v);
@@ -101,6 +112,7 @@ namespace spades {
101112
value = (float)v;
102113

103114
pref->set(name.c_str(), string.c_str());
115+
loaded = true;
104116
}
105117

106118
void Settings::Item::Set(float v){
@@ -111,6 +123,8 @@ namespace spades {
111123
value = v;
112124

113125
pref->set(name.c_str(), string.c_str());
126+
127+
loaded = true;
114128
}
115129

116130
Settings::ItemHandle::ItemHandle(const std::string& name,
@@ -139,18 +153,23 @@ namespace spades {
139153
item->Set(value);
140154
}
141155
Settings::ItemHandle::operator std::string() {
156+
item->Load();
142157
return item->string;
143158
}
144159
Settings::ItemHandle::operator int() {
160+
item->Load();
145161
return item->intValue;
146162
}
147163
Settings::ItemHandle::operator float() {
164+
item->Load();
148165
return item->value;
149166
}
150167
Settings::ItemHandle::operator bool() {
168+
item->Load();
151169
return item->intValue != 0;
152170
}
153171
const char *Settings::ItemHandle::CString() {
172+
item->Load();
154173
return item->string.c_str();
155174
}
156175
std::string Settings::ItemHandle::GetDescription() {

Diff for: Sources/Core/Settings.h

+3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ namespace spades {
3131
std::string string;
3232
float value;
3333
int intValue;
34+
35+
bool loaded;
3436

3537
std::string defaultValue;
3638
std::string desc;
3739

40+
void Load();
3841
void Set(const std::string&);
3942
void Set(int);
4043
void Set(float);

0 commit comments

Comments
 (0)