mirror of
https://github.com/UberGames/ioef.git
synced 2025-02-21 11:11:19 +00:00
* Partial implementation of FS_Seek for files in pk3s
* A couple of RIFF decoder tweaks/fixes
This commit is contained in:
parent
736d34c989
commit
387d8041ce
2 changed files with 57 additions and 40 deletions
|
@ -66,44 +66,38 @@ static int S_ReadChunkInfo(fileHandle_t f, char *name)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
len = FGetLittleLong(f);
|
len = FGetLittleLong(f);
|
||||||
if(len < 0 || len > 0xffffffff)
|
if( len < 0 ) {
|
||||||
|
Com_Printf( S_COLOR_YELLOW "WARNING: Negative chunk length\n" );
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
//FIXME: 11/11/05 <tim@ngus.net>
|
|
||||||
// I'm not sure I understand why this needs to be padded.
|
|
||||||
// Surely this results in reading past the end of the data?
|
|
||||||
//len = (len + 1 ) & ~1; // pad to word boundary
|
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
=================
|
=================
|
||||||
S_FindWavChunk
|
S_FindRIFFChunk
|
||||||
|
|
||||||
Returns the length of the data in the chunk, or 0 if not found
|
Returns the length of the data in the chunk, or 0 if not found
|
||||||
=================
|
=================
|
||||||
*/
|
*/
|
||||||
static int S_FindWavChunk( fileHandle_t f, char *chunk ) {
|
static int S_FindRIFFChunk( fileHandle_t f, char *chunk ) {
|
||||||
char name[5];
|
char name[5];
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
// This is a bit dangerous...
|
while( ( len = S_ReadChunkInfo(f, name) ) )
|
||||||
while(1)
|
|
||||||
{
|
{
|
||||||
len = S_ReadChunkInfo(f, name);
|
|
||||||
|
|
||||||
// Read failure?
|
|
||||||
if(len == 0)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
// If this is the right chunk, return
|
// If this is the right chunk, return
|
||||||
if(!strcmp(name, chunk))
|
if( !Q_strncmp( name, chunk, 4 ) )
|
||||||
return len;
|
return len;
|
||||||
|
|
||||||
|
len = (len + 1 ) & ~1; // pad to word boundary
|
||||||
|
|
||||||
// Not the right chunk - skip it
|
// Not the right chunk - skip it
|
||||||
FS_Seek( f, len, FS_SEEK_CUR );
|
FS_Seek( f, len, FS_SEEK_CUR );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -131,10 +125,10 @@ static void S_ByteSwapRawSamples( int samples, int width, int s_channels, const
|
||||||
|
|
||||||
/*
|
/*
|
||||||
=================
|
=================
|
||||||
S_ReadWavHeader
|
S_ReadRIFFHeader
|
||||||
=================
|
=================
|
||||||
*/
|
*/
|
||||||
static qboolean S_ReadWavHeader(fileHandle_t file, snd_info_t *info)
|
static qboolean S_ReadRIFFHeader(fileHandle_t file, snd_info_t *info)
|
||||||
{
|
{
|
||||||
char dump[16];
|
char dump[16];
|
||||||
int wav_format;
|
int wav_format;
|
||||||
|
@ -144,9 +138,9 @@ static qboolean S_ReadWavHeader(fileHandle_t file, snd_info_t *info)
|
||||||
FS_Read(dump, 12, file);
|
FS_Read(dump, 12, file);
|
||||||
|
|
||||||
// Scan for the format chunk
|
// Scan for the format chunk
|
||||||
if((fmtlen = S_FindWavChunk(file, "fmt ")) == 0)
|
if((fmtlen = S_FindRIFFChunk(file, "fmt ")) == 0)
|
||||||
{
|
{
|
||||||
Com_Printf("No fmt chunk\n");
|
Com_Printf( S_COLOR_RED "ERROR: Couldn't find \"fmt\" chunk\n");
|
||||||
return qfalse;
|
return qfalse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -167,9 +161,9 @@ static qboolean S_ReadWavHeader(fileHandle_t file, snd_info_t *info)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scan for the data chunk
|
// Scan for the data chunk
|
||||||
if( (info->size = S_FindWavChunk(file, "data")) == 0)
|
if( (info->size = S_FindRIFFChunk(file, "data")) == 0)
|
||||||
{
|
{
|
||||||
Com_Printf("No data chunk\n");
|
Com_Printf( S_COLOR_RED "ERROR: Couldn't find \"data\" chunk\n");
|
||||||
return qfalse;
|
return qfalse;
|
||||||
}
|
}
|
||||||
info->samples = (info->size / info->width) / info->channels;
|
info->samples = (info->size / info->width) / info->channels;
|
||||||
|
@ -202,15 +196,17 @@ void *S_WAV_CodecLoad(const char *filename, snd_info_t *info)
|
||||||
FS_FOpenFileRead(filename, &file, qtrue);
|
FS_FOpenFileRead(filename, &file, qtrue);
|
||||||
if(!file)
|
if(!file)
|
||||||
{
|
{
|
||||||
Com_Printf("Can't read sound file %s\n", filename);
|
Com_Printf( S_COLOR_RED "ERROR: Could not open \"%s\"\n",
|
||||||
|
filename);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the RIFF header
|
// Read the RIFF header
|
||||||
if(!S_ReadWavHeader(file, info))
|
if(!S_ReadRIFFHeader(file, info))
|
||||||
{
|
{
|
||||||
FS_FCloseFile(file);
|
FS_FCloseFile(file);
|
||||||
Com_Printf("Can't understand wav file %s\n", filename);
|
Com_Printf( S_COLOR_RED "ERROR: Incorrect/unsupported format in \"%s\"\n",
|
||||||
|
filename);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,7 +215,8 @@ void *S_WAV_CodecLoad(const char *filename, snd_info_t *info)
|
||||||
if(!buffer)
|
if(!buffer)
|
||||||
{
|
{
|
||||||
FS_FCloseFile(file);
|
FS_FCloseFile(file);
|
||||||
Com_Printf("Out of memory reading %s\n", filename);
|
Com_Printf( S_COLOR_RED "ERROR: Out of memory reading \"%s\"\n",
|
||||||
|
filename);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -247,7 +244,7 @@ snd_stream_t *S_WAV_CodecOpenStream(const char *filename)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
// Read the RIFF header
|
// Read the RIFF header
|
||||||
if(!S_ReadWavHeader(rv->file, &rv->info))
|
if(!S_ReadRIFFHeader(rv->file, &rv->info))
|
||||||
{
|
{
|
||||||
S_CodecUtilClose(rv);
|
S_CodecUtilClose(rv);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
|
@ -1361,6 +1361,8 @@ void QDECL FS_Printf( fileHandle_t h, const char *fmt, ... ) {
|
||||||
FS_Write(msg, strlen(msg), h);
|
FS_Write(msg, strlen(msg), h);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define PK3_SEEK_BUFFER_SIZE 65536
|
||||||
|
|
||||||
/*
|
/*
|
||||||
=================
|
=================
|
||||||
FS_Seek
|
FS_Seek
|
||||||
|
@ -1369,7 +1371,6 @@ FS_Seek
|
||||||
*/
|
*/
|
||||||
int FS_Seek( fileHandle_t f, long offset, int origin ) {
|
int FS_Seek( fileHandle_t f, long offset, int origin ) {
|
||||||
int _origin;
|
int _origin;
|
||||||
char foo[65536];
|
|
||||||
|
|
||||||
if ( !fs_searchpaths ) {
|
if ( !fs_searchpaths ) {
|
||||||
Com_Error( ERR_FATAL, "Filesystem call made without initialization\n" );
|
Com_Error( ERR_FATAL, "Filesystem call made without initialization\n" );
|
||||||
|
@ -1383,19 +1384,38 @@ int FS_Seek( fileHandle_t f, long offset, int origin ) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fsh[f].zipFile == qtrue) {
|
if (fsh[f].zipFile == qtrue) {
|
||||||
if (offset == 0 && origin == FS_SEEK_SET) {
|
//FIXME: this is incomplete and really, really
|
||||||
// set the file position in the zip file (also sets the current file info)
|
//crappy (but better than what was here before)
|
||||||
unzSetCurrentFileInfoPosition(fsh[f].handleFiles.file.z, fsh[f].zipFilePos);
|
byte buffer[PK3_SEEK_BUFFER_SIZE];
|
||||||
return unzOpenCurrentFile(fsh[f].handleFiles.file.z);
|
int remainder = offset;
|
||||||
} else if (offset<65536) {
|
|
||||||
// set the file position in the zip file (also sets the current file info)
|
Com_Printf( S_COLOR_YELLOW "%s %d %d\n", fsh[f].name, offset, origin );
|
||||||
unzSetCurrentFileInfoPosition(fsh[f].handleFiles.file.z, fsh[f].zipFilePos);
|
if( offset < 0 || origin == FS_SEEK_END ) {
|
||||||
unzOpenCurrentFile(fsh[f].handleFiles.file.z);
|
Com_Error( ERR_FATAL, "Negative offsets and FS_SEEK_END not implemented "
|
||||||
return FS_Read(foo, offset, f);
|
"for FS_Seek on pk3 file contents\n" );
|
||||||
} else {
|
|
||||||
Com_Error( ERR_FATAL, "ZIP FILE FSEEK NOT YET IMPLEMENTED\n" );
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch( origin ) {
|
||||||
|
case FS_SEEK_SET:
|
||||||
|
unzSetCurrentFileInfoPosition(fsh[f].handleFiles.file.z, fsh[f].zipFilePos);
|
||||||
|
unzOpenCurrentFile(fsh[f].handleFiles.file.z);
|
||||||
|
//fallthrough
|
||||||
|
|
||||||
|
case FS_SEEK_CUR:
|
||||||
|
while( remainder > PK3_SEEK_BUFFER_SIZE ) {
|
||||||
|
FS_Read( buffer, PK3_SEEK_BUFFER_SIZE, f );
|
||||||
|
remainder -= PK3_SEEK_BUFFER_SIZE;
|
||||||
|
}
|
||||||
|
FS_Read( buffer, remainder, f );
|
||||||
|
return offset;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
Com_Error( ERR_FATAL, "Bad origin in FS_Seek\n" );
|
||||||
|
return -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
FILE *file;
|
FILE *file;
|
||||||
file = FS_FileForHandle(f);
|
file = FS_FileForHandle(f);
|
||||||
|
|
Loading…
Reference in a new issue