optimise writing of checksum on save scenario

This commit is contained in:
IntelOrca 2014-11-28 18:00:18 +00:00
parent 0a51752bd9
commit 35145f897b
1 changed files with 9 additions and 12 deletions

View File

@ -885,7 +885,7 @@ int scenario_save(char *path, int flags)
s6Header->version = S6_RCT2_VERSION;
s6Header->magic_number = S6_MAGIC_NUMBER;
file = fopen(path, "wb");
file = fopen(path, "wb+");
if (file == NULL) {
log_error("Unable to write to %s", path);
return 0;
@ -994,22 +994,19 @@ int scenario_save(char *path, int flags)
free(buffer);
// Due to the buffered writing and the writing of packed objects, there is no single buffer to calculate the checksum.
// Therefore the file is closed, opened, closed and opened again to save the checksum. Inefficient, but will do until
// the chunk writing code is better.
fclose(file);
// Determine number of bytes written
fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
file = fopen(path, "rb");
fileSize = fsize(file);
// Read all written bytes back into a single buffer
buffer = malloc(fileSize);
fread(buffer, fileSize, 1, file);
fclose(file);
checksum = sawyercoding_calculate_checksum(buffer, fileSize);
free(buffer);
fopen(path, "wb");
fwrite(buffer, fileSize, 1, file);
fwrite(&checksum, sizeof(int), 1, file);
// Append the checksum
fseek(file, fileSize, SEEK_SET);
fwrite(&checksum, sizeof(uint32), 1, file);
fclose(file);
if (!(flags & 0x80000000))