Complete.Org: Mailing Lists: Archives: freeciv-dev: April 2004:
[Freeciv-Dev] Re: (PR#8540) civclientrc incompatibility
Home

[Freeciv-Dev] Re: (PR#8540) civclientrc incompatibility

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: per@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: (PR#8540) civclientrc incompatibility
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 20 Apr 2004 09:06:40 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=8540 >

Mike Kaufman wrote:
> <URL: http://rt.freeciv.org/Ticket/Display.html?id=8540 >
> 
> On Tue, Apr 20, 2004 at 08:02:18AM -0700, Jason Short wrote:
> 
>>Ahh, because the entire file is overwritten so the options that aren't 
>>in 1.14 are lost.
>>
>>There are ways around this, but I think they're pretty complicated.
> 
> 
> before saving can we simply load the rc file into a secfile and then write
> the values we want into that secfile? That should save the values in the
> secfile we don't write to... It's been a while since I looked at the
> registry code, I don't remember if this is allowed.

Hmm, yeah.  I guess it's not so hard after all.  All we have to do is 
keep the loaded secfile data around, rather than freeing it.  Then when 
we save any data that wasn't overwritten will be resaved.

Alternately we could reload the file right before the save, as Mike 
suggests.  Maybe that would be easier?  I dunno.

I've tested this patch, but only with a single version of the game.  It 
seems to save and load just fine.  I haven't tested it with different 
game versions.

jason

? cma_weirdness
? data/civ3
? data/womoks
Index: client/options.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/options.c,v
retrieving revision 1.96
diff -u -r1.96 options.c
--- client/options.c    3 Apr 2004 01:57:22 -0000       1.96
+++ client/options.c    20 Apr 2004 16:04:06 -0000
@@ -277,6 +277,8 @@
   GEN_EV_TERMINATOR
 };
 
+static struct section_file options_secfile;
+
 /* 
  * Maps from enum event_type to indexes of events[]. Set by
  * init_messages_where. 
@@ -417,7 +419,6 @@
 *****************************************************************/
 void load_general_options(void)
 {
-  struct section_file sf;
   const char * const prefix = "client";
   char *name;
   int i, num;
@@ -435,12 +436,14 @@
     /* fail silently */
     return;
   }
-  if (!section_file_load(&sf, name))
-    return;  
+  if (!section_file_load(&options_secfile, name)) {
+    return;
+  }
 
   /* a "secret" option for the lazy. TODO: make this saveable */
   sz_strlcpy(password, 
-             secfile_lookup_str_default(&sf, "", "%s.password", prefix));
+             secfile_lookup_str_default(&options_secfile, "",
+                                       "%s.password", prefix));
 
   for (i = 0; i < num_options; i++) {
     client_option *o = options + i;
@@ -448,45 +451,50 @@
     switch (o->type) {
     case COT_BOOL:
       *(o->p_bool_value) =
-         secfile_lookup_bool_default(&sf, *(o->p_bool_value), "%s.%s",
+         secfile_lookup_bool_default(&options_secfile,
+                                     *(o->p_bool_value), "%s.%s",
                                      prefix, o->name);
       break;
     case COT_INT:
       *(o->p_int_value) =
-         secfile_lookup_int_default(&sf, *(o->p_int_value), "%s.%s",
-                                     prefix, o->name);
+         secfile_lookup_int_default(&options_secfile,
+                                    *(o->p_int_value), "%s.%s",
+                                    prefix, o->name);
       break;
     case COT_STR:
       mystrlcpy(o->p_string_value,
-                     secfile_lookup_str_default(&sf, o->p_string_value, 
"%s.%s",
-                     prefix, o->name), o->string_length);
+                     secfile_lookup_str_default(&options_secfile,
+                                               o->p_string_value, "%s.%s",
+                                               prefix, o->name),
+               o->string_length);
       break;
     }
   }
   for (v = view_options; v->name; v++) {
     *(v->p_value) =
-       secfile_lookup_bool_default(&sf, *(v->p_value), "%s.%s", prefix,
+       secfile_lookup_bool_default(&options_secfile,
+                                   *(v->p_value), "%s.%s", prefix,
                                    v->name);
   }
   for (i = 0; i < E_LAST; i++) {
     messages_where[i] =
-      secfile_lookup_int_default(&sf, messages_where[i],
+      secfile_lookup_int_default(&options_secfile, messages_where[i],
                                 "%s.message_where_%02d", prefix, i);
   }
   for (i = 1; i < num_city_report_spec(); i++) {
     bool *ip = city_report_spec_show_ptr(i);
-    *ip = secfile_lookup_bool_default(&sf, *ip, "%s.city_report_%s", prefix,
-                                    city_report_spec_tagname(i));
+    *ip = secfile_lookup_bool_default(&options_secfile, *ip,
+                                     "%s.city_report_%s", prefix,
+                                     city_report_spec_tagname(i));
   }
 
   /* Load cma presets. If cma.number_of_presets doesn't exist, don't load 
    * any, the order here should be reversed to keep the order the same */
-  num = secfile_lookup_int_default(&sf, 0, "cma.number_of_presets");
+  num = secfile_lookup_int_default(&options_secfile, 0,
+                                  "cma.number_of_presets");
   for (i = num - 1; i >= 0; i--) {
-    load_cma_preset(&sf, i);
+    load_cma_preset(&options_secfile, i);
   }
- 
-  section_file_free(&sf);
 }
 
 /****************************************************************
@@ -496,7 +504,6 @@
 *****************************************************************/
 void load_ruleset_specific_options(void)
 {
-  struct section_file sf;
   char *name;
   int i;
 
@@ -505,22 +512,20 @@
     /* fail silently */
     return;
   }
-  if (!section_file_load(&sf, name))
+  if (!section_file_load(&options_secfile, name))
     return;
 
   /* load global worklists */
   for (i = 0; i < MAX_NUM_WORKLISTS; i++) {
     game.player_ptr->worklists[i].is_valid =
-       secfile_lookup_bool_default(&sf, FALSE,
+       secfile_lookup_bool_default(&options_secfile, FALSE,
                                    "worklists.worklist%d.is_valid", i);
     strcpy(game.player_ptr->worklists[i].name,
-           secfile_lookup_str_default(&sf, "",
+           secfile_lookup_str_default(&options_secfile, "",
                                       "worklists.worklist%d.name", i));
-    load_global_worklist(&sf, "worklists.worklist%d", i, 
+    load_global_worklist(&options_secfile, "worklists.worklist%d", i, 
                          &(game.player_ptr->worklists[i]));
   }
-
-  section_file_free(&sf);
 }
 
 /****************************************************************
@@ -528,7 +533,6 @@
 *****************************************************************/
 void save_options(void)
 {
-  struct section_file sf;
   char *name = option_file_name();
   char output_buffer[256];
   view_option *v;
@@ -539,37 +543,40 @@
     return;
   }
 
-  section_file_init(&sf);
-  secfile_insert_str(&sf, VERSION_STRING, "client.version");
+  secfile_insert_str(&options_secfile, VERSION_STRING, "client.version");
 
   for (i = 0; i < num_options; i++) {
     client_option *o = options + i;
 
     switch (o->type) {
     case COT_BOOL:
-      secfile_insert_bool(&sf, *(o->p_bool_value), "client.%s", o->name);
+      secfile_insert_bool(&options_secfile, *(o->p_bool_value),
+                         "client.%s", o->name);
       break;
     case COT_INT:
-      secfile_insert_int(&sf, *(o->p_int_value), "client.%s", o->name);
+      secfile_insert_int(&options_secfile, *(o->p_int_value),
+                        "client.%s", o->name);
       break;
     case COT_STR:
-      secfile_insert_str(&sf, o->p_string_value, "client.%s", o->name);
+      secfile_insert_str(&options_secfile, o->p_string_value,
+                        "client.%s", o->name);
       break;
     }
   }
 
   for (v = view_options; v->name; v++) {
-    secfile_insert_bool(&sf, *(v->p_value), "client.%s", v->name);
+    secfile_insert_bool(&options_secfile, *(v->p_value),
+                       "client.%s", v->name);
   }
 
   for (i = 0; i < E_LAST; i++) {
-    secfile_insert_int_comment(&sf, messages_where[i],
+    secfile_insert_int_comment(&options_secfile, messages_where[i],
                               get_message_text(i),
                               "client.message_where_%02d", i);
   }
 
   for (i = 1; i < num_city_report_spec(); i++) {
-    secfile_insert_bool(&sf, *(city_report_spec_show_ptr(i)),
+    secfile_insert_bool(&options_secfile, *(city_report_spec_show_ptr(i)),
                       "client.city_report_%s",
                       city_report_spec_tagname(i));
   }
@@ -577,28 +584,30 @@
   /* insert global worklists */
   for(i = 0; i < MAX_NUM_WORKLISTS; i++){
     if (game.player_ptr->worklists[i].is_valid) {
-      secfile_insert_bool(&sf, game.player_ptr->worklists[i].is_valid,
+      secfile_insert_bool(&options_secfile,
+                         game.player_ptr->worklists[i].is_valid,
                          "worklists.worklist%d.is_valid", i);
-      secfile_insert_str(&sf, game.player_ptr->worklists[i].name,
+      secfile_insert_str(&options_secfile,
+                        game.player_ptr->worklists[i].name,
                          "worklists.worklist%d.name", i);
-      save_global_worklist(&sf, "worklists.worklist%d", i, 
+      save_global_worklist(&options_secfile, "worklists.worklist%d", i, 
                            &(game.player_ptr->worklists[i]));
     }
   }
 
 
   /* insert cma presets */
-  secfile_insert_int_comment(&sf, cmafec_preset_num(),
+  secfile_insert_int_comment(&options_secfile, cmafec_preset_num(),
                             _("If you add a preset by "
                               "hand, also update \"number_of_presets\""),
                             "cma.number_of_presets");
   for (i = 0; i < cmafec_preset_num(); i++) {
-    save_cma_preset(&sf, cmafec_preset_get_descr(i),
+    save_cma_preset(&options_secfile, cmafec_preset_get_descr(i),
                    cmafec_preset_get_parameter(i), i);
   }
 
   /* save to disk */
-  if (!section_file_save(&sf, name, 0)) {
+  if (!section_file_save(&options_secfile, name, 0)) {
     my_snprintf(output_buffer, sizeof(output_buffer),
                _("Save failed, cannot write to file %s"), name);
   } else {
@@ -607,7 +616,6 @@
   }
 
   append_output_window(output_buffer);
-  section_file_free(&sf);
 }
 
 /****************************************************************

[Prev in Thread] Current Thread [Next in Thread]