Archiviert
13
0

Updating TinyProtocol significantly.

Added the ability to send and recieve packets, along with a more 
powerful reflection system. You can now retireve Minecraft classes 
by name.
Dieser Commit ist enthalten in:
Kristian S. Stangeland 2014-01-28 00:58:57 +01:00
Ursprung 5d7895d741
Commit fb07a91fb0
2 geänderte Dateien mit 293 neuen und 62 gelöschten Zeilen

Datei anzeigen

@ -1,28 +1,62 @@
package com.comphenix.tinyprotocol; package com.comphenix.tinyprotocol;
import net.minecraft.server.v1_7_R1.PacketPlayInChat; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import com.google.common.base.Function; import com.comphenix.tinyprotocol.TinyProtocol.FieldAccessor;
public class ExamplePlugin extends JavaPlugin { public class ExamplePlugin extends JavaPlugin {
private Function<Object, String> CHAT_MESSAGE = TinyProtocol.getFieldAccessor( // Chat packets
PacketPlayInChat.class, String.class, 0); private FieldAccessor<String> CHAT_MESSAGE = TinyProtocol.getField(
"PacketPlayInChat", String.class, 0);
// Explosion packet
private Class<?> particleClass = TinyProtocol.getMinecraftClass("PacketPlayOutWorldParticles");
private FieldAccessor<String> particleName = TinyProtocol.getField(particleClass, String.class, 0);
private FieldAccessor<Float> particleX = TinyProtocol.getField(particleClass, float.class, 0);
private FieldAccessor<Float> particleY = TinyProtocol.getField(particleClass, float.class, 1);
private FieldAccessor<Float> particleZ = TinyProtocol.getField(particleClass, float.class, 2);
private FieldAccessor<Integer> particleCount = TinyProtocol.getField(particleClass, int.class, 0);
private TinyProtocol protocol;
@Override @Override
public void onEnable() { public void onEnable() {
new TinyProtocol(this) { protocol = new TinyProtocol(this) {
@Override @Override
public Object onPacketInAsync(Player sender, Object packet) { public Object onPacketInAsync(Player sender, Object packet) {
// Cancel chat packets // Cancel chat packets
if (packet instanceof PacketPlayInChat) { if (CHAT_MESSAGE.hasField(packet)) {
if (CHAT_MESSAGE.apply(packet).contains("dirty")) if (CHAT_MESSAGE.get(packet).contains("dirty")) {
sendExplosion(sender);
return null; return null;
} }
}
if (particleName.hasField(packet)) {
System.out.println("Sending particle field:" + packet);
}
return super.onPacketInAsync(sender, packet); return super.onPacketInAsync(sender, packet);
} }
}; };
} }
private void sendExplosion(Player player) {
try {
// Only visible for the client
Object explosionPacket = particleClass.newInstance();
Location loc = player.getLocation();
particleName.set(explosionPacket, "hugeexplosion");
particleX.set(explosionPacket, (float) loc.getX());
particleY.set(explosionPacket, (float) loc.getY());
particleZ.set(explosionPacket, (float) loc.getZ());
particleCount.set(explosionPacket, 1);
// Send the packet to the player
protocol.sendPacket(player, explosionPacket);
} catch (Exception e) {
throw new RuntimeException("Cannot send packet.", e);
}
}
} }

Datei anzeigen

