From bf8a2ee5736f65a5c50c675673aae5760fd345ad Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 20 Oct 2019 17:09:58 +0200 Subject: [PATCH] - started refactoring of file handling, starting with some nasty bits in Shadow Warrior. --- source/duke3d/src/common_game.h | 4 -- source/rr/src/common_game.h | 4 -- source/sw/src/scrip2.cpp | 21 +++++----- source/sw/src/sounds.cpp | 69 ++++++++++----------------------- 4 files changed, 30 insertions(+), 68 deletions(-) diff --git a/source/duke3d/src/common_game.h b/source/duke3d/src/common_game.h index b1df09bec..fab04137a 100644 --- a/source/duke3d/src/common_game.h +++ b/source/duke3d/src/common_game.h @@ -140,13 +140,9 @@ extern void G_LoadLookups(void); ////////// -#if defined HAVE_FLAC || defined HAVE_VORBIS # define FORMAT_UPGRADE_ELIGIBLE extern int g_maybeUpgradeSoundFormats; extern buildvfs_kfd S_OpenAudio(const char *fn, char searchfirst, uint8_t ismusic); -#else -# define S_OpenAudio(fn, searchfirst, ismusic) kopen4loadfrommod(fn, searchfirst) -#endif void G_AddGroup(const char* buffer); void G_AddPath(const char* buffer); diff --git a/source/rr/src/common_game.h b/source/rr/src/common_game.h index d7f5fa74b..d0216484a 100644 --- a/source/rr/src/common_game.h +++ b/source/rr/src/common_game.h @@ -148,12 +148,8 @@ extern void G_LoadLookups(void); ////////// -#if defined HAVE_FLAC || defined HAVE_VORBIS # define FORMAT_UPGRADE_ELIGIBLE extern int32_t S_OpenAudio(const char *fn, char searchfirst, uint8_t ismusic); -#else -# define S_OpenAudio(fn, searchfirst, ismusic) kopen4loadfrommod(fn, searchfirst) -#endif void G_AddGroup(const char* buffer); void G_AddPath(const char* buffer); diff --git a/source/sw/src/scrip2.cpp b/source/sw/src/scrip2.cpp index 235852ba8..cc17198dd 100644 --- a/source/sw/src/scrip2.cpp +++ b/source/sw/src/scrip2.cpp @@ -76,25 +76,24 @@ SWBOOL tokenready; // only TRUE if UnGetToken was just ca SWBOOL LoadScriptFile(const char *filename) { - int size, readsize; - int fp; + size_t size, readsize; + FileReader fp; - if ((fp=kopen4load(filename,0)) == -1) - { - // If there's no script file, forget it. - return FALSE; - } - size = kfilelength(fp); + if (!(fp = kopenFileReader(filename, 0)).isOpen()) + { + // If there's no script file, forget it. + return FALSE; + } + + size = fp.GetLength(); scriptbuffer = (char *)AllocMem(size); ASSERT(scriptbuffer != NULL); - readsize = kread(fp, scriptbuffer, size); - - kclose(fp); + readsize = fp.Read(scriptbuffer, size); ASSERT(readsize == size); diff --git a/source/sw/src/sounds.cpp b/source/sw/src/sounds.cpp index 8bb1c32eb..6d59a521f 100644 --- a/source/sw/src/sounds.cpp +++ b/source/sw/src/sounds.cpp @@ -160,8 +160,8 @@ AMB_INFO ambarray[] = #define MAXSONGS 10 // This is the max songs per episode -SWBOOL OpenSound(VOC_INFOp vp, int *handle, int *length); -int ReadSound(int handle, VOC_INFOp vp, int length); +SWBOOL OpenSound(VOC_INFOp vp, FileReader &handle, int *length); +int ReadSound(FileReader & handle, VOC_INFOp vp, int length); // 3d sound engine function prototype VOC3D_INFOp Insert3DSound(void); @@ -752,10 +752,10 @@ SWBOOL CacheSound(int num, int type) // if no data we need to cache it in if (!vp->data) { - int handle; + FileReader handle; int length; - if (!OpenSound(vp, &handle, &length)) + if (!OpenSound(vp, handle, &length)) { sprintf(ds,"Could not open sound %s, num %d, priority %d\n",vp->name,num,vp->priority); CON_ConMessage("%s", ds); @@ -778,29 +778,11 @@ SWBOOL CacheSound(int num, int type) cacheAllocateBlock((intptr_t*)&vp->data, length, &vp->lock); -#if 0 - // DEBUG - globsndata[num] = AllocMem(length); - glength[num] = length; - - fp = fopen(vp->name, "rb"); - if (fp != NULL) - { - fread(globsndata[num], length, 1, fp); - ASSERT(globsndata[num] != NULL); - fclose(fp); - } -#endif /////// ASSERT(vp->data); ReadSound(handle, vp, length); -#if 0 - //DEBUG - globvpdata[num] = vp->data; - CheckSndData(__FILE__, __LINE__); -#endif } } @@ -1093,25 +1075,25 @@ void PlaySoundRTS(int rts_num) /////////////////////////////////////////////// SWBOOL -OpenSound(VOC_INFOp vp, int *handle, int *length) +OpenSound(VOC_INFOp vp, FileReader &handle, int *length) { - *handle = kopen4load(vp->name, 0); + handle = kopenFileReader(vp->name, 0); - if (*handle == -1) + if (!handle.isOpen()) { return FALSE; } - *length = kfilelength(*handle); + *length = handle.GetLength(); return TRUE; } int -ReadSound(int handle, VOC_INFOp vp, int length) +ReadSound(FileReader &handle, VOC_INFOp vp, int length) { - if (kread(handle, vp->data, length) != length) + if (handle.Read(vp->data, length) != length) { TerminateGame(); printf("Error reading file '%s'.\n", vp->name); @@ -1119,41 +1101,31 @@ ReadSound(int handle, VOC_INFOp vp, int length) } vp->datalen = length; - - kclose(handle); return 0; } SWBOOL LoadSong(const char *filename) { - int handle; - int size; - char *ptr; - - if ((handle = kopen4load(filename, 0)) == -1) + auto fr = kopenFileReader(filename, 0); + if (!fr.isOpen()) { return FALSE; } - size = kfilelength(handle); + auto size = fr.GetLength(); - ptr = (char *) AllocMem(size); + auto ptr = (char *) AllocMem(size); if (ptr == NULL) { - kclose(handle); return FALSE; } - if (kread(handle, ptr, size) != size) + if (fr.Read(ptr, size) != size) { FreeMem(ptr); - kclose(handle); return FALSE; } - - kclose(handle); - SongPtr = ptr; SongLength = size; @@ -1246,14 +1218,13 @@ void loadtmb(void) char tmb[8000]; int fil, l; - fil = kopen4load("swtimbr.tmb",0); - if (fil == -1) + auto fil = kopenFileReader("swtimbr.tmb",0); + if (!fil.isOpen()) return; - l = min((size_t)kfilelength(fil), sizeof(tmb)); - kread(fil,tmb,l); - MUSIC_RegisterTimbreBank(tmb); - kclose(fil); + auto tmb = fil.Read(); + if(tmb.Size()) + MUSIC_RegisterTimbreBank(tmb.Data()); } #endif