From 5022c58a63b5b465338687b768b32e16d79d4cca Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 20 Oct 2019 23:37:07 +0200 Subject: [PATCH] - refactored the file access in the rest of the DN3D frontend and in some duplicated code in the Blood frontend. --- source/blood/src/common.cpp | 84 +++++++++++--------- source/blood/src/common_game.h | 2 +- source/blood/src/sound.cpp | 12 +-- source/common/utility/files.h | 7 ++ source/duke3d/src/common.cpp | 97 ++++++++++++----------- source/duke3d/src/common_game.h | 2 +- source/duke3d/src/game.cpp | 19 ++--- source/duke3d/src/gamedef.cpp | 25 +++--- source/duke3d/src/gameexec.cpp | 12 +-- source/duke3d/src/network.cpp | 7 +- source/duke3d/src/osdcmds.cpp | 4 +- source/duke3d/src/premap.cpp | 5 +- source/duke3d/src/savegame.cpp | 36 +++------ source/duke3d/src/screens.cpp | 12 +-- source/duke3d/src/sounds.cpp | 21 ++--- source/libsmackerdec/include/FileStream.h | 2 +- source/libsmackerdec/src/FileStream.cpp | 37 +++------ source/rr/src/premap.cpp | 7 ++ 18 files changed, 181 insertions(+), 210 deletions(-) diff --git a/source/blood/src/common.cpp b/source/blood/src/common.cpp index d7ec42738..3fa72fcac 100644 --- a/source/blood/src/common.cpp +++ b/source/blood/src/common.cpp @@ -677,13 +677,13 @@ void G_DoAutoload(const char *dirname) #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 @@ -691,16 +691,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'); @@ -710,8 +710,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 @@ -722,28 +722,31 @@ 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; - uint32_t const parentlength = origparent != NULL ? Bstrlen(origparent) : 0; + auto origfp = kopenFileReader(fn, searchfirst); + char const* const origparent = origfp.isOpen() ? kfileparent(origfp) : NULL; + uint32_t const parentlength = origparent != NULL ? Bstrlen(origparent) : 0; auto testfn = (char *)Xmalloc(Bstrlen(fn) + 12 + parentlength); // "music/" + overestimation of parent minus extension + ".flac" + '\0' // look in ./ // ex: ./grabbag.mid Bstrcpy(testfn, fn); - int32_t fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); - if (fp >= 0) - goto success; + auto fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); + if (fp.isOpen()) + { + Bfree(testfn); + return fp; + } // look in ./music// // ex: ./music/duke3d/grabbag.mid @@ -753,26 +756,29 @@ int32_t S_OpenAudio(const char *fn, char searchfirst, uint8_t const ismusic) char const * const parentextension = Bstrrchr(origparent, '.'); uint32_t const namelength = parentextension != NULL ? (unsigned)(parentextension - origparent) : parentlength; - Bsprintf(testfn, "music/%.*s/%s", namelength, origparent, fn); - fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); - if (fp >= 0) - goto success; + Bsprintf(testfn, "music/%.*s/%s", namelength, origparent, fn); + auto fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); + if (fp.isOpen()) + { + Bfree(testfn); + return fp; + } } - // look in ./music/ - // ex: ./music/grabbag.mid - Bsprintf(testfn, "music/%s", fn); - fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); - if (fp >= 0) - goto success; + // look in ./music/ + // ex: ./music/grabbag.mid + { + Bsprintf(testfn, "music/%s", fn); + auto fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); + if (fp.isOpen()) + { + Bfree(testfn); + return fp; + } + } - fp = origfp; -success: - Bfree(testfn); - if (fp != origfp) - kclose(origfp); - - return fp; + Bfree(testfn); + return origfp; } #endif diff --git a/source/blood/src/common_game.h b/source/blood/src/common_game.h index 4dc5f38c5..eb130b1b4 100644 --- a/source/blood/src/common_game.h +++ b/source/blood/src/common_game.h @@ -541,7 +541,7 @@ static inline void G_HandleAsync(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); #pragma pack(push,1) diff --git a/source/blood/src/sound.cpp b/source/blood/src/sound.cpp index 8f5a0e0ac..1a9f77348 100644 --- a/source/blood/src/sound.cpp +++ b/source/blood/src/sound.cpp @@ -98,8 +98,8 @@ int sndPlaySong(const char *songName, bool bLoop) if (!songName || strlen(songName) == 0) return 1; - int32_t fp = S_OpenAudio(songName, 0, 1); - if (EDUKE32_PREDICT_FALSE(fp < 0)) + auto fp = S_OpenAudio(songName, 0, 1); + if (!fp.isOpen()) { hSong = gSoundRes.Lookup(songName, "MID"); if (!hSong) @@ -132,29 +132,25 @@ int sndPlaySong(const char *songName, bool bLoop) return 0; } - int32_t nSongLen = kfilelength(fp); + int32_t nSongLen = fp.Tell(); if (EDUKE32_PREDICT_FALSE(nSongLen < 4)) { OSD_Printf(OSD_ERROR "sndPlaySong(): error: empty music file \"%s\"\n", songName); - kclose(fp); return 3; } char * pNewSongPtr = (char *)Xaligned_alloc(16, nSongLen); - int nNewSongSize = kread(fp, pNewSongPtr, nSongLen); + int nNewSongSize = fp.Read(pNewSongPtr, nSongLen); if (EDUKE32_PREDICT_FALSE(nNewSongSize != nSongLen)) { OSD_Printf(OSD_ERROR "sndPlaySong(): error: read %d bytes from \"%s\", expected %d\n", nNewSongSize, songName, nSongLen); - kclose(fp); ALIGNED_FREE_AND_NULL(pNewSongPtr); return 4; } - kclose(fp); - if (!Bmemcmp(pNewSongPtr, "MThd", 4)) { int32_t retval = MUSIC_PlaySong(pNewSongPtr, nNewSongSize, bLoop); diff --git a/source/common/utility/files.h b/source/common/utility/files.h index 488232797..525cd0a09 100644 --- a/source/common/utility/files.h +++ b/source/common/utility/files.h @@ -262,6 +262,13 @@ public: return LittleShort(v); } + int16_t ReadInt16BE() + { + uint16_t v = 0; + Read(&v, 2); + return BigShort(v); + } + uint32_t ReadUInt32() { uint32_t v = 0; diff --git a/source/duke3d/src/common.cpp b/source/duke3d/src/common.cpp index ebb7c394f..115af03e0 100644 --- a/source/duke3d/src/common.cpp +++ b/source/duke3d/src/common.cpp @@ -1059,29 +1059,30 @@ void G_LoadLookups(void) #ifdef FORMAT_UPGRADE_ELIGIBLE int g_maybeUpgradeSoundFormats = 1; -static buildvfs_kfd 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) { - if (g_maybeUpgradeSoundFormats) - { #ifdef HAVE_FLAC + { Bstrcpy(fn_suffix, ".flac"); - buildvfs_kfd const ffp = kopen4loadfrommod(testfn, searchfirst); - if (ffp != buildvfs_kfd_invalid) - return ffp; + auto fp = kopenFileReader(testfn, searchfirst); + if (fp.isOpen()) + return fp; + } #endif #ifdef HAVE_VORBIS + { Bstrcpy(fn_suffix, ".ogg"); - buildvfs_kfd const fp = kopen4loadfrommod(testfn, searchfirst); - if (fp != buildvfs_kfd_invalid) - return fp; + auto fp = kopenFileReader(testfn, searchfirst); + if (fp.isOpen()) + return fp; + } #endif - } - return buildvfs_kfd_invalid; + return FileReader(); } -static buildvfs_kfd 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'); @@ -1091,8 +1092,8 @@ static buildvfs_kfd S_TryExtensionReplacements(char * const testfn, char const s { *extension = '_'; - buildvfs_kfd const fp = S_TryFormats(testfn, fn_end, searchfirst); - if (fp != buildvfs_kfd_invalid) + auto fp = S_TryFormats(testfn, fn_end, searchfirst); + if (fp.isOpen()) return fp; } else @@ -1103,34 +1104,32 @@ static buildvfs_kfd S_TryExtensionReplacements(char * const testfn, char const s // ex: grabbag.mid --> grabbag.* if (ismusic) { - buildvfs_kfd const fp = S_TryFormats(testfn, extension, searchfirst); - if (fp != buildvfs_kfd_invalid) - return fp; + auto fp = S_TryFormats(testfn, extension, searchfirst); + if (fp.isOpen()) + return fp; } - return buildvfs_kfd_invalid; + return FileReader(); } -buildvfs_kfd S_OpenAudio(const char *fn, char searchfirst, uint8_t const ismusic) +FileReader S_OpenAudio(const char *fn, char searchfirst, uint8_t const ismusic) { - buildvfs_kfd const origfp = kopen4loadfrommod(fn, searchfirst); -#ifndef USE_PHYSFS - char const * const origparent = origfp != buildvfs_kfd_invalid ? kfileparent(origfp) : NULL; - uint32_t const parentlength = origparent != NULL ? Bstrlen(origparent) : 0; + auto origfp = kopenFileReader(fn, searchfirst); + char const* const origparent = origfp.isOpen() ? kfileparent(origfp) : NULL; + uint32_t const parentlength = origparent != NULL ? Bstrlen(origparent) : 0; auto testfn = (char *)Xmalloc(Bstrlen(fn) + 12 + parentlength); // "music/" + overestimation of parent minus extension + ".flac" + '\0' -#else - auto testfn = (char *)Xmalloc(Bstrlen(fn) + 12); -#endif // look in ./ // ex: ./grabbag.mid Bstrcpy(testfn, fn); - buildvfs_kfd fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); - if (fp != buildvfs_kfd_invalid) - goto success; + auto fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); + if (fp.isOpen()) + { + Bfree(testfn); + return fp; + } -#ifndef USE_PHYSFS // look in ./music// // ex: ./music/duke3d/grabbag.mid // ex: ./music/nwinter/grabbag.mid @@ -1139,27 +1138,29 @@ buildvfs_kfd S_OpenAudio(const char *fn, char searchfirst, uint8_t const ismusic char const * const parentextension = Bstrrchr(origparent, '.'); uint32_t const namelength = parentextension != NULL ? (unsigned)(parentextension - origparent) : parentlength; - Bsprintf(testfn, "music/%.*s/%s", namelength, origparent, fn); - fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); - if (fp != buildvfs_kfd_invalid) - goto success; + Bsprintf(testfn, "music/%.*s/%s", namelength, origparent, fn); + auto fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); + if (fp.isOpen()) + { + Bfree(testfn); + return fp; + } } - // look in ./music/ - // ex: ./music/grabbag.mid - Bsprintf(testfn, "music/%s", fn); - fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); - if (fp != buildvfs_kfd_invalid) - goto success; -#endif + // look in ./music/ + // ex: ./music/grabbag.mid + { + Bsprintf(testfn, "music/%s", fn); + auto fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); + if (fp.isOpen()) + { + Bfree(testfn); + return fp; + } + } - Xfree(testfn); - return origfp; - -success: - Xfree(testfn); - kclose(origfp); - return fp; + Bfree(testfn); + return origfp; } void Duke_CommonCleanup(void) diff --git a/source/duke3d/src/common_game.h b/source/duke3d/src/common_game.h index fab04137a..89483f448 100644 --- a/source/duke3d/src/common_game.h +++ b/source/duke3d/src/common_game.h @@ -142,7 +142,7 @@ extern void G_LoadLookups(void); # define FORMAT_UPGRADE_ELIGIBLE extern int g_maybeUpgradeSoundFormats; -extern buildvfs_kfd 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/duke3d/src/game.cpp b/source/duke3d/src/game.cpp index df1bcc4ee..72c4198c5 100644 --- a/source/duke3d/src/game.cpp +++ b/source/duke3d/src/game.cpp @@ -6096,11 +6096,9 @@ static void G_Startup(void) Bcorrectfilename(boardfilename,0); - buildvfs_kfd ii = kopen4loadfrommod(boardfilename, 0); - if (ii != buildvfs_kfd_invalid) - { + if (testkopen(boardfilename, 0)) + { initprintf("Using level: \"%s\".\n",boardfilename); - kclose(ii); } else { @@ -6461,14 +6459,11 @@ int app_main(int argc, char const * const * argv) g_Shareware = 1; else { - buildvfs_kfd const kFile = kopen4load("DUKESW.BIN",1); // JBF 20030810 - - if (kFile != buildvfs_kfd_invalid) - { - g_Shareware = 1; - kclose(kFile); - } - } + if (testkopen("DUKESW.BIN", 1)) // JBF 20030810 + { + g_Shareware = 1; + } + } #endif // gotta set the proper title after we compile the CONs if this is the full version diff --git a/source/duke3d/src/gamedef.cpp b/source/duke3d/src/gamedef.cpp index 9d835cf61..05629f30d 100644 --- a/source/duke3d/src/gamedef.cpp +++ b/source/duke3d/src/gamedef.cpp @@ -1901,22 +1901,22 @@ static int C_CountCaseStatements() static void C_Include(const char *confile) { - buildvfs_kfd fp = kopen4loadfrommod(confile, g_loadFromGroupOnly); + auto fp = kopenFileReader(confile,g_loadFromGroupOnly); - if (EDUKE32_PREDICT_FALSE(fp == buildvfs_kfd_invalid)) + if (!fp.isOpen()) { g_errorCnt++; initprintf("%s:%d: error: could not find file `%s'.\n",g_scriptFileName,g_lineNumber,confile); return; } - int32_t const len = kfilelength(fp); + int32_t const len = fp.GetLength(); char *mptr = (char *)Xmalloc(len+1); initprintf("Including: %s (%d bytes)\n",confile, len); - kread(fp, mptr, len); - kclose(fp); + fp.Read(mptr, len); + fp.Close(); mptr[len] = 0; g_scriptcrc = Bcrc32(mptr, len, g_scriptcrc); @@ -1956,9 +1956,8 @@ static void C_Include(const char *confile) #ifdef _WIN32 static void check_filename_case(const char *fn) { - buildvfs_kfd fp; - if ((fp = kopen4loadfrommod(fn, g_loadFromGroupOnly)) != buildvfs_kfd_invalid) - kclose(fp); + // WTF?!? + testkopen(fn, g_loadFromGroupOnly); } #else static void check_filename_case(const char *fn) { UNREFERENCED_PARAMETER(fn); } @@ -6261,9 +6260,9 @@ void C_Compile(const char *fileName) Gv_Init(); C_InitProjectiles(); - buildvfs_kfd kFile = kopen4loadfrommod(fileName, g_loadFromGroupOnly); + auto kFile = kopenFileReader(fileName,g_loadFromGroupOnly); - if (kFile == buildvfs_kfd_invalid) // JBF: was 0 + if (!kFile.isOpen()) { if (g_loadFromGroupOnly == 1 || numgroupfiles == 0) { @@ -6286,7 +6285,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); @@ -6298,8 +6297,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/duke3d/src/gameexec.cpp b/source/duke3d/src/gameexec.cpp index 993f1f941..8937621c0 100644 --- a/source/duke3d/src/gameexec.cpp +++ b/source/duke3d/src/gameexec.cpp @@ -5647,12 +5647,12 @@ badindex: VM_ASSERT((unsigned)quoteFilename < MAXQUOTES && apStrings[quoteFilename], "invalid quote %d\n", quoteFilename); - buildvfs_kfd kFile = kopen4loadfrommod(apStrings[quoteFilename], 0); + auto kFile = kopenFileReader(apStrings[quoteFilename], 0); - if (kFile == buildvfs_kfd_invalid) + if (!kFile.isOpen()) dispatch(); - size_t const filelength = kfilelength(kFile); + size_t const filelength = kFile.GetLength(); size_t const numElements = Gv_GetArrayCountForAllocSize(arrayNum, filelength); if (numElements > 0) @@ -5680,7 +5680,7 @@ badindex: { void *const pArray = Xcalloc(1, newBytes); - kread(kFile, pArray, readBytes); + kFile.Read(pArray, readBytes); if (flags & GAMEARRAY_UNSIGNED) { @@ -5699,12 +5699,12 @@ badindex: #endif default: memset((char *)pValues + readBytes, 0, newBytes - readBytes); - kread(kFile, pValues, readBytes); + kFile.Read(pValues, readBytes); break; } } - kclose(kFile); + kFile.Close(); dispatch(); } diff --git a/source/duke3d/src/network.cpp b/source/duke3d/src/network.cpp index 55254ea50..e08bdf303 100644 --- a/source/duke3d/src/network.cpp +++ b/source/duke3d/src/network.cpp @@ -1988,16 +1988,11 @@ static void Net_ReceiveUserMapName(uint8_t *pbuf, int32_t packbufleng) Bcorrectfilename(boardfilename, 0); if (boardfilename[0] != 0) { - buildvfs_kfd i; - if ((i = kopen4loadfrommod(boardfilename, 0)) == buildvfs_kfd_invalid) + 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/duke3d/src/osdcmds.cpp b/source/duke3d/src/osdcmds.cpp index de5bd1275..5b3d4f548 100644 --- a/source/duke3d/src/osdcmds.cpp +++ b/source/duke3d/src/osdcmds.cpp @@ -209,13 +209,11 @@ static int osdcmd_map(osdcmdptr_t parm) maybe_append_ext(filename, sizeof(filename), parm->parms[0], ".map"); - buildvfs_kfd ii; - if ((ii = kopen4loadfrommod(filename,0)) == buildvfs_kfd_invalid) + if (!testkopen(filename,0)) { OSD_Printf(OSD_ERROR "map: file \"%s\" not found.\n", filename); return OSDCMD_OK; } - kclose(ii); boardfilename[0] = '/'; boardfilename[1] = 0; diff --git a/source/duke3d/src/premap.cpp b/source/duke3d/src/premap.cpp index c501bb1f6..6a855e8b8 100644 --- a/source/duke3d/src/premap.cpp +++ b/source/duke3d/src/premap.cpp @@ -1728,9 +1728,8 @@ void G_SetupFilenameBasedMusic(char *nameBuf, const char *fileName) Bmemcpy(p+1, ext, Bstrlen(ext) + 1); - if ((kFile = kopen4loadfrommod(nameBuf, 0)) != buildvfs_kfd_invalid) - { - kclose(kFile); + if (testkopen(nameBuf, 0)) + { realloc_copy(&g_mapInfo[USERMAPMUSICFAKESLOT].musicfn, nameBuf); return; } diff --git a/source/duke3d/src/savegame.cpp b/source/duke3d/src/savegame.cpp index 7ecaee989..a62cf84dc 100644 --- a/source/duke3d/src/savegame.cpp +++ b/source/duke3d/src/savegame.cpp @@ -173,14 +173,12 @@ static void ReadSaveGameHeaders_CACHE1D(CACHE1D_FIND_REC *f) { if (FURY) { - char extfn[BMAX_PATH]; - snprintf(extfn, ARRAY_SIZE(extfn), "%s.ext", fn); - buildvfs_kfd extfil = kopen4loadfrommod(extfn, 0); - if (extfil != buildvfs_kfd_invalid) - { - msv.brief.isExt = 1; - kclose(extfil); - } + FStringf extfn("%s.ext", fn); + auto extfil = fopenFileReader(extfn, 0); + if (extfil.isOpen()) + { + msv.brief.isExt = 1; + } } } msv.isOldVer = 1; @@ -376,32 +374,22 @@ int32_t G_LoadPlayer(savebrief_t & sv) } } - char extfn[BMAX_PATH]; - snprintf(extfn, ARRAY_SIZE(extfn), "%s.ext", sv.path); - buildvfs_kfd extfil = kopen4loadfrommod(extfn, 0); - if (extfil == buildvfs_kfd_invalid) + FStringf extfn("%s.ext", sv.path); + auto extfil = fopenFileReader(extfn, 0); + if (!extfil.isOpen()) { return -1; } - int32_t len = kfilelength(extfil); - auto text = (char *)Xmalloc(len+1); - text[len] = '\0'; + auto text = extfil.ReadPadded(1); - if (kread_and_test(extfil, text, len)) + if (text.Size() == 0) { - kclose(extfil); - Xfree(text); return -1; } - kclose(extfil); - - sjson_context * ctx = sjson_create_context(0, 0, NULL); - sjson_node * root = sjson_decode(ctx, text); - - Xfree(text); + sjson_node * root = sjson_decode(ctx, (const char *)text.Data()); if (volume == -1) volume = sjson_get_int(root, "volume", volume); diff --git a/source/duke3d/src/screens.cpp b/source/duke3d/src/screens.cpp index 8ce467e9c..d0fc29451 100644 --- a/source/duke3d/src/screens.cpp +++ b/source/duke3d/src/screens.cpp @@ -1460,14 +1460,10 @@ void gameDisplay3DRScreen() buildvfs_kfd i; Net_GetPackets(); - i = kopen4loadfrommod("3dr.ivf", 0); - - if (i == buildvfs_kfd_invalid) - i = kopen4loadfrommod("3dr.anm", 0); - - if (i != buildvfs_kfd_invalid) - { - kclose(i); + if (testkopen("3dr.ivf", 0) || testkopen("3dr.anm", 0)) + { + Anim_Play("3dr.anm"); + kclose(i); Anim_Play("3dr.anm"); G_FadePalette(0, 0, 0, 252); I_ClearAllInput(); diff --git a/source/duke3d/src/sounds.cpp b/source/duke3d/src/sounds.cpp index bd0b73dd8..4eda9f73b 100644 --- a/source/duke3d/src/sounds.cpp +++ b/source/duke3d/src/sounds.cpp @@ -212,36 +212,32 @@ static int S_PlayMusic(const char *fn) if (fn == NULL) return 1; - buildvfs_kfd fp = S_OpenAudio(fn, 0, 1); - if (EDUKE32_PREDICT_FALSE(fp == buildvfs_kfd_invalid)) + 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, MUSIC_LoopSong); @@ -450,20 +446,19 @@ int32_t S_LoadSound(int num) auto &snd = g_sounds[num]; - buildvfs_kfd fp = S_OpenAudio(snd.filename, g_loadFromGroupOnly, 0); + auto fp = S_OpenAudio(snd.filename, g_loadFromGroupOnly, 0); - if (EDUKE32_PREDICT_FALSE(fp == buildvfs_kfd_invalid)) + 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.GetLength(); g_soundlocks[num] = 255; 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; } diff --git a/source/libsmackerdec/include/FileStream.h b/source/libsmackerdec/include/FileStream.h index 7fe408466..02ae8ee84 100644 --- a/source/libsmackerdec/include/FileStream.h +++ b/source/libsmackerdec/include/FileStream.h @@ -58,7 +58,7 @@ class FileStream bool Is_Eos(); private: - int file; + FileReader file; }; } // close namespace SmackerCommon diff --git a/source/libsmackerdec/src/FileStream.cpp b/source/libsmackerdec/src/FileStream.cpp index 414c68f4c..1cea5dbcd 100644 --- a/source/libsmackerdec/src/FileStream.cpp +++ b/source/libsmackerdec/src/FileStream.cpp @@ -24,8 +24,8 @@ namespace SmackerCommon { bool FileStream::Open(const std::string &fileName) { - file = kopen4loadfrommod(fileName.c_str(), 0); - if (file == -1) + file = kopenFileReader(fileName.c_str(), 0); + if (!file.isOpen()) { // log error return false; @@ -36,18 +36,17 @@ bool FileStream::Open(const std::string &fileName) bool FileStream::Is_Open() { - return file != -1; + return file.isOpen(); } void FileStream::Close() { - kclose(file); - file = -1; + file.Close(); } int32_t FileStream::ReadBytes(uint8_t *data, uint32_t nBytes) { - uint32_t nCount = (uint32_t)kread(file, data, static_cast(nBytes)); + uint32_t nCount = (uint32_t)file.Read(data, static_cast(nBytes)); if (nCount != nBytes) { @@ -59,47 +58,37 @@ int32_t FileStream::ReadBytes(uint8_t *data, uint32_t nBytes) uint32_t FileStream::ReadUint32LE() { - uint32_t value; - kread(file, &value, 4); - return B_LITTLE32(value); + return file.ReadInt32(); } uint32_t FileStream::ReadUint32BE() { - uint32_t value; - kread(file, &value, 4); - return B_BIG32(value); + return file.ReadInt32BE(); } uint16_t FileStream::ReadUint16LE() { - uint16_t value; - kread(file, &value, 2); - return B_LITTLE16(value); + return file.ReadInt16(); } uint16_t FileStream::ReadUint16BE() { - uint16_t value; - kread(file, &value, 2); - return B_BIG16(value); + return file.ReadInt16BE(); } uint8_t FileStream::ReadByte() { - uint8_t value; - kread(file, &value, 1); - return value; + return file.ReadInt8(); } bool FileStream::Seek(int32_t offset, SeekDirection direction) { int32_t nStatus = -1; if (kSeekStart == direction) { - nStatus = klseek(file, offset, SEEK_SET); + nStatus = file.Seek(offset, FileReader::SeekSet); } else if (kSeekCurrent == direction) { - nStatus = klseek(file, offset, SEEK_CUR); + nStatus = file.Seek(offset, FileReader::SeekCur); } // TODO - end seek @@ -125,7 +114,7 @@ bool FileStream::Is_Eos() int32_t FileStream::GetPosition() { - return ktell(file); + return file.Tell(); } } // close namespace SmackerCommon diff --git a/source/rr/src/premap.cpp b/source/rr/src/premap.cpp index d626323d9..fd75ab255 100644 --- a/source/rr/src/premap.cpp +++ b/source/rr/src/premap.cpp @@ -2256,6 +2256,13 @@ void G_SetupFilenameBasedMusic(char *nameBuf, const char *fileName, int levelNum #endif #ifdef HAVE_VORBIS "ogg", +#endif +#ifdef HAVE_XMP + "xm", + "mod", + "it", + "s3m", + "mtm", #endif "mid" };