141 lines
4.2 KiB
C#
141 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using RebootKit.Engine.Foundation;
|
|
using Logger = RebootKit.Engine.Foundation.Logger;
|
|
|
|
namespace RebootKit.Engine.Services.Console {
|
|
public interface IConsoleCommand {
|
|
string Name { get; }
|
|
string Description { get; }
|
|
void Execute(string[] args);
|
|
}
|
|
|
|
public class HelpCommand : IConsoleCommand {
|
|
public string Name { get; } = "help";
|
|
public string Description { get; } = "Prints available commands/cvars and their descriptions.";
|
|
|
|
public void Execute(string[] args) {
|
|
RR.Console.PrintHelp();
|
|
}
|
|
}
|
|
|
|
public class ConsoleService : IService {
|
|
static readonly Logger _logger = new(nameof(ConsoleService));
|
|
|
|
readonly List<IConsoleCommand> _commands = new();
|
|
readonly List<CVar> _cvars = new();
|
|
|
|
public ConsoleService() {
|
|
_logger.Info("Waking up");
|
|
|
|
RegisterCommand(new HelpCommand());
|
|
}
|
|
|
|
public void Dispose() {
|
|
}
|
|
|
|
public event Action<string> OnOutputMessage = _ => { };
|
|
|
|
public void WriteToOutput(string message) {
|
|
OnOutputMessage?.Invoke(message);
|
|
}
|
|
|
|
public bool CVarExists(string name) {
|
|
foreach (CVar cvar in _cvars)
|
|
if (cvar.name.Equals(name))
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
public CVar GetCVar(string name) {
|
|
foreach (CVar cvar in _cvars)
|
|
if (cvar.name.Equals(name))
|
|
return cvar;
|
|
|
|
return null;
|
|
}
|
|
|
|
public CVar ReplaceOrDefault(CVar cvar) {
|
|
CVar foundCVar = GetCVar(cvar.name);
|
|
if (foundCVar != null) return foundCVar;
|
|
|
|
_cvars.Add(cvar);
|
|
return cvar;
|
|
}
|
|
|
|
public void Replace(CVar cvar) {
|
|
CVar foundCVar = GetCVar(cvar.name);
|
|
if (foundCVar != null) _cvars.Remove(foundCVar);
|
|
|
|
_cvars.Add(cvar);
|
|
}
|
|
|
|
string[] ParseCommandInputArguments(string text) {
|
|
if (text.Length == 0) return Array.Empty<string>();
|
|
|
|
return new[] {text};
|
|
}
|
|
|
|
public void Execute(string input) {
|
|
if (input.Length == 0) return;
|
|
|
|
string commandName = input;
|
|
if (input.IndexOf(' ') != -1) commandName = input.Substring(0, input.IndexOf(' '));
|
|
|
|
string[] arguments = ParseCommandInputArguments(input.Substring(commandName.Length));
|
|
|
|
foreach (IConsoleCommand command in _commands)
|
|
if (command.Name.Equals(commandName)) {
|
|
command.Execute(arguments);
|
|
return;
|
|
}
|
|
|
|
foreach (CVar cvar in _cvars)
|
|
if (cvar.name.Equals(commandName)) {
|
|
if (arguments.Length == 1) cvar.ParseFromString(arguments[0]);
|
|
|
|
WriteToOutput($"<b>{cvar.name}</b> - {cvar}\n");
|
|
return;
|
|
}
|
|
|
|
WriteToOutput($"ERROR: Command/CVar `{commandName}` not found.");
|
|
}
|
|
|
|
public void RegisterCommand(IConsoleCommand command) {
|
|
if (_commands.Any(t => t.Name.Equals(command.Name))) {
|
|
_logger.Error($"`{command.Name}` command is already registered");
|
|
return;
|
|
}
|
|
|
|
_commands.Add(command);
|
|
_logger.Info($"Registered command: {command.Name}");
|
|
}
|
|
|
|
public void PrintHelp() {
|
|
StringBuilder message = new();
|
|
|
|
message.AppendLine("Available commands:");
|
|
foreach (IConsoleCommand command in _commands) {
|
|
message.Append(" ");
|
|
message.Append(command.Name);
|
|
message.Append(" - ");
|
|
message.Append(command.Description);
|
|
message.AppendLine();
|
|
}
|
|
|
|
message.AppendLine("Available cvars:");
|
|
foreach (CVar cvar in _cvars) {
|
|
message.Append(" ");
|
|
message.Append(cvar.name);
|
|
message.Append(" - ");
|
|
message.Append(cvar.description);
|
|
message.AppendLine();
|
|
}
|
|
|
|
WriteToOutput(message.ToString());
|
|
}
|
|
}
|
|
} |