Slightly simplify S_OpenAudio()...

git-svn-id: https://svn.eduke32.com/eduke32@7210 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2018-11-18 18:12:11 +00:00
parent 871207294e
commit bb8e441d0d
1 changed files with 22 additions and 33 deletions

View File

@ -1120,58 +1120,47 @@ static int32_t S_TryExtensionReplacements(char * const testfn, char const search
int32_t S_OpenAudio(const char *fn, char searchfirst, uint8_t const ismusic) int32_t S_OpenAudio(const char *fn, char searchfirst, uint8_t const ismusic)
{ {
int32_t const origfp = kopen4loadfrommod(fn, searchfirst); int32_t const origfp = kopen4loadfrommod(fn, searchfirst);
char const * const origparent = origfp != -1 ? kfileparent(origfp) : NULL; char const *const origparent = origfp != -1 ? kfileparent(origfp) : NULL;
uint32_t const origparentlength = origparent != NULL ? Bstrlen(origparent) : 0; uint32_t const parentlength = origparent != NULL ? Bstrlen(origparent) : 0;
char * const testfn = (char *)Xmalloc(Bstrlen(fn) + 12 + origparentlength); // "music/" + overestimation of parent minus extension + ".flac" + '\0' auto testfn = (char *)Xmalloc(Bstrlen(fn) + 12 + parentlength); // "music/" + overestimation of parent minus extension + ".flac" + '\0'
// look in ./ // look in ./
// ex: ./grabbag.mid // ex: ./grabbag.mid
{ Bstrcpy(testfn, fn);
Bstrcpy(testfn, fn); int32_t fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic);
int32_t const fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); if (fp >= 0)
if (fp >= 0) goto success;
{
Bfree(testfn);
kclose(origfp);
return fp;
}
}
// look in ./music/<file's parent GRP name>/ // look in ./music/<file's parent GRP name>/
// ex: ./music/duke3d/grabbag.mid // ex: ./music/duke3d/grabbag.mid
// ex: ./music/nwinter/grabbag.mid // ex: ./music/nwinter/grabbag.mid
if (origparent != NULL) if (origparent != NULL)
{ {
char const * const origparentextension = Bstrrchr(origparent, '.'); char const * const parentextension = Bstrrchr(origparent, '.');
uint32_t namelength = origparentextension != NULL ? (unsigned)(origparentextension - origparent) : origparentlength; uint32_t const namelength = parentextension != NULL ? (unsigned)(parentextension - origparent) : parentlength;
Bsprintf(testfn, "music/%.*s/%s", namelength, origparent, fn); Bsprintf(testfn, "music/%.*s/%s", namelength, origparent, fn);
int32_t const fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic);
if (fp >= 0) if (fp >= 0)
{ goto success;
Bfree(testfn);
kclose(origfp);
return fp;
}
} }
// look in ./music/ // look in ./music/
// ex: ./music/grabbag.mid // ex: ./music/grabbag.mid
{ Bsprintf(testfn, "music/%s", fn);
Bsprintf(testfn, "music/%s", fn); fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic);
int32_t const fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); if (fp >= 0)
if (fp >= 0) goto success;
{
Bfree(testfn);
kclose(origfp);
return fp;
}
}
fp = origfp;
success:
Bfree(testfn); Bfree(testfn);
return origfp; if (fp != origfp)
kclose(origfp);
return fp;
} }
void Duke_CommonCleanup(void) void Duke_CommonCleanup(void)