Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.20.2] Endec - Simplier Codec Alternative #194

Merged
merged 50 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
f6d935b
Initial Codeck implementation
Dragon-Seeker Oct 10, 2023
7ac9a08
Method refactors and fix errors
Dragon-Seeker Oct 11, 2023
dd47bef
Implement More Targeted approach for doing different methods of steri…
Dragon-Seeker Oct 21, 2023
e13e5c3
Implement Codeck within Owo Networking
Dragon-Seeker Oct 22, 2023
e090877
Adjust Attributes use case within De-Serilizers
Dragon-Seeker Oct 22, 2023
cb59dbc
Remove SelfDescribedSerilalizer and handle Format Specific Codecks di…
Dragon-Seeker Oct 22, 2023
8559f26
Fix incorrect constructor calls
Dragon-Seeker Oct 22, 2023
440e0be
Preallocate list and map with the given size before adding entries
Dragon-Seeker Oct 23, 2023
f6c017d
Adjust Test Command for Codeck and Replace PacketBufSeriaizer within …
Dragon-Seeker Oct 23, 2023
2a9e5bd
Resolve issue with inablity to have StructField be called with requir…
Dragon-Seeker Oct 23, 2023
7336ee7
Rename unsafe to safeHandling
Dragon-Seeker Oct 23, 2023
08cde20
Change name to Endec as the possible final name
Dragon-Seeker Oct 24, 2023
cfdfc6b
Fix Coopt Class name and add easy way of converting to DFU Codec
Dragon-Seeker Oct 25, 2023
2247a50
Add test for CooptCodec and adjust how error recover is handled with …
Dragon-Seeker Oct 27, 2023
d14078f
Implement MapCarrier with Endec
Dragon-Seeker Nov 7, 2023
3bcfaca
Implement BinaryNbtSerializer and fix class loading issue with UwUSha…
Dragon-Seeker Nov 11, 2023
342aefa
[endec] initial cleanup and first refactors
gliscowo Nov 17, 2023
ed9d552
[endec] move struct-, sequence- and map- (de)serializer types into ba…
gliscowo Nov 17, 2023
1b3b887
[endec] clean up all (de)serializer implementations, standardize (de)…
gliscowo Nov 17, 2023
7d71fec
Just going to leave this here
Dragon-Seeker Nov 19, 2023
6a7d27e
[endec] do you like electronic dance music?
gliscowo Nov 19, 2023
a6588c6
[endec] add support for assuming attributes when embedding endec into…
gliscowo Nov 19, 2023
797d059
[endec] remove reimplementations of vanilla recipes codecs and associ…
gliscowo Nov 19, 2023
7ba7a8a
[endec] quote field names in deserializer error messages
gliscowo Nov 19, 2023
5251110
[endec] superficial cleanup
gliscowo Nov 19, 2023
192dae0
Cleanup ReflectionEndecBuilder
Dragon-Seeker Nov 19, 2023
e4f8808
[endec] initial cleanup on endec interface
gliscowo Nov 20, 2023
269f57a
[endec] remove MapEndec and ListEndec classes, being adding basic doc…
gliscowo Nov 20, 2023
9508b19
[endec] refactor MapCarrier implementations and related infrastructur…
gliscowo Nov 20, 2023
9b0cc57
[endec] add Endec.forEnum
gliscowo Nov 20, 2023
9a6edbe
Merge branch '1.20.2' into 1.20.2-Codeck
gliscowo Nov 21, 2023
3b9e98c
[endec] remove ReflectionEndecBuilder.createEnumEndec, add vector end…
gliscowo Nov 21, 2023
9d7548f
[endec] clean up ReflectiveEndecBuilder and RecordEndec
gliscowo Nov 22, 2023
5a1b521
[endec] add endec-based read/write methods to PacketByteBuf
gliscowo Nov 22, 2023
370c98d
version bump for initial endec alpha
gliscowo Nov 22, 2023
22e5ca0
[endec] fix visibility of static methods on BuiltInEndecs, replace Ma…
gliscowo Nov 24, 2023
176f781
move public extension interfaces (EndecBuffer, MatrixStackTransformer…
gliscowo Nov 24, 2023
1649f83
Fix Vector endec validation checking for valid input
Dragon-Seeker Nov 24, 2023
c425c7c
[endec] change inner type of Endec#optionalFieldOf to an optional, sk…
gliscowo Nov 25, 2023
308c4f7
Implement Forwarding classes for (De)-Serializer
Dragon-Seeker Nov 27, 2023
5aac029
Make Forwarding (De)-Serializer constructor not private
Dragon-Seeker Nov 28, 2023
fead80e
[endec] support non-constant default via value factory for struct fie…
gliscowo Dec 2, 2023
fc48514
[endec] completely remove PacketBufSerializer and replace with endecs…
gliscowo Dec 4, 2023
6ecc469
[endec] completely remove NbtKey (superseded by KeyedEndec)
gliscowo Dec 5, 2023
93a58d5
update yarn, loader and fabric
gliscowo Dec 5, 2023
dd57589
[endec] clean up package structure
gliscowo Dec 5, 2023
1eefcbf
[endec] fix injected interface paths after package restructure
gliscowo Dec 5, 2023
f460e60
[endec] my brain is expanding
gliscowo Dec 5, 2023
8de7b27
[endec] fix NbtEndec for non-self-describing formats
gliscowo Dec 5, 2023
6d0c7c1
1.20.3
gliscowo Dec 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//file:noinspection GradlePackageVersionRange
plugins {
id 'fabric-loom' version '1.2-SNAPSHOT'
id 'fabric-loom' version '1.4-SNAPSHOT'
id 'maven-publish'
id 'io.github.juuxel.loom-vineflower' version '1.11.0'
}

allprojects {
Expand Down Expand Up @@ -124,7 +123,7 @@ loom {
}

dependencies {
modLocalRuntime("me.shedaniel:RoughlyEnoughItems-fabric:${project.rei_version}")
// modLocalRuntime("me.shedaniel:RoughlyEnoughItems-fabric:${project.rei_version}")
modCompileOnly("me.shedaniel:RoughlyEnoughItems-api-fabric:${project.rei_version}")

modCompileOnly("dev.emi:emi-fabric:${project.emi_version}")
Expand Down
16 changes: 8 additions & 8 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
org.gradle.jvmargs=-Xmx2G
# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_base_version=1.20.2
minecraft_version=1.20.2
yarn_mappings=1.20.2+build.1
loader_version=0.14.22
minecraft_base_version=1.20.3
minecraft_version=1.20.3
yarn_mappings=1.20.3+build.1
loader_version=0.15.0
# Mod Properties
mod_version=0.11.3
mod_version=0.12.0-endec-alpha.15
maven_group=io.wispforest
archives_base_name=owo-lib
# Dependencies
fabric_version=0.89.2+1.20.2
fabric_version=0.91.1+1.20.3

# https://maven.shedaniel.me/me/shedaniel/RoughlyEnoughItems-fabric/
rei_version=13.0.661
Expand All @@ -23,7 +23,7 @@ emi_version=1.0.19+1.20.1
jankson_version=1.2.2

# https://maven.terraformersmc.com/releases/com/terraformersmc/modmenu
modmenu_version=8.0.0
modmenu_version=9.0.0-pre.1

# https://maven.nucleoid.xyz/xyz/nucleoid/server-translations-api/
stapi_version=2.0.0+1.20
stapi_version=2.2.0+1.20.3-rc1
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void launch() {
if (FabricLoader.getInstance().isModLoaded("owo-impl")) return;

try {
if (GraphicsEnvironment.isHeadless() || FORCE_HEADLESS || System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("mac")) {
if (System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("mac") || GraphicsEnvironment.isHeadless() || FORCE_HEADLESS) {
SentinelConsole.run();
} else {
SentinelWindow.open();
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/io/wispforest/owo/Owo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import io.wispforest.owo.client.screens.ScreenInternals;
import io.wispforest.owo.command.debug.OwoDebugCommands;
import io.wispforest.owo.ops.LootOps;
import io.wispforest.owo.text.CustomTextRegistry;
import io.wispforest.owo.text.InsertingTextContent;
import io.wispforest.owo.ui.parsing.UIModelLoader;
import io.wispforest.owo.util.Wisdom;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.resource.ResourceType;
import net.minecraft.server.MinecraftServer;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
Expand Down Expand Up @@ -51,7 +49,7 @@ public class Owo implements ModInitializer {
@ApiStatus.Internal
public void onInitialize() {
LootOps.registerListener();
InsertingTextContent.init();
CustomTextRegistry.register(InsertingTextContent.TYPE, "index");
ScreenInternals.init();

ServerLifecycleEvents.SERVER_STARTING.register(server -> SERVER = server);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.wispforest.owo.client.screens;

import io.wispforest.owo.serialization.Endec;
import io.wispforest.owo.serialization.endec.ReflectiveEndecBuilder;
import net.minecraft.entity.player.PlayerEntity;
import org.jetbrains.annotations.NotNull;

Expand All @@ -12,12 +14,21 @@ public interface OwoScreenHandler {
* and will automatically synchronize to the client - think {@link net.minecraft.screen.PropertyDelegate}
* but without being restricted to integers
*
* @param klass The class of the property's value
* @param clazz The class of the property's value
* @param endec The endec to use for (de-)serializing the value of this property over the network
* @param initial The value with which to initialize the property
* @return The created property
*/
default <T> SyncedProperty<T> createProperty(Class<T> klass, T initial) {
throw new IllegalStateException("Implemented in ScreenHandlerMixin");
default <T> SyncedProperty<T> createProperty(Class<T> clazz, Endec<T> endec, T initial) {
throw new UnsupportedOperationException("Implemented in ScreenHandlerMixin");
}

/**
* Shorthand for {@link #createProperty(Class, Endec, Object)} which creates the endec
* through {@link ReflectiveEndecBuilder#get(Class)}
*/
default <T> SyncedProperty<T> createProperty(Class<T> clazz, T initial) {
return this.createProperty(clazz, ReflectiveEndecBuilder.get(clazz), initial);
}

/**
Expand All @@ -28,11 +39,20 @@ default <T> SyncedProperty<T> createProperty(Class<T> klass, T initial) {
*
* @param messageClass The class of message to send, must be a record - much like
* packets in an {@link io.wispforest.owo.network.OwoNetChannel}
* @param endec The endec to use for (de-)serializing messages sent over the network
* @param handler The handler to execute when a message of the given class is
* received on the server
*/
default <R extends Record> void addServerboundMessage(Class<R> messageClass, Endec<R> endec, Consumer<R> handler) {
throw new UnsupportedOperationException("Implemented in ScreenHandlerMixin");
}

/**
* Shorthand for {@link #addServerboundMessage(Class, Endec, Consumer)} which creates the endec
* through {@link ReflectiveEndecBuilder#get(Class)}
*/
default <R extends Record> void addServerboundMessage(Class<R> messageClass, Consumer<R> handler) {
throw new IllegalStateException("Implemented in ScreenHandlerMixin");
this.addServerboundMessage(messageClass, ReflectiveEndecBuilder.get(messageClass), handler);
}

/**
Expand All @@ -43,27 +63,36 @@ default <R extends Record> void addServerboundMessage(Class<R> messageClass, Con
*
* @param messageClass The class of message to send, must be a record - much like
* packets in an {@link io.wispforest.owo.network.OwoNetChannel}
* @param endec The endec to use for (de-)serializing messages sent over the network
* @param handler The handler to execute when a message of the given class is
* received on the client
*/
default <R extends Record> void addClientboundMessage(Class<R> messageClass, Endec<R> endec, Consumer<R> handler) {
throw new UnsupportedOperationException("Implemented in ScreenHandlerMixin");
}

/**
* Shorthand for {@link #addClientboundMessage(Class, Endec, Consumer)} which creates the endec
* through {@link ReflectiveEndecBuilder#get(Class)}
*/
default <R extends Record> void addClientboundMessage(Class<R> messageClass, Consumer<R> handler) {
throw new IllegalStateException("Implemented in ScreenHandlerMixin");
this.addClientboundMessage(messageClass, ReflectiveEndecBuilder.get(messageClass), handler);
}

/**
* Send the given message. This message must have been previously
* registered through a call to {@link #addServerboundMessage(Class, Consumer)}
* or {@link #addClientboundMessage(Class, Consumer)} - this also dictates where
* registered through a call to {@link #addServerboundMessage(Class, Endec, Consumer)}
* or {@link #addClientboundMessage(Class, Endec, Consumer)} - this also dictates where
* the message will be sent to
*/
default <R extends Record> void sendMessage(@NotNull R message) {
throw new IllegalStateException("Implemented in ScreenHandlerMixin");
throw new UnsupportedOperationException("Implemented in ScreenHandlerMixin");
}

/**
* @return The player this screen handler is attached to
*/
default PlayerEntity player() {
throw new IllegalStateException("Implemented in ScreenHandlerMixin");
throw new UnsupportedOperationException("Implemented in ScreenHandlerMixin");
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.wispforest.owo.client.screens;

import io.wispforest.owo.network.serialization.PacketBufSerializer;
import io.wispforest.owo.serialization.Endec;
import org.jetbrains.annotations.ApiStatus;

import java.util.function.Consumer;

@ApiStatus.Internal
public record ScreenhandlerMessageData<T>(int id, boolean clientbound, PacketBufSerializer<T> serializer, Consumer<T> handler) {}
public record ScreenhandlerMessageData<T>(int id, boolean clientbound, Endec<T> endec, Consumer<T> handler) {}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package io.wispforest.owo.client.screens;

import io.wispforest.owo.network.serialization.PacketBufSerializer;
import io.wispforest.owo.serialization.Endec;
import io.wispforest.owo.util.Observable;
import net.minecraft.network.PacketByteBuf;
import org.jetbrains.annotations.ApiStatus;

public class SyncedProperty<T> extends Observable<T> {
private final int index;
private final PacketBufSerializer<T> serializer;
private final Endec<T> endec;
private boolean needsSync;

@ApiStatus.Internal
public SyncedProperty(int index, Class<T> klass, T initial) {
public SyncedProperty(int index, Endec<T> endec, T initial) {
super(initial);

this.index = index;
this.serializer = PacketBufSerializer.get(klass);
this.endec = endec;
}

public int index() {
Expand All @@ -30,13 +30,12 @@ public boolean needsSync() {
@ApiStatus.Internal
public void write(PacketByteBuf buf) {
needsSync = false;

serializer.serializer().accept(buf, value);
buf.write(this.endec, value);
}

@ApiStatus.Internal
public void read(PacketByteBuf buf) {
set(serializer.deserializer().apply(buf));
this.set(buf.read(this.endec));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private static void applyServer(MinecraftServer server, ServerPlayerEntity playe

read(buf, (option, optionBuf) -> {
var config = CLIENT_OPTION_STORAGE.computeIfAbsent(connection, $ -> new HashMap<>()).computeIfAbsent(option.configName(), s -> new HashMap<>());
config.put(option.key(), option.serializer().deserializer().apply(optionBuf));
config.put(option.key(), optionBuf.read(option.endec()));
});
}

Expand Down
15 changes: 8 additions & 7 deletions src/main/java/io/wispforest/owo/config/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import io.wispforest.owo.Owo;
import io.wispforest.owo.config.annotation.RestartRequired;
import io.wispforest.owo.network.serialization.PacketBufSerializer;
import io.wispforest.owo.serialization.Endec;
import io.wispforest.owo.serialization.endec.ReflectiveEndecBuilder;
import io.wispforest.owo.util.Observable;
import net.minecraft.network.PacketByteBuf;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -36,7 +37,7 @@ public final class Option<T> {
private final Class<T> clazz;

private final ConfigWrapper.@Nullable Constraint constraint;
private final @Nullable PacketBufSerializer<T> serializer;
private final @Nullable Endec<T> endec;
private final SyncMode syncMode;

/**
Expand Down Expand Up @@ -79,7 +80,7 @@ public Option(String configName,

this.constraint = constraint;
this.syncMode = syncMode;
this.serializer = syncMode.isNone() ? null : (PacketBufSerializer<T>) PacketBufSerializer.getGeneric(this.backingField.field.getGenericType());
this.endec = syncMode.isNone() ? null : (Endec<T>) ReflectiveEndecBuilder.get(this.backingField.field.getGenericType());
}

/**
Expand Down Expand Up @@ -164,7 +165,7 @@ public void observe(Consumer<T> observer) {
* @param buf The packet buffer to write to
*/
void write(PacketByteBuf buf) {
this.serializer.serializer().accept(buf, this.value());
buf.write(this.endec, this.value());
}

/**
Expand All @@ -176,7 +177,7 @@ void write(PacketByteBuf buf) {
* the server's value otherwise
*/
T read(PacketByteBuf buf) {
final var newValue = this.serializer.deserializer().apply(buf);
final var newValue = buf.read(this.endec);

if (!Objects.equals(newValue, this.value()) && this.backingField.hasAnnotation(RestartRequired.class)) {
return newValue;
Expand All @@ -191,8 +192,8 @@ T read(PacketByteBuf buf) {
/**
* @return The serializer for this option's value
*/
PacketBufSerializer<T> serializer() {
return this.serializer;
Endec<T> endec() {
return this.endec;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ItemGroupButtonWidget(int x, int y, int baseU, OwoItemGroup.ButtonDefinit
}

@Override
public void renderButton(DrawContext context, int mouseX, int mouseY, float delta) {
public void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) {
RenderSystem.enableDepthTest();
context.drawTexture(this.definition.texture(), this.getX(), this.getY(), this.baseU, this.isSelected() || this.isSelected ? this.height : 0, this.width, this.height, 64, 64);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.wispforest.owo.mixin;

import io.wispforest.owo.util.pond.MatrixStackTransformer;
import io.wispforest.owo.ui.util.MatrixStackTransformer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.util.math.MatrixStack;
import org.spongepowered.asm.mixin.Mixin;
Expand Down
30 changes: 19 additions & 11 deletions src/main/java/io/wispforest/owo/mixin/ItemStackMixin.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package io.wispforest.owo.mixin;

import io.wispforest.owo.nbt.NbtCarrier;
import io.wispforest.owo.nbt.NbtKey;
import io.wispforest.owo.serialization.util.MapCarrier;
import io.wispforest.owo.serialization.endec.KeyedEndec;
import io.wispforest.owo.serialization.format.forwarding.ForwardingDeserializer;
import io.wispforest.owo.serialization.format.forwarding.ForwardingSerializer;
import io.wispforest.owo.serialization.format.nbt.NbtDeserializer;
import io.wispforest.owo.serialization.format.nbt.NbtSerializer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

@SuppressWarnings("AddedMixinMembersNamePattern")
@Mixin(ItemStack.class)
public abstract class ItemStackMixin implements NbtCarrier {
public abstract class ItemStackMixin implements MapCarrier {

@Shadow
private @Nullable NbtCompound nbt;
Expand All @@ -19,23 +24,26 @@ public abstract class ItemStackMixin implements NbtCarrier {
public abstract NbtCompound getOrCreateNbt();

@Override
public <T> T get(@NotNull NbtKey<T> key) {
return key.get(this.getOrCreateNbt());
public <T> T getWithErrors(@NotNull KeyedEndec<T> key) {
if (!this.has(key)) return key.defaultValue();
return key.endec()
.decodeFully(e -> ForwardingDeserializer.humanReadable(NbtDeserializer.of(e)), this.nbt.get(key.key()));
}

@Override
public <T> void put(@NotNull NbtKey<T> key, @NotNull T value) {
key.put(this.getOrCreateNbt(), value);
public <T> void put(@NotNull KeyedEndec<T> key, @NotNull T value) {
this.getOrCreateNbt()
.put(key.key(), key.endec().encodeFully(() -> ForwardingSerializer.humanReadable(NbtSerializer.of()), value));
}

@Override
public <T> void delete(@NotNull NbtKey<T> key) {
public <T> void delete(@NotNull KeyedEndec<T> key) {
if (this.nbt == null) return;
key.delete(this.nbt);
this.nbt.remove(key.key());
}

@Override
public <T> boolean has(@NotNull NbtKey<T> key) {
return this.nbt != null && key.isIn(this.nbt);
public <T> boolean has(@NotNull KeyedEndec<T> key) {
return this.nbt != null && this.nbt.contains(key.key());
}
}
Loading
Loading