From 933301e5180c275b6c01a3fd1a78921ca251ac7b Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Thu, 6 Jan 2022 07:22:52 +0200 Subject: [PATCH 43/43] Introduce setcompat.[ch] module See osdn #43394 Signed-off-by: Marko Lindqvist --- meson.build | 1 + server/Makefile.am | 2 ++ server/ruleset.c | 6 ++-- server/savegame/savecompat.c | 25 ++++++++++++--- server/setcompat.c | 61 ++++++++++++++++++++++++++++++++++++ server/setcompat.h | 29 +++++++++++++++++ server/settings.c | 7 ++++- server/settings.h | 3 +- 8 files changed, 126 insertions(+), 8 deletions(-) create mode 100644 server/setcompat.c create mode 100644 server/setcompat.h diff --git a/meson.build b/meson.build index e41aeb6ec2..09778832f9 100644 --- a/meson.build +++ b/meson.build @@ -760,6 +760,7 @@ server_lib = static_library('fc_server', 'server/sanitycheck.c', 'server/score.c', 'server/sernet.c', + 'server/setcompat.c', 'server/settings.c', 'server/spacerace.c', 'server/srv_log.c', diff --git a/server/Makefile.am b/server/Makefile.am index 3124d5507d..ecddc2cdc2 100644 --- a/server/Makefile.am +++ b/server/Makefile.am @@ -99,6 +99,8 @@ libfreeciv_srv_la_SOURCES = \ score.h \ sernet.c \ sernet.h \ + setcompat.c \ + setcompat.h \ settings.c \ settings.h \ spacerace.c \ diff --git a/server/ruleset.c b/server/ruleset.c index 6f854a77fc..c4dc12c7b7 100644 --- a/server/ruleset.c +++ b/server/ruleset.c @@ -8965,7 +8965,9 @@ static bool load_rulesetdir(const char *rsdir, bool compat_mode, if (ok) { /* Only load settings for a sane ruleset */ - ok = settings_ruleset(gamefile, "settings", act); + ok = settings_ruleset(gamefile, "settings", act, + compat_info.compat_mode + && compat_info.ver_game < RSFORMAT_3_2); if (ok) { secfile_check_unused(gamefile); @@ -9076,7 +9078,7 @@ bool reload_rulesets_settings(void) ok = FALSE; } if (ok) { - settings_ruleset(file, "settings", TRUE); + settings_ruleset(file, "settings", TRUE, FALSE); secfile_destroy(file); } diff --git a/server/savegame/savecompat.c b/server/savegame/savecompat.c index 8aa95d3f9d..0f9559c084 100644 --- a/server/savegame/savecompat.c +++ b/server/savegame/savecompat.c @@ -25,6 +25,7 @@ /* server */ #include "aiiface.h" +#include "setcompat.h" #include "unittools.h" #include "savecompat.h" @@ -1873,13 +1874,21 @@ static void compat_load_030200(struct loaddata *loading, "settings.gamestart_valid"); for (i = 0; i < set_count; i++) { - const char *name + const char *old_name = secfile_lookup_str(loading->file, "settings.set%d.name", i); + const char *name; - if (!name) { + if (!old_name) { continue; } + name = setcompat_S3_2_name_from_S3_1(old_name); + + if (fc_strcasecmp(old_name, name)) { + /* Setting's name changed */ + secfile_replace_str(loading->file, name, "settings.set%d.name", i); + } + if (!fc_strcasecmp("compresstype", name)) { const char *val = secfile_lookup_str(loading->file, "settings.set%d.value", i); @@ -2273,13 +2282,21 @@ static void compat_load_dev(struct loaddata *loading) "settings.gamestart_valid"); for (i = 0; i < set_count; i++) { - const char *name + const char *old_name = secfile_lookup_str(loading->file, "settings.set%d.name", i); + const char *name; - if (!name) { + if (!old_name) { continue; } + name = setcompat_S3_2_name_from_S3_1(old_name); + + if (fc_strcasecmp(old_name, name)) { + /* Setting's name changed */ + secfile_replace_str(loading->file, name, "settings.set%d.name", i); + } + if (!fc_strcasecmp("compresstype", name)) { const char *val = secfile_lookup_str(loading->file, "settings.set%d.value", i); diff --git a/server/setcompat.c b/server/setcompat.c new file mode 100644 index 0000000000..c755510cb8 --- /dev/null +++ b/server/setcompat.c @@ -0,0 +1,61 @@ +/*********************************************************************** + Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. +***********************************************************************/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +/* ANSI */ +#include + +/* utility */ +#include "support.h" + +#include "setcompat.h" + +struct set_name_compat { + const char *old_name; + const char *new_name; +}; + +static struct set_name_compat set_name_compat_S3_1_to_S3_2[] = +{ + { NULL, NULL } +}; + + +/**********************************************************************//** + Version agnostic helper function to find new name of a setting when + updating between two versions. +**************************************************************************/ +static const char *setcompat_name_generic(const char *old_name, + struct set_name_compat *compats) +{ + int i; + + for (i = 0; compats[i].old_name != NULL; i++) { + if (!fc_strcasecmp(old_name, compats[i].old_name)) { + return compats[i].new_name; + } + } + + return old_name; +} + +/**********************************************************************//** + Find a 3.2 name of the setting with the given 3.1 name. +**************************************************************************/ +const char *setcompat_S3_2_name_from_S3_1(const char *old_name) +{ + return setcompat_name_generic(old_name, set_name_compat_S3_1_to_S3_2); +} diff --git a/server/setcompat.h b/server/setcompat.h new file mode 100644 index 0000000000..117bd372fd --- /dev/null +++ b/server/setcompat.h @@ -0,0 +1,29 @@ +/*********************************************************************** + Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. +***********************************************************************/ +#ifndef FC__SETCOMPAT_H +#define FC__SETCOMPAT_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#define setcompat_current_name_from_previous(_old_name_) \ + setcompat_S3_2_name_from_S3_1(_old_name_) + +const char *setcompat_S3_2_name_from_S3_1(const char *old_name); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* FC__SETCOMPAT_H */ diff --git a/server/settings.c b/server/settings.c index cd3727ee4e..fc1099a3ef 100644 --- a/server/settings.c +++ b/server/settings.c @@ -36,6 +36,7 @@ #include "plrhand.h" #include "report.h" #include "rssanity.h" +#include "setcompat.h" #include "srv_main.h" #include "stdinhand.h" @@ -4132,7 +4133,7 @@ void setting_action(const struct setting *pset) Load game settings from ruleset file 'game.ruleset'. ****************************************************************************/ bool settings_ruleset(struct section_file *file, const char *section, - bool act) + bool act, bool compat) { const char *name; int j; @@ -4154,6 +4155,10 @@ bool settings_ruleset(struct section_file *file, const char *section, char path[256]; fc_snprintf(path, sizeof(path), "%s.set%d", section, j); + if (compat) { + name = setcompat_current_name_from_previous(name); + } + if (!setting_ruleset_one(file, name, path)) { log_error("unknown setting in '%s': %s", secfile_name(file), name); } diff --git a/server/settings.h b/server/settings.h index d92ec64924..008ecb2c4e 100644 --- a/server/settings.h +++ b/server/settings.h @@ -199,7 +199,8 @@ int settings_number(void); void settings_list_update(void); struct setting_list *settings_list_get(enum sset_level level); -bool settings_ruleset(struct section_file *file, const char *section, bool act); +bool settings_ruleset(struct section_file *file, const char *section, + bool act, bool compat); void send_server_setting(struct conn_list *dest, const struct setting *pset); void send_server_settings(struct conn_list *dest); -- 2.34.1