diff --git a/include/serialize.h b/include/serialize.h index eeffb46..5e8af3e 100644 --- a/include/serialize.h +++ b/include/serialize.h @@ -8,11 +8,13 @@ void writeBlockChangesToDisk (int from, int to); void writeChestChangesToDisk (uint8_t *storage_ptr, uint8_t slot); void writePlayerDataToDisk (); + void writeDataToDiskOnInterval (); #else // Define no-op placeholders for when disk syncing isn't enabled #define writeBlockChangesToDisk(a, b) #define writeChestChangesToDisk(a, b) #define writePlayerDataToDisk() + #define writeDataToDiskOnInterval() #define initSerializer() 0 #endif diff --git a/src/procedures.c b/src/procedures.c index 71ad0ae..faedde9 100644 --- a/src/procedures.c +++ b/src/procedures.c @@ -1483,11 +1483,8 @@ void handleServerTick (int64_t time_since_last_tick) { sc_setHealth(player->client_fd, player->health, player->hunger, player->saturation); } - // Write data to file (if applicable) - writePlayerDataToDisk(); - #ifdef DISK_SYNC_BLOCKS_ON_INTERVAL - writeBlockChangesToDisk(0, block_changes_count); - #endif + // Perform regular checks for if it's time to write to disk + writeDataToDiskOnInterval(); /** * If the RNG seed ever hits 0, it'll never generate anything diff --git a/src/serialize.c b/src/serialize.c index 5ba2cf2..9c822a1 100644 --- a/src/serialize.c +++ b/src/serialize.c @@ -115,12 +115,6 @@ int initSerializer () { // Writes a range of block change entries to disk void writeBlockChangesToDisk (int from, int to) { - #ifdef DISK_SYNC_BLOCKS_ON_INTERVAL - // Skip this write if enough time hasn't passed since the last one - if (get_program_time() - last_disk_sync_time < DISK_SYNC_INTERVAL) return; - last_disk_sync_time = get_program_time(); - #endif - // Try to open the file in rw (without overwriting) FILE *file = fopen(FILE_PATH, "r+b"); if (!file) { @@ -149,10 +143,6 @@ void writeBlockChangesToDisk (int from, int to) { // Writes all player data to disk void writePlayerDataToDisk () { - // Skip this write if enough time hasn't passed since the last one - if (get_program_time() - last_disk_sync_time < DISK_SYNC_INTERVAL) return; - last_disk_sync_time = get_program_time(); - // Try to open the file in rw (without overwriting) FILE *file = fopen(FILE_PATH, "r+b"); if (!file) { @@ -176,6 +166,21 @@ void writePlayerDataToDisk () { fclose(file); } +// Writes data queued for interval writes, but only if enough time has passed +void writeDataToDiskOnInterval () { + + // Skip this write if enough time hasn't passed since the last one + if (get_program_time() - last_disk_sync_time < DISK_SYNC_INTERVAL) return; + last_disk_sync_time = get_program_time(); + + // Write full player data and block changes buffers + writePlayerDataToDisk(); + #ifdef DISK_SYNC_BLOCKS_ON_INTERVAL + writeBlockChangesToDisk(0, block_changes_count); + #endif + +} + #ifdef ALLOW_CHESTS // Writes a chest slot change to disk void writeChestChangesToDisk (uint8_t *storage_ptr, uint8_t slot) {