Skip to content

Commit

Permalink
WIP jackson serialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Dec 4, 2023
1 parent d4ecf36 commit 6338110
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 55 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ repositories {
dependencies {
// package libraries
api(kotlin("stdlib"))
api(libs.kotlin)
// api(libs.kotlin)
api(libs.lavalink.protocol)
api(libs.okhttp)
api(libs.jackson)
api(libs.bundles.reactor)
api(libs.logger.api)

Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fun VersionCatalogBuilder.versionRefs() {

fun VersionCatalogBuilder.common() {
library("kotlin", "org.jetbrains.kotlin", "kotlin-stdlib").versionRef("kotlin")
library("jackson", "com.fasterxml.jackson.core", "jackson-core").version("2.16.0")
library("lavalink-protocol", "dev.arbjerg.lavalink", "protocol").version("4.0.0")
library("okhttp", "com.squareup.okhttp3", "okhttp").version("4.10.0")
library("reactor-core", "io.projectreactor", "reactor-core").version("3.5.6")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
package dev.arbjerg.lavalink.client

import dev.arbjerg.lavalink.protocol.v4.LoadResult
import dev.arbjerg.lavalink.client.protocol.*
import java.util.function.Consumer

abstract class AbstractAudioLoadResultHandler : Consumer<LoadResult> {
override fun accept(loadResult: LoadResult) {
abstract class AbstractAudioLoadResultHandler : Consumer<LavalinkLoadResult> {
override fun accept(loadResult: LavalinkLoadResult) {
when (loadResult) {
is LoadResult.TrackLoaded -> {
is TrackLoaded -> {
this.ontrackLoaded(loadResult)
}

is LoadResult.PlaylistLoaded -> {
is PlaylistLoaded -> {
this.onPlaylistLoaded(loadResult)
}

is LoadResult.SearchResult -> {
is SearchResult -> {
this.onSearchResultLoaded(loadResult)
}

is LoadResult.NoMatches -> {
is NoMatches -> {
this.noMatches()
}

is LoadResult.LoadFailed -> {
is LoadFailed -> {
this.loadFailed(loadResult)
}
}
}

abstract fun ontrackLoaded(result: LoadResult.TrackLoaded)
abstract fun onPlaylistLoaded(result: LoadResult.PlaylistLoaded)
abstract fun onSearchResultLoaded(result: LoadResult.SearchResult)
abstract fun ontrackLoaded(result: TrackLoaded)
abstract fun onPlaylistLoaded(result: PlaylistLoaded)
abstract fun onSearchResultLoaded(result: SearchResult)
abstract fun noMatches()
abstract fun loadFailed(result: LoadResult.LoadFailed)
abstract fun loadFailed(result: LoadFailed)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.arbjerg.lavalink.client

import dev.arbjerg.lavalink.client.protocol.Track
import dev.arbjerg.lavalink.protocol.v4.Filters
import dev.arbjerg.lavalink.protocol.v4.Track
import dev.arbjerg.lavalink.protocol.v4.VoiceState

interface IUpdatablePlayer {
Expand Down Expand Up @@ -32,12 +32,15 @@ interface IUpdatablePlayer {
*
* @param encodedTrack The encoded track to be played. Set it to {@code null} to make the player stop playing.
*/
@Deprecated(message = "Use setTrack instead")
fun setEncodedTrack(encodedTrack: String?): PlayerUpdateBuilder

/**
* Omits the encoded track field from being sent to the node during updates.
*/
@Deprecated(message = "Use setTrack instead")
fun omitEncodedTrack(): PlayerUpdateBuilder
@Deprecated(message = "Use setTrack instead")
fun setIdentifier(identifier: String?): PlayerUpdateBuilder
fun setPosition(position: Long?): PlayerUpdateBuilder
fun setEndTime(endTime: Long?): PlayerUpdateBuilder
Expand Down
11 changes: 8 additions & 3 deletions src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package dev.arbjerg.lavalink.client

import dev.arbjerg.lavalink.client.http.HttpBuilder
import dev.arbjerg.lavalink.client.loadbalancing.IRegionFilter
import dev.arbjerg.lavalink.client.protocol.LavalinkLoadResult
import dev.arbjerg.lavalink.client.protocol.Track
import dev.arbjerg.lavalink.client.protocol.toLavalinkLoadResult
import dev.arbjerg.lavalink.internal.LavalinkRestClient
import dev.arbjerg.lavalink.internal.LavalinkSocket
import dev.arbjerg.lavalink.internal.loadbalancing.Penalties
Expand Down Expand Up @@ -164,10 +167,10 @@ class LavalinkNode(
*
* @return The [LoadResult] of whatever you tried to load.
*/
fun loadItem(identifier: String): Mono<LoadResult> {
fun loadItem(identifier: String): Mono<LavalinkLoadResult> {
if (!available) return Mono.error(IllegalStateException("Node is not available"))

return rest.loadItem(identifier)
return rest.loadItem(identifier).map { it.toLavalinkLoadResult() }
}

/**
Expand All @@ -181,6 +184,7 @@ class LavalinkNode(
if (!available) return Mono.error(IllegalStateException("Node is not available"))

return rest.decodeTrack(encoded)
.map { Track(it) }
}

/**
Expand All @@ -190,10 +194,11 @@ class LavalinkNode(
*
* @return The decoded tracks.
*/
fun decodeTracks(encoded: List<String>): Mono<Tracks> {
fun decodeTracks(encoded: List<String>): Mono<List<Track>> {
if (!available) return Mono.error(IllegalStateException("Node is not available"))

return rest.decodeTracks(encoded)
.map { it.tracks.map { track -> Track(track) } }
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkPlayer.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.arbjerg.lavalink.client

import dev.arbjerg.lavalink.client.protocol.Track
import dev.arbjerg.lavalink.protocol.v4.*
import kotlin.math.min

Expand Down Expand Up @@ -45,12 +46,15 @@ class LavalinkPlayer(private val node: LavalinkNode, protocolPlayer: Player) : I
override fun stopTrack() = PlayerUpdateBuilder(node, guildId)
.stopTrack()

@Deprecated("Use setTrack instead")
override fun setEncodedTrack(encodedTrack: String?) = PlayerUpdateBuilder(node, guildId)
.setEncodedTrack(encodedTrack)

@Deprecated("Use setTrack instead")
override fun omitEncodedTrack() = PlayerUpdateBuilder(node, guildId)
.omitEncodedTrack()

@Deprecated("Use setTrack instead")
override fun setIdentifier(identifier: String?) = PlayerUpdateBuilder(node, guildId)
.setIdentifier(identifier)

Expand All @@ -77,8 +81,7 @@ class LavalinkPlayer(private val node: LavalinkNode, protocolPlayer: Player) : I

// For re-creating the player
internal fun stateToBuilder() = PlayerUpdateBuilder(node, guildId)
.setEncodedTrack(track?.encoded)
.setIdentifier(null)
.setTrack(track?.let { Track(it) })
.setPosition(position)
.setEndTime(null) // TODO Should we keep track of the end time?
.setVolume(volume)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package dev.arbjerg.lavalink.client

import dev.arbjerg.lavalink.client.protocol.Track
import dev.arbjerg.lavalink.internal.toLavalinkPlayer
import dev.arbjerg.lavalink.protocol.v4.*
import kotlinx.serialization.json.JsonObject
import org.checkerframework.checker.optional.qual.Present
import reactor.core.publisher.Mono
import kotlin.math.max
import kotlin.math.min

class PlayerUpdateBuilder internal constructor(private val node: LavalinkNode, private val guildId: Long) : IUpdatablePlayer {
private var encodedTrack: Omissible<String?> = Omissible.omitted()
Expand All @@ -21,8 +19,8 @@ class PlayerUpdateBuilder internal constructor(private val node: LavalinkNode, p
private var noReplace = false

override fun setTrack(track: Track?): PlayerUpdateBuilder {
this.encodedTrack = Omissible.of(track?.encoded)
this.trackUserData = track?.userData.toOmissible()
this.encodedTrack = Omissible.of(track?.internalTrack?.encoded)
this.trackUserData = track?.internalTrack?.userData.toOmissible()
return this
}

Expand All @@ -33,16 +31,19 @@ class PlayerUpdateBuilder internal constructor(private val node: LavalinkNode, p
return this
}

@Deprecated("Use setTrack instead")
override fun setEncodedTrack(encodedTrack: String?): PlayerUpdateBuilder {
this.encodedTrack = Omissible.of(encodedTrack)
return this
}

@Deprecated("Use setTrack instead")
override fun omitEncodedTrack(): PlayerUpdateBuilder {
this.encodedTrack = Omissible.omitted()
return this
}

@Deprecated("Use setTrack instead")
override fun setIdentifier(identifier: String?): PlayerUpdateBuilder {
this.identifier = identifier.toOmissible()
return this
Expand Down
17 changes: 13 additions & 4 deletions src/main/kotlin/dev/arbjerg/lavalink/client/events.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.arbjerg.lavalink.client

import dev.arbjerg.lavalink.client.protocol.Track
import dev.arbjerg.lavalink.protocol.v4.Message

internal fun Message.toClientEvent(node: LavalinkNode) = when (this) {
Expand Down Expand Up @@ -30,16 +31,24 @@ sealed class EmittedEvent<T : Message.EmittedEvent>(override val node: LavalinkN
: ClientEvent<T>(node, event)

data class TrackStartEvent(override val node: LavalinkNode, override val event: Message.EmittedEvent.TrackStartEvent)
: EmittedEvent<Message.EmittedEvent.TrackStartEvent>(node, event)
: EmittedEvent<Message.EmittedEvent.TrackStartEvent>(node, event) {
val track = Track(event.track)
}

data class TrackEndEvent(override val node: LavalinkNode, override val event: Message.EmittedEvent.TrackEndEvent)
: EmittedEvent<Message.EmittedEvent.TrackEndEvent>(node, event)
: EmittedEvent<Message.EmittedEvent.TrackEndEvent>(node, event) {
val track = Track(event.track)
}

data class TrackExceptionEvent(override val node: LavalinkNode, override val event: Message.EmittedEvent.TrackExceptionEvent)
: EmittedEvent<Message.EmittedEvent.TrackExceptionEvent>(node, event)
: EmittedEvent<Message.EmittedEvent.TrackExceptionEvent>(node, event) {
val track = Track(event.track)
}

data class TrackStuckEvent(override val node: LavalinkNode, override val event: Message.EmittedEvent.TrackStuckEvent)
: EmittedEvent<Message.EmittedEvent.TrackStuckEvent>(node, event)
: EmittedEvent<Message.EmittedEvent.TrackStuckEvent>(node, event) {
val track = Track(event.track)
}

data class WebSocketClosedEvent(override val node: LavalinkNode, override val event: Message.EmittedEvent.WebSocketClosedEvent)
: EmittedEvent<Message.EmittedEvent.WebSocketClosedEvent>(node, event)
26 changes: 26 additions & 0 deletions src/main/kotlin/dev/arbjerg/lavalink/client/protocol/Track.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dev.arbjerg.lavalink.client.protocol

import dev.arbjerg.lavalink.internal.fromJsonElement
import dev.arbjerg.lavalink.protocol.v4.deserialize
import dev.arbjerg.lavalink.protocol.v4.json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.encodeToJsonElement
import kotlinx.serialization.serializer
import kotlin.reflect.KClass
import dev.arbjerg.lavalink.protocol.v4.Track as ProtocolTrack

class Track internal constructor(internal var internalTrack: ProtocolTrack) {
val info = internalTrack.info
val pluginInfo = internalTrack.pluginInfo

fun setUserData(userData: Any) {
internalTrack = internalTrack.copyWithUserData(
json.encodeToJsonElement(userData) as JsonObject
)
}

fun <T> getUserData(klass: Class<T>): T {
return fromJsonElement(internalTrack.userData, klass)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dev.arbjerg.lavalink.client.protocol

import dev.arbjerg.lavalink.protocol.v4.LoadResult

sealed class LavalinkLoadResult

fun LoadResult.toLavalinkLoadResult() = when (this) {
is LoadResult.TrackLoaded -> TrackLoaded(this)
is LoadResult.PlaylistLoaded -> PlaylistLoaded(this)
is LoadResult.SearchResult -> SearchResult(this)
is LoadResult.NoMatches -> NoMatches()
is LoadResult.LoadFailed -> LoadFailed(this)
}

class TrackLoaded(result: LoadResult.TrackLoaded) : LavalinkLoadResult() {
val track = Track(result.data)
}

class PlaylistLoaded(result: LoadResult.PlaylistLoaded) : LavalinkLoadResult() {
val info = result.data.info
val pluginInfo = result.data.pluginInfo
val tracks = result.data.tracks.map { Track(it) }
}

class NoMatches : LavalinkLoadResult() {}

class SearchResult(result: LoadResult.SearchResult) : LavalinkLoadResult() {
val tracks = result.data.tracks.map { Track(it) }
}

class LoadFailed(result: LoadResult.LoadFailed) : LavalinkLoadResult() {
val exception = result.data
}
20 changes: 20 additions & 0 deletions src/main/kotlin/dev/arbjerg/lavalink/internal/JsonParser.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.arbjerg.lavalink.internal

import com.fasterxml.jackson.databind.ObjectMapper
import dev.arbjerg.lavalink.protocol.v4.json
import kotlinx.serialization.json.JsonElement


private val objectMapper = ObjectMapper()

fun toJsonElement(obj: Any): JsonElement {
val jsonString = objectMapper.writeValueAsString(obj)

return json.parseToJsonElement(jsonString)
}

fun <T> fromJsonElement(jsonElement: JsonElement, klass: Class<T>): T {
val stringValue = jsonElement.toString()

return objectMapper.readValue(stringValue, klass)
}
Loading

0 comments on commit 6338110

Please sign in to comment.