From d2df2b72ea6064d2dfae178b1d13d6a99ea0bc8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Lach?= Date: Sat, 8 Jan 2022 19:36:25 +0100 Subject: [PATCH 1/3] - Remove empty line after the function header - Use SIZE_T_PRINTF instead of "%ld" to print size_t, for portability - Do not ignore the failure. Should probably use sg_failure_ret() instead of log_error() & break - Memory for counters has already been allocated when the virtual city was created. Do not allocate again - Check that there was actual city was found by game_city_by_number() (is not NULL) - You assume that counters in the savegame are in the same order as they currently are in the memory. Use city_counters_order instead - I don't think you own what you get from secfile_lookup_int_vec() but it's still part of the secfile. If so, you should not free(city_count) - ++i; -> i++; --- server/savegame/savegame3.c | 45 ++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/server/savegame/savegame3.c b/server/savegame/savegame3.c index 3d91053bfb..df447b4653 100644 --- a/server/savegame/savegame3.c +++ b/server/savegame/savegame3.c @@ -2580,37 +2580,62 @@ static void sg_save_settings(struct savedata *saving) /************************************************************************//** Load '[counters]'. ****************************************************************************/ - static void sg_load_counters (struct loaddata * loading) { + const char **countnames; struct city *pcity; - int i, j; + int i, j, k; size_t length; int *city_count; int city_ccount = secfile_lookup_int_default(loading->file, 0, "savefile.city_counters_order_size"); + countnames = secfile_lookup_str_vec(loading->file, + &length, + "savefile.city" + "_counters_order_vector"); + i = 0; while (NULL != (city_count = secfile_lookup_int_vec(loading->file, &length, "counters.c%d", i))) { - if (length -1 != (size_t) city_ccount) { - - log_error("Bad city counters vector size. Should be %d. Is %ld.", city_ccount, length - 1); - break; - } + sg_failure_ret((length -1 == (size_t) city_ccount), + _("Bad city counters vector size. " + "Should be %d. Is %zu."), city_ccount, length - 1); pcity = game_city_by_number(city_count[0]); - pcity->counter_values = fc_calloc(city_ccount, sizeof(*pcity->counter_values)); + sg_failure_ret((pcity != NULL), + _("Internal error: do not found" + " city pointed by counters vector")); + + pcity->counter_values = city_count; for (j = 0; j < city_ccount; ++j) { pcity->counter_values[j] = city_count[j+1]; } - free(city_count); - ++i; + pcity->counter_values = realloc(pcity->counter_values, city_ccount); + // sort + + for (k = 0; k < counters_get_city_counters_count(); k++) { + + for (j = k; j < counters_get_city_counters_count(); j++) { + + const char *ctg_rule_name = counter_rule_name( + counter_by_index(j, CTGT_CITY)); + if (0 == fc_strcasecmp(countnames[k], ctg_rule_name)) { + + int temp = pcity->counter_values[k]; + + pcity->counter_values[k] = pcity->counter_values[j]; + pcity->counter_values[j] = temp; + } + } + } + + i++; } } -- 2.34.1