Skip to content

Commit

Permalink
feat: all sound/music related server -> client packets
Browse files Browse the repository at this point in the history
  • Loading branch information
Z-Kris committed Apr 6, 2024
1 parent 7cb9f17 commit 2d7412e
Show file tree
Hide file tree
Showing 14 changed files with 628 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.rsprot.protocol.game.outgoing.codec.sound

import io.netty.channel.ChannelHandlerContext
import net.rsprot.buffer.JagByteBuf
import net.rsprot.protocol.ServerProt
import net.rsprot.protocol.game.outgoing.prot.GameServerProt
import net.rsprot.protocol.game.outgoing.sound.MidiJingle
import net.rsprot.protocol.message.codec.MessageEncoder

public class MidiJingleEncoder : MessageEncoder<MidiJingle> {
override val prot: ServerProt = GameServerProt.MIDI_JINGLE

override fun encode(
ctx: ChannelHandlerContext,
buffer: JagByteBuf,
message: MidiJingle,
) {
buffer.p3(message.lengthInMillis)
buffer.p2Alt1(message.id)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.rsprot.protocol.game.outgoing.codec.sound

import io.netty.channel.ChannelHandlerContext
import net.rsprot.buffer.JagByteBuf
import net.rsprot.protocol.ServerProt
import net.rsprot.protocol.game.outgoing.prot.GameServerProt
import net.rsprot.protocol.game.outgoing.sound.MidiSong
import net.rsprot.protocol.message.codec.MessageEncoder

public class MidiSongEncoder : MessageEncoder<MidiSong> {
override val prot: ServerProt = GameServerProt.MIDI_SONG

override fun encode(
ctx: ChannelHandlerContext,
buffer: JagByteBuf,
message: MidiSong,
) {
// The order in the client remains the same for the function call at the end
// of the packet, as:
// playSongList(ids, fadeOutDelay, fadeOutSpeed, fadeInDelay, fadeInSpeed)
buffer.p2Alt1(message.fadeOutDelay)
buffer.p2Alt3(message.fadeOutSpeed)
buffer.p2Alt2(message.id)
buffer.p2Alt1(message.fadeInDelay)
buffer.p2Alt1(message.fadeInSpeed)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.rsprot.protocol.game.outgoing.codec.sound

import io.netty.channel.ChannelHandlerContext
import net.rsprot.buffer.JagByteBuf
import net.rsprot.protocol.ServerProt
import net.rsprot.protocol.game.outgoing.prot.GameServerProt
import net.rsprot.protocol.game.outgoing.sound.MidiSongOld
import net.rsprot.protocol.message.codec.MessageEncoder

public class MidiSongOldEncoder : MessageEncoder<MidiSongOld> {
override val prot: ServerProt = GameServerProt.MIDI_SONG_OLD

override fun encode(
ctx: ChannelHandlerContext,
buffer: JagByteBuf,
message: MidiSongOld,
) {
buffer.p2Alt1(message.id)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.rsprot.protocol.game.outgoing.codec.sound

import io.netty.channel.ChannelHandlerContext
import net.rsprot.buffer.JagByteBuf
import net.rsprot.protocol.ServerProt
import net.rsprot.protocol.game.outgoing.prot.GameServerProt
import net.rsprot.protocol.game.outgoing.sound.MidiSongStop
import net.rsprot.protocol.message.codec.MessageEncoder

public class MidiSongStopEncoder : MessageEncoder<MidiSongStop> {
override val prot: ServerProt = GameServerProt.MIDI_SONG_STOP

override fun encode(
ctx: ChannelHandlerContext,
buffer: JagByteBuf,
message: MidiSongStop,
) {
// The order in the client remains the same for the function call at the end
// of the packet, as:
// fadeOut(fadeOutDelay, fadeOutSpeed)
buffer.p2Alt2(message.fadeOutDelay)
buffer.p2Alt3(message.fadeOutSpeed)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.rsprot.protocol.game.outgoing.codec.sound

import io.netty.channel.ChannelHandlerContext
import net.rsprot.buffer.JagByteBuf
import net.rsprot.protocol.ServerProt
import net.rsprot.protocol.game.outgoing.prot.GameServerProt
import net.rsprot.protocol.game.outgoing.sound.MidiSongWithSecondary
import net.rsprot.protocol.message.codec.MessageEncoder

public class MidiSongWithSecondaryEncoder : MessageEncoder<MidiSongWithSecondary> {
override val prot: ServerProt = GameServerProt.MIDI_SONG_WITHSECONDARY

override fun encode(
ctx: ChannelHandlerContext,
buffer: JagByteBuf,
message: MidiSongWithSecondary,
) {
// The order in the client remains the same for the function call at the end
// of the packet, as (the ids list has primary id as the first song):
// playSongList(ids, fadeOutDelay, fadeOutSpeed, fadeInDelay, fadeInSpeed)
buffer.p2Alt2(message.primaryId)
buffer.p2Alt1(message.fadeInSpeed)
buffer.p2(message.secondaryId)
buffer.p2(message.fadeInDelay)
buffer.p2Alt3(message.fadeOutSpeed)
buffer.p2Alt1(message.fadeOutDelay)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.rsprot.protocol.game.outgoing.codec.sound

import io.netty.channel.ChannelHandlerContext
import net.rsprot.buffer.JagByteBuf
import net.rsprot.protocol.ServerProt
import net.rsprot.protocol.game.outgoing.prot.GameServerProt
import net.rsprot.protocol.game.outgoing.sound.MidiSwap
import net.rsprot.protocol.message.codec.MessageEncoder

public class MidiSwapEncoder : MessageEncoder<MidiSwap> {
override val prot: ServerProt = GameServerProt.MIDI_SWAP

override fun encode(
ctx: ChannelHandlerContext,
buffer: JagByteBuf,
message: MidiSwap,
) {
// The order in the client remains the same for the function call at the end
// of the packet, as:
// swap(fadeOutDelay, fadeOutSpeed, fadeInDelay, fadeInSpeed)
buffer.p2(message.fadeInSpeed)
buffer.p2Alt2(message.fadeOutDelay)
buffer.p2Alt3(message.fadeInDelay)
buffer.p2Alt3(message.fadeOutSpeed)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.rsprot.protocol.game.outgoing.codec.sound

import io.netty.channel.ChannelHandlerContext
import net.rsprot.buffer.JagByteBuf
import net.rsprot.protocol.ServerProt
import net.rsprot.protocol.game.outgoing.prot.GameServerProt
import net.rsprot.protocol.game.outgoing.sound.SynthSound
import net.rsprot.protocol.message.codec.MessageEncoder
import net.rsprot.protocol.metadata.Consistent

@Consistent
public class SynthSoundEncoder : MessageEncoder<SynthSound> {
override val prot: ServerProt = GameServerProt.SYNTH_SOUND

override fun encode(
ctx: ChannelHandlerContext,
buffer: JagByteBuf,
message: SynthSound,
) {
buffer.p2(message.id)
buffer.p1(message.loops)
buffer.p2(message.delay)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package net.rsprot.protocol.game.outgoing.sound

import net.rsprot.protocol.message.OutgoingMessage

/**
* Midi jingle packet is used to play a short midi song, typically when
* the player accomplishes something. The normal song that was playing
* will be resumed after the jingle finishes playing.
* In the old days, the [lengthInMillis] property was used to tell the client
* how long the jingle lasts, so it knows when to resume the normal midi song.
* It has long since been removed, however - while the client expects a 24-bit
* integer for the length, it does not use this value in any way.
* @property id the id of the jingle to play
* @property lengthInMillis the length in milliseconds of the jingle, now unused.
*/
public class MidiJingle private constructor(
private val _id: UShort,
public val lengthInMillis: Int,
) : OutgoingMessage {
public constructor(
id: Int,
) : this(
id.toUShort(),
0,
)

public constructor(
id: Int,
lengthInMillis: Int,
) : this(
id.toUShort(),
lengthInMillis,
)

public val id: Int
get() = _id.toInt()

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as MidiJingle

if (_id != other._id) return false
if (lengthInMillis != other.lengthInMillis) return false

return true
}

override fun hashCode(): Int {
var result = _id.hashCode()
result = 31 * result + lengthInMillis
return result
}

override fun toString(): String {
return "MidiJingle(" +
"id=$id, " +
"lengthInMillis=$lengthInMillis" +
")"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package net.rsprot.protocol.game.outgoing.sound

import net.rsprot.protocol.message.OutgoingMessage

/**
* Midi song packets are used to play songs through the music player.
* @property id the id of the midi song
* @property fadeOutDelay the delay in client cycles (20ms/cc) until the old song
* begins fading out. The default value for this, based on the old midi song packet, is 0.
* @property fadeOutSpeed the speed at which the old song fades out in client cycles (20ms/cc).
* The default value for this, based on the old midi song packet, is 60.
* @property fadeInDelay the delay until the new song begins playing, in client cycles (20ms/cc).
* The default value for this, based on the old midi song packet is 60.
* @property fadeInSpeed the speed at which the new song fades in, in client cycles (20ms/cc).
* The default value for this, based on the old midi song packet is 0.
*/
@Suppress("DuplicatedCode", "MemberVisibilityCanBePrivate")
public class MidiSong private constructor(
private val _id: UShort,
private val _fadeOutDelay: UShort,
private val _fadeOutSpeed: UShort,
private val _fadeInDelay: UShort,
private val _fadeInSpeed: UShort,
) : OutgoingMessage {
public constructor(
id: Int,
fadeOutDelay: Int,
fadeOutSpeed: Int,
fadeInDelay: Int,
fadeInSpeed: Int,
) : this(
id.toUShort(),
fadeOutDelay.toUShort(),
fadeOutSpeed.toUShort(),
fadeInDelay.toUShort(),
fadeInSpeed.toUShort(),
)

public val id: Int
get() = _id.toInt()
public val fadeOutDelay: Int
get() = _fadeOutDelay.toInt()
public val fadeOutSpeed: Int
get() = _fadeOutSpeed.toInt()
public val fadeInDelay: Int
get() = _fadeInDelay.toInt()
public val fadeInSpeed: Int
get() = _fadeInSpeed.toInt()

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as MidiSong

if (_id != other._id) return false
if (_fadeOutDelay != other._fadeOutDelay) return false
if (_fadeOutSpeed != other._fadeOutSpeed) return false
if (_fadeInDelay != other._fadeInDelay) return false
if (_fadeInSpeed != other._fadeInSpeed) return false

return true
}

override fun hashCode(): Int {
var result = _id.hashCode()
result = 31 * result + _fadeOutDelay.hashCode()
result = 31 * result + _fadeOutSpeed.hashCode()
result = 31 * result + _fadeInDelay.hashCode()
result = 31 * result + _fadeInSpeed.hashCode()
return result
}

override fun toString(): String {
return "MidiSong(" +
"id=$id, " +
"fadeOutDelay=$fadeOutDelay, " +
"fadeOutSpeed=$fadeOutSpeed, " +
"fadeInDelay=$fadeInDelay, " +
"fadeInSpeed=$fadeInSpeed" +
")"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.rsprot.protocol.game.outgoing.sound

import net.rsprot.protocol.message.OutgoingMessage

/**
* Midi song old packet is used to play a midi song, in the old format.
* This is equal to playing [MidiSong] with the arguments of `id, 0, 60, 60, 0`.
* @property id the id of the song to play
*/
public class MidiSongOld(
public val id: Int,
) : OutgoingMessage {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as MidiSongOld

return id == other.id
}

override fun hashCode(): Int {
return id
}

override fun toString(): String {
return "MidiSongOld(id=$id)"
}
}
Loading

0 comments on commit 2d7412e

Please sign in to comment.