Cleaned connecting code by removing useless mixins/fields and fixed a few issues

This commit is contained in:
FlorianMichael 2023-11-28 17:25:59 +01:00
parent a3fe3bee87
commit 770e0ff29c
No known key found for this signature in database
GPG Key ID: C2FB87E71C425126
9 changed files with 61 additions and 60 deletions

View File

@ -26,10 +26,10 @@ public interface IServerInfo {
VersionEnum viaFabricPlus$forcedVersion();
void viaFabricPlus$forceVersion(final VersionEnum version);
boolean viaFabricPlus$enabled();
void viaFabricPlus$enable();
boolean viaFabricPlus$passedDirectConnectScreen();
void viaFabricPlus$passDirectConnectScreen();
int viaFabricPlus$translatingVersion();
void viaFabricPlus$setTranslatingVersion(final int version);
VersionEnum viaFabricPlus$translatingVersion();
void viaFabricPlus$setTranslatingVersion(final VersionEnum version);
}

View File

@ -69,9 +69,11 @@ public abstract class MixinConnectScreen_1 {
@SuppressWarnings("InvalidInjectorMethodSignature")
@Inject(method = "run", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/ClientConnection;connect(Ljava/net/InetSocketAddress;ZLnet/minecraft/network/ClientConnection;)Lio/netty/channel/ChannelFuture;", shift = At.Shift.BEFORE), locals = LocalCapture.CAPTURE_FAILHARD)
private void setServerInfo(CallbackInfo ci, InetSocketAddress inetSocketAddress) {
final VersionEnum serverVersion = ((IServerInfo) this.field_40415).viaFabricPlus$forcedVersion();
if (serverVersion != null) {
ProtocolHack.setTargetVersion(serverVersion);
final IServerInfo mixinServerInfo = (IServerInfo) this.field_40415;
final VersionEnum targetVersion = mixinServerInfo.viaFabricPlus$forcedVersion();
if (targetVersion != null && !mixinServerInfo.viaFabricPlus$passedDirectConnectScreen()) {
ProtocolHack.setTargetVersion(targetVersion);
} else if (GeneralSettings.global().autoDetectVersion.getValue()) {
this.field_2416.setStatus(Text.translatable("base.viafabricplus.detecting_server_version"));
MCPing

View File

@ -40,8 +40,7 @@ public abstract class MixinMultiplayerScreen extends Screen {
@Inject(method = "init", at = @At("RETURN"))
private void addProtocolSelectionButton(CallbackInfo ci) {
// Create the button
var builder = ButtonWidget.builder(Text.literal("ViaFabricPlus"), button ->
MainScreen.INSTANCE.open(this)).size(98, 20);
var builder = ButtonWidget.builder(Text.literal("ViaFabricPlus"), button -> MainScreen.INSTANCE.open(this)).size(98, 20);
// Set the button's position according to the configured orientation
builder = GeneralSettings.withOrientation(builder, GeneralSettings.global().multiplayerScreenButtonOrientation.getIndex(), width, height);

View File

@ -51,29 +51,33 @@ public abstract class MixinMultiplayerServerListPinger_1 implements ClientQueryP
@Inject(method = "onResponse(Lnet/minecraft/network/packet/s2c/query/QueryResponseS2CPacket;)V", at = @At("HEAD"))
private void trackTranslatingState(QueryResponseS2CPacket packet, CallbackInfo ci) {
final UserConnection userConnection = ((IClientConnection) field_3774).viaFabricPlus$getUserConnection();
// If ViaVersion is translating the current connection, we track the target version, and it's state in the server info
// So we can later draw this information when hovering over the ping bar in the server list
if (userConnection != null) {
((IServerInfo) field_3776).viaFabricPlus$enable();
((IServerInfo) field_3776).viaFabricPlus$setTranslatingVersion(userConnection.getProtocolInfo().getServerProtocolVersion());
if (field_3774 instanceof IClientConnection mixinClientConnection) {
((IServerInfo) field_3776).viaFabricPlus$setTranslatingVersion(mixinClientConnection.viaFabricPlus$getTargetVersion());
}
}
@Inject(method = "onResponse", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/ServerMetadata;version()Ljava/util/Optional;", shift = At.Shift.AFTER))
@Inject(method = "onResponse", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/ClientConnection;send(Lnet/minecraft/network/packet/Packet;)V", shift = At.Shift.AFTER))
private void setProtocolVersion(CallbackInfo ci) {
final VersionEnum version = ((IClientConnection) this.field_3774).viaFabricPlus$getTargetVersion();
// ViaVersion is not translating the current connection, so we don't need to do anything
if (version == null) {
return;
}
final boolean isCompatible;
if (ProtocolHack.getTargetVersion().isOlderThanOrEqualTo(VersionEnum.r1_6_4)) {
if (version.isOlderThanOrEqualTo(VersionEnum.r1_6_4)) {
// Because of ViaVersion not supporting legacy minecraft versions where protocol ids are overlapping, ViaLegacy
// has its own protocol id offset, where realVersion = -(ViaLegacyVersion >> 2). Normally ViaVersion sends the client
// version to the client so its detection doesn't break when checking for serverVersion == clientVersion, but since
// ViaLegacy doesn't do that, we have to do it ourselves
isCompatible = LegacyProtocolVersion.getRealProtocolVersion(((IClientConnection) this.field_3774).viaFabricPlus$getTargetVersion().getVersion()) == this.field_3776.protocolVersion;
isCompatible = LegacyProtocolVersion.getRealProtocolVersion(version.getVersion()) == this.field_3776.protocolVersion;
} else if (ProtocolHack.getTargetVersion().equals(VersionEnum.bedrockLatest)) {
// Bedrock edition doesn't have a protocol id like the Java edition, ViaBedrock also has its own protocol id offset
// Which we need to remove to get the real protocol id
isCompatible = ((IClientConnection) this.field_3774).viaFabricPlus$getTargetVersion().getVersion() - BedrockProtocolVersion.PROTOCOL_ID_OVERLAP_PREVENTION_OFFSET == this.field_3776.protocolVersion;
isCompatible = version.getVersion() - BedrockProtocolVersion.PROTOCOL_ID_OVERLAP_PREVENTION_OFFSET == this.field_3776.protocolVersion;
} else {
return;
}

View File

@ -47,12 +47,10 @@ public abstract class MixinMultiplayerServerListWidget_ServerEntry {
private void drawTranslatingState(MultiplayerScreen instance, List<Text> tooltip, Operation<Void> original) {
final List<Text> tooltipCopy = new ArrayList<>(tooltip);
if (GeneralSettings.global().showAdvertisedServerVersion.getValue()) {
final IServerInfo mixinServerInfo = ((IServerInfo) server);
final VersionEnum versionEnum = ((IServerInfo) server).viaFabricPlus$translatingVersion();
if (mixinServerInfo.viaFabricPlus$enabled()) {
final var versionEnum = VersionEnum.fromProtocolId(mixinServerInfo.viaFabricPlus$translatingVersion());
tooltipCopy.add(Text.translatable("base.viafabricplus.via_translates_to", versionEnum != VersionEnum.UNKNOWN ? versionEnum.getName() + " (" + versionEnum.getVersion() + ")" : mixinServerInfo.viaFabricPlus$translatingVersion()));
if (versionEnum != null) {
tooltipCopy.add(Text.translatable("base.viafabricplus.via_translates_to", versionEnum.getName() + " (" + versionEnum.getOriginalVersion() + ")"));
tooltipCopy.add(Text.translatable("base.viafabricplus.server_version", server.version.getString() + " (" + server.protocolVersion + ")"));
}
}

View File

@ -20,28 +20,22 @@
package de.florianmichael.viafabricplus.injection.mixin.base.perserverversion;
import de.florianmichael.viafabricplus.injection.access.IServerInfo;
import de.florianmichael.viafabricplus.protocolhack.ProtocolHack;
import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen;
import net.minecraft.client.network.ServerInfo;
import org.spongepowered.asm.mixin.Final;
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;
import org.spongepowered.asm.mixin.injection.Redirect;
@Mixin(targets = "net.minecraft.client.gui.screen.ConnectScreen$1")
public abstract class MixinConnectScreen_1 {
@Mixin(MultiplayerScreen.class)
public abstract class MixinMultiplayerScreen {
@Final
@Shadow
ServerInfo field_40415;
@Shadow protected abstract void connect(ServerInfo entry);
@Inject(method = "run", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/ClientConnection;connect(Ljava/net/InetSocketAddress;ZLnet/minecraft/network/ClientConnection;)Lio/netty/channel/ChannelFuture;"))
private void setForcedTargetVersion(CallbackInfo ci) {
if (field_40415 != null) {
// Set the target version to the forced version when connecting to a server
ProtocolHack.setTargetVersion(((IServerInfo) field_40415).viaFabricPlus$forcedVersion());
}
@Redirect(method = "directConnect", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/multiplayer/MultiplayerScreen;connect(Lnet/minecraft/client/network/ServerInfo;)V"))
private void storeDirectConnectionPhase(MultiplayerScreen instance, ServerInfo entry) {
((IServerInfo) entry).viaFabricPlus$passDirectConnectScreen();
connect(entry);
}
}

View File

@ -39,7 +39,8 @@ public abstract class MixinMultiplayerServerListPinger {
@Redirect(method = "add", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/ClientConnection;connect(Ljava/net/InetSocketAddress;ZLnet/minecraft/util/profiler/PerformanceLog;)Lnet/minecraft/network/ClientConnection;"))
private ClientConnection setForcedVersion(InetSocketAddress address, boolean useEpoll, PerformanceLog packetSizeLog, @Local ServerInfo serverInfo) {
final VersionEnum forcedVersion = ((IServerInfo) serverInfo).viaFabricPlus$forcedVersion();
if (forcedVersion != null) {
if (forcedVersion != null && !((IServerInfo) serverInfo).viaFabricPlus$passedDirectConnectScreen()) {
// We use the PerformanceLog field to store the forced version since it's always null when pinging a server
// So we can create a dummy instance, store the forced version in it and later destroy the instance again
// To avoid any side effects, we also support cases where a mod is also creating a PerformanceLog instance

View File

@ -41,15 +41,11 @@ public abstract class MixinServerInfo implements IServerInfo {
@Unique
private VersionEnum viaFabricPlus$forcedVersion = null;
@Override
public VersionEnum viaFabricPlus$forcedVersion() {
return viaFabricPlus$forcedVersion;
}
@Unique
private boolean viaFabricPlus$passedDirectConnectScreen;
@Override
public void viaFabricPlus$forceVersion(VersionEnum version) {
viaFabricPlus$forcedVersion = version;
}
@Unique
private VersionEnum viaFabricPlus$translatingVersion;
@Inject(method = "toNbt", at = @At("TAIL"), locals = LocalCapture.CAPTURE_FAILHARD)
private void saveForcedVersion(CallbackInfoReturnable<NbtCompound> cir, NbtCompound nbtCompound) {
@ -77,29 +73,36 @@ public abstract class MixinServerInfo implements IServerInfo {
viaFabricPlus$forceVersion(((IServerInfo) serverInfo).viaFabricPlus$forcedVersion());
}
@Unique
private boolean viaFabricPlus$enabled;
@Override
public boolean viaFabricPlus$enabled() {
return viaFabricPlus$enabled;
public VersionEnum viaFabricPlus$forcedVersion() {
return viaFabricPlus$forcedVersion;
}
@Override
public void viaFabricPlus$enable() {
viaFabricPlus$enabled = true;
public void viaFabricPlus$forceVersion(VersionEnum version) {
viaFabricPlus$forcedVersion = version;
}
@Unique
private int viaFabricPlus$translatingVersion;
@Override
public boolean viaFabricPlus$passedDirectConnectScreen() {
final boolean previous = viaFabricPlus$passedDirectConnectScreen;
viaFabricPlus$passedDirectConnectScreen = false;
return previous;
}
@Override
public int viaFabricPlus$translatingVersion() {
public void viaFabricPlus$passDirectConnectScreen() {
viaFabricPlus$passedDirectConnectScreen = true;
}
@Override
public VersionEnum viaFabricPlus$translatingVersion() {
return viaFabricPlus$translatingVersion;
}
@Override
public void viaFabricPlus$setTranslatingVersion(int version) {
public void viaFabricPlus$setTranslatingVersion(VersionEnum version) {
viaFabricPlus$translatingVersion = version;
}
}

View File

@ -19,7 +19,7 @@
"base.integration.MixinMultiplayerServerListPinger_1",
"base.integration.MixinMultiplayerServerListWidget_ServerEntry",
"base.integration.MixinRakSessionCodec",
"base.perserverversion.MixinConnectScreen_1",
"base.perserverversion.MixinMultiplayerScreen",
"base.perserverversion.MixinMultiplayerServerListPinger",
"base.perserverversion.MixinPerformanceLog",
"base.perserverversion.MixinServerInfo",
@ -162,7 +162,6 @@
"fixes.minecraft.screen.screenhandler.MixinScreenHandler",
"fixes.vialegacy.MixinClassicProtocolExtension",
"fixes.vialegacy.MixinClientboundPacketsc0_30cpe",
"vialegacy.MixinExtensionProtocolMetadataStorage",
"fixes.vialegacy.MixinProtocol1_8to1_7_6_10",
"fixes.vialegacy.MixinProtocolc0_30toc0_30cpe",
"fixes.viaversion.MixinChatItemRewriter",
@ -193,6 +192,7 @@
"viabedrock.MixinBedrockProtocol",
"viabedrock.MixinBlobCache",
"viabedrock.MixinJoinPackets",
"vialegacy.MixinExtensionProtocolMetadataStorage",
"viaversion.MixinAbstractFenceConnectionHandler",
"viaversion.MixinGlassConnectionHandler",
"viaversion.MixinProtocolVersion"