- refactored the file access in the rest of the DN3D frontend and in some duplicated code in the Blood frontend.

This commit is contained in:
Christoph Oelckers 2019-10-20 23:37:07 +02:00
parent d3c2d8e96f
commit 5022c58a63
18 changed files with 181 additions and 210 deletions

View file

@ -677,13 +677,13 @@ void G_DoAutoload(const char *dirname)
#ifdef FORMAT_UPGRADE_ELIGIBLE #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 #ifdef HAVE_FLAC
{ {
Bstrcpy(fn_suffix, ".flac"); Bstrcpy(fn_suffix, ".flac");
int32_t const fp = kopen4loadfrommod(testfn, searchfirst); auto fp = kopenFileReader(testfn, searchfirst);
if (fp >= 0) if (fp.isOpen())
return fp; return fp;
} }
#endif #endif
@ -691,16 +691,16 @@ static int32_t S_TryFormats(char * const testfn, char * const fn_suffix, char co
#ifdef HAVE_VORBIS #ifdef HAVE_VORBIS
{ {
Bstrcpy(fn_suffix, ".ogg"); Bstrcpy(fn_suffix, ".ogg");
int32_t const fp = kopen4loadfrommod(testfn, searchfirst); auto fp = kopenFileReader(testfn, searchfirst);
if (fp >= 0) if (fp.isOpen())
return fp; return fp;
} }
#endif #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 * extension = Bstrrchr(testfn, '.');
char * const fn_end = Bstrchr(testfn, '\0'); char * const fn_end = Bstrchr(testfn, '\0');
@ -710,8 +710,8 @@ static int32_t S_TryExtensionReplacements(char * const testfn, char const search
{ {
*extension = '_'; *extension = '_';
int32_t const fp = S_TryFormats(testfn, fn_end, searchfirst); auto fp = S_TryFormats(testfn, fn_end, searchfirst);
if (fp >= 0) if (fp.isOpen())
return fp; return fp;
} }
else else
@ -722,28 +722,31 @@ static int32_t S_TryExtensionReplacements(char * const testfn, char const search
// ex: grabbag.mid --> grabbag.* // ex: grabbag.mid --> grabbag.*
if (ismusic) if (ismusic)
{ {
int32_t const fp = S_TryFormats(testfn, extension, searchfirst); auto fp = S_TryFormats(testfn, extension, searchfirst);
if (fp >= 0) if (fp.isOpen())
return fp; 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); auto origfp = kopenFileReader(fn, searchfirst);
char const *const origparent = origfp != -1 ? kfileparent(origfp) : NULL; char const* const origparent = origfp.isOpen() ? kfileparent(origfp) : NULL;
uint32_t const parentlength = origparent != NULL ? Bstrlen(origparent) : 0; 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' 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); auto fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic);
if (fp >= 0) if (fp.isOpen())
goto success; {
Bfree(testfn);
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
@ -753,26 +756,29 @@ int32_t S_OpenAudio(const char *fn, char searchfirst, uint8_t const ismusic)
char const * const parentextension = Bstrrchr(origparent, '.'); char const * const parentextension = Bstrrchr(origparent, '.');
uint32_t const namelength = parentextension != NULL ? (unsigned)(parentextension - origparent) : parentlength; 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);
fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); auto fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic);
if (fp >= 0) if (fp.isOpen())
goto success; {
Bfree(testfn);
return fp;
}
} }
// look in ./music/ // look in ./music/
// ex: ./music/grabbag.mid // ex: ./music/grabbag.mid
Bsprintf(testfn, "music/%s", fn); {
fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); Bsprintf(testfn, "music/%s", fn);
if (fp >= 0) auto fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic);
goto success; if (fp.isOpen())
{
Bfree(testfn);
return fp;
}
}
fp = origfp; Bfree(testfn);
success: return origfp;
Bfree(testfn);
if (fp != origfp)
kclose(origfp);
return fp;
} }
#endif #endif

View file

@ -541,7 +541,7 @@ static inline void G_HandleAsync(void)
} }
# define FORMAT_UPGRADE_ELIGIBLE # 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) #pragma pack(push,1)

View file

