"Fix" image loading after bumping LWJGL by using slower code path

This commit is contained in:
DBotThePony 2024-12-16 09:17:10 +07:00
parent 3a5dbed4e7
commit c9be37e37b
Signed by: DBot
GPG Key ID: DCC23B5715498507
4 changed files with 68 additions and 29 deletions

View File

@ -128,6 +128,9 @@ object Starbound : BlockableEventLoop("Universe Thread"), Scheduler, ISBFileLoca
// especially for data which will explode legacy client // especially for data which will explode legacy client
// also changes some constants // also changes some constants
const val DEBUG_BUILD = true const val DEBUG_BUILD = true
// LWJGL 3.3.4 STBI "bugfix"
const val DEBUG_IMAGE_INFO = true
// ---- // ----
fun <E : Any> interner(): Interner<E> { fun <E : Any> interner(): Interner<E> {

View File

@ -37,10 +37,13 @@ import ru.dbotthepony.kommons.gson.consumeNull
import ru.dbotthepony.kommons.gson.contains import ru.dbotthepony.kommons.gson.contains
import ru.dbotthepony.kommons.gson.get import ru.dbotthepony.kommons.gson.get
import ru.dbotthepony.kommons.gson.getObject 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.json.JsonPatch
import ru.dbotthepony.kstarbound.math.vector.Vector2d import ru.dbotthepony.kstarbound.math.vector.Vector2d
import ru.dbotthepony.kstarbound.util.AssetPathStack import ru.dbotthepony.kstarbound.util.AssetPathStack
import java.io.BufferedInputStream import java.io.BufferedInputStream
import java.io.File
import java.io.FileNotFoundException import java.io.FileNotFoundException
import java.lang.ref.Reference import java.lang.ref.Reference
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
@ -412,43 +415,63 @@ class Image private constructor(
val getWidth = intArrayOf(0) val getWidth = intArrayOf(0)
val getHeight = intArrayOf(0) val getHeight = intArrayOf(0)
val components = intArrayOf(0) val components = intArrayOf(0)
val status: Boolean
val stream = BufferedInputStream(file.open()) if (Starbound.DEBUG_IMAGE_INFO) {
val callback = STBIIOCallbacks.malloc() val idata = file.readDirect()
val readCallback = STBIReadCallback.create(STBIReadCallbackI { _, buf, size -> status = STBImage.stbi_info_from_memory(
val readBuf = ByteArray(size) idata,
val read = stream.read(readBuf) getWidth, getHeight,
components
)
for (i in 0 until read) { Reference.reachabilityFence(idata)
MemoryUtil.memPutByte(buf + i, readBuf[i]) } 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 { readCallback.free()
stream.mark(1) skipCallback.free()
val empty = stream.read() == -1 eofCallback.free()
stream.reset() callback.free()
if (empty) 1 else 0
} }
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) { if (!status) {
val asset = AssetPathStack.lastFile() val asset = AssetPathStack.lastFile()

View File

@ -141,6 +141,19 @@ class StarboundPak(val path: File, callback: (finished: Boolean, status: String)
return object : InputStream() { return object : InputStream() {
private var innerOffset = 0L 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 { override fun read(): Int {
if (innerOffset >= length) { if (innerOffset >= length) {
return -1 return -1

View File

@ -748,7 +748,7 @@ class ServerConnection(val server: StarboundServer, type: ConnectionType) : Conn
enqueueWarp(WarpAlias.OwnShip) enqueueWarp(WarpAlias.OwnShip)
} }
enqueueWarp(WarpAction.World(WorldID.Instance("outpost"))) //enqueueWarp(WarpAction.World(WorldID.Instance("outpost")))
scope.launch { shipFlightEventLoop(context.shipCoordinate, context.systemLocation) } scope.launch { shipFlightEventLoop(context.shipCoordinate, context.systemLocation) }
} }