fix sound loading crash, fixes #63

This commit is contained in:
Bixilon 2021-12-15 16:56:44 +01:00
parent c955a15de6
commit d29b6d043f
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
5 changed files with 17 additions and 11 deletions

View File

@ -225,8 +225,6 @@ class RenderWindow(
connection.fireEvent(ResizeWindowEvent(previousSize = Vec2i(0, 0), size = window.size)) connection.fireEvent(ResizeWindowEvent(previousSize = Vec2i(0, 0), size = window.size))
Log.log(LogMessageType.RENDERING_LOADING) { "Unloading assets manager" }
connection.assetsManager.unload()
Log.log(LogMessageType.RENDERING_LOADING) { "Rendering is fully prepared in ${stopwatch.totalTime()}" } Log.log(LogMessageType.RENDERING_LOADING) { "Rendering is fully prepared in ${stopwatch.totalTime()}" }
initialized = true initialized = true
latch.dec() latch.dec()

View File

@ -35,15 +35,17 @@ class WeightedBakedModel(
totalWeight += weight totalWeight += weight
} }
check(totalWeight > 0) { "Weight must be >= 1" }
this.totalWeight = totalWeight this.totalWeight = totalWeight
} }
override fun getTouchingFaceProperties(random: Random, direction: Directions): Array<AbstractFaceProperties> { override fun getTouchingFaceProperties(random: Random, direction: Directions): Array<AbstractFaceProperties> {
return getModel(random).getTouchingFaceProperties(random, direction) return getModel(random)?.getTouchingFaceProperties(random, direction) ?: arrayOf()
} }
private fun getModel(random: Random): BakedBlockModel { private fun getModel(random: Random): BakedBlockModel? {
if (models.isEmpty()) {
return null
}
var weightLeft = abs(random.nextLong() % totalWeight) var weightLeft = abs(random.nextLong() % totalWeight)
for ((model, weight) in models) { for ((model, weight) in models) {
@ -57,12 +59,12 @@ class WeightedBakedModel(
} }
override fun singleRender(position: Vec3i, mesh: WorldMesh, random: Random, blockState: BlockState, neighbours: Array<BlockState?>, light: ByteArray, ambientLight: FloatArray, tints: IntArray?): Boolean { override fun singleRender(position: Vec3i, mesh: WorldMesh, random: Random, blockState: BlockState, neighbours: Array<BlockState?>, light: ByteArray, ambientLight: FloatArray, tints: IntArray?): Boolean {
return getModel(random).singleRender(position, mesh, random, blockState, neighbours, light, ambientLight, tints) return getModel(random)?.singleRender(position, mesh, random, blockState, neighbours, light, ambientLight, tints) ?: false
} }
override fun getParticleTexture(random: Random, blockPosition: Vec3i): AbstractTexture? { override fun getParticleTexture(random: Random, blockPosition: Vec3i): AbstractTexture? {
random.setSeed(VecUtil.generatePositionHash(blockPosition.x, blockPosition.y, blockPosition.z)) random.setSeed(VecUtil.generatePositionHash(blockPosition.x, blockPosition.y, blockPosition.z))
return getModel(random).getParticleTexture(random, blockPosition) return getModel(random)?.getParticleTexture(random, blockPosition)
} }
} }

View File

@ -45,7 +45,10 @@ class SoundData(
companion object { companion object {
operator fun invoke(assetsManager: AssetsManager, sound: Sound): SoundData { operator fun invoke(assetsManager: AssetsManager, sound: Sound): SoundData {
val buffer = ByteBuffer.wrap(assetsManager[sound.path].readAllBytes()) val vorbisData = assetsManager[sound.path].readAllBytes()
val buffer = BufferUtils.createByteBuffer(vorbisData.size)
buffer.put(vorbisData)
buffer.rewind()
val error = BufferUtils.createIntBuffer(1) val error = BufferUtils.createIntBuffer(1)
val vorbis = stb_vorbis_open_memory(buffer, error, null) val vorbis = stb_vorbis_open_memory(buffer, error, null)
@ -73,7 +76,7 @@ class SoundData(
channels = channels, channels = channels,
sampleRate = sampleRate, sampleRate = sampleRate,
samplesLength = samplesLength, samplesLength = samplesLength,
sampleSeconds = sampleSeconds sampleSeconds = sampleSeconds,
) )
} }
} }

View File

@ -32,11 +32,13 @@ data class SoundType(
totalWeight += sound.weight totalWeight += sound.weight
} }
check(totalWeight >= 1) { "Weight must be >= 1" }
this.totalWeight = totalWeight this.totalWeight = totalWeight
} }
fun getSound(random: Random): Sound { fun getSound(random: Random): Sound? {
if (sounds.isEmpty()) {
return null
}
var weightLeft = abs(random.nextLong() % totalWeight) var weightLeft = abs(random.nextLong() % totalWeight)
for (sound in sounds) { for (sound in sounds) {

View File

@ -204,6 +204,7 @@ class PlayConnection(
if (this::randomTickTask.isInitialized) { if (this::randomTickTask.isInitialized) {
TimeWorker.removeTask(randomTickTask) TimeWorker.removeTask(randomTickTask)
} }
assetsManager.unload()
state = PlayConnectionStates.DISCONNECTED state = PlayConnectionStates.DISCONNECTED
ACTIVE_CONNECTIONS -= this ACTIVE_CONNECTIONS -= this
} }