encoding cache

This commit is contained in:
lcy0x1
2024-07-01 14:26:50 +08:00
parent 42e788a018
commit 18c302008f
2 changed files with 13 additions and 11 deletions

View File

@@ -18,7 +18,7 @@ loader_version_range=[2,)
mod_id=l2core mod_id=l2core
mod_name=L2Core mod_name=L2Core
mod_license=LGPL-2.1 mod_license=LGPL-2.1
mod_version=3.0.3-pre7 mod_version=3.0.3-pre8
mod_group_id=dev.xkmc mod_group_id=dev.xkmc
mod_authors=lcy0x1 mod_authors=lcy0x1
mod_description=Core Library mod for all L2 mods mod_description=Core Library mod for all L2 mods

View File

@@ -21,38 +21,40 @@ public record DCReg(DeferredRegister<DataComponentType<?>> reg) {
return new DCReg(parent.make(BuiltInRegistries.DATA_COMPONENT_TYPE)); return new DCReg(parent.make(BuiltInRegistries.DATA_COMPONENT_TYPE));
} }
public <T> DCVal<T> reg(String id, Codec<T> codec, StreamCodec<? super RegistryFriendlyByteBuf, T> stream) { public <T> DCVal<T> reg(String id, Codec<T> codec, StreamCodec<? super RegistryFriendlyByteBuf, T> stream, boolean cache) {
return new DCValImpl<>(reg.register(id, () -> DataComponentType.<T>builder().persistent(codec).networkSynchronized(stream).build())); var builder = DataComponentType.<T>builder().persistent(codec).networkSynchronized(stream);
if (cache) builder.cacheEncoding();
return new DCValImpl<>(reg.register(id, builder::build));
} }
public <T> DCVal<T> reg(String id, Class<T> cls) { public <T> DCVal<T> reg(String id, Class<T> cls, boolean cache) {
var cdc = new CodecAdaptor<>(cls); var cdc = new CodecAdaptor<>(cls);
return new DCValImpl<>(reg.register(id, () -> DataComponentType.<T>builder().persistent(cdc).networkSynchronized(cdc.toNetwork()).build())); return reg(id, cdc, cdc.toNetwork(), cache);
} }
public DCVal<Integer> intVal(String id) { public DCVal<Integer> intVal(String id) {
return reg(id, Codec.INT, ByteBufCodecs.INT); return reg(id, Codec.INT, ByteBufCodecs.INT, false);
} }
public DCVal<Double> doubleVal(String id) { public DCVal<Double> doubleVal(String id) {
return reg(id, Codec.DOUBLE, ByteBufCodecs.DOUBLE); return reg(id, Codec.DOUBLE, ByteBufCodecs.DOUBLE, false);
} }
public DCVal<Long> longVal(String id) { public DCVal<Long> longVal(String id) {
return reg(id, Codec.LONG, ByteBufCodecs.VAR_LONG); return reg(id, Codec.LONG, ByteBufCodecs.VAR_LONG, false);
} }
public DCVal<String> str(String id) { public DCVal<String> str(String id) {
return reg(id, Codec.STRING, ByteBufCodecs.STRING_UTF8); return reg(id, Codec.STRING, ByteBufCodecs.STRING_UTF8, false);
} }
public DCVal<UUID> uuid(String id) { public DCVal<UUID> uuid(String id) {
return reg(id, Codec.STRING.xmap(UUID::fromString, UUID::toString), return reg(id, Codec.STRING.xmap(UUID::fromString, UUID::toString),
StreamCodec.of((b, e) -> FriendlyByteBuf.writeUUID(b, e), b -> FriendlyByteBuf.readUUID(b))); StreamCodec.of((b, e) -> FriendlyByteBuf.writeUUID(b, e), b -> FriendlyByteBuf.readUUID(b)), true);
} }
public DCVal<Component> component(String id) { public DCVal<Component> component(String id) {
return reg(id, ComponentSerialization.CODEC, ComponentSerialization.STREAM_CODEC); return reg(id, ComponentSerialization.CODEC, ComponentSerialization.STREAM_CODEC, true);
} }
private record DCValImpl<T>(DeferredHolder<DataComponentType<?>, DataComponentType<T>> val) implements DCVal<T> { private record DCValImpl<T>(DeferredHolder<DataComponentType<?>, DataComponentType<T>> val) implements DCVal<T> {