From 8b28c84f8442caf911011e68afc84c8ad29982e7 Mon Sep 17 00:00:00 2001 From: avolkov Date: Tue, 3 May 2005 23:40:16 +0000 Subject: [PATCH] Allow for mod<->ogg switching everywhere, including tracks loaded with LoadMusicFile() git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@1707 8092fc87-c524-0410-9efc-e669fe64eaf9 --- sc2/src/sc2code/libs/sound/fileinst.c | 13 ++++- sc2/src/sc2code/libs/sound/music.c | 68 +++++++++++++++++---------- sc2/src/sc2code/libs/sound/sndintrn.h | 2 + 3 files changed, 56 insertions(+), 27 deletions(-) diff --git a/sc2/src/sc2code/libs/sound/fileinst.c b/sc2/src/sc2code/libs/sound/fileinst.c index 567a3f796..84fa853ef 100644 --- a/sc2/src/sc2code/libs/sound/fileinst.c +++ b/sc2/src/sc2code/libs/sound/fileinst.c @@ -19,6 +19,7 @@ #include "sndintrn.h" #include "options.h" #include "reslib.h" +#include SOUND_REF @@ -52,18 +53,26 @@ MUSIC_REF LoadMusicFile (PVOID pStr) { uio_Stream *fp; + char filename[256]; // FIXME: this theoretically needs a mechanism to prevent races if (_cur_resfile_name) // something else is loading resources atm return 0; - fp = res_OpenResFile (contentDir, pStr, "rb"); + strncpy (filename, pStr, sizeof(filename) - 1); + filename[sizeof(filename) - 1] = '\0'; + CheckMusicResName (filename); + + // Opening the res file is not technically necessary right now + // since _GetMusicData() completely ignores the arguments + // But just for the sake of correctness + fp = res_OpenResFile (contentDir, filename, "rb"); if (fp) { MEM_HANDLE hData; - _cur_resfile_name = pStr; + _cur_resfile_name = filename; hData = _GetMusicData (fp, LengthResFile (fp)); _cur_resfile_name = 0; diff --git a/sc2/src/sc2code/libs/sound/music.c b/sc2/src/sc2code/libs/sound/music.c index 4b58f1f50..14aca8b10 100644 --- a/sc2/src/sc2code/libs/sound/music.c +++ b/sc2/src/sc2code/libs/sound/music.c @@ -108,6 +108,43 @@ SetMusicVolume (COUNT Volume) audio_Sourcef (soundSource[MUSIC_SOURCE].handle, audio_GAIN, f); } +char* +CheckMusicResName (char* fileName) +{ + char otherName[256]; + const char* otherExt; + char* curExt; + + if (strlen (fileName) < 4) + return fileName; + + strncpy (otherName, fileName, sizeof (otherName) - 1); + otherName[sizeof (otherName) - 1] = '\0'; + + switch (optWhichMusic) + { + default: + case OPT_3DO: + otherExt = "ogg"; + break; + case OPT_PC: + otherExt = "mod"; + break; + } + + curExt = otherName + strlen (otherName) - strlen (otherExt); + if (strcmp(curExt, otherExt) != 0) + { + strcpy (curExt, otherExt); + if (fileExists2 (contentDir, otherName)) + strcpy (fileName, otherName); + else + fprintf (stderr, "Requested track '%s' not found!\n", otherName); + } + + return fileName; +} + MEM_HANDLE _GetMusicData (uio_Stream *fp, DWORD length) { @@ -127,33 +164,14 @@ _GetMusicData (uio_Stream *fp, DWORD length) } else { - char filename[1000]; + char filename[256]; - *pmus = (TFB_SoundSample *) HCalloc (sizeof (TFB_SoundSample)); - strcpy (filename, _cur_resfile_name); + *pmus = (TFB_SoundSample *) HCalloc (sizeof (TFB_SoundSample)); + strncpy (filename, _cur_resfile_name, sizeof(filename) - 1); + filename[sizeof(filename) - 1] = '\0'; + CheckMusicResName (filename); - switch (optWhichMusic) - { - default: - case OPT_3DO: - { - char threedoname[1000]; - - strcpy (threedoname, filename); - strcpy (&threedoname[strlen (threedoname)-3], "ogg"); - if (fileExists2 (contentDir, threedoname)) - { - strcpy (filename, threedoname); - } - break; - } - case OPT_PC: - { - break; - } - } - - fprintf (stderr, "_GetMusicData(): loading %s\n", filename); + fprintf (stderr, "_GetMusicData(): loading %s\n", filename); if (((*pmus)->decoder = SoundDecoder_Load (contentDir, filename, 4096, 0, 0)) == 0) { diff --git a/sc2/src/sc2code/libs/sound/sndintrn.h b/sc2/src/sc2code/libs/sound/sndintrn.h index c6b63bedc..6aced96c2 100644 --- a/sc2/src/sc2code/libs/sound/sndintrn.h +++ b/sc2/src/sc2code/libs/sound/sndintrn.h @@ -34,5 +34,7 @@ extern BOOLEAN _ReleaseSoundBankData (MEM_HANDLE handle); #define UnlockMusicData UnlockResourceData #define FreeMusicData _ReleaseMusicData +extern char* CheckMusicResName (char* filename); + #endif /* _SNDINTRN_H */