@ -98,8 +98,8 @@ int sndPlaySong(const char *songName, bool bLoop)
if (!songName || strlen(songName) == 0) if (!songName || strlen(songName) == 0)
return 1; return 1;
int32_t fp = S_OpenAudio(songName, 0, 1); auto fp = S_OpenAudio(songName, 0, 1);
if (EDUKE32_PREDICT_FALSE(fp < 0)) if (!fp.isOpen())
{ {
hSong = gSoundRes.Lookup(songName, "MID"); hSong = gSoundRes.Lookup(songName, "MID");
if (!hSong) if (!hSong)
@ -132,29 +132,25 @@ int sndPlaySong(const char *songName, bool bLoop)
return 0; return 0;
} }
int32_t nSongLen = kfilelength(fp); int32_t nSongLen = fp.Tell();
if (EDUKE32_PREDICT_FALSE(nSongLen < 4)) if (EDUKE32_PREDICT_FALSE(nSongLen < 4))
{ {
OSD_Printf(OSD_ERROR "sndPlaySong(): error: empty music file \"%s\"\n", songName); OSD_Printf(OSD_ERROR "sndPlaySong(): error: empty music file \"%s\"\n", songName);
kclose(fp);
return 3; return 3;
} }
char * pNewSongPtr = (char *)Xaligned_alloc(16, nSongLen); 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)) if (EDUKE32_PREDICT_FALSE(nNewSongSize != nSongLen))
{ {
OSD_Printf(OSD_ERROR "sndPlaySong(): error: read %d bytes from \"%s\", expected %d\n", OSD_Printf(OSD_ERROR "sndPlaySong(): error: read %d bytes from \"%s\", expected %d\n",
nNewSongSize, songName, nSongLen); nNewSongSize, songName, nSongLen);
kclose(fp);
ALIGNED_FREE_AND_NULL(pNewSongPtr); ALIGNED_FREE_AND_NULL(pNewSongPtr);
return 4; return 4;
} }
kclose(fp);
if (!Bmemcmp(pNewSongPtr, "MThd", 4)) if (!Bmemcmp(pNewSongPtr, "MThd", 4))
{ {
int32_t retval = MUSIC_PlaySong(pNewSongPtr, nNewSongSize, bLoop); int32_t retval = MUSIC_PlaySong(pNewSongPtr, nNewSongSize, bLoop);

View file

@ -262,6 +262,13 @@ public:
return LittleShort(v); return LittleShort(v);
} }
int16_t ReadInt16BE()
{
uint16_t v = 0;
Read(&v, 2);
return BigShort(v);
}
uint32_t ReadUInt32() uint32_t ReadUInt32()
{ {
uint32_t v = 0; uint32_t v = 0;

View file

@ -1059,29 +1059,30 @@ void G_LoadLookups(void)
#ifdef FORMAT_UPGRADE_ELIGIBLE #ifdef FORMAT_UPGRADE_ELIGIBLE
int g_maybeUpgradeSoundFormats = 1; 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 #ifdef HAVE_FLAC
{
Bstrcpy(fn_suffix, ".flac"); Bstrcpy(fn_suffix, ".flac");
buildvfs_kfd const ffp = kopen4loadfrommod(testfn, searchfirst); auto fp = kopenFileReader(testfn, searchfirst);
if (ffp != buildvfs_kfd_invalid) if (fp.isOpen())
return ffp; return fp;
}
#endif #endif
#ifdef HAVE_VORBIS #ifdef HAVE_VORBIS
{
Bstrcpy(fn_suffix, ".ogg"); Bstrcpy(fn_suffix, ".ogg");
buildvfs_kfd const fp = kopen4loadfrommod(testfn, searchfirst); auto fp = kopenFileReader(testfn, searchfirst);
if (fp != buildvfs_kfd_invalid) if (fp.isOpen())
return fp; return fp;
}
#endif #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 * extension = Bstrrchr(testfn, '.');
char * const fn_end = Bstrchr(testfn, '\0'); char * const fn_end = Bstrchr(testfn, '\0');
@ -1091,8 +1092,8 @@ static buildvfs_kfd S_TryExtensionReplacements(char * const testfn, char const s
{ {
*extension = '_'; *extension = '_';
buildvfs_kfd const fp = S_TryFormats(testfn, fn_end, searchfirst); auto fp = S_TryFormats(testfn, fn_end, searchfirst);
if (fp != buildvfs_kfd_invalid) if (fp.isOpen())
return fp; return fp;
} }
else else
@ -1103,34 +1104,32 @@ static buildvfs_kfd S_TryExtensionReplacements(char * const testfn, char const s
// ex: grabbag.mid --> grabbag.* // ex: grabbag.mid --> grabbag.*
if (ismusic) if (ismusic)
{ {
buildvfs_kfd const fp = S_TryFormats(testfn, extension, searchfirst); auto fp = S_TryFormats(testfn, extension, searchfirst);
if (fp != buildvfs_kfd_invalid) if (fp.isOpen())
return fp; 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); auto origfp = kopenFileReader(fn, searchfirst);
#ifndef USE_PHYSFS char const* const origparent = origfp.isOpen() ? kfileparent(origfp) : NULL;
char const * const origparent = origfp != buildvfs_kfd_invalid ? kfileparent(origfp) : NULL; uint32_t const parentlength = origparent != NULL ? Bstrlen(origparent) : 0;
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' 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 ./ // look in ./
// ex: ./grabbag.mid // ex: ./grabbag.mid
Bstrcpy(testfn, fn); Bstrcpy(testfn, fn);
buildvfs_kfd fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); auto fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic);
if (fp != buildvfs_kfd_invalid) if (fp.isOpen())
goto success; {
Bfree(testfn);
return fp;
}
#ifndef USE_PHYSFS
// 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
@ -1139,27 +1138,29 @@ buildvfs_kfd S_OpenAudio(const char *fn, char searchfirst, uint8_t const ismusic
char const * const parentextension = Bstrrchr(origparent, '.'); char const * const parentextension = Bstrrchr(origparent, '.');
uint32_t const namelength = parentextension != NULL ? (unsigned)(parentextension - origparent) : parentlength; 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);
fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); auto fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic);
if (fp != buildvfs_kfd_invalid) if (fp.isOpen())
goto success; {
Bfree(testfn);
return fp;
}
} }
// look in ./music/ // look in ./music/
// ex: ./music/grabbag.mid // ex: ./music/grabbag.mid
Bsprintf(testfn, "music/%s", fn); {
fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); Bsprintf(testfn, "music/%s", fn);
if (fp != buildvfs_kfd_invalid) auto fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic);
goto success; if (fp.isOpen())
#endif {
Bfree(testfn);
return fp;
}
}
Xfree(testfn); Bfree(testfn);
return origfp; return origfp;
success:
Xfree(testfn);
kclose(origfp);
return fp;
} }
void Duke_CommonCleanup(void) void Duke_CommonCleanup(void)

