291 lines
11 KiB
C#
291 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
namespace Cabrito
|
|
{
|
|
public static class Console
|
|
{
|
|
private static ConsoleScriptBase scriptInstance = null;
|
|
|
|
// Returns if Console window open right now.
|
|
public static bool IsOpen { get; internal set; }
|
|
|
|
// For debugging only: Returns true when Console was not closed during the same frame.
|
|
// Needed when Escape-key both closes the console and exits the game.
|
|
public static bool IsSafeToQuit { get { return stopwatch.Elapsed.TotalSeconds > 0.1; } }
|
|
private static Stopwatch stopwatch = Stopwatch.StartNew();
|
|
|
|
// Called when Console is opened.
|
|
public static Action OnOpen;
|
|
|
|
// Called when Console is closed.
|
|
public static Action OnClose;
|
|
|
|
public static bool ShowExecutedLines = true;
|
|
public static string LinePrefix { get; internal set; } = "]";
|
|
|
|
|
|
private static List<string> consoleLines = new List<string>();
|
|
private static Dictionary<string, ConsoleCommand> consoleCommands = new Dictionary<string, ConsoleCommand>();
|
|
private static Dictionary<string, ConsoleVariable> consoleVariables = new Dictionary<string, ConsoleVariable>();
|
|
|
|
// Initializes the Console system.
|
|
public static void Init()
|
|
{
|
|
consoleLines.Clear();
|
|
consoleCommands.Clear();
|
|
consoleVariables.Clear();
|
|
|
|
AppDomain currentDomain = AppDomain.CurrentDomain;
|
|
Assembly[] assemblies = currentDomain.GetAssemblies();
|
|
|
|
foreach (var assembly in assemblies)
|
|
{
|
|
// Skip common assemblies
|
|
var assemblyName = assembly.GetName().Name;
|
|
if (assemblyName == "System" ||
|
|
assemblyName.StartsWith("System.") ||
|
|
assemblyName.StartsWith("Mono.") ||
|
|
assemblyName == "mscorlib" ||
|
|
assemblyName == "Newtonsoft.Json" ||
|
|
assemblyName.StartsWith("FlaxEngine."))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
foreach (var type in assembly.GetTypes())
|
|
{
|
|
Dictionary<string, ConsoleCommand> cmdParsed = new Dictionary<string, ConsoleCommand>();
|
|
Dictionary<string, List<MethodInfo>> cmdMethods = new Dictionary<string, List<MethodInfo>>();
|
|
|
|
foreach (MethodInfo method in type.GetMethods())
|
|
{
|
|
if (!method.IsStatic)
|
|
continue;
|
|
|
|
Attribute[] attributes = Attribute.GetCustomAttributes(method);
|
|
|
|
foreach (Attribute attr in attributes)
|
|
{
|
|
if (attr is ConsoleCommandAttribute cmdAttribute)
|
|
{
|
|
//Console.Print("found cmd '" + cmdAttribute.name + "' bound to field '" + method.Name + "'");
|
|
|
|
// Defer constructing the command until we have parsed all the methods for it in this assembly.
|
|
|
|
List<MethodInfo> methods;
|
|
if (!cmdMethods.TryGetValue(cmdAttribute.name, out methods))
|
|
{
|
|
methods = new List<MethodInfo>();
|
|
cmdMethods.Add(cmdAttribute.name, methods);
|
|
}
|
|
methods.Add(method);
|
|
|
|
ConsoleCommand cmd = new ConsoleCommand(cmdAttribute.name, null);
|
|
if (!cmdParsed.ContainsKey(cmdAttribute.name))
|
|
cmdParsed.Add(cmdAttribute.name, cmd);
|
|
foreach (var alias in cmdAttribute.aliases)
|
|
{
|
|
if (!cmdParsed.ContainsKey(alias))
|
|
cmdParsed.Add(alias, cmd);
|
|
|
|
List<MethodInfo> aliasMethods;
|
|
if (!cmdMethods.TryGetValue(alias, out aliasMethods))
|
|
{
|
|
aliasMethods = new List<MethodInfo>();
|
|
cmdMethods.Add(alias, aliasMethods);
|
|
}
|
|
aliasMethods.Add(method);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var kv in cmdParsed)
|
|
{
|
|
var methods = cmdMethods[kv.Key];
|
|
var definition = kv.Value;
|
|
|
|
ConsoleCommand cmd = new ConsoleCommand(definition.name, methods.ToArray());
|
|
consoleCommands.Add(kv.Key, cmd);
|
|
}
|
|
|
|
foreach (FieldInfo field in type.GetFields())
|
|
{
|
|
if (!field.IsStatic)
|
|
continue;
|
|
|
|
Attribute[] attributes = Attribute.GetCustomAttributes(field);
|
|
|
|
foreach (Attribute attr in attributes)
|
|
{
|
|
if (attr is ConsoleVariableAttribute cvarAttribute)
|
|
{
|
|
//Console.Print("found cvar '" + cvarAttribute.name + "' bound to field '" + field.Name + "'");
|
|
consoleVariables.Add(cvarAttribute.name, new ConsoleVariable(cvarAttribute.name, cvarAttribute.flags, field));
|
|
foreach (var alias in cvarAttribute.aliases)
|
|
consoleVariables.Add(alias, new ConsoleVariable(cvarAttribute.name, cvarAttribute.flags | ConsoleFlags.NoSerialize, field));
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (PropertyInfo prop in type.GetProperties())
|
|
{
|
|
MethodInfo getter = prop.GetGetMethod();
|
|
MethodInfo setter = prop.GetSetMethod();
|
|
if (getter == null || setter == null || !getter.IsStatic || !setter.IsStatic)
|
|
continue;
|
|
|
|
Attribute[] attributes = Attribute.GetCustomAttributes(prop);
|
|
|
|
foreach (Attribute attr in attributes)
|
|
{
|
|
if (attr is ConsoleVariableAttribute cvarAttribute)
|
|
{
|
|
//Console.Print("found cvar '" + cvarAttribute.name + "' bound to field '" + field.Name + "'");
|
|
consoleVariables.Add(cvarAttribute.name, new ConsoleVariable(cvarAttribute.name, cvarAttribute.flags, getter, setter));
|
|
foreach (var alias in cvarAttribute.aliases)
|
|
consoleVariables.Add(alias, new ConsoleVariable(cvarAttribute.name, cvarAttribute.flags | ConsoleFlags.NoSerialize, getter, setter));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Register script for handling the Console frontend.
|
|
internal static void RegisterConsoleScript(ConsoleScriptBase instance)
|
|
{
|
|
if (scriptInstance == instance)
|
|
return;
|
|
|
|
scriptInstance = instance;
|
|
OnOpen += instance.OnConsoleOpen;
|
|
OnClose += instance.OnConsoleClose;
|
|
|
|
foreach (var line in consoleLines)
|
|
scriptInstance.AddLine(line);
|
|
}
|
|
|
|
/// Unregister already registered Console script.
|
|
internal static void UnregisterConsoleScript(ConsoleScriptBase instance)
|
|
{
|
|
if (scriptInstance != instance)
|
|
return;
|
|
|
|
Close();
|
|
|
|
scriptInstance = null;
|
|
OnOpen -= instance.OnConsoleOpen;
|
|
OnClose -= instance.OnConsoleClose;
|
|
}
|
|
|
|
public static IReadOnlyCollection<string> GetLines()
|
|
{
|
|
return consoleLines.AsReadOnly();
|
|
}
|
|
|
|
// Echoes text to Console.
|
|
public static void Print(string text)
|
|
{
|
|
consoleLines.Add(text);
|
|
scriptInstance?.AddLine(text);
|
|
}
|
|
|
|
// Echoes warning text to Console.
|
|
public static void PrintWarning(string text)
|
|
{
|
|
consoleLines.Add(text);
|
|
scriptInstance?.AddLine(text);
|
|
}
|
|
|
|
// Echoes error text to Console.
|
|
public static void PrintError(string text)
|
|
{
|
|
consoleLines.Add(text);
|
|
scriptInstance?.AddLine(text);
|
|
}
|
|
|
|
// Echoes developer/debug text to Console.
|
|
public static void PrintDebug(string text)
|
|
{
|
|
consoleLines.Add(text);
|
|
scriptInstance?.AddLine(text);
|
|
}
|
|
|
|
// Opens the Console.
|
|
public static void Open()
|
|
{
|
|
if (IsOpen)
|
|
return;
|
|
|
|
IsOpen = true;
|
|
OnOpen?.Invoke();
|
|
}
|
|
|
|
// Closes the Console;
|
|
public static void Close()
|
|
{
|
|
if (!IsOpen)
|
|
return;
|
|
|
|
IsOpen = false;
|
|
OnClose?.Invoke();
|
|
|
|
stopwatch.Restart();
|
|
}
|
|
|
|
public static void ClearInput()
|
|
{
|
|
scriptInstance?.SetInput("");
|
|
}
|
|
|
|
public static void Execute(string str)
|
|
{
|
|
str = str.Trim();
|
|
|
|
if (ShowExecutedLines)
|
|
Console.Print(LinePrefix + str);
|
|
|
|
string[] strs = str.Split(' ');
|
|
string execute = strs[0];
|
|
string executeLower = execute.ToLowerInvariant();
|
|
string value = strs.Length > 1 ? str.Substring(execute.Length + 1) : null;
|
|
|
|
//Console.PrintDebug("Executed '" + execute + "' with params: '" + value + "'");
|
|
|
|
if (consoleCommands.TryGetValue(executeLower, out ConsoleCommand cmd))
|
|
{
|
|
string[] values = strs.Skip(1).ToArray();
|
|
if (values.Length > 0)
|
|
cmd.Invoke(values);
|
|
else
|
|
cmd.Invoke();
|
|
//Console.Print("Command bound to '" + execute + "' is '" + cmd.method.Name + "'");
|
|
}
|
|
else if (consoleVariables.TryGetValue(executeLower, out ConsoleVariable cvar))
|
|
{
|
|
if (value != null)
|
|
cvar.SetValue(value);
|
|
|
|
Console.Print("'" + execute + "' is '" + cvar.GetValueString() + "'");
|
|
}
|
|
else
|
|
Console.Print("Unknown command '" + execute + "'");
|
|
}
|
|
|
|
public static string GetVariable(string variableName)
|
|
{
|
|
if (consoleVariables.TryGetValue(variableName, out ConsoleVariable cvar))
|
|
{
|
|
string value = cvar.GetValueString();
|
|
return value;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|