Fix having multiple starbound clients being broken

This commit is contained in:
DBotThePony 2023-09-17 21:32:43 +07:00
parent 3ad0e78c10
commit 328cab8127
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -104,7 +104,7 @@ class StarboundClient : Closeable {
var fullbright = true
var gameTerminated = false
var clientTerminated = false
private set
/**
@ -156,6 +156,8 @@ class StarboundClient : Closeable {
lock.lock()
try {
clients++
if (!glfwInitialized) {
check(GLFW.glfwInit()) { "Unable to initialize GLFW" }
glfwInitialized = true
@ -991,21 +993,30 @@ class StarboundClient : Closeable {
}
override fun close() {
if (gameTerminated)
if (clientTerminated)
return
if (window != MemoryUtil.NULL) {
Callbacks.glfwFreeCallbacks(window)
GLFW.glfwDestroyWindow(window)
}
lock.lock()
GLFW.glfwTerminate()
GLFW.glfwSetErrorCallback(null)?.free()
try {
if (window != MemoryUtil.NULL) {
Callbacks.glfwFreeCallbacks(window)
GLFW.glfwDestroyWindow(window)
}
gameTerminated = true
if (--clients == 0) {
GLFW.glfwTerminate()
GLFW.glfwSetErrorCallback(null)?.free()
glfwInitialized = false
}
for (callback in terminateCallbacks) {
callback.invoke()
clientTerminated = true
for (callback in terminateCallbacks) {
callback.invoke()
}
} finally {
lock.unlock()
}
}
@ -1021,6 +1032,8 @@ class StarboundClient : Closeable {
private val lock = ReentrantLock()
@Volatile
private var glfwInitialized = false
@Volatile
private var clients = 0
@JvmStatic
fun readInternal(file: String): String {