Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.nio.charset.Charset;

public class ByteBufHelper {

public static int capacity(Object buffer) {
return PacketEvents.getAPI().getNettyManager().getByteBufOperator().capacity(buffer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.nio.charset.Charset;

public interface ByteBufOperator {

int capacity(Object buffer);
Object capacity(Object buffer, int capacity);
int readerIndex(Object buffer);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.retrooper.packetevents.util.meta;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class PacketWrapperMeta {
private final Map<String, Object> data = new HashMap<>();
private final long timestamp = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(10);

public void put(@NotNull String key, @NotNull Object value) {
this.data.put(key, value);
}

public boolean has(@NotNull String key) {
return this.data.containsKey(key);
}

public @Nullable Object get(@NotNull String key) {
return this.data.get(key);
}

public long getTimestamp() {
return this.timestamp;
}

public boolean isExpired() {
return getTimestamp() < System.currentTimeMillis();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.github.retrooper.packetevents.util.meta;

import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

public class PacketWrapperMetaCache {
private static final Map<IdentityKey, PacketWrapperMeta> META_CACHE = new ConcurrentHashMap<>();

public static void setMeta(@NotNull Object buffer, @NotNull String key, @NotNull Object value) {
META_CACHE.computeIfAbsent(new IdentityKey(buffer), unused -> new PacketWrapperMeta())
.put(key, value);
}

public static boolean hasMeta(@NotNull Object buffer, @NotNull String key) {
return META_CACHE.computeIfAbsent(new IdentityKey(buffer), unused -> new PacketWrapperMeta())
.has(key);
}

public static Object getMeta(@NotNull Object buffer, @NotNull String key) {
return META_CACHE.computeIfAbsent(new IdentityKey(buffer), unused -> new PacketWrapperMeta())
.get(key);
}

public static void clean() {
Set<IdentityKey> toRemoveKeys = new HashSet<>();
META_CACHE.forEach((identityKey, meta) -> {
if (meta.isExpired()) {
toRemoveKeys.add(identityKey);
}
});
toRemoveKeys.forEach(META_CACHE::remove);
}

static {
ThreadFactory threadFactory = new ThreadFactory() {
private int counter = 0;

@Override
public @NotNull Thread newThread(@NotNull Runnable runnable) {
Thread thread = new Thread(runnable);
thread.setName("packet-events - meta clean #" + counter++);
thread.setDaemon(false);
return thread;
}
};

ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(threadFactory);
executor.scheduleAtFixedRate(PacketWrapperMetaCache::clean, 0, 10, TimeUnit.SECONDS);
}

private static final class IdentityKey {
private final Object ref;

public IdentityKey(Object ref) {
this.ref = ref;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
IdentityKey that = (IdentityKey) o;
return ref == that.ref;
}

@Override
public int hashCode() {
return System.identityHashCode(ref);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,8 @@
import com.github.retrooper.packetevents.netty.buffer.ByteBufHelper;
import com.github.retrooper.packetevents.netty.channel.ChannelHelper;
import com.github.retrooper.packetevents.protocol.PacketSide;
import com.github.retrooper.packetevents.protocol.chat.ChatType;
import com.github.retrooper.packetevents.protocol.chat.ChatTypes;
import com.github.retrooper.packetevents.protocol.chat.LastSeenMessages;
import com.github.retrooper.packetevents.protocol.chat.MessageSignature;
import com.github.retrooper.packetevents.protocol.chat.Node;
import com.github.retrooper.packetevents.protocol.chat.Parsers;
import com.github.retrooper.packetevents.protocol.chat.*;
import com.github.retrooper.packetevents.protocol.chat.Parsers.Parser;
import com.github.retrooper.packetevents.protocol.chat.RemoteChatSession;
import com.github.retrooper.packetevents.protocol.chat.SignedCommandArgument;
import com.github.retrooper.packetevents.protocol.chat.filter.FilterMask;
import com.github.retrooper.packetevents.protocol.chat.filter.FilterMaskType;
import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
Expand Down Expand Up @@ -89,18 +82,15 @@
import com.github.retrooper.packetevents.protocol.world.Dimension;
import com.github.retrooper.packetevents.protocol.world.WorldBlockPosition;
import com.github.retrooper.packetevents.resources.ResourceLocation;
import com.github.retrooper.packetevents.util.Either;
import com.github.retrooper.packetevents.util.KnownPack;
import com.github.retrooper.packetevents.util.MathUtil;
import com.github.retrooper.packetevents.util.StringUtil;
import com.github.retrooper.packetevents.util.Vector3i;
import com.github.retrooper.packetevents.util.*;
import com.github.retrooper.packetevents.util.adventure.AdventureSerializer;
import com.github.retrooper.packetevents.util.crypto.MinecraftEncryptionUtil;
import com.github.retrooper.packetevents.util.crypto.SaltSignature;
import com.github.retrooper.packetevents.util.crypto.SignatureData;
import com.github.retrooper.packetevents.util.mappings.GlobalRegistryHolder;
import com.github.retrooper.packetevents.util.mappings.IRegistry;
import com.github.retrooper.packetevents.util.mappings.IRegistryHolder;
import com.github.retrooper.packetevents.util.meta.PacketWrapperMetaCache;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.Style;
import org.jetbrains.annotations.ApiStatus;
Expand All @@ -112,31 +102,26 @@
import java.nio.charset.StandardCharsets;
import java.security.PublicKey;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.IntFunction;

public class PacketWrapper<T extends PacketWrapper<T>> {

@Nullable
public Object buffer;

@ApiStatus.Internal
public final Object bufferLock = new Object();

@Nullable
private Map<String, Object> metadata;

protected ClientVersion clientVersion;
protected ServerVersion serverVersion;
private PacketTypeData packetTypeData;
private final PacketTypeData packetTypeData;
// For sending chunk data packets, which need this data
@Nullable
protected User user;
Expand Down Expand Up @@ -236,6 +221,9 @@ public final void prepareForSend(Object channel, boolean outgoing, boolean proxy
buffer = ChannelHelper.pooledByteBuf(channel);
}

// Wrapped meta system
writeMeta();

//On proxies, we must rewrite the packet ID in a format compatible for the targeted client version
if (proxy) {
User user = PacketEvents.getAPI().getProtocolManager().getUser(channel);
Expand All @@ -259,6 +247,36 @@ public final void prepareForSend(Object channel, boolean outgoing) {
prepareForSend(channel, outgoing, PacketEvents.getAPI().getInjector().isProxy());
}

public void setMeta(@NotNull String key, @NotNull Object value) {
if (this.metadata == null) {
this.metadata = new HashMap<>();
}

this.metadata.put(key, value);
}

public Object getMeta(@NotNull String key) {
if (this.buffer != null) {
return PacketWrapperMetaCache.getMeta(this.buffer, key);
}

return null;
}

public boolean hasMeta(@NotNull String key) {
if (this.buffer != null) {
return PacketWrapperMetaCache.hasMeta(this.buffer, key);
}

return false;
}

public void writeMeta() {
if (this.buffer != null && this.metadata != null) {
this.metadata.forEach((key, value) -> PacketWrapperMetaCache.setMeta(this.buffer, key, value));
}
}

public void read() {
}

Expand All @@ -281,6 +299,7 @@ public final void readEvent(ProtocolPacketEvent event) {
} else {
read();
}

event.setLastUsedWrapper(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
if (!msg.isReadable()) {
return;
}

PacketEventsImplHelper.handlePacket(ctx.channel(), this.user, this.player,
msg, false, this.side);

if (msg.isReadable()) {
out.add(msg.retain());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.nio.charset.Charset;

public class ByteBufOperatorImpl implements ByteBufOperator {

@Override
public int capacity(Object buffer) {
return ((ByteBuf)buffer).capacity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@
import com.github.retrooper.packetevents.netty.buffer.ByteBufHelper;
import com.github.retrooper.packetevents.protocol.ConnectionState;
import com.github.retrooper.packetevents.protocol.player.User;
import com.github.retrooper.packetevents.util.EventCreationUtil;
import com.github.retrooper.packetevents.util.ExceptionUtil;
import com.github.retrooper.packetevents.util.PacketEventsImplHelper;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerDisconnect;
import io.github.retrooper.packetevents.injector.connection.ServerConnectionInitializer;
import io.github.retrooper.packetevents.util.SpigotReflectionUtil;
Expand Down
Loading