eros: don't use WeakReferenceQueue, use stage list

This should fix an unknown crash on macos
This commit is contained in:
Bixilon 2023-01-10 10:24:40 +01:00
parent 078dbdc800
commit 97bfe87f0a
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 71 additions and 4 deletions

View File

@ -13,7 +13,6 @@
package de.bixilon.minosoft.gui.eros.util
import com.sun.javafx.util.WeakReferenceQueue
import de.bixilon.kutil.cast.CastUtil.unsafeCast
import de.bixilon.kutil.concurrent.pool.DefaultThreadPool
import de.bixilon.kutil.reflection.ReflectionUtil.forceSet
@ -46,7 +45,7 @@ import kotlin.reflect.jvm.javaField
object JavaFXUtil {
private const val DEFAULT_STYLE = "resource:minosoft:eros/style.css"
private val stages: WeakReferenceQueue<Stage> = WeakReferenceQueue()
private val stages = StageList()
lateinit var JAVA_FX_THREAD: Thread
lateinit var MINOSOFT_LOGO: Image
lateinit var HOST_SERVICES: HostServices
@ -62,8 +61,8 @@ object JavaFXUtil {
ErosProfileManager.selected.theme::theme.observeFX(this) {
stages.cleanup()
for (stage in stages.iterator().unsafeCast<Iterator<Stage?>>()) {
stage ?: continue
for (stage in stages.iterator()) {
stage ?: break
stage.scene.stylesheets.clear()
stage.scene.stylesheets.add(DEFAULT_STYLE)
stage.scene.stylesheets.add(getThemeURL(it))

View File

@ -0,0 +1,68 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.gui.eros.util
import de.bixilon.kutil.concurrent.lock.simple.SimpleLock
import javafx.stage.Stage
import java.lang.ref.WeakReference
class StageList : Iterable<Stage?> {
private val stages: MutableList<WeakReference<Stage>> = mutableListOf()
val lock = SimpleLock()
private fun <T> MutableList<WeakReference<T>>.cleanup() {
val iterator = this.iterator()
for (reference in iterator) {
if (reference.get() != null) {
continue
}
iterator.remove()
}
}
fun cleanup() {
lock.lock()
stages.cleanup()
lock.unlock()
}
fun add(stage: Stage) {
lock.lock()
stages += WeakReference(stage)
lock.unlock()
}
override fun iterator(): Iterator<Stage?> {
return object : Iterator<Stage?> {
private val iterator = stages.iterator()
override fun hasNext(): Boolean {
return iterator.hasNext()
}
override fun next(): Stage? {
while (iterator.hasNext()) {
val entry = iterator.next().get()
if (entry != null) {
return entry
}
iterator.remove()
}
return null
}
}
}
}