Do not use RWops for saving track designs

This commit is contained in:
Ted John 2017-02-09 21:32:17 +00:00
parent fb7f0a21d2
commit fbe0e2504a
5 changed files with 33 additions and 10 deletions

View File

@ -60,13 +60,39 @@ namespace File
*length = (size_t)fsize; *length = (size_t)fsize;
return result; 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" extern "C"
{ {
bool readentirefile(const utf8 * path, void * * outBuffer, size_t * outLength) bool readentirefile(const utf8 * path, void * * outBuffer, size_t * outLength)
{ {
*outBuffer = File::ReadAllBytes(String::ToStd(path), outLength); try
return (*outBuffer != nullptr); {
*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;
}
} }
} }

View File

@ -25,4 +25,5 @@ namespace File
bool Delete(const std::string &path); bool Delete(const std::string &path);
bool Move(const std::string &srcPath, const std::string &dstPath); bool Move(const std::string &srcPath, const std::string &dstPath);
void * ReadAllBytes(const std::string &path, size_t * length); void * ReadAllBytes(const std::string &path, size_t * length);
void WriteAllBytes(const std::string &path, const void * buffer, size_t length);
} }

View File

@ -74,7 +74,7 @@ rct_track_td6 *track_design_open(const utf8 *path)
uint8 *buffer; uint8 *buffer;
size_t bufferLength; size_t bufferLength;
if (readentirefile(path, &buffer, &bufferLength)) { if (readentirefile(path, (void * *)&buffer, &bufferLength)) {
if (!sawyercoding_validate_track_checksum(buffer, bufferLength)) { if (!sawyercoding_validate_track_checksum(buffer, bufferLength)) {
log_error("Track checksum failed. %s", path); log_error("Track checksum failed. %s", path);
free(buffer); free(buffer);

View File

@ -1275,14 +1275,9 @@ bool track_design_save_to_file(const utf8 *path)
// Save encoded TD6 data to file // Save encoded TD6 data to file
bool result; bool result;
log_verbose("saving track %s", path); log_verbose("saving track %s", path);
SDL_RWops *file = SDL_RWFromFile(path, "wb"); result = writeentirefile(path, encodedData, encodedDataLength);
if (file != NULL) { if (!result) {
SDL_RWwrite(file, encodedData, encodedDataLength, 1);
SDL_RWclose(file);
result = true;
} else {
log_error("Failed to save %s", path); log_error("Failed to save %s", path);
result = false;
} }
free(encodedData); free(encodedData);

View File

@ -34,6 +34,7 @@ void path_append_extension(utf8 *path, const utf8 *newExtension, size_t size);
void path_remove_extension(utf8 *path); void path_remove_extension(utf8 *path);
void path_end_with_separator(utf8 *path, size_t size); void path_end_with_separator(utf8 *path, size_t size);
bool readentirefile(const utf8 *path, void **outBuffer, size_t *outLength); 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); sint32 bitscanforward(sint32 source);
void bitcount_init(); void bitcount_init();