View file

@ -142,7 +142,7 @@ extern void G_LoadLookups(void);
# define FORMAT_UPGRADE_ELIGIBLE # define FORMAT_UPGRADE_ELIGIBLE
extern int g_maybeUpgradeSoundFormats; 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_AddGroup(const char* buffer);
void G_AddPath(const char* buffer); void G_AddPath(const char* buffer);

View file

@ -6096,11 +6096,9 @@ static void G_Startup(void)
Bcorrectfilename(boardfilename,0); Bcorrectfilename(boardfilename,0);
buildvfs_kfd ii = kopen4loadfrommod(boardfilename, 0); if (testkopen(boardfilename, 0))
if (ii != buildvfs_kfd_invalid) {
{
initprintf("Using level: \"%s\".\n",boardfilename); initprintf("Using level: \"%s\".\n",boardfilename);
kclose(ii);
} }
else else
{ {
@ -6461,14 +6459,11 @@ int app_main(int argc, char const * const * argv)
g_Shareware = 1; g_Shareware = 1;
else else
{ {
buildvfs_kfd const kFile = kopen4load("DUKESW.BIN",1); // JBF 20030810 if (testkopen("DUKESW.BIN", 1)) // JBF 20030810
{
if (kFile != buildvfs_kfd_invalid) g_Shareware = 1;
{ }
g_Shareware = 1; }
kclose(kFile);
}
}
#endif #endif
// gotta set the proper title after we compile the CONs if this is the full version // gotta set the proper title after we compile the CONs if this is the full version

View file

@ -1901,22 +1901,22 @@ static int C_CountCaseStatements()
static void C_Include(const char *confile) 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++; g_errorCnt++;
initprintf("%s:%d: error: could not find file `%s'.\n",g_scriptFileName,g_lineNumber,confile); initprintf("%s:%d: error: could not find file `%s'.\n",g_scriptFileName,g_lineNumber,confile);
return; return;
} }
int32_t const len = kfilelength(fp); int32_t const len = fp.GetLength();
char *mptr = (char *)Xmalloc(len+1); char *mptr = (char *)Xmalloc(len+1);
initprintf("Including: %s (%d bytes)\n",confile, len); initprintf("Including: %s (%d bytes)\n",confile, len);
kread(fp, mptr, len); fp.Read(mptr, len);
kclose(fp); fp.Close();
mptr[len] = 0; mptr[len] = 0;
g_scriptcrc = Bcrc32(mptr, len, g_scriptcrc); g_scriptcrc = Bcrc32(mptr, len, g_scriptcrc);
@ -1956,9 +1956,8 @@ static void C_Include(const char *confile)
#ifdef _WIN32 #ifdef _WIN32
static void check_filename_case(const char *fn) static void check_filename_case(const char *fn)
{ {
buildvfs_kfd fp; // WTF?!?
if ((fp = kopen4loadfrommod(fn, g_loadFromGroupOnly)) != buildvfs_kfd_invalid) testkopen(fn, g_loadFromGroupOnly);
kclose(fp);
} }
#else #else
static void check_filename_case(const char *fn) { UNREFERENCED_PARAMETER(fn); } static void check_filename_case(const char *fn) { UNREFERENCED_PARAMETER(fn); }
@ -6261,9 +6260,9 @@ void C_Compile(const char *fileName)
Gv_Init(); Gv_Init();
C_InitProjectiles(); 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) if (g_loadFromGroupOnly == 1 || numgroupfiles == 0)
{ {
@ -6286,7 +6285,7 @@ void C_Compile(const char *fileName)
return; //Not there return; //Not there
} }
int const kFileLen = kfilelength(kFile); int const kFileLen = kFile.GetLength();
initprintf("Compiling: %s (%d bytes)\n", fileName, kFileLen); initprintf("Compiling: %s (%d bytes)\n", fileName, kFileLen);
@ -6298,8 +6297,8 @@ void C_Compile(const char *fileName)
mptr[kFileLen] = 0; mptr[kFileLen] = 0;
textptr = (char *)mptr; textptr = (char *)mptr;
kread(kFile, (char *)textptr, kFileLen); kFile.Read((char*)textptr, kFileLen);
kclose(kFile); kFile.Close();
g_scriptcrc = Bcrc32(NULL, 0, 0L); g_scriptcrc = Bcrc32(NULL, 0, 0L);
g_scriptcrc = Bcrc32(textptr, kFileLen, g_scriptcrc); g_scriptcrc = Bcrc32(textptr, kFileLen, g_scriptcrc);

