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

262 lines
5.3 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;
2018-07-06 15:30:00 +02:00
public class ServerMessage
{
private int header;
private ByteBufOutputStream stream;
private ByteBuf channelBuffer;
2018-09-28 21:25:00 +02:00
public ServerMessage()
{
this.channelBuffer = Unpooled.buffer();
this.stream = new ByteBufOutputStream(this.channelBuffer);
}
2018-07-06 15:30:00 +02:00
public ServerMessage(int header)
{
this.header = header;
this.channelBuffer = Unpooled.buffer();
this.stream = new ByteBufOutputStream(this.channelBuffer);
try
{
this.stream.writeInt(0);
this.stream.writeShort(header);
}
catch (Exception e)
{
Emulator.getLogging().handleException(e);
}
}
public ServerMessage init(int id)
{
this.header = id;
this.channelBuffer = Unpooled.buffer();
this.stream = new ByteBufOutputStream(this.channelBuffer);
try
{
this.stream.writeInt(0);
this.stream.writeShort(id);
}
catch (Exception e)
{
Emulator.getLogging().handleException(e);
}
return this;
}
public void appendRawBytes(byte[] bytes)
{
try
{
this.stream.write(bytes);
}
catch(IOException e)
{
Emulator.getLogging().logPacketError(e);
}
}
public void appendString(String obj)
{
if (obj == null)
{
this.appendString("");
return;
}
try
{
byte[] data = obj.getBytes();
this.stream.writeShort(data.length);
this.stream.write(data);
}
catch(IOException e)
{
Emulator.getLogging().logPacketError(e);
}
}
public void appendChar(int obj)
{
try
{
this.stream.writeChar(obj);
}
catch(IOException e)
{
Emulator.getLogging().logPacketError(e);
}
}
public void appendChars(Object obj)
{
try
{
this.stream.writeChars(obj.toString());
}
catch(IOException e)
{
Emulator.getLogging().logPacketError(e);
}
}
public void appendInt(Integer obj)
{
try
{
2019-03-18 02:22:00 +01:00
this.stream.writeInt(obj);
2018-07-06 15:30:00 +02:00
}
catch(IOException e)
{
Emulator.getLogging().logPacketError(e);
}
}
2018-10-07 00:28:00 +02:00
public void appendInt(Short obj)
2018-07-06 15:30:00 +02:00
{
this.appendShort(0);
this.appendShort(obj);
}
2018-10-07 00:28:00 +02:00
public void appendInt(Byte obj)
2018-07-06 15:30:00 +02:00
{
try
{
this.stream.writeInt((int) obj);
}
catch (IOException e)
{
Emulator.getLogging().logPacketError(e);
}
}
2018-10-07 00:28:00 +02:00
public void appendInt(Boolean obj)
2018-07-06 15:30:00 +02:00
{
try
{
this.stream.writeInt(obj ? 1 : 0);
}
catch(IOException e)
{
Emulator.getLogging().logPacketError(e);
}
}
public void appendShort(int obj)
{
try
{
this.stream.writeShort((short) obj);
}
catch(IOException e)
{
Emulator.getLogging().logPacketError(e);
}
}
public void appendByte(Integer b)
{
try
{
2019-03-18 02:22:00 +01:00
this.stream.writeByte(b);
2018-07-06 15:30:00 +02:00
}
catch(IOException e)
{
Emulator.getLogging().logPacketError(e);
}
}
public void appendBoolean(Boolean obj)
{
try
{
2019-03-18 02:22:00 +01:00
this.stream.writeBoolean(obj);
2018-07-06 15:30:00 +02:00
}
catch(IOException e)
{
Emulator.getLogging().logPacketError(e);
}
}
public void appendDouble(double d)
{
try
{
this.stream.writeDouble(d);
}
catch (IOException e)
{
Emulator.getLogging().logPacketError(e);
}
}
public void appendDouble(Double obj)
{
try
{
2019-03-18 02:22:00 +01:00
this.stream.writeDouble(obj);
2018-07-06 15:30:00 +02:00
}
catch (IOException e)
{
Emulator.getLogging().logPacketError(e);
}
}
public ServerMessage appendResponse(ServerMessage obj)
{
try
{
this.stream.write(obj.get().array());
}
catch(IOException e)
{
Emulator.getLogging().logPacketError(e);
}
return this;
}
public void append(ISerialize obj)
{
obj.serialize(this);
}
public String getBodyString()
{
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++) {
consoleText = consoleText.replace(Character.toString((char)i), "[" + i + "]");
}
buffer.discardSomeReadBytes();
return consoleText;
}
public int getHeader()
{
return this.header;
}
public ByteBuf get()
{
this.channelBuffer.setInt(0, this.channelBuffer.writerIndex() - 4);
return this.channelBuffer.copy();
}
}