From 6e7db1b63a10b8ab7bfcbc95252ec4385b321870 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 20 Oct 2019 21:56:13 +0200 Subject: [PATCH] - refactored file access in RR frontend's sound code. --- source/build/include/cache1d.h | 10 +++++- source/rr/src/common.cpp | 57 ++++++++++++++++------------------ source/rr/src/common_game.h | 2 +- source/rr/src/game.cpp | 9 ++---- source/rr/src/gamedef.cpp | 25 +++++++-------- source/rr/src/net.cpp | 6 ++-- source/rr/src/osdcmds.cpp | 3 +- source/rr/src/premap.cpp | 3 +- source/rr/src/sounds.cpp | 21 +++++-------- 9 files changed, 63 insertions(+), 73 deletions(-) diff --git a/source/build/include/cache1d.h b/source/build/include/cache1d.h index 940ee615c..d39db1ed2 100644 --- a/source/build/include/cache1d.h +++ b/source/build/include/cache1d.h @@ -132,7 +132,9 @@ public: virtual char* Gets(char* strbuf, int len) { // Not supported by the underlying system, so we do not need it anyway. - return nullptr; + // Gross hack alert: Abuse this function to return the container's name until we have a better resource management in place. + // Right now there is no way to cleanly pass this through and this function is the most convenient workaround. + return (char*)kfileparent(khandle); } }; @@ -177,5 +179,11 @@ inline int32_t kfilesize(const char* name, int where) return -1; } +inline char const* kfileparent(FileReader &fr) +{ + // This is by no means a good implementation. Its only advantage is that it can be done without hacking in something more invasive. + return fr.Gets(nullptr, 0); +} + #endif // cache1d_h_ diff --git a/source/rr/src/common.cpp b/source/rr/src/common.cpp index 0fa290b54..cf0e11b1a 100644 --- a/source/rr/src/common.cpp +++ b/source/rr/src/common.cpp @@ -1077,13 +1077,13 @@ void G_LoadLookups(void) #ifdef FORMAT_UPGRADE_ELIGIBLE -static int32_t S_TryFormats(char * const testfn, char * const fn_suffix, char const searchfirst) +static FileReader S_TryFormats(char * const testfn, char * const fn_suffix, char const searchfirst) { #ifdef HAVE_FLAC { Bstrcpy(fn_suffix, ".flac"); - int32_t const fp = kopen4loadfrommod(testfn, searchfirst); - if (fp >= 0) + auto fp = kopenFileReader(testfn, searchfirst); + if (fp.isOpen()) return fp; } #endif @@ -1091,16 +1091,16 @@ static int32_t S_TryFormats(char * const testfn, char * const fn_suffix, char co #ifdef HAVE_VORBIS { Bstrcpy(fn_suffix, ".ogg"); - int32_t const fp = kopen4loadfrommod(testfn, searchfirst); - if (fp >= 0) - return fp; - } + auto fp = kopenFileReader(testfn, searchfirst); + if (fp.isOpen()) + return fp; + } #endif - return -1; + return FileReader(); } -static int32_t S_TryExtensionReplacements(char * const testfn, char const searchfirst, uint8_t const ismusic) +static FileReader S_TryExtensionReplacements(char * const testfn, char const searchfirst, uint8_t const ismusic) { char * extension = Bstrrchr(testfn, '.'); char * const fn_end = Bstrchr(testfn, '\0'); @@ -1110,8 +1110,8 @@ static int32_t S_TryExtensionReplacements(char * const testfn, char const search { *extension = '_'; - int32_t const fp = S_TryFormats(testfn, fn_end, searchfirst); - if (fp >= 0) + auto fp = S_TryFormats(testfn, fn_end, searchfirst); + if (fp.isOpen()) return fp; } else @@ -1122,18 +1122,18 @@ static int32_t S_TryExtensionReplacements(char * const testfn, char const search // ex: grabbag.mid --> grabbag.* if (ismusic) { - int32_t const fp = S_TryFormats(testfn, extension, searchfirst); - if (fp >= 0) - return fp; + auto fp = S_TryFormats(testfn, extension, searchfirst); + if (fp.isOpen()) + return fp; } - return -1; + return FileReader(); } -int32_t S_OpenAudio(const char *fn, char searchfirst, uint8_t const ismusic) +FileReader S_OpenAudio(const char *fn, char searchfirst, uint8_t const ismusic) { - int32_t const origfp = kopen4loadfrommod(fn, searchfirst); - char const * const origparent = origfp != -1 ? kfileparent(origfp) : NULL; + auto origfp = kopenFileReader(fn, searchfirst); + char const * const origparent = origfp.isOpen() ? kfileparent(origfp) : NULL; uint32_t const origparentlength = origparent != NULL ? Bstrlen(origparent) : 0; char * const testfn = (char *)Xmalloc(Bstrlen(fn) + 12 + origparentlength); // "music/" + overestimation of parent minus extension + ".flac" + '\0' @@ -1142,11 +1142,10 @@ int32_t S_OpenAudio(const char *fn, char searchfirst, uint8_t const ismusic) // ex: ./grabbag.mid { Bstrcpy(testfn, fn); - int32_t const fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); - if (fp >= 0) - { + auto fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); + if (fp.isOpen()) + { Bfree(testfn); - kclose(origfp); return fp; } } @@ -1160,11 +1159,10 @@ int32_t S_OpenAudio(const char *fn, char searchfirst, uint8_t const ismusic) uint32_t namelength = origparentextension != NULL ? (unsigned)(origparentextension - origparent) : origparentlength; Bsprintf(testfn, "music/%.*s/%s", namelength, origparent, fn); - int32_t const fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); - if (fp >= 0) - { + auto fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); + if (fp.isOpen()) + { Bfree(testfn); - kclose(origfp); return fp; } } @@ -1173,11 +1171,10 @@ int32_t S_OpenAudio(const char *fn, char searchfirst, uint8_t const ismusic) // ex: ./music/grabbag.mid { Bsprintf(testfn, "music/%s", fn); - int32_t const fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); - if (fp >= 0) - { + auto fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); + if (fp.isOpen()) + { Bfree(testfn); - kclose(origfp); return fp; } } diff --git a/source/rr/src/common_game.h b/source/rr/src/common_game.h index d0216484a..2d2a60ee7 100644 --- a/source/rr/src/common_game.h +++ b/source/rr/src/common_game.h @@ -149,7 +149,7 @@ extern void G_LoadLookups(void); ////////// # define FORMAT_UPGRADE_ELIGIBLE -extern int32_t S_OpenAudio(const char *fn, char searchfirst, uint8_t ismusic); +extern FileReader S_OpenAudio(const char *fn, char searchfirst, uint8_t ismusic); void G_AddGroup(const char* buffer); void G_AddPath(const char* buffer); diff --git a/source/rr/src/game.cpp b/source/rr/src/game.cpp index 75daafb09..21787ccb8 100644 --- a/source/rr/src/game.cpp +++ b/source/rr/src/game.cpp @@ -7485,11 +7485,9 @@ static void G_Startup(void) Bcorrectfilename(boardfilename,0); - i = kopen4loadfrommod(boardfilename,0); - if (i!=-1) + if (testkopen(boardfilename, 0)) { initprintf("Using level: \"%s\".\n",boardfilename); - kclose(i); } else { @@ -7834,12 +7832,9 @@ int app_main(int argc, char const * const * argv) g_Shareware = 1; else { - int const kFile = kopen4load("DUKESW.BIN",1); // JBF 20030810 - - if (kFile != -1) + if (testkopen("DUKESW.BIN",1)) // JBF 20030810 { g_Shareware = 1; - kclose(kFile); } } diff --git a/source/rr/src/gamedef.cpp b/source/rr/src/gamedef.cpp index 7d1e5995e..513ec7f65 100644 --- a/source/rr/src/gamedef.cpp +++ b/source/rr/src/gamedef.cpp @@ -776,23 +776,23 @@ static int32_t C_CheckEmptyBranch(int32_t tw, intptr_t lastScriptPtr) static void C_Include(const char *confile) { - int32_t fp = kopen4loadfrommod(confile,g_loadFromGroupOnly); + auto fp = kopenFileReader(confile,g_loadFromGroupOnly); - if (EDUKE32_PREDICT_FALSE(fp < 0)) + if (!fp.isOpen()) { g_errorCnt++; initprintf("%s:%d: error: could not find file `%s'.\n",g_scriptFileName,g_lineNumber,confile); return; } - int32_t j = kfilelength(fp); + int32_t j = fp.GetLength(); char *mptr = (char *)Xmalloc(j+1); initprintf("Including: %s (%d bytes)\n",confile, j); - kread(fp, mptr, j); - kclose(fp); + fp.Read(mptr, j); + fp.Close(); g_scriptcrc = Bcrc32(mptr, j, g_scriptcrc); mptr[j] = 0; @@ -830,9 +830,8 @@ static void C_Include(const char *confile) #ifdef _WIN32 static void check_filename_case(const char *fn) { - int32_t fp; - if ((fp = kopen4loadfrommod(fn, g_loadFromGroupOnly)) >= 0) - kclose(fp); + // WTF?!? + testkopen(fn, g_loadFromGroupOnly); } #else static void check_filename_case(const char *fn) { UNREFERENCED_PARAMETER(fn); } @@ -2374,9 +2373,9 @@ void C_Compile(const char *fileName) C_InitHashes(); - int kFile = kopen4loadfrommod(fileName,g_loadFromGroupOnly); + auto kFile = kopenFileReader(fileName,g_loadFromGroupOnly); - if (kFile == -1) // JBF: was 0 + if (!kFile.isOpen()) { if (g_loadFromGroupOnly == 1 || numgroupfiles == 0) { @@ -2395,7 +2394,7 @@ void C_Compile(const char *fileName) return; //Not there } - int const kFileLen = kfilelength(kFile); + int const kFileLen = kFile.GetLength(); initprintf("Compiling: %s (%d bytes)\n", fileName, kFileLen); @@ -2407,8 +2406,8 @@ void C_Compile(const char *fileName) mptr[kFileLen] = 0; textptr = (char *) mptr; - kread(kFile,(char *)textptr,kFileLen); - kclose(kFile); + kFile.Read((char *)textptr,kFileLen); + kFile.Close(); g_scriptcrc = Bcrc32(NULL, 0, 0L); g_scriptcrc = Bcrc32(textptr, kFileLen, g_scriptcrc); diff --git a/source/rr/src/net.cpp b/source/rr/src/net.cpp index feddf72d3..f607176e0 100644 --- a/source/rr/src/net.cpp +++ b/source/rr/src/net.cpp @@ -2631,12 +2631,11 @@ void Net_ParsePacket(uint8_t *packbuf, int packbufleng) Bcorrectfilename(boardfilename,0); if (boardfilename[0] != 0) { - if ((i = kopen4loadfrommod(boardfilename,0)) < 0) + if (testkopen(boardfilename,0)) { Bmemset(boardfilename,0,sizeof(boardfilename)); Net_SendUserMapName(); } - else kclose(i); } if (ud.m_level_number == 7 && ud.m_volume_number == 0 && boardfilename[0] == 0) @@ -3563,12 +3562,11 @@ void Net_ReceiveUserMapName(uint8_t *pbuf, int32_t packbufleng) Bcorrectfilename(boardfilename,0); if (boardfilename[0] != 0) { - if ((i = kopen4loadfrommod(boardfilename,0)) < 0) + if (testkopen(boardfilename,0)) { Bmemset(boardfilename,0,sizeof(boardfilename)); Net_SendUserMapName(); } - else kclose(i); } if (ud.m_level_number == 7 && ud.m_volume_number == 0 && boardfilename[0] == 0) diff --git a/source/rr/src/osdcmds.cpp b/source/rr/src/osdcmds.cpp index e91a9c786..8696e3a43 100644 --- a/source/rr/src/osdcmds.cpp +++ b/source/rr/src/osdcmds.cpp @@ -205,12 +205,11 @@ static int osdcmd_map(osdcmdptr_t parm) maybe_append_ext(filename, sizeof(filename), parm->parms[0], ".map"); - if ((i = kopen4loadfrommod(filename,0)) < 0) + if (!testkopen(filename,0)) { OSD_Printf(OSD_ERROR "map: file \"%s\" not found.\n", filename); return OSDCMD_OK; } - kclose(i); boardfilename[0] = '/'; boardfilename[1] = 0; diff --git a/source/rr/src/premap.cpp b/source/rr/src/premap.cpp index 5e3f6fcd7..be175b145 100644 --- a/source/rr/src/premap.cpp +++ b/source/rr/src/premap.cpp @@ -2278,9 +2278,8 @@ void G_SetupFilenameBasedMusic(char *nameBuf, const char *fileName, int levelNum Bmemcpy(p+1, ext, Bstrlen(ext) + 1); - if ((kFile = kopen4loadfrommod(nameBuf, 0)) != -1) + if (testkopen(nameBuf, 0)) { - kclose(kFile); realloc_copy(&g_mapInfo[levelNum].musicfn, nameBuf); return; } diff --git a/source/rr/src/sounds.cpp b/source/rr/src/sounds.cpp index 80e837e95..398c88402 100644 --- a/source/rr/src/sounds.cpp +++ b/source/rr/src/sounds.cpp @@ -193,36 +193,32 @@ static int S_PlayMusic(const char *fn, int loop) if (fn == NULL) return 1; - int32_t fp = S_OpenAudio(fn, 0, 1); - if (EDUKE32_PREDICT_FALSE(fp < 0)) + auto fp = S_OpenAudio(fn, 0, 1); + if (!fp.isOpen()) { OSD_Printf(OSD_ERROR "S_PlayMusic(): error: can't open \"%s\" for playback!\n",fn); return 2; } - int32_t MusicLen = kfilelength(fp); + int32_t MusicLen = fp.GetLength(); if (EDUKE32_PREDICT_FALSE(MusicLen < 4)) { OSD_Printf(OSD_ERROR "S_PlayMusic(): error: empty music file \"%s\"\n", fn); - kclose(fp); return 3; } char * MyMusicPtr = (char *)Xaligned_alloc(16, MusicLen); - int MyMusicSize = kread(fp, MyMusicPtr, MusicLen); + int MyMusicSize = fp.Read(MyMusicPtr, MusicLen); if (EDUKE32_PREDICT_FALSE(MyMusicSize != MusicLen)) { OSD_Printf(OSD_ERROR "S_PlayMusic(): error: read %d bytes from \"%s\", expected %d\n", MyMusicSize, fn, MusicLen); - kclose(fp); ALIGNED_FREE_AND_NULL(MyMusicPtr); return 4; } - kclose(fp); - if (!Bmemcmp(MyMusicPtr, "MThd", 4)) { int32_t retval = MUSIC_PlaySong(MyMusicPtr, MyMusicSize, loop); @@ -437,20 +433,19 @@ int32_t S_LoadSound(int num) auto &snd = g_sounds[num]; - int32_t fp = S_OpenAudio(snd.filename, g_loadFromGroupOnly, 0); + auto fp = S_OpenAudio(snd.filename, g_loadFromGroupOnly, 0); - if (EDUKE32_PREDICT_FALSE(fp == -1)) + if (!fp.isOpen()) { OSD_Printf(OSDTEXT_RED "Sound %s(#%d) not found!\n", snd.filename, num); return 0; } - int32_t l = kfilelength(fp); + int32_t l = fp.Tell(); g_soundlocks[num] = 200; snd.siz = l; cacheAllocateBlock((intptr_t *)&snd.ptr, l, &g_soundlocks[num]); - l = kread(fp, snd.ptr, l); - kclose(fp); + l = fp.Read(snd.ptr, l); return l; }