View file

@ -5647,12 +5647,12 @@ badindex:
VM_ASSERT((unsigned)quoteFilename < MAXQUOTES && apStrings[quoteFilename], "invalid quote %d\n", quoteFilename); 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(); dispatch();
size_t const filelength = kfilelength(kFile); size_t const filelength = kFile.GetLength();
size_t const numElements = Gv_GetArrayCountForAllocSize(arrayNum, filelength); size_t const numElements = Gv_GetArrayCountForAllocSize(arrayNum, filelength);
if (numElements > 0) if (numElements > 0)
@ -5680,7 +5680,7 @@ badindex:
{ {
void *const pArray = Xcalloc(1, newBytes); void *const pArray = Xcalloc(1, newBytes);
kread(kFile, pArray, readBytes); kFile.Read(pArray, readBytes);
if (flags & GAMEARRAY_UNSIGNED) if (flags & GAMEARRAY_UNSIGNED)
{ {
@ -5699,12 +5699,12 @@ badindex:
#endif #endif
default: default:
memset((char *)pValues + readBytes, 0, newBytes - readBytes); memset((char *)pValues + readBytes, 0, newBytes - readBytes);
kread(kFile, pValues, readBytes); kFile.Read(pValues, readBytes);
break; break;
} }
} }
kclose(kFile); kFile.Close();
dispatch(); dispatch();
} }

