Implement writing for RWops

This commit is contained in:
Alexander van Gessel 2017-09-20 14:20:54 +02:00
parent 7c71bab57c
commit 5f17e70741
5 changed files with 114 additions and 28 deletions

View file

@ -39,7 +39,8 @@ using scoped_ostream = std::unique_ptr<std::ostream>;
typedef std::unique_ptr<SDL_RWops, void(*)(SDL_RWops*)> rwops_ptr;
rwops_ptr load_RWops(const std::string &path);
rwops_ptr read_RWops(const std::string &path);
rwops_ptr write_RWops(const std::string &path);
/** An exception object used when an IO error occurs */
struct io_exception : public game::error {

View file

@ -17,18 +17,29 @@
#include "filesystem.hpp"
#include "log.hpp"
#include <algorithm>
static lg::log_domain log_filesystem("filesystem");
#define ERR_FS LOG_STREAM(err, log_filesystem)
namespace filesystem {
static Sint64 ifs_size (struct SDL_RWops * context);
static Sint64 SDLCALL ifs_seek(struct SDL_RWops *context, Sint64 offset, int whence);
static size_t SDLCALL ifs_read(struct SDL_RWops *context, void *ptr, size_t size, size_t maxnum);
static size_t SDLCALL ifs_write(struct SDL_RWops *context, const void *ptr, size_t size, size_t num);
static int SDLCALL ifs_close(struct SDL_RWops *context);
// Arbitrary numbers larger than 5
static const Uint32 read_type = 7;
static const Uint32 write_type = 8;
rwops_ptr load_RWops(const std::string &path) {
static Sint64 ifs_size (struct SDL_RWops * context);
static Sint64 ofs_size (struct SDL_RWops * context);
static Sint64 SDLCALL ifs_seek(struct SDL_RWops *context, Sint64 offset, int whence);
static Sint64 SDLCALL ofs_seek(struct SDL_RWops *context, Sint64 offset, int whence);
static size_t SDLCALL ifs_read(struct SDL_RWops *context, void *ptr, size_t size, size_t maxnum);
static size_t SDLCALL ofs_read(struct SDL_RWops *context, void *ptr, size_t size, size_t maxnum);
static size_t SDLCALL ifs_write(struct SDL_RWops *context, const void *ptr, size_t size, size_t num);
static size_t SDLCALL ofs_write(struct SDL_RWops *context, const void *ptr, size_t size, size_t num);
static int SDLCALL ifs_close(struct SDL_RWops *context);
static int SDLCALL ofs_close(struct SDL_RWops *context);
rwops_ptr read_RWops(const std::string &path) {
rwops_ptr rw(SDL_AllocRW(), &SDL_FreeRW);
rw->size = &ifs_size;
@ -37,11 +48,11 @@ rwops_ptr load_RWops(const std::string &path) {
rw->write = &ifs_write;
rw->close = &ifs_close;
rw->type = 7; // Random number that is larger than 5
rw->type = read_type;
scoped_istream ifs = istream_file(path);
if(!ifs) {
ERR_FS << "load_RWops: istream_file returned NULL on " << path << '\n';
ERR_FS << "read_RWops: istream_file returned NULL on " << path << '\n';
rw.reset();
return rw;
}
@ -50,7 +61,28 @@ rwops_ptr load_RWops(const std::string &path) {
return rw;
}
rwops_ptr write_RWops(const std::string &path) {
rwops_ptr rw(SDL_AllocRW(), &SDL_FreeRW);
rw->size = &ofs_size;
rw->seek = &ofs_seek;
rw->read = &ofs_read;
rw->write = &ofs_write;
rw->close = &ofs_close;
rw->type = write_type;
scoped_ostream ofs = ostream_file(path);
if(!ofs) {
ERR_FS << "write_RWops: ostream_file returned NULL on " << path << '\n';
rw.reset();
return rw;
}
rw->hidden.unknown.data1 = ofs.release();
return rw;
}
static Sint64 ifs_size (struct SDL_RWops * context) {
std::istream *ifs = static_cast<std::istream*>(context->hidden.unknown.data1);
@ -63,30 +95,38 @@ static Sint64 ifs_size (struct SDL_RWops * context) {
ifs->seekg(orig);
return len;
}
static Sint64 ofs_size (struct SDL_RWops * context) {
std::ostream *ofs = static_cast<std::ostream*>(context->hidden.unknown.data1);
std::streampos orig = ofs->tellp();
ofs->seekp(0, std::ios::end);
std::streampos len = ofs->tellp();
ofs->seekp(orig);
return len;
}
static Sint64 SDLCALL ifs_seek(struct SDL_RWops *context, Sint64 offset, int whence) {
std::ios_base::seekdir seekdir;
typedef std::pair<Sint64, std::ios_base::seekdir> offset_dir;
static offset_dir translate_seekdir(Sint64 offset, int whence){
switch(whence){
case RW_SEEK_SET:
seekdir = std::ios_base::beg;
if(offset < 0)
offset = 0;
break;
return std::make_pair(std::max(0l, offset), std::ios_base::beg);
case RW_SEEK_CUR:
seekdir = std::ios_base::cur;
break;
return std::make_pair(offset, std::ios_base::cur);
case RW_SEEK_END:
seekdir = std::ios_base::end;
if(offset > 0)
offset = 0;
break;
return std::make_pair(std::min(0l, offset), std::ios_base::end);
default:
assert(false);
throw "assertion ignored";
}
}
static Sint64 SDLCALL ifs_seek(struct SDL_RWops *context, Sint64 offset, int whence) {
std::ios_base::seekdir seekdir;
std::tie(offset, seekdir) = translate_seekdir(offset, whence);
std::istream *ifs = static_cast<std::istream*>(context->hidden.unknown.data1);
const std::ios_base::iostate saved_state = ifs->rdstate();
@ -100,6 +140,23 @@ static Sint64 SDLCALL ifs_seek(struct SDL_RWops *context, Sint64 offset, int whe
std::streamsize pos = ifs->tellg();
return static_cast<int>(pos);
}
static Sint64 SDLCALL ofs_seek(struct SDL_RWops *context, Sint64 offset, int whence) {
std::ios_base::seekdir seekdir;
std::tie(offset, seekdir) = translate_seekdir(offset, whence);
std::ostream *ofs = static_cast<std::ostream*>(context->hidden.unknown.data1);
const std::ios_base::iostate saved_state = ofs->rdstate();
ofs->seekp(offset, seekdir);
if(saved_state != ofs->rdstate() && offset < 0) {
ofs->clear(saved_state);
ofs->seekp(0, std::ios_base::beg);
}
std::streamsize pos = ofs->tellp();
return static_cast<int>(pos);
}
static size_t SDLCALL ifs_read(struct SDL_RWops *context, void *ptr, size_t size, size_t maxnum) {
std::istream *ifs = static_cast<std::istream*>(context->hidden.unknown.data1);
@ -114,11 +171,31 @@ static size_t SDLCALL ifs_read(struct SDL_RWops *context, void *ptr, size_t size
return static_cast<int>(num);
}
static size_t SDLCALL ofs_read(struct SDL_RWops * /*context*/, void * /*ptr*/, size_t /*size*/, size_t /*maxnum*/) {
SDL_SetError("Reading not implemented");
return 0;
}
static size_t SDLCALL ifs_write(struct SDL_RWops * /*context*/, const void * /*ptr*/, size_t /*size*/, size_t /*num*/) {
SDL_SetError("Writing not implemented");
return 0;
}
static size_t SDLCALL ofs_write(struct SDL_RWops *context, const void *ptr, size_t size, size_t num) {
std::ostream *ofs = static_cast<std::ostream*>(context->hidden.unknown.data1);
const std::streampos before = ofs->tellp();
ofs->write(static_cast<const char*>(ptr), num * size);
const std::streampos after = ofs->tellp();
const int written = (after - before) / size;
// Fail sticks unless we clear it. Bad is an actual I/O error
if(!ofs->bad())
ofs->clear();
return static_cast<int>(written);
}
static int SDLCALL ifs_close(struct SDL_RWops *context) {
if (context) {
std::istream *ifs = static_cast<std::istream*>(context->hidden.unknown.data1);
@ -127,5 +204,13 @@ static int SDLCALL ifs_close(struct SDL_RWops *context) {
}
return 0;
}
static int SDLCALL ofs_close(struct SDL_RWops *context) {
if (context) {
std::ostream *ofs = static_cast<std::ostream*>(context->hidden.unknown.data1);
delete ofs;
SDL_FreeRW(context);
}
return 0;
}
}

View file

@ -184,7 +184,7 @@ static TTF_Font* open_font_impl(const std::string & fname, int size) {
}
}
filesystem::rwops_ptr rwops = filesystem::load_RWops(name);
filesystem::rwops_ptr rwops = filesystem::read_RWops(name);
TTF_Font* font = TTF_OpenFontRW(rwops.release(), true, size); // SDL takes ownership of rwops
if(font == nullptr) {
ERR_FT << "Failed opening font: '" << fname << "'\n";

View file

@ -513,7 +513,7 @@ static std::string get_localized_path(const std::string& file, const std::string
// Load overlay image and compose it with the original surface.
static void add_localized_overlay(const std::string& ovr_file, surface& orig_surf)
{
filesystem::rwops_ptr rwops = filesystem::load_RWops(ovr_file);
filesystem::rwops_ptr rwops = filesystem::read_RWops(ovr_file);
surface ovr_surf = IMG_Load_RW(rwops.release(), true); // SDL takes ownership of rwops
if(ovr_surf.null()) {
return;
@ -537,7 +537,7 @@ static surface load_image_file(const image::locator& loc)
if(!loc_location.empty()) {
location = loc_location;
}
filesystem::rwops_ptr rwops = filesystem::load_RWops(location);
filesystem::rwops_ptr rwops = filesystem::read_RWops(location);
res = IMG_Load_RW(rwops.release(), true); // SDL takes ownership of rwops
// If there was no standalone localized image, check if there is an overlay.
if(!res.null() && loc_location.empty()) {
@ -1217,7 +1217,7 @@ bool save_image(const surface& surf, const std::string& filename)
LOG_DP << "Writing a png image to " << filename << std::endl;
surface tmp = SDL_PNGFormatAlpha(surf.get());
// SDL_SavePNG_RW(tmp, filesystem::load_RWops(filename).release(), 1); //1 means to close the file (RWops) when we finish
// SDL_SavePNG_RW(tmp, filesystem::write_RWops(filename).release(), 1); //1 means to close the file (RWops) when we finish
//^ This doesn't work, load_RWops is only for reading not writing
return SDL_SavePNG(tmp, filename.c_str()) == 0;
}

View file

@ -601,7 +601,7 @@ static void play_new_music()
if(itor == music_cache.end()) {
LOG_AUDIO << "attempting to insert track '" << filename << "' into cache\n";
filesystem::rwops_ptr rwops = filesystem::load_RWops(filename);
filesystem::rwops_ptr rwops = filesystem::read_RWops(filename);
// SDL takes ownership of rwops
const std::shared_ptr<Mix_Music> music(Mix_LoadMUSType_RW(rwops.release(), MUS_NONE, true), &Mix_FreeMusic);
@ -882,7 +882,7 @@ static Mix_Chunk* load_chunk(const std::string& file, channel_group group)
const std::string& filename = filesystem::get_binary_file_location("sounds", file);
if(!filename.empty()) {
filesystem::rwops_ptr rwops = filesystem::load_RWops(filename);
filesystem::rwops_ptr rwops = filesystem::read_RWops(filename);
temp_chunk.set_data(Mix_LoadWAV_RW(rwops.release(), true)); // SDL takes ownership of rwops
} else {
ERR_AUDIO << "Could not load sound file '" << file << "'." << std::endl;