@ -1,27 +1,41 @@
package com.comphenix.tinyprotocol; package com.comphenix.tinyprotocol;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import javax.annotation.Nullable; import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;
import java.util.logging.Level;
import net.minecraft.server.v1_7_R1.NetworkManager; // These are not versioned, but they require CraftBukkit
import net.minecraft.util.io.netty.channel.Channel; import net.minecraft.util.io.netty.channel.Channel;
import net.minecraft.util.io.netty.channel.ChannelDuplexHandler; import net.minecraft.util.io.netty.channel.ChannelDuplexHandler;
import net.minecraft.util.io.netty.channel.ChannelHandlerContext; import net.minecraft.util.io.netty.channel.ChannelHandlerContext;
import net.minecraft.util.io.netty.channel.ChannelPromise; import net.minecraft.util.io.netty.channel.ChannelPromise;
import org.bukkit.craftbukkit.v1_7_R1.entity.CraftPlayer; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.server.PluginDisableEvent; import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import com.google.common.base.Function; import com.google.common.collect.MapMaker;
public abstract class TinyProtocol implements Listener { public abstract class TinyProtocol implements Listener {
private static Function<Object, Channel> CHANNEL_ACCESSOR = getFieldAccessor(NetworkManager.class, Channel.class, 0); // Deduce the net.minecraft.server.v* package
private static String OBC_PREFIX = Bukkit.getServer().getClass().getPackage().getName();
private static String NMS_PREFIX = OBC_PREFIX.replace("org.bukkit.craftbukkit", "net.minecraft.server");
// Used in order to lookup a channel
private MethodInvoker getPlayerHandle = getMethod(getCraftBukkitClass("entity.CraftPlayer"), "getHandle");
private FieldAccessor<Object> getConnection = getField(getMinecraftClass("EntityPlayer"), "playerConnection", Object.class);
private FieldAccessor<Object> getManager = getField(getMinecraftClass("PlayerConnection"), "networkManager", Object.class);
private FieldAccessor<Channel> getChannel = getField(getMinecraftClass("NetworkManager"), Channel.class, 0);
// Speedup channel lookup
private Map<Player, Channel> channelLookup = new MapMaker().weakKeys().makeMap();
private boolean closed; private boolean closed;
private Plugin plugin; private Plugin plugin;
@ -36,31 +50,32 @@ public abstract class TinyProtocol implements Listener {
} }
} }
@EventHandler
public final void onPlayerJoin(PlayerJoinEvent e) {
if (closed)
return;
injectPlayer(e.getPlayer());
}
private void injectPlayer(final Player player) { private void injectPlayer(final Player player) {
// Inject our packet interceptor // Inject our packet interceptor
getChannel(player).pipeline().addBefore("packet_handler", getHandlerName(), new ChannelDuplexHandler() { getChannel(player).pipeline().addBefore("packet_handler", getHandlerName(), new ChannelDuplexHandler() {
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
Object result = onPacketInAsync(player, msg); try {
msg = onPacketInAsync(player, msg);
} catch (Exception e) {
plugin.getLogger().log(Level.SEVERE, "Error in onPacketInAsync().", e);
}
if (result != null) { if (msg != null) {
super.channelRead(ctx, result); super.channelRead(ctx, msg);
} }
} }
@Override @Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
Object result = onPacketOutAsync(player, msg); try {
msg = onPacketOutAsync(player, msg);
} catch (Exception e) {
plugin.getLogger().log(Level.SEVERE, "Error in onPacketOutAsync().", e);
}
if (result != null) { if (msg != null) {
super.write(ctx, result, promise); super.write(ctx, msg, promise);
} }
} }
}); });
@ -70,6 +85,13 @@ public abstract class TinyProtocol implements Listener {
return "tiny-" + plugin.getName(); return "tiny-" + plugin.getName();
} }
@EventHandler(priority = EventPriority.LOWEST)
public final void onPlayerJoin(PlayerJoinEvent e) {
if (closed)
return;
injectPlayer(e.getPlayer());
}
@EventHandler @EventHandler
public final void onPluginDisable(PluginDisableEvent e) { public final void onPluginDisable(PluginDisableEvent e) {
if (e.getPlugin().equals(plugin)) { if (e.getPlugin().equals(plugin)) {
@ -77,9 +99,40 @@ public abstract class TinyProtocol implements Listener {
} }
} }
/**
* Send a packet to a particular player.
* @param player - the destination player.
* @param packet - the packet to send.
*/
public void sendPacket(Player player, Object packet) {
getChannel(player).pipeline().writeAndFlush(packet);
}
/**
* Pretend that a given packet has been received from a player.
* @param player - the player that sent the packet.
* @param packet - the packet that will be received by the server.
*/
public void receivePacket(Player player, Object packet) {
getChannel(player).pipeline().context("encoder").fireChannelRead(packet);
}
/**
* Retrieve the Netty channel associated with a player. This is cached.
* @param player - the player.
* @return The Netty channel.
*/
private Channel getChannel(Player player) { private Channel getChannel(Player player) {
NetworkManager manager = ((CraftPlayer) player.getPlayer()).getHandle().playerConnection.networkManager; Channel channel = channelLookup.get(player);
return CHANNEL_ACCESSOR.apply(manager);
// Lookup channel again
if (channel == null) {
Object connection = getConnection.get(getPlayerHandle.invoke(player));
Object manager = getManager.get(connection);
channelLookup.put(player, channel = getChannel.get(manager));
}
return channel;
} }
/** /**
@ -105,39 +158,8 @@ public abstract class TinyProtocol implements Listener {
} }
/** /**
* Retrieve a field accessor for a specific field type and index. * Cease listening for packets. This is called automatically when your plugin is disabled.
* @param target - the target type.
* @param fieldType - the field type.
* @param index - the index.
* @return The field accessor.
*/ */
public static <T> Function<Object, T> getFieldAccessor(Class<?> target, Class<T> fieldType, int index) {
for (Field field : target.getDeclaredFields()) {
if (fieldType.isAssignableFrom(field.getType()) && index-- <= 0) {
final Field targetField = field;
field.setAccessible(true);
// A function for retrieving a specific field value
return new Function<Object, T>() {
@SuppressWarnings("unchecked")
@Override
public T apply(@Nullable Object instance) {
try {
return (T) targetField.get(instance);
} catch (IllegalAccessException e) {
throw new RuntimeException("Cannot access reflection.", e);
}
}
};
}
}
// Search in parent classes
if (target.getSuperclass() != null)
return getFieldAccessor(target.getSuperclass(), fieldType, index);
throw new IllegalArgumentException("Cannot find field with type " + fieldType);
}
public final void close() { public final void close() {
if (!closed) { if (!closed) {
closed = true; closed = true;
@ -148,4 +170,179 @@ public abstract class TinyProtocol implements Listener {
} }
} }
} }
/**
* Retrieve a field accessor for a specific field type and name.
* @param target - the target type.
* @param name - the name of the field, or NULL to ignore.
* @param fieldType - a compatible field type.
* @return The field accessor.
*/
public static <T> FieldAccessor<T> getField(Class<?> target, String name, Class<T> fieldType) {
return getField(target, name, fieldType, 0);
}
/**
* Retrieve a field accessor for a specific field type and name.
* @param target - the target type.
* @param fieldType - a compatible field type.
* @param index - the number of compatible fields to skip.
* @return The field accessor.
*/
public static <T> FieldAccessor<T> getField(Class<?> target, Class<T> fieldType, int index) {
return getField(target, null, fieldType, index);
}
/**
* Retrieve a field accessor for a specific field type and name.
* @param nmsTargetClass - the net.minecraft.server class name.
* @param fieldType - a compatible field type.
* @param index - the number of compatible fields to skip.
* @return The field accessor.
*/
public static <T> FieldAccessor<T> getField(String nmsTargetClass, Class<T> fieldType, int index) {
return getField(getMinecraftClass(nmsTargetClass), fieldType, index);
}
// Common method
private static <T> FieldAccessor<T> getField(Class<?> target, String name, Class<T> fieldType, int index) {
for (final Field field : target.getDeclaredFields()) {
if ((name == null || field.getName().equals(name)) &&
fieldType.isAssignableFrom(field.getType()) && index-- <= 0) {
field.setAccessible(true);
// A function for retrieving a specific field value
return new FieldAccessor<T>() {
@SuppressWarnings("unchecked")
@Override
public T get(Object target) {
try {
return (T) field.get(target);
} catch (IllegalAccessException e) {
throw new RuntimeException("Cannot access reflection.", e);
}
}
@Override
public void set(Object target, Object value) {
try {
field.set(target, value);
} catch (IllegalAccessException e) {
throw new RuntimeException("Cannot access reflection.", e);
}
}
@Override
public boolean hasField(Object target) {
// target instanceof DeclaringClass
return field.getDeclaringClass().isAssignableFrom(target.getClass());
}
};
}
}
// Search in parent classes
if (target.getSuperclass() != null)
return getField(target.getSuperclass(), name, fieldType, index);
throw new IllegalArgumentException("Cannot find field with type " + fieldType);
}
/**
* Search for the first publically and privately defined method of the given name and parameter count.
* @param clazz - a class to start with.
* @param methodName - the method name, or NULL to skip.
* @param params - the expected parameters.
* @return An object that invokes this specific method.
* @throws IllegalStateException If we cannot find this method.
*/
public static MethodInvoker getMethod(Class<?> clazz, String methodName, Class<?>... params) {
for (final Method method : clazz.getDeclaredMethods()) {
if ((methodName == null || method.getName().equals(methodName)) &&
Arrays.equals(method.getParameterTypes(), params)) {
method.setAccessible(true);
return new MethodInvoker() {
@Override
public Object invoke(Object target, Object... arguments) {
try {
return method.invoke(target, arguments);
} catch (Exception e) {
throw new RuntimeException("Cannot invoke method " + method, e);
}
}
};
}
}
// Search in every superclass
if (clazz.getSuperclass() != null)
return getMethod(clazz.getSuperclass(), methodName, params);
throw new IllegalStateException(String.format(
"Unable to find method %s (%s).", methodName, Arrays.asList(params)));
}
/**
* Retrieve a class in the net.minecraft.server.VERSION.* package.
* @param name - the name of the class, excluding the package.
* @throws IllegalArgumentException If the class doesn't exist.
*/
public static Class<?> getMinecraftClass(String name) {
try {
return Class.forName(NMS_PREFIX + "." + name);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Cannot find nms." + name, e);
}
}
/**
* Retrieve a class in the org.bukkit.craftbukkit.VERSION.* package.
* @param name - the name of the class, excluding the package.
* @throws IllegalArgumentException If the class doesn't exist.
*/
public static Class<?> getCraftBukkitClass(String name) {
try {
return Class.forName(OBC_PREFIX + "." + name);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Cannot find obc." + name, e);
}
}
/**
* An interface for invoking a specific method.
*/
public interface MethodInvoker {
/**
* Invoke a method on a specific target object.
* @param target - the target object, or NULL for a static method.
* @param arguments - the arguments to pass to the method.
* @return The return value, or NULL if is void.
*/
public Object invoke(Object target, Object... arguments);
}
/**
* An interface for retrieving the field content.
* @param <T> - field type.
*/
public interface FieldAccessor<T> {
/**
* Retrieve the content of a field.
* @param target - the target object, or NULL for a static field.
* @return The value of the field.
*/
public T get(Object target);
/**
* Set the content of a field.
* @param target - the target object, or NULL for a static field.
* @param value - the new value of the field.
*/
public void set(Object target, Object value);
/**
* Determine if the given object has this field.
* @param target - the object to test.
* @return TRUE if it does, FALSE otherwise.
*/
public boolean hasField(Object target);
}
} }