Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate audio-related packets to new packet system #94

Merged
merged 4 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package org.runejs.client.message.handler.rs435;

import org.runejs.client.message.handler.MessageHandlerRegistry;
import org.runejs.client.message.handler.rs435.audio.PlayQuickSongMessageHandler;
import org.runejs.client.message.handler.rs435.audio.PlaySongMessageHandler;
import org.runejs.client.message.handler.rs435.audio.PlaySoundMessageHandler;
import org.runejs.client.message.inbound.audio.PlayQuickSongInboundMessage;
import org.runejs.client.message.inbound.audio.PlaySongInboundMessage;
import org.runejs.client.message.inbound.audio.PlaySoundInboundMessage;

/**
* A {@link MessageHandlerRegistry} for the RS revision 435 client.
*/
public class RS435HandlerRegistry extends MessageHandlerRegistry {
public RS435HandlerRegistry() {
register(PlaySongInboundMessage.class, new PlaySongMessageHandler());
register(PlayQuickSongInboundMessage.class, new PlayQuickSongMessageHandler());
register(PlaySoundInboundMessage.class, new PlaySoundMessageHandler());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.runejs.client.message.handler.rs435.audio;

import org.runejs.client.message.handler.MessageHandler;
import org.runejs.client.message.inbound.audio.PlayQuickSongInboundMessage;
import org.runejs.client.sound.MusicSystem;

public class PlayQuickSongMessageHandler implements MessageHandler<PlayQuickSongInboundMessage> {
@Override
public void handle(PlayQuickSongInboundMessage message) {
MusicSystem.playSoundJingle(message.songTimeout, message.songId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.runejs.client.message.handler.rs435.audio;

import org.runejs.client.message.handler.MessageHandler;
import org.runejs.client.message.inbound.audio.PlaySongInboundMessage;
import org.runejs.client.sound.MusicSystem;

public class PlaySongMessageHandler implements MessageHandler<PlaySongInboundMessage> {
@Override
public void handle(PlaySongInboundMessage message) {
MusicSystem.playSong(message.songId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.runejs.client.message.handler.rs435.audio;

import org.runejs.client.message.handler.MessageHandler;
import org.runejs.client.message.inbound.audio.PlaySoundInboundMessage;
import org.runejs.client.sound.SoundSystem;

public class PlaySoundMessageHandler implements MessageHandler<PlaySoundInboundMessage> {
@Override
public void handle(PlaySoundInboundMessage message) {

SoundSystem.play(message.soundId, message.volume, message.delay);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.runejs.client.message.inbound.audio;

import org.runejs.client.message.InboundMessage;

/**
* A message sent from the client to the server to play a song
*/
public class PlayQuickSongInboundMessage implements InboundMessage {
/**
* The song id to play
*/
public final int songId;

/**
* The song timeout.
*/
public final int songTimeout;

/**
* Creates a new PlaySongInboundMessage
*/
public PlayQuickSongInboundMessage(int songId, int songTimeout) {
this.songId = songId;
this.songTimeout = songTimeout;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.runejs.client.message.inbound.audio;

import org.runejs.client.message.InboundMessage;

/**
* A message sent from the client to the server to play a song
*/
public class PlaySongInboundMessage implements InboundMessage {
/**
* The song id to play
*/
public final int songId;

/**
* Creates a new PlaySongInboundMessage
*/
public PlaySongInboundMessage(int songId) {
this.songId = songId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.runejs.client.message.inbound.audio;

import org.runejs.client.message.InboundMessage;

/**
* A message sent from the client to the server to play a sound effect
*/
public class PlaySoundInboundMessage implements InboundMessage {
/**
* The sound id to play
*/
public final int soundId;

/**
* The volume to play the sound at
*/
public final int volume;

/**
* The delay to play the sound at
*/
public final int delay;

/**
* Creates a new PlaySoundInboundMessage
*/
public PlaySoundInboundMessage(int soundId, int volume, int delay) {
this.soundId = soundId;
this.volume = volume;
this.delay = delay;
}
}
32 changes: 3 additions & 29 deletions src/main/java/org/runejs/client/net/IncomingPackets.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.runejs.client.net;

import org.runejs.client.*;
import org.runejs.client.cache.def.*;
import org.runejs.client.cache.media.gameInterface.GameInterface;
import org.runejs.client.cache.media.gameInterface.InterfaceModelType;
import org.runejs.client.cache.media.gameInterface.GameInterfaceType;
import org.runejs.client.cache.media.gameInterface.InterfaceModelType;
import org.runejs.client.frame.ChatBox;
import org.runejs.client.frame.console.Console;
import org.runejs.client.input.KeyFocusListener;
Expand All @@ -26,11 +28,8 @@
import org.runejs.client.scene.tile.GenericTile;
import org.runejs.client.scene.tile.Wall;
import org.runejs.client.scene.tile.WallDecoration;
import org.runejs.client.sound.MusicSystem;
import org.runejs.client.sound.SoundSystem;
import org.runejs.client.util.TextUtils;
import org.runejs.client.*;
import org.runejs.client.cache.def.*;

public class IncomingPackets {
public static int incomingPacketSize = 0;
Expand Down Expand Up @@ -675,23 +674,6 @@ public static boolean parseIncomingPackets() {
MovedStatics.lastContinueTextWidgetId = -1;
return true;
}
if(opcode == PacketType.PLAY_SONG.getOpcode()) {
int songId = incomingPacketBuffer.getUnsignedShortLE();
if(songId == 65535)
songId = -1;
MusicSystem.playSong(songId);
opcode = -1;
return true;
}
if(opcode == PacketType.PLAY_QUICK_SONG.getOpcode()) {
int songTimeout = incomingPacketBuffer.getMediumBE();
int songId = incomingPacketBuffer.getUnsignedShortBE();
if(songId == 65535)
songId = -1;
MusicSystem.playSoundJingle(songTimeout, songId);
opcode = -1;
return true;
}
if(opcode == PacketType.UPDATE_REFERENCE_POSITION.getOpcode()) {
OverlayDefinition.placementY = incomingPacketBuffer.getUnsignedByte();
MovedStatics.placementX = incomingPacketBuffer.getUnsignedByte();
Expand Down Expand Up @@ -758,14 +740,6 @@ public static boolean parseIncomingPackets() {
opcode = -1;
return true;
}
if(opcode == PacketType.PLAY_SOUND.getOpcode()) {
int soundId = incomingPacketBuffer.getUnsignedShortBE();
int volume = incomingPacketBuffer.getUnsignedByte();
int delay = incomingPacketBuffer.getUnsignedShortBE();
SoundSystem.play(soundId, volume, delay);
opcode = -1;
return true;
}
if(opcode == 237) { // show tab overlay widget
int i_68_ = incomingPacketBuffer.getUnsignedShortBE();
GameInterface.resetInterfaceAnimations(i_68_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.runejs.client.net.PacketType;
import org.runejs.client.net.codec.MessagePacketCodec;
import org.runejs.client.net.codec.runejs435.decoder.audio.PlayQuickSongMessageDecoder;
import org.runejs.client.net.codec.runejs435.decoder.audio.PlaySongMessageDecoder;
import org.runejs.client.net.codec.runejs435.decoder.audio.PlaySoundMessageDecoder;

/**
* A {@link MessagePacketCodec} for the RuneJS customised 435 protocol.
Expand All @@ -18,5 +21,8 @@ private void registerEncoders() {
}

private void registerDecoders() {
register(PacketType.PLAY_SONG.getOpcode(), new PlaySongMessageDecoder());
register(PacketType.PLAY_QUICK_SONG.getOpcode(), new PlayQuickSongMessageDecoder());
register(PacketType.PLAY_SOUND.getOpcode(), new PlaySoundMessageDecoder());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.runejs.client.net.codec.runejs435.decoder.audio;

import org.runejs.client.message.inbound.audio.PlaySongInboundMessage;
import org.runejs.client.net.PacketBuffer;
import org.runejs.client.net.codec.MessageDecoder;

/**
* Decodes the PlayQuickSong message
*/
public class PlayQuickSongMessageDecoder implements MessageDecoder<PlaySongInboundMessage> {
@Override
public PlaySongInboundMessage decode(PacketBuffer buffer) {
int songId = buffer.getUnsignedShortLE();
if(songId == 65535)
songId = -1;

return new PlaySongInboundMessage(songId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.runejs.client.net.codec.runejs435.decoder.audio;

import org.runejs.client.message.inbound.audio.PlaySongInboundMessage;
import org.runejs.client.net.PacketBuffer;
import org.runejs.client.net.codec.MessageDecoder;

public class PlaySongMessageDecoder implements MessageDecoder<PlaySongInboundMessage> {
@Override
public PlaySongInboundMessage decode(PacketBuffer buffer) {
int songId = buffer.getUnsignedShortLE();
if(songId == 65535)
songId = -1;

return new PlaySongInboundMessage(songId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.runejs.client.net.codec.runejs435.decoder.audio;

import org.runejs.client.message.inbound.audio.PlaySoundInboundMessage;
import org.runejs.client.net.PacketBuffer;
import org.runejs.client.net.codec.MessageDecoder;

/**
* Decodes the PlaySoundInboundMessage
*/
public class PlaySoundMessageDecoder implements MessageDecoder<PlaySoundInboundMessage> {
@Override
public PlaySoundInboundMessage decode(PacketBuffer buffer) {
int soundId = buffer.getUnsignedShortBE();
int volume = buffer.getUnsignedByte();
int delay = buffer.getUnsignedShortBE();

return new PlaySoundInboundMessage(soundId, volume, delay);
}
}