Browse Source

Always use SDL wrapper for file access

pull/3185/head
Anders Jenbo 5 years ago
parent
commit
8df5912dc6
  1. 7
      Source/init.cpp
  2. 36
      Source/sound.cpp
  3. 28
      Source/storm/storm_file_wrapper.cpp
  4. 4
      Source/storm/storm_file_wrapper.h
  5. 4
      Source/storm/storm_sdl_rw.cpp
  6. 9
      Source/storm/storm_sdl_rw.h
  7. 11
      Source/storm/storm_svid.cpp
  8. 9
      Source/utils/soundsample.cpp

7
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)

36
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<Aulib::Stream> music;
std::unique_ptr<char[]> 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<Aulib::DecoderDrwav>(),
music.emplace(handle, std::make_unique<Aulib::DecoderDrwav>(),
std::make_unique<Aulib::ResamplerSpeex>(sgOptions.Audio.nResamplingQuality), /*closeRw=*/true);
}
@ -166,15 +163,15 @@ std::unique_ptr<TSnd> 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<std::uint8_t>(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());

28
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<HANDLE>(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<SDL_RWops *>(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<HANDLE>(cookie), *pos, swhence);
if (spos == static_cast<std::uint64_t>(-1)) {
Log("SFileRwSeek error: {}", SErrGetLastError());
const Sint64 spos = SDL_RWseek(static_cast<SDL_RWops *>(cookie), *pos, swhence);
if (spos < 0) {
Log("SFileRwSeek error: {}", SDL_GetError());
return -1;
}
*pos = static_cast<off64_t>(spos);
@ -49,13 +45,13 @@ int SFileCookieSeek(void *cookie, off64_t *pos, int whence)
int SFileCookieClose(void *cookie)
{
return SFileCloseFileThreadSafe(static_cast<HANDLE>(cookie)) ? 0 : -1;
return SDL_RWclose(static_cast<SDL_RWops *>(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;

4
Source/storm/storm_file_wrapper.h

@ -3,14 +3,14 @@
#if (defined(__linux__) && !defined(__ANDROID__)) || defined(__FreeBSD__) || defined(__DragonFly__) || defined(__HAIKU__)
#include <cstdio>
#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

4
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__

9
Source/storm/storm_sdl_rw.h

@ -2,17 +2,8 @@
#include <SDL.h>
#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.
*

11
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<uint8_t[]> { 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) {

9
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<Aulib::Stream>(SFileRw_FromStormHandle(handle), std::make_unique<Aulib::DecoderDrwav>(),
stream_ = std::make_unique<Aulib::Stream>(handle, std::make_unique<Aulib::DecoderDrwav>(),
std::make_unique<Aulib::ResamplerSpeex>(sgOptions.Audio.nResamplingQuality), /*closeRw=*/true);
if (!stream_->open()) {
stream_ = nullptr;

Loading…
Cancel
Save