View file

@ -1988,16 +1988,11 @@ static void Net_ReceiveUserMapName(uint8_t *pbuf, int32_t packbufleng)
Bcorrectfilename(boardfilename, 0); Bcorrectfilename(boardfilename, 0);
if (boardfilename[0] != 0) if (boardfilename[0] != 0)
{ {
buildvfs_kfd i; if (testkopen(boardfilename, 0))
if ((i = kopen4loadfrommod(boardfilename, 0)) == buildvfs_kfd_invalid)
{ {
Bmemset(boardfilename, 0, sizeof(boardfilename)); Bmemset(boardfilename, 0, sizeof(boardfilename));
Net_SendUserMapName(); Net_SendUserMapName();
} }
else
{
kclose(i);
}
} }
if (ud.m_level_number == 7 && ud.m_volume_number == 0 && boardfilename[0] == 0) if (ud.m_level_number == 7 && ud.m_volume_number == 0 && boardfilename[0] == 0)

View file

@ -209,13 +209,11 @@ static int osdcmd_map(osdcmdptr_t parm)
maybe_append_ext(filename, sizeof(filename), parm->parms[0], ".map"); maybe_append_ext(filename, sizeof(filename), parm->parms[0], ".map");
buildvfs_kfd ii; if (!testkopen(filename,0))
if ((ii = kopen4loadfrommod(filename,0)) == buildvfs_kfd_invalid)
{ {
OSD_Printf(OSD_ERROR "map: file \"%s\" not found.\n", filename); OSD_Printf(OSD_ERROR "map: file \"%s\" not found.\n", filename);
return OSDCMD_OK; return OSDCMD_OK;
} }
kclose(ii);
boardfilename[0] = '/'; boardfilename[0] = '/';
boardfilename[1] = 0; boardfilename[1] = 0;

View file

@ -1728,9 +1728,8 @@ void G_SetupFilenameBasedMusic(char *nameBuf, const char *fileName)
Bmemcpy(p+1, ext, Bstrlen(ext) + 1); Bmemcpy(p+1, ext, Bstrlen(ext) + 1);
if ((kFile = kopen4loadfrommod(nameBuf, 0)) != buildvfs_kfd_invalid) if (testkopen(nameBuf, 0))
{ {
kclose(kFile);
realloc_copy(&g_mapInfo[USERMAPMUSICFAKESLOT].musicfn, nameBuf); realloc_copy(&g_mapInfo[USERMAPMUSICFAKESLOT].musicfn, nameBuf);
return; return;
} }

View file

@ -173,14 +173,12 @@ static void ReadSaveGameHeaders_CACHE1D(CACHE1D_FIND_REC *f)
{ {
if (FURY) if (FURY)
{ {
char extfn[BMAX_PATH]; FStringf extfn("%s.ext", fn);
snprintf(extfn, ARRAY_SIZE(extfn), "%s.ext", fn); auto extfil = fopenFileReader(extfn, 0);
buildvfs_kfd extfil = kopen4loadfrommod(extfn, 0); if (extfil.isOpen())
if (extfil != buildvfs_kfd_invalid) {
{ msv.brief.isExt = 1;
msv.brief.isExt = 1; }
kclose(extfil);
}
} }
} }
msv.isOldVer = 1; msv.isOldVer = 1;
@ -376,32 +374,22 @@ int32_t G_LoadPlayer(savebrief_t & sv)
} }
} }
char extfn[BMAX_PATH]; FStringf extfn("%s.ext", sv.path);
snprintf(extfn, ARRAY_SIZE(extfn), "%s.ext", sv.path); auto extfil = fopenFileReader(extfn, 0);
buildvfs_kfd extfil = kopen4loadfrommod(extfn, 0); if (!extfil.isOpen())
if (extfil == buildvfs_kfd_invalid)
{ {
return -1; return -1;
} }
int32_t len = kfilelength(extfil); auto text = extfil.ReadPadded(1);
auto text = (char *)Xmalloc(len+1);
text[len] = '\0';
if (kread_and_test(extfil, text, len)) if (text.Size() == 0)
{ {
kclose(extfil);
Xfree(text);
return -1; return -1;
} }
kclose(extfil);
sjson_context * ctx = sjson_create_context(0, 0, NULL); sjson_context * ctx = sjson_create_context(0, 0, NULL);
sjson_node * root = sjson_decode(ctx, text); sjson_node * root = sjson_decode(ctx, (const char *)text.Data());
Xfree(text);
if (volume == -1) if (volume == -1)
volume = sjson_get_int(root, "volume", volume); volume = sjson_get_int(root, "volume", volume);

