- refactored file access in RR frontend's sound code.

This commit is contained in:
Christoph Oelckers 2019-10-20 21:56:13 +02:00
parent 9aae157e27
commit 6e7db1b63a
9 changed files with 63 additions and 73 deletions

View file

@ -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_

View file

@ -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;
}
}

View file

@ -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);

View file

@ -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);
}
}

View file

@ -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);

View file

@ -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)

View file

@ -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;

View file

@ -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;
}

View file

@ -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;
}