foxygit / doom Log in
commit 50742cfc923a5be33398973072818f0c94950809
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Sat Apr 12 21:02:41 2014 -0400
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Sat Apr 12 21:02:41 2014 -0400

    music: Fix -dumpsubstconfig for Heretic/Hexen.

    The config dumping command line option assumed that music lumps were
    named like D_MYLUMP, but this is not the case for Heretic and Hexen,
    where there is no D_ prefix and music lumps can have any name.

    Change the logic to instead look at the contents of lumps and identify
    music lumps from the MUS / MIDI header.
---
 src/i_sdlmusic.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)

diff --git a/src/i_sdlmusic.c b/src/i_sdlmusic.c
index e2be3f50..2a714d3d 100644
--- a/src/i_sdlmusic.c
+++ b/src/i_sdlmusic.c
@@ -48,6 +48,8 @@
 #include "z_zone.h"

 #define MAXMIDLENGTH (96 * 1024)
+#define MID_HEADER_MAGIC "MThd"
+#define MUS_HEADER_MAGIC "MUS\x1a"

 // Structure for music substitution.
 // We store a mapping based on SHA1 checksum -> filename of substitute music
@@ -344,6 +346,31 @@ static void LoadSubstituteConfigs(void)
     }
 }

+// Returns true if the given lump number is a music lump that should
+// be included in substitute configs.
+// Identifying music lumps by name is not feasible; some games (eg.
+// Heretic, Hexen) don't have a common naming pattern for music lumps.
+
+static boolean IsMusicLump(int lumpnum)
+{
+    byte *data;
+    boolean result;
+
+    if (W_LumpLength(lumpnum) < 4)
+    {
+        return false;
+    }
+
+    data = W_CacheLumpNum(lumpnum, PU_STATIC);
+
+    result = memcmp(data, MUS_HEADER_MAGIC, 4) == 0
+          || memcmp(data, MID_HEADER_MAGIC, 4) == 0;
+
+    W_ReleaseLumpNum(lumpnum);
+
+    return result;
+}
+
 // Dump an example config file containing checksums for all MIDI music
 // found in the WAD directory.

@@ -372,7 +399,7 @@ static void DumpSubstituteConfig(char *filename)
         strncpy(name, lumpinfo[lumpnum].name, 8);
         name[8] = '\0';

-        if (!M_StringStartsWith(name, "D_"))
+        if (!IsMusicLump(lumpnum))
         {
             continue;
         }