View file

@ -1460,14 +1460,10 @@ void gameDisplay3DRScreen()
buildvfs_kfd i; buildvfs_kfd i;
Net_GetPackets(); Net_GetPackets();
i = kopen4loadfrommod("3dr.ivf", 0); if (testkopen("3dr.ivf", 0) || testkopen("3dr.anm", 0))
{
if (i == buildvfs_kfd_invalid) Anim_Play("3dr.anm");
i = kopen4loadfrommod("3dr.anm", 0); kclose(i);
if (i != buildvfs_kfd_invalid)
{
kclose(i);
Anim_Play("3dr.anm"); Anim_Play("3dr.anm");
G_FadePalette(0, 0, 0, 252); G_FadePalette(0, 0, 0, 252);
I_ClearAllInput(); I_ClearAllInput();

View file

@ -212,36 +212,32 @@ static int S_PlayMusic(const char *fn)
if (fn == NULL) if (fn == NULL)
return 1; return 1;
buildvfs_kfd fp = S_OpenAudio(fn, 0, 1); auto fp = S_OpenAudio(fn, 0, 1);
if (EDUKE32_PREDICT_FALSE(fp == buildvfs_kfd_invalid)) if (!fp.isOpen())
{ {
OSD_Printf(OSD_ERROR "S_PlayMusic(): error: can't open \"%s\" for playback!\n",fn); OSD_Printf(OSD_ERROR "S_PlayMusic(): error: can't open \"%s\" for playback!\n",fn);
return 2; return 2;
} }
int32_t MusicLen = kfilelength(fp); int32_t MusicLen = fp.GetLength();
if (EDUKE32_PREDICT_FALSE(MusicLen < 4)) if (EDUKE32_PREDICT_FALSE(MusicLen < 4))
{ {
OSD_Printf(OSD_ERROR "S_PlayMusic(): error: empty music file \"%s\"\n", fn); OSD_Printf(OSD_ERROR "S_PlayMusic(): error: empty music file \"%s\"\n", fn);
kclose(fp);
return 3; return 3;
} }
char * MyMusicPtr = (char *)Xaligned_alloc(16, MusicLen); 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)) if (EDUKE32_PREDICT_FALSE(MyMusicSize != MusicLen))
{ {
OSD_Printf(OSD_ERROR "S_PlayMusic(): error: read %d bytes from \"%s\", expected %d\n", OSD_Printf(OSD_ERROR "S_PlayMusic(): error: read %d bytes from \"%s\", expected %d\n",
MyMusicSize, fn, MusicLen); MyMusicSize, fn, MusicLen);
kclose(fp);
ALIGNED_FREE_AND_NULL(MyMusicPtr); ALIGNED_FREE_AND_NULL(MyMusicPtr);
return 4; return 4;
} }
kclose(fp);
if (!Bmemcmp(MyMusicPtr, "MThd", 4)) if (!Bmemcmp(MyMusicPtr, "MThd", 4))
{ {
int32_t retval = MUSIC_PlaySong(MyMusicPtr, MyMusicSize, MUSIC_LoopSong); int32_t retval = MUSIC_PlaySong(MyMusicPtr, MyMusicSize, MUSIC_LoopSong);
@ -450,20 +446,19 @@ int32_t S_LoadSound(int num)
auto &snd = g_sounds[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); OSD_Printf(OSDTEXT_RED "Sound %s(#%d) not found!\n", snd.filename, num);
return 0; return 0;
} }
int32_t l = kfilelength(fp); int32_t l = fp.GetLength();
g_soundlocks[num] = 255; g_soundlocks[num] = 255;
snd.siz = l; snd.siz = l;
cacheAllocateBlock((intptr_t *)&snd.ptr, l, &g_soundlocks[num]); cacheAllocateBlock((intptr_t *)&snd.ptr, l, &g_soundlocks[num]);
l = kread(fp, snd.ptr, l); l = fp.Read(snd.ptr, l);
kclose(fp);
return l; return l;
} }

