This repository has been archived by the owner on May 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
WarpedMinecraftDecoder.java
executable file
·243 lines (221 loc) · 8.83 KB
/
WarpedMinecraftDecoder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package dev.wolveringer.bungeeutil.netty;
import static dev.wolveringer.bungeeutil.i18n.tr;
import java.util.List;
import dev.wolveringer.bungeeutil.BungeeUtil;
import dev.wolveringer.bungeeutil.Configuration;
import dev.wolveringer.bungeeutil.packetlib.PacketHandleEvent;
import dev.wolveringer.bungeeutil.packetlib.PacketLib;
import dev.wolveringer.bungeeutil.packetlib.PacketRegistry;
import dev.wolveringer.bungeeutil.packetlib.handler.MainPacketHandler;
import dev.wolveringer.bungeeutil.packetlib.reader.ByteBuffCreator;
import dev.wolveringer.bungeeutil.packets.Packet;
import dev.wolveringer.bungeeutil.player.ClientVersion;
import dev.wolveringer.bungeeutil.player.connection.IInitialHandler;
import dev.wolveringer.bungeeutil.statistics.profiler.Profiler;
import dev.wolveringer.bungeeutil.system.ProxyType;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import lombok.Getter;
import lombok.Setter;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.protocol.BadPacketException;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.MinecraftDecoder;
import net.md_5.bungee.protocol.PacketWrapper;
import net.md_5.bungee.protocol.Protocol;
import net.md_5.bungee.protocol.ProtocolConstants.Direction;
import net.md_5.bungee.protocol.packet.Title;
public class WarpedMinecraftDecoder extends MinecraftDecoder {
private final static String DECODING;
@SuppressWarnings("unused")
private final static String BUNGEE_WRITE;
private final static String PACKET_CREATION;
private final static String HANDLE_GENERAL;
private final static String HANDLE_INTERN;
private final static String HANDLE_EXTERN;
@SuppressWarnings("unused")
private final static String WRITE_BUFF;
static {
BungeeUtil.debug("Loading WarpedMinecraftDecoder timings translations");
DECODING = tr("network.timings.decoder.decoding");
BUNGEE_WRITE = tr("network.timings.decoder.create.bungeepacket");
PACKET_CREATION = tr("network.timings.decoder.create.packet");
HANDLE_GENERAL = tr("network.timings.decoder.handle");
HANDLE_INTERN = tr("network.timings.decoder.handle.intern");
HANDLE_EXTERN = tr("network.timings.decoder.handle.extern");
WRITE_BUFF = tr("network.timings.decoder.write.writeNewByteBuff");
}
@Getter
@Setter
private IInitialHandler initHandler;
private Protocol protocol;
private int version = -1;
private boolean server;
private ClientVersion clientVersion = ClientVersion.UnderknownVersion;
private Direction direction;
public WarpedMinecraftDecoder(Protocol protocol, boolean server, int protocolVersion, IInitialHandler i, Direction dir) {
super(protocol, server, protocolVersion);
this.protocol = protocol;
this.direction = dir;
this.initHandler = i;
this.server = server;
this.setProtocolVersion(protocolVersion);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if(this.initHandler == null){
System.out.println("Missing initial handler in WarpedMinecraftDecoder instance ("+super.toString()+"; initHandler="+this.initHandler+")");
super.decode(ctx, in, out);
return;
}
if(this.clientVersion == null){ //In the theorie impossible :)
if(initHandler.getHandshake() == null){
System.err.println("Invalid WarpedMinecraftDecoder parameters! Cant having an client without a handshake but with a version!");
initHandler.closeChannel();
return;
}
if(initHandler.getHandshake().getRequestedProtocol() == 2){
System.err.println("Could not find the ClientVersion for the ProtocolVersion "+this.version+". Disconnecting the client.");
this.initHandler.disconnect(ChatColor.RED+"Your client version isnt supported by BungeeUtils!");
} else super.decode(ctx, in, out); //Decoding stuff for outdated status requests
return;
}
Profiler.decoder_timings.start(DECODING);
try{
Packet packet;
try{
Profiler.decoder_timings.start(PACKET_CREATION);
packet = PacketRegistry.getPacket(this.clientVersion.getProtocollVersion(), this.getProtocol(), this.direction, in, this.initHandler.getPlayer());
Profiler.decoder_timings.stop(PACKET_CREATION);
if(packet != null){
packet.use();
Profiler.decoder_timings.start(HANDLE_GENERAL);
Profiler.decoder_timings.start(HANDLE_INTERN);
PacketHandleEvent<? extends Packet> e = new PacketHandleEvent(packet, this.initHandler.getPlayer());
if(!MainPacketHandler.handlePacket(e)){
Profiler.decoder_timings.stop(HANDLE_INTERN);
Profiler.decoder_timings.start(HANDLE_EXTERN);
PacketLib.handlePacket(e);
Profiler.decoder_timings.stop(HANDLE_EXTERN);
if(!e.isCancelled()){
packet = e.getPacket();
}else{
Profiler.decoder_timings.stop(HANDLE_GENERAL);
packet.unuse();
return;
}
}else{
Profiler.decoder_timings.stop(HANDLE_INTERN);
Profiler.decoder_timings.stop(HANDLE_GENERAL);
packet.unuse();
return;
}
}
}catch (Exception e){
if(e instanceof ClassNotFoundException){
e.printStackTrace();
System.err.println("Could not find class '"+e.getMessage()+"' in decode methode.");
if(e.getMessage().startsWith("dev.wolveringer.BungeeUtil") || e.getMessage().startsWith("dev.wolveringer.packet") || e.getMessage().startsWith("dev.wolveringer.network")){
System.err.println("");
System.err.println("");
System.err.println("-----------------------------------------------------------------------------------------------");
System.err.println(" Missing initial class! Shutting down BungeeUtils!");
if(ChannelInizializer.getChannelInitializer() instanceof BungeeUtilChannelInitializer){
BungeeUtilChannelInitializer channelInit = (BungeeUtilChannelInitializer) ChannelInizializer.getChannelInitializer();
channelInit.throwClassNotFoundError((ClassNotFoundException) e);
}
System.err.println("-----------------------------------------------------------------------------------------------");
}
}
BungeeUtil.debug("");
BungeeUtil.debug("Having an exception while decoding a packet!");
BungeeUtil.debug(e);
BungeeUtil.debug("Buffer contains:");
if(in.capacity() < 1024){
BungeeUtil.debug(ByteBufUtil.prettyHexDump(in, 0, in.capacity()));
} else {
BungeeUtil.debug("Buffer to large for a dump ("+in+")");
}
BungeeUtil.debug("");
in.skipBytes(in.readableBytes());
if(!this.initHandler.isConnected) {
return;
}
switch (Configuration.getHandleExceptionAction()) {
case DISCONNECT:
this.initHandler.disconnect(e);
case PRINT:
e.printStackTrace();
default:
break;
}
return;
}
Protocol.DirectionData prot = this.isServer() ? this.getProtocol().TO_SERVER : this.getProtocol().TO_CLIENT;
ByteBuf copy = packet == null ? in.copy() : packet.writeToByteBuff(ByteBuffCreator.createByteBuff(),this.clientVersion);
if(packet != null) packet.unuse();
try{
int packetId = DefinedPacket.readVarInt(in);
DefinedPacket bungeePacket = null;
switch (ProxyType.getType()) {
case BUNGEECORD:
bungeePacket = prot.createPacket(packetId, this.getProtocolVersion());
break;
case WATERFALL:
bungeePacket = prot.createPacket(packetId, this.getProtocolVersion(), false); //Dont support forge
break;
default:
break;
}
if(bungeePacket != null){
bungeePacket.read(in, prot.getDirection(), this.getProtocolVersion());
if(in.readableBytes() != 0){
Profiler.decoder_timings.stop(DECODING);
throw new BadPacketException("Did not read all bytes from packet " + bungeePacket.getClass() + " " + packetId + " Protocol " + this.getProtocolVersion() + " Direction " + prot+"! Left bytes: "+in.readableBytes());
}
}else{
in.skipBytes(in.readableBytes());
}
out.add(new PacketWrapper(bungeePacket, copy));
copy = null;
}finally{
if(copy != null){
copy.release();
}
copy = null;
}
packet = null;
}catch(Exception e){
BungeeUtil.debug(e, "An error occurent while decoding a packet");
}
Profiler.decoder_timings.stop(DECODING);
}
public IInitialHandler getHandler() {
return this.initHandler;
}
public Protocol getProtocol() {
return this.protocol;
}
public int getProtocolVersion() {
return this.version;
}
public boolean isServer() {
return this.server;
}
@Override
public void setProtocol(Protocol protocol) {
super.setProtocol(protocol);
this.protocol = protocol;
}
@Override
public void setProtocolVersion(int protocolVersion) {
super.setProtocolVersion(protocolVersion);
this.version = protocolVersion;
this.clientVersion = ClientVersion.fromProtocoll(protocolVersion);
if(this.clientVersion != null && !this.clientVersion.getProtocollVersion().isSupported()) {
this.clientVersion = null;
}
}
}