diff --git a/build.gradle b/build.gradle index b30e747..72c42fa 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ plugins { } def ENV = System.getenv() -def vvVer = "4.0.0-21w19a" +def vvVer = "4.0.0-21w19a-SNAPSHOT" description = "Client-side and server-side ViaVersion implementation for Fabric" version = "0.4.0+" + ENV.GITHUB_RUN_NUMBER + "-" + getBranch() diff --git a/gradle.properties b/gradle.properties index 549a923..3663089 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1,2 @@ -org.gradle.jvmargs=-Xmx1G -XX:+UseG1GC +# rip my ram +org.gradle.jvmargs=-Xmx512M \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index e5338d3..0f80bbf 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/handler/FabricDecodeHandler.java b/src/main/java/com/viaversion/fabric/common/handler/FabricDecodeHandler.java similarity index 70% rename from viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/handler/FabricDecodeHandler.java rename to src/main/java/com/viaversion/fabric/common/handler/FabricDecodeHandler.java index 1a245a1..6f3686e 100644 --- a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/handler/FabricDecodeHandler.java +++ b/src/main/java/com/viaversion/fabric/common/handler/FabricDecodeHandler.java @@ -1,14 +1,13 @@ -package com.viaversion.fabric.mc18.handler; +package com.viaversion.fabric.common.handler; -import com.viaversion.fabric.common.handler.CommonTransformer; -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.MessageToMessageDecoder; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.exception.CancelCodecException; import com.viaversion.viaversion.exception.CancelDecoderException; import com.viaversion.viaversion.util.PipelineUtil; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToMessageDecoder; import java.lang.reflect.InvocationTargetException; import java.util.List; @@ -16,8 +15,6 @@ import java.util.List; @ChannelHandler.Sharable public class FabricDecodeHandler extends MessageToMessageDecoder { private final UserConnection info; - private boolean handledCompression; - private boolean skipDoubleTransform; public FabricDecodeHandler(UserConnection info) { this.info = info; @@ -30,12 +27,6 @@ public class FabricDecodeHandler extends MessageToMessageDecoder { // https://github.com/ViaVersion/ViaVersion/blob/master/velocity/src/main/java/us/myles/ViaVersion/velocity/handlers/VelocityDecodeHandler.java @Override protected void decode(ChannelHandlerContext ctx, ByteBuf bytebuf, List out) throws Exception { - if (skipDoubleTransform) { - skipDoubleTransform = false; - out.add(bytebuf.retain()); - return; - } - if (!info.checkIncomingPacket()) throw CancelDecoderException.generate(null); if (!info.shouldTransformPacket()) { out.add(bytebuf.retain()); @@ -44,38 +35,25 @@ public class FabricDecodeHandler extends MessageToMessageDecoder { ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf); try { - boolean needsCompress = handleCompressionOrder(ctx, transformedBuf); - info.transformIncoming(transformedBuf, CancelDecoderException::generate); - if (needsCompress) { - CommonTransformer.compress(ctx, transformedBuf); - skipDoubleTransform = true; - } out.add(transformedBuf.retain()); } finally { transformedBuf.release(); } } - private boolean handleCompressionOrder(ChannelHandlerContext ctx, ByteBuf buf) throws InvocationTargetException { - if (handledCompression) return false; - + private void reorder(ChannelHandlerContext ctx) { int decoderIndex = ctx.pipeline().names().indexOf("decompress"); - if (decoderIndex == -1) return false; - handledCompression = true; + if (decoderIndex == -1) return; if (decoderIndex > ctx.pipeline().names().indexOf("via-decoder")) { - // Need to decompress this packet due to bad order - CommonTransformer.decompress(ctx, buf); ChannelHandler encoder = ctx.pipeline().get("via-encoder"); ChannelHandler decoder = ctx.pipeline().get("via-decoder"); ctx.pipeline().remove(encoder); ctx.pipeline().remove(decoder); ctx.pipeline().addAfter("compress", "via-encoder", encoder); ctx.pipeline().addAfter("decompress", "via-decoder", decoder); - return true; } - return false; } @Override @@ -83,4 +61,12 @@ public class FabricDecodeHandler extends MessageToMessageDecoder { if (PipelineUtil.containsCause(cause, CancelCodecException.class)) return; super.exceptionCaught(ctx, cause); } + + @Override + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { + if (evt instanceof PipelineReorderEvent) { + reorder(ctx); + } + super.userEventTriggered(ctx, evt); + } } \ No newline at end of file diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/handler/FabricEncodeHandler.java b/src/main/java/com/viaversion/fabric/common/handler/FabricEncodeHandler.java similarity index 53% rename from viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/handler/FabricEncodeHandler.java rename to src/main/java/com/viaversion/fabric/common/handler/FabricEncodeHandler.java index db26fba..28771c8 100644 --- a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/handler/FabricEncodeHandler.java +++ b/src/main/java/com/viaversion/fabric/common/handler/FabricEncodeHandler.java @@ -1,22 +1,19 @@ -package com.viaversion.fabric.mc18.handler; +package com.viaversion.fabric.common.handler; -import com.viaversion.fabric.common.handler.CommonTransformer; -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.MessageToMessageEncoder; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.exception.CancelCodecException; import com.viaversion.viaversion.exception.CancelEncoderException; import com.viaversion.viaversion.util.PipelineUtil; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToMessageEncoder; -import java.lang.reflect.InvocationTargetException; import java.util.List; @ChannelHandler.Sharable public class FabricEncodeHandler extends MessageToMessageEncoder { private final UserConnection info; - private boolean handledCompression; public FabricEncodeHandler(UserConnection info) { this.info = info; @@ -32,39 +29,14 @@ public class FabricEncodeHandler extends MessageToMessageEncoder { ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf); try { - boolean needsCompress = handleCompressionOrder(ctx, transformedBuf); - info.transformOutgoing(transformedBuf, CancelEncoderException::generate); - if (needsCompress) { - CommonTransformer.compress(ctx, transformedBuf); - } out.add(transformedBuf.retain()); } finally { transformedBuf.release(); } } - private boolean handleCompressionOrder(ChannelHandlerContext ctx, ByteBuf buf) throws InvocationTargetException { - if (handledCompression) return false; - - int encoderIndex = ctx.pipeline().names().indexOf("compress"); - if (encoderIndex == -1) return false; - handledCompression = true; - if (encoderIndex > ctx.pipeline().names().indexOf("via-encoder")) { - // Need to decompress this packet due to bad order - CommonTransformer.decompress(ctx, buf); - ChannelHandler encoder = ctx.pipeline().get("via-encoder"); - ChannelHandler decoder = ctx.pipeline().get("via-decoder"); - ctx.pipeline().remove(encoder); - ctx.pipeline().remove(decoder); - ctx.pipeline().addAfter("compress", "via-encoder", encoder); - ctx.pipeline().addAfter("decompress", "via-decoder", decoder); - return true; - } - return false; - } - @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { if (PipelineUtil.containsCause(cause, CancelCodecException.class)) return; diff --git a/src/main/java/com/viaversion/fabric/common/handler/PipelineReorderEvent.java b/src/main/java/com/viaversion/fabric/common/handler/PipelineReorderEvent.java new file mode 100644 index 0000000..ed4ffc9 --- /dev/null +++ b/src/main/java/com/viaversion/fabric/common/handler/PipelineReorderEvent.java @@ -0,0 +1,4 @@ +package com.viaversion.fabric.common.handler; + +public class PipelineReorderEvent { +} diff --git a/src/main/java/com/viaversion/fabric/common/provider/AbstractFabricPlatform.java b/src/main/java/com/viaversion/fabric/common/provider/AbstractFabricPlatform.java new file mode 100644 index 0000000..65739c7 --- /dev/null +++ b/src/main/java/com/viaversion/fabric/common/provider/AbstractFabricPlatform.java @@ -0,0 +1,125 @@ +package com.viaversion.fabric.common.provider; + +import com.viaversion.fabric.common.platform.FabricViaAPI; +import com.viaversion.fabric.common.platform.FabricViaConfig; +import com.viaversion.fabric.common.util.JLoggerToLog4j; +import com.viaversion.viaversion.api.ViaAPI; +import com.viaversion.viaversion.api.configuration.ConfigurationProvider; +import com.viaversion.viaversion.api.configuration.ViaVersionConfig; +import com.viaversion.viaversion.api.platform.ViaPlatform; +import com.viaversion.viaversion.libs.gson.JsonArray; +import com.viaversion.viaversion.libs.gson.JsonObject; +import net.fabricmc.loader.api.FabricLoader; +import net.fabricmc.loader.api.ModContainer; +import net.fabricmc.loader.api.Version; +import net.fabricmc.loader.api.metadata.ModMetadata; +import org.apache.logging.log4j.LogManager; + +import java.io.File; +import java.nio.file.Path; +import java.util.UUID; +import java.util.logging.Logger; + +public abstract class AbstractFabricPlatform implements ViaPlatform { + private final Logger logger = new JLoggerToLog4j(LogManager.getLogger("ViaVersion")); + private final FabricViaConfig config; + private final File dataFolder; + private final ViaAPI api; + + { + Path configDir = FabricLoader.getInstance().getConfigDir().resolve("ViaFabric"); + config = new FabricViaConfig(configDir.resolve("viaversion.yml").toFile()); + dataFolder = configDir.toFile(); + api = new FabricViaAPI(); + } + + @Override + public boolean isProxy() { + return false; + } + + @Override + public void onReload() { + } + + @Override + public Logger getLogger() { + return logger; + } + + @Override + public ViaVersionConfig getConf() { + return config; + } + + @Override + public ViaAPI getApi() { + return api; + } + + @Override + public File getDataFolder() { + return dataFolder; + } + + @Override + public String getPluginVersion() { + return FabricLoader.getInstance().getModContainer("viaversion").map(ModContainer::getMetadata) + .map(ModMetadata::getVersion).map(Version::getFriendlyString).orElse("UNKNOWN"); + } + + @Override + public String getPlatformName() { + return "ViaFabric"; + } + + @Override + public String getPlatformVersion() { + return FabricLoader.getInstance().getModContainer("viafabric") + .get().getMetadata().getVersion().getFriendlyString(); + } + + @Override + public boolean isPluginEnabled() { + return true; + } + + @Override + public ConfigurationProvider getConfigurationProvider() { + return config; + } + + @Override + public boolean isOldClientsAllowed() { + return true; + } + + @Override + public JsonObject getDump() { + JsonObject platformSpecific = new JsonObject(); + JsonArray mods = new JsonArray(); + FabricLoader.getInstance().getAllMods().stream().map((mod) -> { + JsonObject jsonMod = new JsonObject(); + jsonMod.addProperty("id", mod.getMetadata().getId()); + jsonMod.addProperty("name", mod.getMetadata().getName()); + jsonMod.addProperty("version", mod.getMetadata().getVersion().getFriendlyString()); + JsonArray authors = new JsonArray(); + mod.getMetadata().getAuthors().stream().map(it -> { + JsonObject info = new JsonObject(); + JsonObject contact = new JsonObject(); + it.getContact().asMap().entrySet().stream() + .forEach(c -> contact.addProperty(c.getKey(), c.getValue())); + info.add("contact", contact); + info.addProperty("name", it.getName()); + + return info; + }).forEach(authors::add); + jsonMod.add("authors", authors); + + return jsonMod; + }).forEach(mods::add); + + platformSpecific.add("mods", mods); + return platformSpecific; + } +} diff --git a/src/main/java/com/viaversion/fabric/common/util/ProtocolUtils.java b/src/main/java/com/viaversion/fabric/common/util/ProtocolUtils.java index 926e7a4..50aa0b4 100644 --- a/src/main/java/com/viaversion/fabric/common/util/ProtocolUtils.java +++ b/src/main/java/com/viaversion/fabric/common/util/ProtocolUtils.java @@ -25,6 +25,7 @@ public class ProtocolUtils { Integer.parseInt(s + '0'); return true; } catch (NumberFormatException e2) { + if (ProtocolVersion.getClosest(s) != null) return true; return ProtocolVersion.getProtocols().stream() .map(ProtocolVersion::getName) .flatMap(str -> Stream.concat( diff --git a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/handler/FabricDecodeHandler.java b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/handler/FabricDecodeHandler.java deleted file mode 100644 index 0ec3d66..0000000 --- a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/handler/FabricDecodeHandler.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.viaversion.fabric.mc114.handler; - -import com.viaversion.fabric.common.handler.CommonTransformer; -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.MessageToMessageDecoder; -import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.exception.CancelCodecException; -import com.viaversion.viaversion.exception.CancelDecoderException; -import com.viaversion.viaversion.util.PipelineUtil; - -import java.lang.reflect.InvocationTargetException; -import java.util.List; - -@ChannelHandler.Sharable -public class FabricDecodeHandler extends MessageToMessageDecoder { - private final UserConnection info; - private boolean handledCompression; - private boolean skipDoubleTransform; - - public FabricDecodeHandler(UserConnection info) { - this.info = info; - } - - public UserConnection getInfo() { - return info; - } - - // https://github.com/ViaVersion/ViaVersion/blob/master/velocity/src/main/java/us/myles/ViaVersion/velocity/handlers/VelocityDecodeHandler.java - @Override - protected void decode(ChannelHandlerContext ctx, ByteBuf bytebuf, List out) throws Exception { - if (skipDoubleTransform) { - skipDoubleTransform = false; - out.add(bytebuf.retain()); - return; - } - - if (!info.checkIncomingPacket()) throw CancelDecoderException.generate(null); - if (!info.shouldTransformPacket()) { - out.add(bytebuf.retain()); - return; - } - - ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf); - try { - boolean needsCompress = handleCompressionOrder(ctx, transformedBuf); - - info.transformIncoming(transformedBuf, CancelDecoderException::generate); - - if (needsCompress) { - CommonTransformer.compress(ctx, transformedBuf); - skipDoubleTransform = true; - } - out.add(transformedBuf.retain()); - } finally { - transformedBuf.release(); - } - } - - private boolean handleCompressionOrder(ChannelHandlerContext ctx, ByteBuf buf) throws InvocationTargetException { - if (handledCompression) return false; - - int decoderIndex = ctx.pipeline().names().indexOf("decompress"); - if (decoderIndex == -1) return false; - handledCompression = true; - if (decoderIndex > ctx.pipeline().names().indexOf("via-decoder")) { - // Need to decompress this packet due to bad order - CommonTransformer.decompress(ctx, buf); - ChannelHandler encoder = ctx.pipeline().get("via-encoder"); - ChannelHandler decoder = ctx.pipeline().get("via-decoder"); - ctx.pipeline().remove(encoder); - ctx.pipeline().remove(decoder); - ctx.pipeline().addAfter("compress", "via-encoder", encoder); - ctx.pipeline().addAfter("decompress", "via-decoder", decoder); - return true; - } - return false; - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - if (PipelineUtil.containsCause(cause, CancelCodecException.class)) return; - super.exceptionCaught(ctx, cause); - } -} \ No newline at end of file diff --git a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/handler/FabricEncodeHandler.java b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/handler/FabricEncodeHandler.java deleted file mode 100644 index dc90692..0000000 --- a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/handler/FabricEncodeHandler.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.viaversion.fabric.mc114.handler; - -import com.viaversion.fabric.common.handler.CommonTransformer; -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.MessageToMessageEncoder; -import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.exception.CancelCodecException; -import com.viaversion.viaversion.exception.CancelEncoderException; -import com.viaversion.viaversion.util.PipelineUtil; - -import java.lang.reflect.InvocationTargetException; -import java.util.List; - -@ChannelHandler.Sharable -public class FabricEncodeHandler extends MessageToMessageEncoder { - private final UserConnection info; - private boolean handledCompression; - - public FabricEncodeHandler(UserConnection info) { - this.info = info; - } - - @Override - protected void encode(final ChannelHandlerContext ctx, ByteBuf bytebuf, List out) throws Exception { - if (!info.checkOutgoingPacket()) throw CancelEncoderException.generate(null); - if (!info.shouldTransformPacket()) { - out.add(bytebuf.retain()); - return; - } - - ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf); - try { - boolean needsCompress = handleCompressionOrder(ctx, transformedBuf); - - info.transformOutgoing(transformedBuf, CancelEncoderException::generate); - - if (needsCompress) { - CommonTransformer.compress(ctx, transformedBuf); - } - out.add(transformedBuf.retain()); - } finally { - transformedBuf.release(); - } - } - - private boolean handleCompressionOrder(ChannelHandlerContext ctx, ByteBuf buf) throws InvocationTargetException { - if (handledCompression) return false; - - int encoderIndex = ctx.pipeline().names().indexOf("compress"); - if (encoderIndex == -1) return false; - handledCompression = true; - if (encoderIndex > ctx.pipeline().names().indexOf("via-encoder")) { - // Need to decompress this packet due to bad order - CommonTransformer.decompress(ctx, buf); - ChannelHandler encoder = ctx.pipeline().get("via-encoder"); - ChannelHandler decoder = ctx.pipeline().get("via-decoder"); - ctx.pipeline().remove(encoder); - ctx.pipeline().remove(decoder); - ctx.pipeline().addAfter("compress", "via-encoder", encoder); - ctx.pipeline().addAfter("decompress", "via-decoder", decoder); - return true; - } - return false; - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - if (PipelineUtil.containsCause(cause, CancelCodecException.class)) return; - super.exceptionCaught(ctx, cause); - } -} diff --git a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/debug/client/MixinDebugHud.java b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/debug/client/MixinDebugHud.java index b23bd3c..e5d600f 100644 --- a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/debug/client/MixinDebugHud.java +++ b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/debug/client/MixinDebugHud.java @@ -1,7 +1,7 @@ package com.viaversion.fabric.mc114.mixin.debug.client; import com.viaversion.fabric.common.handler.CommonTransformer; -import com.viaversion.fabric.mc114.handler.FabricDecodeHandler; +import com.viaversion.fabric.common.handler.FabricDecodeHandler; import com.viaversion.viaversion.api.connection.ProtocolInfo; import io.netty.channel.ChannelHandler; import net.minecraft.client.MinecraftClient; diff --git a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/pipeline/MixinClientConnection.java b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/pipeline/MixinClientConnection.java index ec83fa1..40f6a6b 100644 --- a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/pipeline/MixinClientConnection.java +++ b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/pipeline/MixinClientConnection.java @@ -1,12 +1,15 @@ package com.viaversion.fabric.mc114.mixin.pipeline; +import com.viaversion.fabric.common.handler.PipelineReorderEvent; import io.netty.channel.Channel; import net.minecraft.network.ClientConnection; import org.apache.logging.log4j.Logger; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(ClientConnection.class) @@ -28,4 +31,10 @@ public class MixinClientConnection { logger.debug(message, t); } } + + + @Inject(method = "setCompressionThreshold", at = @At("RETURN")) + private void reorderCompression(int compressionThreshold, CallbackInfo ci) { + channel.pipeline().fireUserEventTriggered(new PipelineReorderEvent()); + } } diff --git a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/pipeline/MixinServerNetworkIoChInit.java b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/pipeline/MixinServerNetworkIoChInit.java index f358ee7..beb551d 100644 --- a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/pipeline/MixinServerNetworkIoChInit.java +++ b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/pipeline/MixinServerNetworkIoChInit.java @@ -1,8 +1,8 @@ package com.viaversion.fabric.mc114.mixin.pipeline; import com.viaversion.fabric.common.handler.CommonTransformer; -import com.viaversion.fabric.mc114.handler.FabricDecodeHandler; -import com.viaversion.fabric.mc114.handler.FabricEncodeHandler; +import com.viaversion.fabric.common.handler.FabricDecodeHandler; +import com.viaversion.fabric.common.handler.FabricEncodeHandler; import com.viaversion.viaversion.connection.UserConnectionImpl; import com.viaversion.viaversion.protocol.ProtocolPipelineImpl; import io.netty.channel.Channel; diff --git a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/pipeline/client/MixinClientConnectionChInit.java b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/pipeline/client/MixinClientConnectionChInit.java index baaef34..9b8aa27 100644 --- a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/pipeline/client/MixinClientConnectionChInit.java +++ b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/pipeline/client/MixinClientConnectionChInit.java @@ -1,9 +1,9 @@ package com.viaversion.fabric.mc114.mixin.pipeline.client; +import com.viaversion.fabric.common.handler.FabricDecodeHandler; +import com.viaversion.fabric.common.handler.FabricEncodeHandler; import com.viaversion.fabric.mc114.ViaFabric; import com.viaversion.fabric.common.handler.CommonTransformer; -import com.viaversion.fabric.mc114.handler.FabricDecodeHandler; -import com.viaversion.fabric.mc114.handler.FabricEncodeHandler; import com.viaversion.fabric.mc114.handler.clientside.ProtocolDetectionHandler; import com.viaversion.fabric.common.protocol.HostnameParserProtocol; import com.viaversion.viaversion.connection.UserConnectionImpl; diff --git a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/platform/FabricPlatform.java b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/platform/FabricPlatform.java index 99c3955..a2cd751 100644 --- a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/platform/FabricPlatform.java +++ b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/platform/FabricPlatform.java @@ -1,65 +1,34 @@ package com.viaversion.fabric.mc114.platform; -import com.viaversion.fabric.common.platform.FabricViaAPI; -import com.viaversion.fabric.common.platform.FabricViaConfig; +import com.viaversion.fabric.common.commands.UserCommandSender; +import com.viaversion.fabric.common.provider.AbstractFabricPlatform; +import com.viaversion.fabric.common.util.FutureTaskId; import com.viaversion.fabric.mc114.ViaFabric; import com.viaversion.fabric.mc114.commands.NMSCommandSender; -import com.viaversion.fabric.common.commands.UserCommandSender; -import com.viaversion.fabric.common.util.FutureTaskId; -import com.viaversion.fabric.common.util.JLoggerToLog4j; -import com.viaversion.viaversion.api.configuration.ViaVersionConfig; +import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.api.command.ViaCommandSender; +import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; +import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import io.netty.util.concurrent.Future; import io.netty.util.concurrent.GenericFutureListener; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.fabricmc.loader.api.FabricLoader; -import net.fabricmc.loader.api.ModContainer; -import net.fabricmc.loader.api.Version; -import net.fabricmc.loader.api.metadata.ModMetadata; import net.minecraft.client.MinecraftClient; import net.minecraft.entity.Entity; import net.minecraft.network.MessageType; import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; -import org.apache.logging.log4j.LogManager; -import com.viaversion.viaversion.api.Via; -import com.viaversion.viaversion.api.ViaAPI; -import com.viaversion.viaversion.api.command.ViaCommandSender; -import com.viaversion.viaversion.api.configuration.ConfigurationProvider; -import com.viaversion.viaversion.api.platform.ViaPlatform; -import com.viaversion.viaversion.dump.PluginInfo; -import com.viaversion.viaversion.util.GsonUtil; -import com.viaversion.viaversion.libs.gson.JsonObject; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import java.io.File; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; import java.util.UUID; import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; import java.util.logging.Level; -import java.util.logging.Logger; -import java.util.stream.Collectors; - -public class FabricPlatform implements ViaPlatform { - private final Logger logger = new JLoggerToLog4j(LogManager.getLogger("ViaVersion")); - private final FabricViaConfig config; - private final File dataFolder; - private final ViaAPI api; - - public FabricPlatform() { - Path configDir = FabricLoader.getInstance().getConfigDirectory().toPath().resolve("ViaFabric"); - config = new FabricViaConfig(configDir.resolve("viaversion.yml").toFile()); - dataFolder = configDir.toFile(); - api = new FabricViaAPI(); - } +public class FabricPlatform extends AbstractFabricPlatform { public static MinecraftServer getServer() { if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { return getIntegratedServer(); @@ -76,27 +45,6 @@ public class FabricPlatform implements ViaPlatform { return GsonComponentSerializer.gson().serialize(LegacyComponentSerializer.legacySection().deserialize(legacy)); } - @Override - public Logger getLogger() { - return logger; - } - - @Override - public String getPlatformName() { - return "ViaFabric"; - } - - @Override - public String getPlatformVersion() { - return ViaFabric.getVersion(); - } - - @Override - public String getPluginVersion() { - return FabricLoader.getInstance().getModContainer("viaversion").map(ModContainer::getMetadata) - .map(ModMetadata::getVersion).map(Version::getFriendlyString).orElse("UNKNOWN"); - } - @Override public FutureTaskId runAsync(Runnable runnable) { return new FutureTaskId(CompletableFuture @@ -215,59 +163,4 @@ public class FabricPlatform implements ViaPlatform { } return false; // Can't know if it worked } - - @Override - public boolean isPluginEnabled() { - return true; - } - - @Override - public ViaAPI getApi() { - return api; - } - - @Override - public ViaVersionConfig getConf() { - return config; - } - - @Override - public ConfigurationProvider getConfigurationProvider() { - return config; - } - - @Override - public File getDataFolder() { - return dataFolder; - } - - @Override - public void onReload() { - // Nothing to do - } - - @Override - public JsonObject getDump() { - JsonObject platformSpecific = new JsonObject(); - List mods = new ArrayList<>(); - for (ModContainer mod : FabricLoader.getInstance().getAllMods()) { - mods.add(new PluginInfo(true, - mod.getMetadata().getId() + " (" + mod.getMetadata().getName() + ")", - mod.getMetadata().getVersion().getFriendlyString(), - null, - mod.getMetadata().getAuthors().stream() - .map(info -> info.getName() - + (info.getContact().asMap().isEmpty() ? "" : " " + info.getContact().asMap())) - .collect(Collectors.toList()) - )); - } - - platformSpecific.add("mods", GsonUtil.getGson().toJsonTree(mods)); - return platformSpecific; - } - - @Override - public boolean isOldClientsAllowed() { - return true; - } } diff --git a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/handler/FabricDecodeHandler.java b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/handler/FabricDecodeHandler.java deleted file mode 100644 index 402d31d..0000000 --- a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/handler/FabricDecodeHandler.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.viaversion.fabric.mc115.handler; - -import com.viaversion.fabric.common.handler.CommonTransformer; -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.MessageToMessageDecoder; -import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.exception.CancelCodecException; -import com.viaversion.viaversion.exception.CancelDecoderException; -import com.viaversion.viaversion.util.PipelineUtil; - -import java.lang.reflect.InvocationTargetException; -import java.util.List; - -@ChannelHandler.Sharable -public class FabricDecodeHandler extends MessageToMessageDecoder { - private final UserConnection info; - private boolean handledCompression; - private boolean skipDoubleTransform; - - public FabricDecodeHandler(UserConnection info) { - this.info = info; - } - - public UserConnection getInfo() { - return info; - } - - // https://github.com/ViaVersion/ViaVersion/blob/master/velocity/src/main/java/us/myles/ViaVersion/velocity/handlers/VelocityDecodeHandler.java - @Override - protected void decode(ChannelHandlerContext ctx, ByteBuf bytebuf, List out) throws Exception { - if (skipDoubleTransform) { - skipDoubleTransform = false; - out.add(bytebuf.retain()); - return; - } - - if (!info.checkIncomingPacket()) throw CancelDecoderException.generate(null); - if (!info.shouldTransformPacket()) { - out.add(bytebuf.retain()); - return; - } - - ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf); - try { - boolean needsCompress = handleCompressionOrder(ctx, transformedBuf); - - info.transformIncoming(transformedBuf, CancelDecoderException::generate); - - if (needsCompress) { - CommonTransformer.compress(ctx, transformedBuf); - skipDoubleTransform = true; - } - out.add(transformedBuf.retain()); - } finally { - transformedBuf.release(); - } - } - - private boolean handleCompressionOrder(ChannelHandlerContext ctx, ByteBuf buf) throws InvocationTargetException { - if (handledCompression) return false; - - int decoderIndex = ctx.pipeline().names().indexOf("decompress"); - if (decoderIndex == -1) return false; - handledCompression = true; - if (decoderIndex > ctx.pipeline().names().indexOf("via-decoder")) { - // Need to decompress this packet due to bad order - CommonTransformer.decompress(ctx, buf); - ChannelHandler encoder = ctx.pipeline().get("via-encoder"); - ChannelHandler decoder = ctx.pipeline().get("via-decoder"); - ctx.pipeline().remove(encoder); - ctx.pipeline().remove(decoder); - ctx.pipeline().addAfter("compress", "via-encoder", encoder); - ctx.pipeline().addAfter("decompress", "via-decoder", decoder); - return true; - } - return false; - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - if (PipelineUtil.containsCause(cause, CancelCodecException.class)) return; - super.exceptionCaught(ctx, cause); - } -} \ No newline at end of file diff --git a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/handler/FabricEncodeHandler.java b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/handler/FabricEncodeHandler.java deleted file mode 100644 index 908aa3c..0000000 --- a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/handler/FabricEncodeHandler.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.viaversion.fabric.mc115.handler; - -import com.viaversion.fabric.common.handler.CommonTransformer; -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.MessageToMessageEncoder; -import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.exception.CancelCodecException; -import com.viaversion.viaversion.exception.CancelEncoderException; -import com.viaversion.viaversion.util.PipelineUtil; - -import java.lang.reflect.InvocationTargetException; -import java.util.List; - -@ChannelHandler.Sharable -public class FabricEncodeHandler extends MessageToMessageEncoder { - private final UserConnection info; - private boolean handledCompression; - - public FabricEncodeHandler(UserConnection info) { - this.info = info; - } - - @Override - protected void encode(final ChannelHandlerContext ctx, ByteBuf bytebuf, List out) throws Exception { - if (!info.checkOutgoingPacket()) throw CancelEncoderException.generate(null); - if (!info.shouldTransformPacket()) { - out.add(bytebuf.retain()); - return; - } - - ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf); - try { - boolean needsCompress = handleCompressionOrder(ctx, transformedBuf); - - info.transformOutgoing(transformedBuf, CancelEncoderException::generate); - - if (needsCompress) { - CommonTransformer.compress(ctx, transformedBuf); - } - out.add(transformedBuf.retain()); - } finally { - transformedBuf.release(); - } - } - - private boolean handleCompressionOrder(ChannelHandlerContext ctx, ByteBuf buf) throws InvocationTargetException { - if (handledCompression) return false; - - int encoderIndex = ctx.pipeline().names().indexOf("compress"); - if (encoderIndex == -1) return false; - handledCompression = true; - if (encoderIndex > ctx.pipeline().names().indexOf("via-encoder")) { - // Need to decompress this packet due to bad order - CommonTransformer.decompress(ctx, buf); - ChannelHandler encoder = ctx.pipeline().get("via-encoder"); - ChannelHandler decoder = ctx.pipeline().get("via-decoder"); - ctx.pipeline().remove(encoder); - ctx.pipeline().remove(decoder); - ctx.pipeline().addAfter("compress", "via-encoder", encoder); - ctx.pipeline().addAfter("decompress", "via-decoder", decoder); - return true; - } - return false; - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - if (PipelineUtil.containsCause(cause, CancelCodecException.class)) return; - super.exceptionCaught(ctx, cause); - } -} diff --git a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/debug/client/MixinDebugHud.java b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/debug/client/MixinDebugHud.java index 65179d6..96a0182 100644 --- a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/debug/client/MixinDebugHud.java +++ b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/debug/client/MixinDebugHud.java @@ -1,7 +1,7 @@ package com.viaversion.fabric.mc115.mixin.debug.client; import com.viaversion.fabric.common.handler.CommonTransformer; -import com.viaversion.fabric.mc115.handler.FabricDecodeHandler; +import com.viaversion.fabric.common.handler.FabricDecodeHandler; import com.viaversion.viaversion.api.connection.ProtocolInfo; import io.netty.channel.ChannelHandler; import net.minecraft.client.MinecraftClient; diff --git a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/pipeline/MixinClientConnection.java b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/pipeline/MixinClientConnection.java index 771c821..288cf8c 100644 --- a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/pipeline/MixinClientConnection.java +++ b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/pipeline/MixinClientConnection.java @@ -1,12 +1,15 @@ package com.viaversion.fabric.mc115.mixin.pipeline; +import com.viaversion.fabric.common.handler.PipelineReorderEvent; import io.netty.channel.Channel; import net.minecraft.network.ClientConnection; import org.apache.logging.log4j.Logger; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(ClientConnection.class) @@ -28,4 +31,9 @@ public class MixinClientConnection { logger.debug(message, t); } } + + @Inject(method = "setCompressionThreshold", at = @At("RETURN")) + private void reorderCompression(int compressionThreshold, CallbackInfo ci) { + channel.pipeline().fireUserEventTriggered(new PipelineReorderEvent()); + } } diff --git a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/pipeline/MixinServerNetworkIoChInit.java b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/pipeline/MixinServerNetworkIoChInit.java index 84bd72b..bdcc66f 100644 --- a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/pipeline/MixinServerNetworkIoChInit.java +++ b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/pipeline/MixinServerNetworkIoChInit.java @@ -1,8 +1,8 @@ package com.viaversion.fabric.mc115.mixin.pipeline; import com.viaversion.fabric.common.handler.CommonTransformer; -import com.viaversion.fabric.mc115.handler.FabricDecodeHandler; -import com.viaversion.fabric.mc115.handler.FabricEncodeHandler; +import com.viaversion.fabric.common.handler.FabricDecodeHandler; +import com.viaversion.fabric.common.handler.FabricEncodeHandler; import com.viaversion.viaversion.connection.UserConnectionImpl; import com.viaversion.viaversion.protocol.ProtocolPipelineImpl; import io.netty.channel.Channel; diff --git a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/pipeline/client/MixinClientConnectionChInit.java b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/pipeline/client/MixinClientConnectionChInit.java index a0545cd..58356df 100644 --- a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/pipeline/client/MixinClientConnectionChInit.java +++ b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/pipeline/client/MixinClientConnectionChInit.java @@ -1,9 +1,9 @@ package com.viaversion.fabric.mc115.mixin.pipeline.client; +import com.viaversion.fabric.common.handler.FabricDecodeHandler; +import com.viaversion.fabric.common.handler.FabricEncodeHandler; import com.viaversion.fabric.mc115.ViaFabric; import com.viaversion.fabric.common.handler.CommonTransformer; -import com.viaversion.fabric.mc115.handler.FabricDecodeHandler; -import com.viaversion.fabric.mc115.handler.FabricEncodeHandler; import com.viaversion.fabric.mc115.handler.clientside.ProtocolDetectionHandler; import com.viaversion.fabric.common.protocol.HostnameParserProtocol; import com.viaversion.viaversion.connection.UserConnectionImpl; diff --git a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/platform/FabricPlatform.java b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/platform/FabricPlatform.java index 016bf7e..5895b1a 100644 --- a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/platform/FabricPlatform.java +++ b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/platform/FabricPlatform.java @@ -3,6 +3,7 @@ package com.viaversion.fabric.mc115.platform; import com.viaversion.fabric.common.commands.UserCommandSender; import com.viaversion.fabric.common.platform.FabricViaAPI; import com.viaversion.fabric.common.platform.FabricViaConfig; +import com.viaversion.fabric.common.provider.AbstractFabricPlatform; import com.viaversion.fabric.common.util.FutureTaskId; import com.viaversion.fabric.common.util.JLoggerToLog4j; import com.viaversion.fabric.mc115.ViaFabric; @@ -47,19 +48,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; -public class FabricPlatform implements ViaPlatform { - private final Logger logger = new JLoggerToLog4j(LogManager.getLogger("ViaVersion")); - private final FabricViaConfig config; - private final File dataFolder; - private final ViaAPI api; - - public FabricPlatform() { - Path configDir = FabricLoader.getInstance().getConfigDirectory().toPath().resolve("ViaFabric"); - config = new FabricViaConfig(configDir.resolve("viaversion.yml").toFile()); - dataFolder = configDir.toFile(); - api = new FabricViaAPI(); - } - +public class FabricPlatform extends AbstractFabricPlatform { public static MinecraftServer getServer() { if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { return getIntegratedServer(); @@ -76,27 +65,6 @@ public class FabricPlatform implements ViaPlatform { return GsonComponentSerializer.gson().serialize(LegacyComponentSerializer.legacySection().deserialize(legacy)); } - @Override - public Logger getLogger() { - return logger; - } - - @Override - public String getPlatformName() { - return "ViaFabric"; - } - - @Override - public String getPlatformVersion() { - return ViaFabric.getVersion(); - } - - @Override - public String getPluginVersion() { - return FabricLoader.getInstance().getModContainer("viaversion").map(ModContainer::getMetadata) - .map(ModMetadata::getVersion).map(Version::getFriendlyString).orElse("UNKNOWN"); - } - @Override public FutureTaskId runAsync(Runnable runnable) { return new FutureTaskId(CompletableFuture @@ -215,59 +183,4 @@ public class FabricPlatform implements ViaPlatform { } return false; // Can't know if it worked } - - @Override - public boolean isPluginEnabled() { - return true; - } - - @Override - public ViaAPI getApi() { - return api; - } - - @Override - public ViaVersionConfig getConf() { - return config; - } - - @Override - public ConfigurationProvider getConfigurationProvider() { - return config; - } - - @Override - public File getDataFolder() { - return dataFolder; - } - - @Override - public void onReload() { - // Nothing to do - } - - @Override - public JsonObject getDump() { - JsonObject platformSpecific = new JsonObject(); - List mods = new ArrayList<>(); - for (ModContainer mod : FabricLoader.getInstance().getAllMods()) { - mods.add(new PluginInfo(true, - mod.getMetadata().getId() + " (" + mod.getMetadata().getName() + ")", - mod.getMetadata().getVersion().getFriendlyString(), - null, - mod.getMetadata().getAuthors().stream() - .map(info -> info.getName() - + (info.getContact().asMap().isEmpty() ? "" : " " + info.getContact().asMap())) - .collect(Collectors.toList()) - )); - } - - platformSpecific.add("mods", GsonUtil.getGson().toJsonTree(mods)); - return platformSpecific; - } - - @Override - public boolean isOldClientsAllowed() { - return true; - } } diff --git a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/handler/FabricDecodeHandler.java b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/handler/FabricDecodeHandler.java deleted file mode 100644 index bd25e1f..0000000 --- a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/handler/FabricDecodeHandler.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.viaversion.fabric.mc116.handler; - -import com.viaversion.fabric.common.handler.CommonTransformer; -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.MessageToMessageDecoder; -import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.exception.CancelCodecException; -import com.viaversion.viaversion.exception.CancelDecoderException; -import com.viaversion.viaversion.util.PipelineUtil; - -import java.lang.reflect.InvocationTargetException; -import java.util.List; - -@ChannelHandler.Sharable -public class FabricDecodeHandler extends MessageToMessageDecoder { - private final UserConnection info; - private boolean handledCompression; - private boolean skipDoubleTransform; - - public FabricDecodeHandler(UserConnection info) { - this.info = info; - } - - public UserConnection getInfo() { - return info; - } - - // https://github.com/ViaVersion/ViaVersion/blob/master/velocity/src/main/java/us/myles/ViaVersion/velocity/handlers/VelocityDecodeHandler.java - @Override - protected void decode(ChannelHandlerContext ctx, ByteBuf bytebuf, List out) throws Exception { - if (skipDoubleTransform) { - skipDoubleTransform = false; - out.add(bytebuf.retain()); - return; - } - - if (!info.checkIncomingPacket()) throw CancelDecoderException.generate(null); - if (!info.shouldTransformPacket()) { - out.add(bytebuf.retain()); - return; - } - - ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf); - try { - boolean needsCompress = handleCompressionOrder(ctx, transformedBuf); - - info.transformIncoming(transformedBuf, CancelDecoderException::generate); - - if (needsCompress) { - CommonTransformer.compress(ctx, transformedBuf); - skipDoubleTransform = true; - } - out.add(transformedBuf.retain()); - } finally { - transformedBuf.release(); - } - } - - private boolean handleCompressionOrder(ChannelHandlerContext ctx, ByteBuf buf) throws InvocationTargetException { - if (handledCompression) return false; - - int decoderIndex = ctx.pipeline().names().indexOf("decompress"); - if (decoderIndex == -1) return false; - handledCompression = true; - if (decoderIndex > ctx.pipeline().names().indexOf("via-decoder")) { - // Need to decompress this packet due to bad order - CommonTransformer.decompress(ctx, buf); - ChannelHandler encoder = ctx.pipeline().get("via-encoder"); - ChannelHandler decoder = ctx.pipeline().get("via-decoder"); - ctx.pipeline().remove(encoder); - ctx.pipeline().remove(decoder); - ctx.pipeline().addAfter("compress", "via-encoder", encoder); - ctx.pipeline().addAfter("decompress", "via-decoder", decoder); - return true; - } - return false; - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - if (PipelineUtil.containsCause(cause, CancelCodecException.class)) return; - super.exceptionCaught(ctx, cause); - } -} \ No newline at end of file diff --git a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/handler/FabricEncodeHandler.java b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/handler/FabricEncodeHandler.java deleted file mode 100644 index 5691e01..0000000 --- a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/handler/FabricEncodeHandler.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.viaversion.fabric.mc116.handler; - -import com.viaversion.fabric.common.handler.CommonTransformer; -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.MessageToMessageEncoder; -import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.exception.CancelCodecException; -import com.viaversion.viaversion.exception.CancelEncoderException; -import com.viaversion.viaversion.util.PipelineUtil; - -import java.lang.reflect.InvocationTargetException; -import java.util.List; - -@ChannelHandler.Sharable -public class FabricEncodeHandler extends MessageToMessageEncoder { - private final UserConnection info; - private boolean handledCompression; - - public FabricEncodeHandler(UserConnection info) { - this.info = info; - } - - @Override - protected void encode(final ChannelHandlerContext ctx, ByteBuf bytebuf, List out) throws Exception { - if (!info.checkOutgoingPacket()) throw CancelEncoderException.generate(null); - if (!info.shouldTransformPacket()) { - out.add(bytebuf.retain()); - return; - } - - ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf); - try { - boolean needsCompress = handleCompressionOrder(ctx, transformedBuf); - - info.transformOutgoing(transformedBuf, CancelEncoderException::generate); - - if (needsCompress) { - CommonTransformer.compress(ctx, transformedBuf); - } - out.add(transformedBuf.retain()); - } finally { - transformedBuf.release(); - } - } - - private boolean handleCompressionOrder(ChannelHandlerContext ctx, ByteBuf buf) throws InvocationTargetException { - if (handledCompression) return false; - - int encoderIndex = ctx.pipeline().names().indexOf("compress"); - if (encoderIndex == -1) return false; - handledCompression = true; - if (encoderIndex > ctx.pipeline().names().indexOf("via-encoder")) { - // Need to decompress this packet due to bad order - CommonTransformer.decompress(ctx, buf); - ChannelHandler encoder = ctx.pipeline().get("via-encoder"); - ChannelHandler decoder = ctx.pipeline().get("via-decoder"); - ctx.pipeline().remove(encoder); - ctx.pipeline().remove(decoder); - ctx.pipeline().addAfter("compress", "via-encoder", encoder); - ctx.pipeline().addAfter("decompress", "via-decoder", decoder); - return true; - } - return false; - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - if (PipelineUtil.containsCause(cause, CancelCodecException.class)) return; - super.exceptionCaught(ctx, cause); - } -} diff --git a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/debug/client/MixinDebugHud.java b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/debug/client/MixinDebugHud.java index 509df6c..e9c5ee8 100644 --- a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/debug/client/MixinDebugHud.java +++ b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/debug/client/MixinDebugHud.java @@ -1,7 +1,7 @@ package com.viaversion.fabric.mc116.mixin.debug.client; import com.viaversion.fabric.common.handler.CommonTransformer; -import com.viaversion.fabric.mc116.handler.FabricDecodeHandler; +import com.viaversion.fabric.common.handler.FabricDecodeHandler; import com.viaversion.viaversion.api.connection.ProtocolInfo; import io.netty.channel.ChannelHandler; import net.minecraft.client.MinecraftClient; diff --git a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/pipeline/MixinClientConnection.java b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/pipeline/MixinClientConnection.java index 7064550..c2c16f0 100644 --- a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/pipeline/MixinClientConnection.java +++ b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/pipeline/MixinClientConnection.java @@ -1,12 +1,15 @@ package com.viaversion.fabric.mc116.mixin.pipeline; +import com.viaversion.fabric.common.handler.PipelineReorderEvent; import io.netty.channel.Channel; import net.minecraft.network.ClientConnection; import org.apache.logging.log4j.Logger; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(ClientConnection.class) @@ -28,4 +31,9 @@ public class MixinClientConnection { logger.debug(message, t); } } + + @Inject(method = "setCompressionThreshold", at = @At("RETURN")) + private void reorderCompression(int compressionThreshold, CallbackInfo ci) { + channel.pipeline().fireUserEventTriggered(new PipelineReorderEvent()); + } } diff --git a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/pipeline/MixinServerNetworkIoChInit.java b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/pipeline/MixinServerNetworkIoChInit.java index 3cef35b..7ab4db1 100644 --- a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/pipeline/MixinServerNetworkIoChInit.java +++ b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/pipeline/MixinServerNetworkIoChInit.java @@ -1,8 +1,8 @@ package com.viaversion.fabric.mc116.mixin.pipeline; import com.viaversion.fabric.common.handler.CommonTransformer; -import com.viaversion.fabric.mc116.handler.FabricDecodeHandler; -import com.viaversion.fabric.mc116.handler.FabricEncodeHandler; +import com.viaversion.fabric.common.handler.FabricDecodeHandler; +import com.viaversion.fabric.common.handler.FabricEncodeHandler; import com.viaversion.viaversion.connection.UserConnectionImpl; import com.viaversion.viaversion.protocol.ProtocolPipelineImpl; import io.netty.channel.Channel; diff --git a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/pipeline/client/MixinClientConnectionChInit.java b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/pipeline/client/MixinClientConnectionChInit.java index 2f3cdf5..1c019e3 100644 --- a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/pipeline/client/MixinClientConnectionChInit.java +++ b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/pipeline/client/MixinClientConnectionChInit.java @@ -1,9 +1,9 @@ package com.viaversion.fabric.mc116.mixin.pipeline.client; +import com.viaversion.fabric.common.handler.FabricDecodeHandler; +import com.viaversion.fabric.common.handler.FabricEncodeHandler; import com.viaversion.fabric.mc116.ViaFabric; import com.viaversion.fabric.common.handler.CommonTransformer; -import com.viaversion.fabric.mc116.handler.FabricDecodeHandler; -import com.viaversion.fabric.mc116.handler.FabricEncodeHandler; import com.viaversion.fabric.mc116.handler.clientside.ProtocolDetectionHandler; import com.viaversion.fabric.common.protocol.HostnameParserProtocol; import com.viaversion.viaversion.connection.UserConnectionImpl; diff --git a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/platform/FabricPlatform.java b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/platform/FabricPlatform.java index 13b6e3d..d971e9f 100644 --- a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/platform/FabricPlatform.java +++ b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/platform/FabricPlatform.java @@ -1,64 +1,33 @@ package com.viaversion.fabric.mc116.platform; -import com.viaversion.fabric.common.platform.FabricViaAPI; -import com.viaversion.fabric.common.platform.FabricViaConfig; +import com.viaversion.fabric.common.commands.UserCommandSender; +import com.viaversion.fabric.common.provider.AbstractFabricPlatform; import com.viaversion.fabric.common.util.FutureTaskId; import com.viaversion.fabric.mc116.ViaFabric; import com.viaversion.fabric.mc116.commands.NMSCommandSender; -import com.viaversion.fabric.common.commands.UserCommandSender; -import com.viaversion.fabric.common.util.JLoggerToLog4j; -import com.viaversion.viaversion.api.configuration.ViaVersionConfig; +import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.api.command.ViaCommandSender; +import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; +import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import io.netty.util.concurrent.Future; import io.netty.util.concurrent.GenericFutureListener; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.fabricmc.loader.api.FabricLoader; -import net.fabricmc.loader.api.ModContainer; -import net.fabricmc.loader.api.Version; -import net.fabricmc.loader.api.metadata.ModMetadata; import net.minecraft.client.MinecraftClient; import net.minecraft.entity.Entity; import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; -import org.apache.logging.log4j.LogManager; -import com.viaversion.viaversion.api.Via; -import com.viaversion.viaversion.api.ViaAPI; -import com.viaversion.viaversion.api.command.ViaCommandSender; -import com.viaversion.viaversion.api.configuration.ConfigurationProvider; -import com.viaversion.viaversion.api.platform.ViaPlatform; -import com.viaversion.viaversion.dump.PluginInfo; -import com.viaversion.viaversion.util.GsonUtil; -import com.viaversion.viaversion.libs.gson.JsonObject; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import java.io.File; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; import java.util.UUID; import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; import java.util.logging.Level; -import java.util.logging.Logger; -import java.util.stream.Collectors; - -public class FabricPlatform implements ViaPlatform { - private final Logger logger = new JLoggerToLog4j(LogManager.getLogger("ViaVersion")); - private final FabricViaConfig config; - private final File dataFolder; - private final ViaAPI api; - - public FabricPlatform() { - Path configDir = FabricLoader.getInstance().getConfigDirectory().toPath().resolve("ViaFabric"); - config = new FabricViaConfig(configDir.resolve("viaversion.yml").toFile()); - dataFolder = configDir.toFile(); - api = new FabricViaAPI(); - } +public class FabricPlatform extends AbstractFabricPlatform { public static MinecraftServer getServer() { if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { return getIntegratedServer(); @@ -75,27 +44,6 @@ public class FabricPlatform implements ViaPlatform { return GsonComponentSerializer.gson().serialize(LegacyComponentSerializer.legacySection().deserialize(legacy)); } - @Override - public Logger getLogger() { - return logger; - } - - @Override - public String getPlatformName() { - return "ViaFabric"; - } - - @Override - public String getPlatformVersion() { - return ViaFabric.getVersion(); - } - - @Override - public String getPluginVersion() { - return FabricLoader.getInstance().getModContainer("viaversion").map(ModContainer::getMetadata) - .map(ModMetadata::getVersion).map(Version::getFriendlyString).orElse("UNKNOWN"); - } - @Override public FutureTaskId runAsync(Runnable runnable) { return new FutureTaskId(CompletableFuture @@ -216,59 +164,4 @@ public class FabricPlatform implements ViaPlatform { } return false; // Can't know if it worked } - - @Override - public boolean isPluginEnabled() { - return true; - } - - @Override - public ViaAPI getApi() { - return api; - } - - @Override - public ViaVersionConfig getConf() { - return config; - } - - @Override - public ConfigurationProvider getConfigurationProvider() { - return config; - } - - @Override - public File getDataFolder() { - return dataFolder; - } - - @Override - public void onReload() { - // Nothing to do - } - - @Override - public JsonObject getDump() { - JsonObject platformSpecific = new JsonObject(); - List mods = new ArrayList<>(); - for (ModContainer mod : FabricLoader.getInstance().getAllMods()) { - mods.add(new PluginInfo(true, - mod.getMetadata().getId() + " (" + mod.getMetadata().getName() + ")", - mod.getMetadata().getVersion().getFriendlyString(), - null, - mod.getMetadata().getAuthors().stream() - .map(info -> info.getName() - + (info.getContact().asMap().isEmpty() ? "" : " " + info.getContact().asMap())) - .collect(Collectors.toList()) - )); - } - - platformSpecific.add("mods", GsonUtil.getGson().toJsonTree(mods)); - return platformSpecific; - } - - @Override - public boolean isOldClientsAllowed() { - return true; - } } diff --git a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/handler/FabricDecodeHandler.java b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/handler/FabricDecodeHandler.java deleted file mode 100644 index a51cf3d..0000000 --- a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/handler/FabricDecodeHandler.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.viaversion.fabric.mc117.handler; - -import com.viaversion.fabric.common.handler.CommonTransformer; -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.MessageToMessageDecoder; -import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.exception.CancelCodecException; -import com.viaversion.viaversion.exception.CancelDecoderException; -import com.viaversion.viaversion.util.PipelineUtil; - -import java.lang.reflect.InvocationTargetException; -import java.util.List; - -@ChannelHandler.Sharable -public class FabricDecodeHandler extends MessageToMessageDecoder { - private final UserConnection info; - private boolean handledCompression; - private boolean skipDoubleTransform; - - public FabricDecodeHandler(UserConnection info) { - this.info = info; - } - - public UserConnection getInfo() { - return info; - } - - // https://github.com/ViaVersion/ViaVersion/blob/master/velocity/src/main/java/us/myles/ViaVersion/velocity/handlers/VelocityDecodeHandler.java - @Override - protected void decode(ChannelHandlerContext ctx, ByteBuf bytebuf, List out) throws Exception { - if (skipDoubleTransform) { - skipDoubleTransform = false; - out.add(bytebuf.retain()); - return; - } - - if (!info.checkIncomingPacket()) throw CancelDecoderException.generate(null); - if (!info.shouldTransformPacket()) { - out.add(bytebuf.retain()); - return; - } - - ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf); - try { - boolean needsCompress = handleCompressionOrder(ctx, transformedBuf); - - info.transformIncoming(transformedBuf, CancelDecoderException::generate); - - if (needsCompress) { - CommonTransformer.compress(ctx, transformedBuf); - skipDoubleTransform = true; - } - out.add(transformedBuf.retain()); - } finally { - transformedBuf.release(); - } - } - - private boolean handleCompressionOrder(ChannelHandlerContext ctx, ByteBuf buf) throws InvocationTargetException { - if (handledCompression) return false; - - int decoderIndex = ctx.pipeline().names().indexOf("decompress"); - if (decoderIndex == -1) return false; - handledCompression = true; - if (decoderIndex > ctx.pipeline().names().indexOf("via-decoder")) { - // Need to decompress this packet due to bad order - CommonTransformer.decompress(ctx, buf); - ChannelHandler encoder = ctx.pipeline().get("via-encoder"); - ChannelHandler decoder = ctx.pipeline().get("via-decoder"); - ctx.pipeline().remove(encoder); - ctx.pipeline().remove(decoder); - ctx.pipeline().addAfter("compress", "via-encoder", encoder); - ctx.pipeline().addAfter("decompress", "via-decoder", decoder); - return true; - } - return false; - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - if (PipelineUtil.containsCause(cause, CancelCodecException.class)) return; - super.exceptionCaught(ctx, cause); - } -} \ No newline at end of file diff --git a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/handler/FabricEncodeHandler.java b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/handler/FabricEncodeHandler.java deleted file mode 100644 index e95d6ff..0000000 --- a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/handler/FabricEncodeHandler.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.viaversion.fabric.mc117.handler; - -import com.viaversion.fabric.common.handler.CommonTransformer; -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.MessageToMessageEncoder; -import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.exception.CancelCodecException; -import com.viaversion.viaversion.exception.CancelEncoderException; -import com.viaversion.viaversion.util.PipelineUtil; - -import java.lang.reflect.InvocationTargetException; -import java.util.List; - -@ChannelHandler.Sharable -public class FabricEncodeHandler extends MessageToMessageEncoder { - private final UserConnection info; - private boolean handledCompression; - - public FabricEncodeHandler(UserConnection info) { - this.info = info; - } - - @Override - protected void encode(final ChannelHandlerContext ctx, ByteBuf bytebuf, List out) throws Exception { - if (!info.checkOutgoingPacket()) throw CancelEncoderException.generate(null); - if (!info.shouldTransformPacket()) { - out.add(bytebuf.retain()); - return; - } - - ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf); - try { - boolean needsCompress = handleCompressionOrder(ctx, transformedBuf); - - info.transformOutgoing(transformedBuf, CancelEncoderException::generate); - - if (needsCompress) { - CommonTransformer.compress(ctx, transformedBuf); - } - out.add(transformedBuf.retain()); - } finally { - transformedBuf.release(); - } - } - - private boolean handleCompressionOrder(ChannelHandlerContext ctx, ByteBuf buf) throws InvocationTargetException { - if (handledCompression) return false; - - int encoderIndex = ctx.pipeline().names().indexOf("compress"); - if (encoderIndex == -1) return false; - handledCompression = true; - if (encoderIndex > ctx.pipeline().names().indexOf("via-encoder")) { - // Need to decompress this packet due to bad order - CommonTransformer.decompress(ctx, buf); - ChannelHandler encoder = ctx.pipeline().get("via-encoder"); - ChannelHandler decoder = ctx.pipeline().get("via-decoder"); - ctx.pipeline().remove(encoder); - ctx.pipeline().remove(decoder); - ctx.pipeline().addAfter("compress", "via-encoder", encoder); - ctx.pipeline().addAfter("decompress", "via-decoder", decoder); - return true; - } - return false; - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - if (PipelineUtil.containsCause(cause, CancelCodecException.class)) return; - super.exceptionCaught(ctx, cause); - } -} diff --git a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/debug/client/MixinDebugHud.java b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/debug/client/MixinDebugHud.java index 40896e0..ae0ac3e 100644 --- a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/debug/client/MixinDebugHud.java +++ b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/debug/client/MixinDebugHud.java @@ -1,7 +1,7 @@ package com.viaversion.fabric.mc117.mixin.debug.client; import com.viaversion.fabric.common.handler.CommonTransformer; -import com.viaversion.fabric.mc117.handler.FabricDecodeHandler; +import com.viaversion.fabric.common.handler.FabricDecodeHandler; import com.viaversion.viaversion.api.connection.ProtocolInfo; import io.netty.channel.ChannelHandler; import net.minecraft.client.MinecraftClient; diff --git a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/pipeline/MixinClientConnection.java b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/pipeline/MixinClientConnection.java index 934ed5f..f119d7f 100644 --- a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/pipeline/MixinClientConnection.java +++ b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/pipeline/MixinClientConnection.java @@ -1,12 +1,15 @@ package com.viaversion.fabric.mc117.mixin.pipeline; +import com.viaversion.fabric.common.handler.PipelineReorderEvent; import io.netty.channel.Channel; import net.minecraft.network.ClientConnection; import org.apache.logging.log4j.Logger; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(ClientConnection.class) @@ -28,4 +31,9 @@ public class MixinClientConnection { logger.debug(message, t); } } + + @Inject(method = "setCompressionThreshold", at = @At("RETURN")) + private void reorderCompression(int compressionThreshold, CallbackInfo ci) { + channel.pipeline().fireUserEventTriggered(new PipelineReorderEvent()); + } } diff --git a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/pipeline/MixinServerNetworkIoChInit.java b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/pipeline/MixinServerNetworkIoChInit.java index f436855..4f4d698 100644 --- a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/pipeline/MixinServerNetworkIoChInit.java +++ b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/pipeline/MixinServerNetworkIoChInit.java @@ -1,8 +1,8 @@ package com.viaversion.fabric.mc117.mixin.pipeline; import com.viaversion.fabric.common.handler.CommonTransformer; -import com.viaversion.fabric.mc117.handler.FabricDecodeHandler; -import com.viaversion.fabric.mc117.handler.FabricEncodeHandler; +import com.viaversion.fabric.common.handler.FabricDecodeHandler; +import com.viaversion.fabric.common.handler.FabricEncodeHandler; import com.viaversion.viaversion.connection.UserConnectionImpl; import com.viaversion.viaversion.protocol.ProtocolPipelineImpl; import io.netty.channel.Channel; diff --git a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/pipeline/client/MixinClientConnectionChInit.java b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/pipeline/client/MixinClientConnectionChInit.java index 79c1ff3..715b9d3 100644 --- a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/pipeline/client/MixinClientConnectionChInit.java +++ b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/pipeline/client/MixinClientConnectionChInit.java @@ -1,9 +1,9 @@ package com.viaversion.fabric.mc117.mixin.pipeline.client; +import com.viaversion.fabric.common.handler.FabricDecodeHandler; +import com.viaversion.fabric.common.handler.FabricEncodeHandler; import com.viaversion.fabric.mc117.ViaFabric; import com.viaversion.fabric.common.handler.CommonTransformer; -import com.viaversion.fabric.mc117.handler.FabricDecodeHandler; -import com.viaversion.fabric.mc117.handler.FabricEncodeHandler; import com.viaversion.fabric.mc117.handler.clientside.ProtocolDetectionHandler; import com.viaversion.fabric.common.protocol.HostnameParserProtocol; import com.viaversion.viaversion.connection.UserConnectionImpl; diff --git a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/platform/FabricPlatform.java b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/platform/FabricPlatform.java index 6087dcb..62e3e06 100644 --- a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/platform/FabricPlatform.java +++ b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/platform/FabricPlatform.java @@ -1,64 +1,33 @@ package com.viaversion.fabric.mc117.platform; -import com.viaversion.fabric.common.platform.FabricViaAPI; -import com.viaversion.fabric.common.platform.FabricViaConfig; +import com.viaversion.fabric.common.commands.UserCommandSender; +import com.viaversion.fabric.common.provider.AbstractFabricPlatform; import com.viaversion.fabric.common.util.FutureTaskId; import com.viaversion.fabric.mc117.ViaFabric; import com.viaversion.fabric.mc117.commands.NMSCommandSender; -import com.viaversion.fabric.common.commands.UserCommandSender; -import com.viaversion.fabric.common.util.JLoggerToLog4j; -import com.viaversion.viaversion.api.configuration.ViaVersionConfig; +import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.api.command.ViaCommandSender; +import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; +import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import io.netty.util.concurrent.Future; import io.netty.util.concurrent.GenericFutureListener; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.fabricmc.loader.api.FabricLoader; -import net.fabricmc.loader.api.ModContainer; -import net.fabricmc.loader.api.Version; -import net.fabricmc.loader.api.metadata.ModMetadata; import net.minecraft.client.MinecraftClient; import net.minecraft.entity.Entity; import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; -import org.apache.logging.log4j.LogManager; -import com.viaversion.viaversion.api.Via; -import com.viaversion.viaversion.api.ViaAPI; -import com.viaversion.viaversion.api.command.ViaCommandSender; -import com.viaversion.viaversion.api.configuration.ConfigurationProvider; -import com.viaversion.viaversion.api.platform.ViaPlatform; -import com.viaversion.viaversion.dump.PluginInfo; -import com.viaversion.viaversion.util.GsonUtil; -import com.viaversion.viaversion.libs.gson.JsonObject; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import java.io.File; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; import java.util.UUID; import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; import java.util.logging.Level; -import java.util.logging.Logger; -import java.util.stream.Collectors; - -public class FabricPlatform implements ViaPlatform { - private final Logger logger = new JLoggerToLog4j(LogManager.getLogger("ViaVersion")); - private final FabricViaConfig config; - private final File dataFolder; - private final ViaAPI api; - - public FabricPlatform() { - Path configDir = FabricLoader.getInstance().getConfigDirectory().toPath().resolve("ViaFabric"); - config = new FabricViaConfig(configDir.resolve("viaversion.yml").toFile()); - dataFolder = configDir.toFile(); - api = new FabricViaAPI(); - } +public class FabricPlatform extends AbstractFabricPlatform { public static MinecraftServer getServer() { if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { return getIntegratedServer(); @@ -75,27 +44,6 @@ public class FabricPlatform implements ViaPlatform { return GsonComponentSerializer.gson().serialize(LegacyComponentSerializer.legacySection().deserialize(legacy)); } - @Override - public Logger getLogger() { - return logger; - } - - @Override - public String getPlatformName() { - return "ViaFabric"; - } - - @Override - public String getPlatformVersion() { - return ViaFabric.getVersion(); - } - - @Override - public String getPluginVersion() { - return FabricLoader.getInstance().getModContainer("viaversion").map(ModContainer::getMetadata) - .map(ModMetadata::getVersion).map(Version::getFriendlyString).orElse("UNKNOWN"); - } - @Override public FutureTaskId runAsync(Runnable runnable) { return new FutureTaskId(CompletableFuture @@ -216,59 +164,4 @@ public class FabricPlatform implements ViaPlatform { } return false; // Can't know if it worked } - - @Override - public boolean isPluginEnabled() { - return true; - } - - @Override - public ViaAPI getApi() { - return api; - } - - @Override - public ViaVersionConfig getConf() { - return config; - } - - @Override - public ConfigurationProvider getConfigurationProvider() { - return config; - } - - @Override - public File getDataFolder() { - return dataFolder; - } - - @Override - public void onReload() { - // Nothing to do - } - - @Override - public JsonObject getDump() { - JsonObject platformSpecific = new JsonObject(); - List mods = new ArrayList<>(); - for (ModContainer mod : FabricLoader.getInstance().getAllMods()) { - mods.add(new PluginInfo(true, - mod.getMetadata().getId() + " (" + mod.getMetadata().getName() + ")", - mod.getMetadata().getVersion().getFriendlyString(), - null, - mod.getMetadata().getAuthors().stream() - .map(info -> info.getName() - + (info.getContact().asMap().isEmpty() ? "" : " " + info.getContact().asMap())) - .collect(Collectors.toList()) - )); - } - - platformSpecific.add("mods", GsonUtil.getGson().toJsonTree(mods)); - return platformSpecific; - } - - @Override - public boolean isOldClientsAllowed() { - return true; - } } diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/debug/client/MixinDebugHud.java b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/debug/client/MixinDebugHud.java index 3789793..0a2609e 100644 --- a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/debug/client/MixinDebugHud.java +++ b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/debug/client/MixinDebugHud.java @@ -1,7 +1,7 @@ package com.viaversion.fabric.mc18.mixin.debug.client; import com.viaversion.fabric.common.handler.CommonTransformer; -import com.viaversion.fabric.mc18.handler.FabricDecodeHandler; +import com.viaversion.fabric.common.handler.FabricDecodeHandler; import com.viaversion.viaversion.api.connection.ProtocolInfo; import io.netty.channel.ChannelHandler; import net.minecraft.client.MinecraftClient; diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/pipeline/MixinClientConnection.java b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/pipeline/MixinClientConnection.java index 1028034..7a90c6e 100644 --- a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/pipeline/MixinClientConnection.java +++ b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/pipeline/MixinClientConnection.java @@ -1,15 +1,24 @@ package com.viaversion.fabric.mc18.mixin.pipeline; +import com.viaversion.fabric.common.handler.PipelineReorderEvent; import io.netty.channel.Channel; import net.minecraft.network.ClientConnection; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(ClientConnection.class) public class MixinClientConnection { @Shadow private Channel channel; + @Inject(method = "setCompressionThreshold", at = @At("RETURN")) + private void reorderCompression(int compressionThreshold, CallbackInfo ci) { + channel.pipeline().fireUserEventTriggered(new PipelineReorderEvent()); + } + /* @Redirect( method = "exceptionCaught", diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/pipeline/MixinServerNetworkIoChInit.java b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/pipeline/MixinServerNetworkIoChInit.java index 0a58a19..25ab1fd 100644 --- a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/pipeline/MixinServerNetworkIoChInit.java +++ b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/pipeline/MixinServerNetworkIoChInit.java @@ -1,8 +1,8 @@ package com.viaversion.fabric.mc18.mixin.pipeline; import com.viaversion.fabric.common.handler.CommonTransformer; -import com.viaversion.fabric.mc18.handler.FabricDecodeHandler; -import com.viaversion.fabric.mc18.handler.FabricEncodeHandler; +import com.viaversion.fabric.common.handler.FabricDecodeHandler; +import com.viaversion.fabric.common.handler.FabricEncodeHandler; import com.viaversion.viaversion.connection.UserConnectionImpl; import com.viaversion.viaversion.protocol.ProtocolPipelineImpl; import io.netty.channel.Channel; diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/pipeline/client/MixinClientConnectionChInit.java b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/pipeline/client/MixinClientConnectionChInit.java index 674670e..f6dd177 100644 --- a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/pipeline/client/MixinClientConnectionChInit.java +++ b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/pipeline/client/MixinClientConnectionChInit.java @@ -1,9 +1,9 @@ package com.viaversion.fabric.mc18.mixin.pipeline.client; +import com.viaversion.fabric.common.handler.FabricDecodeHandler; +import com.viaversion.fabric.common.handler.FabricEncodeHandler; import com.viaversion.fabric.mc18.ViaFabric; import com.viaversion.fabric.common.handler.CommonTransformer; -import com.viaversion.fabric.mc18.handler.FabricDecodeHandler; -import com.viaversion.fabric.mc18.handler.FabricEncodeHandler; import com.viaversion.fabric.mc18.handler.clientside.ProtocolDetectionHandler; import com.viaversion.fabric.common.protocol.HostnameParserProtocol; import com.viaversion.viaversion.connection.UserConnectionImpl; diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/platform/FabricPlatform.java b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/platform/FabricPlatform.java index 0fa1fd4..11b7e60 100644 --- a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/platform/FabricPlatform.java +++ b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/platform/FabricPlatform.java @@ -1,40 +1,23 @@ package com.viaversion.fabric.mc18.platform; -import com.viaversion.fabric.common.platform.FabricViaAPI; -import com.viaversion.fabric.common.platform.FabricViaConfig; +import com.viaversion.fabric.common.commands.UserCommandSender; +import com.viaversion.fabric.common.provider.AbstractFabricPlatform; import com.viaversion.fabric.common.util.FutureTaskId; import com.viaversion.fabric.mc18.ViaFabric; import com.viaversion.fabric.mc18.commands.NMSCommandSender; -import com.viaversion.fabric.common.commands.UserCommandSender; -import com.viaversion.fabric.common.util.JLoggerToLog4j; -import com.viaversion.viaversion.api.configuration.ViaVersionConfig; +import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.api.command.ViaCommandSender; +import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; +import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import io.netty.util.concurrent.Future; import io.netty.util.concurrent.GenericFutureListener; import net.fabricmc.api.EnvType; import net.fabricmc.loader.api.FabricLoader; -import net.fabricmc.loader.api.ModContainer; -import net.fabricmc.loader.api.Version; -import net.fabricmc.loader.api.metadata.ModMetadata; import net.minecraft.client.MinecraftClient; import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; -import org.apache.logging.log4j.LogManager; -import com.viaversion.viaversion.api.Via; -import com.viaversion.viaversion.api.ViaAPI; -import com.viaversion.viaversion.api.command.ViaCommandSender; -import com.viaversion.viaversion.api.configuration.ConfigurationProvider; -import com.viaversion.viaversion.api.platform.ViaPlatform; -import com.viaversion.viaversion.dump.PluginInfo; -import com.viaversion.viaversion.util.GsonUtil; -import com.viaversion.viaversion.libs.gson.JsonObject; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import java.io.File; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; import java.util.UUID; import java.util.concurrent.Callable; import java.util.concurrent.CancellationException; @@ -42,22 +25,8 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; import java.util.logging.Level; -import java.util.logging.Logger; -import java.util.stream.Collectors; - -public class FabricPlatform implements ViaPlatform { - private final Logger logger = new JLoggerToLog4j(LogManager.getLogger("ViaVersion")); - private final FabricViaConfig config; - private final File dataFolder; - private final ViaAPI api; - - public FabricPlatform() { - Path configDir = FabricLoader.getInstance().getConfigDirectory().toPath().resolve("ViaFabric"); - config = new FabricViaConfig(configDir.resolve("viaversion.yml").toFile()); - dataFolder = configDir.toFile(); - api = new FabricViaAPI(); - } +public class FabricPlatform extends AbstractFabricPlatform { public static MinecraftServer getServer() { // In 1.8.9 integrated server instance exists even if it's not running if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT @@ -69,27 +38,6 @@ public class FabricPlatform implements ViaPlatform { return GsonComponentSerializer.gson().serialize(LegacyComponentSerializer.legacySection().deserialize(legacy)); } - @Override - public Logger getLogger() { - return logger; - } - - @Override - public String getPlatformName() { - return "ViaFabric"; - } - - @Override - public String getPlatformVersion() { - return ViaFabric.getVersion(); - } - - @Override - public String getPluginVersion() { - return FabricLoader.getInstance().getModContainer("viaversion").map(ModContainer::getMetadata) - .map(ModMetadata::getVersion).map(Version::getFriendlyString).orElse("UNKNOWN"); - } - @Override public FutureTaskId runAsync(Runnable runnable) { return new FutureTaskId(CompletableFuture @@ -210,59 +158,4 @@ public class FabricPlatform implements ViaPlatform { } return false; // Can't know if it worked } - - @Override - public boolean isPluginEnabled() { - return true; - } - - @Override - public ViaAPI getApi() { - return api; - } - - @Override - public ViaVersionConfig getConf() { - return config; - } - - @Override - public ConfigurationProvider getConfigurationProvider() { - return config; - } - - @Override - public File getDataFolder() { - return dataFolder; - } - - @Override - public void onReload() { - // Nothing to do - } - - @Override - public JsonObject getDump() { - JsonObject platformSpecific = new JsonObject(); - List mods = new ArrayList<>(); - for (ModContainer mod : FabricLoader.getInstance().getAllMods()) { - mods.add(new PluginInfo(true, - mod.getMetadata().getId() + " (" + mod.getMetadata().getName() + ")", - mod.getMetadata().getVersion().getFriendlyString(), - null, - mod.getMetadata().getAuthors().stream() - .map(info -> info.getName() - + (info.getContact().asMap().isEmpty() ? "" : " " + info.getContact().asMap())) - .collect(Collectors.toList()) - )); - } - - platformSpecific.add("mods", GsonUtil.getGson().toJsonTree(mods)); - return platformSpecific; - } - - @Override - public boolean isOldClientsAllowed() { - return true; - } }