Codechange: use separate pre and post callbacks for string settings

This commit is contained in:
rubidium42 2021-05-24 09:44:20 +02:00 committed by rubidium42
parent ea9715d970
commit e2f5d9e561
8 changed files with 89 additions and 78 deletions

View File

@ -1344,27 +1344,22 @@ bool NetworkValidateClientName()
}
/**
* Send the server our name.
* Send the server our name as callback from the setting.
* @param newname The new client name.
*/
void NetworkUpdateClientName()
void NetworkUpdateClientName(const std::string &client_name)
{
NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(_network_own_client_id);
if (ci == nullptr) return;
/* There is no validation on string settings, it is actually a post change callback.
* This method is called from that post change callback. So, when the client name is
* changed via the console there is no easy way to prevent an invalid name. Though,
* we can prevent it getting sent here. */
if (!NetworkValidateClientName()) return;
/* Don't change the name if it is the same as the old name */
if (_settings_client.network.client_name.compare(ci->client_name) != 0) {
if (client_name.compare(ci->client_name) != 0) {
if (!_network_server) {
MyClient::SendSetName(_settings_client.network.client_name.c_str());
MyClient::SendSetName(client_name.c_str());
} else {
/* Copy to a temporary buffer so no #n gets added after our name in the settings when there are duplicate names. */
char temporary_name[NETWORK_CLIENT_NAME_LENGTH];
strecpy(temporary_name, _settings_client.network.client_name.c_str(), lastof(temporary_name));
strecpy(temporary_name, client_name.c_str(), lastof(temporary_name));
if (NetworkFindName(temporary_name, lastof(temporary_name))) {
NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, temporary_name);
ci->client_name = temporary_name;

View File

@ -38,7 +38,7 @@ byte NetworkSpectatorCount();
bool NetworkIsValidClientName(const std::string_view client_name);
bool NetworkValidateClientName();
bool NetworkValidateClientName(std::string &client_name);
void NetworkUpdateClientName();
void NetworkUpdateClientName(const std::string &client_name);
bool NetworkCompanyHasClients(CompanyID company);
std::string NetworkChangeCompanyPassword(CompanyID company_id, std::string password);
void NetworkReboot();

View File

@ -1343,37 +1343,28 @@ static bool InvalidateShipPathCache(int32 p1)
return true;
}
static bool UpdateClientName(int32 p1)
/**
* Replace a passwords that are a literal asterisk with an empty string.
* @param newval The new string value for this password field.
* @return Always true.
*/
static bool ReplaceAsteriskWithEmptyPassword(std::string &newval)
{
NetworkUpdateClientName();
return true;
}
static bool UpdateServerPassword(int32 p1)
{
if (_settings_client.network.server_password.compare("*") == 0) {
_settings_client.network.server_password.clear();
}
NetworkServerUpdateGameInfo();
return true;
}
static bool UpdateRconPassword(int32 p1)
{
if (_settings_client.network.rcon_password.compare("*") == 0) {
_settings_client.network.rcon_password.clear();
}
if (newval.compare("*") == 0) newval.clear();
return true;
}
static bool UpdateClientConfigValues(int32 p1)
{
UpdateClientConfigValues();
return true;
}
/** Update the game info, and send it to the clients when we are running as a server. */
static void UpdateClientConfigValues()
{
NetworkServerUpdateGameInfo();
if (_network_server) NetworkServerSendConfigUpdate();
return true;
}
/* End - Callback Functions */
@ -2069,8 +2060,10 @@ bool SetSettingValue(const StringSettingDesc *sd, std::string value, bool force_
void StringSettingDesc::ChangeValue(const void *object, std::string &newval) const
{
this->MakeValueValid(newval);
if (this->pre_check != nullptr && !this->pre_check(newval)) return;
this->Write(object, newval);
if (this->proc != nullptr) this->proc(0);
if (this->post_callback != nullptr) this->post_callback(newval);
if (_save_config) SaveToConfig();
}

View File

@ -212,15 +212,31 @@ struct ManyOfManySettingDesc : OneOfManySettingDesc {
/** String settings. */
struct StringSettingDesc : SettingDesc {
/**
* A check to be performed before the setting gets changed. The passed string may be
* changed by the check if that is important, for example to remove unwanted white
* space. The return value denotes whether the value, potentially after the changes,
* is allowed to be used/set in the configuration.
* @param value The prospective new value for the setting.
* @return True when the setting is accepted.
*/
typedef bool PreChangeCheck(std::string &value);
/**
* A callback to denote that a setting has been changed.
* @param The new value for the setting.
*/
typedef void PostChangeCallback(const std::string &value);
StringSettingDesc(SaveLoad save, const char *name, SettingGuiFlag flags, bool startup, const char *def,
uint32 max_length, OnChange proc) :
uint32 max_length, PreChangeCheck pre_check, PostChangeCallback post_callback) :
SettingDesc(save, name, flags, startup), def(def == nullptr ? "" : def), max_length(max_length),
proc(proc) {}
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
OnChange *proc; ///< callback procedure for when the value is changed
std::string def; ///< Default value given when none is present
uint32 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;

View File

@ -9,8 +9,8 @@ static const SettingTable _currency_settings{
[post-amble]
};
[templates]
SDT_VAR = SDT_VAR ($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDT_SSTR = SDT_SSTR($base, $var, $type, $flags, $guiflags, $def, $proc, $from, $to, $cat, $extra, $startup),
SDT_VAR = SDT_VAR ($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDT_SSTR = SDT_SSTR($base, $var, $type, $flags, $guiflags, $def, $pre_cb, $post_cb, $from, $to, $cat, $extra, $startup),
[validation]
SDT_VAR = static_assert($max <= MAX_$type, "Maximum value for $base.$var exceeds storage size");
@ -23,6 +23,8 @@ str = STR_NULL
strhelp = STR_CONFIG_SETTING_NO_EXPLANATION_AVAILABLE_HELPTEXT
strval = STR_NULL
proc = nullptr
pre_cb = nullptr
post_cb = nullptr
load = nullptr
from = SL_MIN_VERSION
to = SL_MAX_VERSION

View File

@ -21,12 +21,12 @@ static const SettingTable _misc_settings{
[post-amble]
};
[templates]
SDTG_LIST = SDTG_LIST($name, $type, $flags, $guiflags, $var, $def, $length, $from, $to, $cat, $extra, $startup),
SDTG_MMANY = SDTG_MMANY($name, $type, $flags, $guiflags, $var, $def, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDTG_OMANY = SDTG_OMANY($name, $type, $flags, $guiflags, $var, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDTG_SSTR = SDTG_SSTR($name, $type, $flags, $guiflags, $var, $def, 0, $proc, $from, $to, $cat, $extra, $startup),
SDTG_BOOL = SDTG_BOOL($name, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDTG_LIST = SDTG_LIST($name, $type, $flags, $guiflags, $var, $def, $length, $from, $to, $cat, $extra, $startup),
SDTG_MMANY = SDTG_MMANY($name, $type, $flags, $guiflags, $var, $def, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDTG_OMANY = SDTG_OMANY($name, $type, $flags, $guiflags, $var, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDTG_SSTR = SDTG_SSTR($name, $type, $flags, $guiflags, $var, $def, 0, $pre_cb, $post_cb, $from, $to, $cat, $extra, $startup),
SDTG_BOOL = SDTG_BOOL($name, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
[validation]
SDTG_VAR = static_assert($max <= MAX_$type, "Maximum value for $var exceeds storage size");
@ -40,6 +40,8 @@ str = STR_NULL
strhelp = STR_CONFIG_SETTING_NO_EXPLANATION_AVAILABLE_HELPTEXT
strval = STR_NULL
proc = nullptr
pre_cb = nullptr
post_cb = nullptr
load = nullptr
from = SL_MIN_VERSION
to = SL_MAX_VERSION

View File

@ -67,8 +67,8 @@ static size_t ConvertLandscape(const char *value);
#define SDTG_LIST(name, type, flags, guiflags, var, def, length, from, to, cat, extra, startup)\
NSD(List, SLEG_GENERAL(SL_ARR, var, type | flags, length, from, to, extra), name, guiflags, startup, def)
#define SDTG_SSTR(name, type, flags, guiflags, var, def, max_length, proc, from, to, cat, extra, startup)\
NSD(String, SLEG_GENERAL(SL_STDSTR, var, type | flags, sizeof(var), from, to, extra), name, guiflags, startup, def, max_length, proc)
#define SDTG_SSTR(name, type, flags, guiflags, var, def, max_length, pre_check, post_callback, from, to, cat, extra, startup)\
NSD(String, SLEG_GENERAL(SL_STDSTR, var, type | flags, sizeof(var), from, to, extra), name, guiflags, startup, def, max_length, pre_check, post_callback)
#define SDTG_OMANY(name, type, flags, guiflags, var, def, max, full, str, strhelp, strval, proc, from, to, cat, extra, startup)\
NSD(OneOfMany, SLEG_GENERAL(SL_VAR, var, type | flags, 1, from, to, extra), name, guiflags, startup, def, max, str, strhelp, strval, cat, proc, full, nullptr)
@ -90,8 +90,8 @@ static size_t ConvertLandscape(const char *value);
#define SDT_LIST(base, var, type, flags, guiflags, def, from, to, cat, extra, startup)\
NSD(List, SLE_GENERAL(SL_ARR, base, var, type | flags, lengthof(((base*)8)->var), from, to, extra), #var, guiflags, startup, def)
#define SDT_SSTR(base, var, type, flags, guiflags, def, proc, from, to, cat, extra, startup)\
NSD(String, SLE_GENERAL(SL_STDSTR, base, var, type | flags, sizeof(((base*)8)->var), from, to, extra), #var, guiflags, startup, def, 0, proc)
#define SDT_SSTR(base, var, type, flags, guiflags, def, pre_check, post_callback, from, to, cat, extra, startup)\
NSD(String, SLE_GENERAL(SL_STDSTR, base, var, type | flags, sizeof(((base*)8)->var), from, to, extra), #var, guiflags, startup, def, 0, pre_check, post_callback)
#define SDT_OMANY(base, var, type, flags, guiflags, def, max, full, str, strhelp, strval, proc, from, to, load, cat, extra, startup)\
NSD(OneOfMany, SLE_GENERAL(SL_VAR, base, var, type | flags, 1, from, to, extra), #var, guiflags, startup, def, max, str, strhelp, strval, cat, proc, full, load)
@ -112,8 +112,8 @@ static size_t ConvertLandscape(const char *value);
#define SDTC_LIST(var, type, flags, guiflags, def, from, to, cat, extra, startup)\
SDTG_LIST(#var, type, flags, guiflags, _settings_client.var, def, lengthof(_settings_client.var), from, to, cat, extra, startup)
#define SDTC_SSTR(var, type, flags, guiflags, def, max_length, proc, from, to, cat, extra, startup)\
SDTG_SSTR(#var, type, flags, guiflags, _settings_client.var, def, max_length, proc, from, to, cat, extra, startup)\
#define SDTC_SSTR(var, type, flags, guiflags, def, max_length, pre_check, post_callback, from, to, cat, extra, startup)\
SDTG_SSTR(#var, type, flags, guiflags, _settings_client.var, def, max_length, pre_check, post_callback, from, to, cat, extra, startup)\
#define SDTC_OMANY(var, type, flags, guiflags, def, max, full, str, strhelp, strval, proc, from, to, cat, extra, startup)\
SDTG_OMANY(#var, type, flags, guiflags, _settings_client.var, def, max, full, str, strhelp, strval, proc, from, to, cat, extra, startup)

View File

@ -43,9 +43,8 @@ static bool SpriteZoomMinChanged(int32 p1);
static bool MaxVehiclesChanged(int32 p1);
static bool InvalidateShipPathCache(int32 p1);
static bool UpdateClientName(int32 p1);
static bool UpdateServerPassword(int32 p1);
static bool UpdateRconPassword(int32 p1);
static bool ReplaceAsteriskWithEmptyPassword(std::string &newval);
static void UpdateClientConfigValues();
static bool UpdateClientConfigValues(int32 p1);
/* End - Callback Functions for the various settings */
@ -54,7 +53,7 @@ static bool UpdateClientConfigValues(int32 p1);
* These include for example the GUI settings and will not be saved with the
* savegame.
* It is also a bit tricky since you would think that service_interval
* for example doesn't need to be synched. Every client assigns the
* for example does not need to be synched. Every client assigns the
* service_interval value to the v->service_interval, meaning that every client
* assigns its own value. If the setting was company-based, that would mean that
* vehicles could decide on different moments that they are heading back to a
@ -63,18 +62,18 @@ const SettingTable _settings{
[post-amble]
};
[templates]
SDTG_BOOL = SDTG_BOOL($name, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDTG_OMANY = SDTG_OMANY($name, $type, $flags, $guiflags, $var, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDTC_BOOL = SDTC_BOOL( $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDTC_LIST = SDTC_LIST( $var, $type, $flags, $guiflags, $def, $from, $to, $cat, $extra, $startup),
SDTC_OMANY = SDTC_OMANY( $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDTC_SSTR = SDTC_SSTR( $var, $type, $flags, $guiflags, $def, $length, $proc, $from, $to, $cat, $extra, $startup),
SDTC_VAR = SDTC_VAR( $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDT_BOOL = SDT_BOOL($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDT_OMANY = SDT_OMANY($base, $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $load, $cat, $extra, $startup),
SDT_SSTR = SDT_SSTR($base, $var, $type, $flags, $guiflags, $def, $proc, $from, $to, $cat, $extra, $startup),
SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDTG_BOOL = SDTG_BOOL($name, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDTG_OMANY = SDTG_OMANY($name, $type, $flags, $guiflags, $var, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDTC_BOOL = SDTC_BOOL( $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDTC_LIST = SDTC_LIST( $var, $type, $flags, $guiflags, $def, $from, $to, $cat, $extra, $startup),
SDTC_OMANY = SDTC_OMANY( $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDTC_SSTR = SDTC_SSTR( $var, $type, $flags, $guiflags, $def, $length, $pre_cb, $post_cb, $from, $to, $cat, $extra, $startup),
SDTC_VAR = SDTC_VAR( $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDT_BOOL = SDT_BOOL($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDT_OMANY = SDT_OMANY($base, $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $load, $cat, $extra, $startup),
SDT_SSTR = SDT_SSTR($base, $var, $type, $flags, $guiflags, $def, $pre_cb, $post_cb, $from, $to, $cat, $extra, $startup),
SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup),
SDT_NULL = SDT_NULL($length, $from, $to),
[validation]
@ -93,6 +92,8 @@ str = STR_NULL
strhelp = STR_CONFIG_SETTING_NO_EXPLANATION_AVAILABLE_HELPTEXT
strval = STR_NULL
proc = nullptr
pre_cb = nullptr
post_cb = nullptr
load = nullptr
from = SL_MIN_VERSION
to = SL_MAX_VERSION
@ -2646,7 +2647,7 @@ type = SLE_STRQ
from = SLV_118
flags = SLF_NO_NETWORK_SYNC
def = nullptr
proc = RedrawScreen
post_cb = [](auto) { MarkWholeScreenDirty(); }
cat = SC_BASIC
[SDT_SSTR]
@ -2656,7 +2657,7 @@ type = SLE_STRQ
from = SLV_118
flags = SLF_NO_NETWORK_SYNC
def = nullptr
proc = RedrawScreen
post_cb = [](auto) { MarkWholeScreenDirty(); }
cat = SC_BASIC
[SDT_SSTR]
@ -2666,7 +2667,7 @@ type = SLE_STRQ
from = SLV_126
flags = SLF_NO_NETWORK_SYNC
def = nullptr
proc = RedrawScreen
post_cb = [](auto) { MarkWholeScreenDirty(); }
cat = SC_BASIC
@ -3921,7 +3922,8 @@ type = SLE_STR
length = NETWORK_CLIENT_NAME_LENGTH
flags = SLF_NOT_IN_SAVE | SLF_NO_NETWORK_SYNC
def = nullptr
proc = UpdateClientName
pre_cb = NetworkValidateClientName
post_cb = NetworkUpdateClientName
cat = SC_BASIC
[SDTC_SSTR]
@ -3931,7 +3933,8 @@ length = NETWORK_PASSWORD_LENGTH
flags = SLF_NOT_IN_SAVE | SLF_NO_NETWORK_SYNC
guiflags = SGF_NETWORK_ONLY
def = nullptr
proc = UpdateServerPassword
pre_cb = ReplaceAsteriskWithEmptyPassword
post_cb = [](auto) { NetworkServerUpdateGameInfo(); }
cat = SC_BASIC
[SDTC_SSTR]
@ -3941,7 +3944,7 @@ length = NETWORK_PASSWORD_LENGTH
flags = SLF_NOT_IN_SAVE | SLF_NO_NETWORK_SYNC
guiflags = SGF_NETWORK_ONLY
def = nullptr
proc = UpdateRconPassword
pre_cb = ReplaceAsteriskWithEmptyPassword
cat = SC_BASIC
[SDTC_SSTR]
@ -3967,7 +3970,7 @@ length = NETWORK_NAME_LENGTH
flags = SLF_NOT_IN_SAVE | SLF_NO_NETWORK_SYNC
guiflags = SGF_NETWORK_ONLY
def = nullptr
proc = UpdateClientConfigValues
post_cb = [](auto) { UpdateClientConfigValues(); }
cat = SC_BASIC
[SDTC_SSTR]