summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/settings.cpp92
-rw-r--r--src/settings_internal.h17
2 files changed, 64 insertions, 45 deletions
diff --git a/src/settings.cpp b/src/settings.cpp
index 2c7d3aefe..2964bad64 100644
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -285,13 +285,13 @@ static bool LoadIntList(const char *str, void *array, int nelems, VarType type)
* @param nelems the number of elements the array holds.
* @param type the type of elements the array holds (eg INT8, UINT16, etc.)
*/
-static void MakeIntList(char *buf, const char *last, const void *array, int nelems, VarType type)
+void ListSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
{
+ const byte *p = static_cast<const byte *>(GetVariableAddress(object, &this->save));
int i, v = 0;
- const byte *p = (const byte *)array;
- for (i = 0; i != nelems; i++) {
- switch (GetVarMemType(type)) {
+ for (i = 0; i != this->save.length; i++) {
+ switch (GetVarMemType(this->save.conv)) {
case SLE_VAR_BL:
case SLE_VAR_I8: v = *(const int8 *)p; p += 1; break;
case SLE_VAR_U8: v = *(const uint8 *)p; p += 1; break;
@@ -301,9 +301,9 @@ static void MakeIntList(char *buf, const char *last, const void *array, int nele
case SLE_VAR_U32: v = *(const uint32 *)p; p += 4; break;
default: NOT_REACHED();
}
- if (IsSignedVarMemType(type)) {
+ if (IsSignedVarMemType(this->save.conv)) {
buf += seprintf(buf, last, (i == 0) ? "%d" : ",%d", v);
- } else if (type & SLF_HEX) {
+ } else if (this->save.conv & SLF_HEX) {
buf += seprintf(buf, last, (i == 0) ? "0x%X" : ",0x%X", v);
} else {
buf += seprintf(buf, last, (i == 0) ? "%u" : ",%u", v);
@@ -549,6 +549,16 @@ static void Write_ValidateStdString(void *ptr, const SettingDesc *sd, const char
}
/**
+ * Read the string from the the actual setting.
+ * @param object The object the setting is to be saved in.
+ * @return The value of the saved string.
+ */
+const std::string &StringSettingDesc::Read(const void *object) const
+{
+ return *reinterpret_cast<std::string *>(GetVariableAddress(object, &this->save));
+}
+
+/**
* Load values from a group of an IniFile structure into the internal representation
* @param ini pointer to IniFile structure that holds administrative information
* @param settings_table table with SettingDesc structures whose internally pointed variables will
@@ -665,11 +675,11 @@ static void IniSaveSettings(IniFile *ini, const SettingTable &settings_table, co
}
item = group->GetItem(s, true);
- ptr = GetVariableAddress(object, sld);
if (item->value.has_value()) {
/* check if the value is the same as the old value */
const void *p = StringToVal(sdb, item->value->c_str());
+ ptr = GetVariableAddress(object, sld);
/* The main type of a variable/setting is in bytes 8-15
* The subtype (what kind of numbers do we have there) is in 0-7 */
@@ -707,48 +717,40 @@ static void IniSaveSettings(IniFile *ini, const SettingTable &settings_table, co
}
/* Value has changed, get the new value and put it into a buffer */
- switch (sdb->cmd) {
- case SDT_BOOLX:
- case SDT_NUMX:
- case SDT_ONEOFMANY:
- case SDT_MANYOFMANY: {
- uint32 i = (uint32)ReadValue(ptr, sld->conv);
-
- switch (sdb->cmd) {
- case SDT_BOOLX: strecpy(buf, (i != 0) ? "true" : "false", lastof(buf)); break;
- case SDT_NUMX: seprintf(buf, lastof(buf), IsSignedVarMemType(sld->conv) ? "%d" : (sld->conv & SLF_HEX) ? "%X" : "%u", i); break;
- case SDT_ONEOFMANY: MakeOneOfMany(buf, lastof(buf), sdb->many, i); break;
- case SDT_MANYOFMANY: MakeManyOfMany(buf, lastof(buf), sdb->many, i); break;
- default: NOT_REACHED();
- }
- break;
- }
+ sdb->FormatValue(buf, lastof(buf), object);
- case SDT_STDSTRING:
- switch (GetVarMemType(sld->conv)) {
- case SLE_VAR_STR: strecpy(buf, reinterpret_cast<std::string *>(ptr)->c_str(), lastof(buf)); break;
-
- case SLE_VAR_STRQ:
- if (reinterpret_cast<std::string *>(ptr)->empty()) {
- buf[0] = '\0';
- } else {
- seprintf(buf, lastof(buf), "\"%s\"", reinterpret_cast<std::string *>(ptr)->c_str());
- }
- break;
-
- default: NOT_REACHED();
- }
- break;
+ /* The value is different, that means we have to write it to the ini */
+ item->value.emplace(buf);
+ }
+}
- case SDT_INTLIST:
- MakeIntList(buf, lastof(buf), ptr, sld->length, sld->conv);
- break;
+void IntSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
+{
+ uint32 i = (uint32)ReadValue(GetVariableAddress(object, &this->save), this->save.conv);
+ switch (this->cmd) {
+ case SDT_BOOLX: strecpy(buf, (i != 0) ? "true" : "false", last); break;
+ case SDT_NUMX: seprintf(buf, last, IsSignedVarMemType(this->save.conv) ? "%d" : (this->save.conv & SLF_HEX) ? "%X" : "%u", i); break;
+ case SDT_ONEOFMANY: MakeOneOfMany(buf, last, this->many, i); break;
+ case SDT_MANYOFMANY: MakeManyOfMany(buf, last, this->many, i); break;
+ default: NOT_REACHED();
+ }
+}
- default: NOT_REACHED();
- }
+void StringSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
+{
+ const std::string &str = this->Read(object);
+ switch (GetVarMemType(this->save. conv)) {
+ case SLE_VAR_STR: strecpy(buf, str.c_str(), last); break;
- /* The value is different, that means we have to write it to the ini */
- item->value.emplace(buf);
+ case SLE_VAR_STRQ:
+ if (str.empty()) {
+ buf[0] = '\0';
+ } else {
+ seprintf(buf, last, "\"%s\"", str.c_str());
+ }
+ break;
+
+ default: NOT_REACHED();
}
}
diff --git a/src/settings_internal.h b/src/settings_internal.h
index 1accc47ed..ee8b59be1 100644
--- a/src/settings_internal.h
+++ b/src/settings_internal.h
@@ -110,6 +110,14 @@ struct SettingDesc {
bool IsEditable(bool do_command = false) const;
SettingType GetType() const;
+
+ /**
+ * Format the value of the setting associated with this object.
+ * @param buf The before of the buffer to format into.
+ * @param last The end of the buffer to format into.
+ * @param object The object the setting is in.
+ */
+ virtual void FormatValue(char *buf, const char *last, const void *object) const = 0;
};
/** Integer type, including boolean, settings. Only these are shown in the settings UI. */
@@ -120,6 +128,8 @@ struct IntSettingDesc : SettingDesc {
SettingDesc(save, name, (void*)(size_t)def, cmd, flags, min, max, interval, many, str, str_help, str_val,
proc, many_cnvt, cat, startup) {}
virtual ~IntSettingDesc() {}
+
+ void FormatValue(char *buf, const char *last, const void *object) const override;
};
/** String settings. */
@@ -128,6 +138,9 @@ struct StringSettingDesc : SettingDesc {
uint32 max_length, OnChange proc) :
SettingDesc(save, name, def, cmd, flags, 0, max_length, 0, nullptr, 0, 0, 0, proc, nullptr, SC_NONE, startup) {}
virtual ~StringSettingDesc() {}
+
+ void FormatValue(char *buf, const char *last, const void *object) const override;
+ const std::string &Read(const void *object) const;
};
/** List/array settings. */
@@ -135,6 +148,8 @@ struct ListSettingDesc : SettingDesc {
ListSettingDesc(SaveLoad save, const char *name, SettingGuiFlag flags, SettingDescType cmd, bool startup, const char *def) :
SettingDesc(save, name, def, cmd, flags, 0, 0, 0, nullptr, 0, 0, 0, proc, nullptr, SC_NONE, startup) {}
virtual ~ListSettingDesc() {}
+
+ void FormatValue(char *buf, const char *last, const void *object) const override;
};
/** Placeholder for settings that have been removed, but might still linger in the savegame. */
@@ -142,6 +157,8 @@ struct NullSettingDesc : SettingDesc {
NullSettingDesc(SaveLoad save) :
SettingDesc(save, "", nullptr, SDT_NULL, SGF_NONE, 0, 0, 0, nullptr, 0, 0, 0, nullptr, nullptr, SC_NONE, false) {}
virtual ~NullSettingDesc() {}
+
+ void FormatValue(char *buf, const char *last, const void *object) const override { NOT_REACHED(); }
};
typedef std::initializer_list<std::unique_ptr<const SettingDesc>> SettingTable;