mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2026-08-15 13:04:36 +00:00
462 lines
16 KiB
C#
462 lines
16 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Brigadier.NET;
|
|
using FuzzySharp;
|
|
using MinecraftClient.CommandHandler;
|
|
using MinecraftClient.Scripting;
|
|
using MinecraftClient.Tui;
|
|
using static MinecraftClient.Settings;
|
|
|
|
namespace MinecraftClient
|
|
{
|
|
/// <summary>
|
|
/// Allows simultaneous console input and output without breaking user input
|
|
/// (Without having this annoying behaviour : User inp[Some Console output]ut)
|
|
/// Provide some fancy features such as formatted output, text pasting and tab-completion.
|
|
/// By ORelio - (c) 2012-2018 - Available under the CDDL-1.0 License
|
|
/// </summary>
|
|
public static class ConsoleIO
|
|
{
|
|
private static IAutoComplete? autocomplete_engine;
|
|
|
|
/// <summary>
|
|
/// The active console backend. Set once during startup.
|
|
/// </summary>
|
|
public static IConsoleBackend Backend { get; set; } = null!;
|
|
|
|
/// <summary>
|
|
/// Reset the IO mechanism and clear all buffers
|
|
/// </summary>
|
|
public static void Reset()
|
|
{
|
|
Backend?.ClearInputBuffer();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set an auto-completion engine for TAB autocompletion.
|
|
/// </summary>
|
|
/// <param name="engine">Engine implementing the IAutoComplete interface</param>
|
|
public static void SetAutoCompleteEngine(IAutoComplete engine)
|
|
{
|
|
autocomplete_engine = engine;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines whether to use basic IO (legacy flag, kept for compatibility).
|
|
/// In the new architecture this is true when Backend is BasicConsoleBackend.
|
|
/// </summary>
|
|
public static bool BasicIO = false;
|
|
|
|
/// <summary>
|
|
/// Determines whether not to print color codes in BasicIO mode.
|
|
/// </summary>
|
|
public static bool BasicIO_NoColor = false;
|
|
|
|
/// <summary>
|
|
/// Determine whether WriteLineFormatted() should prepend lines with timestamps by default.
|
|
/// </summary>
|
|
public static bool EnableTimestamps = false;
|
|
|
|
/// <summary>
|
|
/// Specify a generic log line prefix for WriteLogLine()
|
|
/// </summary>
|
|
public static string LogPrefix = "§8[MCC] ";
|
|
|
|
/// <summary>
|
|
/// Read a password from the standard input
|
|
/// </summary>
|
|
public static string? ReadPassword()
|
|
{
|
|
if (BasicIO)
|
|
return Console.ReadLine();
|
|
return Backend.ReadPassword();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Read a line from the standard input
|
|
/// </summary>
|
|
public static string ReadLine()
|
|
{
|
|
if (BasicIO)
|
|
return Console.ReadLine() ?? String.Empty;
|
|
return Backend.RequestImmediateInput();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Debug routine: print all keys pressed in the console
|
|
/// </summary>
|
|
public static void DebugReadInput()
|
|
{
|
|
ConsoleKeyInfo k;
|
|
while (true)
|
|
{
|
|
k = Console.ReadKey(true);
|
|
Console.WriteLine("Key: {0}\tChar: {1}\tModifiers: {2}", k.Key, k.KeyChar, k.Modifiers);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Write a string to the standard output with a trailing newline
|
|
/// </summary>
|
|
public static void WriteLine(string line)
|
|
{
|
|
if (BasicIO || Backend is null)
|
|
Console.WriteLine(line);
|
|
else
|
|
Backend.WriteLine(line);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Write a Minecraft-Like formatted string to the standard output, using §c color codes
|
|
/// <see href="https://minecraft.wiki/w/Classic_server_protocol#Color_codes"/> for more info
|
|
/// </summary>
|
|
/// <param name="str">String to write</param>
|
|
/// <param name="acceptnewlines">If false, space are printed instead of newlines</param>
|
|
/// <param name="displayTimestamp">
|
|
/// If false, no timestamp is prepended.
|
|
/// 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 = false, bool? displayTimestamp = null)
|
|
{
|
|
StringBuilder output = new();
|
|
|
|
if (!String.IsNullOrEmpty(str))
|
|
{
|
|
displayTimestamp ??= EnableTimestamps;
|
|
if (displayTimestamp.Value)
|
|
{
|
|
int hour = DateTime.Now.Hour, minute = DateTime.Now.Minute, second = DateTime.Now.Second;
|
|
output.Append(String.Format("{0}:{1}:{2} ", hour.ToString("00"), minute.ToString("00"), second.ToString("00")));
|
|
}
|
|
if (!acceptnewlines)
|
|
{
|
|
str = str.Replace('\n', ' ');
|
|
}
|
|
if (BasicIO || Backend is null)
|
|
{
|
|
if (BasicIO_NoColor)
|
|
{
|
|
output.Append(ChatBot.GetVerbatim(str));
|
|
}
|
|
else
|
|
{
|
|
output.Append(str);
|
|
}
|
|
Console.WriteLine(output.ToString());
|
|
return;
|
|
}
|
|
output.Append(str);
|
|
Backend.WriteLineFormatted(output.ToString());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Write a prefixed log line. Prefix is set in LogPrefix.
|
|
/// </summary>
|
|
/// <param name="text">Text of the log line</param>
|
|
/// <param name="acceptnewlines">Allow line breaks</param>
|
|
public static void WriteLogLine(string text, bool acceptnewlines = true)
|
|
{
|
|
if (!acceptnewlines)
|
|
text = text.Replace('\n', ' ');
|
|
WriteLineFormatted(LogPrefix + text, acceptnewlines);
|
|
}
|
|
|
|
#region Subfunctions
|
|
|
|
/// <summary>
|
|
/// Clear all text inside the input prompt
|
|
/// </summary>
|
|
private static void ClearLineAndBuffer()
|
|
{
|
|
if (BasicIO) return;
|
|
Backend.ClearInputBuffer();
|
|
}
|
|
|
|
#endregion
|
|
|
|
internal static bool AutoCompleteDone = false;
|
|
internal static string[] AutoCompleteResult = Array.Empty<string>();
|
|
|
|
private static HashSet<string> Commands = new();
|
|
private static string[] CommandsFromAutoComplete = Array.Empty<string>();
|
|
private static string[] CommandsFromDeclareCommands = Array.Empty<string>();
|
|
|
|
private static Task _latestTask = Task.CompletedTask;
|
|
private static CancellationTokenSource? _cancellationTokenSource;
|
|
|
|
private static void SendSuggestions(
|
|
ConsoleInteractive.ConsoleSuggestion.Suggestion[] classicSugs,
|
|
Tuple<int, int> range)
|
|
{
|
|
if (Backend is ClassicConsoleBackend classic)
|
|
{
|
|
classic.UpdateSuggestions(classicSugs, range);
|
|
}
|
|
else if (Backend is TuiConsoleBackend tui)
|
|
{
|
|
var tuiSugs = new CommandSuggestion[classicSugs.Length];
|
|
for (int i = 0; i < classicSugs.Length; i++)
|
|
tuiSugs[i] = new CommandSuggestion(classicSugs[i].Text, classicSugs[i].Tooltip);
|
|
tui.UpdateSuggestions(tuiSugs, (range.Item1, range.Item2));
|
|
}
|
|
}
|
|
|
|
private static void DoClearSuggestions()
|
|
{
|
|
if (Backend is ClassicConsoleBackend classic)
|
|
classic.ClearSuggestions();
|
|
else if (Backend is TuiConsoleBackend tui)
|
|
tui.ClearSuggestions();
|
|
}
|
|
|
|
private static void MccAutocompleteHandler(ConsoleInputBuffer buffer)
|
|
{
|
|
string fullCommand = buffer.Text;
|
|
if (string.IsNullOrEmpty(fullCommand))
|
|
{
|
|
DoClearSuggestions();
|
|
return;
|
|
}
|
|
|
|
var InternalCmdChar = Config.Main.Advanced.InternalCmdChar;
|
|
if (InternalCmdChar == MainConfigHelper.MainConfig.AdvancedConfig.InternalCmdCharType.none || fullCommand[0] == InternalCmdChar.ToChar())
|
|
{
|
|
int offset = InternalCmdChar == MainConfigHelper.MainConfig.AdvancedConfig.InternalCmdCharType.none ? 0 : 1;
|
|
if (buffer.CursorPosition - offset < 0)
|
|
{
|
|
DoClearSuggestions();
|
|
return;
|
|
}
|
|
|
|
_cancellationTokenSource?.Cancel();
|
|
var cts = new CancellationTokenSource();
|
|
_cancellationTokenSource = cts;
|
|
Task newTask = UpdateSuggestionsAsync(fullCommand, offset, buffer.CursorPosition, cts.Token);
|
|
_latestTask = newTask;
|
|
_ = ObserveAutocompleteTaskAsync(newTask, cts);
|
|
}
|
|
else
|
|
{
|
|
DoClearSuggestions();
|
|
return;
|
|
}
|
|
}
|
|
|
|
private static async Task UpdateSuggestionsAsync(string fullCommand, int offset, int cursorPosition, CancellationToken cancellationToken)
|
|
{
|
|
string command = fullCommand[offset..];
|
|
if (command.Length == 0)
|
|
{
|
|
List<ConsoleInteractive.ConsoleSuggestion.Suggestion> suggestionList = new()
|
|
{
|
|
new("/")
|
|
};
|
|
|
|
var childs = McClient.dispatcher.GetRoot().Children;
|
|
if (childs is not null)
|
|
{
|
|
foreach (var child in childs)
|
|
suggestionList.Add(new(child.Name));
|
|
}
|
|
|
|
foreach (var cmd in Commands)
|
|
suggestionList.Add(new(cmd));
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
return;
|
|
|
|
SendSuggestions(suggestionList.ToArray(), new(offset, offset));
|
|
return;
|
|
}
|
|
|
|
if (command[0] == '/' && !command.Contains(' '))
|
|
{
|
|
var sorted = Process.ExtractSorted(command[1..], Commands);
|
|
var suggestionList = new ConsoleInteractive.ConsoleSuggestion.Suggestion[sorted.Count()];
|
|
|
|
int index = 0;
|
|
foreach (var suggestion in sorted)
|
|
suggestionList[index++] = new(suggestion.Value);
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
return;
|
|
|
|
SendSuggestions(suggestionList, new(offset, offset + command.Length));
|
|
return;
|
|
}
|
|
|
|
CommandDispatcher<CmdResult>? dispatcher = McClient.dispatcher;
|
|
if (dispatcher is null)
|
|
return;
|
|
|
|
ParseResults<CmdResult> parse = dispatcher.Parse(command, CmdResult.Empty);
|
|
Brigadier.NET.Suggestion.Suggestions suggestions =
|
|
await dispatcher.GetCompletionSuggestions(parse, cursorPosition - offset);
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
return;
|
|
|
|
int suggestionCount = suggestions.List.Count;
|
|
if (suggestionCount == 0)
|
|
{
|
|
DoClearSuggestions();
|
|
return;
|
|
}
|
|
|
|
Dictionary<string, string?> tooltips = new();
|
|
foreach (var suggestion in suggestions.List)
|
|
tooltips.Add(suggestion.Text, suggestion.Tooltip?.String);
|
|
|
|
Tuple<int, int> range = new(suggestions.Range.Start + offset, suggestions.Range.End + offset);
|
|
var sortedSuggestions = Process.ExtractSorted(fullCommand[range.Item1..range.Item2], tooltips.Keys);
|
|
if (cancellationToken.IsCancellationRequested)
|
|
return;
|
|
|
|
var suggestionListWithTooltips = new ConsoleInteractive.ConsoleSuggestion.Suggestion[suggestionCount];
|
|
int suggestionIndex = 0;
|
|
foreach (var suggestion in sortedSuggestions)
|
|
suggestionListWithTooltips[suggestionIndex++] = new(suggestion.Value, tooltips[suggestion.Value] ?? string.Empty);
|
|
|
|
SendSuggestions(suggestionListWithTooltips, range);
|
|
}
|
|
|
|
private static async Task ObserveAutocompleteTaskAsync(Task task, CancellationTokenSource cancellationTokenSource)
|
|
{
|
|
try
|
|
{
|
|
await task;
|
|
}
|
|
catch (OperationCanceledException) when (cancellationTokenSource.IsCancellationRequested)
|
|
{
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if (Settings.Config.Logging.DebugMessages)
|
|
WriteLogLine(e.ToString(), acceptnewlines: true);
|
|
DoClearSuggestions();
|
|
}
|
|
finally
|
|
{
|
|
if (ReferenceEquals(_cancellationTokenSource, cancellationTokenSource))
|
|
_cancellationTokenSource = null;
|
|
|
|
cancellationTokenSource.Dispose();
|
|
}
|
|
}
|
|
|
|
public static void AutocompleteHandler(object? sender, ConsoleInputBuffer buffer)
|
|
{
|
|
if (Settings.Config.Console.CommandSuggestion.Enable)
|
|
MccAutocompleteHandler(buffer);
|
|
}
|
|
|
|
private static readonly string[] OfflineCommands = ["quit", "exit", "connect", "reco", "help"];
|
|
|
|
public static void OfflineAutocompleteHandler(object? sender, ConsoleInputBuffer buffer)
|
|
{
|
|
if (!Settings.Config.Console.CommandSuggestion.Enable)
|
|
return;
|
|
|
|
string fullCommand = buffer.Text;
|
|
if (string.IsNullOrEmpty(fullCommand))
|
|
{
|
|
DoClearSuggestions();
|
|
return;
|
|
}
|
|
|
|
var InternalCmdChar = Config.Main.Advanced.InternalCmdChar;
|
|
int offset = 0;
|
|
if (InternalCmdChar != MainConfigHelper.MainConfig.AdvancedConfig.InternalCmdCharType.none)
|
|
{
|
|
if (fullCommand[0] != InternalCmdChar.ToChar())
|
|
{
|
|
DoClearSuggestions();
|
|
return;
|
|
}
|
|
offset = 1;
|
|
}
|
|
|
|
string command = fullCommand[offset..];
|
|
if (command.Contains(' '))
|
|
{
|
|
DoClearSuggestions();
|
|
return;
|
|
}
|
|
|
|
var sugList = new List<ConsoleInteractive.ConsoleSuggestion.Suggestion>();
|
|
foreach (string cmd in OfflineCommands)
|
|
{
|
|
if (command.Length == 0 || cmd.StartsWith(command, StringComparison.OrdinalIgnoreCase))
|
|
sugList.Add(new(cmd));
|
|
}
|
|
|
|
if (sugList.Count > 0)
|
|
SendSuggestions(sugList.ToArray(), new(offset, offset + command.Length));
|
|
else
|
|
DoClearSuggestions();
|
|
}
|
|
|
|
public static void CancelAutocomplete()
|
|
{
|
|
_cancellationTokenSource?.Cancel();
|
|
_latestTask = Task.CompletedTask;
|
|
DoClearSuggestions();
|
|
|
|
AutoCompleteDone = false;
|
|
AutoCompleteResult = Array.Empty<string>();
|
|
CommandsFromAutoComplete = Array.Empty<string>();
|
|
CommandsFromDeclareCommands = Array.Empty<string>();
|
|
}
|
|
|
|
private static void MergeCommands()
|
|
{
|
|
Commands.Clear();
|
|
foreach (string cmd in CommandsFromAutoComplete)
|
|
Commands.Add('/' + cmd);
|
|
foreach (string cmd in CommandsFromDeclareCommands)
|
|
Commands.Add('/' + cmd);
|
|
}
|
|
|
|
public static void OnAutoCompleteDone(int transactionId, string[] result)
|
|
{
|
|
AutoCompleteResult = result;
|
|
if (transactionId == 0)
|
|
{
|
|
CommandsFromAutoComplete = result;
|
|
MergeCommands();
|
|
}
|
|
AutoCompleteDone = true;
|
|
}
|
|
|
|
public static void OnDeclareMinecraftCommand(string[] rootCommands)
|
|
{
|
|
CommandsFromDeclareCommands = rootCommands;
|
|
MergeCommands();
|
|
}
|
|
|
|
public static void InitCommandList(CommandDispatcher<CmdResult> dispatcher)
|
|
{
|
|
autocomplete_engine!.AutoComplete("/");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Interface for TAB autocompletion
|
|
/// Allows to use any object which has an AutoComplete() method using the IAutocomplete interface
|
|
/// </summary>
|
|
public interface IAutoComplete
|
|
{
|
|
/// <summary>
|
|
/// Provide a list of auto-complete strings based on the provided input behing the cursor
|
|
/// </summary>
|
|
/// <param name="BehindCursor">Text behind the cursor, e.g. "my input comm"</param>
|
|
/// <returns>List of auto-complete words, e.g. ["command", "comment"]</returns>
|
|
int AutoComplete(string BehindCursor);
|
|
}
|
|
}
|