Arcturus-Community/src/main/java/com/eu/habbo/messages/ServerMessage.java

194 lines
4.9 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.messages;
import com.eu.habbo.Emulator;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.buffer.Unpooled;
2018-09-28 21:25:00 +02:00
import java.io.IOException;
import java.nio.charset.Charset;
2019-05-26 20:14:53 +02:00
public class ServerMessage {
2018-07-06 15:30:00 +02:00
private int header;
private ByteBufOutputStream stream;
private ByteBuf channelBuffer;
2019-05-26 20:14:53 +02:00
public ServerMessage() {
2018-09-28 21:25:00 +02:00
this.channelBuffer = Unpooled.buffer();
this.stream = new ByteBufOutputStream(this.channelBuffer);
}
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public ServerMessage(int header) {
2018-07-06 15:30:00 +02:00
this.header = header;
this.channelBuffer = Unpooled.buffer();
this.stream = new ByteBufOutputStream(this.channelBuffer);
2019-05-26 20:14:53 +02:00
try {
2018-07-06 15:30:00 +02:00
this.stream.writeInt(0);
this.stream.writeShort(header);
2019-05-26 20:14:53 +02:00
} catch (Exception e) {
2018-07-06 15:30:00 +02:00
Emulator.getLogging().handleException(e);
}
}
2019-05-26 20:14:53 +02:00
public ServerMessage init(int id) {
2018-07-06 15:30:00 +02:00
this.header = id;
this.channelBuffer = Unpooled.buffer();
this.stream = new ByteBufOutputStream(this.channelBuffer);
2019-05-26 20:14:53 +02:00
try {
2018-07-06 15:30:00 +02:00
this.stream.writeInt(0);
this.stream.writeShort(id);
2019-05-26 20:14:53 +02:00
} catch (Exception e) {
2018-07-06 15:30:00 +02:00
Emulator.getLogging().handleException(e);
}
return this;
}
2019-05-26 20:14:53 +02:00
public void appendRawBytes(byte[] bytes) {
try {
2018-07-06 15:30:00 +02:00
this.stream.write(bytes);
2019-05-26 20:14:53 +02:00
} catch (IOException e) {
2018-07-06 15:30:00 +02:00
Emulator.getLogging().logPacketError(e);
}
}
2019-05-26 20:14:53 +02:00
public void appendString(String obj) {
if (obj == null) {
2018-07-06 15:30:00 +02:00
this.appendString("");
return;
}
2019-05-26 20:14:53 +02:00
try {
2018-07-06 15:30:00 +02:00
byte[] data = obj.getBytes();
this.stream.writeShort(data.length);
this.stream.write(data);
2019-05-26 20:14:53 +02:00
} catch (IOException e) {
2018-07-06 15:30:00 +02:00
Emulator.getLogging().logPacketError(e);
}
}
2019-05-26 20:14:53 +02:00
public void appendChar(int obj) {
try {
2018-07-06 15:30:00 +02:00
this.stream.writeChar(obj);
2019-05-26 20:14:53 +02:00
} catch (IOException e) {
2018-07-06 15:30:00 +02:00
Emulator.getLogging().logPacketError(e);
}
}
2019-05-26 20:14:53 +02:00
public void appendChars(Object obj) {
try {
2018-07-06 15:30:00 +02:00
this.stream.writeChars(obj.toString());
2019-05-26 20:14:53 +02:00
} catch (IOException e) {
2018-07-06 15:30:00 +02:00
Emulator.getLogging().logPacketError(e);
}
}
2019-05-26 20:14:53 +02:00
public void appendInt(Integer obj) {
try {
2019-03-18 02:22:00 +01:00
this.stream.writeInt(obj);
2019-05-26 20:14:53 +02:00
} catch (IOException e) {
2018-07-06 15:30:00 +02:00
Emulator.getLogging().logPacketError(e);
}
}
2019-05-26 20:14:53 +02:00
public void appendInt(Short obj) {
2018-07-06 15:30:00 +02:00
this.appendShort(0);
this.appendShort(obj);
}
2019-05-26 20:14:53 +02:00
public void appendInt(Byte obj) {
try {
2018-07-06 15:30:00 +02:00
this.stream.writeInt((int) obj);
2019-05-26 20:14:53 +02:00
} catch (IOException e) {
2018-07-06 15:30:00 +02:00
Emulator.getLogging().logPacketError(e);
}
}
2019-05-26 20:14:53 +02:00
public void appendInt(Boolean obj) {
try {
2018-07-06 15:30:00 +02:00
this.stream.writeInt(obj ? 1 : 0);
2019-05-26 20:14:53 +02:00
} catch (IOException e) {
2018-07-06 15:30:00 +02:00
Emulator.getLogging().logPacketError(e);
}
}
2019-05-26 20:14:53 +02:00
public void appendShort(int obj) {
try {
2018-07-06 15:30:00 +02:00
this.stream.writeShort((short) obj);
2019-05-26 20:14:53 +02:00
} catch (IOException e) {
2018-07-06 15:30:00 +02:00
Emulator.getLogging().logPacketError(e);
}
}
2019-05-26 20:14:53 +02:00
public void appendByte(Integer b) {
try {
2019-03-18 02:22:00 +01:00
this.stream.writeByte(b);
2019-05-26 20:14:53 +02:00
} catch (IOException e) {
2018-07-06 15:30:00 +02:00
Emulator.getLogging().logPacketError(e);
}
}
2019-05-26 20:14:53 +02:00
public void appendBoolean(Boolean obj) {
try {
2019-03-18 02:22:00 +01:00
this.stream.writeBoolean(obj);
2019-05-26 20:14:53 +02:00
} catch (IOException e) {
2018-07-06 15:30:00 +02:00
Emulator.getLogging().logPacketError(e);
}
}
2019-05-26 20:14:53 +02:00
public void appendDouble(double d) {
try {
2018-07-06 15:30:00 +02:00
this.stream.writeDouble(d);
2019-05-26 20:14:53 +02:00
} catch (IOException e) {
2018-07-06 15:30:00 +02:00
Emulator.getLogging().logPacketError(e);
}
}
2019-05-26 20:14:53 +02:00
public void appendDouble(Double obj) {
try {
2019-03-18 02:22:00 +01:00
this.stream.writeDouble(obj);
2019-05-26 20:14:53 +02:00
} catch (IOException e) {
2018-07-06 15:30:00 +02:00
Emulator.getLogging().logPacketError(e);
}
}
2019-05-26 20:14:53 +02:00
public ServerMessage appendResponse(ServerMessage obj) {
try {
2018-07-06 15:30:00 +02:00
this.stream.write(obj.get().array());
2019-05-26 20:14:53 +02:00
} catch (IOException e) {
2018-07-06 15:30:00 +02:00
Emulator.getLogging().logPacketError(e);
}
return this;
}
2019-05-26 20:14:53 +02:00
public void append(ISerialize obj) {
2018-07-06 15:30:00 +02:00
obj.serialize(this);
}
2019-05-26 20:14:53 +02:00
public String getBodyString() {
2018-07-06 15:30:00 +02:00
ByteBuf buffer = this.stream.buffer().duplicate();
buffer.setInt(0, buffer.writerIndex() - 4);
String consoleText = buffer.toString(Charset.forName("UTF-8"));
for (int i = 0; i < 14; i++) {
2019-05-26 20:14:53 +02:00
consoleText = consoleText.replace(Character.toString((char) i), "[" + i + "]");
2018-07-06 15:30:00 +02:00
}
buffer.discardSomeReadBytes();
return consoleText;
}
2019-05-26 20:14:53 +02:00
public int getHeader() {
2018-07-06 15:30:00 +02:00
return this.header;
}
2019-05-26 20:14:53 +02:00
public ByteBuf get() {
2018-07-06 15:30:00 +02:00
this.channelBuffer.setInt(0, this.channelBuffer.writerIndex() - 4);
return this.channelBuffer.copy();
}
}