Author: sveinung
Date: Fri Apr 21 02:56:09 2017
New Revision: 35270

URL: http://svn.gna.org/viewcvs/freeciv?rev=35270&view=rev
Log:
Shared server setting access.

Server settings are stored and accessed differently in the client and in the
server. Introduce primitives for server setting access that works both in
client code and in server code. The plan is to build more complex access on
top of them.

Only access to the common (non value type specific) part of a setting is
included here.

See hrm Feature #653552

Modified:
    trunk/client/client_main.c
    trunk/common/fc_interface.c
    trunk/common/fc_interface.h
    trunk/common/server_settings.h
    trunk/server/srv_main.c

Modified: trunk/client/client_main.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/client/client_main.c?rev=35270&r1=35269&r2=35270&view=diff
==============================================================================
--- trunk/client/client_main.c  (original)
+++ trunk/client/client_main.c  Fri Apr 21 02:56:09 2017
@@ -1355,6 +1355,78 @@
   return pcity ? pcity->id : IDENTITY_NUMBER_ZERO;
 }
 
+/***************************************************************************
+  Returns the id of the server setting with the specified name.
+***************************************************************************/
+static server_setting_id client_ss_by_name(const char *name)
+{
+  struct option *pset = optset_option_by_name(server_optset, name);
+
+  if (pset) {
+    return option_number(pset);
+  } else {
+    log_error("No server setting named %s exists.", name);
+    return SERVER_SETTING_NONE;
+  }
+}
+
+/***************************************************************************
+  Returns the name of the server setting with the specified id.
+***************************************************************************/
+static const char *client_ss_name_get(server_setting_id id)
+{
+  struct option *pset = optset_option_by_number(server_optset, id);
+
+  if (pset) {
+    return option_name(pset);
+  } else {
+    log_error("No server setting with the id %d exists.", id);
+    return NULL;
+  }
+}
+
+/***************************************************************************
+  Returns the type of the server setting with the specified id.
+***************************************************************************/
+static enum sset_type client_ss_type_get(server_setting_id id)
+{
+  enum option_type opt_type;
+  struct option *pset = optset_option_by_number(server_optset, id);
+
+  if (!pset) {
+    log_error("No server setting with the id %d exists.", id);
+    return sset_type_invalid();
+  }
+
+  opt_type = option_type(pset);
+
+  /* The option type isn't client only. */
+  fc_assert_ret_val_msg((opt_type != OT_FONT
+                         && opt_type != OT_COLOR
+                         && opt_type != OT_VIDEO_MODE),
+                        sset_type_invalid(),
+                        "%s is a client option type but not a server "
+                        "setting type",
+                        option_type_name(option_type(pset)));
+
+  /* The option type is valid. */
+  fc_assert_ret_val(sset_type_is_valid(opt_type), sset_type_invalid());
+
+  /* Each server setting type value equals the client option type value with
+   * the same meaning. */
+  FC_STATIC_ASSERT((enum sset_type)OT_BOOLEAN == SST_BOOL
+                   && (enum sset_type)OT_INTEGER == SST_INT
+                   && (enum sset_type)OT_STRING == SST_STRING
+                   && (enum sset_type)OT_ENUM == SST_ENUM
+                   && (enum sset_type)OT_BITWISE == SST_BITWISE
+                   && SST_COUNT == (enum sset_type)5,
+                   server_setting_type_not_equal_to_client_option_type);
+
+  /* Exploit the fact that each server setting type value corresponds to the
+   * client option type value with the same meaning. */
+  return opt_type;
+}
+
 /***************************************************************
   Initialize client specific functions.
 ***************************************************************/
