From c9be37e37b8f8c78960d344345f2ba14c13804d5 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Mon, 16 Dec 2024 09:17:10 +0700 Subject: [PATCH] "Fix" image loading after bumping LWJGL by using slower code path --- .../ru/dbotthepony/kstarbound/Starbound.kt | 3 + .../kstarbound/defs/image/Image.kt | 79 ++++++++++++------- .../dbotthepony/kstarbound/io/StarboundPak.kt | 13 +++ .../kstarbound/server/ServerConnection.kt | 2 +- 4 files changed, 68 insertions(+), 29 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt index 17a697ea..1ea90aef 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt @@ -128,6 +128,9 @@ object Starbound : BlockableEventLoop("Universe Thread"), Scheduler, ISBFileLoca // especially for data which will explode legacy client // also changes some constants const val DEBUG_BUILD = true + + // LWJGL 3.3.4 STBI "bugfix" + const val DEBUG_IMAGE_INFO = true // ---- fun interner(): Interner { diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/image/Image.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/image/Image.kt index e012ce84..0f681e03 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/image/Image.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/image/Image.kt @@ -37,10 +37,13 @@ import ru.dbotthepony.kommons.gson.consumeNull import ru.dbotthepony.kommons.gson.contains import ru.dbotthepony.kommons.gson.get import ru.dbotthepony.kommons.gson.getObject +import ru.dbotthepony.kommons.util.XXHash64 +import ru.dbotthepony.kommons.util.xxhash32 import ru.dbotthepony.kstarbound.json.JsonPatch import ru.dbotthepony.kstarbound.math.vector.Vector2d import ru.dbotthepony.kstarbound.util.AssetPathStack import java.io.BufferedInputStream +import java.io.File import java.io.FileNotFoundException import java.lang.ref.Reference import java.lang.ref.WeakReference @@ -412,43 +415,63 @@ class Image private constructor( val getWidth = intArrayOf(0) val getHeight = intArrayOf(0) val components = intArrayOf(0) + val status: Boolean - val stream = BufferedInputStream(file.open()) - val callback = STBIIOCallbacks.malloc() + if (Starbound.DEBUG_IMAGE_INFO) { + val idata = file.readDirect() - val readCallback = STBIReadCallback.create(STBIReadCallbackI { _, buf, size -> - val readBuf = ByteArray(size) - val read = stream.read(readBuf) + status = STBImage.stbi_info_from_memory( + idata, + getWidth, getHeight, + components + ) - for (i in 0 until read) { - MemoryUtil.memPutByte(buf + i, readBuf[i]) + Reference.reachabilityFence(idata) + } else { + // FIXME: after upgrading to LWJGL 3.3.4 from 3.3.0 this code is broken + val stream = BufferedInputStream(file.open()) + val callback = STBIIOCallbacks.malloc() + + val readCallback = STBIReadCallback.create(STBIReadCallbackI { _, buf, size -> + var read = 0 + + for (i in 0 until size) { + val b = stream.read() + + if (b == -1) + break + else { + MemoryUtil.memPutByte(buf + i, b.toByte()) + read++ + } + } + + return@STBIReadCallbackI read + }) + + val skipCallback = STBISkipCallback.create { o, n -> stream.skip(n.toLong()) } + + val eofCallback = STBIEOFCallback.create { + stream.mark(1) + val empty = stream.read() == -1 + stream.reset() + if (empty) 1 else 0 } - return@STBIReadCallbackI read.coerceAtLeast(0) - }) + callback.set(readCallback, skipCallback, eofCallback) - val skipCallback = STBISkipCallback.create { _, n -> stream.skip(n.toLong()) } + status = STBImage.stbi_info_from_callbacks( + callback, 0L, + getWidth, getHeight, + components + ) - val eofCallback = STBIEOFCallback.create { - stream.mark(1) - val empty = stream.read() == -1 - stream.reset() - if (empty) 1 else 0 + readCallback.free() + skipCallback.free() + eofCallback.free() + callback.free() } - callback.set(readCallback, skipCallback, eofCallback) - - val status = STBImage.stbi_info_from_callbacks( - callback, 0L, - getWidth, getHeight, - components - ) - - readCallback.free() - skipCallback.free() - eofCallback.free() - callback.free() - if (!status) { val asset = AssetPathStack.lastFile() diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/io/StarboundPak.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/io/StarboundPak.kt index 4ed7a454..42b52af2 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/io/StarboundPak.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/io/StarboundPak.kt @@ -141,6 +141,19 @@ class StarboundPak(val path: File, callback: (finished: Boolean, status: String) return object : InputStream() { private var innerOffset = 0L + override fun skip(n: Long): Long { + if (innerOffset >= length) { + return 0 + } else if (innerOffset + n < length) { + innerOffset += n + return n + } else { + val diff = length - innerOffset + innerOffset = length + return diff + } + } + override fun read(): Int { if (innerOffset >= length) { return -1 diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/server/ServerConnection.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/server/ServerConnection.kt index 9bde92e0..28f879ec 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/server/ServerConnection.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/server/ServerConnection.kt @@ -748,7 +748,7 @@ class ServerConnection(val server: StarboundServer, type: ConnectionType) : Conn enqueueWarp(WarpAlias.OwnShip) } - enqueueWarp(WarpAction.World(WorldID.Instance("outpost"))) + //enqueueWarp(WarpAction.World(WorldID.Instance("outpost"))) scope.launch { shipFlightEventLoop(context.shipCoordinate, context.systemLocation) } }