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