Update to 14.0-beta1

This commit is contained in:
dP
2024-02-04 02:18:17 +05:30
parent 79037e2c65
commit 33ef333b57
1325 changed files with 138461 additions and 70983 deletions
+111 -56
View File
@@ -13,10 +13,9 @@
#include <variant>
#include "saveload/saveload.h"
enum SettingFlag : uint16 {
enum SettingFlag : uint16_t {
SF_NONE = 0,
SF_GUI_0_IS_SPECIAL = 1 << 0, ///< A value of zero is possible and has a custom string (the one after "strval").
SF_GUI_NEGATIVE_IS_SPECIAL = 1 << 1, ///< A negative value has another string (the one after "strval").
SF_GUI_DROPDOWN = 1 << 2, ///< The value represents a limited number of string-options (internally integer) presented as dropdown.
SF_GUI_CURRENCY = 1 << 3, ///< The number represents money, so when reading value multiply by exchange rate.
SF_NETWORK_ONLY = 1 << 4, ///< This setting only applies to network games.
@@ -70,9 +69,9 @@ struct IniItem;
/** Properties of config file settings. */
struct SettingDesc {
SettingDesc(SaveLoad save, SettingFlag flags, bool startup) :
SettingDesc(const SaveLoad &save, SettingFlag flags, bool startup) :
flags(flags), startup(startup), save(save) {}
virtual ~SettingDesc() {}
virtual ~SettingDesc() = default;
SettingFlag flags; ///< Handles how a setting would show up in the GUI (text/currency, etc.).
bool startup; ///< Setting has to be loaded directly at startup?.
@@ -111,7 +110,7 @@ struct SettingDesc {
* @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;
virtual std::string FormatValue(const void *object) const = 0;
/**
* Parse/read the value from the Ini item into the setting associated with this object.
@@ -130,10 +129,22 @@ struct SettingDesc {
* @return True if the value is definitely the same (might be false when the same).
*/
virtual bool IsSameValue(const IniItem *item, void *object) const = 0;
/**
* Check whether the value is the same as the default value.
*
* @param object The object the setting is in.
* @return true iff the value is the default value.
*/
virtual bool IsDefaultValue(void *object) const = 0;
};
/** Base integer type, including boolean, settings. Only these are shown in the settings UI. */
struct IntSettingDesc : SettingDesc {
typedef StringID GetTitleCallback(const IntSettingDesc &sd);
typedef StringID GetHelpCallback(const IntSettingDesc &sd);
typedef void SetValueDParamsCallback(const IntSettingDesc &sd, uint first_param, int32_t value);
/**
* A check to be performed before the setting gets changed. The passed integer may be
* changed by the check if that is important, for example to remove some unwanted bit.
@@ -142,31 +153,73 @@ struct IntSettingDesc : SettingDesc {
* @param value The prospective new value for the setting.
* @return True when the setting is accepted.
*/
typedef bool PreChangeCheck(int32 &value);
typedef bool PreChangeCheck(int32_t &value);
/**
* A callback to denote that a setting has been changed.
* @param The new value for the setting.
*/
typedef void PostChangeCallback(int32 value);
typedef void PostChangeCallback(int32_t value);
IntSettingDesc(SaveLoad save, SettingFlag flags, bool startup, int32 def,
int32 min, uint32 max, int32 interval, StringID str, StringID str_help, StringID str_val,
SettingCategory cat, PreChangeCheck pre_check, PostChangeCallback post_callback) :
SettingDesc(save, flags, startup), def(def), min(min), max(max), interval(interval),
template <
typename Tdef,
typename Tmin,
typename Tmax,
typename Tinterval,
std::enable_if_t<std::disjunction_v<std::is_convertible<Tdef, int32_t>, std::is_base_of<StrongTypedefBase, Tdef>>, int> = 0,
std::enable_if_t<std::disjunction_v<std::is_convertible<Tmin, int32_t>, std::is_base_of<StrongTypedefBase, Tmin>>, int> = 0,
std::enable_if_t<std::disjunction_v<std::is_convertible<Tmax, uint32_t>, std::is_base_of<StrongTypedefBase, Tmax>>, int> = 0,
std::enable_if_t<std::disjunction_v<std::is_convertible<Tinterval, int32_t>, std::is_base_of<StrongTypedefBase, Tinterval>>, int> = 0
>
IntSettingDesc(const SaveLoad &save, SettingFlag flags, bool startup, Tdef def,
Tmin min, Tmax max, Tinterval interval, StringID str, StringID str_help, StringID str_val,
SettingCategory cat, PreChangeCheck pre_check, PostChangeCallback post_callback,
GetTitleCallback get_title_cb, GetHelpCallback get_help_cb, SetValueDParamsCallback set_value_dparams_cb) :
SettingDesc(save, flags, startup),
str(str), str_help(str_help), str_val(str_val), cat(cat), pre_check(pre_check),
post_callback(post_callback) {}
virtual ~IntSettingDesc() {}
post_callback(post_callback),
get_title_cb(get_title_cb), get_help_cb(get_help_cb), set_value_dparams_cb(set_value_dparams_cb) {
if constexpr (std::is_base_of_v<StrongTypedefBase, Tdef>) {
this->def = def.base();
} else {
this->def = def;
}
int32 def; ///< default value given when none is present
int32 min; ///< minimum values
uint32 max; ///< maximum values
int32 interval; ///< the interval to use between settings in the 'settings' window. If interval is '0' the interval is dynamically determined
if constexpr (std::is_base_of_v<StrongTypedefBase, Tmin>) {
this->min = min.base();
} else {
this->min = min;
}
if constexpr (std::is_base_of_v<StrongTypedefBase, Tmax>) {
this->max = max.base();
} else {
this->max = max;
}
if constexpr (std::is_base_of_v<StrongTypedefBase, Tinterval>) {
this->interval = interval.base();
} else {
this->interval = interval;
}
}
int32_t def; ///< default value given when none is present
int32_t min; ///< minimum values
uint32_t max; ///< maximum values
int32_t interval; ///< the interval to use between settings in the 'settings' window. If interval is '0' the interval is dynamically determined
StringID str; ///< (translated) string with descriptive text; gui and console
StringID str_help; ///< (Translated) string with help text; gui only.
StringID str_val; ///< (Translated) first string describing the value.
SettingCategory cat; ///< assigned categories of the setting
PreChangeCheck *pre_check; ///< Callback to check for the validity of the setting.
PostChangeCallback *post_callback; ///< Callback when the setting has been changed.
GetTitleCallback *get_title_cb;
GetHelpCallback *get_help_cb;
SetValueDParamsCallback *set_value_dparams_cb;
StringID GetTitle() const;
StringID GetHelp() const;
void SetValueDParams(uint first_param, int32_t value) const;
/**
* Check whether this setting is a boolean type setting.
@@ -175,72 +228,74 @@ struct IntSettingDesc : SettingDesc {
virtual bool IsBoolSetting() const { return false; }
bool IsIntSetting() const override { return true; }
void ChangeValue(const void *object, int32 newvalue) const;
void MakeValueValidAndWrite(const void *object, int32 value) const;
void ChangeValue(const void *object, int32_t newvalue) const;
void MakeValueValidAndWrite(const void *object, int32_t value) const;
virtual size_t ParseValue(const char *str) const;
void FormatValue(char *buf, const char *last, const void *object) const override;
std::string FormatValue(const void *object) const override;
void ParseValue(const IniItem *item, void *object) const override;
bool IsSameValue(const IniItem *item, void *object) const override;
int32 Read(const void *object) const;
bool IsDefaultValue(void *object) const override;
int32_t Read(const void *object) const;
private:
void MakeValueValid(int32 &value) const;
void Write(const void *object, int32 value) const;
void MakeValueValid(int32_t &value) const;
void Write(const void *object, int32_t value) const;
};
/** Boolean setting. */
struct BoolSettingDesc : IntSettingDesc {
BoolSettingDesc(SaveLoad save, SettingFlag flags, bool startup, bool def,
BoolSettingDesc(const SaveLoad &save, SettingFlag flags, bool startup, bool def,
StringID str, StringID str_help, StringID str_val, SettingCategory cat,
PreChangeCheck pre_check, PostChangeCallback post_callback) :
IntSettingDesc(save, flags, startup, def, 0, 1, 0, str, str_help, str_val, cat,
pre_check, post_callback) {}
virtual ~BoolSettingDesc() {}
PreChangeCheck pre_check, PostChangeCallback post_callback,
GetTitleCallback get_title_cb, GetHelpCallback get_help_cb, SetValueDParamsCallback set_value_dparams_cb) :
IntSettingDesc(save, flags, startup, def ? 1 : 0, 0, 1, 0, str, str_help, str_val, cat,
pre_check, post_callback, get_title_cb, get_help_cb, set_value_dparams_cb) {}
static std::optional<bool> ParseSingleValue(const char *str);
bool IsBoolSetting() const override { return true; }
size_t ParseValue(const char *str) const override;
void FormatValue(char *buf, const char *last, const void *object) const override;
std::string FormatValue(const void *object) const override;
};
/** One of many setting. */
struct OneOfManySettingDesc : IntSettingDesc {
typedef size_t OnConvert(const char *value); ///< callback prototype for conversion error
OneOfManySettingDesc(SaveLoad save, SettingFlag flags, bool startup, int32 def,
int32 max, StringID str, StringID str_help, StringID str_val, SettingCategory cat,
OneOfManySettingDesc(const SaveLoad &save, SettingFlag flags, bool startup, int32_t def,
int32_t max, StringID str, StringID str_help, StringID str_val, SettingCategory cat,
PreChangeCheck pre_check, PostChangeCallback post_callback,
GetTitleCallback get_title_cb, GetHelpCallback get_help_cb, SetValueDParamsCallback set_value_dparams_cb,
std::initializer_list<const char *> many, OnConvert *many_cnvt) :
IntSettingDesc(save, flags, startup, def, 0, max, 0, str, str_help, str_val, cat,
pre_check, post_callback), many_cnvt(many_cnvt)
pre_check, post_callback, get_title_cb, get_help_cb, set_value_dparams_cb), many_cnvt(many_cnvt)
{
for (auto one : many) this->many.push_back(one);
}
virtual ~OneOfManySettingDesc() {}
std::vector<std::string> many; ///< possible values for this type
OnConvert *many_cnvt; ///< callback procedure when loading value mechanism fails
static size_t ParseSingleValue(const char *str, size_t len, const std::vector<std::string> &many);
char *FormatSingleValue(char *buf, const char *last, uint id) const;
std::string FormatSingleValue(uint id) const;
size_t ParseValue(const char *str) const override;
void FormatValue(char *buf, const char *last, const void *object) const override;
std::string FormatValue(const void *object) const override;
};
/** Many of many setting. */
struct ManyOfManySettingDesc : OneOfManySettingDesc {
ManyOfManySettingDesc(SaveLoad save, SettingFlag flags, bool startup,
int32 def, StringID str, StringID str_help, StringID str_val, SettingCategory cat,
ManyOfManySettingDesc(const SaveLoad &save, SettingFlag flags, bool startup,
int32_t def, StringID str, StringID str_help, StringID str_val, SettingCategory cat,
PreChangeCheck pre_check, PostChangeCallback post_callback,
GetTitleCallback get_title_cb, GetHelpCallback get_help_cb, SetValueDParamsCallback set_value_dparams_cb,
std::initializer_list<const char *> many, OnConvert *many_cnvt) :
OneOfManySettingDesc(save, flags, startup, def, (1 << many.size()) - 1, str, str_help,
str_val, cat, pre_check, post_callback, many, many_cnvt) {}
virtual ~ManyOfManySettingDesc() {}
str_val, cat, pre_check, post_callback, get_title_cb, get_help_cb, set_value_dparams_cb, many, many_cnvt) {}
size_t ParseValue(const char *str) const override;
void FormatValue(char *buf, const char *last, const void *object) const override;
std::string FormatValue(const void *object) const override;
};
/** String settings. */
@@ -260,23 +315,23 @@ struct StringSettingDesc : SettingDesc {
*/
typedef void PostChangeCallback(const std::string &value);
StringSettingDesc(SaveLoad save, SettingFlag flags, bool startup, const char *def,
uint32 max_length, PreChangeCheck pre_check, PostChangeCallback post_callback) :
StringSettingDesc(const SaveLoad &save, SettingFlag flags, bool startup, const char *def,
uint32_t max_length, PreChangeCheck pre_check, PostChangeCallback post_callback) :
SettingDesc(save, flags, startup), def(def == nullptr ? "" : def), max_length(max_length),
pre_check(pre_check), post_callback(post_callback) {}
virtual ~StringSettingDesc() {}
std::string def; ///< Default value given when none is present
uint32 max_length; ///< Maximum length of the string, 0 means no maximum length
uint32_t max_length; ///< Maximum length of the string, 0 means no maximum length
PreChangeCheck *pre_check; ///< Callback to check for the validity of the setting.
PostChangeCallback *post_callback; ///< Callback when the setting has been changed.
bool IsStringSetting() const override { return true; }
void ChangeValue(const void *object, std::string &newval) const;
void FormatValue(char *buf, const char *last, const void *object) const override;
std::string FormatValue(const void *object) const override;
void ParseValue(const IniItem *item, void *object) const override;
bool IsSameValue(const IniItem *item, void *object) const override;
bool IsDefaultValue(void *object) const override;
const std::string &Read(const void *object) const;
private:
@@ -286,26 +341,26 @@ private:
/** List/array settings. */
struct ListSettingDesc : SettingDesc {
ListSettingDesc(SaveLoad save, SettingFlag flags, bool startup, const char *def) :
ListSettingDesc(const SaveLoad &save, SettingFlag flags, bool startup, const char *def) :
SettingDesc(save, flags, startup), def(def) {}
virtual ~ListSettingDesc() {}
const char *def; ///< default value given when none is present
void FormatValue(char *buf, const char *last, const void *object) const override;
std::string FormatValue(const void *object) const override;
void ParseValue(const IniItem *item, void *object) const override;
bool IsSameValue(const IniItem *item, void *object) const override;
bool IsDefaultValue(void *object) const override;
};
/** Placeholder for settings that have been removed, but might still linger in the savegame. */
struct NullSettingDesc : SettingDesc {
NullSettingDesc(SaveLoad save) :
NullSettingDesc(const SaveLoad &save) :
SettingDesc(save, SF_NOT_IN_CONFIG, false) {}
virtual ~NullSettingDesc() {}
void FormatValue(char *buf, const char *last, const void *object) const override { NOT_REACHED(); }
void ParseValue(const IniItem *item, void *object) const override { NOT_REACHED(); }
bool IsSameValue(const IniItem *item, void *object) const override { NOT_REACHED(); }
std::string FormatValue(const void *) const override { NOT_REACHED(); }
void ParseValue(const IniItem *, void *) const override { NOT_REACHED(); }
bool IsSameValue(const IniItem *, void *) const override { NOT_REACHED(); }
bool IsDefaultValue(void *) const override { NOT_REACHED(); }
};
typedef std::variant<IntSettingDesc, BoolSettingDesc, OneOfManySettingDesc, ManyOfManySettingDesc, StringSettingDesc, ListSettingDesc, NullSettingDesc> SettingVariant;
@@ -320,11 +375,11 @@ static constexpr const SettingDesc *GetSettingDesc(const SettingVariant &desc)
return std::visit([](auto&& arg) -> const SettingDesc * { return &arg; }, desc);
}
typedef span<const SettingVariant> SettingTable;
typedef std::span<const SettingVariant> SettingTable;
const SettingDesc *GetSettingFromName(const std::string_view name);
void GetSaveLoadFromSettingTable(SettingTable settings, std::vector<SaveLoad> &saveloads);
bool SetSettingValue(const IntSettingDesc *sd, int32 value, bool force_newgame = false);
bool SetSettingValue(const IntSettingDesc *sd, int32_t value, bool force_newgame = false);
bool SetSettingValue(const StringSettingDesc *sd, const std::string value, bool force_newgame = false);
#endif /* SETTINGS_INTERNAL_H */