From ae94c4ae1345b145e798c030229279cbf2348ee1 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Wed, 30 Aug 2023 07:44:17 +0300
Subject: [PATCH 11/11] Add support for musicset summary and description

Only client-common part. No gui shows the data yet.

See osdn #47639

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 client/audio.c   | 13 +++++++--
 client/music.c   | 71 ++++++++++++++++++++++++++++++++++++++++++++++++
 client/music.h   |  8 +++++-
 client/options.c |  3 +-
 4 files changed, 90 insertions(+), 5 deletions(-)

diff --git a/client/audio.c b/client/audio.c
index 5522cd45b8..b48b30bdfc 100644
--- a/client/audio.c
+++ b/client/audio.c
@@ -34,8 +34,9 @@
 #include "audio_none.h"
 #ifdef AUDIO_SDL
 #include "audio_sdl.h"
-#endif
+#endif /* AUDIO_SDL */
 #include "client_main.h"
+#include "music.h"
 #include "options.h"
 
 #include "audio.h"
@@ -283,7 +284,9 @@ void audio_real_init(const char *const soundset_name,
     /* We explicitly choose none plugin, silently skip the code below */
     log_verbose("Proceeding with sound support disabled.");
     ss_tagfile = NULL;
+    musicspec_close(ms_tagfile);
     ms_tagfile = NULL;
+
     return;
   }
   if (num_plugins_used == 1) {
@@ -293,7 +296,9 @@ void audio_real_init(const char *const soundset_name,
     log_normal(_("For sound support, install SDL2_mixer"));
     log_normal("http://www.libsdl.org/projects/SDL_mixer/index.html");
     ss_tagfile = NULL;
+    musicspec_close(ms_tagfile);
     ms_tagfile = NULL;
+
     return;
   }
   if (!soundset_name) {
@@ -316,7 +321,9 @@ void audio_real_init(const char *const soundset_name,
                "https://www.freeciv.org/wiki/Sounds");
     log_normal(_("Proceeding with sound support disabled."));
     ss_tagfile = NULL;
+    musicspec_close(ms_tagfile);
     ms_tagfile = NULL;
+
     return;
   }
   if (!(ss_tagfile = secfile_load(ss_filename, TRUE))) {
@@ -324,7 +331,7 @@ void audio_real_init(const char *const soundset_name,
               secfile_error());
     exit(EXIT_FAILURE);
   }
-  if (!(ms_tagfile = secfile_load(ms_filename, TRUE))) {
+  if (!(ms_tagfile = musicspec_load(ms_filename))) {
     log_fatal(_("Could not load music spec-file '%s':\n%s"), ms_filename,
               secfile_error());
     exit(EXIT_FAILURE);
@@ -661,7 +668,7 @@ void audio_shutdown(bool play_quit_tag)
     ss_tagfile = NULL;
   }
   if (NULL != ms_tagfile) {
-    secfile_destroy(ms_tagfile);
+    musicspec_close(ms_tagfile);
     ms_tagfile = NULL;
   }
 }
diff --git a/client/music.c b/client/music.c
index cce87977fa..544489aefe 100644
--- a/client/music.c
+++ b/client/music.c
@@ -30,6 +30,10 @@
 
 #include "music.h"
 
+
+static char *ms_summary = NULL;
+static char *ms_description = NULL;
+
 /**********************************************************************//**
   Start music suitable for current game situation
 **************************************************************************/
@@ -131,3 +135,70 @@ void musicspec_reread_callback(struct option *poption)
     start_style_music();
   }
 }
+
+/**********************************************************************//**
+  Load specified musicspec.
+
+  This is called from the audio code, and not the vice versa.
+**************************************************************************/
+struct section_file *musicspec_load(const char *ms_filename)
+{
+  struct section_file *tagfile = secfile_load(ms_filename, TRUE);
+
+  if (tagfile != NULL) {
+    const char *mstr;
+
+    mstr = secfile_lookup_str_default(tagfile, "", "musicspec.summary");
+    if (mstr[0] != '\0') {
+      size_t len;
+
+      /* Musicset summary found */
+      len = strlen(mstr);
+      ms_summary = fc_malloc(len + 1);
+      fc_strlcpy(ms_summary, mstr, len + 1);
+    } else {
+      /* No summary */
+      if (ms_summary != NULL) {
+        free(ms_summary);
+        ms_summary = NULL;
+      }
+    }
+
+    mstr = secfile_lookup_str_default(tagfile, "", "musicspec.description");
+    if (mstr[0] != '\0') {
+      size_t len;
+
+      /* Musicset description found */
+      len = strlen(mstr);
+      ms_description = fc_malloc(len + 1);
+      fc_strlcpy(ms_description, mstr, len + 1);
+    } else {
+      /* No summary */
+      if (ms_description != NULL) {
+        free(ms_description);
+        ms_description = NULL;
+      }
+    }
+  }
+
+  return tagfile;
+}
+
+/**********************************************************************//**
+  Close the musicspec.
+  Tagfile should refer to currently active musicspec as some data
+  is not retrievable from tagfile alone, but currently active music only.
+
+  This is called from the audio code, and not the vice versa.
+**************************************************************************/
+void musicspec_close(struct section_file *tagfile)
+{
+  if (tagfile != NULL) {
+    free(ms_summary);
+    ms_summary = NULL;
+    free(ms_description);
+    ms_description = NULL;
+
+    secfile_destroy(tagfile);
+  }
+}
diff --git a/client/music.h b/client/music.h
index 9159787006..1790dede98 100644
--- a/client/music.h
+++ b/client/music.h
@@ -1,4 +1,4 @@
-/********************************************************************** 
+/***********************************************************************
  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
@@ -17,6 +17,9 @@
 extern "C" {
 #endif /* __cplusplus */
 
+/* client */
+#include "options.h"
+
 void start_style_music(void);
 void stop_style_music(void);
 void start_menu_music(const char *const tag, char *const alt_tag);
@@ -25,6 +28,9 @@ void play_single_track(const char *const tag);
 
 void musicspec_reread_callback(struct option *poption);
 
+struct section_file *musicspec_load(const char *ms_filename);
+void musicspec_close(struct section_file *tagfile);
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
diff --git a/client/options.c b/client/options.c
index 931d81e959..a10c9cf360 100644
--- a/client/options.c
+++ b/client/options.c
@@ -1961,7 +1961,8 @@ static struct client_option client_options[] = {
                          "this is the same as using the -m command-line "
                          "parameter. Use modpack installer utility to install "
                          "additional musicsets."),
-                      COC_SOUND, GUI_STUB, "stdmusic", get_musicset_list, musicspec_reread_callback, 0),
+                      COC_SOUND, GUI_STUB, "stdmusic", get_musicset_list,
+                      musicspec_reread_callback, 0),
   GEN_STR_LIST_OPTION(default_sound_plugin_name,
                       N_("Sound plugin"),
                       N_("If you have a problem with sound, try changing "
-- 
2.40.1