Virtualized the CommandsManager rather than using an interface for it

This commit is contained in:
Chris Bell 2025-01-01 22:00:03 -06:00
parent c89cd76396
commit c48543138d
3 changed files with 29 additions and 39 deletions

View File

@ -3,7 +3,7 @@
using Cogwheel;
ICogwheelConsole cogwheelConsole = new DeafultCogwheelConsole();
CommandsManager commandsManager = new CommandsManager(cogwheelConsole);
CommandsManager commandsManager = new CommandsManager();
COGWHEEL.Initialize(commandsManager, cogwheelConsole);

View File

@ -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 CommandsManager _commandsManager;
private static ICogwheelConsole _console;
public static void Initialize(CommandsManager commandsManager, ICogwheelConsole console)
{
_commandsManager = commandsManager;
_console = console;
_commandsManager = commandsManager;
_commandsManager.Initialize(_console);
_console.Initialize(_commandsManager);
}

View File

@ -8,30 +8,28 @@ namespace Cogwheel;
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 CommandsManager(ICogwheelConsole console)
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 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
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);
@ -90,7 +88,7 @@ public class CommandsManager
return false;
}
public string GetCommandUsage(ICommand command)
public virtual string GetCommandUsage(ICommand command)
{
string paramUsage = string.Join(" ",
command.Method.GetParameters().Select(param =>
@ -99,7 +97,7 @@ public class CommandsManager
return $"{command.Name}: {paramUsage}";
}
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 +161,7 @@ public class CommandsManager
}
}
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 +207,14 @@ public class CommandsManager
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 +236,7 @@ public class CommandsManager
}
}
public bool IsCommandContextValid(ICommand command, object? context = null)
public virtual bool IsCommandContextValid(ICommand command, object? context = null)
{
if (context is not null)
{
@ -247,7 +245,7 @@ public class CommandsManager
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 +257,7 @@ public class CommandsManager
return true;
}
public bool SetContext(Guid guid)
public virtual bool SetContext(Guid guid)
{
if (RegisteredObjectInstances.ContainsKey(guid))
{
@ -275,7 +273,7 @@ public class CommandsManager
return true;
}
public bool SetContext(string guidString)
public virtual bool SetContext(string guidString)
{
if (Guid.TryParse(guidString, out var guid))
{
@ -286,12 +284,12 @@ public class CommandsManager
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 +299,7 @@ public class CommandsManager
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 +313,7 @@ public class CommandsManager
return sb.ToString();
}
private void RefreshCommandsList()
protected virtual void RefreshCommandsList()
{
foreach (var type in Assembly.GetCallingAssembly().GetTypes())
{