diff --git a/Source/init.cpp b/Source/init.cpp index e7a81a74f..25953bc7a 100644 --- a/Source/init.cpp +++ b/Source/init.cpp @@ -16,6 +16,7 @@ #include "dx.h" #include "pfile.h" #include "storm/storm.h" +#include "storm/storm_sdl_rw.h" #include "utils/language.h" #include "utils/log.hpp" #include "utils/paths.h" @@ -189,10 +190,10 @@ void init_archives() if (spawn_mpq != nullptr) gbIsSpawn = true; } - HANDLE fh = nullptr; - if (!SFileOpenFile("ui_art\\title.pcx", &fh)) + SDL_RWops *handle = SFileOpenRw("ui_art\\title.pcx"); + if (handle == nullptr) InsertCDDlg(); - SFileCloseFileThreadSafe(fh); + SDL_RWclose(handle); patch_rt_mpq = LoadMPQ(paths, "patch_rt.mpq"); if (patch_rt_mpq == nullptr) diff --git a/Source/sound.cpp b/Source/sound.cpp index 4538c648b..2a2b11610 100644 --- a/Source/sound.cpp +++ b/Source/sound.cpp @@ -18,7 +18,6 @@ #include "init.h" #include "options.h" -#include "storm/storm.h" #include "storm/storm_sdl_rw.h" #include "utils/log.hpp" #include "utils/math.h" @@ -46,19 +45,17 @@ std::optional music; std::unique_ptr musicBuffer; #endif -void LoadMusic(HANDLE handle) +void LoadMusic(SDL_RWops *handle) { -#ifndef DISABLE_STREAMING_MUSIC - SDL_RWops *musicRw = SFileRw_FromStormHandle(handle); -#else - size_t bytestoread = SFileGetFileSize(handle); +#ifdef DISABLE_STREAMING_MUSIC + size_t bytestoread = SDL_RWsize(handle); musicBuffer.reset(new char[bytestoread]); - SFileReadFileThreadSafe(handle, musicBuffer.get(), bytestoread); - SFileCloseFileThreadSafe(handle); + SDL_RWread(handle, musicBuffer.get(), bytestoread, 1); + SDL_RWclose(handle); - SDL_RWops *musicRw = SDL_RWFromConstMem(musicBuffer.get(), bytestoread); + handle = SDL_RWFromConstMem(musicBuffer.get(), bytestoread); #endif - music.emplace(musicRw, std::make_unique(), + music.emplace(handle, std::make_unique(), std::make_unique(sgOptions.Audio.nResamplingQuality), /*closeRw=*/true); } @@ -166,15 +163,15 @@ std::unique_ptr sound_file_load(const char *path, bool stream) } #ifndef STREAM_ALL_AUDIO } else { - HANDLE file; - if (!SFileOpenFile(path, &file)) { + SDL_RWops *file = SFileOpenRw(path); + if (path == nullptr) { ErrDlg("SFileOpenFile failed", path, __FILE__, __LINE__); } - size_t dwBytes = SFileGetFileSize(file); + size_t dwBytes = SDL_RWsize(file); auto waveFile = MakeArraySharedPtr(dwBytes); - SFileReadFileThreadSafe(file, waveFile.get(), dwBytes); + SDL_RWread(file, waveFile.get(), dwBytes, 1); int error = snd->DSB.SetChunk(waveFile, dwBytes); - SFileCloseFileThreadSafe(file); + SDL_RWclose(file); if (error != 0) { ErrSdl(); } @@ -231,7 +228,6 @@ void music_stop() void music_start(uint8_t nTrack) { - bool success; const char *trackPath; assert(nTrack < NUM_MUSIC); @@ -241,11 +237,9 @@ void music_start(uint8_t nTrack) trackPath = SpawnMusicTracks[nTrack]; else trackPath = MusicTracks[nTrack]; - HANDLE handle; - success = SFileOpenFile(trackPath, &handle); - if (!success) { - handle = nullptr; - } else { + + SDL_RWops *handle = SFileOpenRw(trackPath); + if (handle != nullptr) { LoadMusic(handle); if (!music->open()) { LogError(LogCategory::Audio, "Aulib::Stream::open (from music_start): {}", SDL_GetError()); diff --git a/Source/storm/storm_file_wrapper.cpp b/Source/storm/storm_file_wrapper.cpp index c1befa51f..ecf1f84ec 100644 --- a/Source/storm/storm_file_wrapper.cpp +++ b/Source/storm/storm_file_wrapper.cpp @@ -2,7 +2,6 @@ #ifdef DEVILUTIONX_STORM_FILE_WRAPPER_AVAILABLE -#include "storm/storm.h" #include "utils/log.hpp" namespace devilution { @@ -12,14 +11,11 @@ extern "C" { ssize_t SFileCookieRead(void *cookie, char *buf, size_t nbytes) { - size_t numRead = 0; - if (!SFileReadFileThreadSafe(static_cast(cookie), buf, nbytes, &numRead)) { - const auto errCode = SErrGetLastError(); - if (errCode != STORM_ERROR_HANDLE_EOF) { - Log("SFileRwRead error: {} ERROR CODE {}", (unsigned int)nbytes, (unsigned int)errCode); - } + size_t numRead = SDL_RWread(static_cast(cookie), buf, nbytes, 1); + if (numRead == 0) { + Log("SDL_RWread error: {} ERROR CODE {}", SDL_GetError(), numRead); } - return numRead; + return numRead * nbytes; } int SFileCookieSeek(void *cookie, off64_t *pos, int whence) @@ -27,20 +23,20 @@ int SFileCookieSeek(void *cookie, off64_t *pos, int whence) int swhence; switch (whence) { case SEEK_SET: - swhence = DVL_FILE_BEGIN; + swhence = RW_SEEK_SET; break; case SEEK_CUR: - swhence = DVL_FILE_CURRENT; + swhence = RW_SEEK_CUR; break; case SEEK_END: - swhence = DVL_FILE_END; + swhence = RW_SEEK_END; break; default: return -1; } - const std::uint64_t spos = SFileSetFilePointer(static_cast(cookie), *pos, swhence); - if (spos == static_cast(-1)) { - Log("SFileRwSeek error: {}", SErrGetLastError()); + const Sint64 spos = SDL_RWseek(static_cast(cookie), *pos, swhence); + if (spos < 0) { + Log("SFileRwSeek error: {}", SDL_GetError()); return -1; } *pos = static_cast(spos); @@ -49,13 +45,13 @@ int SFileCookieSeek(void *cookie, off64_t *pos, int whence) int SFileCookieClose(void *cookie) { - return SFileCloseFileThreadSafe(static_cast(cookie)) ? 0 : -1; + return SDL_RWclose(static_cast(cookie)); } } // extern "C" #endif -FILE *FILE_FromStormHandle(HANDLE handle) +FILE *FILE_FromStormHandle(SDL_RWops *handle) { #ifdef DEVILUTIONX_STORM_FILE_WRAPPER_IMPL_FOPENCOOKIE cookie_io_functions_t ioFns; diff --git a/Source/storm/storm_file_wrapper.h b/Source/storm/storm_file_wrapper.h index 3e204cbb3..761846944 100644 --- a/Source/storm/storm_file_wrapper.h +++ b/Source/storm/storm_file_wrapper.h @@ -3,14 +3,14 @@ #if (defined(__linux__) && !defined(__ANDROID__)) || defined(__FreeBSD__) || defined(__DragonFly__) || defined(__HAIKU__) #include -#include "miniwin/miniwin.h" +#include "storm/storm_sdl_rw.h" #define DEVILUTIONX_STORM_FILE_WRAPPER_AVAILABLE #define DEVILUTIONX_STORM_FILE_WRAPPER_IMPL_FOPENCOOKIE namespace devilution { -FILE *FILE_FromStormHandle(HANDLE handle); +FILE *FILE_FromStormHandle(SDL_RWops *handle); } // namespace devilution diff --git a/Source/storm/storm_sdl_rw.cpp b/Source/storm/storm_sdl_rw.cpp index 198c56e49..2a389a44f 100644 --- a/Source/storm/storm_sdl_rw.cpp +++ b/Source/storm/storm_sdl_rw.cpp @@ -9,6 +9,8 @@ namespace devilution { +namespace { + static HANDLE SFileRwGetHandle(struct SDL_RWops *context) { return (HANDLE)context->hidden.unknown.data1; @@ -96,6 +98,8 @@ SDL_RWops *SFileRw_FromStormHandle(HANDLE handle) return result; } +} // namespace + SDL_RWops *SFileOpenRw(const char *filename) { #ifdef __ANDROID__ diff --git a/Source/storm/storm_sdl_rw.h b/Source/storm/storm_sdl_rw.h index 16b800fa8..e3395609d 100644 --- a/Source/storm/storm_sdl_rw.h +++ b/Source/storm/storm_sdl_rw.h @@ -2,17 +2,8 @@ #include -#include "miniwin/miniwin.h" - namespace devilution { -/** - * @brief Creates a read-only SDL_RWops from a Storm file handle. - * - * Closes the handle when it gets closed. - */ -SDL_RWops *SFileRw_FromStormHandle(HANDLE handle); - /** * @brief Opens a Storm file and creates a read-only SDL_RWops from its handle. * diff --git a/Source/storm/storm_svid.cpp b/Source/storm/storm_svid.cpp index 4fc9c7ed5..e9e73365a 100644 --- a/Source/storm/storm_svid.cpp +++ b/Source/storm/storm_svid.cpp @@ -16,7 +16,7 @@ #include "dx.h" #include "options.h" #include "palette.h" -#include "storm/storm.h" +#include "storm/storm_sdl_rw.h" #include "storm/storm_file_wrapper.h" #include "utils/display.h" #include "utils/log.hpp" @@ -150,16 +150,15 @@ bool SVidPlayBegin(const char *filename, int flags) //0x800000 // Edge detection //0x200800 // Clear FB - HANDLE videoStream; - SFileOpenFile(filename, &videoStream); + SDL_RWops *videoStream = SFileOpenRw(filename); #ifdef DEVILUTIONX_STORM_FILE_WRAPPER_AVAILABLE FILE *file = FILE_FromStormHandle(videoStream); SVidSMK = smk_open_filepointer(file, SMK_MODE_DISK); #else - size_t bytestoread = SFileGetFileSize(videoStream); + size_t bytestoread = SDL_RWsize(videoStream); SVidBuffer = std::unique_ptr { new uint8_t[bytestoread] }; - SFileReadFileThreadSafe(videoStream, SVidBuffer.get(), bytestoread); - SFileCloseFileThreadSafe(videoStream); + SDL_RWread(videoStream, SVidBuffer.get(), bytestoread, 1); + SDL_RWclose(videoStream); SVidSMK = smk_open_memory(SVidBuffer.get(), bytestoread); #endif if (SVidSMK == nullptr) { diff --git a/Source/utils/soundsample.cpp b/Source/utils/soundsample.cpp index f7771b8e8..d037df26d 100644 --- a/Source/utils/soundsample.cpp +++ b/Source/utils/soundsample.cpp @@ -14,7 +14,6 @@ #endif #include "options.h" -#include "storm/storm.h" #include "storm/storm_sdl_rw.h" #include "utils/log.hpp" #include "utils/math.h" @@ -117,13 +116,13 @@ void SoundSample::Stop() int SoundSample::SetChunkStream(std::string filePath) { file_path_ = std::move(filePath); - HANDLE handle; - if (!SFileOpenFile(file_path_.c_str(), &handle)) { - LogError(LogCategory::Audio, "SFileOpenFile failed (from SoundSample::SetChunkStream): {}", SErrGetLastError()); + SDL_RWops *handle = SFileOpenRw(file_path_.c_str()); + if (handle == nullptr) { + LogError(LogCategory::Audio, "SFileOpenRw failed (from SoundSample::SetChunkStream): {}", SDL_GetError()); return -1; } - stream_ = std::make_unique(SFileRw_FromStormHandle(handle), std::make_unique(), + stream_ = std::make_unique(handle, std::make_unique(), std::make_unique(sgOptions.Audio.nResamplingQuality), /*closeRw=*/true); if (!stream_->open()) { stream_ = nullptr;