@@ -1362,6 +1434,9 @@
 {
   struct functions *funcs = fc_interface_funcs();
 
+  funcs->server_setting_by_name = client_ss_by_name;
+  funcs->server_setting_name_get = client_ss_name_get;
+  funcs->server_setting_type_get = client_ss_type_get;
   funcs->create_extra = NULL;
   funcs->destroy_extra = NULL;
   funcs->player_tile_vision_get = client_map_is_known_and_seen;

Modified: trunk/common/fc_interface.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/fc_interface.c?rev=35270&r1=35269&r2=35270&view=diff
==============================================================================
--- trunk/common/fc_interface.c (original)
+++ trunk/common/fc_interface.c Fri Apr 21 02:56:09 2017
@@ -56,6 +56,9 @@
   fc_funcs = &fc_functions;
 
   /* Test the existence of each required function here! */
+  fc_assert_exit(fc_funcs->server_setting_by_name);
+  fc_assert_exit(fc_funcs->server_setting_name_get);
+  fc_assert_exit(fc_funcs->server_setting_type_get);
   fc_assert_exit(fc_funcs->player_tile_vision_get);
   fc_assert_exit(fc_funcs->player_tile_city_id_get);
   fc_assert_exit(fc_funcs->gui_color_free);

Modified: trunk/common/fc_interface.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/fc_interface.h?rev=35270&r1=35269&r2=35270&view=diff
==============================================================================
--- trunk/common/fc_interface.h (original)
+++ trunk/common/fc_interface.h Fri Apr 21 02:56:09 2017
@@ -21,6 +21,7 @@
 #include "support.h"
 
 /* common */
+#include "server_settings.h"
 #include "vision.h"
 
 struct player;
@@ -30,6 +31,9 @@
 
 /* The existence of each function should be checked in interface_init()! */
 struct functions {
+  server_setting_id (*server_setting_by_name)(const char *name);
+  const char *(*server_setting_name_get)(server_setting_id id);
+  enum sset_type (*server_setting_type_get)(server_setting_id id);
   void (*create_extra)(struct tile *ptile, struct extra_type *pextra,
                        struct player *pplayer);
   void (*destroy_extra)(struct tile *ptile, struct extra_type *pextra);

Modified: trunk/common/server_settings.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/server_settings.h?rev=35270&r1=35269&r2=35270&view=diff
==============================================================================
--- trunk/common/server_settings.h      (original)
+++ trunk/common/server_settings.h      Fri Apr 21 02:56:09 2017
@@ -28,6 +28,12 @@
 #define SPECENUM_COUNT  SST_COUNT
 #include "specenum_gen.h"
 
+/* Special value to signal the absence of a server setting. */
+#define SERVER_SETTING_NONE ((server_setting_id) -1)
+
+/* Mark server setting id's. */
+typedef int server_setting_id;
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */

Modified: trunk/server/srv_main.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/server/srv_main.c?rev=35270&r1=35269&r2=35270&view=diff
==============================================================================
--- trunk/server/srv_main.c     (original)
+++ trunk/server/srv_main.c     Fri Apr 21 02:56:09 2017
@@ -3264,6 +3264,51 @@
                                   : IDENTITY_NUMBER_ZERO;
 }
 
+/***************************************************************************
+  Returns the id of the server setting with the specified name.
+***************************************************************************/
+static server_setting_id server_ss_by_name(const char *name)
+{
+  struct setting *pset = setting_by_name(name);
+
+  if (pset) {
+    return setting_number(pset);
+  } else {
+    log_error("No server setting named %s exists.", name);
+    return SERVER_SETTING_NONE;
+  }
+}
+
+/***************************************************************************
+  Returns the name of the server setting with the specified id.
+***************************************************************************/
+static const char *server_ss_name_get(server_setting_id id)
+{
+  struct setting *pset = setting_by_number(id);
+
+  if (pset) {
+    return setting_name(pset);
+  } else {
+    log_error("No server setting with the id %d exists.", id);
+    return NULL;
+  }
+}
+
+/***************************************************************************
+  Returns the type of the server setting with the specified id.
+***************************************************************************/
+static enum sset_type server_ss_type_get(server_setting_id id)
+{
+  struct setting *pset = setting_by_number(id);
+
+  if (pset) {
+    return setting_type(pset);
+  } else {
+    log_error("No server setting with the id %d exists.", id);
+    return sset_type_invalid();
+  }
+}
+
 /***************************************************************
   Initialize client specific functions.
 ***************************************************************/
@@ -3271,6 +3316,9 @@
 {
   struct functions *funcs = fc_interface_funcs();
 
+  funcs->server_setting_by_name = server_ss_by_name;
+  funcs->server_setting_name_get = server_ss_name_get;
+  funcs->server_setting_type_get = server_ss_type_get;
   funcs->create_extra = create_extra;
   funcs->destroy_extra = destroy_extra;
   funcs->player_tile_vision_get = map_is_known_and_seen;


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to