Skip to content

Commit

Permalink
fix: bandwidth chart on 1.20.2+
Browse files Browse the repository at this point in the history
  • Loading branch information
ishland committed Dec 18, 2024
1 parent 53a140c commit 8ff8f27
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ public String getMeasureTrafficOutFormatted() {
return measureTrafficOutFormatted;
}

public long getBytesIn() {
return bytesIn;
}

// ========== Misc ==========

private MetricsSynchronizationHandler metricsSynchronizationHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
return AFTER_1_20_1;
if (mixinClassName.equals("com.ishland.raknetify.fabric.mixin.server.MixinPlayerManager1_20_2"))
return AFTER_1_20_1;
if (mixinClassName.equals("com.ishland.raknetify.fabric.mixin.common.MixinClientConnection1_20_2"))
return AFTER_1_20_1;
if (mixinClassName.equals("com.ishland.raknetify.fabric.mixin.server.MixinPlayerManager1_20_1"))
return !AFTER_1_20_1;
if (mixinClassName.equals("com.ishland.raknetify.fabric.mixin.access.INetworkState1_20_4"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* This file is a part of the Raknetify project, licensed under MIT.
*
* Copyright (c) 2022-2023 ishland
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.ishland.raknetify.fabric.mixin.common;

import com.ishland.raknetify.common.connection.SimpleMetricsLogger;
import io.netty.channel.Channel;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.handler.PacketSizeLogger;
import network.ycc.raknet.RakNet;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
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 MixinClientConnection1_20_2 {

@Shadow private Channel channel;
@Shadow private @Nullable PacketSizeLogger packetSizeLogger;

@Unique
private long raknetify$lastBytesIn = 0L;

@Inject(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/handler/PacketSizeLogger;push()V"))
private void onPacketLoggerPush(CallbackInfo ci) {
PacketSizeLogger logger = this.packetSizeLogger;
if (logger != null && this.channel.config() instanceof RakNet.Config config && config.getMetrics() instanceof SimpleMetricsLogger simpleMetricsLogger) {
long bytesIn = simpleMetricsLogger.getBytesIn();
logger.increment((int) (bytesIn - this.raknetify$lastBytesIn));
this.raknetify$lastBytesIn = bytesIn;
}
}

}
1 change: 1 addition & 0 deletions fabric/src/main/resources/raknetify-fabric.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"common.MixinCCConnect",
"common.MixinClientConnection",
"common.MixinClientConnection1",
"common.MixinClientConnection1_20_2",
"common.encryption.MixinClientConnection",
"common.encryption.MixinPacketDecryptor",
"common.encryption.MixinPacketEncryptionManager",
Expand Down

0 comments on commit 8ff8f27

Please sign in to comment.