Detect and store the list of forge mods.

This commit is contained in:
Pokechu22 2015-10-23 16:54:36 -07:00
parent f67a3e3384
commit 7cc87d8e71
6 changed files with 119 additions and 21 deletions

View file

@ -8,6 +8,7 @@ using System.IO;
using System.Net; using System.Net;
using MinecraftClient.Protocol; using MinecraftClient.Protocol;
using MinecraftClient.Proxy; using MinecraftClient.Proxy;
using MinecraftClient.Protocol.Handlers.Forge;
namespace MinecraftClient namespace MinecraftClient
{ {
@ -54,9 +55,9 @@ namespace MinecraftClient
/// <param name="port">The server port to use</param> /// <param name="port">The server port to use</param>
/// <param name="protocolversion">Minecraft protocol version 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> /// <summary>
@ -70,9 +71,9 @@ namespace MinecraftClient
/// <param name="protocolversion">Minecraft protocol version to use</param> /// <param name="protocolversion">Minecraft protocol version to use</param>
/// <param name="command">The text or command to send.</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> /// <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="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> /// <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; bool retry = false;
this.sessionid = sessionID; this.sessionid = sessionID;
@ -113,7 +114,7 @@ namespace MinecraftClient
{ {
client = ProxyHandler.newTcpClient(host, port); client = ProxyHandler.newTcpClient(host, port);
client.ReceiveBufferSize = 1024 * 1024; 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..."); Console.WriteLine("Version is supported.\nLogging in...");
try try

View file

@ -114,6 +114,7 @@
<Compile Include="Crypto\Streams\RegularAesStream.cs" /> <Compile Include="Crypto\Streams\RegularAesStream.cs" />
<Compile Include="Crypto\CryptoHandler.cs" /> <Compile Include="Crypto\CryptoHandler.cs" />
<Compile Include="CSharpRunner.cs" /> <Compile Include="CSharpRunner.cs" />
<Compile Include="Protocol\Handlers\Forge\ForgeInfo.cs" />
<Compile Include="Protocol\Handlers\Compression\CRC32.cs" /> <Compile Include="Protocol\Handlers\Compression\CRC32.cs" />
<Compile Include="Protocol\Handlers\Compression\Deflate.cs" /> <Compile Include="Protocol\Handlers\Compression\Deflate.cs" />
<Compile Include="Protocol\Handlers\Compression\GZipStream.cs" /> <Compile Include="Protocol\Handlers\Compression\GZipStream.cs" />

View file

@ -5,6 +5,7 @@ using System.Text;
using MinecraftClient.Protocol; using MinecraftClient.Protocol;
using System.Reflection; using System.Reflection;
using System.Threading; using System.Threading;
using MinecraftClient.Protocol.Handlers.Forge;
namespace MinecraftClient namespace MinecraftClient
{ {
@ -145,6 +146,7 @@ namespace MinecraftClient
//Get server version //Get server version
int protocolversion = 0; int protocolversion = 0;
ForgeInfo forgeInfo = null;
if (Settings.ServerVersion != "" && Settings.ServerVersion.ToLower() != "auto") if (Settings.ServerVersion != "" && Settings.ServerVersion.ToLower() != "auto")
{ {
@ -166,7 +168,7 @@ namespace MinecraftClient
if (protocolversion == 0) if (protocolversion == 0)
{ {
Console.WriteLine("Retrieving Server Info..."); 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); HandleFailure("Failed to ping this IP.", true, ChatBots.AutoRelog.DisconnectReason.ConnectionLost);
return; return;
@ -180,9 +182,9 @@ namespace MinecraftClient
//Start the main TCP client //Start the main TCP client
if (Settings.SingleCommand != "") 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 //Update console title
if (Settings.ConsoleTitle != "") if (Settings.ConsoleTitle != "")

View 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));
}
}
}
}

View file

@ -7,6 +7,7 @@ using System.Threading;
using MinecraftClient.Crypto; using MinecraftClient.Crypto;
using MinecraftClient.Proxy; using MinecraftClient.Proxy;
using System.Security.Cryptography; using System.Security.Cryptography;
using MinecraftClient.Protocol.Handlers.Forge;
namespace MinecraftClient.Protocol.Handlers namespace MinecraftClient.Protocol.Handlers
{ {
@ -25,18 +26,22 @@ namespace MinecraftClient.Protocol.Handlers
private bool encrypted = false; private bool encrypted = false;
private int protocolversion; private int protocolversion;
// Server forge info -- may be null.
private ForgeInfo forgeInfo;
IMinecraftComHandler handler; IMinecraftComHandler handler;
Thread netRead; Thread netRead;
IAesStream s; IAesStream s;
TcpClient c; TcpClient c;
public Protocol18Handler(TcpClient Client, int ProtocolVersion, IMinecraftComHandler Handler) public Protocol18Handler(TcpClient Client, int ProtocolVersion, IMinecraftComHandler Handler, ForgeInfo ForgeInfo)
{ {
ConsoleIO.SetAutoCompleteEngine(this); ConsoleIO.SetAutoCompleteEngine(this);
ChatParser.InitTranslations(); ChatParser.InitTranslations();
this.c = Client; this.c = Client;
this.protocolversion = ProtocolVersion; this.protocolversion = ProtocolVersion;
this.handler = Handler; this.handler = Handler;
this.forgeInfo = ForgeInfo;
} }
private Protocol18Handler(TcpClient Client) private Protocol18Handler(TcpClient Client)
@ -730,7 +735,7 @@ namespace MinecraftClient.Protocol.Handlers
/// </summary> /// </summary>
/// <returns>True if ping was successful</returns> /// <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 = ""; string version = "";
TcpClient tcp = ProxyHandler.newTcpClient(host, port); TcpClient tcp = ProxyHandler.newTcpClient(host, port);
@ -766,21 +771,37 @@ namespace MinecraftClient.Protocol.Handlers
Json.JSONData jsonData = Json.ParseJson(result); Json.JSONData jsonData = Json.ParseJson(result);
if (jsonData.Type == Json.JSONData.DataType.Object && jsonData.Properties.ContainsKey("version")) 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 //Retrieve display name of the Minecraft version
if (jsonData.Properties.ContainsKey("name")) if (versionData.Properties.ContainsKey("name"))
version = jsonData.Properties["name"].StringValue; version = versionData.Properties["name"].StringValue;
//Retrieve protocol version number for handling this server //Retrieve protocol version number for handling this server
if (jsonData.Properties.ContainsKey("protocol")) if (versionData.Properties.ContainsKey("protocol"))
protocolversion = atoi(jsonData.Properties["protocol"].StringValue); protocolversion = atoi(versionData.Properties["protocol"].StringValue);
//Automatic fix for BungeeCord 1.8 reporting itself as 1.7... //Automatic fix for BungeeCord 1.8 reporting itself as 1.7...
if (protocolversion < 47 && version.Split(' ', '/').Contains("1.8")) if (protocolversion < 47 && version.Split(' ', '/').Contains("1.8"))
protocolversion = ProtocolHandler.MCVer2ProtocolVersion("1.8.0"); protocolversion = ProtocolHandler.MCVer2ProtocolVersion("1.8.0");
ConsoleIO.WriteLineFormatted("§8Server version : " + version + " (protocol v" + protocolversion + ")."); 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; return true;
} }
} }

View file

@ -6,6 +6,7 @@ using MinecraftClient.Protocol.Handlers;
using MinecraftClient.Proxy; using MinecraftClient.Proxy;
using System.Net.Sockets; using System.Net.Sockets;
using System.Net.Security; using System.Net.Security;
using MinecraftClient.Protocol.Handlers.Forge;
namespace MinecraftClient.Protocol namespace MinecraftClient.Protocol
{ {
@ -23,16 +24,17 @@ namespace MinecraftClient.Protocol
/// <param name="protocolversion">Will contain protocol version, if ping successful</param> /// <param name="protocolversion">Will contain protocol version, if ping successful</param>
/// <returns>TRUE if ping was successful</returns> /// <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; bool success = false;
int protocolversionTmp = 0; int protocolversionTmp = 0;
ForgeInfo forgeInfoTmp = null;
if (AutoTimeout.Perform(() => if (AutoTimeout.Perform(() =>
{ {
try try
{ {
if (Protocol16Handler.doPing(serverIP, serverPort, ref protocolversionTmp) if (Protocol16Handler.doPing(serverIP, serverPort, ref protocolversionTmp)
|| Protocol18Handler.doPing(serverIP, serverPort, ref protocolversionTmp)) || Protocol18Handler.doPing(serverIP, serverPort, ref protocolversionTmp, ref forgeInfoTmp))
{ {
success = true; success = true;
} }
@ -40,11 +42,12 @@ namespace MinecraftClient.Protocol
} }
catch (Exception e) catch (Exception e)
{ {
ConsoleIO.WriteLineFormatted("§8" + e.Message); ConsoleIO.WriteLineFormatted("§8" + e.ToString());
} }
}, TimeSpan.FromSeconds(30))) }, TimeSpan.FromSeconds(30)))
{ {
protocolversion = protocolversionTmp; protocolversion = protocolversionTmp;
forgeInfo = forgeInfoTmp;
return success; return success;
} }
else else
@ -62,14 +65,14 @@ namespace MinecraftClient.Protocol
/// <param name="Handler">Handler with the appropriate callbacks</param> /// <param name="Handler">Handler with the appropriate callbacks</param>
/// <returns></returns> /// <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 }; int[] supportedVersions_Protocol16 = { 51, 60, 61, 72, 73, 74, 78 };
if (Array.IndexOf(supportedVersions_Protocol16, ProtocolVersion) > -1) if (Array.IndexOf(supportedVersions_Protocol16, ProtocolVersion) > -1)
return new Protocol16Handler(Client, ProtocolVersion, Handler); return new Protocol16Handler(Client, ProtocolVersion, Handler);
int[] supportedVersions_Protocol18 = { 4, 5, 47 }; int[] supportedVersions_Protocol18 = { 4, 5, 47 };
if (Array.IndexOf(supportedVersions_Protocol18, ProtocolVersion) > -1) 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."); throw new NotSupportedException("The protocol version no." + ProtocolVersion + " is not supported.");
} }