foxygit / doom Log in
commit ceb3e491892eb8facd2fac1ea7b09752fffc40f4
Author:     Simon Howard <fraggle@soulsphere.org>
AuthorDate: Sun Oct 21 19:30:44 2018 -0400
Commit:     Simon Howard <fraggle@soulsphere.org>
CommitDate: Sun Oct 21 19:30:44 2018 -0400

    music: Auto-configure a music pack directory.

    Currently the user has to create a music pack directory themselves and
    configure the path to it in the setup tool. To make configuration
    simpler, just create a directory automatically along with a README file
    about how to use it, since the user can now just dump files in this
    directory.

    Part of #1051.
---
 src/i_sound.c        |  5 ++++-
 src/m_config.c       | 39 +++++++++++++++++++++++++++++++++++++++
 src/m_config.h       |  1 +
 src/setup/mainmenu.c |  4 ++++
 4 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/src/i_sound.c b/src/i_sound.c
index e0c912e8..6bf19208 100644
--- a/src/i_sound.c
+++ b/src/i_sound.c
@@ -187,7 +187,7 @@ static void InitMusicModule(void)
 //

 void I_InitSound(boolean use_sfx_prefix)
-{
+{
     boolean nosound, nosfx, nomusic;

     //!
@@ -214,6 +214,9 @@ void I_InitSound(boolean use_sfx_prefix)

     nomusic = M_CheckParm("-nomusic") > 0;

+    // Auto configure the music pack directory.
+    M_SetMusicPackDir();
+
     // Initialize the sound and music subsystems.

     if (!nosound && !screensaver_mode)
diff --git a/src/m_config.c b/src/m_config.c
index c39b9f60..eeb825e9 100644
--- a/src/m_config.c
+++ b/src/m_config.c
@@ -2211,6 +2211,45 @@ void M_SetConfigDir(const char *dir)
     M_MakeDirectory(configdir);
 }

+#define MUSIC_PACK_README \
+"Extract music packs into this directory in .flac or .ogg format;\n"   \
+"they will be automatically loaded based on filename to replace the\n" \
+"in-game music with high quality versions.\n\n" \
+"For more information check here:\n\n" \
+"  <https://www.chocolate-doom.org/wiki/index.php/Digital_music_packs>\n\n"
+
+// Set the value of music_pack_path if it is currently empty, and create
+// the directory if necessary.
+void M_SetMusicPackDir(void)
+{
+    const char *current_path;
+    char *prefdir, *music_pack_path, *readme_path;
+
+    current_path = M_GetStringVariable("music_pack_path");
+
+    if (current_path != NULL && strlen(current_path) > 0)
+    {
+        return;
+    }
+
+    prefdir = SDL_GetPrefPath("", PACKAGE_TARNAME);
+    music_pack_path = M_StringJoin(prefdir, "music-packs", NULL);
+
+    M_MakeDirectory(prefdir);
+    M_MakeDirectory(music_pack_path);
+    M_SetVariable("music_pack_path", music_pack_path);
+
+    // We write a README file with some basic instructions on how to use
+    // the directory.
+    readme_path = M_StringJoin(music_pack_path, DIR_SEPARATOR_S,
+                               "README.txt", NULL);
+    M_WriteFile(readme_path, MUSIC_PACK_README, strlen(MUSIC_PACK_README));
+
+    free(readme_path);
+    free(music_pack_path);
+    free(prefdir);
+}
+
 //
 // Calculate the path to the directory to use to store save games.
 // Creates the directory as necessary.
diff --git a/src/m_config.h b/src/m_config.h
index 17b9210e..4bc3ba54 100644
--- a/src/m_config.h
+++ b/src/m_config.h
@@ -26,6 +26,7 @@ void M_LoadDefaults(void);
 void M_SaveDefaults(void);
 void M_SaveDefaultsAlternate(const char *main, const char *extra);
 void M_SetConfigDir(const char *dir);
+void M_SetMusicPackDir(void);
 void M_BindIntVariable(const char *name, int *variable);
 void M_BindFloatVariable(const char *name, float *variable);
 void M_BindStringVariable(const char *name, char **variable);
diff --git a/src/setup/mainmenu.c b/src/setup/mainmenu.c
index 80cdda6d..67089d8c 100644
--- a/src/setup/mainmenu.c
+++ b/src/setup/mainmenu.c
@@ -262,6 +262,10 @@ static void InitConfig(void)
     SetPlayerNameDefault();

     M_LoadDefaults();
+
+    // Create and configure the music pack directory if it does not
+    // already exist.
+    M_SetMusicPackDir();
 }

 //