From 1f4aaf05d52de530378dd3a95c457db6a6c74c42 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 26 Nov 2022 06:20:06 +0200 Subject: [PATCH 34/34] Savegame: Fix "unused entry" warnings about traderoute padding See osdn #45856 Signed-off-by: Marko Lindqvist --- server/savegame/savecompat.c | 7 ++++ server/savegame/savegame3.c | 69 +++++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/server/savegame/savecompat.c b/server/savegame/savecompat.c index 7539ddbbea..048386a50a 100644 --- a/server/savegame/savecompat.c +++ b/server/savegame/savecompat.c @@ -39,6 +39,9 @@ static char *special_names[] = "Fallout", NULL }; +/* Old savegames might have padding up to this amount of trade routes */ +#define MAX_TRADE_ROUTES_OLD 5 + /* For each savefile format after 2.3.0, compatibility functions are defined which translate secfile structures from previous version to that version; @@ -2251,6 +2254,8 @@ static void compat_load_030200(struct loaddata *loading, secfile_insert_int(loading->file, wlist_max_length, "player%d.wl_max_length", plrno); + secfile_insert_int(loading->file, MAX_TRADE_ROUTES_OLD, + "player%d.routes_max_length", plrno); } player_slots_iterate_end; } @@ -2895,6 +2900,8 @@ static void compat_load_dev(struct loaddata *loading) secfile_insert_int(loading->file, wlist_max_length, "player%d.wl_max_length", plrno); + secfile_insert_int(loading->file, MAX_TRADE_ROUTES_OLD, + "player%d.routes_max_length", plrno); } player_slots_iterate_end; } diff --git a/server/savegame/savegame3.c b/server/savegame/savegame3.c index 277987ca68..4811769a5d 100644 --- a/server/savegame/savegame3.c +++ b/server/savegame/savegame3.c @@ -364,7 +364,7 @@ static void sg_load_player_cities(struct loaddata *loading, struct player *plr); static bool sg_load_player_city(struct loaddata *loading, struct player *plr, struct city *pcity, const char *citystr, - int wlist_max_length); + int wlist_max_length, int routes_max); static void sg_load_player_city_citizens(struct loaddata *loading, struct player *plr, struct city *pcity, @@ -4805,7 +4805,7 @@ static void sg_load_player_cities(struct loaddata *loading, { int ncities, i, plrno = player_number(plr); bool tasks_handled; - int wlist_max_length; + int wlist_max_length, routes_max; /* Check status and return if not OK (sg_success FALSE). */ sg_check_ret(); @@ -4826,6 +4826,8 @@ static void sg_load_player_cities(struct loaddata *loading, wlist_max_length = secfile_lookup_int_default(loading->file, 0, "player%d.wl_max_length", plrno); + routes_max = secfile_lookup_int_default(loading->file, 0, + "player%d.routes_max_length", plrno); /* Load all cities of the player. */ for (i = 0; i < ncities; i++) { @@ -4837,7 +4839,8 @@ static void sg_load_player_cities(struct loaddata *loading, /* Create a dummy city. */ pcity = create_city_virtual(plr, NULL, buf); adv_city_alloc(pcity); - if (!sg_load_player_city(loading, plr, pcity, buf, wlist_max_length)) { + if (!sg_load_player_city(loading, plr, pcity, buf, + wlist_max_length, routes_max)) { adv_city_free(pcity); destroy_city_virtual(pcity); sg_failure_ret(FALSE, "Error loading city %d of player %d.", i, plrno); @@ -4922,7 +4925,7 @@ static void sg_load_player_cities(struct loaddata *loading, ****************************************************************************/ static bool sg_load_player_city(struct loaddata *loading, struct player *plr, struct city *pcity, const char *citystr, - int wlist_max_length) + int wlist_max_length, int routes_max) { struct player *past; const char *kind, *name, *str; @@ -4930,7 +4933,7 @@ static bool sg_load_player_city(struct loaddata *loading, struct player *plr, int nat_x, nat_y; citizens size; const char *stylename; - int partner = 1; + int partner; int want; sg_warn_ret_val(secfile_lookup_int(loading->file, &nat_x, "%s.x", citystr), @@ -4974,33 +4977,39 @@ static bool sg_load_player_city(struct loaddata *loading, struct player *plr, sp_count += value; } + partner = secfile_lookup_int_default(loading->file, 0, "%s.traderoute0", citystr); for (i = 0; partner != 0; i++) { + struct trade_route *proute = fc_malloc(sizeof(struct trade_route)); + const char *dir; + const char *good_str; + + /* Append to routes list immediately, so the pointer can be found for freeing + * even if we abort */ + trade_route_list_append(pcity->routes, proute); + + proute->partner = partner; + dir = secfile_lookup_str(loading->file, "%s.route_direction%d", citystr, i); + sg_warn_ret_val(dir != NULL, FALSE, + "No traderoute direction found for %s", citystr); + proute->dir = route_direction_by_name(dir, fc_strcasecmp); + sg_warn_ret_val(route_direction_is_valid(proute->dir), FALSE, + "Illegal route direction %s", dir); + good_str = secfile_lookup_str(loading->file, "%s.route_good%d", citystr, i); + sg_warn_ret_val(dir != NULL, FALSE, + "No good found for %s", citystr); + proute->goods = goods_by_rule_name(good_str); + sg_warn_ret_val(proute->goods != NULL, FALSE, + "Illegal good %s", good_str); + + /* Next one */ partner = secfile_lookup_int_default(loading->file, 0, - "%s.traderoute%d", citystr, i); - - if (partner != 0) { - struct trade_route *proute = fc_malloc(sizeof(struct trade_route)); - const char *dir; - const char *good_str; - - /* Append to routes list immediately, so the pointer can be found for freeing - * even if we abort */ - trade_route_list_append(pcity->routes, proute); + "%s.traderoute%d", citystr, i + 1); + } - proute->partner = partner; - dir = secfile_lookup_str(loading->file, "%s.route_direction%d", citystr, i); - sg_warn_ret_val(dir != NULL, FALSE, - "No traderoute direction found for %s", citystr); - proute->dir = route_direction_by_name(dir, fc_strcasecmp); - sg_warn_ret_val(route_direction_is_valid(proute->dir), FALSE, - "Illegal route direction %s", dir); - good_str = secfile_lookup_str(loading->file, "%s.route_good%d", citystr, i); - sg_warn_ret_val(dir != NULL, FALSE, - "No good found for %s", citystr); - proute->goods = goods_by_rule_name(good_str); - sg_warn_ret_val(proute->goods != NULL, FALSE, - "Illegal good %s", good_str); - } + for (; i < routes_max; i++) { + (void) secfile_entry_lookup(loading->file, "%s.traderoute%d", citystr, i); + (void) secfile_entry_lookup(loading->file, "%s.route_direction%d", citystr, i); + (void) secfile_entry_lookup(loading->file, "%s.route_good%d", citystr, i); } sg_warn_ret_val(secfile_lookup_int(loading->file, &pcity->food_stock, @@ -5453,6 +5462,8 @@ static void sg_save_player_cities(struct savedata *saving, secfile_insert_int(saving->file, wlist_max_length, "player%d.wl_max_length", plrno); + secfile_insert_int(saving->file, routes_max, + "player%d.routes_max_length", plrno); city_list_iterate(plr->cities, pcity) { struct tile *pcenter = city_tile(pcity); -- 2.35.1