From 5820948761166e345fa793660ba7a5ba8d89526a Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Sun, 2 May 2021 01:28:34 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=20Make=20`SFileRw`=20own?= =?UTF-8?q?=20the=20Storm=20file=20handle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit All of our use-cases for `SDL_RWops` Storm file require closing the file when closing the `SDL_RWops` -- doing this automatically simplifies the code. --- Source/sound.cpp | 25 ++++++++++--------------- Source/storm/storm_sdl_rw.cpp | 1 + Source/storm/storm_sdl_rw.h | 2 +- Source/utils/soundsample.cpp | 16 +++------------- Source/utils/soundsample.h | 4 +--- 5 files changed, 16 insertions(+), 32 deletions(-) diff --git a/Source/sound.cpp b/Source/sound.cpp index 9d8b1af7e..b2783f364 100644 --- a/Source/sound.cpp +++ b/Source/sound.cpp @@ -26,8 +26,6 @@ namespace devilution { bool gbSndInited; -/** Handle to the music Storm file. */ -HANDLE sghMusic; /** The active background music track id. */ _music_id sgnMusicTrack = NUM_MUSIC; @@ -39,16 +37,15 @@ std::optional music; char *musicBuffer; #endif -void LoadMusic() +void LoadMusic(HANDLE handle) { #ifndef DISABLE_STREAMING_MUSIC - SDL_RWops *musicRw = SFileRw_FromStormHandle(sghMusic); + SDL_RWops *musicRw = SFileRw_FromStormHandle(handle); #else - int bytestoread = SFileGetFileSize(sghMusic, 0); + int bytestoread = SFileGetFileSize(handle, 0); musicBuffer = (char *)DiabloAllocPtr(bytestoread); - SFileReadFile(sghMusic, musicBuffer, bytestoread, NULL, 0); - SFileCloseFile(sghMusic); - sghMusic = NULL; + SFileReadFile(handle, musicBuffer, bytestoread, NULL, 0); + SFileCloseFile(handle); SDL_RWops *musicRw = SDL_RWFromConstMem(musicBuffer, bytestoread); #endif @@ -60,10 +57,7 @@ void CleanupMusic() { music = std::nullopt; sgnMusicTrack = NUM_MUSIC; -#ifndef DISABLE_STREAMING_MUSIC - SFileCloseFile(sghMusic); - sghMusic = nullptr; -#else +#ifdef DISABLE_STREAMING_MUSIC if (musicBuffer != nullptr) { mem_free_dbg(musicBuffer); musicBuffer = nullptr; @@ -246,11 +240,12 @@ void music_start(uint8_t nTrack) trackPath = sgszSpawnMusicTracks[nTrack]; else trackPath = sgszMusicTracks[nTrack]; - success = SFileOpenFile(trackPath, &sghMusic); + HANDLE handle; + success = SFileOpenFile(trackPath, &handle); if (!success) { - sghMusic = nullptr; + handle = nullptr; } else { - LoadMusic(); + LoadMusic(handle); if (!music->open()) { LogError(LogCategory::Audio, "Aulib::Stream::open (from music_start): {}", SDL_GetError()); CleanupMusic(); diff --git a/Source/storm/storm_sdl_rw.cpp b/Source/storm/storm_sdl_rw.cpp index aadb57c93..78a75a7f2 100644 --- a/Source/storm/storm_sdl_rw.cpp +++ b/Source/storm/storm_sdl_rw.cpp @@ -71,6 +71,7 @@ static int SFileRwRead(struct SDL_RWops *context, void *ptr, int size, int maxnu static int SFileRwClose(struct SDL_RWops *context) { + SFileCloseFile(SFileRwGetHandle(context)); delete context; return 0; } diff --git a/Source/storm/storm_sdl_rw.h b/Source/storm/storm_sdl_rw.h index d380d9155..8727c6106 100644 --- a/Source/storm/storm_sdl_rw.h +++ b/Source/storm/storm_sdl_rw.h @@ -9,7 +9,7 @@ namespace devilution { /** * @brief Creates a read-only SDL_RWops from a Storm file handle. * - * Does not close the handle when it gets closed. + * Closes the handle when it gets closed. */ SDL_RWops *SFileRw_FromStormHandle(HANDLE handle); diff --git a/Source/utils/soundsample.cpp b/Source/utils/soundsample.cpp index c74a5150b..e6e960552 100644 --- a/Source/utils/soundsample.cpp +++ b/Source/utils/soundsample.cpp @@ -20,20 +20,11 @@ namespace devilution { -SoundSample::~SoundSample() -{ - if (file_handle_ != nullptr) - SFileCloseFile(file_handle_); -} - void SoundSample::Release() { stream_ = nullptr; file_data_ = nullptr; file_data_size_ = 0; - if (file_handle_ != nullptr) - SFileCloseFile(file_handle_); - file_handle_ = nullptr; }; /** @@ -78,17 +69,16 @@ void SoundSample::Stop() int SoundSample::SetChunkStream(std::string filePath) { file_path_ = std::move(filePath); - if (!SFileOpenFile(file_path_.c_str(), &file_handle_)) { + HANDLE handle; + if (!SFileOpenFile(file_path_.c_str(), &handle)) { LogError(LogCategory::Audio, "SFileOpenFile failed (from SoundSample::SetChunkStream): {}", SErrGetLastError()); return -1; } - stream_ = std::make_unique(SFileRw_FromStormHandle(file_handle_), std::make_unique(), + stream_ = std::make_unique(SFileRw_FromStormHandle(handle), std::make_unique(), std::make_unique(sgOptions.Audio.nResamplingQuality), /*closeRw=*/true); if (!stream_->open()) { stream_ = nullptr; - SFileCloseFile(file_handle_); - file_handle_ = nullptr; LogError(LogCategory::Audio, "Aulib::Stream::open (from SoundSample::SetChunkStream): {}", SDL_GetError()); return -1; } diff --git a/Source/utils/soundsample.h b/Source/utils/soundsample.h index e60e4604d..aa6622502 100644 --- a/Source/utils/soundsample.h +++ b/Source/utils/soundsample.h @@ -16,7 +16,6 @@ public: SoundSample() = default; SoundSample(SoundSample &&) noexcept = default; SoundSample &operator=(SoundSample &&) noexcept = default; - ~SoundSample(); void Release(); bool IsPlaying(); @@ -51,8 +50,7 @@ private: ArraySharedPtr file_data_; std::size_t file_data_size_; - // Streaming audio fields: - HANDLE file_handle_ = nullptr; + // Set for streaming audio to allow for duplicating it: std::string file_path_; std::unique_ptr stream_;