Complete.Org: Mailing Lists: Archives: freeciv-dev: October 2005:
[Freeciv-Dev] Re: (PR#14243) [PATCH] Scenario descriptions
Home

[Freeciv-Dev] Re: (PR#14243) [PATCH] Scenario descriptions

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: vasc@xxxxxxxxxxxxxx
Subject: [Freeciv-Dev] Re: (PR#14243) [PATCH] Scenario descriptions
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 9 Oct 2005 16:55:22 -0700
Reply-to: bugs@xxxxxxxxxxx

<URL: http://bugs.freeciv.org/Ticket/Display.html?id=14243 >

Jason Short wrote:
> <URL: http://bugs.freeciv.org/Ticket/Display.html?id=14243 >
> 
> But section_file_load is very expensive: probably well over half the 
> work of fully loading the savegame.  Doing this for every scenario isn't 
> good (even though right now all our scenarios are tiny).
> 
> This patch is, I think, much better.  The real solution would probably 
> be a section_file_load_part where you could pass it "scenario" and it 
> would only load the [scenario] section.
> 
> One thing this patch doesn't do is sorting.  Scenarios should be sorted 
> by name I think.

Here is a better patch that uses section_file_load_section to shortcut 
the loading.  I believe section_file_load_section can also be used to 
load info about savegames for the loading page.

Also I added translation support.

The remaining issue is still sorting.  AFAICT the scenarios are sorted 
by date, which is clearly wrong.  Sorting them differently will require 
some GUI magic (Vasco).

This patch is clearly awesome and should be committed very soon.

-jason

Index: client/gui-gtk-2.0/pages.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/pages.c,v
retrieving revision 1.45
diff -p -u -r1.45 pages.c
--- client/gui-gtk-2.0/pages.c  9 Oct 2005 21:25:30 -0000       1.45
+++ client/gui-gtk-2.0/pages.c  9 Oct 2005 23:54:40 -0000
@@ -1551,18 +1551,22 @@ static void update_scenario_page(void)
   datafile_list_iterate(files, pfile) {
     GtkTreeIter it;
     struct section_file sf;
-    char *description;
+    char *description = NULL, *name = NULL;
 
-    if (section_file_load(&sf, pfile->fullname)) {
-      description = secfile_lookup_str_default(&sf, "", "game.description");
-    } else {
-      description = "";
+    if (section_file_load_section(&sf, pfile->fullname, "scenario")) {
+      name = secfile_lookup_str_default(&sf, NULL, "scenario.name");
+      description = secfile_lookup_str_default(&sf,
+                                              NULL, "scenario.description");
+      section_file_free(&sf);
     }
-    section_file_free(&sf);
+
+    /* Translated loaded names (if any). */
+    name = name ? _(name) : pfile->name;
+    description = description ? _(description) : "";
 
     gtk_list_store_append(scenario_store, &it);
     gtk_list_store_set(scenario_store, &it,
-       0, pfile->name, 1, pfile->fullname, 2, description, -1);
+                      0, name, 1, pfile->fullname, 2, description, -1);
 
     free(pfile->name);
     free(pfile->fullname);
Index: data/scenario/british-isles-85x80-v2.80.sav
===================================================================
RCS file: 
/home/freeciv/CVS/freeciv/data/scenario/british-isles-85x80-v2.80.sav,v
retrieving revision 1.4
diff -p -u -r1.4 british-isles-85x80-v2.80.sav
--- data/scenario/british-isles-85x80-v2.80.sav 26 Jul 2005 09:55:22 -0000      
1.4
+++ data/scenario/british-isles-85x80-v2.80.sav 9 Oct 2005 23:54:41 -0000
@@ -1,3 +1,8 @@
+# Keep this section first
+[scenario]
+name=_("British Isles (medium/classic)")
+description=_("Classic-style 85x80 map of the British Isles.")
+
 [game]
 version=13702
 server_state=0
Index: data/scenario/earth-160x90-v2.sav
===================================================================
RCS file: /home/freeciv/CVS/freeciv/data/scenario/earth-160x90-v2.sav,v
retrieving revision 1.8
diff -p -u -r1.8 earth-160x90-v2.sav
--- data/scenario/earth-160x90-v2.sav   3 Oct 2005 07:26:41 -0000       1.8
+++ data/scenario/earth-160x90-v2.sav   9 Oct 2005 23:54:41 -0000
@@ -1,3 +1,7 @@
+# Keep this section first
+[scenario]
+name=_("Earth (classic, large)")
+description=_("Classic-style 160x90 Earth scenario.")
 
 [game]
 version=10900
Index: data/scenario/earth-80x50-v2.sav
===================================================================
RCS file: /home/freeciv/CVS/freeciv/data/scenario/earth-80x50-v2.sav,v
retrieving revision 1.7
diff -p -u -r1.7 earth-80x50-v2.sav
--- data/scenario/earth-80x50-v2.sav    18 Sep 2005 11:47:56 -0000      1.7
+++ data/scenario/earth-80x50-v2.sav    9 Oct 2005 23:54:41 -0000
@@ -1,3 +1,7 @@
+# Keep this section first
+[scenario]
+name=_("Earth (classic, small)")
+description=_("Classic-style 80x50 Earth scenario.")
 
 [game]
 version=11105
Index: data/scenario/europe-200x100-v2.sav
===================================================================
RCS file: /home/freeciv/CVS/freeciv/data/scenario/europe-200x100-v2.sav,v
retrieving revision 1.8
diff -p -u -r1.8 europe-200x100-v2.sav
--- data/scenario/europe-200x100-v2.sav 30 Jun 2005 08:23:37 -0000      1.8
+++ data/scenario/europe-200x100-v2.sav 9 Oct 2005 23:54:41 -0000
@@ -1,3 +1,7 @@
+# Keep this section first
+[scenario]
+name="Europe (giant/classic)"
+description="Very large classic-style map of Europe."
 
 [game]
 version=11105
Index: data/scenario/hagworld-120x60-v1.2.sav
===================================================================
RCS file: /home/freeciv/CVS/freeciv/data/scenario/hagworld-120x60-v1.2.sav,v
retrieving revision 1.3
diff -p -u -r1.3 hagworld-120x60-v1.2.sav
--- data/scenario/hagworld-120x60-v1.2.sav      2 Aug 2005 06:23:33 -0000       
1.3
+++ data/scenario/hagworld-120x60-v1.2.sav      9 Oct 2005 23:54:41 -0000
@@ -1,4 +1,10 @@
 #  2001-05-28 by Helge Arne Gudmestad, gudmesta@xxxxxxx
+
+# Keep this section first
+[scenario]
+name=_("Earth (classic, medium)")
+description=_("Classic-style 120x60 Earth scenario.")
+
 [game]
 version=11105
 server_state=2
Index: data/scenario/iberian-peninsula-136x100-v1.0.sav
===================================================================
RCS file: 
/home/freeciv/CVS/freeciv/data/scenario/iberian-peninsula-136x100-v1.0.sav,v
retrieving revision 1.2
diff -p -u -r1.2 iberian-peninsula-136x100-v1.0.sav
--- data/scenario/iberian-peninsula-136x100-v1.0.sav    18 Feb 2004 21:06:43 
-0000      1.2
+++ data/scenario/iberian-peninsula-136x100-v1.0.sav    9 Oct 2005 23:54:41 
-0000
@@ -1,3 +1,8 @@
+# Keep this section first
+[scenario]
+name=_("Iberian Peninsula (classic/large)")
+description=_("Large classic-style map of the Iberian Peninsula\
+ (modern-day Spain and Portugal).")
 
 [game]
 version=11104
Index: data/scenario/tutorial.sav
===================================================================
RCS file: /home/freeciv/CVS/freeciv/data/scenario/tutorial.sav,v
retrieving revision 1.5
diff -p -u -r1.5 tutorial.sav
--- data/scenario/tutorial.sav  9 Oct 2005 21:25:31 -0000       1.5
+++ data/scenario/tutorial.sav  9 Oct 2005 23:54:41 -0000
@@ -1,3 +1,8 @@
+# Keep this section first
+[scenario]
+name=_("Tutorial")
+description=_("Tutorial Scenario.")
+
 [script]
 code="
 function turn_callback(turn, year)
Index: po/POTFILES.in
===================================================================
RCS file: /home/freeciv/CVS/freeciv/po/POTFILES.in,v
retrieving revision 1.98
diff -p -u -r1.98 POTFILES.in
--- po/POTFILES.in      3 Oct 2005 02:50:46 -0000       1.98
+++ po/POTFILES.in      9 Oct 2005 23:54:41 -0000
@@ -301,4 +301,10 @@ data/nation/vietnamese.ruleset
 data/nation/viking.ruleset
 data/nation/welsh.ruleset
 data/nation/zulu.ruleset
+data/scenario/british-isles-85x80-v2.80.sav
+data/scenario/earth-160x90-v2.sav
+data/scenario/earth-80x50-v2.sav
+data/scenario/europe-200x100-v2.sav
+data/scenario/hagworld-120x60-v1.2.sav
+data/scenario/iberian-peninsula-136x100-v1.0.sav
 data/scenario/tutorial.sav
Index: utility/registry.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/registry.c,v
retrieving revision 1.70
diff -p -u -r1.70 registry.c
--- utility/registry.c  7 May 2005 18:45:13 -0000       1.70
+++ utility/registry.c  9 Oct 2005 23:54:42 -0000
@@ -401,7 +401,8 @@ static struct section *find_section_by_n
 static bool section_file_read_dup(struct section_file *sf,
                                  const char *filename,
                                  struct inputfile *inf,
-                                 bool allow_duplicates)
+                                 bool allow_duplicates,
+                                 const char *requested_section)
 {
   struct section *psection = NULL;
   struct entry *pentry;
@@ -413,6 +414,7 @@ static bool section_file_read_dup(struct
   struct astring base_name = ASTRING_INIT;    /* for table or single entry */
   struct astring entry_name = ASTRING_INIT;
   struct astring_vector columns;    /* astrings for column headings */
+  bool found_my_section = FALSE;
 
   if (!inf) {
     return FALSE;
@@ -441,6 +443,16 @@ static bool section_file_read_dup(struct
     }
     tok = inf_token(inf, INF_TOK_SECTION_NAME);
     if (tok) {
+      if (found_my_section) {
+       /* This shortcut will stop any further loading after the requested
+        * section has been loaded (i.e., at the start of a new section).
+        * This is needed to make the behavior useful, since the whole
+        * purpose is to short-cut further loading of the file.  However
+        * normally a section may be split up, and that will no longer
+        * work here because it will be short-cut. */
+       inf_log(inf, LOG_DEBUG, "found requested section; finishing");
+       return TRUE;
+      }
       if (table_state) {
        inf_log(inf, LOG_ERROR, "new section during table");
         return FALSE;
@@ -452,15 +464,25 @@ static bool section_file_read_dup(struct
       */
       psection = find_section_by_name(sf, tok);
       if (!psection) {
-       psection = section_file_append_section(sf, tok);
+       if (!requested_section || strcmp(tok, requested_section) == 0) {
+         psection = section_file_append_section(sf, tok);
+         if (requested_section) {
+           found_my_section = TRUE;
+         }
+       }
       }
       (void) inf_token_required(inf, INF_TOK_EOL);
       continue;
     }
+#if 0
     if (!psection) {
+      /* This used to be an error.  However there's no reason it shouldn't
+       * be allowed, and it breaks the rest of the requested_section code.
+       * It's been defined out but may be of use sometime. */
       inf_log(inf, LOG_ERROR, "data before first section");
       return FALSE;
     }
+#endif
     if (inf_token(inf, INF_TOK_TABLE_END)) {
       if (!table_state) {
        inf_log(inf, LOG_ERROR, "misplaced \"}\"");
@@ -493,9 +515,13 @@ static bool section_file_read_dup(struct
                      columns.p[num_columns - 1].str,
                      (int) (i - num_columns + 1));
        }
-       pentry = new_entry(sb, entry_name.str, tok);
-       entry_list_append(psection->entries, pentry);
-       sf->num_entries++;
+       if (psection) {
+         /* Load this entry (if psection == NULL the entry is silently
+          * skipped). */
+         pentry = new_entry(sb, entry_name.str, tok);
+         entry_list_append(psection->entries, pentry);
+         sf->num_entries++;
+       }
       } while(inf_token(inf, INF_TOK_COMMA));
       
       (void) inf_token_required(inf, INF_TOK_EOL);
@@ -562,8 +588,12 @@ static bool section_file_read_dup(struct
                    "%s,%d", base_name.str, i);
        pentry = new_entry(sb, entry_name.str, tok);
       }
-      entry_list_append(psection->entries, pentry);
-      sf->num_entries++;
+      if (psection) {
+       /* Load this entry (if psection == NULL the entry is silently
+        * skipped). */
+       entry_list_append(psection->entries, pentry);
+       sf->num_entries++;
+      }
     } while(inf_token(inf, INF_TOK_COMMA));
     (void) inf_token_required(inf, INF_TOK_EOL);
   }
@@ -598,13 +628,27 @@ static bool section_file_read_dup(struct
 bool section_file_load(struct section_file *my_section_file,
                      const char *filename)
 {
+  return section_file_load_section(my_section_file, filename, NULL);
+}
+
+/**************************************************************************
+  Like section_file_load, but this function will only load one "part" of
+  the section file.  For instance if you pass in "tutorial", then it will
+  only load the [tutorial] section.
+
+  Passing in NULL will give identical results to section_file_load.
+**************************************************************************/
+bool section_file_load_section(struct section_file *my_section_file,
+                              const char *filename, const char *part)
+{
   char real_filename[1024];
   struct inputfile *inf;
 
   interpret_tilde(real_filename, sizeof(real_filename), filename);
   inf = inf_from_file(real_filename, datafilename);
 
-  return section_file_read_dup(my_section_file, real_filename, inf, TRUE);
+  return section_file_read_dup(my_section_file, real_filename,
+                              inf, TRUE, NULL);
 }
 
 /**************************************************************************
@@ -619,7 +663,8 @@ bool section_file_load_nodup(struct sect
   interpret_tilde(real_filename, sizeof(real_filename), filename);
   inf = inf_from_file(real_filename, datafilename);
 
-  return section_file_read_dup(my_section_file, real_filename, inf, FALSE);
+  return section_file_read_dup(my_section_file, real_filename,
+                              inf, FALSE, NULL);
 }
 
 /**************************************************************************
@@ -630,7 +675,7 @@ bool section_file_load_from_stream(struc
 {
   struct inputfile *inf = inf_from_stream(stream, datafilename);
 
-  return section_file_read_dup(my_section_file, NULL, inf, TRUE);
+  return section_file_read_dup(my_section_file, NULL, inf, TRUE, NULL);
 }
 
 /**************************************************************************
Index: utility/registry.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/registry.h,v
retrieving revision 1.26
diff -p -u -r1.26 registry.h
--- utility/registry.h  7 May 2005 18:45:13 -0000       1.26
+++ utility/registry.h  9 Oct 2005 23:54:42 -0000
@@ -32,6 +32,8 @@ struct section_file {
 void section_file_init(struct section_file *file);
 bool section_file_load(struct section_file *my_section_file,
                       const char *filename);
+bool section_file_load_section(struct section_file *my_section_file,
+                              const char *filename, const char *section);
 bool section_file_load_nodup(struct section_file *my_section_file,
                            const char *filename);
 bool section_file_load_from_stream(struct section_file *my_section_file,

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