wip inline rgbcolor

This should make a lot of things faster and reduce a lot of memory allocations
This commit is contained in:
Moritz Zwerger 2024-04-23 20:43:58 +02:00
parent 15faff1b6a
commit 8cdf703f2b
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
6 changed files with 43 additions and 77 deletions

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 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.
*
@ -21,52 +21,21 @@ import de.bixilon.minosoft.data.text.formatting.color.RGBColor.Companion.asColor
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap
object ChatColors {
@JvmField
val BLACK = RGBColor(0, 0, 0)
@JvmField
val DARK_BLUE = RGBColor(0, 0, 170)
@JvmField
val DARK_GREEN = RGBColor(0, 170, 0)
@JvmField
val DARK_AQUA = RGBColor(0, 170, 170)
@JvmField
val DARK_RED = RGBColor(170, 0, 0)
@JvmField
val DARK_PURPLE = RGBColor(170, 0, 170)
@JvmField
val GOLD = RGBColor(255, 170, 0)
@JvmField
val GRAY = RGBColor(170, 170, 170)
@JvmField
val DARK_GRAY = RGBColor(85, 85, 85)
@JvmField
val BLUE = RGBColor(85, 85, 255)
@JvmField
val GREEN = RGBColor(85, 255, 85)
@JvmField
val AQUA = RGBColor(85, 255, 255)
@JvmField
val RED = RGBColor(255, 85, 85)
@JvmField
val LIGHT_PURPLE = RGBColor(255, 85, 255)
@JvmField
val YELLOW = RGBColor(255, 255, 85)
@JvmField
val WHITE = RGBColor(255, 255, 255)
@ -82,10 +51,11 @@ object ChatColors {
var index = 0
for (field in this::class.java.declaredFields) {
val color = field.get(null)
if (color !is RGBColor) {
val int = field.get(null)
if (int !is Int) { // RGBColor is inlined
continue
}
val color = RGBColor(int)
values[index] = color
CHAR_MAP[color] = index
nameMap[field.name.lowercase()] = color

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 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.
*
@ -21,7 +21,8 @@ import de.bixilon.minosoft.data.text.TextComponent
import de.bixilon.minosoft.data.text.formatting.TextFormattable
import org.checkerframework.common.value.qual.IntRange
class RGBColor(val rgba: Int) : TextFormattable {
@JvmInline
value class RGBColor(val rgba: Int) : TextFormattable {
val ansi: String get() = ANSI.rgb(red, green, blue)
@JvmOverloads
@ -68,21 +69,6 @@ class RGBColor(val rgba: Int) : TextFormattable {
val rgb: Int
get() = rgba ushr 8
override fun hashCode(): Int {
return rgba
}
override fun equals(other: Any?): Boolean {
if (super.equals(other)) {
return true
}
if (other !is RGBColor) {
return false
}
val their = other as RGBColor? ?: return false
return rgba == their.rgba
}
override fun toString(): String {
return if (alpha != 255) {
String.format("#%08X", rgba)
@ -103,10 +89,6 @@ class RGBColor(val rgba: Int) : TextFormattable {
return RGBColor(red.clamp(0.0f, 1.0f), green.clamp(0.0f, 1.0f), blue.clamp(0.0f, 1.0f), alpha.clamp(0.0f, 1.0f))
}
fun mix(vararg colors: RGBColor): RGBColor {
return Companion.mix(this, *colors)
}
fun mix(other: RGBColor): RGBColor {
return RGBColor((red + other.red) / 2, (green + other.green) / 2, (blue + other.blue) / 2, (alpha + other.alpha) / 2)
}
@ -158,18 +140,5 @@ class RGBColor(val rgba: Int) : TextFormattable {
fun Double.asGray(): RGBColor {
return RGBColor(this.toFloat(), this.toFloat(), this.toFloat())
}
fun mix(vararg colors: RGBColor): RGBColor {
var red = 0
var green = 0
var blue = 0
for (color in colors) {
red += color.red
green += color.green
blue += color.blue
}
return RGBColor(red / colors.size, green / colors.size, blue / colors.size)
}
}
}

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 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.
*
@ -22,7 +22,7 @@ import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.modding.event.events.chat.ChatMessageEvent
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import de.bixilon.minosoft.protocol.protocol.KProtocolDefinition
import de.bixilon.minosoft.protocol.protocol.ProtocolVersions
import de.bixilon.minosoft.protocol.protocol.buffers.play.PlayInByteBuffer
import de.bixilon.minosoft.util.KUtil
@ -51,7 +51,7 @@ class ChatMessageS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
}
}
}
text.setFallbackColor(ProtocolDefinition.DEFAULT_COLOR)
text.setFallbackColor(KProtocolDefinition.DEFAULT_COLOR)
}
override fun handle(connection: PlayConnection) {

View File

@ -0,0 +1,20 @@
/*
* Minosoft
* Copyright (C) 2020-2024 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.protocol.protocol
import de.bixilon.minosoft.data.text.formatting.color.ChatColors
object KProtocolDefinition {
val DEFAULT_COLOR = ChatColors.WHITE
}

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 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.
*
@ -14,8 +14,6 @@
package de.bixilon.minosoft.protocol.protocol;
import de.bixilon.kotlinglm.vec3.Vec3i;
import de.bixilon.minosoft.data.text.formatting.color.ChatColors;
import de.bixilon.minosoft.data.text.formatting.color.RGBColor;
import java.util.regex.Pattern;
@ -69,8 +67,6 @@ public final class ProtocolDefinition {
public static final float VELOCITY_NETWORK_DIVIDER = 8000.0f;
public static final RGBColor DEFAULT_COLOR = ChatColors.WHITE;
public static final byte LIGHT_LEVELS = 16;
public static final byte MAX_LIGHT_LEVEL = LIGHT_LEVELS - 1;
public static final int MAX_LIGHT_LEVEL_I = MAX_LIGHT_LEVEL;

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 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.
*
@ -28,6 +28,7 @@ object RGBColorSerializer : SimpleModule() {
init {
addDeserializer(RGBColor::class.java, Deserializer)
addDeserializer(Int::class.java, IntDeserializer)
addSerializer(RGBColor::class.java, Serializer)
}
@ -41,6 +42,16 @@ object RGBColorSerializer : SimpleModule() {
}
}
}
object IntDeserializer : StdDeserializer<Int>(Int::class.java) { // TODO: This is shit?
override fun deserialize(parser: JsonParser, context: DeserializationContext?): Int {
return when (parser.currentToken) {
JsonToken.VALUE_NUMBER_INT -> parser.valueAsInt
JsonToken.VALUE_STRING -> parser.valueAsString.toColor()!!.rgba
else -> TODO("Can not parse color!")
}
}
}
object Serializer : StdSerializer<RGBColor>(RGBColor::class.java) {