From 1a791e855653ab74734a33af343e2d2a0ce87411 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sat, 15 Mar 2025 19:36:34 +0300 Subject: [PATCH] engine: soundlib: get rid of useless formatstring, which is always the same for all types --- engine/client/soundlib/snd_main.c | 57 ++++++++++++++++++------------ engine/common/soundlib/snd_utils.c | 38 +++++++++----------- engine/common/soundlib/soundlib.h | 2 -- 3 files changed, 52 insertions(+), 45 deletions(-) diff --git a/engine/client/soundlib/snd_main.c b/engine/client/soundlib/snd_main.c index 8553405f..57761ae0 100644 --- a/engine/client/soundlib/snd_main.c +++ b/engine/client/soundlib/snd_main.c @@ -56,12 +56,10 @@ loading and unpack to wav any known sound */ wavdata_t *FS_LoadSound( const char *filename, const byte *buffer, size_t size ) { - const char *ext = COM_FileExtension( filename ); - string path, loadname; - qboolean anyformat = true; - fs_offset_t filesize = 0; - const loadwavfmt_t *format; - byte *f; + const char *ext = COM_FileExtension( filename ); + string loadname; + qboolean anyformat = true; + const loadwavfmt_t *format; Sound_Reset(); // clear old sounddata Q_strncpy( loadname, filename, sizeof( loadname )); @@ -70,7 +68,7 @@ wavdata_t *FS_LoadSound( const char *filename, const byte *buffer, size_t size ) { // we needs to compare file extension with list of supported formats // and be sure what is real extension, not a filename with dot - for( format = sound.loadformats; format && format->formatstring; format++ ) + for( format = sound.loadformats; format && format->ext; format++ ) { if( !Q_stricmp( format->ext, ext )) { @@ -86,28 +84,42 @@ wavdata_t *FS_LoadSound( const char *filename, const byte *buffer, size_t size ) goto load_internal; // now try all the formats in the selected list - for( format = sound.loadformats; format && format->formatstring; format++) + for( format = sound.loadformats; format && format->ext; format++) { if( anyformat || !Q_stricmp( ext, format->ext )) { - Q_snprintf( path, sizeof( path ), - format->formatstring, loadname, "", format->ext ); + qboolean success = false; + fs_offset_t filesize = 0; + byte *f; + string path; + + Q_snprintf( path, sizeof( path ), DEFAULT_SOUNDPATH "%s.%s", loadname, format->ext ); f = FS_LoadFile( path, &filesize, false ); if( f && filesize > 0 ) { - if( format->loadfunc( path, f, filesize )) - { - Mem_Free(f); // release buffer - return SoundPack(); // loaded - } - else Mem_Free(f); // release buffer + success = format->loadfunc( path, f, filesize ); + Mem_Free( f ); // release buffer } + + if( success ) + return SoundPack(); // loaded + + Q_snprintf( path, sizeof( path ), "%s.%s", loadname, format->ext ); + f = FS_LoadFile( path, &filesize, false ); + if( f && filesize > 0 ) + { + success = format->loadfunc( path, f, filesize ); + Mem_Free( f ); // release buffer + } + + if( success ) + return SoundPack(); } } load_internal: - for( format = sound.loadformats; format && format->formatstring; format++ ) + for( format = sound.loadformats; format && format->ext; format++ ) { if( anyformat || !Q_stricmp( ext, format->ext )) { @@ -148,7 +160,7 @@ open and reading basic info from sound stream stream_t *FS_OpenStream( const char *filename ) { const char *ext = COM_FileExtension( filename ); - string path, loadname; + string loadname; qboolean anyformat = true; const streamfmt_t *format; stream_t *stream = NULL; @@ -160,7 +172,7 @@ stream_t *FS_OpenStream( const char *filename ) { // we needs to compare file extension with list of supported formats // and be sure what is real extension, not a filename with dot - for( format = sound.streamformat; format && format->formatstring; format++ ) + for( format = sound.streamformat; format && format->ext; format++ ) { if( !Q_stricmp( format->ext, ext )) { @@ -172,12 +184,13 @@ stream_t *FS_OpenStream( const char *filename ) } // now try all the formats in the selected list - for( format = sound.streamformat; format && format->formatstring; format++) + for( format = sound.streamformat; format && format->ext; format++) { if( anyformat || !Q_stricmp( ext, format->ext )) { - Q_snprintf( path, sizeof( path ), - format->formatstring, loadname, "", format->ext ); + string path; + + Q_snprintf( path, sizeof( path ), "%s.%s", loadname, format->ext ); if(( stream = format->openfunc( path )) != NULL ) { diff --git a/engine/common/soundlib/snd_utils.c b/engine/common/soundlib/snd_utils.c index d9930ba9..4d6fe4d0 100644 --- a/engine/common/soundlib/snd_utils.c +++ b/engine/common/soundlib/snd_utils.c @@ -31,19 +31,15 @@ sndlib_t sound; static const loadwavfmt_t load_game[] = { #ifndef XASH_DEDICATED -{ DEFAULT_SOUNDPATH "%s%s.%s", "wav", Sound_LoadWAV }, -{ "%s%s.%s", "wav", Sound_LoadWAV }, -{ DEFAULT_SOUNDPATH "%s%s.%s", "mp3", Sound_LoadMPG }, -{ "%s%s.%s", "mp3", Sound_LoadMPG }, -{ DEFAULT_SOUNDPATH "%s%s.%s", "ogg", Sound_LoadOggVorbis }, -{ "%s%s.%s", "ogg", Sound_LoadOggVorbis }, -{ DEFAULT_SOUNDPATH "%s%s.%s", "opus", Sound_LoadOggOpus }, -{ "%s%s.%s", "opus", Sound_LoadOggOpus }, +{ "wav", Sound_LoadWAV }, +{ "mp3", Sound_LoadMPG }, +{ "ogg", Sound_LoadOggVorbis }, +{ "opus", Sound_LoadOggOpus }, #else // we only need extensions -{ NULL, "wav" }, -{ NULL, "mp3" }, -{ NULL, "ogg" }, -{ NULL, "opus" }, +{ "wav" }, +{ "mp3" }, +{ "ogg" }, +{ "opus" }, #endif { NULL }, }; @@ -58,15 +54,15 @@ static const loadwavfmt_t load_game[] = static const streamfmt_t stream_game[] = { #ifndef XASH_DEDICATED -{ "%s%s.%s", "mp3", Stream_OpenMPG, Stream_ReadMPG, Stream_SetPosMPG, Stream_GetPosMPG, Stream_FreeMPG }, -{ "%s%s.%s", "wav", Stream_OpenWAV, Stream_ReadWAV, Stream_SetPosWAV, Stream_GetPosWAV, Stream_FreeWAV }, -{ "%s%s.%s", "ogg", Stream_OpenOggVorbis, Stream_ReadOggVorbis, Stream_SetPosOggVorbis, Stream_GetPosOggVorbis, Stream_FreeOggVorbis }, -{ "%s%s.%s", "opus", Stream_OpenOggOpus, Stream_ReadOggOpus, Stream_SetPosOggOpus, Stream_GetPosOggOpus, Stream_FreeOggOpus }, +{ "mp3", Stream_OpenMPG, Stream_ReadMPG, Stream_SetPosMPG, Stream_GetPosMPG, Stream_FreeMPG }, +{ "wav", Stream_OpenWAV, Stream_ReadWAV, Stream_SetPosWAV, Stream_GetPosWAV, Stream_FreeWAV }, +{ "ogg", Stream_OpenOggVorbis, Stream_ReadOggVorbis, Stream_SetPosOggVorbis, Stream_GetPosOggVorbis, Stream_FreeOggVorbis }, +{ "opus", Stream_OpenOggOpus, Stream_ReadOggOpus, Stream_SetPosOggOpus, Stream_GetPosOggOpus, Stream_FreeOggOpus }, #else // we only need extensions -{ NULL, "mp3" }, -{ NULL, "wav" }, -{ NULL, "ogg" }, -{ NULL, "opus" }, +{ "mp3" }, +{ "wav" }, +{ "ogg" }, +{ "opus" }, #endif { NULL }, }; @@ -481,7 +477,7 @@ qboolean Sound_SupportedFileFormat( const char *fileext ) const loadwavfmt_t *format; if( COM_CheckStringEmpty( fileext )) { - for( format = sound.loadformats; format && format->formatstring; format++ ) + for( format = sound.loadformats; format && format->ext; format++ ) { if( !Q_stricmp( format->ext, fileext )) return true; diff --git a/engine/common/soundlib/soundlib.h b/engine/common/soundlib/soundlib.h index 651b3097..0e20c8c7 100644 --- a/engine/common/soundlib/soundlib.h +++ b/engine/common/soundlib/soundlib.h @@ -23,14 +23,12 @@ GNU General Public License for more details. typedef struct loadwavfmt_s { - const char *formatstring; const char *ext; qboolean (*loadfunc)( const char *name, const byte *buffer, fs_offset_t filesize ); } loadwavfmt_t; typedef struct streamfmt_s { - const char *formatstring; const char *ext; stream_t *(*openfunc)( const char *filename );