mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-11-07 17:36:07 +00:00
Merge pull request #100 from Pokechu22/forge-support
Add forge (1.8 and 1.7.10) support by Pokechu22!
This commit is contained in:
commit
29a9fe810a
10 changed files with 566 additions and 111 deletions
|
|
@ -8,6 +8,7 @@ using System.IO;
|
|||
using System.Net;
|
||||
using MinecraftClient.Protocol;
|
||||
using MinecraftClient.Proxy;
|
||||
using MinecraftClient.Protocol.Handlers.Forge;
|
||||
|
||||
namespace MinecraftClient
|
||||
{
|
||||
|
|
@ -54,9 +55,9 @@ namespace MinecraftClient
|
|||
/// <param name="port">The server port to use</param>
|
||||
/// <param name="protocolversion">Minecraft protocol version to use</param>
|
||||
|
||||
public McTcpClient(string username, string uuid, string sessionID, int protocolversion, string server_ip, ushort port)
|
||||
public McTcpClient(string username, string uuid, string sessionID, int protocolversion, ForgeInfo forgeInfo, string server_ip, ushort port)
|
||||
{
|
||||
StartClient(username, uuid, sessionID, server_ip, port, protocolversion, false, "");
|
||||
StartClient(username, uuid, sessionID, server_ip, port, protocolversion, forgeInfo, false, "");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -70,9 +71,9 @@ namespace MinecraftClient
|
|||
/// <param name="protocolversion">Minecraft protocol version to use</param>
|
||||
/// <param name="command">The text or command to send.</param>
|
||||
|
||||
public McTcpClient(string username, string uuid, string sessionID, string server_ip, ushort port, int protocolversion, string command)
|
||||
public McTcpClient(string username, string uuid, string sessionID, string server_ip, ushort port, int protocolversion, ForgeInfo forgeInfo, string command)
|
||||
{
|
||||
StartClient(username, uuid, sessionID, server_ip, port, protocolversion, true, command);
|
||||
StartClient(username, uuid, sessionID, server_ip, port, protocolversion, forgeInfo, true, command);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -87,7 +88,7 @@ namespace MinecraftClient
|
|||
/// <param name="singlecommand">If set to true, the client will send a single command and then disconnect from the server</param>
|
||||
/// <param name="command">The text or command to send. Will only be sent if singlecommand is set to true.</param>
|
||||
|
||||
private void StartClient(string user, string uuid, string sessionID, string server_ip, ushort port, int protocolversion, bool singlecommand, string command)
|
||||
private void StartClient(string user, string uuid, string sessionID, string server_ip, ushort port, int protocolversion, ForgeInfo forgeInfo, bool singlecommand, string command)
|
||||
{
|
||||
bool retry = false;
|
||||
this.sessionid = sessionID;
|
||||
|
|
@ -113,7 +114,7 @@ namespace MinecraftClient
|
|||
{
|
||||
client = ProxyHandler.newTcpClient(host, port);
|
||||
client.ReceiveBufferSize = 1024 * 1024;
|
||||
handler = Protocol.ProtocolHandler.getProtocolHandler(client, protocolversion, this);
|
||||
handler = Protocol.ProtocolHandler.getProtocolHandler(client, protocolversion, forgeInfo, this);
|
||||
Console.WriteLine("Version is supported.\nLogging in...");
|
||||
|
||||
try
|
||||
|
|
|
|||
|
|
@ -114,6 +114,9 @@
|
|||
<Compile Include="Crypto\Streams\RegularAesStream.cs" />
|
||||
<Compile Include="Crypto\CryptoHandler.cs" />
|
||||
<Compile Include="CSharpRunner.cs" />
|
||||
<Compile Include="Protocol\Handlers\Forge\FMLHandshakeClientState.cs" />
|
||||
<Compile Include="Protocol\Handlers\Forge\FMLHandshakeDiscriminator.cs" />
|
||||
<Compile Include="Protocol\Handlers\Forge\ForgeInfo.cs" />
|
||||
<Compile Include="Protocol\Handlers\Compression\CRC32.cs" />
|
||||
<Compile Include="Protocol\Handlers\Compression\Deflate.cs" />
|
||||
<Compile Include="Protocol\Handlers\Compression\GZipStream.cs" />
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ using System.Text;
|
|||
using MinecraftClient.Protocol;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using MinecraftClient.Protocol.Handlers.Forge;
|
||||
|
||||
namespace MinecraftClient
|
||||
{
|
||||
|
|
@ -145,6 +146,7 @@ namespace MinecraftClient
|
|||
|
||||
//Get server version
|
||||
int protocolversion = 0;
|
||||
ForgeInfo forgeInfo = null;
|
||||
|
||||
if (Settings.ServerVersion != "" && Settings.ServerVersion.ToLower() != "auto")
|
||||
{
|
||||
|
|
@ -166,7 +168,7 @@ namespace MinecraftClient
|
|||
if (protocolversion == 0)
|
||||
{
|
||||
Console.WriteLine("Retrieving Server Info...");
|
||||
if (!ProtocolHandler.GetServerInfo(Settings.ServerIP, Settings.ServerPort, ref protocolversion))
|
||||
if (!ProtocolHandler.GetServerInfo(Settings.ServerIP, Settings.ServerPort, ref protocolversion, ref forgeInfo))
|
||||
{
|
||||
HandleFailure("Failed to ping this IP.", true, ChatBots.AutoRelog.DisconnectReason.ConnectionLost);
|
||||
return;
|
||||
|
|
@ -180,9 +182,9 @@ namespace MinecraftClient
|
|||
//Start the main TCP client
|
||||
if (Settings.SingleCommand != "")
|
||||
{
|
||||
Client = new McTcpClient(Settings.Username, UUID, sessionID, Settings.ServerIP, Settings.ServerPort, protocolversion, Settings.SingleCommand);
|
||||
Client = new McTcpClient(Settings.Username, UUID, sessionID, Settings.ServerIP, Settings.ServerPort, protocolversion, forgeInfo, Settings.SingleCommand);
|
||||
}
|
||||
else Client = new McTcpClient(Settings.Username, UUID, sessionID, protocolversion, Settings.ServerIP, Settings.ServerPort);
|
||||
else Client = new McTcpClient(Settings.Username, UUID, sessionID, protocolversion, forgeInfo, Settings.ServerIP, Settings.ServerPort);
|
||||
|
||||
//Update console title
|
||||
if (Settings.ConsoleTitle != "")
|
||||
|
|
|
|||
23
MinecraftClient/Protocol/Handlers/Forge/FMLHandshakeClientState.cs
Executable file
23
MinecraftClient/Protocol/Handlers/Forge/FMLHandshakeClientState.cs
Executable file
|
|
@ -0,0 +1,23 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace MinecraftClient.Protocol.Handlers.Forge
|
||||
{
|
||||
/// <summary>
|
||||
/// Copy of the forge enum for client states.
|
||||
/// https://github.com/MinecraftForge/MinecraftForge/blob/ebe9b6d4cbc4a5281c386994f1fbda04df5d2e1f/src/main/java/net/minecraftforge/fml/common/network/handshake/FMLHandshakeClientState.java
|
||||
/// </summary>
|
||||
enum FMLHandshakeClientState : byte
|
||||
{
|
||||
START,
|
||||
HELLO,
|
||||
WAITINGSERVERDATA,
|
||||
WAITINGSERVERCOMPLETE,
|
||||
PENDINGCOMPLETE,
|
||||
COMPLETE,
|
||||
DONE,
|
||||
ERROR
|
||||
}
|
||||
}
|
||||
21
MinecraftClient/Protocol/Handlers/Forge/FMLHandshakeDiscriminator.cs
Executable file
21
MinecraftClient/Protocol/Handlers/Forge/FMLHandshakeDiscriminator.cs
Executable file
|
|
@ -0,0 +1,21 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace MinecraftClient.Protocol.Handlers.Forge
|
||||
{
|
||||
/// <summary>
|
||||
/// Different "discriminator byte" values for the forge handshake.
|
||||
/// https://github.com/MinecraftForge/MinecraftForge/blob/ebe9b6d4cbc4a5281c386994f1fbda04df5d2e1f/src/main/java/net/minecraftforge/fml/common/network/handshake/FMLHandshakeCodec.java
|
||||
/// </summary>
|
||||
enum FMLHandshakeDiscriminator : byte
|
||||
{
|
||||
ServerHello = 0,
|
||||
ClientHello = 1,
|
||||
ModList = 2,
|
||||
RegistryData = 3,
|
||||
HandshakeAck = 255, //-1
|
||||
HandshakeReset = 254, //-2
|
||||
}
|
||||
}
|
||||
70
MinecraftClient/Protocol/Handlers/Forge/ForgeInfo.cs
Executable file
70
MinecraftClient/Protocol/Handlers/Forge/ForgeInfo.cs
Executable file
|
|
@ -0,0 +1,70 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace MinecraftClient.Protocol.Handlers.Forge
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains information about a modded server install.
|
||||
/// </summary>
|
||||
public class ForgeInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents an individual forge mod.
|
||||
/// </summary>
|
||||
public class ForgeMod
|
||||
{
|
||||
public ForgeMod(String ModID, String Version)
|
||||
{
|
||||
this.ModID = ModID;
|
||||
this.Version = Version;
|
||||
}
|
||||
|
||||
public readonly String ModID;
|
||||
public readonly String Version;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return ModID + " v" + Version;
|
||||
}
|
||||
}
|
||||
|
||||
public List<ForgeMod> Mods;
|
||||
|
||||
/// <summary>
|
||||
/// Create a new ForgeInfo from the given data.
|
||||
/// </summary>
|
||||
/// <param name="data">The modinfo JSON tag.</param>
|
||||
internal ForgeInfo(Json.JSONData data)
|
||||
{
|
||||
// Example ModInfo (with spacing):
|
||||
|
||||
// "modinfo": {
|
||||
// "type": "FML",
|
||||
// "modList": [{
|
||||
// "modid": "mcp",
|
||||
// "version": "9.05"
|
||||
// }, {
|
||||
// "modid": "FML",
|
||||
// "version": "8.0.99.99"
|
||||
// }, {
|
||||
// "modid": "Forge",
|
||||
// "version": "11.14.3.1512"
|
||||
// }, {
|
||||
// "modid": "rpcraft",
|
||||
// "version": "Beta 1.3 - 1.8.0"
|
||||
// }]
|
||||
// }
|
||||
|
||||
this.Mods = new List<ForgeMod>();
|
||||
foreach (Json.JSONData mod in data.Properties["modList"].DataArray)
|
||||
{
|
||||
String modid = mod.Properties["modid"].StringValue;
|
||||
String version = mod.Properties["version"].StringValue;
|
||||
|
||||
this.Mods.Add(new ForgeMod(modid, version));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -615,6 +615,31 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
return false; //Only supported since MC 1.7
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send a plugin channel packet to the server.
|
||||
/// </summary>
|
||||
/// <param name="channel">Channel to send packet on</param>
|
||||
/// <param name="data">packet Data</param>
|
||||
|
||||
public bool SendPluginChannelPacket(string channel, byte[] data)
|
||||
{
|
||||
try {
|
||||
byte[] channelLength = BitConverter.GetBytes((short)channel.Length);
|
||||
Array.Reverse(channelLength);
|
||||
|
||||
byte[] channelData = Encoding.BigEndianUnicode.GetBytes(channel);
|
||||
|
||||
byte[] dataLength = BitConverter.GetBytes((short)data.Length);
|
||||
Array.Reverse(dataLength);
|
||||
|
||||
Send(concatBytes(new byte[] { 0xFA }, channelLength, channelData, dataLength, data));
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (SocketException) { return false; }
|
||||
catch (System.IO.IOException) { return false; }
|
||||
}
|
||||
|
||||
public string AutoComplete(string BehindCursor)
|
||||
{
|
||||
if (String.IsNullOrEmpty(BehindCursor))
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ using System.Threading;
|
|||
using MinecraftClient.Crypto;
|
||||
using MinecraftClient.Proxy;
|
||||
using System.Security.Cryptography;
|
||||
using MinecraftClient.Protocol.Handlers.Forge;
|
||||
|
||||
namespace MinecraftClient.Protocol.Handlers
|
||||
{
|
||||
|
|
@ -25,18 +26,23 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
private bool encrypted = false;
|
||||
private int protocolversion;
|
||||
|
||||
// Server forge info -- may be null.
|
||||
private ForgeInfo forgeInfo;
|
||||
private FMLHandshakeClientState fmlHandshakeState = FMLHandshakeClientState.START;
|
||||
|
||||
IMinecraftComHandler handler;
|
||||
Thread netRead;
|
||||
IAesStream s;
|
||||
TcpClient c;
|
||||
|
||||
public Protocol18Handler(TcpClient Client, int ProtocolVersion, IMinecraftComHandler Handler)
|
||||
public Protocol18Handler(TcpClient Client, int ProtocolVersion, IMinecraftComHandler Handler, ForgeInfo ForgeInfo)
|
||||
{
|
||||
ConsoleIO.SetAutoCompleteEngine(this);
|
||||
ChatParser.InitTranslations();
|
||||
this.c = Client;
|
||||
this.protocolversion = ProtocolVersion;
|
||||
this.handler = Handler;
|
||||
this.forgeInfo = ForgeInfo;
|
||||
}
|
||||
|
||||
private Protocol18Handler(TcpClient Client)
|
||||
|
|
@ -133,8 +139,8 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
return false; //Ignored packet
|
||||
}
|
||||
}
|
||||
else //Regular in-game packets
|
||||
{
|
||||
// Regular in-game packets
|
||||
|
||||
switch (packetID)
|
||||
{
|
||||
case 0x00: //Keep-Alive
|
||||
|
|
@ -204,6 +210,147 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
if (tab_list.Length > 0)
|
||||
ConsoleIO.WriteLineFormatted("§8" + tab_list, false);
|
||||
break;
|
||||
case 0x3F: //Plugin message.
|
||||
String channel = readNextString(ref packetData);
|
||||
if (protocolversion < MC18Version)
|
||||
{
|
||||
if (forgeInfo == null)
|
||||
{
|
||||
// 1.7 and lower prefix plugin channel packets with the length.
|
||||
// We can skip it, though.
|
||||
readNextShort(ref packetData);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Forge does something even weirder with the length.
|
||||
readNextVarShort(ref packetData);
|
||||
}
|
||||
}
|
||||
if (forgeInfo != null)
|
||||
{
|
||||
if (channel == "FML|HS")
|
||||
{
|
||||
FMLHandshakeDiscriminator discriminator = (FMLHandshakeDiscriminator)readNextByte(ref packetData);
|
||||
|
||||
if (discriminator == FMLHandshakeDiscriminator.HandshakeReset)
|
||||
{
|
||||
fmlHandshakeState = FMLHandshakeClientState.START;
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (fmlHandshakeState)
|
||||
{
|
||||
case FMLHandshakeClientState.START:
|
||||
if (discriminator != FMLHandshakeDiscriminator.ServerHello)
|
||||
return false;
|
||||
|
||||
// Send the plugin channel registration.
|
||||
// REGISTER is somewhat special in that it doesn't actually include length information,
|
||||
// and is also \0-separated.
|
||||
// Also, yes, "FML" is there twice. Don't ask me why, but that's the way forge does it.
|
||||
string[] channels = { "FML|HS", "FML", "FML|MP", "FML", "FORGE" };
|
||||
SendPluginChannelPacket("REGISTER", Encoding.UTF8.GetBytes(string.Join("\0", channels)));
|
||||
|
||||
byte fmlProtocolVersion = readNextByte(ref packetData);
|
||||
// There's another value afterwards for the dimension, but we don't need it.
|
||||
|
||||
ConsoleIO.WriteLineFormatted("§8Forge protocol version : " + fmlProtocolVersion);
|
||||
|
||||
// Tell the server we're running the same version.
|
||||
SendForgeHandshakePacket(FMLHandshakeDiscriminator.ClientHello, new byte[] { fmlProtocolVersion });
|
||||
|
||||
// Then tell the server that we're running the same mods.
|
||||
ConsoleIO.WriteLineFormatted("§8Sending falsified mod list to server...");
|
||||
byte[][] mods = new byte[forgeInfo.Mods.Count][];
|
||||
for (int i = 0; i < forgeInfo.Mods.Count; i++)
|
||||
{
|
||||
ForgeInfo.ForgeMod mod = forgeInfo.Mods[i];
|
||||
mods[i] = concatBytes(getString(mod.ModID), getString(mod.Version));
|
||||
}
|
||||
SendForgeHandshakePacket(FMLHandshakeDiscriminator.ModList,
|
||||
concatBytes(getVarInt(forgeInfo.Mods.Count), concatBytes(mods)));
|
||||
|
||||
fmlHandshakeState = FMLHandshakeClientState.WAITINGSERVERDATA;
|
||||
|
||||
return true;
|
||||
case FMLHandshakeClientState.WAITINGSERVERDATA:
|
||||
if (discriminator != FMLHandshakeDiscriminator.ModList)
|
||||
return false;
|
||||
|
||||
Thread.Sleep(2000);
|
||||
|
||||
ConsoleIO.WriteLineFormatted("§8Accepting server mod list...");
|
||||
// Tell the server that yes, we are OK with the mods it has
|
||||
// even though we don't actually care what mods it has.
|
||||
|
||||
SendForgeHandshakePacket(FMLHandshakeDiscriminator.HandshakeAck,
|
||||
new byte[] { (byte)FMLHandshakeClientState.WAITINGSERVERDATA });
|
||||
|
||||
fmlHandshakeState = FMLHandshakeClientState.WAITINGSERVERCOMPLETE;
|
||||
return false;
|
||||
case FMLHandshakeClientState.WAITINGSERVERCOMPLETE:
|
||||
// The server now will tell us a bunch of registry information.
|
||||
// We need to read it all, though, until it says that there is no more.
|
||||
if (discriminator != FMLHandshakeDiscriminator.RegistryData)
|
||||
return false;
|
||||
|
||||
if (protocolversion < MC18Version)
|
||||
{
|
||||
// 1.7.10 and below have one registry
|
||||
// with blocks and items.
|
||||
int registrySize = readNextVarInt(ref packetData);
|
||||
|
||||
ConsoleIO.WriteLineFormatted("§8Received registry " +
|
||||
"with " + registrySize + " entries");
|
||||
|
||||
fmlHandshakeState = FMLHandshakeClientState.PENDINGCOMPLETE;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 1.8+ has more than one registry.
|
||||
|
||||
bool hasNextRegistry = readNextBool(ref packetData);
|
||||
string registryName = readNextString(ref packetData);
|
||||
int registrySize = readNextVarInt(ref packetData);
|
||||
|
||||
ConsoleIO.WriteLineFormatted("§8Received registry " + registryName +
|
||||
" with " + registrySize + " entries");
|
||||
|
||||
if (!hasNextRegistry)
|
||||
{
|
||||
fmlHandshakeState = FMLHandshakeClientState.PENDINGCOMPLETE;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
case FMLHandshakeClientState.PENDINGCOMPLETE:
|
||||
// The server will ask us to accept the registries.
|
||||
// Just say yes.
|
||||
if (discriminator != FMLHandshakeDiscriminator.HandshakeAck)
|
||||
return false;
|
||||
|
||||
ConsoleIO.WriteLineFormatted("§8Accepting server registries...");
|
||||
|
||||
SendForgeHandshakePacket(FMLHandshakeDiscriminator.HandshakeAck,
|
||||
new byte[] { (byte)FMLHandshakeClientState.PENDINGCOMPLETE });
|
||||
fmlHandshakeState = FMLHandshakeClientState.COMPLETE;
|
||||
|
||||
return true;
|
||||
case FMLHandshakeClientState.COMPLETE:
|
||||
// One final "OK". On the actual forge source, a packet is sent from
|
||||
// the client to the client saying that the connection was complete, but
|
||||
// we don't need to do that.
|
||||
|
||||
SendForgeHandshakePacket(FMLHandshakeDiscriminator.HandshakeAck,
|
||||
new byte[] { (byte)FMLHandshakeClientState.COMPLETE });
|
||||
ConsoleIO.WriteLine("Forge server connection complete!");
|
||||
|
||||
fmlHandshakeState = FMLHandshakeClientState.DONE;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
case 0x40: //Kick Packet
|
||||
handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick, ChatParser.ParseText(readNextString(ref packetData)));
|
||||
return false;
|
||||
|
|
@ -221,7 +368,6 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
default:
|
||||
return false; //Ignored packet
|
||||
}
|
||||
}
|
||||
return true; //Packet processed
|
||||
}
|
||||
|
||||
|
|
@ -326,6 +472,18 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
return BitConverter.ToInt16(rawValue, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read an unsigned short integer from a cache of bytes and remove it from the cache
|
||||
/// </summary>
|
||||
/// <returns>The unsigned short integer value</returns>
|
||||
|
||||
private static ushort readNextUShort(ref byte[] cache)
|
||||
{
|
||||
byte[] rawValue = readData(2, ref cache);
|
||||
Array.Reverse(rawValue); //Endianness
|
||||
return BitConverter.ToUInt16(rawValue, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read a uuid from a cache of bytes and remove it from the cache
|
||||
/// </summary>
|
||||
|
|
@ -396,6 +554,36 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
return i;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read an "extended short", which is actually an int of some kind, from the cache of bytes.
|
||||
/// This is only done with forge. It looks like it's a normal short, except that if the high
|
||||
/// bit is set, it has an extra byte.
|
||||
/// </summary>
|
||||
/// <param name="cache">Cache of bytes to read from</param>
|
||||
/// <returns>The int</returns>
|
||||
|
||||
private static int readNextVarShort(ref byte[] cache)
|
||||
{
|
||||
ushort low = readNextUShort(ref cache);
|
||||
byte high = 0;
|
||||
if ((low & 0x8000) != 0)
|
||||
{
|
||||
low &= 0x7FFF;
|
||||
high = readNextByte(ref cache);
|
||||
}
|
||||
return ((high & 0xFF) << 15) | low;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read a single byte from a cache of bytes and remove it from the cache
|
||||
/// </summary>
|
||||
/// <returns>The byte that was read</returns>
|
||||
|
||||
private static byte readNextByte(ref byte[] cache)
|
||||
{
|
||||
return readData(1, ref cache)[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Build an integer for sending over the network
|
||||
/// </summary>
|
||||
|
|
@ -431,6 +619,19 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
else return concatBytes(getVarInt(array.Length), array);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a byte array from the given string for sending over the network, with length information prepended.
|
||||
/// </summary>
|
||||
/// <param name="array">String to process</param>
|
||||
/// <returns>Array ready to send</returns>
|
||||
|
||||
private byte[] getString(string text)
|
||||
{
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(text);
|
||||
|
||||
return concatBytes(getVarInt(bytes.Length), bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Easily append several byte arrays
|
||||
/// </summary>
|
||||
|
|
@ -473,6 +674,17 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send a forge plugin channel packet ("FML|HS"). Compression and encryption will be handled automatically
|
||||
/// </summary>
|
||||
/// <param name="discriminator">Discriminator to use.</param>
|
||||
/// <param name="data">packet Data</param>
|
||||
|
||||
private void SendForgeHandshakePacket(FMLHandshakeDiscriminator discriminator, byte[] data)
|
||||
{
|
||||
SendPluginChannelPacket("FML|HS", concatBytes(new byte[] { (byte)discriminator }, data));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send a packet to the server, compression and encryption will be handled automatically
|
||||
/// </summary>
|
||||
|
|
@ -525,7 +737,7 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
public bool Login()
|
||||
{
|
||||
byte[] protocol_version = getVarInt(protocolversion);
|
||||
byte[] server_adress_val = Encoding.UTF8.GetBytes(handler.GetServerHost());
|
||||
byte[] server_adress_val = Encoding.UTF8.GetBytes(handler.GetServerHost() + (forgeInfo != null ? "\0FML\0" : ""));
|
||||
byte[] server_adress_len = getVarInt(server_adress_val.Length);
|
||||
byte[] server_port = BitConverter.GetBytes((ushort)handler.GetServerPort()); Array.Reverse(server_port);
|
||||
byte[] next_state = getVarInt(2);
|
||||
|
|
@ -560,6 +772,15 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
{
|
||||
ConsoleIO.WriteLineFormatted("§8Server is in offline mode.");
|
||||
login_phase = false;
|
||||
|
||||
if (forgeInfo != null) {
|
||||
// Do the forge handshake.
|
||||
if (!CompleteForgeHandshake())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
StartUpdating();
|
||||
return true; //No need to check session or start encryption
|
||||
}
|
||||
|
|
@ -567,6 +788,33 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Completes the Minecraft Forge handshake.
|
||||
/// </summary>
|
||||
/// <returns>Whether the handshake was successful.</returns>
|
||||
private bool CompleteForgeHandshake()
|
||||
{
|
||||
int packetID = -1;
|
||||
byte[] packetData = new byte[0];
|
||||
|
||||
while (fmlHandshakeState != FMLHandshakeClientState.DONE)
|
||||
{
|
||||
readNextPacket(ref packetID, ref packetData);
|
||||
|
||||
if (packetID == 0x40) // Disconect
|
||||
{
|
||||
handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, ChatParser.ParseText(readNextString(ref packetData)));
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
handlePacket(packetID, packetData);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start network encryption. Automatically called by Login() if the server requests encryption.
|
||||
/// </summary>
|
||||
|
|
@ -614,6 +862,16 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
else if (packetID == 0x02) //Login successful
|
||||
{
|
||||
login_phase = false;
|
||||
|
||||
if (forgeInfo != null)
|
||||
{
|
||||
// Do the forge handshake.
|
||||
if (!CompleteForgeHandshake())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
StartUpdating();
|
||||
return true;
|
||||
}
|
||||
|
|
@ -669,13 +927,34 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
{
|
||||
if (String.IsNullOrEmpty(brandInfo))
|
||||
return false;
|
||||
|
||||
return SendPluginChannelPacket("MC|Brand", getString(brandInfo));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send a plugin channel packet (0x17) to the server, compression and encryption will be handled automatically
|
||||
/// </summary>
|
||||
/// <param name="channel">Channel to send packet on</param>
|
||||
/// <param name="data">packet Data</param>
|
||||
|
||||
public bool SendPluginChannelPacket(string channel, byte[] data)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] channel = Encoding.UTF8.GetBytes("MC|Brand");
|
||||
byte[] channelLen = getVarInt(channel.Length);
|
||||
byte[] brand = Encoding.UTF8.GetBytes(brandInfo);
|
||||
byte[] brandLen = getVarInt(brand.Length);
|
||||
SendPacket(0x17, concatBytes(channelLen, channel, brandLen, brand));
|
||||
// In 1.7, length needs to be included.
|
||||
// In 1.8, it must not be.
|
||||
if (protocolversion < MC18Version)
|
||||
{
|
||||
byte[] length = BitConverter.GetBytes((short)data.Length);
|
||||
Array.Reverse(length);
|
||||
|
||||
SendPacket(0x17, concatBytes(getString(channel), length, data));
|
||||
}
|
||||
else
|
||||
{
|
||||
SendPacket(0x17, concatBytes(getString(channel), data));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (SocketException) { return false; }
|
||||
|
|
@ -730,7 +1009,7 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
/// </summary>
|
||||
/// <returns>True if ping was successful</returns>
|
||||
|
||||
public static bool doPing(string host, int port, ref int protocolversion)
|
||||
public static bool doPing(string host, int port, ref int protocolversion, ref ForgeInfo forgeInfo)
|
||||
{
|
||||
string version = "";
|
||||
TcpClient tcp = ProxyHandler.newTcpClient(host, port);
|
||||
|
|
@ -760,26 +1039,43 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
if (readNextVarInt(ref packetData) == 0x00) //Read Packet ID
|
||||
{
|
||||
string result = readNextString(ref packetData); //Get the Json data
|
||||
|
||||
if (!String.IsNullOrEmpty(result) && result.StartsWith("{") && result.EndsWith("}"))
|
||||
{
|
||||
Json.JSONData jsonData = Json.ParseJson(result);
|
||||
if (jsonData.Type == Json.JSONData.DataType.Object && jsonData.Properties.ContainsKey("version"))
|
||||
{
|
||||
jsonData = jsonData.Properties["version"];
|
||||
Json.JSONData versionData = jsonData.Properties["version"];
|
||||
|
||||
//Retrieve display name of the Minecraft version
|
||||
if (jsonData.Properties.ContainsKey("name"))
|
||||
version = jsonData.Properties["name"].StringValue;
|
||||
if (versionData.Properties.ContainsKey("name"))
|
||||
version = versionData.Properties["name"].StringValue;
|
||||
|
||||
//Retrieve protocol version number for handling this server
|
||||
if (jsonData.Properties.ContainsKey("protocol"))
|
||||
protocolversion = atoi(jsonData.Properties["protocol"].StringValue);
|
||||
if (versionData.Properties.ContainsKey("protocol"))
|
||||
protocolversion = atoi(versionData.Properties["protocol"].StringValue);
|
||||
|
||||
//Automatic fix for BungeeCord 1.8 reporting itself as 1.7...
|
||||
if (protocolversion < 47 && version.Split(' ', '/').Contains("1.8"))
|
||||
protocolversion = ProtocolHandler.MCVer2ProtocolVersion("1.8.0");
|
||||
|
||||
ConsoleIO.WriteLineFormatted("§8Server version : " + version + " (protocol v" + protocolversion + ").");
|
||||
|
||||
// Check for forge on the server.
|
||||
if (jsonData.Properties.ContainsKey("modinfo") && jsonData.Properties["modinfo"].Type == Json.JSONData.DataType.Object)
|
||||
{
|
||||
Json.JSONData modData = jsonData.Properties["modinfo"];
|
||||
if (modData.Properties.ContainsKey("type") && modData.Properties["type"].StringValue == "FML")
|
||||
{
|
||||
forgeInfo = new ForgeInfo(modData);
|
||||
|
||||
ConsoleIO.WriteLineFormatted("§8Server is running forge. Mod list:");
|
||||
foreach (ForgeInfo.ForgeMod mod in forgeInfo.Mods)
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted("§8 " + mod.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,5 +51,16 @@ namespace MinecraftClient.Protocol
|
|||
/// <returns>True if brand info was successfully sent</returns>
|
||||
|
||||
bool SendBrandInfo(string brandInfo);
|
||||
|
||||
/// <summary>
|
||||
/// Send a plugin channel packet to the server.
|
||||
///
|
||||
/// http://dinnerbone.com/blog/2012/01/13/minecraft-plugin-channels-messaging/
|
||||
/// </summary>
|
||||
/// <param name="channel">Channel to send packet on</param>
|
||||
/// <param name="data">packet Data</param>
|
||||
/// <returns>True if message was successfully sent</returns>
|
||||
|
||||
bool SendPluginChannelPacket(string channel, byte[] data);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ using MinecraftClient.Protocol.Handlers;
|
|||
using MinecraftClient.Proxy;
|
||||
using System.Net.Sockets;
|
||||
using System.Net.Security;
|
||||
using MinecraftClient.Protocol.Handlers.Forge;
|
||||
|
||||
namespace MinecraftClient.Protocol
|
||||
{
|
||||
|
|
@ -23,16 +24,17 @@ namespace MinecraftClient.Protocol
|
|||
/// <param name="protocolversion">Will contain protocol version, if ping successful</param>
|
||||
/// <returns>TRUE if ping was successful</returns>
|
||||
|
||||
public static bool GetServerInfo(string serverIP, ushort serverPort, ref int protocolversion)
|
||||
public static bool GetServerInfo(string serverIP, ushort serverPort, ref int protocolversion, ref ForgeInfo forgeInfo)
|
||||
{
|
||||
bool success = false;
|
||||
int protocolversionTmp = 0;
|
||||
ForgeInfo forgeInfoTmp = null;
|
||||
if (AutoTimeout.Perform(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Protocol16Handler.doPing(serverIP, serverPort, ref protocolversionTmp)
|
||||
|| Protocol18Handler.doPing(serverIP, serverPort, ref protocolversionTmp))
|
||||
|| Protocol18Handler.doPing(serverIP, serverPort, ref protocolversionTmp, ref forgeInfoTmp))
|
||||
{
|
||||
success = true;
|
||||
}
|
||||
|
|
@ -40,11 +42,12 @@ namespace MinecraftClient.Protocol
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted("§8" + e.Message);
|
||||
ConsoleIO.WriteLineFormatted("§8" + e.ToString());
|
||||
}
|
||||
}, TimeSpan.FromSeconds(30)))
|
||||
{
|
||||
protocolversion = protocolversionTmp;
|
||||
forgeInfo = forgeInfoTmp;
|
||||
return success;
|
||||
}
|
||||
else
|
||||
|
|
@ -62,14 +65,14 @@ namespace MinecraftClient.Protocol
|
|||
/// <param name="Handler">Handler with the appropriate callbacks</param>
|
||||
/// <returns></returns>
|
||||
|
||||
public static IMinecraftCom getProtocolHandler(TcpClient Client, int ProtocolVersion, IMinecraftComHandler Handler)
|
||||
public static IMinecraftCom getProtocolHandler(TcpClient Client, int ProtocolVersion, ForgeInfo forgeInfo, IMinecraftComHandler Handler)
|
||||
{
|
||||
int[] supportedVersions_Protocol16 = { 51, 60, 61, 72, 73, 74, 78 };
|
||||
if (Array.IndexOf(supportedVersions_Protocol16, ProtocolVersion) > -1)
|
||||
return new Protocol16Handler(Client, ProtocolVersion, Handler);
|
||||
int[] supportedVersions_Protocol18 = { 4, 5, 47 };
|
||||
if (Array.IndexOf(supportedVersions_Protocol18, ProtocolVersion) > -1)
|
||||
return new Protocol18Handler(Client, ProtocolVersion, Handler);
|
||||
return new Protocol18Handler(Client, ProtocolVersion, Handler, forgeInfo);
|
||||
throw new NotSupportedException("The protocol version no." + ProtocolVersion + " is not supported.");
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue