added looking for game music directory by checking directory files
authorHolger Schemel <info@artsoft.org>
Sun, 5 Feb 2023 17:55:51 +0000 (18:55 +0100)
committerHolger Schemel <info@artsoft.org>
Sun, 5 Feb 2023 17:55:53 +0000 (18:55 +0100)
Before, the first matching music directory was used -- regardless of
its contents. This means that it may indeed contain game music files
(that are not configured for any other purpose), or that it is a level
specific music directory that only contains some additional music
configured for a certain purpose, but no game music files, in which
case no game music files were found and used at all (as the general
custom music directory with all the game music would not be used in
this case).

This change adds looking for the first matching music directory that
really contains game music (not configured for other purposes).

src/files.c
src/libgame/setup.c
src/libgame/setup.h
src/libgame/sound.c

index fb0c740cb3f117a4622b29faefc851ba485697cb..ef38d17026b1277b953e48e0a19997601f148c50 100644 (file)
@@ -12911,7 +12911,7 @@ static boolean sound_info_listed(struct MusicFileInfo *list, char *basename)
 
 void LoadMusicInfo(void)
 {
-  char *music_directory = getCustomMusicDirectory();
+  char *music_directory = getCustomMusicDirectory_NoConf();
   int num_music = getMusicListSize();
   int num_music_noconf = 0;
   int num_sounds = getSoundListSize();
index e2405996731e195ec72dcd9b69ed92fa5a2ddeb2..ee7733fcd5216b97df49d184b9a0b1e87d2b8892 100644 (file)
@@ -19,6 +19,7 @@
 #include "platform.h"
 
 #include "setup.h"
+#include "sound.h"
 #include "joystick.h"
 #include "text.h"
 #include "misc.h"
@@ -1253,7 +1254,58 @@ char *getCustomArtworkLevelConfigFilename(int type)
   return filename;
 }
 
-char *getCustomMusicDirectory(void)
+static boolean directoryExists_CheckMusic(char *directory, boolean check_music)
+{
+  if (!directoryExists(directory))
+    return FALSE;
+
+  if (!check_music)
+    return TRUE;
+
+  Directory *dir;
+  DirectoryEntry *dir_entry;
+  int num_music = getMusicListSize();
+  boolean music_found = FALSE;
+
+  if ((dir = openDirectory(directory)) == NULL)
+    return FALSE;
+
+  while ((dir_entry = readDirectory(dir)) != NULL)     // loop all entries
+  {
+    char *basename = dir_entry->basename;
+    boolean music_already_used = FALSE;
+    int i;
+
+    // skip all music files that are configured in music config file
+    for (i = 0; i < num_music; i++)
+    {
+      struct FileInfo *music = getMusicListEntry(i);
+
+      if (strEqual(basename, music->filename))
+      {
+       music_already_used = TRUE;
+
+       break;
+      }
+    }
+
+    if (music_already_used)
+      continue;
+
+    if (FileIsMusic(dir_entry->filename))
+    {
+      music_found = TRUE;
+
+      break;
+    }
+  }
+
+  closeDirectory(dir);
+
+  return music_found;
+}
+
+static char *getCustomMusicDirectoryExt(boolean check_music)
 {
   static char *directory = NULL;
   boolean skip_setup_artwork = FALSE;
@@ -1264,7 +1316,7 @@ char *getCustomMusicDirectory(void)
   {
     // 1st try: look for special artwork in current level series directory
     directory = getPath2(getCurrentLevelDir(), MUSIC_DIRECTORY);
-    if (directoryExists(directory))
+    if (directoryExists_CheckMusic(directory, check_music))
       return directory;
 
     free(directory);
@@ -1274,7 +1326,7 @@ char *getCustomMusicDirectory(void)
     {
       // 2nd try: look for special artwork configured in level series config
       directory = getStringCopy(getLevelArtworkDir(TREE_TYPE_MUSIC_DIR));
-      if (directoryExists(directory))
+      if (directoryExists_CheckMusic(directory, check_music))
        return directory;
 
       free(directory);
@@ -1288,7 +1340,7 @@ char *getCustomMusicDirectory(void)
   {
     // 3rd try: look for special artwork in configured artwork directory
     directory = getStringCopy(getSetupArtworkDir(artwork.mus_current));
-    if (directoryExists(directory))
+    if (directoryExists_CheckMusic(directory, check_music))
       return directory;
 
     free(directory);
@@ -1296,19 +1348,29 @@ char *getCustomMusicDirectory(void)
 
   // 4th try: look for default artwork in new default artwork directory
   directory = getStringCopy(getDefaultMusicDir(MUS_DEFAULT_SUBDIR));
-  if (directoryExists(directory))
+  if (directoryExists_CheckMusic(directory, check_music))
     return directory;
 
   free(directory);
 
   // 5th try: look for default artwork in old default artwork directory
   directory = getStringCopy(options.music_directory);
-  if (directoryExists(directory))
+  if (directoryExists_CheckMusic(directory, check_music))
     return directory;
 
   return NULL;         // cannot find specified artwork file anywhere
 }
 
+char *getCustomMusicDirectory(void)
+{
+  return getCustomMusicDirectoryExt(FALSE);
+}
+
+char *getCustomMusicDirectory_NoConf(void)
+{
+  return getCustomMusicDirectoryExt(TRUE);
+}
+
 void MarkTapeDirectoryUploadsAsComplete(char *level_subdir)
 {
   char *filename = getPath2(getTapeDir(level_subdir), UPLOADED_FILENAME);
index a2107cd4ebed82076c2409c7f009d63570b4cb34..29f2f7c3f86ef169f65f557acd2255c3472d333c 100644 (file)
@@ -291,6 +291,7 @@ char *getCustomArtworkFilename(char *, int);
 char *getCustomArtworkConfigFilename(int);
 char *getCustomArtworkLevelConfigFilename(int);
 char *getCustomMusicDirectory(void);
+char *getCustomMusicDirectory_NoConf(void);
 
 void MarkTapeDirectoryUploadsAsComplete(char *);
 void MarkTapeDirectoryUploadsAsIncomplete(char *);
index 6ca95ea47de709bce3727f24bed5315fe753550c..3d085a9dbe8f8f336d241978616723cbcb8ec465 100644 (file)
@@ -591,7 +591,7 @@ static void LoadCustomMusic_NoConf(void)
 {
   static boolean draw_init_text = TRUE;                // only draw at startup
   static char *last_music_directory = NULL;
-  char *music_directory = getCustomMusicDirectory();
+  char *music_directory = getCustomMusicDirectory_NoConf();
   Directory *dir;
   DirectoryEntry *dir_entry;
   int num_music = getMusicListSize();