mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
.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:
parent
b3cc2351ee
commit
d9f1a77ac2
117 changed files with 1028 additions and 9058 deletions
|
|
@ -2,7 +2,6 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using System.Threading;
|
||||
|
||||
namespace MinecraftClient
|
||||
|
|
@ -16,27 +15,13 @@ namespace MinecraftClient
|
|||
public static class ConsoleIO
|
||||
{
|
||||
private static IAutoComplete autocomplete_engine;
|
||||
private static LinkedList<string> autocomplete_words = new LinkedList<string>();
|
||||
private static LinkedList<string> previous = new LinkedList<string>();
|
||||
private static readonly object io_lock = new object();
|
||||
private static bool reading = false;
|
||||
private static string buffer = "";
|
||||
private static string buffer2 = "";
|
||||
|
||||
/// <summary>
|
||||
/// Reset the IO mechanism and clear all buffers
|
||||
/// </summary>
|
||||
public static void Reset()
|
||||
{
|
||||
lock (io_lock)
|
||||
{
|
||||
if (reading)
|
||||
{
|
||||
ClearLineAndBuffer();
|
||||
reading = false;
|
||||
Console.Write("\b \b");
|
||||
}
|
||||
}
|
||||
ClearLineAndBuffer();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -75,44 +60,15 @@ namespace MinecraftClient
|
|||
/// </summary>
|
||||
public static string ReadPassword()
|
||||
{
|
||||
StringBuilder password = new StringBuilder();
|
||||
|
||||
ConsoleKeyInfo k;
|
||||
while ((k = Console.ReadKey(true)).Key != ConsoleKey.Enter)
|
||||
if (BasicIO)
|
||||
return Console.ReadLine();
|
||||
else
|
||||
{
|
||||
switch (k.Key)
|
||||
{
|
||||
case ConsoleKey.Backspace:
|
||||
if (password.Length > 0)
|
||||
{
|
||||
Console.Write("\b \b");
|
||||
password.Remove(password.Length - 1, 1);
|
||||
}
|
||||
break;
|
||||
|
||||
case ConsoleKey.Escape:
|
||||
case ConsoleKey.LeftArrow:
|
||||
case ConsoleKey.RightArrow:
|
||||
case ConsoleKey.Home:
|
||||
case ConsoleKey.End:
|
||||
case ConsoleKey.Delete:
|
||||
case ConsoleKey.DownArrow:
|
||||
case ConsoleKey.UpArrow:
|
||||
case ConsoleKey.Tab:
|
||||
break;
|
||||
|
||||
default:
|
||||
if (k.KeyChar != 0)
|
||||
{
|
||||
Console.Write('*');
|
||||
password.Append(k.KeyChar);
|
||||
}
|
||||
break;
|
||||
}
|
||||
ConsoleInteractive.ConsoleReader.SetInputVisible(false);
|
||||
var input = ConsoleInteractive.ConsoleReader.RequestImmediateInput();
|
||||
ConsoleInteractive.ConsoleReader.SetInputVisible(true);
|
||||
return input;
|
||||
}
|
||||
|
||||
Console.WriteLine();
|
||||
return password.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -121,117 +77,9 @@ namespace MinecraftClient
|
|||
public static string ReadLine()
|
||||
{
|
||||
if (BasicIO)
|
||||
{
|
||||
return Console.ReadLine();
|
||||
}
|
||||
|
||||
ConsoleKeyInfo k = new ConsoleKeyInfo();
|
||||
|
||||
lock (io_lock)
|
||||
{
|
||||
Console.Write('>');
|
||||
reading = true;
|
||||
buffer = "";
|
||||
buffer2 = "";
|
||||
}
|
||||
|
||||
while (k.Key != ConsoleKey.Enter)
|
||||
{
|
||||
k = Console.ReadKey(true);
|
||||
lock (io_lock)
|
||||
{
|
||||
if (k.Key == ConsoleKey.V && k.Modifiers == ConsoleModifiers.Control)
|
||||
{
|
||||
string clip = ReadClipboard();
|
||||
foreach (char c in clip)
|
||||
AddChar(c);
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (k.Key)
|
||||
{
|
||||
case ConsoleKey.Escape:
|
||||
ClearLineAndBuffer();
|
||||
break;
|
||||
case ConsoleKey.Backspace:
|
||||
RemoveOneChar();
|
||||
break;
|
||||
case ConsoleKey.Enter:
|
||||
Console.Write('\n');
|
||||
break;
|
||||
case ConsoleKey.LeftArrow:
|
||||
GoLeft();
|
||||
break;
|
||||
case ConsoleKey.RightArrow:
|
||||
GoRight();
|
||||
break;
|
||||
case ConsoleKey.Home:
|
||||
while (buffer.Length > 0) { GoLeft(); }
|
||||
break;
|
||||
case ConsoleKey.End:
|
||||
while (buffer2.Length > 0) { GoRight(); }
|
||||
break;
|
||||
case ConsoleKey.Delete:
|
||||
if (buffer2.Length > 0)
|
||||
{
|
||||
GoRight();
|
||||
RemoveOneChar();
|
||||
}
|
||||
break;
|
||||
case ConsoleKey.DownArrow:
|
||||
if (previous.Count > 0)
|
||||
{
|
||||
ClearLineAndBuffer();
|
||||
buffer = previous.First.Value;
|
||||
previous.AddLast(buffer);
|
||||
previous.RemoveFirst();
|
||||
Console.Write(buffer);
|
||||
}
|
||||
break;
|
||||
case ConsoleKey.UpArrow:
|
||||
if (previous.Count > 0)
|
||||
{
|
||||
ClearLineAndBuffer();
|
||||
buffer = previous.Last.Value;
|
||||
previous.AddFirst(buffer);
|
||||
previous.RemoveLast();
|
||||
Console.Write(buffer);
|
||||
}
|
||||
break;
|
||||
case ConsoleKey.Tab:
|
||||
if (autocomplete_words.Count == 0 && autocomplete_engine != null && buffer.Length > 0)
|
||||
foreach (string result in autocomplete_engine.AutoComplete(buffer))
|
||||
autocomplete_words.AddLast(result);
|
||||
string word_autocomplete = null;
|
||||
if (autocomplete_words.Count > 0)
|
||||
{
|
||||
word_autocomplete = autocomplete_words.First.Value;
|
||||
autocomplete_words.RemoveFirst();
|
||||
autocomplete_words.AddLast(word_autocomplete);
|
||||
}
|
||||
if (!String.IsNullOrEmpty(word_autocomplete) && word_autocomplete != buffer)
|
||||
{
|
||||
while (buffer.Length > 0 && buffer[buffer.Length - 1] != ' ') { RemoveOneChar(); }
|
||||
foreach (char c in word_autocomplete) { AddChar(c); }
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (k.KeyChar != 0)
|
||||
AddChar(k.KeyChar);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (k.Key != ConsoleKey.Tab)
|
||||
autocomplete_words.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
lock (io_lock)
|
||||
{
|
||||
reading = false;
|
||||
previous.AddLast(buffer + buffer2);
|
||||
return buffer + buffer2;
|
||||
}
|
||||
else
|
||||
return ConsoleInteractive.ConsoleReader.RequestImmediateInput();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -247,68 +95,15 @@ namespace MinecraftClient
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write a string to the standard output, without newline character
|
||||
/// </summary>
|
||||
public static void Write(string text)
|
||||
{
|
||||
if (!BasicIO)
|
||||
{
|
||||
lock (io_lock)
|
||||
{
|
||||
if (reading)
|
||||
{
|
||||
try
|
||||
{
|
||||
string buf = buffer;
|
||||
string buf2 = buffer2;
|
||||
ClearLineAndBuffer();
|
||||
if (Console.CursorLeft == 0)
|
||||
{
|
||||
Console.CursorLeft = Console.BufferWidth - 1;
|
||||
Console.CursorTop--;
|
||||
Console.Write(' ');
|
||||
Console.CursorLeft = Console.BufferWidth - 1;
|
||||
Console.CursorTop--;
|
||||
}
|
||||
else Console.Write("\b \b");
|
||||
Console.Write(text);
|
||||
buffer = buf;
|
||||
buffer2 = buf2;
|
||||
Console.Write(">" + buffer);
|
||||
if (buffer2.Length > 0)
|
||||
{
|
||||
Console.Write(buffer2 + " \b");
|
||||
for (int i = 0; i < buffer2.Length; i++) { GoBack(); }
|
||||
}
|
||||
}
|
||||
catch (ArgumentOutOfRangeException)
|
||||
{
|
||||
//Console resized: Try again
|
||||
Console.Write('\n');
|
||||
Write(text);
|
||||
}
|
||||
}
|
||||
else Console.Write(text);
|
||||
}
|
||||
}
|
||||
else Console.Write(text);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write a string to the standard output with a trailing newline
|
||||
/// </summary>
|
||||
public static void WriteLine(string line)
|
||||
{
|
||||
Write(line + '\n');
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write a single character to the standard output
|
||||
/// </summary>
|
||||
public static void Write(char c)
|
||||
{
|
||||
Write("" + c);
|
||||
if (BasicIO)
|
||||
Console.WriteLine(line);
|
||||
else
|
||||
ConsoleInteractive.ConsoleWriter.WriteLine(line);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -322,71 +117,40 @@ namespace MinecraftClient
|
|||
/// If true, "hh-mm-ss" timestamp will be prepended.
|
||||
/// If unspecified, value is retrieved from EnableTimestamps.
|
||||
/// </param>
|
||||
public static void WriteLineFormatted(string str, bool acceptnewlines = true, bool? displayTimestamp = null)
|
||||
public static void WriteLineFormatted(string str, bool acceptnewlines = false, bool? displayTimestamp = null)
|
||||
{
|
||||
StringBuilder output = new StringBuilder();
|
||||
|
||||
if (!String.IsNullOrEmpty(str))
|
||||
{
|
||||
if (!acceptnewlines)
|
||||
if (!acceptnewlines)
|
||||
{
|
||||
str = str.Replace('\n', ' ');
|
||||
output.Append(str.Replace('\n', ' '));
|
||||
}
|
||||
if (displayTimestamp == null)
|
||||
else
|
||||
{
|
||||
output.Append(str);
|
||||
}
|
||||
if (displayTimestamp == null)
|
||||
{
|
||||
displayTimestamp = EnableTimestamps;
|
||||
}
|
||||
if (displayTimestamp.Value)
|
||||
if (displayTimestamp.Value)
|
||||
{
|
||||
int hour = DateTime.Now.Hour, minute = DateTime.Now.Minute, second = DateTime.Now.Second;
|
||||
ConsoleIO.Write(String.Format("{0}:{1}:{2} ", hour.ToString("00"), minute.ToString("00"), second.ToString("00")));
|
||||
output.Append(String.Format("{0}:{1}:{2} ", hour.ToString("00"), minute.ToString("00"), second.ToString("00")));
|
||||
}
|
||||
if (BasicIO)
|
||||
{
|
||||
if (BasicIO_NoColor)
|
||||
{
|
||||
str = ChatBot.GetVerbatim(str);
|
||||
output.Append(ChatBot.GetVerbatim(str));
|
||||
}
|
||||
Console.WriteLine(str);
|
||||
Console.WriteLine(output.ToString());
|
||||
return;
|
||||
}
|
||||
string[] parts = str.Split(new char[] { '§' });
|
||||
if (parts[0].Length > 0)
|
||||
{
|
||||
ConsoleIO.Write(parts[0]);
|
||||
}
|
||||
for (int i = 1; i < parts.Length; i++)
|
||||
{
|
||||
if (parts[i].Length > 0)
|
||||
{
|
||||
switch (parts[i][0])
|
||||
{
|
||||
case '0': Console.ForegroundColor = ConsoleColor.Gray; break; //Should be Black but Black is non-readable on a black background
|
||||
case '1': Console.ForegroundColor = ConsoleColor.DarkBlue; break;
|
||||
case '2': Console.ForegroundColor = ConsoleColor.DarkGreen; break;
|
||||
case '3': Console.ForegroundColor = ConsoleColor.DarkCyan; break;
|
||||
case '4': Console.ForegroundColor = ConsoleColor.DarkRed; break;
|
||||
case '5': Console.ForegroundColor = ConsoleColor.DarkMagenta; break;
|
||||
case '6': Console.ForegroundColor = ConsoleColor.DarkYellow; break;
|
||||
case '7': Console.ForegroundColor = ConsoleColor.Gray; break;
|
||||
case '8': Console.ForegroundColor = ConsoleColor.DarkGray; break;
|
||||
case '9': Console.ForegroundColor = ConsoleColor.Blue; break;
|
||||
case 'a': Console.ForegroundColor = ConsoleColor.Green; break;
|
||||
case 'b': Console.ForegroundColor = ConsoleColor.Cyan; break;
|
||||
case 'c': Console.ForegroundColor = ConsoleColor.Red; break;
|
||||
case 'd': Console.ForegroundColor = ConsoleColor.Magenta; break;
|
||||
case 'e': Console.ForegroundColor = ConsoleColor.Yellow; break;
|
||||
case 'f': Console.ForegroundColor = ConsoleColor.White; break;
|
||||
case 'r': Console.ForegroundColor = ConsoleColor.Gray; break;
|
||||
}
|
||||
|
||||
if (parts[i].Length > 1)
|
||||
{
|
||||
ConsoleIO.Write(parts[i].Substring(1, parts[i].Length - 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
ConsoleInteractive.ConsoleWriter.WriteLineFormatted(output.ToString());
|
||||
}
|
||||
ConsoleIO.Write('\n');
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -408,135 +172,23 @@ namespace MinecraftClient
|
|||
/// </summary>
|
||||
private static void ClearLineAndBuffer()
|
||||
{
|
||||
while (buffer2.Length > 0)
|
||||
{
|
||||
GoRight();
|
||||
}
|
||||
while (buffer.Length > 0)
|
||||
{
|
||||
RemoveOneChar();
|
||||
}
|
||||
if (BasicIO) return;
|
||||
ConsoleInteractive.ConsoleReader.ClearBuffer();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove one character on the left of the cursor in input prompt
|
||||
/// </summary>
|
||||
private static void RemoveOneChar()
|
||||
{
|
||||
if (buffer.Length > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
GoBack();
|
||||
Console.Write(' ');
|
||||
GoBack();
|
||||
}
|
||||
catch (ArgumentOutOfRangeException) { /* Console was resized!? */ }
|
||||
buffer = buffer.Substring(0, buffer.Length - 1);
|
||||
|
||||
if (buffer2.Length > 0)
|
||||
{
|
||||
Console.Write(buffer2);
|
||||
Console.Write(' ');
|
||||
GoBack();
|
||||
for (int i = 0; i < buffer2.Length; i++)
|
||||
{
|
||||
GoBack();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move the cursor one character to the left inside the console, regardless of input prompt state
|
||||
/// </summary>
|
||||
private static void GoBack()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Console.CursorLeft == 0)
|
||||
{
|
||||
Console.CursorLeft = Console.BufferWidth - 1;
|
||||
if (Console.CursorTop > 0)
|
||||
Console.CursorTop--;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.CursorLeft = Console.CursorLeft - 1;
|
||||
}
|
||||
}
|
||||
catch (ArgumentOutOfRangeException) { /* Console was resized!? */ }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move the cursor one character to the left in input prompt, adjusting buffers accordingly
|
||||
/// </summary>
|
||||
private static void GoLeft()
|
||||
{
|
||||
if (buffer.Length > 0)
|
||||
{
|
||||
buffer2 = "" + buffer[buffer.Length - 1] + buffer2;
|
||||
buffer = buffer.Substring(0, buffer.Length - 1);
|
||||
GoBack();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move the cursor one character to the right in input prompt, adjusting buffers accordingly
|
||||
/// </summary>
|
||||
private static void GoRight()
|
||||
{
|
||||
if (buffer2.Length > 0)
|
||||
{
|
||||
buffer = buffer + buffer2[0];
|
||||
Console.Write(buffer2[0]);
|
||||
buffer2 = buffer2.Substring(1);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Insert a new character in the input prompt
|
||||
/// </summary>
|
||||
/// <param name="c">New character</param>
|
||||
private static void AddChar(char c)
|
||||
{
|
||||
Console.Write(c);
|
||||
buffer += c;
|
||||
Console.Write(buffer2);
|
||||
for (int i = 0; i < buffer2.Length; i++)
|
||||
{
|
||||
GoBack();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Clipboard management
|
||||
|
||||
/// <summary>
|
||||
/// Read a string from the Windows clipboard
|
||||
/// </summary>
|
||||
/// <returns>String from the Windows clipboard</returns>
|
||||
private static string ReadClipboard()
|
||||
public static void AutocompleteHandler(object? sender, ConsoleKey e)
|
||||
{
|
||||
string clipdata = "";
|
||||
Thread staThread = new Thread(new ThreadStart(
|
||||
delegate
|
||||
{
|
||||
try
|
||||
{
|
||||
clipdata = Clipboard.GetText();
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
));
|
||||
staThread.SetApartmentState(ApartmentState.STA);
|
||||
staThread.Start();
|
||||
staThread.Join();
|
||||
return clipdata;
|
||||
if (e != ConsoleKey.Tab) return;
|
||||
|
||||
if (autocomplete_engine == null)
|
||||
return;
|
||||
|
||||
var buffer = ConsoleInteractive.ConsoleReader.GetBufferContent();
|
||||
autocomplete_engine.AutoComplete(buffer.Text[..buffer.CursorPosition]);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue