Compare commits
3 Commits
83bc55526a
...
22b25cd8a8
Author | SHA1 | Date | |
---|---|---|---|
22b25cd8a8 | |||
c48543138d | |||
c89cd76396 |
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
@ -8,15 +8,15 @@ public class DeafultCogwheelConsole : ICogwheelConsole
|
||||
{
|
||||
public string OpeningMessage { get; set; } = "** COGWHEEL CONSOLE **";
|
||||
public bool IsRunning { get; set; }
|
||||
public ICommandsManager CommandsManager { get; set; }
|
||||
public CommandsManager CommandsManager { get; set; }
|
||||
|
||||
|
||||
public void Initialize(ICommandsManager commandsManager)
|
||||
public void Initialize(CommandsManager commandsManager)
|
||||
{
|
||||
CommandsManager = commandsManager;
|
||||
CommandsManager.RegisterObject(this);
|
||||
|
||||
Log(OpeningMessage);
|
||||
Write(OpeningMessage);
|
||||
|
||||
IsRunning = true;
|
||||
while (IsRunning)
|
||||
@ -42,6 +42,11 @@ public class DeafultCogwheelConsole : ICogwheelConsole
|
||||
Console.WriteLine($"[COGWHEEL WARNING] {message}");
|
||||
}
|
||||
|
||||
public void Write(string message)
|
||||
{
|
||||
Console.WriteLine(message);
|
||||
}
|
||||
|
||||
public void ClearConsole()
|
||||
{
|
||||
Console.Clear();
|
||||
|
@ -3,7 +3,7 @@
|
||||
using Cogwheel;
|
||||
|
||||
ICogwheelConsole cogwheelConsole = new DeafultCogwheelConsole();
|
||||
ICommandsManager commandsManager = new CommandsManager(cogwheelConsole);
|
||||
CommandsManager commandsManager = new CommandsManager();
|
||||
|
||||
COGWHEEL.Initialize(commandsManager, cogwheelConsole);
|
||||
|
||||
|
@ -2,23 +2,15 @@ namespace Cogwheel;
|
||||
|
||||
public static class COGWHEEL
|
||||
{
|
||||
/* TODO:
|
||||
* -[X] Initialize the CommandsManager
|
||||
* -[ ] Public static methods for the CommandsManager
|
||||
* -[X] Create built-in commands
|
||||
* -[ ]
|
||||
* -[ ]
|
||||
* -[ ]
|
||||
*/
|
||||
|
||||
private static ICommandsManager _commandsManager;
|
||||
private static CommandsManager _commandsManager;
|
||||
private static ICogwheelConsole _console;
|
||||
|
||||
public static void Initialize(ICommandsManager commandsManager, ICogwheelConsole console)
|
||||
public static void Initialize(CommandsManager commandsManager, ICogwheelConsole console)
|
||||
{
|
||||
_commandsManager = commandsManager;
|
||||
_console = console;
|
||||
_commandsManager = commandsManager;
|
||||
|
||||
_commandsManager.Initialize(_console);
|
||||
_console.Initialize(_commandsManager);
|
||||
}
|
||||
|
||||
@ -51,6 +43,11 @@ public static class COGWHEEL
|
||||
_console.LogWarning(message);
|
||||
}
|
||||
|
||||
public static void Write(string message)
|
||||
{
|
||||
_console.Write(message);
|
||||
}
|
||||
|
||||
// == Built-in commands == //
|
||||
[Command(Name = "quit", Description = "Quits the Cogwheel console.")]
|
||||
public static void QuitCogwheelConsole()
|
||||
@ -84,7 +81,7 @@ public static class COGWHEEL
|
||||
{
|
||||
foreach (var command in _commandsManager.Commands)
|
||||
{
|
||||
_console.Log($"{command.Key} - {command.Value.Description}");
|
||||
Write($"{_commandsManager.GetCommandUsage(command.Value)}");
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,7 +91,7 @@ public static class COGWHEEL
|
||||
_console.Log($"Current context: {_commandsManager.CurrentContext.GetType()} : {_commandsManager.CurrentContextGuid}");
|
||||
}
|
||||
|
||||
[Command(Name = "test")]
|
||||
[Command(Name = "test", Description = "Creates a new TestClass object.")]
|
||||
public static void CreateTestObject(string name)
|
||||
{
|
||||
_console.Log($"Creating new TestClass object with name: {name}");
|
||||
@ -118,7 +115,7 @@ public static class COGWHEEL
|
||||
Log("Available registered objects:");
|
||||
foreach (var (guid, obj) in filteredObjects)
|
||||
{
|
||||
Log($"- {obj.GetType().FullName} : {guid}");
|
||||
Write($"- {obj.GetType().FullName} : {guid}");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,32 +6,30 @@ using Exception = System.Exception;
|
||||
|
||||
namespace Cogwheel;
|
||||
|
||||
public class CommandsManager : ICommandsManager
|
||||
public class CommandsManager
|
||||
{
|
||||
public ICogwheelConsole CogwheelConsole { get; set; }
|
||||
protected virtual ICogwheelConsole CogwheelConsole { get; set; }
|
||||
protected virtual string CommandPattern { get; set; } = "(?<val>(\\([^\\)]+\\)))|\"(?<val>[^\"]+)\"|'(?<val>[^']+)'|(?<val>[^\\s]+)";
|
||||
|
||||
public string CommandPattern { get; set; } = "(?<val>(\\([^\\)]+\\)))|\"(?<val>[^\"]+)\"|'(?<val>[^']+)'|(?<val>[^\\s]+)";
|
||||
public List<Assembly> Assemblies { get; set; } = [];
|
||||
public Dictionary<string, ICommand> Commands { get; set; } = new();
|
||||
public Dictionary<Type, Func<string, object>> CustomParsers { get; set; } = new();
|
||||
protected virtual List<Assembly> Assemblies { get; set; } = [];
|
||||
public virtual Dictionary<string, ICommand> Commands { get; set; } = new();
|
||||
public virtual Dictionary<Type, Func<string, object>> CustomParsers { get; set; } = new();
|
||||
|
||||
// Context related stuff
|
||||
public object? CurrentContext { get; set; }
|
||||
public Guid? CurrentContextGuid { get; set; }
|
||||
public Dictionary<Guid, object> RegisteredObjectInstances { get; set; } = new();
|
||||
public Dictionary<object, Guid> RegisteredObjectGuids { get; set; } = new();
|
||||
public virtual object? CurrentContext { get; set; }
|
||||
public virtual Guid? CurrentContextGuid { get; set; }
|
||||
public virtual Dictionary<Guid, object> RegisteredObjectInstances { get; set; } = new();
|
||||
public virtual Dictionary<object, Guid> RegisteredObjectGuids { get; set; } = new();
|
||||
|
||||
|
||||
public CommandsManager(ICogwheelConsole console)
|
||||
public virtual void Initialize(ICogwheelConsole console)
|
||||
{
|
||||
CogwheelConsole = console;
|
||||
|
||||
RefreshCommandsList();
|
||||
CogwheelConsole.Log($"CommandsManager initialized, Commands found: {Commands.Count}");
|
||||
}
|
||||
|
||||
|
||||
public (ICommand, List<object>)? GetCommandAndArgsFromString(string input)
|
||||
public virtual (ICommand, List<object>)? GetCommandAndArgsFromString(string input)
|
||||
{
|
||||
var splitString = Regex.Matches(input, CommandPattern).Select(m => m.Groups["val"].Value).ToArray();
|
||||
var commandName = splitString[0];
|
||||
@ -49,7 +47,7 @@ public class CommandsManager : ICommandsManager
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool RunCommand(string input, object? context = null)
|
||||
public virtual bool RunCommand(string input, object? context = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input)) return false;
|
||||
var command = GetCommandAndArgsFromString(input);
|
||||
@ -74,7 +72,7 @@ public class CommandsManager : ICommandsManager
|
||||
return ExecuteCommand(CurrentContext, command.Value);
|
||||
}
|
||||
|
||||
CogwheelConsole.LogWarning($"Command '{command.Value.Item1.Name}' is not static and no valid context was provided, searching for first registered context of type '{command.Value.Item1.Method.DeclaringType}'");
|
||||
//CogwheelConsole.LogWarning($"Command '{command.Value.Item1.Name}' is not static and no valid context was provided, searching for first registered context of type '{command.Value.Item1.Method.DeclaringType}'");
|
||||
context = GetFirstValidRegisteredContext(command.Value.Item1.Method.DeclaringType);
|
||||
if (context is null)
|
||||
{
|
||||
@ -90,16 +88,38 @@ public class CommandsManager : ICommandsManager
|
||||
return false;
|
||||
}
|
||||
|
||||
public string GetCommandUsage(ICommand command)
|
||||
public virtual string GetCommandUsage(ICommand command)
|
||||
{
|
||||
string output = "";
|
||||
if (!command.Method.IsStatic)
|
||||
{
|
||||
output += $"|{command.Method.DeclaringType?.FullName}| ";
|
||||
}
|
||||
else
|
||||
{
|
||||
output += "|Global| ";
|
||||
}
|
||||
|
||||
output += $"{command.Name}: ";
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(command.Description))
|
||||
{
|
||||
output += $"{command.Description} ";
|
||||
}
|
||||
|
||||
string paramUsage = string.Join(" ",
|
||||
command.Method.GetParameters().Select(param =>
|
||||
$"<{(param.IsDefined(typeof(ParamArrayAttribute)) ? "params " : "")}{param}>"));
|
||||
|
||||
return $"{command.Name}: {paramUsage}";
|
||||
if (!string.IsNullOrWhiteSpace(paramUsage))
|
||||
{
|
||||
output += $"- {paramUsage}";
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
public bool ExecuteCommand(object obj, (ICommand command, List<object> args) command)
|
||||
public virtual bool ExecuteCommand(object obj, (ICommand command, List<object> args) command)
|
||||
{
|
||||
var parameters = command.command.Method.GetParameters();
|
||||
for (var parameterIndex = 0; parameterIndex < parameters.Length; parameterIndex++)
|
||||
@ -163,7 +183,7 @@ public class CommandsManager : ICommandsManager
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryParseParameter(Type parameterType, string parameterString, out object parsedValue)
|
||||
public virtual bool TryParseParameter(Type parameterType, string parameterString, out object parsedValue)
|
||||
{
|
||||
if (parameterType == typeof(string))
|
||||
{
|
||||
@ -209,14 +229,14 @@ public class CommandsManager : ICommandsManager
|
||||
return false;
|
||||
}
|
||||
|
||||
public ICommand? GetCommandByName(string commandName)
|
||||
public virtual ICommand? GetCommandByName(string commandName)
|
||||
{
|
||||
return Commands.GetValueOrDefault(commandName);
|
||||
}
|
||||
|
||||
// Context related stuff
|
||||
|
||||
public void RegisterObject(object obj)
|
||||
public virtual void RegisterObject(object obj)
|
||||
{
|
||||
// Use a combination of the object's type and a hash of its properties to generate a deterministic GUID
|
||||
string seed = obj.GetType().FullName + GetObjectPropertiesHash(obj);
|
||||
@ -238,7 +258,7 @@ public class CommandsManager : ICommandsManager
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsCommandContextValid(ICommand command, object? context = null)
|
||||
public virtual bool IsCommandContextValid(ICommand command, object? context = null)
|
||||
{
|
||||
if (context is not null)
|
||||
{
|
||||
@ -247,7 +267,7 @@ public class CommandsManager : ICommandsManager
|
||||
return command.Method.DeclaringType == CurrentContext?.GetType();
|
||||
}
|
||||
|
||||
public bool SetContext(object context)
|
||||
public virtual bool SetContext(object context)
|
||||
{
|
||||
CurrentContext = context;
|
||||
if (!RegisteredObjectGuids.ContainsKey(context))
|
||||
@ -259,7 +279,7 @@ public class CommandsManager : ICommandsManager
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool SetContext(Guid guid)
|
||||
public virtual bool SetContext(Guid guid)
|
||||
{
|
||||
if (RegisteredObjectInstances.ContainsKey(guid))
|
||||
{
|
||||
@ -275,7 +295,7 @@ public class CommandsManager : ICommandsManager
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool SetContext(string guidString)
|
||||
public virtual bool SetContext(string guidString)
|
||||
{
|
||||
if (Guid.TryParse(guidString, out var guid))
|
||||
{
|
||||
@ -286,12 +306,12 @@ public class CommandsManager : ICommandsManager
|
||||
return false;
|
||||
}
|
||||
|
||||
public object? GetFirstValidRegisteredContext(Type type)
|
||||
public virtual object? GetFirstValidRegisteredContext(Type type)
|
||||
{
|
||||
return RegisteredObjectInstances.Values.FirstOrDefault(obj => obj.GetType() == type);
|
||||
}
|
||||
|
||||
public Guid GetGuidFromContext(object? context = null)
|
||||
public virtual Guid GetGuidFromContext(object? context = null)
|
||||
{
|
||||
if (context is not null)
|
||||
{
|
||||
@ -301,7 +321,7 @@ public class CommandsManager : ICommandsManager
|
||||
return CurrentContextGuid ?? Guid.Empty;
|
||||
}
|
||||
|
||||
private string GetObjectPropertiesHash(object obj)
|
||||
protected virtual string GetObjectPropertiesHash(object obj)
|
||||
{
|
||||
var properties = obj.GetType().GetProperties();
|
||||
var sb = new StringBuilder();
|
||||
@ -315,7 +335,7 @@ public class CommandsManager : ICommandsManager
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private void RefreshCommandsList()
|
||||
protected virtual void RefreshCommandsList()
|
||||
{
|
||||
foreach (var type in Assembly.GetCallingAssembly().GetTypes())
|
||||
{
|
||||
|
@ -4,12 +4,13 @@ public interface ICogwheelConsole
|
||||
{
|
||||
public string OpeningMessage { get; set; }
|
||||
public bool IsRunning { get; set; }
|
||||
public ICommandsManager CommandsManager { get; set; }
|
||||
public CommandsManager CommandsManager { get; set; }
|
||||
|
||||
public void Initialize(ICommandsManager commandsManager); // Make sure to pass the CommandsManager instance to the Console
|
||||
public void Initialize(CommandsManager commandsManager); // Make sure to pass the CommandsManager instance to the Console
|
||||
public void Log(string message);
|
||||
public void LogError(string message);
|
||||
public void LogWarning(string message);
|
||||
public void Write(string message);
|
||||
public void ClearConsole();
|
||||
public void Exit();
|
||||
|
||||
|
@ -1,33 +0,0 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace Cogwheel;
|
||||
|
||||
public interface ICommandsManager
|
||||
{
|
||||
public ICogwheelConsole CogwheelConsole { get; set; }
|
||||
public string CommandPattern { get; set; }
|
||||
public List<Assembly> Assemblies { get; set; }
|
||||
public Dictionary<string, ICommand> Commands { get; set; }
|
||||
public Dictionary<Type, Func<string, object>> CustomParsers { get; set; }
|
||||
public object? CurrentContext { get; set; }
|
||||
public Guid? CurrentContextGuid { get; set; }
|
||||
public Dictionary<Guid, object> RegisteredObjectInstances { get; set; }
|
||||
public Dictionary<object, Guid> RegisteredObjectGuids { get; set; }
|
||||
|
||||
|
||||
public void RegisterObject(object obj);
|
||||
public (ICommand, List<object>)? GetCommandAndArgsFromString(string input);
|
||||
public bool RunCommand(string input, object? context = null);
|
||||
public string GetCommandUsage(ICommand command);
|
||||
public bool ExecuteCommand(object obj, (ICommand command, List<object> args) command);
|
||||
public bool TryParseParameter(Type parameterType, string parameterString, out object parsedValue);
|
||||
public ICommand? GetCommandByName(string commandName);
|
||||
|
||||
// Context related stuff
|
||||
public bool IsCommandContextValid(ICommand command, object? context = null);
|
||||
public bool SetContext(object context);
|
||||
public bool SetContext(Guid guid);
|
||||
public bool SetContext(string guidString);
|
||||
public object? GetFirstValidRegisteredContext(Type type);
|
||||
public Guid GetGuidFromContext(object? context = null);
|
||||
}
|
Loading…
Reference in New Issue
Block a user