- started refactoring of file handling, starting with some nasty bits in Shadow Warrior.

This commit is contained in:
Christoph Oelckers 2019-10-20 17:09:58 +02:00
parent 06f4138202
commit bf8a2ee573
4 changed files with 30 additions and 68 deletions

View file

@ -140,13 +140,9 @@ extern void G_LoadLookups(void);
////////// //////////
#if defined HAVE_FLAC || defined HAVE_VORBIS
# 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 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_AddGroup(const char* buffer);
void G_AddPath(const char* buffer); void G_AddPath(const char* buffer);

View file

@ -148,12 +148,8 @@ extern void G_LoadLookups(void);
////////// //////////
#if defined HAVE_FLAC || defined HAVE_VORBIS
# define FORMAT_UPGRADE_ELIGIBLE # define FORMAT_UPGRADE_ELIGIBLE
extern int32_t S_OpenAudio(const char *fn, char searchfirst, uint8_t ismusic); 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_AddGroup(const char* buffer);
void G_AddPath(const char* buffer); void G_AddPath(const char* buffer);

View file

@ -76,25 +76,24 @@ SWBOOL tokenready; // only TRUE if UnGetToken was just ca
SWBOOL LoadScriptFile(const char *filename) SWBOOL LoadScriptFile(const char *filename)
{ {
int size, readsize; size_t size, readsize;
int fp; 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); scriptbuffer = (char *)AllocMem(size);
ASSERT(scriptbuffer != NULL); ASSERT(scriptbuffer != NULL);
readsize = kread(fp, scriptbuffer, size); readsize = fp.Read(scriptbuffer, size);
kclose(fp);
ASSERT(readsize == size); ASSERT(readsize == size);

View file

@ -160,8 +160,8 @@ AMB_INFO ambarray[] =
#define MAXSONGS 10 // This is the max songs per episode #define MAXSONGS 10 // This is the max songs per episode
SWBOOL OpenSound(VOC_INFOp vp, int *handle, int *length); SWBOOL OpenSound(VOC_INFOp vp, FileReader &handle, int *length);
int ReadSound(int handle, VOC_INFOp vp, int length); int ReadSound(FileReader & handle, VOC_INFOp vp, int length);
// 3d sound engine function prototype // 3d sound engine function prototype
VOC3D_INFOp Insert3DSound(void); VOC3D_INFOp Insert3DSound(void);
@ -752,10 +752,10 @@ SWBOOL CacheSound(int num, int type)
// if no data we need to cache it in // if no data we need to cache it in
if (!vp->data) if (!vp->data)
{ {
int handle; FileReader handle;
int length; 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); sprintf(ds,"Could not open sound %s, num %d, priority %d\n",vp->name,num,vp->priority);
CON_ConMessage("%s", ds); CON_ConMessage("%s", ds);
@ -778,29 +778,11 @@ SWBOOL CacheSound(int num, int type)
cacheAllocateBlock((intptr_t*)&vp->data, length, &vp->lock); 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); ASSERT(vp->data);
ReadSound(handle, vp, length); 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 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; return FALSE;
} }
*length = kfilelength(*handle); *length = handle.GetLength();
return TRUE; return TRUE;
} }
int 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(); TerminateGame();
printf("Error reading file '%s'.\n", vp->name); printf("Error reading file '%s'.\n", vp->name);
@ -1119,41 +1101,31 @@ ReadSound(int handle, VOC_INFOp vp, int length)
} }
vp->datalen = length; vp->datalen = length;
kclose(handle);
return 0; return 0;
} }
SWBOOL SWBOOL
LoadSong(const char *filename) LoadSong(const char *filename)
{ {
int handle; auto fr = kopenFileReader(filename, 0);
int size; if (!fr.isOpen())
char *ptr;
if ((handle = kopen4load(filename, 0)) == -1)
{ {
return FALSE; return FALSE;
} }
size = kfilelength(handle); auto size = fr.GetLength();
ptr = (char *) AllocMem(size); auto ptr = (char *) AllocMem(size);
if (ptr == NULL) if (ptr == NULL)
{ {
kclose(handle);
return FALSE; return FALSE;
} }
if (kread(handle, ptr, size) != size) if (fr.Read(ptr, size) != size)
{ {
FreeMem(ptr); FreeMem(ptr);
kclose(handle);
return FALSE; return FALSE;
} }
kclose(handle);
SongPtr = ptr; SongPtr = ptr;
SongLength = size; SongLength = size;
@ -1246,14 +1218,13 @@ void loadtmb(void)
char tmb[8000]; char tmb[8000];
int fil, l; int fil, l;
fil = kopen4load("swtimbr.tmb",0); auto fil = kopenFileReader("swtimbr.tmb",0);
if (fil == -1) if (!fil.isOpen())
return; return;
l = min((size_t)kfilelength(fil), sizeof(tmb)); auto tmb = fil.Read();
kread(fil,tmb,l); if(tmb.Size())
MUSIC_RegisterTimbreBank(tmb); MUSIC_RegisterTimbreBank(tmb.Data());
kclose(fil);
} }
#endif #endif