From f673e551c2100891978eb49c6b3b6b2604e5ce24 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Thu, 22 Dec 2022 22:14:18 +0200 Subject: [PATCH 40/40] Add lua signal retirement support See osdn #46332 Signed-off-by: Marko Lindqvist --- common/scriptcore/luascript_signal.c | 76 +++++++++++++++++++--------- common/scriptcore/luascript_signal.h | 16 +++--- server/scripting/script_server.c | 10 ++-- 3 files changed, 66 insertions(+), 36 deletions(-) diff --git a/common/scriptcore/luascript_signal.c b/common/scriptcore/luascript_signal.c index 8a4b76fc97..6973e17299 100644 --- a/common/scriptcore/luascript_signal.c +++ b/common/scriptcore/luascript_signal.c @@ -73,10 +73,10 @@ static void signal_destroy(struct signal *psignal); /* Signal datastructure. */ struct signal { - int nargs; /* number of arguments to pass */ - enum api_types *arg_types; /* argument types */ - struct signal_callback_list *callbacks; /* connected callbacks */ - char *depr_msg; /* deprecation message to show if handler added */ + int nargs; /* Number of arguments to pass */ + enum api_types *arg_types; /* Argument types */ + struct signal_callback_list *callbacks; /* Connected callbacks */ + struct signal_deprecator deprecator; }; /* Signal callback datastructure. */ @@ -139,7 +139,8 @@ static struct signal *signal_new(int nargs, enum api_types *parg_types) psignal->arg_types = parg_types; psignal->callbacks = signal_callback_list_new_full(signal_callback_destroy); - psignal->depr_msg = NULL; + psignal->deprecator.depr_msg = NULL; + psignal->deprecator.retired = NULL; return psignal; } @@ -152,8 +153,11 @@ static void signal_destroy(struct signal *psignal) if (psignal->arg_types) { free(psignal->arg_types); } - if (psignal->depr_msg) { - free(psignal->depr_msg); + if (psignal->deprecator.depr_msg) { + free(psignal->deprecator.depr_msg); + } + if (psignal->deprecator.retired) { + free(psignal->deprecator.retired); } signal_callback_list_destroy(psignal->callbacks); free(psignal); @@ -248,9 +252,9 @@ static struct signal *luascript_signal_create_valist(struct fc_lua *fcl, /**********************************************************************//** Create a new signal type. **************************************************************************/ -signal_deprecator *luascript_signal_create(struct fc_lua *fcl, - const char *signal_name, - int nargs, ...) +struct signal_deprecator *luascript_signal_create(struct fc_lua *fcl, + const char *signal_name, + int nargs, ...) { va_list args; struct signal *created; @@ -260,7 +264,7 @@ signal_deprecator *luascript_signal_create(struct fc_lua *fcl, va_end(args); if (created != NULL) { - return &(created->depr_msg); + return &(created->deprecator); } return NULL; @@ -269,26 +273,41 @@ signal_deprecator *luascript_signal_create(struct fc_lua *fcl, /**********************************************************************//** Mark signal deprecated. **************************************************************************/ -void deprecate_signal(signal_deprecator *deprecator, char *signal_name, - char *replacement, char *deprecated_since) +void deprecate_signal(struct signal_deprecator *deprecator, char *signal_name, + char *replacement, char *deprecated_since, + char *retired_since) { if (deprecator != NULL) { char buffer[1024]; + char *deprtype = ((retired_since != NULL) ? "Retired:" : "Deprecated:"); if (deprecated_since != NULL && replacement != NULL) { - fc_snprintf(buffer, sizeof(buffer), - "Deprecated: lua signal \"%s\", deprecated since \"%s\", used. " - "Use \"%s\" instead", signal_name, deprecated_since, replacement); + if (retired_since != NULL) { + fc_snprintf(buffer, sizeof(buffer), + "%s lua signal \"%s\", retired since \"%s\", " + "and deprecated already since \"%s\", used. " + "Use \"%s\" instead", + deprtype, signal_name, retired_since, deprecated_since, replacement); + } else { + fc_snprintf(buffer, sizeof(buffer), + "%s lua signal \"%s\", deprecated since \"%s\", used. " + "Use \"%s\" instead", + deprtype, signal_name, deprecated_since, replacement); + } } else if (replacement != NULL) { fc_snprintf(buffer, sizeof(buffer), - "Deprecated: lua signal \"%s\" used. Use \"%s\" instead", - signal_name, replacement); + "%s lua signal \"%s\" used. Use \"%s\" instead", + deprtype, signal_name, replacement); } else { fc_snprintf(buffer, sizeof(buffer), - "Deprecated: lua signal \"%s\" used.", signal_name); + "%s lua signal \"%s\" used.", deprtype, signal_name); } - *deprecator = fc_strdup(buffer); + deprecator->depr_msg = fc_strdup(buffer); + + if (retired_since != NULL) { + deprecator->retired = fc_strdup(retired_since); + } } } @@ -305,7 +324,18 @@ void luascript_signal_callback(struct fc_lua *fcl, const char *signal_name, fc_assert_ret(fcl->signals != NULL); if (luascript_signal_hash_lookup(fcl->signals, signal_name, &psignal)) { - /* check for a duplicate callback */ + + if (psignal->deprecator.depr_msg != NULL) { + log_deprecation("%s", psignal->deprecator.depr_msg); + } + + if (psignal->deprecator.retired != NULL) { + luascript_error(fcl->state, "Signal \"%s\" has been retired.", + signal_name); + return; + } + + /* Check for a duplicate callback */ signal_callback_list_iterate(psignal->callbacks, pcallback) { if (!strcmp(pcallback->name, callback_name)) { pcallback_found = pcallback; @@ -313,10 +343,6 @@ void luascript_signal_callback(struct fc_lua *fcl, const char *signal_name, } } signal_callback_list_iterate_end; - if (psignal->depr_msg != NULL) { - log_deprecation("%s", psignal->depr_msg); - } - if (create) { if (pcallback_found) { luascript_error(fcl->state, "Signal \"%s\" already has a callback " diff --git a/common/scriptcore/luascript_signal.h b/common/scriptcore/luascript_signal.h index 8bee0bed4f..07f3619699 100644 --- a/common/scriptcore/luascript_signal.h +++ b/common/scriptcore/luascript_signal.h @@ -22,7 +22,10 @@ extern "C" { struct fc_lua; -typedef char * signal_deprecator; +struct signal_deprecator { + char *depr_msg; /* Deprecation message to show if handler added */ + char *retired; /* Signal no longer available in current freeciv */ +}; void luascript_signal_init(struct fc_lua *fcl); void luascript_signal_free(struct fc_lua *fcl); @@ -30,11 +33,12 @@ void luascript_signal_free(struct fc_lua *fcl); void luascript_signal_emit_valist(struct fc_lua *fcl, const char *signal_name, va_list args); void luascript_signal_emit(struct fc_lua *fcl, const char *signal_name, ...); -signal_deprecator *luascript_signal_create(struct fc_lua *fcl, - const char *signal_name, - int nargs, ...); -void deprecate_signal(signal_deprecator *deprecator, char *signal_name, - char *replacement, char *deprecated_since); +struct signal_deprecator *luascript_signal_create(struct fc_lua *fcl, + const char *signal_name, + int nargs, ...); +void deprecate_signal(struct signal_deprecator *deprecator, char *signal_name, + char *replacement, char *deprecated_since, + char *retired_since); void luascript_signal_callback(struct fc_lua *fcl, const char *signal_name, const char *callback_name, bool create); bool luascript_signal_callback_defined(struct fc_lua *fcl, diff --git a/server/scripting/script_server.c b/server/scripting/script_server.c index ccc01163de..f5e5f6a5b8 100644 --- a/server/scripting/script_server.c +++ b/server/scripting/script_server.c @@ -419,7 +419,7 @@ void script_server_signal_emit(const char *signal_name, ...) ***************************************************************************/ static void script_server_signals_create(void) { - signal_deprecator *depr; + struct signal_deprecator *depr; luascript_signal_create(fcl_main, "turn_begin", 2, API_TYPE_INT, API_TYPE_INT); @@ -428,7 +428,7 @@ static void script_server_signals_create(void) * starting from 0. */ depr = luascript_signal_create(fcl_main, "turn_started", 2, API_TYPE_INT, API_TYPE_INT); - deprecate_signal(depr, "turn_started", "turn_begin", "3.0"); + deprecate_signal(depr, "turn_started", "turn_begin", "3.0", NULL); luascript_signal_create(fcl_main, "player_phase_begin", 2, API_TYPE_PLAYER, API_TYPE_BOOL); @@ -450,7 +450,7 @@ static void script_server_signals_create(void) /* Deprecated form of the 'city_size_change' signal for the case of growth. */ depr = luascript_signal_create(fcl_main, "city_growth", 2, API_TYPE_CITY, API_TYPE_INT); - deprecate_signal(depr, "city_growth", "city_size_change", "2.6"); + deprecate_signal(depr, "city_growth", "city_size_change", "2.6", NULL); /* Only includes units built in cities, for now. */ luascript_signal_create(fcl_main, "unit_built", 2, @@ -497,7 +497,7 @@ static void script_server_signals_create(void) * conquest. */ depr = luascript_signal_create(fcl_main, "city_lost", 3, API_TYPE_CITY, API_TYPE_PLAYER, API_TYPE_PLAYER); - deprecate_signal(depr, "city_lost", "city_transferred", "2.6"); + deprecate_signal(depr, "city_lost", "city_transferred", "2.6", NULL); luascript_signal_create(fcl_main, "hut_enter", 2, API_TYPE_UNIT, API_TYPE_STRING); @@ -517,7 +517,7 @@ static void script_server_signals_create(void) * support. */ depr = luascript_signal_create(fcl_main, "disaster", 2, API_TYPE_DISASTER, API_TYPE_CITY); - deprecate_signal(depr, "disaster", "disaster_occurred", "2.6"); + deprecate_signal(depr, "disaster", "disaster_occurred", "2.6", NULL); luascript_signal_create(fcl_main, "achievement_gained", 3, API_TYPE_ACHIEVEMENT, API_TYPE_PLAYER, -- 2.35.1