.NET 5+ Support (#1674)

Implement changes to support .NET 5 onwards.
Co-authored-by: ReinforceZwei <39955851+ReinforceZwei@users.noreply.github.com>
Co-authored-by: ORelio <ORelio@users.noreply.github.com>
This commit is contained in:
breadbyte 2022-07-03 22:34:07 +08:00 committed by GitHub
parent b3cc2351ee
commit d9f1a77ac2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
117 changed files with 1028 additions and 9058 deletions

View file

@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using MinecraftClient.Protocol;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using MinecraftClient.Protocol.Handlers.Forge;
using MinecraftClient.Protocol.Session;
@ -35,7 +36,7 @@ namespace MinecraftClient
public const string MCHighestVersion = "1.18.2";
public static readonly string BuildInfo = null;
private static Thread offlinePrompt = null;
private static Tuple<Thread, CancellationTokenSource>? offlinePrompt = null;
private static bool useMcVersionOnce = false;
/// <summary>
@ -43,21 +44,6 @@ namespace MinecraftClient
/// </summary>
static void Main(string[] args)
{
Console.WriteLine("Minecraft Console Client v{0} - for MC {1} to {2} - Github.com/MCCTeam", Version, MCLowestVersion, MCHighestVersion);
//Build information to facilitate processing of bug reports
if (BuildInfo != null)
{
ConsoleIO.WriteLineFormatted("§8" + BuildInfo);
}
//Debug input ?
if (args.Length == 1 && args[0] == "--keyboard-debug")
{
ConsoleIO.WriteLine("Keyboard debug mode: Press any key to display info");
ConsoleIO.DebugReadInput();
}
//Setup ConsoleIO
ConsoleIO.LogPrefix = "§8[MCC] ";
if (args.Length >= 1 && args[args.Length - 1] == "BasicIO" || args.Length >= 1 && args[args.Length - 1] == "BasicIO-NoColor")
@ -69,10 +55,34 @@ namespace MinecraftClient
ConsoleIO.BasicIO = true;
args = args.Where(o => !Object.ReferenceEquals(o, args[args.Length - 1])).ToArray();
}
if (!ConsoleIO.BasicIO)
ConsoleInteractive.ConsoleWriter.Init();
ConsoleIO.WriteLine($"Minecraft Console Client v{Version} - for MC {MCLowestVersion} to {MCHighestVersion} - Github.com/MCCTeam");
//Build information to facilitate processing of bug reports
if (BuildInfo != null)
{
ConsoleIO.WriteLineFormatted("§8" + BuildInfo);
}
//Debug input ?
if (args.Length == 1 && args[0] == "--keyboard-debug")
{
ConsoleIO.WriteLine("Keyboard debug mode: Press any key to display info");
ConsoleIO.DebugReadInput();
}
//Take advantage of Windows 10 / Mac / Linux UTF-8 console
if (isUsingMono || WindowsVersion.WinMajorVersion >= 10)
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// If we're on windows, check if our version is Win10 or greater.
if (WindowsVersion.WinMajorVersion >= 10)
Console.OutputEncoding = Console.InputEncoding = Encoding.UTF8;
}
else {
// Apply to all other operating systems.
Console.OutputEncoding = Console.InputEncoding = Encoding.UTF8;
}
@ -145,8 +155,8 @@ namespace MinecraftClient
bool useBrowser = Settings.AccountType == ProtocolHandler.AccountType.Microsoft && Settings.LoginMethod == "browser";
if (Settings.Login == "" && !useBrowser)
{
Console.Write(ConsoleIO.BasicIO ? Translations.Get("mcc.login_basic_io") + "\n" : Translations.Get("mcc.login"));
Settings.Login = Console.ReadLine();
ConsoleIO.WriteLine(ConsoleIO.BasicIO ? Translations.Get("mcc.login_basic_io") : Translations.Get("mcc.login"));
Settings.Login = ConsoleIO.ReadLine();
}
if (Settings.Password == ""
&& (Settings.SessionCaching == CacheType.None || !SessionCache.Contains(Settings.Login.ToLower()))
@ -160,7 +170,7 @@ namespace MinecraftClient
{
// Do NOT use Program.Exit() as creating new Thread cause program to freeze
if (client != null) { client.Disconnect(); ConsoleIO.Reset(); }
if (offlinePrompt != null) { offlinePrompt.Abort(); offlinePrompt = null; ConsoleIO.Reset(); }
if (offlinePrompt != null) { offlinePrompt.Item2.Cancel(); offlinePrompt = null; ConsoleIO.Reset(); }
if (Settings.playerHeadAsIcon) { ConsoleIcon.revertToMCCIcon(); }
});
@ -174,15 +184,9 @@ namespace MinecraftClient
/// </summary>
private static void RequestPassword()
{
Console.Write(ConsoleIO.BasicIO ? Translations.Get("mcc.password_basic_io", Settings.Login) + "\n" : Translations.Get("mcc.password"));
ConsoleIO.WriteLine(ConsoleIO.BasicIO ? Translations.Get("mcc.password_basic_io", Settings.Login) + "\n" : Translations.Get("mcc.password"));
Settings.Password = ConsoleIO.BasicIO ? Console.ReadLine() : ConsoleIO.ReadPassword();
if (Settings.Password == "") { Settings.Password = "-"; }
if (!ConsoleIO.BasicIO)
{
//Hide password length
Console.CursorTop--; Console.Write(Translations.Get("mcc.password_hidden", "<******>"));
for (int i = 19; i < Console.BufferWidth; i++) { Console.Write(' '); }
}
}
/// <summary>
@ -257,7 +261,7 @@ namespace MinecraftClient
if (Settings.ServerIP == "")
{
Translations.Write("mcc.ip");
string addressInput = Console.ReadLine();
string addressInput = ConsoleIO.ReadLine();
if (addressInput.StartsWith("realms:"))
{
if (Settings.MinecraftRealmsEnabled)
@ -387,12 +391,6 @@ namespace MinecraftClient
default: failureReason = "error.login.unknown"; break;
}
failureMessage += Translations.Get(failureReason);
if (result == ProtocolHandler.LoginResult.SSLError && isUsingMono)
{
Translations.WriteLineFormatted("error.login.ssl_help");
return;
}
HandleFailure(failureMessage, false, ChatBot.DisconnectReason.LoginRejected);
}
}
@ -403,10 +401,11 @@ namespace MinecraftClient
/// <param name="delaySeconds">Optional delay, in seconds, before restarting</param>
public static void Restart(int delaySeconds = 0)
{
ConsoleInteractive.ConsoleReader.StopReadThread();
new Thread(new ThreadStart(delegate
{
if (client != null) { client.Disconnect(); ConsoleIO.Reset(); }
if (offlinePrompt != null) { offlinePrompt.Abort(); offlinePrompt = null; ConsoleIO.Reset(); }
if (offlinePrompt != null) { offlinePrompt.Item2.Cancel(); offlinePrompt.Item1.Join(); offlinePrompt = null; ConsoleIO.Reset(); }
if (delaySeconds > 0)
{
Translations.WriteLine("mcc.restart_delay", delaySeconds);
@ -425,7 +424,7 @@ namespace MinecraftClient
new Thread(new ThreadStart(delegate
{
if (client != null) { client.Disconnect(); ConsoleIO.Reset(); }
if (offlinePrompt != null) { offlinePrompt.Abort(); offlinePrompt = null; ConsoleIO.Reset(); }
if (offlinePrompt != null) { offlinePrompt.Item2.Cancel(); offlinePrompt.Item1.Join(); offlinePrompt = null; ConsoleIO.Reset(); }
if (Settings.playerHeadAsIcon) { ConsoleIcon.revertToMCCIcon(); }
Environment.Exit(exitcode);
})).Start();
@ -459,7 +458,7 @@ namespace MinecraftClient
if (versionError)
{
Translations.Write("mcc.server_version");
Settings.ServerVersion = Console.ReadLine();
Settings.ServerVersion = ConsoleInteractive.ConsoleReader.RequestImmediateInput();
if (Settings.ServerVersion != "")
{
useMcVersionOnce = true;
@ -468,53 +467,89 @@ namespace MinecraftClient
}
}
if (offlinePrompt == null)
if (offlinePrompt == null)
{
offlinePrompt = new Thread(new ThreadStart(delegate
{
ConsoleInteractive.ConsoleReader.StopReadThread();
var cancellationTokenSource = new CancellationTokenSource();
offlinePrompt = new(new Thread(new ThreadStart(delegate {
bool exitThread = false;
string command = " ";
ConsoleIO.WriteLineFormatted(Translations.Get("mcc.disconnected", (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar)));
Translations.WriteLineFormatted("mcc.press_exit");
while (command.Length > 0)
while (!cancellationTokenSource.IsCancellationRequested)
{
if (!ConsoleIO.BasicIO)
if (exitThread)
return;
while (command.Length > 0)
{
ConsoleIO.Write('>');
}
command = Console.ReadLine().Trim();
if (command.Length > 0)
{
string message = "";
if (Settings.internalCmdChar != ' '
&& command[0] == Settings.internalCmdChar)
command = command.Substring(1);
if (command.StartsWith("reco"))
{
message = new Commands.Reco().Run(null, Settings.ExpandVars(command), null);
}
else if (command.StartsWith("connect"))
{
message = new Commands.Connect().Run(null, Settings.ExpandVars(command), null);
}
else if (command.StartsWith("exit") || command.StartsWith("quit"))
{
message = new Commands.Exit().Run(null, Settings.ExpandVars(command), null);
}
else if (command.StartsWith("help"))
{
ConsoleIO.WriteLineFormatted("§8MCC: " + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + new Commands.Reco().GetCmdDescTranslated());
ConsoleIO.WriteLineFormatted("§8MCC: " + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + new Commands.Connect().GetCmdDescTranslated());
}
else ConsoleIO.WriteLineFormatted(Translations.Get("icmd.unknown", command.Split(' ')[0]));
if (message != "")
ConsoleIO.WriteLineFormatted("§8MCC: " + message);
if (cancellationTokenSource.IsCancellationRequested)
return;
command = ConsoleInteractive.ConsoleReader.RequestImmediateInput().Trim();
if (command.Length > 0)
{
string message = "";
if (Settings.internalCmdChar != ' '
&& command[0] == Settings.internalCmdChar)
command = command.Substring(1);
if (command.StartsWith("reco"))
{
message = new Commands.Reco().Run(null, Settings.ExpandVars(command), null);
if (message == "")
{
exitThread = true;
break;
}
}
else if (command.StartsWith("connect"))
{
message = new Commands.Connect().Run(null, Settings.ExpandVars(command), null);
if (message == "")
{
exitThread = true;
break;
}
}
else if (command.StartsWith("exit") || command.StartsWith("quit"))
{
message = new Commands.Exit().Run(null, Settings.ExpandVars(command), null);
}
else if (command.StartsWith("help"))
{
ConsoleIO.WriteLineFormatted("§8MCC: " +
(Settings.internalCmdChar == ' '
? ""
: "" + Settings.internalCmdChar) +
new Commands.Reco().GetCmdDescTranslated());
ConsoleIO.WriteLineFormatted("§8MCC: " +
(Settings.internalCmdChar == ' '
? ""
: "" + Settings.internalCmdChar) +
new Commands.Connect().GetCmdDescTranslated());
}
else
ConsoleIO.WriteLineFormatted(Translations.Get("icmd.unknown",
command.Split(' ')[0]));
if (message != "")
ConsoleIO.WriteLineFormatted("§8MCC: " + message);
}
else
{
_ = new Commands.Exit().Run(null, Settings.ExpandVars(command), null);
}
}
if (exitThread)
return;
}
}));
offlinePrompt.Start();
})), cancellationTokenSource);
offlinePrompt.Item1.Start();
}
}
else
@ -533,17 +568,6 @@ namespace MinecraftClient
}
/// <summary>
/// Detect if the user is running Minecraft Console Client through Mono
/// </summary>
public static bool isUsingMono
{
get
{
return Type.GetType("Mono.Runtime") != null;
}
}
/// <summary>
/// Enumerate types in namespace through reflection
/// </summary>