console subsystem initialization stuff

This commit is contained in:
GoaLitiuM
2021-09-07 19:36:00 +03:00
parent b68906ada9
commit eb3329f853
6 changed files with 96 additions and 29 deletions

View File

@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cabrito
{
// Holds common miscellaneous Console variables and commands
public static class CommonCommands
{
[ConsoleCommand("")]
public static void NullCommand()
{
}
[ConsoleCommand("echo")]
public static void EchoCommand()
{
Console.Print("nothing");
}
[ConsoleCommand("echo")]
public static void EchoCommand(string[] text)
{
Console.Print(string.Join(" ", text));
}
[ConsoleCommand("debugthrow")]
public static void DebugThrowCommand(string[] text)
{
throw new Exception(string.Join(" ", text));
}
[ConsoleVariable("developer")]
public static string Developer
{
get
{
return Console.DebugVerbosity.ToString();
}
set
{
if (int.TryParse(value, out int intValue) && intValue >= 0)
Console.DebugVerbosity = intValue;
}
}
}
}

View File

@@ -29,6 +29,7 @@ namespace Cabrito
{
Destroy();
instance = new ConsoleInstance();
instance.InitConsoleSubsystems();
}
public static void Destroy()
@@ -69,6 +70,12 @@ namespace Cabrito
}
public static bool ShowExecutedLines => instance.ShowExecutedLines;
public static int DebugVerbosity
{
get => instance.DebugVerbosity;
set => instance.DebugVerbosity = value;
}
public static string LinePrefix => instance.LinePrefix;
public static IReadOnlyCollection<ConsoleLine> Lines => instance.Lines;
@@ -83,7 +90,10 @@ namespace Cabrito
public static void PrintError(string text) => instance.PrintError(text);
// Echoes developer/debug text to Console
public static void PrintDebug(string text) => instance.PrintDebug(text);
public static void PrintDebug(string text) => instance.PrintDebug(1, text);
// Echoes developer/debug text to Console
public static void PrintDebug(int verbosity, string text) => instance.PrintDebug(verbosity, text);
// Opens the Console
public static void Open() => instance.Open();
@@ -115,6 +125,7 @@ namespace Cabrito
public Action<string> OnPrint;
public bool ShowExecutedLines = true;
public int DebugVerbosity { get; set; } = 1;
public string LinePrefix { get; internal set; } = "]";
//private static List<string> consoleLines = new List<string>();
@@ -122,8 +133,12 @@ namespace Cabrito
private Dictionary<string, ConsoleCommand> consoleCommands = new Dictionary<string, ConsoleCommand>();
private Dictionary<string, ConsoleVariable> consoleVariables = new Dictionary<string, ConsoleVariable>();
internal ConsoleInstance()
{
}
// Initializes the Console system.
public ConsoleInstance()
internal void InitConsoleSubsystems()
{
AppDomain currentDomain = AppDomain.CurrentDomain;
Assembly[] assemblies = currentDomain.GetAssemblies();
@@ -149,6 +164,7 @@ namespace Cabrito
{
Dictionary<string, ConsoleCommand> cmdParsed = new Dictionary<string, ConsoleCommand>();
Dictionary<string, List<MethodInfo>> cmdMethods = new Dictionary<string, List<MethodInfo>>();
MethodInfo cmdInitializer = null;
foreach (MethodInfo method in type.GetMethods())
{
@@ -192,6 +208,10 @@ namespace Cabrito
aliasMethods.Add(method);
}
}
else if (attr is ConsoleSubsystemInitializer)
{
cmdInitializer = method;
}
}
}
@@ -249,6 +269,12 @@ namespace Cabrito
}
}
}
if (cmdInitializer != null)
{
Console.PrintDebug(2, "Initializing " + type.Name);
cmdInitializer.Invoke(null, null);
}
}
}
}
@@ -265,7 +291,7 @@ namespace Cabrito
// Echoes text to Console
public void Print(string text)
{
foreach (var line in text.Split(new []{'\n'}))
foreach (var line in text.Split(new[] { '\n' }))
{
ConsoleLine lineEntry = new ConsoleLine(line);
consoleLines.Add(lineEntry);
@@ -301,8 +327,11 @@ namespace Cabrito
}
// Echoes developer/debug text to Console
public void PrintDebug(string text)
public void PrintDebug(int verbosity, string text)
{
if (DebugVerbosity < verbosity)
return;
foreach (var line in text.Split(new[] { '\n' }))
{
ConsoleLine lineEntry = new ConsoleLine(line);

View File

@@ -56,4 +56,13 @@ namespace Cabrito
{
}
}
/// <summary>
/// Constructor for the subsystem, must be called first before registering console commands.
/// </summary>
[AttributeUsage(AttributeTargets.All)]
public class ConsoleSubsystemInitializer : Attribute
{
}
}

View File

@@ -11,8 +11,6 @@ namespace Game
{
Console.Init();
base.Initialize();
Console.Print("ConsolePlugin initialize");
}
public override void Deinitialize()

View File

@@ -33,7 +33,6 @@ namespace Cabrito
public override void OnStart()
{
Console.Print("ConsoleScript OnStart");
consoleInputEvent = new InputEvent("Console");
consoleInputEvent.Triggered += OnConsoleInputEvent;

View File

@@ -7,12 +7,13 @@ using FlaxEngine;
namespace Cabrito
{
// Holds Consol§e variables and commands to control engine behaviour and other common
public static class SystemCommands
// Holds Console variables and commands to control engine behaviour
public static class EngineSubsystem
{
[ConsoleCommand("")]
public static void NullCommand()
[ConsoleSubsystemInitializer]
public static void Initialize()
{
Console.Print("Renderer: " + GPUDevice.Instance.RendererType);
}
[ConsoleCommand("quit", "exit")]
@@ -21,30 +22,12 @@ namespace Cabrito
Engine.RequestExit();
}
[ConsoleCommand("echo")]
public static void EchoCommand()
{
Console.Print("nothing");
}
[ConsoleCommand("echo")]
public static void EchoCommand(string[] text)
{
Console.Print(string.Join(" ", text));
}
[ConsoleCommand("debuglog")]
public static void DebugLogCommand(string[] text)
{
Debug.Log(string.Join(" ", text));
}
[ConsoleCommand("debugthrow")]
public static void DebugThrowCommand(string[] text)
{
throw new Exception(string.Join(" ", text));
}
// TODO: this should manually set all postprocessing values to 0 or disabled
/*[ConsoleVariable("r_postprocessing")]
public static string PostProcessing