Skip to content

Commit

Permalink
Update ASM + allow non object plugins + some java utility functions i…
Browse files Browse the repository at this point in the history
…n Inject, Vec2i and Vec2f

Signed-off-by: Seppe Volkaerts <seppevolkaerts@hotmail.com>
  • Loading branch information
Cybermaxke committed Oct 23, 2023
1 parent 767bb93 commit ccab412
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 47 deletions.
2 changes: 1 addition & 1 deletion proxy/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ dependencies {
implementation(group = "org.lanternpowered", name = "lmbda", version = "3.0.0-SNAPSHOT")

// ASM
implementation(group = "org.ow2.asm", name = "asm", version = "9.4")
implementation(group = "org.ow2.asm", name = "asm", version = "9.6")

// Plugins
implementation(group = "org.spongepowered", name = "plugin-spi", version = "0.3.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,36 +269,22 @@ internal fun ByteBuf.writeShortVec2i(vec2i: Vec2i) = apply {
/**
* Reads a position where x and y.
*/
internal fun ByteBuf.readVec2i(): Vec2i {
val x = readIntLE()
val y = readIntLE()
return Vec2i(x, y)
}
internal fun ByteBuf.readVec2i() = Vec2i(readLongLE())

/**
* Writes a position where x and y.
*/
internal fun ByteBuf.writeVec2i(vec2i: Vec2i) = apply {
writeIntLE(vec2i.x)
writeIntLE(vec2i.y)
}
internal fun ByteBuf.writeVec2i(vec2i: Vec2i) = writeLongLE(vec2i.packed)

/**
* Reads a float vector.
*/
internal fun ByteBuf.readVec2f(): Vec2f {
val x = readFloatLE()
val y = readFloatLE()
return Vec2f(x, y)
}
internal fun ByteBuf.readVec2f() = Vec2f(readLongLE())

/**
* Writes a float vector.
*/
internal fun ByteBuf.writeVec2f(vec2f: Vec2f) = apply {
writeFloatLE(vec2f.x)
writeFloatLE(vec2f.y)
}
internal fun ByteBuf.writeVec2f(vec2f: Vec2f) = writeLongLE(vec2f.packed)

/**
* Reads a [UUID].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@ import org.lanternpowered.terre.sql.SqlManager
import kotlin.reflect.KClass
import kotlin.reflect.KType

@Suppress("UNCHECKED_CAST")
internal fun <T : Any> inject(instance: Any?, type: Class<T>): T =
instance.inject(type.kotlin, false) as T

@PublishedApi
internal fun Any?.inject(type: KType): Any? =
inject(type.classifier as KClass<*>, type.isMarkedNullable)

/**
* Injects objects.
*/
@PublishedApi
internal fun Any?.inject(type: KType): Any? {
val kClass = type.classifier as KClass<*>

val value: Any? = when (kClass) {
private fun Any?.inject(kClass: KClass<*>, isNullable: Boolean): Any? {
val value = when (kClass) {
RootConfigDirectory::class -> RootConfigDirectory
EventBus::class -> EventBus
Proxy::class -> Proxy
Expand All @@ -42,24 +47,22 @@ internal fun Any?.inject(type: KType): Any? {
SqlManager::class -> SqlManager
else -> {
val pluginContainer = if (this != null) {
ProxyImpl.pluginManager.getPluginContainer(this)
ProxyImpl.pluginManager.getByInstance(this)
} else null ?: activePlugin as? TerrePluginContainer
pluginContainer?.inject(type)
pluginContainer?.inject(kClass)
}
}

if (value == null && !type.isMarkedNullable)
if (value == null && !isNullable)
error("Cannot retrieve ${kClass.qualifiedName} within the current context.")

return value
}

/**
* Injects plugin related objects.
*/
private fun TerrePluginContainer.inject(type: KType): Any? {
private fun TerrePluginContainer.inject(kClass: KClass<*>): Any? {
@Suppress("DEPRECATION")
return when (type.classifier as KClass<*>) {
return when (kClass) {
PluginContainer::class -> this
Logger::class, org.apache.logging.log4j.Logger::class -> this.logger
java.util.logging.Logger::class -> this.javaLogger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import java.nio.file.Files
import java.nio.file.Paths
import java.util.concurrent.ConcurrentHashMap
import kotlin.reflect.KClass
import kotlin.reflect.full.createInstance
import kotlin.reflect.full.findAnnotation

internal class PluginManagerImpl : PluginManager {
Expand Down Expand Up @@ -69,10 +70,14 @@ internal class PluginManagerImpl : PluginManager {
if (url != null)
pluginClassLoader.addURL(url)
val pluginClass = Class.forName(candidate.className, true, pluginClassLoader).kotlin
val instance = pluginClass.objectInstance
var instance = pluginClass.objectInstance
if (instance == null) {
Terre.logger.info("Plugin ${candidate.id} is not an object, skipping...")
continue
try {
instance = pluginClass.createInstance() as Any
} catch (ex: Exception) {
Terre.logger.error("Failed to instantiate plugin ${candidate.id}, skipping...", ex)
continue
}
}
addOrGetPluginContainer(pluginClass.findAnnotation()!!, instance)
EventBus.register(instance)
Expand All @@ -85,7 +90,7 @@ internal class PluginManagerImpl : PluginManager {
/**
* Attempts to get the [PluginContainer] for the given plugin instance.
*/
fun getPluginContainer(instance: Any): TerrePluginContainer? {
override fun getByInstance(instance: Any): TerrePluginContainer? {
if (instance is TerrePluginContainer)
return instance

Expand Down
20 changes: 16 additions & 4 deletions proxy/src/main/kotlin/org/lanternpowered/terre/math/Vec2f.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ package org.lanternpowered.terre.math
* A floating-point 2d vector.
*/
@JvmInline
value class Vec2f private constructor(private val packed: Long) {
value class Vec2f internal constructor(internal val packed: Long) {

/**
* Constructs a new floating-point 2d vector with the given x and y values.
*/
constructor(x: Float, y: Float) :
this((x.toRawBits().toUInt().toLong() shl 32) or y.toRawBits().toUInt().toLong())
this((y.toRawBits().toUInt().toLong() shl 32) or x.toRawBits().toUInt().toLong())

/**
* The x value of the vector.
*/
val x: Float
get() = Float.fromBits((packed ushr 32).toInt())
get() = Float.fromBits((packed and 0xffffffffL).toInt())

/**
* The y value of the vector.
*/
val y: Float
get() = Float.fromBits((packed and 0xffffffffL).toInt())
get() = Float.fromBits((packed ushr 32).toInt())

operator fun plus(that: Vec2f): Vec2f =
Vec2f(x + that.x, y + that.y)
Expand Down Expand Up @@ -106,5 +106,17 @@ value class Vec2f private constructor(private val packed: Long) {
val Up = Vec2f(0f, 1f)

val Down = Vec2f(0f, -1f)

// Java specific API

@JvmStatic
@JvmName("x")
internal fun x(vec: Vec2f) = vec.x
@JvmStatic
@JvmName("y")
internal fun y(vec: Vec2f) = vec.y
@JvmStatic
@JvmName("of")
internal fun of(x: Float, y: Float) = Vec2f(x, y)
}
}
20 changes: 16 additions & 4 deletions proxy/src/main/kotlin/org/lanternpowered/terre/math/Vec2i.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ package org.lanternpowered.terre.math
* An integer 2d vector.
*/
@JvmInline
value class Vec2i private constructor(private val packed: Long) {
value class Vec2i internal constructor(internal val packed: Long) {

/**
* Constructs a new integer 2d vector with the given x and y values.
*/
constructor(x: Int, y: Int) :
this((x.toUInt().toLong() shl 32) or y.toUInt().toLong())
this((y.toUInt().toLong() shl 32) or x.toUInt().toLong())

/**
* The x value of the vector.
*/
val x: Int
get() = (packed ushr 32).toInt()
get() = (packed and 0xffffffffL).toInt()

/**
* The y value of the vector.
*/
val y: Int
get() = (packed and 0xffffffffL).toInt()
get() = (packed ushr 32).toInt()

operator fun plus(that: Vec2i): Vec2i =
Vec2i(x + that.x, y + that.y)
Expand Down Expand Up @@ -103,5 +103,17 @@ value class Vec2i private constructor(private val packed: Long) {
val Up = Vec2i(0, 1)

val Down = Vec2i(0, -1)

// Java specific API

@JvmStatic
@JvmName("x")
internal fun x(vec: Vec2i) = vec.x
@JvmStatic
@JvmName("y")
internal fun y(vec: Vec2i) = vec.y
@JvmStatic
@JvmName("of")
internal fun of(x: Int, y: Int) = Vec2i(x, y)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import kotlin.reflect.KType
import kotlin.reflect.typeOf
import org.lanternpowered.terre.impl.plugin.inject as doInject

@JvmName("inject")
internal fun <T : Any> inject(instance: Any?, type: Class<T>): T = doInject(instance, type)

/**
* Injects a value of type [T] for the target receiver.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@
*/
package org.lanternpowered.terre.plugin

import org.lanternpowered.terre.util.`use named arguments`

/**
* An annotation to mark plugin classes or objects.
*
* @property id The id, can only contain a-z, 0-9, - and _
*/
@Suppress("unused")
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class Plugin(
vararg val `use named arguments`: `use named arguments`,
val id: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ interface PluginManager {
*/
operator fun get(id: String): PluginContainer?

/**
* Attempts to get a [PluginContainer] for the given instance.
*/
fun getByInstance(instance: Any): PluginContainer?

/**
* The singleton instance of the [PluginManager].
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Terre
*
* Copyright (c) LanternPowered <https://www.lanternpowered.org>
* Copyright (c) contributors
*
* This work is licensed under the terms of the MIT License (MIT). For
* a copy, see 'LICENSE.txt' or <https://opensource.org/licenses/MIT>.
*/
package org.lanternpowered.terre.impl.protocol

import io.netty.buffer.Unpooled
import org.lanternpowered.terre.impl.network.buffer.readVec2f
import org.lanternpowered.terre.impl.network.buffer.readVec2i
import org.lanternpowered.terre.impl.network.buffer.writeVec2f
import org.lanternpowered.terre.impl.network.buffer.writeVec2i
import org.lanternpowered.terre.math.Vec2f
import org.lanternpowered.terre.math.Vec2i
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals

class ByteBufTest {

@Test
fun writeVec2f() {
val vec = Vec2f(125.0f, 26669.016f)

val expected = Unpooled.buffer()
expected.writeFloatLE(vec.x)
expected.writeFloatLE(vec.y)

val written = Unpooled.buffer()
written.writeVec2f(vec)

assertContentEquals(expected.array(), written.array())
}

@Test
fun readVec2f() {
val vec = Vec2f(125.0f, 26669.016f)

val twoFloats = Unpooled.buffer()
twoFloats.writeFloatLE(vec.x)
twoFloats.writeFloatLE(vec.y)

val read = twoFloats.readVec2f()

assertEquals(vec, read)
}


@Test
fun writeVec2i() {
val vec = Vec2i(12369, -65891)

val expected = Unpooled.buffer()
expected.writeIntLE(vec.x)
expected.writeIntLE(vec.y)

val written = Unpooled.buffer()
written.writeVec2i(vec)

assertContentEquals(expected.array(), written.array())
}

@Test
fun readVec2i() {
val vec = Vec2i(12369, -65891)

val twoFloats = Unpooled.buffer()
twoFloats.writeIntLE(vec.x)
twoFloats.writeIntLE(vec.y)

val read = twoFloats.readVec2i()

assertEquals(vec, read)
}
}

0 comments on commit ccab412

Please sign in to comment.