Skip to content

Commit

Permalink
Merge pull request #79 from MrNavaStar/fury
Browse files Browse the repository at this point in the history
Fury
  • Loading branch information
MrNavaStar authored Aug 17, 2024
2 parents 0b8bac7 + 0ebd967 commit 22d4a71
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 58 deletions.
7 changes: 3 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ allprojects {

repositories {
mavenCentral()
maven { url "https://maven.minecraftforge.net" } // Forge
maven { url "https://maven.minecraftforge.net/" } // Forge
maven { url "https://repo.papermc.io/repository/maven-public/" } // Paper + Velocity
maven { url "https://api.modrinth.com/maven" } // Modrinth
maven { url "https://api.modrinth.com/maven/" } // Modrinth
}

tasks.withType(JavaCompile) {
Expand All @@ -35,8 +35,7 @@ subprojects {
apply plugin: "io.freefair.lombok"

dependencies {
compileOnly "com.esotericsoftware:kryo5:${project.kyro_version}"
compileOnly "de.javakaffee:kryo-serializers:${project.kyro_ser_version}"
compileOnly "org.apache.fury:fury-core:${project.fury_version}"
compileOnly "io.netty:netty-buffer:${project.netty_version}"
compileOnly "io.netty:netty-transport:${project.netty_version}"
compileOnly "io.netty:netty-handler:${project.netty_version}"
Expand Down
4 changes: 2 additions & 2 deletions client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: "io.github.goooler.shadow"

dependencies {
shadow compileOnly(project(path: ':common', configuration: 'default'))
shadow compileOnly("com.esotericsoftware:kryo5:${project.kyro_version}")
shadow compileOnly("org.apache.fury:fury-core:${project.fury_version}")
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}")
Expand All @@ -16,7 +16,7 @@ shadowJar {
mergeServiceFiles()
}

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

exclude "META-INF/maven/**"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
package me.mrnavastar.protoweaver.api.protocol;

import com.esotericsoftware.kryo.kryo5.Kryo;
import com.esotericsoftware.kryo.kryo5.io.Input;
import com.esotericsoftware.kryo.kryo5.io.Output;
import com.esotericsoftware.kryo.kryo5.objenesis.strategy.StdInstantiatorStrategy;
import com.esotericsoftware.kryo.kryo5.serializers.DefaultSerializers;
import com.esotericsoftware.kryo.kryo5.util.DefaultInstantiatorStrategy;
import lombok.*;
import me.mrnavastar.protoweaver.api.ProtoConnectionHandler;
import me.mrnavastar.protoweaver.api.ProtoWeaver;
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 java.lang.reflect.Modifier;
import java.util.Objects;
import java.util.UUID;

/**
* 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();

@Getter private final String namespace;
@Getter private final String name;
private final Kryo kryo = new Kryo();
@Getter private CompressionType compression = CompressionType.NONE;
@Getter private int compressionLevel = -37;
@Getter private int maxPacketSize = 16384;
Expand All @@ -39,9 +35,6 @@ public class Protocol {
private Protocol(String namespace, String name) {
this.namespace = namespace;
this.name = name;
kryo.setRegistrationRequired(false);
kryo.setInstantiatorStrategy(new DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
kryo.addDefaultSerializer(UUID.class, new DefaultSerializers.UUIDSerializer());
}

/**
Expand Down Expand Up @@ -92,22 +85,11 @@ public ClientAuthHandler newClientAuthHandler() {
}

public byte[] serialize(@NonNull Object packet) {
try (Output output = new Output(maxPacketSize)) {
try {
kryo.writeClassAndObject(output, packet);
} catch (IllegalArgumentException ignore) {}
return output.toBytes();
}
return fury.serialize(packet);
}

public Object deserialize(byte @NonNull [] packet) throws IllegalArgumentException {
try (Input in = new Input(packet)) {
Object obj = kryo.readClassAndObject(in);
if (obj == null || kryo.getClassResolver().getRegistration(obj.getClass()) == null) {
throw new IllegalArgumentException("Protocol:" + this + " received an unknown packet: " + obj);
}
return obj;
}
public Object deserialize(byte @NonNull [] packet) {
return fury.deserialize(packet);
}

/**
Expand Down Expand Up @@ -199,13 +181,8 @@ public Builder setClientAuthHandler(Class<? extends ClientAuthHandler> handler)
* @param packet The packet to register.
*/
public Builder addPacket(@NonNull Class<?> packet) {
if (protocol.kryo.getClassResolver().getRegistration(packet) != null) return this;

protocol.kryo.register(packet);
protocol.fury.register(packet);
protocol.packetHash = 31 * protocol.packetHash + packet.getName().hashCode();

/*for (Field field : packet.getDeclaredFields())
if (((Modifier.STATIC | Modifier.TRANSIENT) & field.getModifiers()) == 0) addPacket(field.getType());*/
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,17 @@ public void channelInactive(ChannelHandlerContext ctx) {

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> list) {
try {
if (byteBuf.readableBytes() == 0) return;
if (byteBuf.readableBytes() == 0) return;

byte[] bytes = new byte[byteBuf.readInt()];
byteBuf.readBytes(bytes);
Object packet = connection.getProtocol().deserialize(bytes);
byte[] bytes = new byte[byteBuf.readInt()];
byteBuf.readBytes(bytes);
Object packet = connection.getProtocol().deserialize(bytes);

try {
handler.handlePacket(connection, packet);
} catch (Exception e) {
ProtoLogger.error("Protocol: " + connection.getProtocol() + " threw an error on when trying to handle: " + packet.getClass() + "!");
e.printStackTrace();
}
} catch (IllegalArgumentException e) {
ProtoLogger.warn(e.getMessage());
try {
handler.handlePacket(connection, packet);
} catch (Exception e) {
ProtoLogger.error("Protocol: " + connection.getProtocol() + " threw an error on when trying to handle: " + packet.getClass() + "!");
e.printStackTrace();
}
}

Expand Down
2 changes: 1 addition & 1 deletion fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies {
modCompileOnly "net.fabricmc:fabric-loader:${project.loader_version}"

// Libs
include implementation("com.esotericsoftware:kryo5:${project.kyro_version}")
include implementation("org.apache.fury:fury-core:${project.fury_version}")
include implementation("org.bouncycastle:bcpkix-jdk18on:${project.bouncy_version}")
include "org.bouncycastle:bcprov-jdk18on:${project.bouncy_version}"
include "org.bouncycastle:bcutil-jdk18on:${project.bouncy_version}"
Expand Down
6 changes: 3 additions & 3 deletions forge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ dependencies {
forge "net.minecraftforge:forge:${project.forge_version}"

// Libs
include forgeRuntimeLibrary(implementation("com.esotericsoftware:kryo5:${project.kyro_version}"))
include forgeRuntimeLibrary(implementation("org.apache.fury:fury-core:${project.fury_version}"))
include forgeRuntimeLibrary(implementation("org.bouncycastle:bcpkix-jdk18on:${project.bouncy_version}"))
include "org.bouncycastle:bcprov-jdk18on:${project.bouncy_version}"
include "org.bouncycastle:bcutil-jdk18on:${project.bouncy_version}"
include forgeRuntimeLibrary("org.bouncycastle:bcprov-jdk18on:${project.bouncy_version}")
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}"))

Expand Down
5 changes: 2 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ velocity_version=3.3.0-SNAPSHOT
waterfall_version=1.21-R0.1-SNAPSHOT

# Dependencies
# https://github.com/EsotericSoftware/kryo/releases
kyro_version=5.6.0
kyro_ser_version=0.45
# https://github.com/apache/fury/releases
fury_version=0.7.0
# 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 @@ -3,7 +3,7 @@ version: 'debug-build'
main: me.mrnavastar.protoweaver.loader.Paper
api-version: '1.13'
libraries:
- com.esotericsoftware:kryo5:5.6.0
- 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

0 comments on commit 22d4a71

Please sign in to comment.