mirror of
https://github.com/p2r3/bareiron.git
synced 2025-10-01 23:25:09 +02:00
add option to sync block changes on an interval
This commit is contained in:
@@ -49,10 +49,15 @@
|
|||||||
#define SYNC_WORLD_TO_DISK
|
#define SYNC_WORLD_TO_DISK
|
||||||
#endif
|
#endif
|
||||||
// The minimum interval (in microseconds) at which certain data is written
|
// The minimum interval (in microseconds) at which certain data is written
|
||||||
// to disk/flash. Bounded on the low end by TIME_BETWEEN_TICKS. Currently
|
// to disk/flash. Bounded on the low end by TIME_BETWEEN_TICKS. By default,
|
||||||
// only applies to player data. Block changes are written as soon as they
|
// applies only to player data. Block changes are written as soon as they
|
||||||
// are made, but in much smaller portions.
|
// are made, but in much smaller portions. Set DISK_SYNC_BLOCKS_ON_INTERVAL
|
||||||
|
// to make this apply to block changes as well.
|
||||||
#define DISK_SYNC_INTERVAL 15000000
|
#define DISK_SYNC_INTERVAL 15000000
|
||||||
|
// Whether to sync block changes to disk on an interval, instead of syncing
|
||||||
|
// on each change. On systems with fast random disk access, this shouldn't
|
||||||
|
// be necessary.
|
||||||
|
// #define DISK_SYNC_BLOCKS_ON_INTERVAL
|
||||||
// Time in microseconds to spend waiting for data transmission before
|
// Time in microseconds to spend waiting for data transmission before
|
||||||
// timing out. Default is 15s, which leaves 5s to prevent starving other
|
// timing out. Default is 15s, which leaves 5s to prevent starving other
|
||||||
// clients from Keep Alive packets.
|
// clients from Keep Alive packets.
|
||||||
|
@@ -412,7 +412,9 @@ uint8_t makeBlockChange (short x, uint8_t y, short z, uint8_t block) {
|
|||||||
#endif
|
#endif
|
||||||
if (is_base_block) block_changes[i].block = 0xFF;
|
if (is_base_block) block_changes[i].block = 0xFF;
|
||||||
else block_changes[i].block = block;
|
else block_changes[i].block = block;
|
||||||
|
#ifndef DISK_SYNC_BLOCKS_ON_INTERVAL
|
||||||
writeBlockChangesToDisk(i, i);
|
writeBlockChangesToDisk(i, i);
|
||||||
|
#endif
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -450,7 +452,9 @@ uint8_t makeBlockChange (short x, uint8_t y, short z, uint8_t block) {
|
|||||||
block_changes_count = i + 1;
|
block_changes_count = i + 1;
|
||||||
}
|
}
|
||||||
// Write changes to disk (if applicable)
|
// Write changes to disk (if applicable)
|
||||||
|
#ifndef DISK_SYNC_BLOCKS_ON_INTERVAL
|
||||||
writeBlockChangesToDisk(last_real_entry + 1, last_real_entry + 15);
|
writeBlockChangesToDisk(last_real_entry + 1, last_real_entry + 15);
|
||||||
|
#endif
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
// If we're here, no changes were made
|
// If we're here, no changes were made
|
||||||
@@ -471,7 +475,9 @@ uint8_t makeBlockChange (short x, uint8_t y, short z, uint8_t block) {
|
|||||||
block_changes[first_gap].z = z;
|
block_changes[first_gap].z = z;
|
||||||
block_changes[first_gap].block = block;
|
block_changes[first_gap].block = block;
|
||||||
// Write change to disk (if applicable)
|
// Write change to disk (if applicable)
|
||||||
|
#ifndef DISK_SYNC_BLOCKS_ON_INTERVAL
|
||||||
writeBlockChangesToDisk(first_gap, first_gap);
|
writeBlockChangesToDisk(first_gap, first_gap);
|
||||||
|
#endif
|
||||||
// Extend future search range if we've appended to the end
|
// Extend future search range if we've appended to the end
|
||||||
if (first_gap == block_changes_count) {
|
if (first_gap == block_changes_count) {
|
||||||
block_changes_count ++;
|
block_changes_count ++;
|
||||||
@@ -1441,8 +1447,11 @@ void handleServerTick (int64_t time_since_last_tick) {
|
|||||||
sc_setHealth(player->client_fd, player->health, player->hunger, player->saturation);
|
sc_setHealth(player->client_fd, player->health, player->hunger, player->saturation);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write player data to file (if applicable)
|
// Write data to file (if applicable)
|
||||||
writePlayerDataToDisk();
|
writePlayerDataToDisk();
|
||||||
|
#ifdef DISK_SYNC_BLOCKS_ON_INTERVAL
|
||||||
|
writeBlockChangesToDisk(0, block_changes_count);
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If the RNG seed ever hits 0, it'll never generate anything
|
* If the RNG seed ever hits 0, it'll never generate anything
|
||||||
@@ -1618,7 +1627,9 @@ void broadcastChestUpdate (int origin_fd, uint8_t *storage_ptr, uint16_t item, u
|
|||||||
sc_setContainerSlot(player_data[i].client_fd, 2, slot, count, item);
|
sc_setContainerSlot(player_data[i].client_fd, 2, slot, count, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef DISK_SYNC_BLOCKS_ON_INTERVAL
|
||||||
writeChestChangesToDisk(storage_ptr, slot);
|
writeChestChangesToDisk(storage_ptr, slot);
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@@ -115,6 +115,12 @@ int initSerializer () {
|
|||||||
// Writes a range of block change entries to disk
|
// Writes a range of block change entries to disk
|
||||||
void writeBlockChangesToDisk (int from, int to) {
|
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)
|
// Try to open the file in rw (without overwriting)
|
||||||
FILE *file = fopen(FILE_PATH, "r+b");
|
FILE *file = fopen(FILE_PATH, "r+b");
|
||||||
if (!file) {
|
if (!file) {
|
||||||
|
Reference in New Issue
Block a user