From 075de8f837b4c08a5f359431e868d4cf1a23e9a7 Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Mon, 13 Feb 2023 11:51:09 +0300 Subject: [PATCH] Review sound IO functions --- src/audio.cc | 2 +- src/audio.h | 2 +- src/audio_file.cc | 2 +- src/audio_file.h | 2 +- src/game_sound.cc | 6 ++-- src/platform_compat.cc | 15 -------- src/platform_compat.h | 3 -- src/sound.cc | 70 ++++++++++++++++++++++++++++++++++---- src/sound.h | 2 +- src/sound_effects_cache.cc | 2 +- src/sound_effects_cache.h | 2 +- 11 files changed, 73 insertions(+), 35 deletions(-) diff --git a/src/audio.cc b/src/audio.cc index 4c80182..ee71201 100644 --- a/src/audio.cc +++ b/src/audio.cc @@ -58,7 +58,7 @@ static int audioSoundDecoderReadHandler(void* data, void* buffer, unsigned int s // AudioOpen // 0x41A2EC -int audioOpen(const char* fname, int flags, ...) +int audioOpen(const char* fname, int flags) { char path[80]; snprintf(path, sizeof(path), "%s", fname); diff --git a/src/audio.h b/src/audio.h index f916b16..2b96ac9 100644 --- a/src/audio.h +++ b/src/audio.h @@ -5,7 +5,7 @@ namespace fallout { typedef bool(AudioQueryCompressedFunc)(char* filePath); -int audioOpen(const char* fname, int mode, ...); +int audioOpen(const char* fname, int mode); int audioClose(int handle); int audioRead(int handle, void* buffer, unsigned int size); long audioSeek(int handle, long offset, int origin); diff --git a/src/audio_file.cc b/src/audio_file.cc index 30db375..d1dce84 100644 --- a/src/audio_file.cc +++ b/src/audio_file.cc @@ -57,7 +57,7 @@ static int audioFileSoundDecoderReadHandler(void* data, void* buffer, unsigned i } // 0x41A88C -int audioFileOpen(const char* fname, int flags, ...) +int audioFileOpen(const char* fname, int flags) { char path[COMPAT_MAX_PATH]; strcpy(path, fname); diff --git a/src/audio_file.h b/src/audio_file.h index 38c7a02..f00734b 100644 --- a/src/audio_file.h +++ b/src/audio_file.h @@ -5,7 +5,7 @@ namespace fallout { typedef bool(AudioFileQueryCompressedFunc)(char* filePath); -int audioFileOpen(const char* fname, int flags, ...); +int audioFileOpen(const char* fname, int flags); int audioFileClose(int handle); int audioFileRead(int handle, void* buf, unsigned int size); long audioFileSeek(int handle, long offset, int origin); diff --git a/src/game_sound.cc b/src/game_sound.cc index 3423f72..e7a838c 100644 --- a/src/game_sound.cc +++ b/src/game_sound.cc @@ -157,7 +157,7 @@ static int _gsound_speech_volume_get_set(int volume); static void speechPause(); static void speechResume(); static void _gsound_bkg_proc(); -static int gameSoundFileOpen(const char* fname, int access, ...); +static int gameSoundFileOpen(const char* fname, int access); static long _gsound_write_(); static long gameSoundFileTellNotImplemented(int handle); static int gameSoundFileWrite(int handle, const void* buf, unsigned int size); @@ -898,7 +898,7 @@ int speechLoad(const char* fname, int a2, int a3, int a4) return -1; } - if (soundSetFileIO(gSpeechSound, &audioOpen, &audioClose, &audioRead, NULL, &audioSeek, &gameSoundFileTellNotImplemented, &audioGetSize)) { + if (soundSetFileIO(gSpeechSound, audioOpen, audioClose, audioRead, NULL, audioSeek, gameSoundFileTellNotImplemented, audioGetSize)) { if (gGameSoundDebugEnabled) { debugPrint("failed because file IO could not be set for compression.\n"); } @@ -1548,7 +1548,7 @@ void _gsound_bkg_proc() } // 0x451A08 -int gameSoundFileOpen(const char* fname, int flags, ...) +int gameSoundFileOpen(const char* fname, int flags) { if ((flags & 2) != 0) { return -1; diff --git a/src/platform_compat.cc b/src/platform_compat.cc index ba49610..339739c 100644 --- a/src/platform_compat.cc +++ b/src/platform_compat.cc @@ -187,21 +187,6 @@ void compat_makepath(char* path, const char* drive, const char* dir, const char* #endif } -int compat_read(int fileHandle, void* buf, unsigned int size) -{ - return read(fileHandle, buf, size); -} - -int compat_write(int fileHandle, const void* buf, unsigned int size) -{ - return write(fileHandle, buf, size); -} - -long compat_lseek(int fileHandle, long offset, int origin) -{ - return lseek(fileHandle, offset, origin); -} - long compat_tell(int fd) { return lseek(fd, 0, SEEK_CUR); diff --git a/src/platform_compat.h b/src/platform_compat.h index 4da0706..be88974 100644 --- a/src/platform_compat.h +++ b/src/platform_compat.h @@ -29,9 +29,6 @@ char* compat_strlwr(char* string); char* compat_itoa(int value, char* buffer, int radix); void compat_splitpath(const char* path, char* drive, char* dir, char* fname, char* ext); void compat_makepath(char* path, const char* drive, const char* dir, const char* fname, const char* ext); -int compat_read(int fileHandle, void* buf, unsigned int size); -int compat_write(int fileHandle, const void* buf, unsigned int size); -long compat_lseek(int fileHandle, long offset, int origin); long compat_tell(int fileHandle); long compat_filelength(int fd); int compat_mkdir(const char* path); diff --git a/src/sound.cc b/src/sound.cc index 3881a68..353b605 100644 --- a/src/sound.cc +++ b/src/sound.cc @@ -45,6 +45,13 @@ typedef struct FadeSound { static void* soundMallocProcDefaultImpl(size_t size); static void* soundReallocProcDefaultImpl(void* ptr, size_t size); static void soundFreeProcDefaultImpl(void* ptr); +static long soundFileSize(int fileHandle); +static long soundTellData(int fileHandle); +static int soundWriteData(int fileHandle, const void* buf, unsigned int size); +static int soundReadData(int fileHandle, void* buf, unsigned int size); +static int soundOpenData(const char* filePath, int flags); +static long soundSeekData(int fileHandle, long offset, int origin); +static int soundCloseData(int fileHandle); static char* soundFileManglerDefaultImpl(char* fname); static void _refreshSoundBuffers(Sound* sound); static int _preloadBuffers(Sound* sound); @@ -77,13 +84,13 @@ static FreeProc* gSoundFreeProc = soundFreeProcDefaultImpl; // 0x51D494 static SoundFileIO gSoundDefaultFileIO = { - open, - close, - compat_read, - compat_write, - compat_lseek, - compat_tell, - compat_filelength, + soundOpenData, + soundCloseData, + soundReadData, + soundWriteData, + soundSeekData, + soundTellData, + soundFileSize, -1, }; @@ -184,6 +191,55 @@ void soundSetMemoryProcs(MallocProc* mallocProc, ReallocProc* reallocProc, FreeP gSoundFreeProc = freeProc; } +// 0x4AC71C +static long soundFileSize(int fileHandle) +{ + long pos; + long size; + + pos = compat_tell(fileHandle); + size = lseek(fileHandle, 0, SEEK_END); + lseek(fileHandle, pos, SEEK_SET); + + return size; +} + +// 0x4AC750 +static long soundTellData(int fileHandle) +{ + return compat_tell(fileHandle); +} + +// 0x4AC758 +static int soundWriteData(int fileHandle, const void* buf, unsigned int size) +{ + return write(fileHandle, buf, size); +} + +// 0x4AC760 +static int soundReadData(int fileHandle, void* buf, unsigned int size) +{ + return read(fileHandle, buf, size); +} + +// 0x4AC768 +static int soundOpenData(const char* filePath, int flags) +{ + return open(filePath, flags); +} + +// 0x4AC774 +static long soundSeekData(int fileHandle, long offset, int origin) +{ + return lseek(fileHandle, offset, origin); +} + +// 0x4AC77C +static int soundCloseData(int fileHandle) +{ + return close(fileHandle); +} + // 0x4AC78C char* soundFileManglerDefaultImpl(char* fname) { diff --git a/src/sound.h b/src/sound.h index 57d371c..ab81e63 100644 --- a/src/sound.h +++ b/src/sound.h @@ -46,7 +46,7 @@ typedef enum SoundError { SOUND_ERR_COUNT, } SoundError; -typedef int SoundOpenProc(const char* filePath, int flags, ...); +typedef int SoundOpenProc(const char* filePath, int flags); typedef int SoundCloseProc(int fileHandle); typedef int SoundReadProc(int fileHandle, void* buf, unsigned int size); typedef int SoundWriteProc(int fileHandle, const void* buf, unsigned int size); diff --git a/src/sound_effects_cache.cc b/src/sound_effects_cache.cc index d11603f..26aead6 100644 --- a/src/sound_effects_cache.cc +++ b/src/sound_effects_cache.cc @@ -154,7 +154,7 @@ void soundEffectsCacheFlush() // sfxc_cached_open // 0x4A915C -int soundEffectsCacheFileOpen(const char* fname, int mode, ...) +int soundEffectsCacheFileOpen(const char* fname, int mode) { if (_sfxc_files_open >= SOUND_EFFECTS_MAX_COUNT) { return -1; diff --git a/src/sound_effects_cache.h b/src/sound_effects_cache.h index 3650bcd..e6e0972 100644 --- a/src/sound_effects_cache.h +++ b/src/sound_effects_cache.h @@ -11,7 +11,7 @@ int soundEffectsCacheInit(int cache_size, const char* effectsPath); void soundEffectsCacheExit(); int soundEffectsCacheInitialized(); void soundEffectsCacheFlush(); -int soundEffectsCacheFileOpen(const char* fname, int mode, ...); +int soundEffectsCacheFileOpen(const char* fname, int mode); int soundEffectsCacheFileClose(int handle); int soundEffectsCacheFileRead(int handle, void* buf, unsigned int size); int soundEffectsCacheFileWrite(int handle, const void* buf, unsigned int size);