View file

@ -58,7 +58,7 @@ class FileStream
bool Is_Eos(); bool Is_Eos();
private: private:
int file; FileReader file;
}; };
} // close namespace SmackerCommon } // close namespace SmackerCommon

View file

@ -24,8 +24,8 @@ namespace SmackerCommon {
bool FileStream::Open(const std::string &fileName) bool FileStream::Open(const std::string &fileName)
{ {
file = kopen4loadfrommod(fileName.c_str(), 0); file = kopenFileReader(fileName.c_str(), 0);
if (file == -1) if (!file.isOpen())
{ {
// log error // log error
return false; return false;
@ -36,18 +36,17 @@ bool FileStream::Open(const std::string &fileName)
bool FileStream::Is_Open() bool FileStream::Is_Open()
{ {
return file != -1; return file.isOpen();
} }
void FileStream::Close() void FileStream::Close()
{ {
kclose(file); file.Close();
file = -1;
} }
int32_t FileStream::ReadBytes(uint8_t *data, uint32_t nBytes) int32_t FileStream::ReadBytes(uint8_t *data, uint32_t nBytes)
{ {
uint32_t nCount = (uint32_t)kread(file, data, static_cast<int32_t>(nBytes)); uint32_t nCount = (uint32_t)file.Read(data, static_cast<int32_t>(nBytes));
if (nCount != nBytes) if (nCount != nBytes)
{ {
@ -59,47 +58,37 @@ int32_t FileStream::ReadBytes(uint8_t *data, uint32_t nBytes)
uint32_t FileStream::ReadUint32LE() uint32_t FileStream::ReadUint32LE()
{ {
uint32_t value; return file.ReadInt32();
kread(file, &value, 4);
return B_LITTLE32(value);
} }
uint32_t FileStream::ReadUint32BE() uint32_t FileStream::ReadUint32BE()
{ {
uint32_t value; return file.ReadInt32BE();
kread(file, &value, 4);
return B_BIG32(value);
} }
uint16_t FileStream::ReadUint16LE() uint16_t FileStream::ReadUint16LE()
{ {
uint16_t value; return file.ReadInt16();
kread(file, &value, 2);
return B_LITTLE16(value);
} }
uint16_t FileStream::ReadUint16BE() uint16_t FileStream::ReadUint16BE()
{ {
uint16_t value; return file.ReadInt16BE();
kread(file, &value, 2);
return B_BIG16(value);
} }
uint8_t FileStream::ReadByte() uint8_t FileStream::ReadByte()
{ {
uint8_t value; return file.ReadInt8();
kread(file, &value, 1);
return value;
} }
bool FileStream::Seek(int32_t offset, SeekDirection direction) bool FileStream::Seek(int32_t offset, SeekDirection direction)
{ {
int32_t nStatus = -1; int32_t nStatus = -1;
if (kSeekStart == direction) { if (kSeekStart == direction) {
nStatus = klseek(file, offset, SEEK_SET); nStatus = file.Seek(offset, FileReader::SeekSet);
} }
else if (kSeekCurrent == direction) { else if (kSeekCurrent == direction) {
nStatus = klseek(file, offset, SEEK_CUR); nStatus = file.Seek(offset, FileReader::SeekCur);
} }
// TODO - end seek // TODO - end seek
@ -125,7 +114,7 @@ bool FileStream::Is_Eos()
int32_t FileStream::GetPosition() int32_t FileStream::GetPosition()
{ {
return ktell(file); return file.Tell();
} }
} // close namespace SmackerCommon } // close namespace SmackerCommon

View file

@ -2256,6 +2256,13 @@ void G_SetupFilenameBasedMusic(char *nameBuf, const char *fileName, int levelNum
#endif #endif
#ifdef HAVE_VORBIS #ifdef HAVE_VORBIS
"ogg", "ogg",
#endif
#ifdef HAVE_XMP
"xm",
"mod",
"it",
"s3m",
"mtm",
#endif #endif
"mid" "mid"
}; };