|
1 |
| -/** |
2 |
| - * ProtocolLib - Bukkit server library that allows access to the Minecraft protocol. |
3 |
| - * Copyright (C) 2015 dmulloy2 |
4 |
| - * |
5 |
| - * This program is free software; you can redistribute it and/or modify it under the terms of the |
6 |
| - * GNU General Public License as published by the Free Software Foundation; either version 2 of |
7 |
| - * the License, or (at your option) any later version. |
8 |
| - * |
9 |
| - * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
10 |
| - * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
11 |
| - * See the GNU General Public License for more details. |
12 |
| - * |
13 |
| - * You should have received a copy of the GNU General Public License along with this program; |
14 |
| - * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
15 |
| - * 02111-1307 USA |
16 |
| - */ |
17 |
| -package com.comphenix.protocol.injector.netty; |
18 |
| - |
19 |
| -import java.util.Map; |
20 |
| -import java.util.Map.Entry; |
21 |
| - |
22 |
| -import com.comphenix.protocol.PacketType; |
23 |
| -import com.comphenix.protocol.PacketType.Protocol; |
24 |
| -import com.comphenix.protocol.PacketType.Sender; |
25 |
| -import com.comphenix.protocol.injector.netty.ProtocolRegistry; |
26 |
| -import com.comphenix.protocol.injector.packet.MapContainer; |
27 |
| -import com.comphenix.protocol.reflect.StructureModifier; |
28 |
| -import com.google.common.collect.Maps; |
29 |
| - |
30 |
| -/** |
31 |
| - * @author dmulloy2 |
32 |
| - */ |
33 |
| - |
34 |
| -public class NettyProtocolRegistry extends ProtocolRegistry { |
35 |
| - |
36 |
| - public NettyProtocolRegistry() { |
37 |
| - super(); |
38 |
| - } |
39 |
| - |
40 |
| - @Override |
41 |
| - protected synchronized void initialize() { |
42 |
| - Object[] protocols = enumProtocol.getEnumConstants(); |
43 |
| - |
44 |
| - // ID to Packet class maps |
45 |
| - Map<Object, Map<Integer, Class<?>>> serverMaps = Maps.newLinkedHashMap(); |
46 |
| - Map<Object, Map<Integer, Class<?>>> clientMaps = Maps.newLinkedHashMap(); |
47 |
| - |
48 |
| - Register result = new Register(); |
49 |
| - StructureModifier<Object> modifier = null; |
50 |
| - |
51 |
| - // Iterate through the protocols |
52 |
| - for (Object protocol : protocols) { |
53 |
| - if (modifier == null) |
54 |
| - modifier = new StructureModifier<Object>(protocol.getClass().getSuperclass(), false); |
55 |
| - StructureModifier<Map<Object, Map<Integer, Class<?>>>> maps = modifier.withTarget(protocol).withType(Map.class); |
56 |
| - for (Entry<Object, Map<Integer, Class<?>>> entry : maps.read(0).entrySet()) { |
57 |
| - String direction = entry.getKey().toString(); |
58 |
| - if (direction.contains("CLIENTBOUND")) { // Sent by Server |
59 |
| - serverMaps.put(protocol, entry.getValue()); |
60 |
| - } else if (direction.contains("SERVERBOUND")) { // Sent by Client |
61 |
| - clientMaps.put(protocol, entry.getValue()); |
62 |
| - } |
63 |
| - } |
64 |
| - } |
65 |
| - |
66 |
| - // Maps we have to occasionally check have changed |
67 |
| - for (Map<Integer, Class<?>> map : serverMaps.values()) { |
68 |
| - result.containers.add(new MapContainer(map)); |
69 |
| - } |
70 |
| - |
71 |
| - for (Map<Integer, Class<?>> map : clientMaps.values()) { |
72 |
| - result.containers.add(new MapContainer(map)); |
73 |
| - } |
74 |
| - |
75 |
| - for (int i = 0; i < protocols.length; i++) { |
76 |
| - Object protocol = protocols[i]; |
77 |
| - Enum<?> enumProtocol = (Enum<?>) protocol; |
78 |
| - Protocol equivalent = Protocol.fromVanilla(enumProtocol); |
79 |
| - |
80 |
| - // Associate known types |
81 |
| - if (serverMaps.containsKey(protocol)) |
82 |
| - associatePackets(result, serverMaps.get(protocol), equivalent, Sender.SERVER); |
83 |
| - if (clientMaps.containsKey(protocol)) |
84 |
| - associatePackets(result, clientMaps.get(protocol), equivalent, Sender.CLIENT); |
85 |
| - } |
86 |
| - |
87 |
| - // Exchange (thread safe, as we have only one writer) |
88 |
| - this.register = result; |
89 |
| - } |
90 |
| - |
91 |
| - @Override |
92 |
| - protected void associatePackets(Register register, Map<Integer, Class<?>> lookup, Protocol protocol, Sender sender) { |
93 |
| - for (Entry<Integer, Class<?>> entry : lookup.entrySet()) { |
94 |
| - PacketType type = PacketType.fromCurrent(protocol, sender, entry.getKey(), entry.getValue()); |
95 |
| - |
96 |
| - try { |
97 |
| - register.typeToClass.put(type, entry.getValue()); |
98 |
| - |
99 |
| - if (sender == Sender.SERVER) |
100 |
| - register.serverPackets.add(type); |
101 |
| - if (sender == Sender.CLIENT) |
102 |
| - register.clientPackets.add(type); |
103 |
| - } catch (IllegalArgumentException ex) { |
104 |
| - // Sometimes this happens with fake packets, just ignore it |
105 |
| - } |
106 |
| - } |
107 |
| - } |
108 |
| -} |
| 1 | +/** |
| 2 | + * ProtocolLib - Bukkit server library that allows access to the Minecraft protocol. |
| 3 | + * Copyright (C) 2015 dmulloy2 |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify it under the terms of the |
| 6 | + * GNU General Public License as published by the Free Software Foundation; either version 2 of |
| 7 | + * the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 10 | + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | + * See the GNU General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License along with this program; |
| 14 | + * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 15 | + * 02111-1307 USA |
| 16 | + */ |
| 17 | +package com.comphenix.protocol.injector.netty; |
| 18 | + |
| 19 | +import java.util.Map; |
| 20 | +import java.util.Map.Entry; |
| 21 | + |
| 22 | +import com.comphenix.protocol.PacketType; |
| 23 | +import com.comphenix.protocol.ProtocolLogger; |
| 24 | +import com.comphenix.protocol.PacketType.Protocol; |
| 25 | +import com.comphenix.protocol.PacketType.Sender; |
| 26 | +import com.comphenix.protocol.injector.netty.ProtocolRegistry; |
| 27 | +import com.comphenix.protocol.injector.packet.MapContainer; |
| 28 | +import com.comphenix.protocol.reflect.StructureModifier; |
| 29 | +import com.google.common.collect.Maps; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author dmulloy2 |
| 33 | + */ |
| 34 | + |
| 35 | +public class NettyProtocolRegistry extends ProtocolRegistry { |
| 36 | + |
| 37 | + public NettyProtocolRegistry() { |
| 38 | + super(); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + protected synchronized void initialize() { |
| 43 | + ProtocolLogger.debug("NettyProtocolRegistry#initialize()"); // Debug for issue #202 |
| 44 | + |
| 45 | + Object[] protocols = enumProtocol.getEnumConstants(); |
| 46 | + |
| 47 | + // ID to Packet class maps |
| 48 | + Map<Object, Map<Integer, Class<?>>> serverMaps = Maps.newLinkedHashMap(); |
| 49 | + Map<Object, Map<Integer, Class<?>>> clientMaps = Maps.newLinkedHashMap(); |
| 50 | + |
| 51 | + Register result = new Register(); |
| 52 | + StructureModifier<Object> modifier = null; |
| 53 | + |
| 54 | + // Iterate through the protocols |
| 55 | + for (Object protocol : protocols) { |
| 56 | + if (modifier == null) |
| 57 | + modifier = new StructureModifier<Object>(protocol.getClass().getSuperclass(), false); |
| 58 | + StructureModifier<Map<Object, Map<Integer, Class<?>>>> maps = modifier.withTarget(protocol).withType(Map.class); |
| 59 | + for (Entry<Object, Map<Integer, Class<?>>> entry : maps.read(0).entrySet()) { |
| 60 | + String direction = entry.getKey().toString(); |
| 61 | + if (direction.contains("CLIENTBOUND")) { // Sent by Server |
| 62 | + serverMaps.put(protocol, entry.getValue()); |
| 63 | + } else if (direction.contains("SERVERBOUND")) { // Sent by Client |
| 64 | + clientMaps.put(protocol, entry.getValue()); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Maps we have to occasionally check have changed |
| 70 | + for (Map<Integer, Class<?>> map : serverMaps.values()) { |
| 71 | + result.containers.add(new MapContainer(map)); |
| 72 | + } |
| 73 | + |
| 74 | + for (Map<Integer, Class<?>> map : clientMaps.values()) { |
| 75 | + result.containers.add(new MapContainer(map)); |
| 76 | + } |
| 77 | + |
| 78 | + for (int i = 0; i < protocols.length; i++) { |
| 79 | + Object protocol = protocols[i]; |
| 80 | + Enum<?> enumProtocol = (Enum<?>) protocol; |
| 81 | + Protocol equivalent = Protocol.fromVanilla(enumProtocol); |
| 82 | + |
| 83 | + // Associate known types |
| 84 | + if (serverMaps.containsKey(protocol)) |
| 85 | + associatePackets(result, serverMaps.get(protocol), equivalent, Sender.SERVER); |
| 86 | + if (clientMaps.containsKey(protocol)) |
| 87 | + associatePackets(result, clientMaps.get(protocol), equivalent, Sender.CLIENT); |
| 88 | + } |
| 89 | + |
| 90 | + // Exchange (thread safe, as we have only one writer) |
| 91 | + this.register = result; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + protected void associatePackets(Register register, Map<Integer, Class<?>> lookup, Protocol protocol, Sender sender) { |
| 96 | + for (Entry<Integer, Class<?>> entry : lookup.entrySet()) { |
| 97 | + PacketType type = PacketType.fromCurrent(protocol, sender, entry.getKey(), entry.getValue()); |
| 98 | + |
| 99 | + try { |
| 100 | + register.typeToClass.put(type, entry.getValue()); |
| 101 | + |
| 102 | + if (sender == Sender.SERVER) |
| 103 | + register.serverPackets.add(type); |
| 104 | + if (sender == Sender.CLIENT) |
| 105 | + register.clientPackets.add(type); |
| 106 | + } catch (IllegalArgumentException ex) { |
| 107 | + // Sometimes this happens with fake packets, just ignore it |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments