From c265b87e48febea74a7ff9ca5c5d279b1a90ec6c Mon Sep 17 00:00:00 2001 From: Bixilon Date: Sun, 14 Nov 2021 13:30:39 +0100 Subject: [PATCH] fix compatibility with Java < 16 --- ReadMe.md | 4 +- .../util/collections/ArrayFloatList.kt | 39 +++++++++++++++---- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index 3ddd44a20..d19b1f96c 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -28,7 +28,7 @@ Minosoft is an open source minecraft client, written from scratch in kotlin (and - RAM: Minimum 300 MiB, 1 GiB recommended - Disk space: Minosoft itself is pretty small (2-3 MiB), the libraries are a bit bigger (~80 MiB). You also need to have the "normal" minecraft assets (~ 300 MiB per minecraft version). - GPU: OpenGL 3.0+. Every modern GPU works and is recommended. -- Java 11+, newest version recommended (This is really important, we use features that are only available in this version. Java 8 is currently **not** supported). +- Java 11+, 16+ recommended (This is really important, we use features that are only available in this version. Java 8 is currently **not** supported). ## Rendering @@ -73,7 +73,7 @@ I always try to add support for the newest version of minecraft. Mostly it is pr ### Supported versions -Almost all versions (and snapshots!) between 1.7 and the latest one (1.17 as of writing this). I plan to maintain Minosoft to at least version 1.20, so stay tuned. Support for older protocols will not be dropped as newer protocols are added. And can still only recommend using the latest stable version, should be the most stable one. +Almost all versions (and snapshots!) between 1.7 and the latest one (1.17 as of writing this). I plan to maintain Minosoft to at least version 1.20, so stay tuned. Support for older protocols will not be dropped as newer protocols are added. It is still recommended using the latest stable version. ### Unsupported versions diff --git a/src/main/java/de/bixilon/minosoft/util/collections/ArrayFloatList.kt b/src/main/java/de/bixilon/minosoft/util/collections/ArrayFloatList.kt index c0bd154f2..73a6fabfa 100644 --- a/src/main/java/de/bixilon/minosoft/util/collections/ArrayFloatList.kt +++ b/src/main/java/de/bixilon/minosoft/util/collections/ArrayFloatList.kt @@ -13,6 +13,7 @@ package de.bixilon.minosoft.util.collections +import de.bixilon.minosoft.util.KUtil import de.bixilon.minosoft.util.KUtil.clean import org.lwjgl.system.MemoryUtil.memAllocFloat import java.nio.FloatBuffer @@ -64,8 +65,14 @@ class ArrayFloatList( } val oldBuffer = buffer buffer = memAllocFloat(newSize) - buffer.put(0, oldBuffer, 0, oldBuffer.position()) - buffer.position(oldBuffer.position()) + if (FLOAT_PUT_METHOD == null) { // Java < 16 + for (i in 0 until oldBuffer.position()) { + buffer.put(oldBuffer.get(i)) + } + } else { + FLOAT_PUT_METHOD.invoke(buffer, 0, oldBuffer, 0, oldBuffer.position()) + buffer.position(oldBuffer.position()) + } oldBuffer.clean() } @@ -83,16 +90,25 @@ class ArrayFloatList( fun addAll(floatList: ArrayFloatList) { ensureSize(floatList.size) - buffer.put(buffer.position(), floatList.buffer, 0, floatList.buffer.position()) - buffer.position(buffer.position() + floatList.buffer.position()) + if (FLOAT_PUT_METHOD == null) { // Java < 16 + for (i in 0 until floatList.buffer.position()) { + buffer.put(floatList.buffer.get(i)) + } + } else { + FLOAT_PUT_METHOD.invoke(buffer, buffer.position(), floatList.buffer, 0, floatList.buffer.position()) + buffer.position(buffer.position() + floatList.buffer.position()) + } } private fun checkOutputArray() { if (outputUpToDate) { return } - output = FloatArray(size) - buffer.get(output, 0, buffer.position()) + val position = buffer.position() + output = FloatArray(position) + buffer.position(0) + buffer.get(output, 0, position) + buffer.position(position) outputUpToDate = true } @@ -105,13 +121,20 @@ class ArrayFloatList( finalized = true val oldBuffer = buffer buffer = memAllocFloat(oldBuffer.position()) - buffer.put(0, oldBuffer, 0, oldBuffer.position()) - buffer.position(buffer.limit()) + if (FLOAT_PUT_METHOD == null) { // Java < 16 + for (i in 0 until oldBuffer.position()) { + buffer.put(oldBuffer.get(i)) + } + } else { + FLOAT_PUT_METHOD.invoke(buffer, 0, oldBuffer, 0, oldBuffer.position()) + buffer.position(buffer.limit()) + } oldBuffer.clean() } private companion object { + private val FLOAT_PUT_METHOD = KUtil.tryCatch { FloatBuffer::class.java.getMethod("put", Int::class.java, FloatBuffer::class.java, Int::class.java, Int::class.java) } private const val DEFAULT_INITIAL_SIZE = 1000 } }