Skip to content

Commit

Permalink
depend on r for even better packet registration
Browse files Browse the repository at this point in the history
  • Loading branch information
MrNavaStar committed Aug 18, 2024
1 parent bcd5d45 commit 4f3b010
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 26 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ subprojects {
compileOnly "io.netty:netty-codec-http:${project.netty_version}"
compileOnly "io.netty:netty-codec-http2:${project.netty_version}"
compileOnly "org.bouncycastle:bcpkix-jdk18on:${project.bouncy_version}"
compileOnly "me.mrnavastar:r:${project.r_version}"

compileOnly "org.projectlombok:lombok:${project.lombok_version}"
annotationProcessor "org.projectlombok:lombok:${project.lombok_version}"
Expand Down
4 changes: 3 additions & 1 deletion client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependencies {
shadow compileOnly("io.netty:netty-buffer:${project.netty_version}")
shadow compileOnly("io.netty:netty-transport:${project.netty_version}")
shadow compileOnly("io.netty:netty-handler:${project.netty_version}")
shadow compileOnly("me.mrnavastar:r:${project.r_version}")
}

shadowJar {
Expand All @@ -18,9 +19,10 @@ shadowJar {

relocate "org.apache.fury", "me.mrnavastar.protoweaver.libs.org.apache.fury"
relocate 'io.netty', 'me.mrnavastar.protoweaver.libs.io.netty'
relocate 'me.mrnavastar.r', 'me.mrnavastar.protoweaver.libs.me.mrnavastar.r'

exclude "META-INF/maven/**"
//minimize()
minimize()
}

jar.finalizedBy(shadowJar)
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,16 @@
import me.mrnavastar.protoweaver.api.auth.ClientAuthHandler;
import me.mrnavastar.protoweaver.api.auth.ServerAuthHandler;
import me.mrnavastar.protoweaver.api.netty.ProtoConnection;
import org.apache.fury.Fury;
import org.apache.fury.ThreadSafeFury;
import org.apache.fury.logging.LoggerFactory;
import me.mrnavastar.protoweaver.core.util.Furious;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* Stores all the registered packets, settings and additional configuration of a {@link ProtoWeaver} protocol.
*/
public class Protocol {

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

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

@Getter private final String namespace;
@Getter private final String name;
@Getter private CompressionType compression = CompressionType.NONE;
Expand Down Expand Up @@ -93,11 +82,11 @@ public ClientAuthHandler newClientAuthHandler() {
}

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

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

/**
Expand Down Expand Up @@ -184,21 +173,12 @@ public Builder setClientAuthHandler(Class<? extends ClientAuthHandler> handler)
return this;
}

private void recursiveRegister(Class<?> packet, List<Class<?>> registered) {
if (packet == null || registered.contains(packet)) return;
protocol.fury.register(packet);
registered.add(packet);

List.of(packet.getDeclaredFields()).forEach(field -> recursiveRegister(field.getType(), registered));
recursiveRegister(packet.getSuperclass(), registered);
}

/**
* Register a class to the {@link Protocol}. Does nothing if the class has already been registered.
* @param packet The packet to register.
*/
public Builder addPacket(@NonNull Class<?> packet) {
recursiveRegister(packet, new ArrayList<>());
Furious.register(packet);
protocol.packetHash = 31 * protocol.packetHash + packet.getName().hashCode();
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package me.mrnavastar.protoweaver.core.util;

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

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

public class Furious {

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

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

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

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

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

public static byte[] serialize(Object object) {
return FURY.serialize(object);
}

public static Object deserialize(byte[] bytes) {
return FURY.deserialize(bytes);
}
}
1 change: 1 addition & 0 deletions fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {
include "org.bouncycastle:bcutil-jdk18on:${project.bouncy_version}"
include implementation("io.netty:netty-codec-http:${project.netty_version}")
include implementation("io.netty:netty-codec-http2:${project.netty_version}")
include "me.mrnavastar:r:${project.r_version}"

// Mod Compat
modCompileOnly "maven.modrinth:fabricproxy-lite:${project.proxy_lite_version}"
Expand Down
1 change: 1 addition & 0 deletions forge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies {
include forgeRuntimeLibrary("org.bouncycastle:bcutil-jdk18on:${project.bouncy_version}")
include forgeRuntimeLibrary(implementation("io.netty:netty-codec-http:${project.netty_version}"))
include forgeRuntimeLibrary(implementation("io.netty:netty-codec-http2:${project.netty_version}"))
include forgeRuntimeLibrary("me.mrnavastar:r:${project.r_version}")

// Mod Compat
modCompileOnly "maven.modrinth:proxy-compatible-forge:${project.proxy_forge_version}"
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +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.3
# 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
3 changes: 2 additions & 1 deletion paper/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ libraries:
- org.apache.fury:fury-core:0.7.0
- 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
- org.bouncycastle:bcpkix-jdk18on:1.78.1
- me.mrnavastar:r:1.0.3

0 comments on commit 4f3b010

Please sign in to comment.