Skip to content

Commit

Permalink
internal changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MrNavaStar committed Aug 24, 2024
1 parent ccada10 commit fd8e7df
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import me.mrnavastar.protoweaver.api.auth.ClientAuthHandler;
import me.mrnavastar.protoweaver.api.auth.ServerAuthHandler;
import me.mrnavastar.protoweaver.api.netty.ProtoConnection;
import me.mrnavastar.protoweaver.core.util.Furious;
import org.apache.fury.Fury;
import org.apache.fury.ThreadSafeFury;
import me.mrnavastar.protoweaver.core.util.ObjectSerializer;
import org.apache.fury.exception.InsecureException;

import java.lang.reflect.Modifier;
Expand All @@ -19,7 +17,7 @@
*/
public class Protocol {

private final ThreadSafeFury fury = Fury.builder().withJdkClassSerializableCheck(false).buildThreadSafeFury();
private final ObjectSerializer serializer = new ObjectSerializer();

@Getter private final String namespace;
@Getter private final String name;
Expand Down Expand Up @@ -87,11 +85,11 @@ public ClientAuthHandler newClientAuthHandler() {
}

public byte[] serialize(@NonNull Object packet) throws InsecureException {
return Furious.serialize(fury, packet);
return serializer.serialize(packet);
}

public Object deserialize(byte @NonNull [] packet) throws InsecureException {
return Furious.deserialize(fury, packet);
return serializer.deserialize(packet);
}

/**
Expand Down Expand Up @@ -183,7 +181,7 @@ public Builder setClientAuthHandler(Class<? extends ClientAuthHandler> handler)
* @param packet The packet to register.
*/
public Builder addPacket(@NonNull Class<?> packet) {
Furious.register(protocol.fury, packet);
protocol.serializer.register(packet);
protocol.packetHash = 31 * protocol.packetHash + packet.getName().hashCode();
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,51 @@
package me.mrnavastar.protoweaver.core.util;

import me.mrnavastar.r.R;
import org.apache.fury.BaseFury;
import org.apache.fury.Fury;
import org.apache.fury.ThreadSafeFury;
import org.apache.fury.exception.InsecureException;
import org.apache.fury.logging.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class Furious {
public class ObjectSerializer {

private final ThreadSafeFury fury = Fury.builder().withJdkClassSerializableCheck(false).buildThreadSafeFury();

static {
// Make fury be quiet
LoggerFactory.disableLogging();
}

private static void recursiveRegister(BaseFury fury, Class<?> type, List<Class<?>> registered) {
private void recursiveRegister(Class<?> type, List<Class<?>> registered) {
if (type == null || type == Object.class || registered.contains(type)) return;
fury.register(type);
registered.add(type);

List.of(type.getDeclaredFields()).forEach(field -> recursiveRegister(fury, field.getType(), registered));
List.of(R.of(type).generics()).forEach(t -> recursiveRegister(fury, t, registered));
if (!type.isEnum()) recursiveRegister(fury, type.getSuperclass(), registered);
List.of(type.getDeclaredFields()).forEach(field -> recursiveRegister(field.getType(), registered));
List.of(R.of(type).generics()).forEach(t -> recursiveRegister(t, registered));
if (!type.isEnum()) recursiveRegister(type.getSuperclass(), registered);
}

public static void register(BaseFury fury, Class<?> type) {
recursiveRegister(fury, type, new ArrayList<>());
public void register(Class<?> type) {
recursiveRegister(type, new ArrayList<>());
}

public static byte[] serialize(BaseFury fury, Object object) throws InsecureException {
public byte[] serialize(Object object) throws InsecureException {
try {
return fury.serialize(object);
} catch (InsecureException e) {
throw new InsecureException("unregistered packet: " + object.getClass().getName());
throw new InsecureException("unregistered object: " + object.getClass().getName());
}
}

public static Object deserialize(BaseFury fury, byte[] bytes) throws InsecureException {
public Object deserialize(byte[] bytes) throws InsecureException {
try {
return fury.deserialize(bytes);
} catch (InsecureException e) {
String packet = e.getMessage().split(" is not registered")[0].replace("class ", "");
throw new InsecureException("unregistered packet: " + packet);
throw new InsecureException("unregistered object: " + packet);
}
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ waterfall_version=1.21-R0.1-SNAPSHOT
# Dependencies
# https://github.com/apache/fury/releases
fury_version=0.7.0
r_version=1.0.5
r_version=1.0.6
# should always match the netty version in (latest) minecraft release
netty_version=4.1.97.Final
# https://mvnrepository.com/artifact/org.bouncycastle/bcpkix-jdk18on
Expand Down
2 changes: 1 addition & 1 deletion paper/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ libraries:
- io.netty:netty-codec-http:4.1.97.Final
- io.netty:netty-codec-http2:4.1.97.Final
- org.bouncycastle:bcpkix-jdk18on:1.78.1
- me.mrnavastar:r:1.0.5
- me.mrnavastar:r:1.0.6

0 comments on commit fd8e7df

Please sign in to comment.