Patch a small bug in SPacketLoginSuccess that lets us emulate the network

handshake. This will be very useful as I rebuild the networking.
This commit is contained in:
cpw 2017-06-16 22:00:54 -04:00
parent b5260a386d
commit 4a4c38b4e0
3 changed files with 192 additions and 4 deletions

View file

@ -1,10 +1,10 @@
{
"assetIndex": {
"id": "1.12",
"sha1": "9838fc4e7d9808b5d9b44c97f6034a304ed4854c",
"sha1": "67e29e024e664064c1f04c728604f83c24cbc218",
"size": 169014,
"url": "https://launchermeta.mojang.com/mc/assets/1.12/9838fc4e7d9808b5d9b44c97f6034a304ed4854c/1.12.json",
"totalSize": 127005140
"url": "https://launchermeta.mojang.com/mc/assets/1.12/67e29e024e664064c1f04c728604f83c24cbc218/1.12.json",
"totalSize": 127037169
},
"assets": "1.12",
"downloads": {
@ -719,6 +719,6 @@
"minecraftArguments": "--username ${auth_player_name} --version ${version_name} --gameDir ${game_directory} --assetsDir ${assets_root} --assetIndex ${assets_index_name} --uuid ${auth_uuid} --accessToken ${auth_access_token} --userType ${user_type} --versionType ${version_type}",
"minimumLauncherVersion": 18,
"releaseTime": "2017-06-02T13:50:27+00:00",
"time": "2017-06-08T13:23:27+00:00",
"time": "2017-06-13T06:57:00+00:00",
"type": "release"
}

View file

@ -0,0 +1,11 @@
--- ../src-base/minecraft/net/minecraft/network/login/server/SPacketLoginSuccess.java
+++ ../src-work/minecraft/net/minecraft/network/login/server/SPacketLoginSuccess.java
@@ -26,7 +26,7 @@
{
String s = p_148837_1_.func_150789_c(36);
String s1 = p_148837_1_.func_150789_c(16);
- UUID uuid = UUID.fromString(s);
+ UUID uuid = s.length() > 0 ? UUID.fromString(s) : null;
this.field_149602_a = new GameProfile(uuid, s1);
}

View file

@ -0,0 +1,177 @@
package net.minecraftforge.fml.test.networkhandshake;
import com.mojang.authlib.GameProfile;
import net.minecraft.network.EnumConnectionState;
import net.minecraft.network.INetHandler;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.NetworkSystem;
import net.minecraft.network.Packet;
import net.minecraft.network.login.INetHandlerLoginClient;
import net.minecraft.network.login.server.SPacketLoginSuccess;
import net.minecraft.network.play.INetHandlerPlayClient;
import net.minecraft.network.play.server.SPacketJoinGame;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.EnumDifficulty;
import net.minecraft.world.GameType;
import net.minecraft.world.WorldType;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Proxy;
import java.net.InetAddress;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import static org.junit.Assert.assertEquals;
/**
* Created by cpw on 10/06/17.
*/
public class TestNetworkHandshake
{
private static final Packet<?>[] vanillapackets = new Packet[] {
new SPacketLoginSuccess(new GameProfile(null, "cheese")), new SPacketJoinGame(1, GameType.NOT_SET, false, 0, EnumDifficulty.PEACEFUL, 100, WorldType.DEFAULT, false)
};
private NetworkSystem listener;
public static NetworkManager client;
public static NetworkManager server;
private CyclicBarrier barrier;
@BeforeClass
public static void setupLogging() {
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
builder.add(builder.newFilter("MarkerFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL).addAttribute("marker", "NETWORK"));
builder.add(builder.newFilter("MarkerFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL).addAttribute("marker", "PACKET_RECEIVED"));
builder.add(builder.newFilter("MarkerFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL).addAttribute("marker", "PACKET_SENT"));
Configurator.initialize(builder.build());
Configurator.setRootLevel(Level.DEBUG);
}
@Before
public void setup() throws IOException, NoSuchFieldException, IllegalAccessException, InterruptedException
{
barrier = new CyclicBarrier(2);
final MinecraftServer minecraftServer = new MinecraftServer(Paths.get(".").toFile(), null, null, null, null, null, null)
{
@Override
public boolean init() throws IOException
{
return false;
}
@Override
public boolean canStructuresSpawn()
{
return false;
}
@Override
public GameType getGameType()
{
return null;
}
@Override
public EnumDifficulty getDifficulty()
{
return null;
}
@Override
public boolean isHardcore()
{
return false;
}
@Override
public int getOpPermissionLevel()
{
return 0;
}
@Override
public boolean shouldBroadcastRconToOps()
{
return false;
}
@Override
public boolean shouldBroadcastConsoleToOps()
{
return false;
}
@Override
public boolean isDedicatedServer()
{
return false;
}
@Override
public boolean shouldUseNativeTransport()
{
return false;
}
@Override
public boolean isCommandBlockEnabled()
{
return false;
}
@Override
public String shareToLAN(GameType type, boolean allowCheats)
{
return null;
}
};
listener = minecraftServer.getNetworkSystem();
listener.addLanEndpoint(InetAddress.getLocalHost(), 54321);
final Field networkManagers = listener.getClass().getDeclaredField("networkManagers");
networkManagers.setAccessible(true);
final List<NetworkManager> nmList = (List<NetworkManager>)networkManagers.get(listener);
client = NetworkManager.createNetworkManagerAndConnect(InetAddress.getLocalHost(), 54321, true);
while (nmList.isEmpty())
{
Thread.sleep(10);
}
server = nmList.get(0);
Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {INetHandlerLoginClient.class, INetHandlerPlayClient.class}, (proxyobj, method, args) ->
{
if ("toString".equals(method.getName())) {
return "PROXY";
}
System.out.println(method.getName());
barrier.await();
return null;
});
client.setNetHandler((INetHandler)proxy);
while (!client.isChannelOpen() || !server.isChannelOpen()) {
Thread.sleep(10);
}
client.setConnectionState(EnumConnectionState.LOGIN);
server.setConnectionState(EnumConnectionState.LOGIN);
}
@Test
public void testNetworkFlow() throws InterruptedException, BrokenBarrierException
{
assertEquals(EnumConnectionState.LOGIN, client.channel().attr(NetworkManager.PROTOCOL_ATTRIBUTE_KEY).get());
assertEquals(EnumConnectionState.LOGIN, server.channel().attr(NetworkManager.PROTOCOL_ATTRIBUTE_KEY).get());
server.sendPacket(vanillapackets[0]);
barrier.await();
client.setConnectionState(EnumConnectionState.PLAY);
server.sendPacket(vanillapackets[1]);
}
}