From a4fa40cbdd9ef5a4c9a4c18e34298fa9ec49f53d Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Sun, 5 Feb 2023 18:55:51 +0100 Subject: [PATCH] added looking for game music directory by checking directory files 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 | 2 +- src/libgame/setup.c | 74 +++++++++++++++++++++++++++++++++++++++++---- src/libgame/setup.h | 1 + src/libgame/sound.c | 2 +- 4 files changed, 71 insertions(+), 8 deletions(-) diff --git a/src/files.c b/src/files.c index fb0c740c..ef38d170 100644 --- a/src/files.c +++ b/src/files.c @@ -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(); diff --git a/src/libgame/setup.c b/src/libgame/setup.c index e2405996..ee7733fc 100644 --- a/src/libgame/setup.c +++ b/src/libgame/setup.c @@ -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); diff --git a/src/libgame/setup.h b/src/libgame/setup.h index a2107cd4..29f2f7c3 100644 --- a/src/libgame/setup.h +++ b/src/libgame/setup.h @@ -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 *); diff --git a/src/libgame/sound.c b/src/libgame/sound.c index 6ca95ea4..3d085a9d 100644 --- a/src/libgame/sound.c +++ b/src/libgame/sound.c @@ -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(); -- 2.34.1