diff --git a/Program.cs b/Program.cs index 04844b9..b72664e 100644 --- a/Program.cs +++ b/Program.cs @@ -3,7 +3,7 @@ using Cogwheel; ICogwheelConsole cogwheelConsole = new DeafultCogwheelConsole(); -CommandsManager commandsManager = new CommandsManager(cogwheelConsole); +CommandsManager commandsManager = new CommandsManager(); COGWHEEL.Initialize(commandsManager, cogwheelConsole); diff --git a/src/COGWHEEL.cs b/src/COGWHEEL.cs index 340c5cb..152bae2 100644 --- a/src/COGWHEEL.cs +++ b/src/COGWHEEL.cs @@ -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); } diff --git a/src/CommandsManager.cs b/src/CommandsManager.cs index 27c291e..74be968 100644 --- a/src/CommandsManager.cs +++ b/src/CommandsManager.cs @@ -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; } = "(?(\\([^\\)]+\\)))|\"(?[^\"]+)\"|'(?[^']+)'|(?[^\\s]+)"; - public string CommandPattern { get; set; } = "(?(\\([^\\)]+\\)))|\"(?[^\"]+)\"|'(?[^']+)'|(?[^\\s]+)"; - public List Assemblies { get; set; } = []; - public Dictionary Commands { get; set; } = new(); - public Dictionary> CustomParsers { get; set; } = new(); + protected virtual List Assemblies { get; set; } = []; + public virtual Dictionary Commands { get; set; } = new(); + public virtual Dictionary> CustomParsers { get; set; } = new(); // Context related stuff - public object? CurrentContext { get; set; } - public Guid? CurrentContextGuid { get; set; } - public Dictionary RegisteredObjectInstances { get; set; } = new(); - public Dictionary RegisteredObjectGuids { get; set; } = new(); - - - public CommandsManager(ICogwheelConsole console) + public virtual object? CurrentContext { get; set; } + public virtual Guid? CurrentContextGuid { get; set; } + public virtual Dictionary RegisteredObjectInstances { get; set; } = new(); + public virtual Dictionary RegisteredObjectGuids { get; set; } = new(); + + public virtual void Initialize(ICogwheelConsole console) { CogwheelConsole = console; - RefreshCommandsList(); CogwheelConsole.Log($"CommandsManager initialized, Commands found: {Commands.Count}"); } - public (ICommand, List)? GetCommandAndArgsFromString(string input) + public virtual (ICommand, List)? 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 args) command) + public virtual bool ExecuteCommand(object obj, (ICommand command, List 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()) {