Skip to content

Commit

Permalink
write fieldname to buffer to prevent wrong data encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiies committed Mar 28, 2024
1 parent 737fefa commit b295e14
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ allprojects {
apply(plugin = "maven-publish")

group = "dev.httpmarco"
version = "1.0.35-SNAPSHOT"
version = "1.0.37-SNAPSHOT"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import dev.httpmarco.osgan.networking.annotation.PacketIncludeObject;
import dev.httpmarco.osgan.reflections.Reflections;
import io.netty5.channel.ChannelHandlerContext;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

Expand All @@ -27,14 +29,15 @@ public void encode(ChannelHandlerContext ctx, Packet msg, @NotNull CodecBuffer b
private void encodeObject(@NotNull CodecBuffer buffer, @NotNull Object packet) throws Exception {
buffer.writeString(packet.getClass().getName());

for (var field : packet.getClass().getDeclaredFields()) {
for (Field field : packet.getClass().getDeclaredFields()) {
if (field.isAnnotationPresent(PacketIgnore.class)) {
continue;
}

field.setAccessible(true);

var nullableObject = field.get(packet);
buffer.writeString(field.getName());
buffer.writeBoolean(nullableObject == null);

if (nullableObject == null) {
Expand Down Expand Up @@ -122,11 +125,19 @@ public void decode(@NotNull ChannelHandlerContext ctx, @NotNull CodecBuffer buff
}
}

private Object decodeObject(@NotNull CodecBuffer buffer) throws ClassNotFoundException, IllegalAccessException {
private Object decodeObject(@NotNull CodecBuffer buffer) throws ClassNotFoundException, IllegalAccessException, NoSuchFieldException {
var clazz = Class.forName(buffer.readString());
var packet = new Reflections<>(clazz).allocate();

for (var field : clazz.getDeclaredFields()) {
//TODO remove
System.out.println("Decoding class: " + clazz);

for (int i = 0; i < clazz.getDeclaredFields().length; i++) {
var fieldName = buffer.readString();
var field = clazz.getDeclaredField(fieldName);

System.out.println("Decoding field: " + fieldName);

if (field.isAnnotationPresent(PacketIgnore.class)) {
continue;
}
Expand All @@ -148,13 +159,13 @@ private Object decodeObject(@NotNull CodecBuffer buffer) throws ClassNotFoundExc
} else if (type.isArray()) {
var array = (Object[]) Array.newInstance(type.getComponentType(), buffer.readInt());

for (int i = 0; i < array.length; i++) {
for (int y = 0; y < array.length; y++) {
var object = decodeParameter(buffer, array.getClass().getComponentType());
if (object != null) {
array[i] = object;
array[y] = object;
continue;
}
array[i] = decodeObject(buffer);
array[y] = decodeObject(buffer);
}
field.set(packet, array);
} else if (Collection.class.isAssignableFrom(type)) {
Expand All @@ -179,7 +190,7 @@ private Object decodeObject(@NotNull CodecBuffer buffer) throws ClassNotFoundExc

int size = buffer.readInt();

for (var i = 0; i < size; i++) {
for (var y = 0; y < size; y++) {
var object = decodeParameter(buffer, collectionElementType);
if (object != null) {
collection.add(object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@
@Accessors(fluent = true)
@AllArgsConstructor
public class ChannelTransmitAuthPacket implements Packet {

private String id;

}

0 comments on commit b295e14

Please sign in to comment.