Kind of un-race-conditioned chunk saving
This commit is contained in:
parent
2e5214ef12
commit
6ffa54fb27
@ -111,47 +111,63 @@ sealed class LegacyWorldStorage() : WorldStorage() {
|
|||||||
}, Starbound.EXECUTOR)
|
}, Starbound.EXECUTOR)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun saveEntities(pos: ChunkPos, data: Collection<AbstractEntity>): Boolean {
|
override fun saveEntities(pos: ChunkPos, data: Collection<AbstractEntity>, now: Boolean): Boolean {
|
||||||
Starbound.EXECUTOR.execute {
|
if (now) {
|
||||||
val chunkX = pos.x
|
val chunkX = pos.x
|
||||||
val chunkY = pos.y
|
val chunkY = pos.y
|
||||||
val key = ByteKey(2, (chunkX shr 8).toByte(), chunkX.toByte(), (chunkY shr 8).toByte(), chunkY.toByte())
|
val key = ByteKey(2, (chunkX shr 8).toByte(), chunkX.toByte(), (chunkY shr 8).toByte(), chunkY.toByte())
|
||||||
|
|
||||||
write(key, writeEntities(data))
|
write(key, writeEntities(data))
|
||||||
|
} else {
|
||||||
|
Starbound.EXECUTOR.execute {
|
||||||
|
val chunkX = pos.x
|
||||||
|
val chunkY = pos.y
|
||||||
|
val key = ByteKey(2, (chunkX shr 8).toByte(), chunkX.toByte(), (chunkY shr 8).toByte(), chunkY.toByte())
|
||||||
|
|
||||||
|
write(key, writeEntities(data))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun saveCells(pos: ChunkPos, data: Object2DArray<out AbstractCell>, state: ChunkState): Boolean {
|
private fun saveCells0(pos: ChunkPos, data: Object2DArray<out AbstractCell>, state: ChunkState) {
|
||||||
Starbound.EXECUTOR.execute {
|
val buff = FastByteArrayOutputStream()
|
||||||
val buff = FastByteArrayOutputStream()
|
val stream = DataOutputStream(BufferedOutputStream(DeflaterOutputStream(buff)))
|
||||||
val stream = DataOutputStream(BufferedOutputStream(DeflaterOutputStream(buff)))
|
|
||||||
|
|
||||||
stream.writeVarInt(when (state) {
|
stream.writeVarInt(when (state) {
|
||||||
ChunkState.FRESH -> 0
|
ChunkState.FRESH -> 0
|
||||||
ChunkState.EMPTY -> 0
|
ChunkState.EMPTY -> 0
|
||||||
ChunkState.TERRAIN -> 1
|
ChunkState.TERRAIN -> 1
|
||||||
ChunkState.MICRO_DUNGEONS -> 2
|
ChunkState.MICRO_DUNGEONS -> 2
|
||||||
ChunkState.CAVE_LIQUID -> 3
|
ChunkState.CAVE_LIQUID -> 3
|
||||||
ChunkState.FULL -> 4
|
ChunkState.FULL -> 4
|
||||||
})
|
})
|
||||||
|
|
||||||
stream.writeVarInt(418)
|
stream.writeVarInt(418)
|
||||||
|
|
||||||
for (y in 0 until CHUNK_SIZE) {
|
for (y in 0 until CHUNK_SIZE) {
|
||||||
for (x in 0 until CHUNK_SIZE) {
|
for (x in 0 until CHUNK_SIZE) {
|
||||||
val cell = data.getOrNull(x, y) ?: AbstractCell.NULL
|
val cell = data.getOrNull(x, y) ?: AbstractCell.NULL
|
||||||
cell.writeLegacy(stream)
|
cell.writeLegacy(stream)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val chunkX = pos.x
|
val chunkX = pos.x
|
||||||
val chunkY = pos.y
|
val chunkY = pos.y
|
||||||
val key = ByteKey(1, (chunkX shr 8).toByte(), chunkX.toByte(), (chunkY shr 8).toByte(), chunkY.toByte())
|
val key = ByteKey(1, (chunkX shr 8).toByte(), chunkX.toByte(), (chunkY shr 8).toByte(), chunkY.toByte())
|
||||||
|
|
||||||
stream.close()
|
stream.close()
|
||||||
write(key, buff.array.copyOf(buff.length))
|
write(key, buff.array.copyOf(buff.length))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun saveCells(pos: ChunkPos, data: Object2DArray<out AbstractCell>, state: ChunkState, now: Boolean): Boolean {
|
||||||
|
if (now) {
|
||||||
|
saveCells0(pos, data, state)
|
||||||
|
} else {
|
||||||
|
Starbound.EXECUTOR.execute {
|
||||||
|
saveCells0(pos, data, state)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
@ -23,12 +23,12 @@ class NativeWorldStorage() : WorldStorage() {
|
|||||||
TODO("Not yet implemented")
|
TODO("Not yet implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun saveEntities(pos: ChunkPos, data: Collection<AbstractEntity>): Boolean {
|
override fun saveEntities(pos: ChunkPos, data: Collection<AbstractEntity>, now: Boolean): Boolean {
|
||||||
return super.saveEntities(pos, data)
|
return super.saveEntities(pos, data, now)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun saveCells(pos: ChunkPos, data: Object2DArray<out AbstractCell>, state: ChunkState): Boolean {
|
override fun saveCells(pos: ChunkPos, data: Object2DArray<out AbstractCell>, state: ChunkState, now: Boolean): Boolean {
|
||||||
return super.saveCells(pos, data, state)
|
return super.saveCells(pos, data, state, now)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun saveMetadata(data: Metadata): Boolean {
|
override fun saveMetadata(data: Metadata): Boolean {
|
||||||
|
@ -546,7 +546,7 @@ class ServerChunk(world: ServerWorld, pos: ChunkPos) : Chunk<ServerWorld, Server
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun writeToStorage(): Collection<AbstractEntity> {
|
private fun writeToStorage(now: Boolean = false): Collection<AbstractEntity> {
|
||||||
if (!cells.isInitialized() || state <= ChunkState.EMPTY)
|
if (!cells.isInitialized() || state <= ChunkState.EMPTY)
|
||||||
return emptyList()
|
return emptyList()
|
||||||
|
|
||||||
@ -555,8 +555,8 @@ class ServerChunk(world: ServerWorld, pos: ChunkPos) : Chunk<ServerWorld, Server
|
|||||||
aabbd,
|
aabbd,
|
||||||
filter = Predicate { it.isPersistent && !it.isRemote && aabbd.isInside(it.position) })
|
filter = Predicate { it.isPersistent && !it.isRemote && aabbd.isInside(it.position) })
|
||||||
|
|
||||||
world.storage.saveCells(pos, copyCells(), state)
|
world.storage.saveCells(pos, copyCells(), state, now)
|
||||||
world.storage.saveEntities(pos, unloadable)
|
world.storage.saveEntities(pos, unloadable, now)
|
||||||
|
|
||||||
return unloadable
|
return unloadable
|
||||||
}
|
}
|
||||||
@ -580,7 +580,7 @@ class ServerChunk(world: ServerWorld, pos: ChunkPos) : Chunk<ServerWorld, Server
|
|||||||
world.chunkMap.remove(pos)
|
world.chunkMap.remove(pos)
|
||||||
flushTask.cancel(false)
|
flushTask.cancel(false)
|
||||||
|
|
||||||
writeToStorage().forEach {
|
writeToStorage(true).forEach {
|
||||||
it.remove(AbstractEntity.RemovalReason.UNLOADED)
|
it.remove(AbstractEntity.RemovalReason.UNLOADED)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,11 +79,11 @@ abstract class WorldStorage : Closeable {
|
|||||||
return buff.array.copyOf(buff.length)
|
return buff.array.copyOf(buff.length)
|
||||||
}
|
}
|
||||||
|
|
||||||
open fun saveEntities(pos: ChunkPos, data: Collection<AbstractEntity>): Boolean {
|
open fun saveEntities(pos: ChunkPos, data: Collection<AbstractEntity>, now: Boolean = false): Boolean {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
open fun saveCells(pos: ChunkPos, data: Object2DArray<out AbstractCell>, state: ChunkState): Boolean {
|
open fun saveCells(pos: ChunkPos, data: Object2DArray<out AbstractCell>, state: ChunkState, now: Boolean = false): Boolean {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,12 +148,12 @@ abstract class WorldStorage : Closeable {
|
|||||||
return chainOptionalFutures(children) { it.loadMetadata() }
|
return chainOptionalFutures(children) { it.loadMetadata() }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun saveEntities(pos: ChunkPos, data: Collection<AbstractEntity>): Boolean {
|
override fun saveEntities(pos: ChunkPos, data: Collection<AbstractEntity>, now: Boolean): Boolean {
|
||||||
return children.any { it.saveEntities(pos, data) }
|
return children.any { it.saveEntities(pos, data, now) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun saveCells(pos: ChunkPos, data: Object2DArray<out AbstractCell>, state: ChunkState): Boolean {
|
override fun saveCells(pos: ChunkPos, data: Object2DArray<out AbstractCell>, state: ChunkState, now: Boolean): Boolean {
|
||||||
return children.any { it.saveCells(pos, data, state) }
|
return children.any { it.saveCells(pos, data, state, now) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun saveMetadata(data: Metadata): Boolean {
|
override fun saveMetadata(data: Metadata): Boolean {
|
||||||
|
Loading…
Reference in New Issue
Block a user