From fbe0e2504ada264b4150839a4e7a3c44bf46e727 Mon Sep 17 00:00:00 2001 From: Ted John Date: Thu, 9 Feb 2017 21:32:17 +0000 Subject: [PATCH] Do not use RWops for saving track designs --- src/openrct2/core/File.cpp | 30 +++++++++++++++++++++++++-- src/openrct2/core/File.h | 1 + src/openrct2/ride/track_design.c | 2 +- src/openrct2/ride/track_design_save.c | 9 ++------ src/openrct2/util/util.h | 1 + 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/openrct2/core/File.cpp b/src/openrct2/core/File.cpp index 7b48e66a3c..f3dd67396d 100644 --- a/src/openrct2/core/File.cpp +++ b/src/openrct2/core/File.cpp @@ -60,13 +60,39 @@ namespace File *length = (size_t)fsize; return result; } + + void WriteAllBytes(const std::string &path, const void * buffer, size_t length) + { + auto fs = FileStream(path, FILE_MODE_WRITE); + fs.Write(buffer, length); + } } extern "C" { bool readentirefile(const utf8 * path, void * * outBuffer, size_t * outLength) { - *outBuffer = File::ReadAllBytes(String::ToStd(path), outLength); - return (*outBuffer != nullptr); + try + { + *outBuffer = File::ReadAllBytes(String::ToStd(path), outLength); + return true; + } + catch (const Exception &) + { + return false; + } + } + + bool writeentirefile(const utf8 * path, const void * buffer, size_t length) + { + try + { + File::WriteAllBytes(String::ToStd(path), buffer, length); + return true; + } + catch (const Exception &) + { + return false; + } } } diff --git a/src/openrct2/core/File.h b/src/openrct2/core/File.h index 88e7128f01..0110bc50da 100644 --- a/src/openrct2/core/File.h +++ b/src/openrct2/core/File.h @@ -25,4 +25,5 @@ namespace File bool Delete(const std::string &path); bool Move(const std::string &srcPath, const std::string &dstPath); void * ReadAllBytes(const std::string &path, size_t * length); + void WriteAllBytes(const std::string &path, const void * buffer, size_t length); } diff --git a/src/openrct2/ride/track_design.c b/src/openrct2/ride/track_design.c index 981d0c9ecb..de368e5b78 100644 --- a/src/openrct2/ride/track_design.c +++ b/src/openrct2/ride/track_design.c @@ -74,7 +74,7 @@ rct_track_td6 *track_design_open(const utf8 *path) uint8 *buffer; size_t bufferLength; - if (readentirefile(path, &buffer, &bufferLength)) { + if (readentirefile(path, (void * *)&buffer, &bufferLength)) { if (!sawyercoding_validate_track_checksum(buffer, bufferLength)) { log_error("Track checksum failed. %s", path); free(buffer); diff --git a/src/openrct2/ride/track_design_save.c b/src/openrct2/ride/track_design_save.c index 406caff4ed..0066e2b4a0 100644 --- a/src/openrct2/ride/track_design_save.c +++ b/src/openrct2/ride/track_design_save.c @@ -1275,14 +1275,9 @@ bool track_design_save_to_file(const utf8 *path) // Save encoded TD6 data to file bool result; log_verbose("saving track %s", path); - SDL_RWops *file = SDL_RWFromFile(path, "wb"); - if (file != NULL) { - SDL_RWwrite(file, encodedData, encodedDataLength, 1); - SDL_RWclose(file); - result = true; - } else { + result = writeentirefile(path, encodedData, encodedDataLength); + if (!result) { log_error("Failed to save %s", path); - result = false; } free(encodedData); diff --git a/src/openrct2/util/util.h b/src/openrct2/util/util.h index 34d00a35b7..b0593a9237 100644 --- a/src/openrct2/util/util.h +++ b/src/openrct2/util/util.h @@ -34,6 +34,7 @@ void path_append_extension(utf8 *path, const utf8 *newExtension, size_t size); void path_remove_extension(utf8 *path); void path_end_with_separator(utf8 *path, size_t size); bool readentirefile(const utf8 *path, void **outBuffer, size_t *outLength); +bool writeentirefile(const utf8 * path, const void * buffer, size_t length); sint32 bitscanforward(sint32 source); void bitcount_init();