Duke3D/Sound: Restructure S_OpenAudio to check in more paths. For each path, it performs the filename transformations and checks it does currently.

First, it will try the local path (as previously).
Second, it will check to see if the filename originally requested exists inside a GRP or SSI file (its parent), and if it does, it will check in "music/<parent's name sans extension>/".
Third, it will check in "music/".

Do not rely on this when distributing mods: These checks are hacks and may potentially be removed.

git-svn-id: https://svn.eduke32.com/eduke32@5445 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
hendricks266 2015-11-25 12:08:07 +00:00
parent fcd7b2e0b1
commit 26aca9b291

View file

@ -1034,63 +1034,112 @@ void G_LoadLookups(void)
kclose(fp); kclose(fp);
} }
int32_t S_OpenAudio(const char *fn, char searchfirst) //////////
static int32_t S_TryFormats(char const * const testfn, char * const fn_suffix, char const searchfirst)
{ {
char *testfn, *extension; #ifdef HAVE_FLAC
int32_t fp = -1; {
Bstrcpy(fn_suffix, ".flac");
int32_t const fp = kopen4loadfrommod(testfn, searchfirst);
if (fp >= 0)
return fp;
}
#endif
testfn = (char *)Xmalloc(Bstrlen(fn) + 6); #ifdef HAVE_VORBIS
Bstrcpy(testfn, fn); {
extension = Bstrrchr(testfn, '.'); Bstrcpy(fn_suffix, ".ogg");
int32_t const fp = kopen4loadfrommod(testfn, searchfirst);
if (fp >= 0)
return fp;
}
#endif
return -1;
}
static int32_t S_TryExtensionReplacements(char const * const testfn, char const searchfirst)
{
char * extension = Bstrrchr(testfn, '.');
char * const fn_end = Bstrchr(testfn, '\0');
// ex: grabbag.voc --> grabbag_voc.*
if (extension != NULL) if (extension != NULL)
{ {
char * const fn_end = Bstrrchr(testfn, '\0');
*extension = '_'; *extension = '_';
#ifdef HAVE_FLAC int32_t const fp = S_TryFormats(testfn, fn_end, searchfirst);
char const * const extFLAC = ".flac";
Bstrcpy(fn_end, extFLAC);
fp = kopen4loadfrommod(testfn, searchfirst);
if (fp >= 0) if (fp >= 0)
{
Bfree(testfn);
return fp; return fp;
} }
#endif else
#ifdef HAVE_VORBIS
char const * const extOGG = ".ogg";
Bstrcpy(fn_end, extOGG);
fp = kopen4loadfrommod(testfn, searchfirst);
if (fp >= 0)
{ {
Bfree(testfn); extension = fn_end;
}
// ex: grabbag.mid --> grabbag.*
{
int32_t const fp = S_TryFormats(testfn, extension, searchfirst);
if (fp >= 0)
return fp; return fp;
} }
#endif
#ifdef HAVE_FLAC return -1;
Bstrcpy(extension, extFLAC); }
fp = kopen4loadfrommod(testfn, searchfirst);
if (fp >= 0) int32_t S_OpenAudio(const char *fn, char searchfirst)
{ {
Bfree(testfn); int32_t const origfp = kopen4loadfrommod(fn, searchfirst);
return fp; char const * const origparent = origfp != -1 ? kfileparent(origfp) : NULL;
} uint32_t const origparentlength = origparent != NULL ? Bstrlen(origparent) : 0;
#endif
char * const testfn = (char *)Xmalloc(Bstrlen(fn) + 12 + origparentlength); // "music/" + overestimation of parent minus extension + ".flac" + '\0'
#ifdef HAVE_VORBIS
Bstrcpy(extension, extOGG); // look in ./
fp = kopen4loadfrommod(testfn, searchfirst); // ex: ./grabbag.mid
if (fp >= 0) {
{ Bstrcpy(testfn, fn);
Bfree(testfn); int32_t const fp = S_TryExtensionReplacements(testfn, searchfirst);
return fp; if (fp >= 0)
} {
#endif Bfree(testfn);
} kclose(origfp);
return fp;
Bfree(testfn); }
return kopen4loadfrommod(fn, searchfirst); }
// look in ./music/<file's parent GRP name>/
// ex: ./music/duke3d/grabbag.mid
// ex: ./music/nwinter/grabbag.mid
if (origparent != NULL)
{
char const * const origparentextension = Bstrrchr(origparent, '.');
uint32_t namelength = origparentextension != NULL ? origparentextension - origparent : origparentlength;
Bsprintf(testfn, "music/%.*s/%s", namelength, origparent, fn);
int32_t const fp = S_TryExtensionReplacements(testfn, searchfirst);
if (fp >= 0)
{
Bfree(testfn);
kclose(origfp);
return fp;
}
}
// look in ./music/
// ex: ./music/grabbag.mid
{
Bsprintf(testfn, "music/%s", fn);
int32_t const fp = S_TryExtensionReplacements(testfn, searchfirst);
if (fp >= 0)
{
Bfree(testfn);
kclose(origfp);
return fp;
}
}
Bfree(testfn);
return origfp;
} }