Skip to content

Commit

Permalink
Adjust Test Command for Codeck and Replace PacketBufSeriaizer within …
Browse files Browse the repository at this point in the history
…OwoHandshake

Co-Authored-By: BasiqueEvangelist <[email protected]>
  • Loading branch information
Dragon-Seeker and BasiqueEvangelist committed Oct 23, 2023
1 parent 440e0be commit 3c8c9f6
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 51 deletions.
19 changes: 11 additions & 8 deletions src/main/java/io/wispforest/owo/network/OwoHandshake.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import io.wispforest.owo.Owo;
import io.wispforest.owo.mixin.ClientCommonNetworkHandlerAccessor;
import io.wispforest.owo.mixin.ServerCommonNetworkHandlerAccessor;
import io.wispforest.owo.network.serialization.PacketBufSerializer;
import io.wispforest.owo.ops.TextOps;
import io.wispforest.owo.particles.systems.ParticleSystemController;
import io.wispforest.owo.serialization.Codeck;
import io.wispforest.owo.serialization.impl.ReflectionCodeckBuilder;
import io.wispforest.owo.serialization.impl.bytebuf.ByteBufDeserializer;
import io.wispforest.owo.serialization.impl.bytebuf.ByteBufSerializer;
import io.wispforest.owo.util.OwoFreezer;
import io.wispforest.owo.util.ServicesFrozenException;
import net.fabricmc.api.EnvType;
Expand Down Expand Up @@ -36,8 +39,8 @@
public final class OwoHandshake {

@SuppressWarnings("unchecked")
private static final PacketBufSerializer<Map<Identifier, Integer>> RESPONSE_SERIALIZER =
(PacketBufSerializer<Map<Identifier, Integer>>) (Object) PacketBufSerializer.createMapSerializer(Map.class, Identifier.class, Integer.class);
private static final Codeck<Map<Identifier, Integer>> RESPONSE_SERIALIZER =
(Codeck<Map<Identifier, Integer>>) (Object) ReflectionCodeckBuilder.createMapSerializer(Map.class, Identifier.class, Integer.class);

private static final MutableText PREFIX = TextOps.concat(Owo.PREFIX, Text.of("§chandshake failure\n"));
public static final Identifier CHANNEL_ID = new Identifier("owo", "handshake");
Expand Down Expand Up @@ -118,7 +121,7 @@ private static void syncClient(MinecraftClient client, ClientConfigurationNetwor
QUERY_RECEIVED = true;

if (buf.readableBytes() > 0) {
final var serverOptionalChannels = RESPONSE_SERIALIZER.deserializer().apply(buf);
final var serverOptionalChannels = RESPONSE_SERIALIZER.decode(ByteBufDeserializer::new, buf);
((OwoClientConnectionExtension) ((ClientCommonNetworkHandlerAccessor) handler).getConnection()).owo$setChannelSet(filterOptionalServices(serverOptionalChannels, OwoNetChannel.REGISTERED_CHANNELS, OwoHandshake::hashChannel));
}

Expand All @@ -133,8 +136,8 @@ private static void syncClient(MinecraftClient client, ClientConfigurationNetwor
private static void syncServer(MinecraftServer server, ServerConfigurationNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) {
Owo.LOGGER.info("[Handshake] Receiving client channels");

final var clientChannels = RESPONSE_SERIALIZER.deserializer().apply(buf);
final var clientParticleControllers = RESPONSE_SERIALIZER.deserializer().apply(buf);
final var clientChannels = RESPONSE_SERIALIZER.decode(ByteBufDeserializer::new, buf);
final var clientParticleControllers = RESPONSE_SERIALIZER.decode(ByteBufDeserializer::new, buf);

StringBuilder disconnectMessage = new StringBuilder();

Expand All @@ -146,7 +149,7 @@ private static void syncServer(MinecraftServer server, ServerConfigurationNetwor
}

if (buf.readableBytes() > 0) {
final var clientOptionalChannels = RESPONSE_SERIALIZER.deserializer().apply(buf);
final var clientOptionalChannels = RESPONSE_SERIALIZER.decode(ByteBufDeserializer::new, buf);
((OwoClientConnectionExtension) ((ServerCommonNetworkHandlerAccessor) handler).owo$getConnection()).owo$setChannelSet(filterOptionalServices(clientOptionalChannels, OwoNetChannel.OPTIONAL_CHANNELS, OwoHandshake::hashChannel));
}

Expand Down Expand Up @@ -229,7 +232,7 @@ private static <T> void writeHashes(PacketByteBuf buffer, Map<Identifier, T> val
hashes.put(entry.getKey(), hashFunction.applyAsInt(entry.getValue()));
}

RESPONSE_SERIALIZER.serializer().accept(buffer, hashes);
RESPONSE_SERIALIZER.encode(() -> new ByteBufSerializer<>(buffer), hashes);
}

private static Pair<Set<Identifier>, Set<Identifier>> findCollisions(Set<Identifier> first, Set<Identifier> second) {
Expand Down
132 changes: 89 additions & 43 deletions src/testmod/java/io/wispforest/uwu/Uwu.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.gson.JsonElement;
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.logging.LogUtils;
import io.netty.buffer.ByteBuf;
import io.wispforest.owo.config.ConfigSynchronizer;
import io.wispforest.owo.config.Option;
Expand Down Expand Up @@ -66,6 +67,7 @@
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import org.slf4j.Logger;

import java.util.*;
import java.util.concurrent.TimeUnit;
Expand All @@ -76,6 +78,8 @@

public class Uwu implements ModInitializer {

private static final Logger LOGGER = LogUtils.getLogger();

public static final boolean WE_TESTEN_HANDSHAKE = false;

public static final TagKey<Item> TAB_2_CONTENT = TagKey.of(RegistryKeys.ITEM, new Identifier("uwu", "tab_2_content"));
Expand Down Expand Up @@ -249,29 +253,29 @@ public void onInitialize() {

String testPhrase = "This is a test to see how kodeck dose.";

source.sendMessage(Text.of("Input: " + testPhrase));
LOGGER.info("Input: " + testPhrase);

var nbtData = Codeck.STRING.encode(NbtSerializer::of, testPhrase);
var fromNbtData = Codeck.STRING.decode(NbtDeserializer::of, nbtData);

var jsonData = Codeck.STRING.encode(JsonSerializer::of, fromNbtData);
var fromJsonData = Codeck.STRING.decode(JsonDeserializer::of, jsonData);

source.sendMessage(Text.of("Output: " + fromJsonData));
LOGGER.info("Output: " + fromJsonData);

source.sendMessage(Text.empty());
LOGGER.info("");

//--

int randomNumber = rand.nextInt(20000);

source.sendMessage(Text.of("Input: " + randomNumber));
LOGGER.info("Input: " + randomNumber);

var jsonNum = Codeck.INT.encode(JsonSerializer::of, randomNumber);

source.sendMessage(Text.of("Output: " + Codeck.INT.decode(JsonDeserializer::of, jsonNum)));
LOGGER.info("Output: " + Codeck.INT.decode(JsonDeserializer::of, jsonNum));

source.sendMessage(Text.empty());
LOGGER.info("");

//--

Expand All @@ -283,60 +287,102 @@ public void onInitialize() {
randomNumbers.add(rand.nextInt(20000));
}

source.sendMessage(Text.of("Input: " + randomNumbers));
LOGGER.info("Input: " + randomNumbers);

Codeck<List<Integer>> INT_LIST_KODECK = Codeck.INT.list();

var nbtListData = INT_LIST_KODECK.encode(NbtSerializer::of, randomNumbers);

source.sendMessage(Text.of("Output: " + INT_LIST_KODECK.decode(NbtDeserializer::of, nbtListData)));
LOGGER.info("Output: " + INT_LIST_KODECK.decode(NbtDeserializer::of, nbtListData));

source.sendMessage(Text.empty());
LOGGER.info("");

//---

if (source.getPlayer() == null) return 0;

ItemStack handStack = source.getPlayer().getStackInHand(Hand.MAIN_HAND);

LOGGER.info(handStack.toString());
LOGGER.info(handStack.getOrCreateNbt().asString().replace("\n", "\\n"));

LOGGER.info("---");

JsonElement stackJsonData;

try {
stackJsonData = Codeck.ITEM_STACK.encode(JsonSerializer::of, handStack);
} catch (Exception exception){
LOGGER.info(exception.getMessage());
LOGGER.info((Arrays.toString(exception.getStackTrace())));

return 0;
}

LOGGER.info(stackJsonData.toString());

LOGGER.info("---");

try {
handStack = Codeck.ITEM_STACK.decode(JsonDeserializer::of, stackJsonData);
} catch (Exception exception){
LOGGER.info(exception.getMessage());
LOGGER.info((Arrays.toString(exception.getStackTrace())));

return 0;
}

LOGGER.info(handStack.toString());
LOGGER.info(handStack.getOrCreateNbt().asString().replace("\n", "\\n"));

LOGGER.info("");

//--

{
if (source.getPlayer() == null) return 0;
LOGGER.info("--- Format Based Codeck Test");

ItemStack stack = source.getPlayer().getStackInHand(Hand.MAIN_HAND);
var nbtDataStack = handStack.getOrCreateNbt();

source.sendMessage(Text.of(stack.toString()));
source.sendMessage(Text.of(String.valueOf(stack.getOrCreateNbt())));
LOGGER.info(" Input: " + nbtDataStack.asString().replace("\n", "\\n"));

source.sendMessage(Text.of("---"));
var jsonDataStack = Codeck.NBT_ELEMENT.encode(JsonSerializer::of, nbtDataStack);

JsonElement stackJsonData;
LOGGER.info(" Json: " + jsonDataStack);

try {
stackJsonData = Codeck.ITEM_STACK.encode(JsonSerializer::of, stack);
} catch (Exception exception){
source.sendMessage(Text.of(exception.getMessage()));
source.sendMessage(Text.of((Arrays.toString(exception.getStackTrace()))));
var convertedNbtDataStack = Codeck.NBT_ELEMENT.decode(JsonDeserializer::of, jsonDataStack);

return 0;
}
LOGGER.info("Output: " + convertedNbtDataStack.asString().replace("\n", "\\n"));

LOGGER.info("---");

LOGGER.info("");
}

source.sendMessage(Text.of(stackJsonData.toString()));
//--

source.sendMessage(Text.of("---"));
{
LOGGER.info("--- Transpose Format Based Codeck Test");

ItemStack stackFromJson;
var nbtDataStack = handStack.getOrCreateNbt();

try {
stackFromJson = Codeck.ITEM_STACK.decode(JsonDeserializer::of, stackJsonData);
} catch (Exception exception){
source.sendMessage(Text.of(exception.getMessage()));
source.sendMessage(Text.of((Arrays.toString(exception.getStackTrace()))));
LOGGER.info(" Input: " + nbtDataStack.asString().replace("\n", "\\n"));

return 0;
}
var jsonDataStack = Codeck.NBT_ELEMENT.encode(JsonSerializer::of, nbtDataStack);

LOGGER.info(" Json: " + jsonDataStack);

source.sendMessage(Text.of(stackFromJson.toString()));
source.sendMessage(Text.of(String.valueOf(stackFromJson.getOrCreateNbt())));
var convertedNbtDataStack = Codeck.JSON_ELEMENT.encode(NbtSerializer::of, jsonDataStack);

LOGGER.info("Output: " + convertedNbtDataStack.asString().replace("\n", "\\n"));

LOGGER.info("---");

LOGGER.info("");
}

// source.sendMessage(Text.empty());
//--

if (source.getPlayer() == null) return 0;

//Vanilla
iterations("Vanilla", (buf) -> {
Expand All @@ -355,8 +401,8 @@ public void onInitialize() {
var stackFromByte = Codeck.ITEM_STACK.decode(ByteBufDeserializer::new, buf);
});
} catch (Exception exception){
source.sendMessage(Text.of(exception.getMessage()));
source.sendMessage(Text.of((Arrays.toString(exception.getStackTrace()))));
LOGGER.info(exception.getMessage());
LOGGER.info(Arrays.toString(exception.getStackTrace()));

return 0;
}
Expand All @@ -373,14 +419,14 @@ public void onInitialize() {
UwuOptionalNetExample.init();
}

public static void iterations(String label, Consumer<PacketByteBuf> action){
private static void iterations(String label, Consumer<PacketByteBuf> action){
int maxTrials = 3;
int maxIterations = 50;

List<Long> durations = new ArrayList<>();

System.out.println("-----");
System.out.println(label);
LOGGER.info("-----");
LOGGER.info(label);

for (int trial = 0; trial < maxTrials; trial++) {
durations.clear();
Expand All @@ -395,10 +441,10 @@ public static void iterations(String label, Consumer<PacketByteBuf> action){
durations.add(System.nanoTime() - startTime);
}

System.out.println(String.format(maxIterations + " Trials took on average: %.2f", ((durations.stream().mapToLong(v -> v).sum()) / (double) durations.size()) / 1000000));
LOGGER.info(String.format(maxIterations + " Trials took on average: %.2f", ((durations.stream().mapToLong(v -> v).sum()) / (double) durations.size()) / 1000000));
}

System.out.println("-----");
LOGGER.info("-----");

}

Expand Down

0 comments on commit 3c8c9f6

Please sign in to comment.